cgroup.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. #include <linux/idr.h>
  18. #ifdef CONFIG_CGROUPS
  19. struct cgroupfs_root;
  20. struct cgroup_subsys;
  21. struct inode;
  22. struct cgroup;
  23. struct css_id;
  24. extern int cgroup_init_early(void);
  25. extern int cgroup_init(void);
  26. extern void cgroup_lock(void);
  27. extern bool cgroup_lock_live_group(struct cgroup *cgrp);
  28. extern void cgroup_unlock(void);
  29. extern void cgroup_fork(struct task_struct *p);
  30. extern void cgroup_fork_callbacks(struct task_struct *p);
  31. extern void cgroup_post_fork(struct task_struct *p);
  32. extern void cgroup_exit(struct task_struct *p, int run_callbacks);
  33. extern int cgroupstats_build(struct cgroupstats *stats,
  34. struct dentry *dentry);
  35. extern struct file_operations proc_cgroup_operations;
  36. /* Define the enumeration of all cgroup subsystems */
  37. #define SUBSYS(_x) _x ## _subsys_id,
  38. enum cgroup_subsys_id {
  39. #include <linux/cgroup_subsys.h>
  40. CGROUP_SUBSYS_COUNT
  41. };
  42. #undef SUBSYS
  43. /* Per-subsystem/per-cgroup state maintained by the system. */
  44. struct cgroup_subsys_state {
  45. /*
  46. * The cgroup that this subsystem is attached to. Useful
  47. * for subsystems that want to know about the cgroup
  48. * hierarchy structure
  49. */
  50. struct cgroup *cgroup;
  51. /*
  52. * State maintained by the cgroup system to allow subsystems
  53. * to be "busy". Should be accessed via css_get(),
  54. * css_tryget() and and css_put().
  55. */
  56. atomic_t refcnt;
  57. unsigned long flags;
  58. /* ID for this css, if possible */
  59. struct css_id *id;
  60. };
  61. /* bits in struct cgroup_subsys_state flags field */
  62. enum {
  63. CSS_ROOT, /* This CSS is the root of the subsystem */
  64. CSS_REMOVED, /* This CSS is dead */
  65. };
  66. /*
  67. * Call css_get() to hold a reference on the css; it can be used
  68. * for a reference obtained via:
  69. * - an existing ref-counted reference to the css
  70. * - task->cgroups for a locked task
  71. */
  72. static inline void css_get(struct cgroup_subsys_state *css)
  73. {
  74. /* We don't need to reference count the root state */
  75. if (!test_bit(CSS_ROOT, &css->flags))
  76. atomic_inc(&css->refcnt);
  77. }
  78. static inline bool css_is_removed(struct cgroup_subsys_state *css)
  79. {
  80. return test_bit(CSS_REMOVED, &css->flags);
  81. }
  82. /*
  83. * Call css_tryget() to take a reference on a css if your existing
  84. * (known-valid) reference isn't already ref-counted. Returns false if
  85. * the css has been destroyed.
  86. */
  87. static inline bool css_tryget(struct cgroup_subsys_state *css)
  88. {
  89. if (test_bit(CSS_ROOT, &css->flags))
  90. return true;
  91. while (!atomic_inc_not_zero(&css->refcnt)) {
  92. if (test_bit(CSS_REMOVED, &css->flags))
  93. return false;
  94. cpu_relax();
  95. }
  96. return true;
  97. }
  98. /*
  99. * css_put() should be called to release a reference taken by
  100. * css_get() or css_tryget()
  101. */
  102. extern void __css_put(struct cgroup_subsys_state *css);
  103. static inline void css_put(struct cgroup_subsys_state *css)
  104. {
  105. if (!test_bit(CSS_ROOT, &css->flags))
  106. __css_put(css);
  107. }
  108. /* bits in struct cgroup flags field */
  109. enum {
  110. /* Control Group is dead */
  111. CGRP_REMOVED,
  112. /*
  113. * Control Group has previously had a child cgroup or a task,
  114. * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
  115. */
  116. CGRP_RELEASABLE,
  117. /* Control Group requires release notifications to userspace */
  118. CGRP_NOTIFY_ON_RELEASE,
  119. /*
  120. * A thread in rmdir() is wating for this cgroup.
  121. */
  122. CGRP_WAIT_ON_RMDIR,
  123. };
  124. struct cgroup {
  125. unsigned long flags; /* "unsigned long" so bitops work */
  126. /*
  127. * count users of this cgroup. >0 means busy, but doesn't
  128. * necessarily indicate the number of tasks in the cgroup
  129. */
  130. atomic_t count;
  131. /*
  132. * We link our 'sibling' struct into our parent's 'children'.
  133. * Our children link their 'sibling' into our 'children'.
  134. */
  135. struct list_head sibling; /* my parent's children */
  136. struct list_head children; /* my children */
  137. struct cgroup *parent; /* my parent */
  138. struct dentry *dentry; /* cgroup fs entry, RCU protected */
  139. /* Private pointers for each registered subsystem */
  140. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  141. struct cgroupfs_root *root;
  142. struct cgroup *top_cgroup;
  143. /*
  144. * List of cg_cgroup_links pointing at css_sets with
  145. * tasks in this cgroup. Protected by css_set_lock
  146. */
  147. struct list_head css_sets;
  148. /*
  149. * Linked list running through all cgroups that can
  150. * potentially be reaped by the release agent. Protected by
  151. * release_list_lock
  152. */
  153. struct list_head release_list;
  154. /* pids_mutex protects the fields below */
  155. struct rw_semaphore pids_mutex;
  156. /* Array of process ids in the cgroup */
  157. pid_t *tasks_pids;
  158. /* How many files are using the current tasks_pids array */
  159. int pids_use_count;
  160. /* Length of the current tasks_pids array */
  161. int pids_length;
  162. /* For RCU-protected deletion */
  163. struct rcu_head rcu_head;
  164. };
  165. /*
  166. * A css_set is a structure holding pointers to a set of
  167. * cgroup_subsys_state objects. This saves space in the task struct
  168. * object and speeds up fork()/exit(), since a single inc/dec and a
  169. * list_add()/del() can bump the reference count on the entire cgroup
  170. * set for a task.
  171. */
  172. struct css_set {
  173. /* Reference count */
  174. atomic_t refcount;
  175. /*
  176. * List running through all cgroup groups in the same hash
  177. * slot. Protected by css_set_lock
  178. */
  179. struct hlist_node hlist;
  180. /*
  181. * List running through all tasks using this cgroup
  182. * group. Protected by css_set_lock
  183. */
  184. struct list_head tasks;
  185. /*
  186. * List of cg_cgroup_link objects on link chains from
  187. * cgroups referenced from this css_set. Protected by
  188. * css_set_lock
  189. */
  190. struct list_head cg_links;
  191. /*
  192. * Set of subsystem states, one for each subsystem. This array
  193. * is immutable after creation apart from the init_css_set
  194. * during subsystem registration (at boot time).
  195. */
  196. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  197. };
  198. /*
  199. * cgroup_map_cb is an abstract callback API for reporting map-valued
  200. * control files
  201. */
  202. struct cgroup_map_cb {
  203. int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
  204. void *state;
  205. };
  206. /*
  207. * struct cftype: handler definitions for cgroup control files
  208. *
  209. * When reading/writing to a file:
  210. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  211. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  212. */
  213. #define MAX_CFTYPE_NAME 64
  214. struct cftype {
  215. /*
  216. * By convention, the name should begin with the name of the
  217. * subsystem, followed by a period
  218. */
  219. char name[MAX_CFTYPE_NAME];
  220. int private;
  221. /*
  222. * If non-zero, defines the maximum length of string that can
  223. * be passed to write_string; defaults to 64
  224. */
  225. size_t max_write_len;
  226. int (*open)(struct inode *inode, struct file *file);
  227. ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
  228. struct file *file,
  229. char __user *buf, size_t nbytes, loff_t *ppos);
  230. /*
  231. * read_u64() is a shortcut for the common case of returning a
  232. * single integer. Use it in place of read()
  233. */
  234. u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
  235. /*
  236. * read_s64() is a signed version of read_u64()
  237. */
  238. s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
  239. /*
  240. * read_map() is used for defining a map of key/value
  241. * pairs. It should call cb->fill(cb, key, value) for each
  242. * entry. The key/value pairs (and their ordering) should not
  243. * change between reboots.
  244. */
  245. int (*read_map)(struct cgroup *cont, struct cftype *cft,
  246. struct cgroup_map_cb *cb);
  247. /*
  248. * read_seq_string() is used for outputting a simple sequence
  249. * using seqfile.
  250. */
  251. int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
  252. struct seq_file *m);
  253. ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
  254. struct file *file,
  255. const char __user *buf, size_t nbytes, loff_t *ppos);
  256. /*
  257. * write_u64() is a shortcut for the common case of accepting
  258. * a single integer (as parsed by simple_strtoull) from
  259. * userspace. Use in place of write(); return 0 or error.
  260. */
  261. int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
  262. /*
  263. * write_s64() is a signed version of write_u64()
  264. */
  265. int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
  266. /*
  267. * write_string() is passed a nul-terminated kernelspace
  268. * buffer of maximum length determined by max_write_len.
  269. * Returns 0 or -ve error code.
  270. */
  271. int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
  272. const char *buffer);
  273. /*
  274. * trigger() callback can be used to get some kick from the
  275. * userspace, when the actual string written is not important
  276. * at all. The private field can be used to determine the
  277. * kick type for multiplexing.
  278. */
  279. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  280. int (*release)(struct inode *inode, struct file *file);
  281. };
  282. struct cgroup_scanner {
  283. struct cgroup *cg;
  284. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  285. void (*process_task)(struct task_struct *p,
  286. struct cgroup_scanner *scan);
  287. struct ptr_heap *heap;
  288. };
  289. /*
  290. * Add a new file to the given cgroup directory. Should only be
  291. * called by subsystems from within a populate() method
  292. */
  293. int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  294. const struct cftype *cft);
  295. /*
  296. * Add a set of new files to the given cgroup directory. Should
  297. * only be called by subsystems from within a populate() method
  298. */
  299. int cgroup_add_files(struct cgroup *cgrp,
  300. struct cgroup_subsys *subsys,
  301. const struct cftype cft[],
  302. int count);
  303. int cgroup_is_removed(const struct cgroup *cgrp);
  304. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  305. int cgroup_task_count(const struct cgroup *cgrp);
  306. /* Return true if cgrp is a descendant of the task's cgroup */
  307. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
  308. /* Control Group subsystem type. See Documentation/cgroups.txt for details */
  309. struct cgroup_subsys {
  310. struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
  311. struct cgroup *cgrp);
  312. int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  313. void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  314. int (*can_attach)(struct cgroup_subsys *ss,
  315. struct cgroup *cgrp, struct task_struct *tsk);
  316. void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
  317. struct cgroup *old_cgrp, struct task_struct *tsk);
  318. void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
  319. void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
  320. int (*populate)(struct cgroup_subsys *ss,
  321. struct cgroup *cgrp);
  322. void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  323. void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
  324. int subsys_id;
  325. int active;
  326. int disabled;
  327. int early_init;
  328. /*
  329. * True if this subsys uses ID. ID is not available before cgroup_init()
  330. * (not available in early_init time.)
  331. */
  332. bool use_id;
  333. #define MAX_CGROUP_TYPE_NAMELEN 32
  334. const char *name;
  335. /*
  336. * Protects sibling/children links of cgroups in this
  337. * hierarchy, plus protects which hierarchy (or none) the
  338. * subsystem is a part of (i.e. root/sibling). To avoid
  339. * potential deadlocks, the following operations should not be
  340. * undertaken while holding any hierarchy_mutex:
  341. *
  342. * - allocating memory
  343. * - initiating hotplug events
  344. */
  345. struct mutex hierarchy_mutex;
  346. struct lock_class_key subsys_key;
  347. /*
  348. * Link to parent, and list entry in parent's children.
  349. * Protected by this->hierarchy_mutex and cgroup_lock()
  350. */
  351. struct cgroupfs_root *root;
  352. struct list_head sibling;
  353. /* used when use_id == true */
  354. struct idr idr;
  355. spinlock_t id_lock;
  356. };
  357. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  358. #include <linux/cgroup_subsys.h>
  359. #undef SUBSYS
  360. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  361. struct cgroup *cgrp, int subsys_id)
  362. {
  363. return cgrp->subsys[subsys_id];
  364. }
  365. static inline struct cgroup_subsys_state *task_subsys_state(
  366. struct task_struct *task, int subsys_id)
  367. {
  368. return rcu_dereference(task->cgroups->subsys[subsys_id]);
  369. }
  370. static inline struct cgroup* task_cgroup(struct task_struct *task,
  371. int subsys_id)
  372. {
  373. return task_subsys_state(task, subsys_id)->cgroup;
  374. }
  375. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
  376. char *nodename);
  377. /* A cgroup_iter should be treated as an opaque object */
  378. struct cgroup_iter {
  379. struct list_head *cg_link;
  380. struct list_head *task;
  381. };
  382. /*
  383. * To iterate across the tasks in a cgroup:
  384. *
  385. * 1) call cgroup_iter_start to intialize an iterator
  386. *
  387. * 2) call cgroup_iter_next() to retrieve member tasks until it
  388. * returns NULL or until you want to end the iteration
  389. *
  390. * 3) call cgroup_iter_end() to destroy the iterator.
  391. *
  392. * Or, call cgroup_scan_tasks() to iterate through every task in a
  393. * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
  394. * the test_task() callback, but not while calling the process_task()
  395. * callback.
  396. */
  397. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  398. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  399. struct cgroup_iter *it);
  400. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  401. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  402. int cgroup_attach_task(struct cgroup *, struct task_struct *);
  403. /*
  404. * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
  405. * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
  406. * CSS ID is assigned at cgroup allocation (create) automatically
  407. * and removed when subsys calls free_css_id() function. This is because
  408. * the lifetime of cgroup_subsys_state is subsys's matter.
  409. *
  410. * Looking up and scanning function should be called under rcu_read_lock().
  411. * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
  412. * But the css returned by this routine can be "not populated yet" or "being
  413. * destroyed". The caller should check css and cgroup's status.
  414. */
  415. /*
  416. * Typically Called at ->destroy(), or somewhere the subsys frees
  417. * cgroup_subsys_state.
  418. */
  419. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
  420. /* Find a cgroup_subsys_state which has given ID */
  421. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
  422. /*
  423. * Get a cgroup whose id is greater than or equal to id under tree of root.
  424. * Returning a cgroup_subsys_state or NULL.
  425. */
  426. struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
  427. struct cgroup_subsys_state *root, int *foundid);
  428. /* Returns true if root is ancestor of cg */
  429. bool css_is_ancestor(struct cgroup_subsys_state *cg,
  430. struct cgroup_subsys_state *root);
  431. /* Get id and depth of css */
  432. unsigned short css_id(struct cgroup_subsys_state *css);
  433. unsigned short css_depth(struct cgroup_subsys_state *css);
  434. #else /* !CONFIG_CGROUPS */
  435. static inline int cgroup_init_early(void) { return 0; }
  436. static inline int cgroup_init(void) { return 0; }
  437. static inline void cgroup_fork(struct task_struct *p) {}
  438. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  439. static inline void cgroup_post_fork(struct task_struct *p) {}
  440. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  441. static inline void cgroup_lock(void) {}
  442. static inline void cgroup_unlock(void) {}
  443. static inline int cgroupstats_build(struct cgroupstats *stats,
  444. struct dentry *dentry)
  445. {
  446. return -EINVAL;
  447. }
  448. #endif /* !CONFIG_CGROUPS */
  449. #endif /* _LINUX_CGROUP_H */