cgroup.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/cpumask.h>
  12. #include <linux/nodemask.h>
  13. #include <linux/rcupdate.h>
  14. #include <linux/cgroupstats.h>
  15. #include <linux/prio_heap.h>
  16. #include <linux/rwsem.h>
  17. #ifdef CONFIG_CGROUPS
  18. struct cgroupfs_root;
  19. struct cgroup_subsys;
  20. struct inode;
  21. struct cgroup;
  22. extern int cgroup_init_early(void);
  23. extern int cgroup_init(void);
  24. extern void cgroup_init_smp(void);
  25. extern void cgroup_lock(void);
  26. extern bool cgroup_lock_live_group(struct cgroup *cgrp);
  27. extern void cgroup_unlock(void);
  28. extern void cgroup_fork(struct task_struct *p);
  29. extern void cgroup_fork_callbacks(struct task_struct *p);
  30. extern void cgroup_post_fork(struct task_struct *p);
  31. extern void cgroup_exit(struct task_struct *p, int run_callbacks);
  32. extern int cgroupstats_build(struct cgroupstats *stats,
  33. struct dentry *dentry);
  34. extern struct file_operations proc_cgroup_operations;
  35. /* Define the enumeration of all cgroup subsystems */
  36. #define SUBSYS(_x) _x ## _subsys_id,
  37. enum cgroup_subsys_id {
  38. #include <linux/cgroup_subsys.h>
  39. CGROUP_SUBSYS_COUNT
  40. };
  41. #undef SUBSYS
  42. /* Per-subsystem/per-cgroup state maintained by the system. */
  43. struct cgroup_subsys_state {
  44. /* The cgroup that this subsystem is attached to. Useful
  45. * for subsystems that want to know about the cgroup
  46. * hierarchy structure */
  47. struct cgroup *cgroup;
  48. /* State maintained by the cgroup system to allow
  49. * subsystems to be "busy". Should be accessed via css_get()
  50. * and css_put() */
  51. atomic_t refcnt;
  52. unsigned long flags;
  53. };
  54. /* bits in struct cgroup_subsys_state flags field */
  55. enum {
  56. CSS_ROOT, /* This CSS is the root of the subsystem */
  57. };
  58. /*
  59. * Call css_get() to hold a reference on the cgroup;
  60. *
  61. */
  62. static inline void css_get(struct cgroup_subsys_state *css)
  63. {
  64. /* We don't need to reference count the root state */
  65. if (!test_bit(CSS_ROOT, &css->flags))
  66. atomic_inc(&css->refcnt);
  67. }
  68. /*
  69. * css_put() should be called to release a reference taken by
  70. * css_get()
  71. */
  72. extern void __css_put(struct cgroup_subsys_state *css);
  73. static inline void css_put(struct cgroup_subsys_state *css)
  74. {
  75. if (!test_bit(CSS_ROOT, &css->flags))
  76. __css_put(css);
  77. }
  78. /* bits in struct cgroup flags field */
  79. enum {
  80. /* Control Group is dead */
  81. CGRP_REMOVED,
  82. /* Control Group has previously had a child cgroup or a task,
  83. * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set) */
  84. CGRP_RELEASABLE,
  85. /* Control Group requires release notifications to userspace */
  86. CGRP_NOTIFY_ON_RELEASE,
  87. };
  88. struct cgroup {
  89. unsigned long flags; /* "unsigned long" so bitops work */
  90. /* count users of this cgroup. >0 means busy, but doesn't
  91. * necessarily indicate the number of tasks in the
  92. * cgroup */
  93. atomic_t count;
  94. /*
  95. * We link our 'sibling' struct into our parent's 'children'.
  96. * Our children link their 'sibling' into our 'children'.
  97. */
  98. struct list_head sibling; /* my parent's children */
  99. struct list_head children; /* my children */
  100. struct cgroup *parent; /* my parent */
  101. struct dentry *dentry; /* cgroup fs entry */
  102. /* Private pointers for each registered subsystem */
  103. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  104. struct cgroupfs_root *root;
  105. struct cgroup *top_cgroup;
  106. /*
  107. * List of cg_cgroup_links pointing at css_sets with
  108. * tasks in this cgroup. Protected by css_set_lock
  109. */
  110. struct list_head css_sets;
  111. /*
  112. * Linked list running through all cgroups that can
  113. * potentially be reaped by the release agent. Protected by
  114. * release_list_lock
  115. */
  116. struct list_head release_list;
  117. /* pids_mutex protects the fields below */
  118. struct rw_semaphore pids_mutex;
  119. /* Array of process ids in the cgroup */
  120. pid_t *tasks_pids;
  121. /* How many files are using the current tasks_pids array */
  122. int pids_use_count;
  123. /* Length of the current tasks_pids array */
  124. int pids_length;
  125. };
  126. /* A css_set is a structure holding pointers to a set of
  127. * cgroup_subsys_state objects. This saves space in the task struct
  128. * object and speeds up fork()/exit(), since a single inc/dec and a
  129. * list_add()/del() can bump the reference count on the entire
  130. * cgroup set for a task.
  131. */
  132. struct css_set {
  133. /* Reference count */
  134. atomic_t refcount;
  135. /*
  136. * List running through all cgroup groups in the same hash
  137. * slot. Protected by css_set_lock
  138. */
  139. struct hlist_node hlist;
  140. /*
  141. * List running through all tasks using this cgroup
  142. * group. Protected by css_set_lock
  143. */
  144. struct list_head tasks;
  145. /*
  146. * List of cg_cgroup_link objects on link chains from
  147. * cgroups referenced from this css_set. Protected by
  148. * css_set_lock
  149. */
  150. struct list_head cg_links;
  151. /*
  152. * Set of subsystem states, one for each subsystem. This array
  153. * is immutable after creation apart from the init_css_set
  154. * during subsystem registration (at boot time).
  155. */
  156. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  157. };
  158. /*
  159. * cgroup_map_cb is an abstract callback API for reporting map-valued
  160. * control files
  161. */
  162. struct cgroup_map_cb {
  163. int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
  164. void *state;
  165. };
  166. /* struct cftype:
  167. *
  168. * The files in the cgroup filesystem mostly have a very simple read/write
  169. * handling, some common function will take care of it. Nevertheless some cases
  170. * (read tasks) are special and therefore I define this structure for every
  171. * kind of file.
  172. *
  173. *
  174. * When reading/writing to a file:
  175. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  176. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  177. */
  178. #define MAX_CFTYPE_NAME 64
  179. struct cftype {
  180. /* By convention, the name should begin with the name of the
  181. * subsystem, followed by a period */
  182. char name[MAX_CFTYPE_NAME];
  183. int private;
  184. /*
  185. * If non-zero, defines the maximum length of string that can
  186. * be passed to write_string; defaults to 64
  187. */
  188. size_t max_write_len;
  189. int (*open)(struct inode *inode, struct file *file);
  190. ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
  191. struct file *file,
  192. char __user *buf, size_t nbytes, loff_t *ppos);
  193. /*
  194. * read_u64() is a shortcut for the common case of returning a
  195. * single integer. Use it in place of read()
  196. */
  197. u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
  198. /*
  199. * read_s64() is a signed version of read_u64()
  200. */
  201. s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
  202. /*
  203. * read_map() is used for defining a map of key/value
  204. * pairs. It should call cb->fill(cb, key, value) for each
  205. * entry. The key/value pairs (and their ordering) should not
  206. * change between reboots.
  207. */
  208. int (*read_map)(struct cgroup *cont, struct cftype *cft,
  209. struct cgroup_map_cb *cb);
  210. /*
  211. * read_seq_string() is used for outputting a simple sequence
  212. * using seqfile.
  213. */
  214. int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
  215. struct seq_file *m);
  216. ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
  217. struct file *file,
  218. const char __user *buf, size_t nbytes, loff_t *ppos);
  219. /*
  220. * write_u64() is a shortcut for the common case of accepting
  221. * a single integer (as parsed by simple_strtoull) from
  222. * userspace. Use in place of write(); return 0 or error.
  223. */
  224. int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
  225. /*
  226. * write_s64() is a signed version of write_u64()
  227. */
  228. int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
  229. /*
  230. * write_string() is passed a nul-terminated kernelspace
  231. * buffer of maximum length determined by max_write_len.
  232. * Returns 0 or -ve error code.
  233. */
  234. int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
  235. const char *buffer);
  236. /*
  237. * trigger() callback can be used to get some kick from the
  238. * userspace, when the actual string written is not important
  239. * at all. The private field can be used to determine the
  240. * kick type for multiplexing.
  241. */
  242. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  243. int (*release)(struct inode *inode, struct file *file);
  244. };
  245. struct cgroup_scanner {
  246. struct cgroup *cg;
  247. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  248. void (*process_task)(struct task_struct *p,
  249. struct cgroup_scanner *scan);
  250. struct ptr_heap *heap;
  251. };
  252. /* Add a new file to the given cgroup directory. Should only be
  253. * called by subsystems from within a populate() method */
  254. int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  255. const struct cftype *cft);
  256. /* Add a set of new files to the given cgroup directory. Should
  257. * only be called by subsystems from within a populate() method */
  258. int cgroup_add_files(struct cgroup *cgrp,
  259. struct cgroup_subsys *subsys,
  260. const struct cftype cft[],
  261. int count);
  262. int cgroup_is_removed(const struct cgroup *cgrp);
  263. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  264. int cgroup_task_count(const struct cgroup *cgrp);
  265. /* Return true if the cgroup is a descendant of the current cgroup */
  266. int cgroup_is_descendant(const struct cgroup *cgrp);
  267. /* Control Group subsystem type. See Documentation/cgroups.txt for details */
  268. struct cgroup_subsys {
  269. struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
  270. struct cgroup *cgrp);
  271. void (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  272. void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  273. int (*can_attach)(struct cgroup_subsys *ss,
  274. struct cgroup *cgrp, struct task_struct *tsk);
  275. void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
  276. struct cgroup *old_cgrp, struct task_struct *tsk);
  277. void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
  278. void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
  279. int (*populate)(struct cgroup_subsys *ss,
  280. struct cgroup *cgrp);
  281. void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  282. void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
  283. /*
  284. * This routine is called with the task_lock of mm->owner held
  285. */
  286. void (*mm_owner_changed)(struct cgroup_subsys *ss,
  287. struct cgroup *old,
  288. struct cgroup *new,
  289. struct task_struct *p);
  290. int subsys_id;
  291. int active;
  292. int disabled;
  293. int early_init;
  294. #define MAX_CGROUP_TYPE_NAMELEN 32
  295. const char *name;
  296. /* Protected by RCU */
  297. struct cgroupfs_root *root;
  298. struct list_head sibling;
  299. void *private;
  300. };
  301. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  302. #include <linux/cgroup_subsys.h>
  303. #undef SUBSYS
  304. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  305. struct cgroup *cgrp, int subsys_id)
  306. {
  307. return cgrp->subsys[subsys_id];
  308. }
  309. static inline struct cgroup_subsys_state *task_subsys_state(
  310. struct task_struct *task, int subsys_id)
  311. {
  312. return rcu_dereference(task->cgroups->subsys[subsys_id]);
  313. }
  314. static inline struct cgroup* task_cgroup(struct task_struct *task,
  315. int subsys_id)
  316. {
  317. return task_subsys_state(task, subsys_id)->cgroup;
  318. }
  319. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
  320. char *nodename);
  321. /* A cgroup_iter should be treated as an opaque object */
  322. struct cgroup_iter {
  323. struct list_head *cg_link;
  324. struct list_head *task;
  325. };
  326. /* To iterate across the tasks in a cgroup:
  327. *
  328. * 1) call cgroup_iter_start to intialize an iterator
  329. *
  330. * 2) call cgroup_iter_next() to retrieve member tasks until it
  331. * returns NULL or until you want to end the iteration
  332. *
  333. * 3) call cgroup_iter_end() to destroy the iterator.
  334. *
  335. * Or, call cgroup_scan_tasks() to iterate through every task in a cpuset.
  336. * - cgroup_scan_tasks() holds the css_set_lock when calling the test_task()
  337. * callback, but not while calling the process_task() callback.
  338. */
  339. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  340. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  341. struct cgroup_iter *it);
  342. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  343. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  344. int cgroup_attach_task(struct cgroup *, struct task_struct *);
  345. #else /* !CONFIG_CGROUPS */
  346. static inline int cgroup_init_early(void) { return 0; }
  347. static inline int cgroup_init(void) { return 0; }
  348. static inline void cgroup_init_smp(void) {}
  349. static inline void cgroup_fork(struct task_struct *p) {}
  350. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  351. static inline void cgroup_post_fork(struct task_struct *p) {}
  352. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  353. static inline void cgroup_lock(void) {}
  354. static inline void cgroup_unlock(void) {}
  355. static inline int cgroupstats_build(struct cgroupstats *stats,
  356. struct dentry *dentry)
  357. {
  358. return -EINVAL;
  359. }
  360. #endif /* !CONFIG_CGROUPS */
  361. #ifdef CONFIG_MM_OWNER
  362. extern void
  363. cgroup_mm_owner_callbacks(struct task_struct *old, struct task_struct *new);
  364. #else /* !CONFIG_MM_OWNER */
  365. static inline void
  366. cgroup_mm_owner_callbacks(struct task_struct *old, struct task_struct *new)
  367. {
  368. }
  369. #endif /* CONFIG_MM_OWNER */
  370. #endif /* _LINUX_CGROUP_H */