cgroup.h 17 KB

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