cgroup.h 15 KB

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