cgroup.h 18 KB

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