cgroup.h 17 KB

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