cgroup.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 not 0, file mode is set to this value, otherwise it will
  223. * be figured out automatically
  224. */
  225. mode_t mode;
  226. /*
  227. * If non-zero, defines the maximum length of string that can
  228. * be passed to write_string; defaults to 64
  229. */
  230. size_t max_write_len;
  231. int (*open)(struct inode *inode, struct file *file);
  232. ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
  233. struct file *file,
  234. char __user *buf, size_t nbytes, loff_t *ppos);
  235. /*
  236. * read_u64() is a shortcut for the common case of returning a
  237. * single integer. Use it in place of read()
  238. */
  239. u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
  240. /*
  241. * read_s64() is a signed version of read_u64()
  242. */
  243. s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
  244. /*
  245. * read_map() is used for defining a map of key/value
  246. * pairs. It should call cb->fill(cb, key, value) for each
  247. * entry. The key/value pairs (and their ordering) should not
  248. * change between reboots.
  249. */
  250. int (*read_map)(struct cgroup *cont, struct cftype *cft,
  251. struct cgroup_map_cb *cb);
  252. /*
  253. * read_seq_string() is used for outputting a simple sequence
  254. * using seqfile.
  255. */
  256. int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
  257. struct seq_file *m);
  258. ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
  259. struct file *file,
  260. const char __user *buf, size_t nbytes, loff_t *ppos);
  261. /*
  262. * write_u64() is a shortcut for the common case of accepting
  263. * a single integer (as parsed by simple_strtoull) from
  264. * userspace. Use in place of write(); return 0 or error.
  265. */
  266. int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
  267. /*
  268. * write_s64() is a signed version of write_u64()
  269. */
  270. int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
  271. /*
  272. * write_string() is passed a nul-terminated kernelspace
  273. * buffer of maximum length determined by max_write_len.
  274. * Returns 0 or -ve error code.
  275. */
  276. int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
  277. const char *buffer);
  278. /*
  279. * trigger() callback can be used to get some kick from the
  280. * userspace, when the actual string written is not important
  281. * at all. The private field can be used to determine the
  282. * kick type for multiplexing.
  283. */
  284. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  285. int (*release)(struct inode *inode, struct file *file);
  286. };
  287. struct cgroup_scanner {
  288. struct cgroup *cg;
  289. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  290. void (*process_task)(struct task_struct *p,
  291. struct cgroup_scanner *scan);
  292. struct ptr_heap *heap;
  293. };
  294. /*
  295. * Add a new file to the given cgroup directory. Should only be
  296. * called by subsystems from within a populate() method
  297. */
  298. int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  299. const struct cftype *cft);
  300. /*
  301. * Add a set of new files to the given cgroup directory. Should
  302. * only be called by subsystems from within a populate() method
  303. */
  304. int cgroup_add_files(struct cgroup *cgrp,
  305. struct cgroup_subsys *subsys,
  306. const struct cftype cft[],
  307. int count);
  308. int cgroup_is_removed(const struct cgroup *cgrp);
  309. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  310. int cgroup_task_count(const struct cgroup *cgrp);
  311. /* Return true if cgrp is a descendant of the task's cgroup */
  312. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
  313. /* Control Group subsystem type. See Documentation/cgroups.txt for details */
  314. struct cgroup_subsys {
  315. struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
  316. struct cgroup *cgrp);
  317. int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  318. void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  319. int (*can_attach)(struct cgroup_subsys *ss,
  320. struct cgroup *cgrp, struct task_struct *tsk);
  321. void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
  322. struct cgroup *old_cgrp, struct task_struct *tsk);
  323. void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
  324. void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
  325. int (*populate)(struct cgroup_subsys *ss,
  326. struct cgroup *cgrp);
  327. void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  328. void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
  329. int subsys_id;
  330. int active;
  331. int disabled;
  332. int early_init;
  333. /*
  334. * True if this subsys uses ID. ID is not available before cgroup_init()
  335. * (not available in early_init time.)
  336. */
  337. bool use_id;
  338. #define MAX_CGROUP_TYPE_NAMELEN 32
  339. const char *name;
  340. /*
  341. * Protects sibling/children links of cgroups in this
  342. * hierarchy, plus protects which hierarchy (or none) the
  343. * subsystem is a part of (i.e. root/sibling). To avoid
  344. * potential deadlocks, the following operations should not be
  345. * undertaken while holding any hierarchy_mutex:
  346. *
  347. * - allocating memory
  348. * - initiating hotplug events
  349. */
  350. struct mutex hierarchy_mutex;
  351. struct lock_class_key subsys_key;
  352. /*
  353. * Link to parent, and list entry in parent's children.
  354. * Protected by this->hierarchy_mutex and cgroup_lock()
  355. */
  356. struct cgroupfs_root *root;
  357. struct list_head sibling;
  358. /* used when use_id == true */
  359. struct idr idr;
  360. spinlock_t id_lock;
  361. };
  362. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  363. #include <linux/cgroup_subsys.h>
  364. #undef SUBSYS
  365. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  366. struct cgroup *cgrp, int subsys_id)
  367. {
  368. return cgrp->subsys[subsys_id];
  369. }
  370. static inline struct cgroup_subsys_state *task_subsys_state(
  371. struct task_struct *task, int subsys_id)
  372. {
  373. return rcu_dereference(task->cgroups->subsys[subsys_id]);
  374. }
  375. static inline struct cgroup* task_cgroup(struct task_struct *task,
  376. int subsys_id)
  377. {
  378. return task_subsys_state(task, subsys_id)->cgroup;
  379. }
  380. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
  381. char *nodename);
  382. /* A cgroup_iter should be treated as an opaque object */
  383. struct cgroup_iter {
  384. struct list_head *cg_link;
  385. struct list_head *task;
  386. };
  387. /*
  388. * To iterate across the tasks in a cgroup:
  389. *
  390. * 1) call cgroup_iter_start to intialize an iterator
  391. *
  392. * 2) call cgroup_iter_next() to retrieve member tasks until it
  393. * returns NULL or until you want to end the iteration
  394. *
  395. * 3) call cgroup_iter_end() to destroy the iterator.
  396. *
  397. * Or, call cgroup_scan_tasks() to iterate through every task in a
  398. * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
  399. * the test_task() callback, but not while calling the process_task()
  400. * callback.
  401. */
  402. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  403. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  404. struct cgroup_iter *it);
  405. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  406. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  407. int cgroup_attach_task(struct cgroup *, struct task_struct *);
  408. /*
  409. * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
  410. * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
  411. * CSS ID is assigned at cgroup allocation (create) automatically
  412. * and removed when subsys calls free_css_id() function. This is because
  413. * the lifetime of cgroup_subsys_state is subsys's matter.
  414. *
  415. * Looking up and scanning function should be called under rcu_read_lock().
  416. * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
  417. * But the css returned by this routine can be "not populated yet" or "being
  418. * destroyed". The caller should check css and cgroup's status.
  419. */
  420. /*
  421. * Typically Called at ->destroy(), or somewhere the subsys frees
  422. * cgroup_subsys_state.
  423. */
  424. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
  425. /* Find a cgroup_subsys_state which has given ID */
  426. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
  427. /*
  428. * Get a cgroup whose id is greater than or equal to id under tree of root.
  429. * Returning a cgroup_subsys_state or NULL.
  430. */
  431. struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
  432. struct cgroup_subsys_state *root, int *foundid);
  433. /* Returns true if root is ancestor of cg */
  434. bool css_is_ancestor(struct cgroup_subsys_state *cg,
  435. const struct cgroup_subsys_state *root);
  436. /* Get id and depth of css */
  437. unsigned short css_id(struct cgroup_subsys_state *css);
  438. unsigned short css_depth(struct cgroup_subsys_state *css);
  439. #else /* !CONFIG_CGROUPS */
  440. static inline int cgroup_init_early(void) { return 0; }
  441. static inline int cgroup_init(void) { return 0; }
  442. static inline void cgroup_fork(struct task_struct *p) {}
  443. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  444. static inline void cgroup_post_fork(struct task_struct *p) {}
  445. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  446. static inline void cgroup_lock(void) {}
  447. static inline void cgroup_unlock(void) {}
  448. static inline int cgroupstats_build(struct cgroupstats *stats,
  449. struct dentry *dentry)
  450. {
  451. return -EINVAL;
  452. }
  453. #endif /* !CONFIG_CGROUPS */
  454. #endif /* _LINUX_CGROUP_H */