cgroup.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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_pidlist {
  125. /* protects the other fields */
  126. struct rw_semaphore mutex;
  127. /* array of xids */
  128. pid_t *list;
  129. /* how many elements the above list has */
  130. int length;
  131. /* how many files are using the current array */
  132. int use_count;
  133. };
  134. struct cgroup {
  135. unsigned long flags; /* "unsigned long" so bitops work */
  136. /*
  137. * count users of this cgroup. >0 means busy, but doesn't
  138. * necessarily indicate the number of tasks in the cgroup
  139. */
  140. atomic_t count;
  141. /*
  142. * We link our 'sibling' struct into our parent's 'children'.
  143. * Our children link their 'sibling' into our 'children'.
  144. */
  145. struct list_head sibling; /* my parent's children */
  146. struct list_head children; /* my children */
  147. struct cgroup *parent; /* my parent */
  148. struct dentry *dentry; /* cgroup fs entry, RCU protected */
  149. /* Private pointers for each registered subsystem */
  150. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  151. struct cgroupfs_root *root;
  152. struct cgroup *top_cgroup;
  153. /*
  154. * List of cg_cgroup_links pointing at css_sets with
  155. * tasks in this cgroup. Protected by css_set_lock
  156. */
  157. struct list_head css_sets;
  158. /*
  159. * Linked list running through all cgroups that can
  160. * potentially be reaped by the release agent. Protected by
  161. * release_list_lock
  162. */
  163. struct list_head release_list;
  164. /* we will have two separate pidlists, one for pids (the tasks file)
  165. * and one for tgids (the procs file). */
  166. struct cgroup_pidlist tasks, procs;
  167. /* For RCU-protected deletion */
  168. struct rcu_head rcu_head;
  169. };
  170. /*
  171. * A css_set is a structure holding pointers to a set of
  172. * cgroup_subsys_state objects. This saves space in the task struct
  173. * object and speeds up fork()/exit(), since a single inc/dec and a
  174. * list_add()/del() can bump the reference count on the entire cgroup
  175. * set for a task.
  176. */
  177. struct css_set {
  178. /* Reference count */
  179. atomic_t refcount;
  180. /*
  181. * List running through all cgroup groups in the same hash
  182. * slot. Protected by css_set_lock
  183. */
  184. struct hlist_node hlist;
  185. /*
  186. * List running through all tasks using this cgroup
  187. * group. Protected by css_set_lock
  188. */
  189. struct list_head tasks;
  190. /*
  191. * List of cg_cgroup_link objects on link chains from
  192. * cgroups referenced from this css_set. Protected by
  193. * css_set_lock
  194. */
  195. struct list_head cg_links;
  196. /*
  197. * Set of subsystem states, one for each subsystem. This array
  198. * is immutable after creation apart from the init_css_set
  199. * during subsystem registration (at boot time).
  200. */
  201. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  202. };
  203. /*
  204. * cgroup_map_cb is an abstract callback API for reporting map-valued
  205. * control files
  206. */
  207. struct cgroup_map_cb {
  208. int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
  209. void *state;
  210. };
  211. /*
  212. * struct cftype: handler definitions for cgroup control files
  213. *
  214. * When reading/writing to a file:
  215. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  216. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  217. */
  218. #define MAX_CFTYPE_NAME 64
  219. struct cftype {
  220. /*
  221. * By convention, the name should begin with the name of the
  222. * subsystem, followed by a period
  223. */
  224. char name[MAX_CFTYPE_NAME];
  225. int private;
  226. /*
  227. * If not 0, file mode is set to this value, otherwise it will
  228. * be figured out automatically
  229. */
  230. mode_t mode;
  231. /*
  232. * If non-zero, defines the maximum length of string that can
  233. * be passed to write_string; defaults to 64
  234. */
  235. size_t max_write_len;
  236. int (*open)(struct inode *inode, struct file *file);
  237. ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
  238. struct file *file,
  239. char __user *buf, size_t nbytes, loff_t *ppos);
  240. /*
  241. * read_u64() is a shortcut for the common case of returning a
  242. * single integer. Use it in place of read()
  243. */
  244. u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
  245. /*
  246. * read_s64() is a signed version of read_u64()
  247. */
  248. s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
  249. /*
  250. * read_map() is used for defining a map of key/value
  251. * pairs. It should call cb->fill(cb, key, value) for each
  252. * entry. The key/value pairs (and their ordering) should not
  253. * change between reboots.
  254. */
  255. int (*read_map)(struct cgroup *cont, struct cftype *cft,
  256. struct cgroup_map_cb *cb);
  257. /*
  258. * read_seq_string() is used for outputting a simple sequence
  259. * using seqfile.
  260. */
  261. int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
  262. struct seq_file *m);
  263. ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
  264. struct file *file,
  265. const char __user *buf, size_t nbytes, loff_t *ppos);
  266. /*
  267. * write_u64() is a shortcut for the common case of accepting
  268. * a single integer (as parsed by simple_strtoull) from
  269. * userspace. Use in place of write(); return 0 or error.
  270. */
  271. int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
  272. /*
  273. * write_s64() is a signed version of write_u64()
  274. */
  275. int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
  276. /*
  277. * write_string() is passed a nul-terminated kernelspace
  278. * buffer of maximum length determined by max_write_len.
  279. * Returns 0 or -ve error code.
  280. */
  281. int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
  282. const char *buffer);
  283. /*
  284. * trigger() callback can be used to get some kick from the
  285. * userspace, when the actual string written is not important
  286. * at all. The private field can be used to determine the
  287. * kick type for multiplexing.
  288. */
  289. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  290. int (*release)(struct inode *inode, struct file *file);
  291. };
  292. struct cgroup_scanner {
  293. struct cgroup *cg;
  294. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  295. void (*process_task)(struct task_struct *p,
  296. struct cgroup_scanner *scan);
  297. struct ptr_heap *heap;
  298. void *data;
  299. };
  300. /*
  301. * Add a new file to the given cgroup directory. Should only be
  302. * called by subsystems from within a populate() method
  303. */
  304. int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
  305. const struct cftype *cft);
  306. /*
  307. * Add a set of new files to the given cgroup directory. Should
  308. * only be called by subsystems from within a populate() method
  309. */
  310. int cgroup_add_files(struct cgroup *cgrp,
  311. struct cgroup_subsys *subsys,
  312. const struct cftype cft[],
  313. int count);
  314. int cgroup_is_removed(const struct cgroup *cgrp);
  315. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  316. int cgroup_task_count(const struct cgroup *cgrp);
  317. /* Return true if cgrp is a descendant of the task's cgroup */
  318. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
  319. /*
  320. * When the subsys has to access css and may add permanent refcnt to css,
  321. * it should take care of racy conditions with rmdir(). Following set of
  322. * functions, is for stop/restart rmdir if necessary.
  323. * Because these will call css_get/put, "css" should be alive css.
  324. *
  325. * cgroup_exclude_rmdir();
  326. * ...do some jobs which may access arbitrary empty cgroup
  327. * cgroup_release_and_wakeup_rmdir();
  328. *
  329. * When someone removes a cgroup while cgroup_exclude_rmdir() holds it,
  330. * it sleeps and cgroup_release_and_wakeup_rmdir() will wake him up.
  331. */
  332. void cgroup_exclude_rmdir(struct cgroup_subsys_state *css);
  333. void cgroup_release_and_wakeup_rmdir(struct cgroup_subsys_state *css);
  334. /*
  335. * Control Group subsystem type.
  336. * See Documentation/cgroups/cgroups.txt for details
  337. */
  338. struct cgroup_subsys {
  339. struct cgroup_subsys_state *(*create)(struct cgroup_subsys *ss,
  340. struct cgroup *cgrp);
  341. int (*pre_destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  342. void (*destroy)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  343. int (*can_attach)(struct cgroup_subsys *ss,
  344. struct cgroup *cgrp, struct task_struct *tsk);
  345. void (*attach)(struct cgroup_subsys *ss, struct cgroup *cgrp,
  346. struct cgroup *old_cgrp, struct task_struct *tsk);
  347. void (*fork)(struct cgroup_subsys *ss, struct task_struct *task);
  348. void (*exit)(struct cgroup_subsys *ss, struct task_struct *task);
  349. int (*populate)(struct cgroup_subsys *ss,
  350. struct cgroup *cgrp);
  351. void (*post_clone)(struct cgroup_subsys *ss, struct cgroup *cgrp);
  352. void (*bind)(struct cgroup_subsys *ss, struct cgroup *root);
  353. int subsys_id;
  354. int active;
  355. int disabled;
  356. int early_init;
  357. /*
  358. * True if this subsys uses ID. ID is not available before cgroup_init()
  359. * (not available in early_init time.)
  360. */
  361. bool use_id;
  362. #define MAX_CGROUP_TYPE_NAMELEN 32
  363. const char *name;
  364. /*
  365. * Protects sibling/children links of cgroups in this
  366. * hierarchy, plus protects which hierarchy (or none) the
  367. * subsystem is a part of (i.e. root/sibling). To avoid
  368. * potential deadlocks, the following operations should not be
  369. * undertaken while holding any hierarchy_mutex:
  370. *
  371. * - allocating memory
  372. * - initiating hotplug events
  373. */
  374. struct mutex hierarchy_mutex;
  375. struct lock_class_key subsys_key;
  376. /*
  377. * Link to parent, and list entry in parent's children.
  378. * Protected by this->hierarchy_mutex and cgroup_lock()
  379. */
  380. struct cgroupfs_root *root;
  381. struct list_head sibling;
  382. /* used when use_id == true */
  383. struct idr idr;
  384. spinlock_t id_lock;
  385. };
  386. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  387. #include <linux/cgroup_subsys.h>
  388. #undef SUBSYS
  389. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  390. struct cgroup *cgrp, int subsys_id)
  391. {
  392. return cgrp->subsys[subsys_id];
  393. }
  394. static inline struct cgroup_subsys_state *task_subsys_state(
  395. struct task_struct *task, int subsys_id)
  396. {
  397. return rcu_dereference(task->cgroups->subsys[subsys_id]);
  398. }
  399. static inline struct cgroup* task_cgroup(struct task_struct *task,
  400. int subsys_id)
  401. {
  402. return task_subsys_state(task, subsys_id)->cgroup;
  403. }
  404. int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
  405. char *nodename);
  406. /* A cgroup_iter should be treated as an opaque object */
  407. struct cgroup_iter {
  408. struct list_head *cg_link;
  409. struct list_head *task;
  410. };
  411. /*
  412. * To iterate across the tasks in a cgroup:
  413. *
  414. * 1) call cgroup_iter_start to intialize an iterator
  415. *
  416. * 2) call cgroup_iter_next() to retrieve member tasks until it
  417. * returns NULL or until you want to end the iteration
  418. *
  419. * 3) call cgroup_iter_end() to destroy the iterator.
  420. *
  421. * Or, call cgroup_scan_tasks() to iterate through every task in a
  422. * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
  423. * the test_task() callback, but not while calling the process_task()
  424. * callback.
  425. */
  426. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  427. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  428. struct cgroup_iter *it);
  429. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  430. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  431. int cgroup_attach_task(struct cgroup *, struct task_struct *);
  432. /*
  433. * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
  434. * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
  435. * CSS ID is assigned at cgroup allocation (create) automatically
  436. * and removed when subsys calls free_css_id() function. This is because
  437. * the lifetime of cgroup_subsys_state is subsys's matter.
  438. *
  439. * Looking up and scanning function should be called under rcu_read_lock().
  440. * Taking cgroup_mutex()/hierarchy_mutex() is not necessary for following calls.
  441. * But the css returned by this routine can be "not populated yet" or "being
  442. * destroyed". The caller should check css and cgroup's status.
  443. */
  444. /*
  445. * Typically Called at ->destroy(), or somewhere the subsys frees
  446. * cgroup_subsys_state.
  447. */
  448. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
  449. /* Find a cgroup_subsys_state which has given ID */
  450. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
  451. /*
  452. * Get a cgroup whose id is greater than or equal to id under tree of root.
  453. * Returning a cgroup_subsys_state or NULL.
  454. */
  455. struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
  456. struct cgroup_subsys_state *root, int *foundid);
  457. /* Returns true if root is ancestor of cg */
  458. bool css_is_ancestor(struct cgroup_subsys_state *cg,
  459. const struct cgroup_subsys_state *root);
  460. /* Get id and depth of css */
  461. unsigned short css_id(struct cgroup_subsys_state *css);
  462. unsigned short css_depth(struct cgroup_subsys_state *css);
  463. #else /* !CONFIG_CGROUPS */
  464. static inline int cgroup_init_early(void) { return 0; }
  465. static inline int cgroup_init(void) { return 0; }
  466. static inline void cgroup_fork(struct task_struct *p) {}
  467. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  468. static inline void cgroup_post_fork(struct task_struct *p) {}
  469. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  470. static inline void cgroup_lock(void) {}
  471. static inline void cgroup_unlock(void) {}
  472. static inline int cgroupstats_build(struct cgroupstats *stats,
  473. struct dentry *dentry)
  474. {
  475. return -EINVAL;
  476. }
  477. #endif /* !CONFIG_CGROUPS */
  478. #endif /* _LINUX_CGROUP_H */