cgroup.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /* bits in struct cgroup flags field */
  77. enum {
  78. /* Control Group is dead */
  79. CGRP_REMOVED,
  80. /* Control Group has previously had a child cgroup or a task,
  81. * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) */
  82. CGRP_RELEASABLE,
  83. /* Control Group requires release notifications to userspace */
  84. CGRP_NOTIFY_ON_RELEASE,
  85. };
  86. struct cgroup {
  87. unsigned long flags; /* "unsigned long" so bitops work */
  88. /* count users of this cgroup. >0 means busy, but doesn't
  89. * necessarily indicate the number of tasks in the
  90. * cgroup */
  91. atomic_t count;
  92. /*
  93. * We link our 'sibling' struct into our parent's 'children'.
  94. * Our children link their 'sibling' into our 'children'.
  95. */
  96. struct list_head sibling; /* my parent's children */
  97. struct list_head children; /* my children */
  98. struct cgroup *parent; /* my parent */
  99. struct dentry *dentry; /* cgroup fs entry */
  100. /* Private pointers for each registered subsystem */
  101. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  102. struct cgroupfs_root *root;
  103. struct cgroup *top_cgroup;
  104. /*
  105. * List of cg_cgroup_links pointing at css_sets with
  106. * tasks in this cgroup. Protected by css_set_lock
  107. */
  108. struct list_head css_sets;
  109. /*
  110. * Linked list running through all cgroups that can
  111. * potentially be reaped by the release agent. Protected by
  112. * release_list_lock
  113. */
  114. struct list_head release_list;
  115. };
  116. /* A css_set is a structure holding pointers to a set of
  117. * cgroup_subsys_state objects. This saves space in the task struct
  118. * object and speeds up fork()/exit(), since a single inc/dec and a
  119. * list_add()/del() can bump the reference count on the entire
  120. * cgroup set for a task.
  121. */
  122. struct css_set {
  123. /* Reference count */
  124. struct kref ref;
  125. /*
  126. * List running through all cgroup groups in the same hash
  127. * slot. Protected by css_set_lock
  128. */
  129. struct hlist_node hlist;
  130. /*
  131. * List running through all tasks using this cgroup
  132. * group. Protected by css_set_lock
  133. */
  134. struct list_head tasks;
  135. /*
  136. * List of cg_cgroup_link objects on link chains from
  137. * cgroups referenced from this css_set. Protected by
  138. * css_set_lock
  139. */
  140. struct list_head cg_links;
  141. /*
  142. * Set of subsystem states, one for each subsystem. This array
  143. * is immutable after creation apart from the init_css_set
  144. * during subsystem registration (at boot time).
  145. */
  146. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  147. };
  148. /*
  149. * cgroup_map_cb is an abstract callback API for reporting map-valued
  150. * control files
  151. */
  152. struct cgroup_map_cb {
  153. int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
  154. void *state;
  155. };
  156. /* struct cftype:
  157. *
  158. * The files in the cgroup filesystem mostly have a very simple read/write
  159. * handling, some common function will take care of it. Nevertheless some cases
  160. * (read tasks) are special and therefore I define this structure for every
  161. * kind of file.
  162. *
  163. *
  164. * When reading/writing to a file:
  165. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  166. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  167. */
  168. #define MAX_CFTYPE_NAME 64
  169. struct cftype {
  170. /* By convention, the name should begin with the name of the
  171. * subsystem, followed by a period */
  172. char name[MAX_CFTYPE_NAME];
  173. int private;
  174. int (*open) (struct inode *inode, struct file *file);
  175. ssize_t (*read) (struct cgroup *cgrp, struct cftype *cft,
  176. struct file *file,
  177. char __user *buf, size_t nbytes, loff_t *ppos);
  178. /*
  179. * read_u64() is a shortcut for the common case of returning a
  180. * single integer. Use it in place of read()
  181. */
  182. u64 (*read_u64) (struct cgroup *cgrp, struct cftype *cft);
  183. /*
  184. * read_s64() is a signed version of read_u64()
  185. */
  186. s64 (*read_s64) (struct cgroup *cgrp, struct cftype *cft);
  187. /*
  188. * read_map() is used for defining a map of key/value
  189. * pairs. It should call cb->fill(cb, key, value) for each
  190. * entry. The key/value pairs (and their ordering) should not
  191. * change between reboots.
  192. */
  193. int (*read_map) (struct cgroup *cont, struct cftype *cft,
  194. struct cgroup_map_cb *cb);
  195. /*
  196. * read_seq_string() is used for outputting a simple sequence
  197. * using seqfile.
  198. */
  199. int (*read_seq_string) (struct cgroup *cont, struct cftype *cft,
  200. struct seq_file *m);
  201. ssize_t (*write) (struct cgroup *cgrp, struct cftype *cft,
  202. struct file *file,
  203. const char __user *buf, size_t nbytes, loff_t *ppos);
  204. /*
  205. * write_u64() is a shortcut for the common case of accepting
  206. * a single integer (as parsed by simple_strtoull) from
  207. * userspace. Use in place of write(); return 0 or error.
  208. */
  209. int (*write_u64) (struct cgroup *cgrp, struct cftype *cft, u64 val);
  210. /*
  211. * write_s64() is a signed version of write_u64()
  212. */
  213. int (*write_s64) (struct cgroup *cgrp, struct cftype *cft, s64 val);
  214. /*
  215. * trigger() callback can be used to get some kick from the
  216. * userspace, when the actual string written is not important
  217. * at all. The private field can be used to determine the
  218. * kick type for multiplexing.
  219. */
  220. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  221. int (*release) (struct inode *inode, struct file *file);
  222. };
  223. struct cgroup_scanner {
  224. struct cgroup *cg;
  225. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  226. void (*process_task)(struct task_struct *p,
  227. struct cgroup_scanner *scan);
  228. struct ptr_heap *heap;
  229. };
  230. /* Add a new file to the given cgroup directory. Should only be
  231. * called by subsystems from within a populate() method */
  232. int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  233. const struct cftype *cft);
  234. /* Add a set of new files to the given cgroup directory. Should
  235. * only be called by subsystems from within a populate() method */
  236. int cgroup_add_files(struct cgroup *cgrp,
  237. struct cgroup_subsys *subsys,
  238. const struct cftype cft[],
  239. int count);
  240. int cgroup_is_removed(const struct cgroup *cgrp);
  241. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  242. int cgroup_task_count(const struct cgroup *cgrp);
  243. /* Return true if the cgroup is a descendant of the current cgroup */
  244. int cgroup_is_descendant(const struct cgroup *cgrp);
  245. /* Control Group subsystem type. See Documentation/cgroups.txt for details */
  246. struct cgroup_subsys {
  247. struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
  248. struct cgroup *cgrp);
  249. void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  250. void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  251. int (*can_attach)(struct cgroup_subsys *ss,
  252. struct cgroup *cgrp, struct task_struct *tsk);
  253. void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
  254. struct cgroup *old_cgrp, struct task_struct *tsk);
  255. void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
  256. void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
  257. int (*populate)(struct cgroup_subsys *ss,
  258. struct cgroup *cgrp);
  259. void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  260. void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
  261. int subsys_id;
  262. int active;
  263. int disabled;
  264. int early_init;
  265. #define MAX_CGROUP_TYPE_NAMELEN 32
  266. const char *name;
  267. /* Protected by RCU */
  268. struct cgroupfs_root *root;
  269. struct list_head sibling;
  270. void *private;
  271. };
  272. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  273. #include <linux/cgroup_subsys.h>
  274. #undef SUBSYS
  275. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  276. struct cgroup *cgrp, int subsys_id)
  277. {
  278. return cgrp->subsys[subsys_id];
  279. }
  280. static inline struct cgroup_subsys_state *task_subsys_state(
  281. struct task_struct *task, int subsys_id)
  282. {
  283. return rcu_dereference(task->cgroups->subsys[subsys_id]);
  284. }
  285. static inline struct cgroup* task_cgroup(struct task_struct *task,
  286. int subsys_id)
  287. {
  288. return task_subsys_state(task, subsys_id)->cgroup;
  289. }
  290. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
  291. /* A cgroup_iter should be treated as an opaque object */
  292. struct cgroup_iter {
  293. struct list_head *cg_link;
  294. struct list_head *task;
  295. };
  296. /* To iterate across the tasks in a cgroup:
  297. *
  298. * 1) call cgroup_iter_start to intialize an iterator
  299. *
  300. * 2) call cgroup_iter_next() to retrieve member tasks until it
  301. * returns NULL or until you want to end the iteration
  302. *
  303. * 3) call cgroup_iter_end() to destroy the iterator.
  304. *
  305. * Or, call cgroup_scan_tasks() to iterate through every task in a cpuset.
  306. * - cgroup_scan_tasks() holds the css_set_lock when calling the test_task()
  307. * callback, but not while calling the process_task() callback.
  308. */
  309. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  310. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  311. struct cgroup_iter *it);
  312. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  313. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  314. int cgroup_attach_task(struct cgroup *, struct task_struct *);
  315. #else /* !CONFIG_CGROUPS */
  316. static inline int cgroup_init_early(void) { return 0; }
  317. static inline int cgroup_init(void) { return 0; }
  318. static inline void cgroup_init_smp(void) {}
  319. static inline void cgroup_fork(struct task_struct *p) {}
  320. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  321. static inline void cgroup_post_fork(struct task_struct *p) {}
  322. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  323. static inline void cgroup_lock(void) {}
  324. static inline void cgroup_unlock(void) {}
  325. static inline int cgroupstats_build(struct cgroupstats *stats,
  326. struct dentry *dentry)
  327. {
  328. return -EINVAL;
  329. }
  330. #endif /* !CONFIG_CGROUPS */
  331. #endif /* _LINUX_CGROUP_H */