cgroup.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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/rculist.h>
  15. #include <linux/cgroupstats.h>
  16. #include <linux/prio_heap.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/idr.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/xattr.h>
  21. #ifdef CONFIG_CGROUPS
  22. struct cgroupfs_root;
  23. struct cgroup_subsys;
  24. struct inode;
  25. struct cgroup;
  26. struct css_id;
  27. extern int cgroup_init_early(void);
  28. extern int cgroup_init(void);
  29. extern void cgroup_lock(void);
  30. extern int cgroup_lock_is_held(void);
  31. extern bool cgroup_lock_live_group(struct cgroup *cgrp);
  32. extern void cgroup_unlock(void);
  33. extern void cgroup_fork(struct task_struct *p);
  34. extern void cgroup_post_fork(struct task_struct *p);
  35. extern void cgroup_exit(struct task_struct *p, int run_callbacks);
  36. extern int cgroupstats_build(struct cgroupstats *stats,
  37. struct dentry *dentry);
  38. extern int cgroup_load_subsys(struct cgroup_subsys *ss);
  39. extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
  40. extern const struct file_operations proc_cgroup_operations;
  41. /* Define the enumeration of all builtin cgroup subsystems */
  42. #define SUBSYS(_x) _x ## _subsys_id,
  43. #define IS_SUBSYS_ENABLED(option) IS_ENABLED(option)
  44. enum cgroup_subsys_id {
  45. #include <linux/cgroup_subsys.h>
  46. CGROUP_SUBSYS_COUNT,
  47. };
  48. #undef IS_SUBSYS_ENABLED
  49. #undef SUBSYS
  50. /* Per-subsystem/per-cgroup state maintained by the system. */
  51. struct cgroup_subsys_state {
  52. /*
  53. * The cgroup that this subsystem is attached to. Useful
  54. * for subsystems that want to know about the cgroup
  55. * hierarchy structure
  56. */
  57. struct cgroup *cgroup;
  58. /*
  59. * State maintained by the cgroup system to allow subsystems
  60. * to be "busy". Should be accessed via css_get(),
  61. * css_tryget() and and css_put().
  62. */
  63. atomic_t refcnt;
  64. unsigned long flags;
  65. /* ID for this css, if possible */
  66. struct css_id __rcu *id;
  67. /* Used to put @cgroup->dentry on the last css_put() */
  68. struct work_struct dput_work;
  69. };
  70. /* bits in struct cgroup_subsys_state flags field */
  71. enum {
  72. CSS_ROOT = (1 << 0), /* this CSS is the root of the subsystem */
  73. CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
  74. };
  75. /* Caller must verify that the css is not for root cgroup */
  76. static inline void __css_get(struct cgroup_subsys_state *css, int count)
  77. {
  78. atomic_add(count, &css->refcnt);
  79. }
  80. /*
  81. * Call css_get() to hold a reference on the css; it can be used
  82. * for a reference obtained via:
  83. * - an existing ref-counted reference to the css
  84. * - task->cgroups for a locked task
  85. */
  86. static inline void css_get(struct cgroup_subsys_state *css)
  87. {
  88. /* We don't need to reference count the root state */
  89. if (!(css->flags & CSS_ROOT))
  90. __css_get(css, 1);
  91. }
  92. /*
  93. * Call css_tryget() to take a reference on a css if your existing
  94. * (known-valid) reference isn't already ref-counted. Returns false if
  95. * the css has been destroyed.
  96. */
  97. extern bool __css_tryget(struct cgroup_subsys_state *css);
  98. static inline bool css_tryget(struct cgroup_subsys_state *css)
  99. {
  100. if (css->flags & CSS_ROOT)
  101. return true;
  102. return __css_tryget(css);
  103. }
  104. /*
  105. * css_put() should be called to release a reference taken by
  106. * css_get() or css_tryget()
  107. */
  108. extern void __css_put(struct cgroup_subsys_state *css);
  109. static inline void css_put(struct cgroup_subsys_state *css)
  110. {
  111. if (!(css->flags & CSS_ROOT))
  112. __css_put(css);
  113. }
  114. /* bits in struct cgroup flags field */
  115. enum {
  116. /* Control Group is dead */
  117. CGRP_REMOVED,
  118. /*
  119. * Control Group has previously had a child cgroup or a task,
  120. * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
  121. */
  122. CGRP_RELEASABLE,
  123. /* Control Group requires release notifications to userspace */
  124. CGRP_NOTIFY_ON_RELEASE,
  125. /*
  126. * Clone the parent's configuration when creating a new child
  127. * cpuset cgroup. For historical reasons, this option can be
  128. * specified at mount time and thus is implemented here.
  129. */
  130. CGRP_CPUSET_CLONE_CHILDREN,
  131. };
  132. struct cgroup {
  133. unsigned long flags; /* "unsigned long" so bitops work */
  134. /*
  135. * count users of this cgroup. >0 means busy, but doesn't
  136. * necessarily indicate the number of tasks in the cgroup
  137. */
  138. atomic_t count;
  139. /*
  140. * We link our 'sibling' struct into our parent's 'children'.
  141. * Our children link their 'sibling' into our 'children'.
  142. */
  143. struct list_head sibling; /* my parent's children */
  144. struct list_head children; /* my children */
  145. struct list_head files; /* my files */
  146. struct cgroup *parent; /* my parent */
  147. struct dentry *dentry; /* cgroup fs entry, RCU protected */
  148. /* Private pointers for each registered subsystem */
  149. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  150. struct cgroupfs_root *root;
  151. struct cgroup *top_cgroup;
  152. /*
  153. * List of cg_cgroup_links pointing at css_sets with
  154. * tasks in this cgroup. Protected by css_set_lock
  155. */
  156. struct list_head css_sets;
  157. struct list_head allcg_node; /* cgroupfs_root->allcg_list */
  158. struct list_head cft_q_node; /* used during cftype add/rm */
  159. /*
  160. * Linked list running through all cgroups that can
  161. * potentially be reaped by the release agent. Protected by
  162. * release_list_lock
  163. */
  164. struct list_head release_list;
  165. /*
  166. * list of pidlists, up to two for each namespace (one for procs, one
  167. * for tasks); created on demand.
  168. */
  169. struct list_head pidlists;
  170. struct mutex pidlist_mutex;
  171. /* For RCU-protected deletion */
  172. struct rcu_head rcu_head;
  173. /* List of events which userspace want to receive */
  174. struct list_head event_list;
  175. spinlock_t event_list_lock;
  176. /* directory xattrs */
  177. struct simple_xattrs xattrs;
  178. };
  179. /*
  180. * A css_set is a structure holding pointers to a set of
  181. * cgroup_subsys_state objects. This saves space in the task struct
  182. * object and speeds up fork()/exit(), since a single inc/dec and a
  183. * list_add()/del() can bump the reference count on the entire cgroup
  184. * set for a task.
  185. */
  186. struct css_set {
  187. /* Reference count */
  188. atomic_t refcount;
  189. /*
  190. * List running through all cgroup groups in the same hash
  191. * slot. Protected by css_set_lock
  192. */
  193. struct hlist_node hlist;
  194. /*
  195. * List running through all tasks using this cgroup
  196. * group. Protected by css_set_lock
  197. */
  198. struct list_head tasks;
  199. /*
  200. * List of cg_cgroup_link objects on link chains from
  201. * cgroups referenced from this css_set. Protected by
  202. * css_set_lock
  203. */
  204. struct list_head cg_links;
  205. /*
  206. * Set of subsystem states, one for each subsystem. This array
  207. * is immutable after creation apart from the init_css_set
  208. * during subsystem registration (at boot time) and modular subsystem
  209. * loading/unloading.
  210. */
  211. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  212. /* For RCU-protected deletion */
  213. struct rcu_head rcu_head;
  214. };
  215. /*
  216. * cgroup_map_cb is an abstract callback API for reporting map-valued
  217. * control files
  218. */
  219. struct cgroup_map_cb {
  220. int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
  221. void *state;
  222. };
  223. /*
  224. * struct cftype: handler definitions for cgroup control files
  225. *
  226. * When reading/writing to a file:
  227. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  228. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  229. */
  230. /* cftype->flags */
  231. #define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
  232. #define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create onp root cg */
  233. #define MAX_CFTYPE_NAME 64
  234. struct cftype {
  235. /*
  236. * By convention, the name should begin with the name of the
  237. * subsystem, followed by a period. Zero length string indicates
  238. * end of cftype array.
  239. */
  240. char name[MAX_CFTYPE_NAME];
  241. int private;
  242. /*
  243. * If not 0, file mode is set to this value, otherwise it will
  244. * be figured out automatically
  245. */
  246. umode_t mode;
  247. /*
  248. * If non-zero, defines the maximum length of string that can
  249. * be passed to write_string; defaults to 64
  250. */
  251. size_t max_write_len;
  252. /* CFTYPE_* flags */
  253. unsigned int flags;
  254. /* file xattrs */
  255. struct simple_xattrs xattrs;
  256. int (*open)(struct inode *inode, struct file *file);
  257. ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
  258. struct file *file,
  259. char __user *buf, size_t nbytes, loff_t *ppos);
  260. /*
  261. * read_u64() is a shortcut for the common case of returning a
  262. * single integer. Use it in place of read()
  263. */
  264. u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
  265. /*
  266. * read_s64() is a signed version of read_u64()
  267. */
  268. s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
  269. /*
  270. * read_map() is used for defining a map of key/value
  271. * pairs. It should call cb->fill(cb, key, value) for each
  272. * entry. The key/value pairs (and their ordering) should not
  273. * change between reboots.
  274. */
  275. int (*read_map)(struct cgroup *cont, struct cftype *cft,
  276. struct cgroup_map_cb *cb);
  277. /*
  278. * read_seq_string() is used for outputting a simple sequence
  279. * using seqfile.
  280. */
  281. int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
  282. struct seq_file *m);
  283. ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
  284. struct file *file,
  285. const char __user *buf, size_t nbytes, loff_t *ppos);
  286. /*
  287. * write_u64() is a shortcut for the common case of accepting
  288. * a single integer (as parsed by simple_strtoull) from
  289. * userspace. Use in place of write(); return 0 or error.
  290. */
  291. int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
  292. /*
  293. * write_s64() is a signed version of write_u64()
  294. */
  295. int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
  296. /*
  297. * write_string() is passed a nul-terminated kernelspace
  298. * buffer of maximum length determined by max_write_len.
  299. * Returns 0 or -ve error code.
  300. */
  301. int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
  302. const char *buffer);
  303. /*
  304. * trigger() callback can be used to get some kick from the
  305. * userspace, when the actual string written is not important
  306. * at all. The private field can be used to determine the
  307. * kick type for multiplexing.
  308. */
  309. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  310. int (*release)(struct inode *inode, struct file *file);
  311. /*
  312. * register_event() callback will be used to add new userspace
  313. * waiter for changes related to the cftype. Implement it if
  314. * you want to provide this functionality. Use eventfd_signal()
  315. * on eventfd to send notification to userspace.
  316. */
  317. int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
  318. struct eventfd_ctx *eventfd, const char *args);
  319. /*
  320. * unregister_event() callback will be called when userspace
  321. * closes the eventfd or on cgroup removing.
  322. * This callback must be implemented, if you want provide
  323. * notification functionality.
  324. */
  325. void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
  326. struct eventfd_ctx *eventfd);
  327. };
  328. /*
  329. * cftype_sets describe cftypes belonging to a subsystem and are chained at
  330. * cgroup_subsys->cftsets. Each cftset points to an array of cftypes
  331. * terminated by zero length name.
  332. */
  333. struct cftype_set {
  334. struct list_head node; /* chained at subsys->cftsets */
  335. struct cftype *cfts;
  336. };
  337. struct cgroup_scanner {
  338. struct cgroup *cg;
  339. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  340. void (*process_task)(struct task_struct *p,
  341. struct cgroup_scanner *scan);
  342. struct ptr_heap *heap;
  343. void *data;
  344. };
  345. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  346. int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  347. int cgroup_is_removed(const struct cgroup *cgrp);
  348. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  349. int cgroup_task_count(const struct cgroup *cgrp);
  350. /* Return true if cgrp is a descendant of the task's cgroup */
  351. int cgroup_is_descendant(const struct cgroup *cgrp, struct task_struct *task);
  352. /*
  353. * Control Group taskset, used to pass around set of tasks to cgroup_subsys
  354. * methods.
  355. */
  356. struct cgroup_taskset;
  357. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
  358. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
  359. struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
  360. int cgroup_taskset_size(struct cgroup_taskset *tset);
  361. /**
  362. * cgroup_taskset_for_each - iterate cgroup_taskset
  363. * @task: the loop cursor
  364. * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
  365. * @tset: taskset to iterate
  366. */
  367. #define cgroup_taskset_for_each(task, skip_cgrp, tset) \
  368. for ((task) = cgroup_taskset_first((tset)); (task); \
  369. (task) = cgroup_taskset_next((tset))) \
  370. if (!(skip_cgrp) || \
  371. cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
  372. /*
  373. * Control Group subsystem type.
  374. * See Documentation/cgroups/cgroups.txt for details
  375. */
  376. struct cgroup_subsys {
  377. struct cgroup_subsys_state *(*css_alloc)(struct cgroup *cgrp);
  378. int (*css_online)(struct cgroup *cgrp);
  379. void (*css_offline)(struct cgroup *cgrp);
  380. void (*css_free)(struct cgroup *cgrp);
  381. int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
  382. void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
  383. void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
  384. void (*fork)(struct task_struct *task);
  385. void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
  386. struct task_struct *task);
  387. void (*bind)(struct cgroup *root);
  388. int subsys_id;
  389. int active;
  390. int disabled;
  391. int early_init;
  392. /*
  393. * True if this subsys uses ID. ID is not available before cgroup_init()
  394. * (not available in early_init time.)
  395. */
  396. bool use_id;
  397. /*
  398. * If %false, this subsystem is properly hierarchical -
  399. * configuration, resource accounting and restriction on a parent
  400. * cgroup cover those of its children. If %true, hierarchy support
  401. * is broken in some ways - some subsystems ignore hierarchy
  402. * completely while others are only implemented half-way.
  403. *
  404. * It's now disallowed to create nested cgroups if the subsystem is
  405. * broken and cgroup core will emit a warning message on such
  406. * cases. Eventually, all subsystems will be made properly
  407. * hierarchical and this will go away.
  408. */
  409. bool broken_hierarchy;
  410. bool warned_broken_hierarchy;
  411. #define MAX_CGROUP_TYPE_NAMELEN 32
  412. const char *name;
  413. /*
  414. * Link to parent, and list entry in parent's children.
  415. * Protected by cgroup_lock()
  416. */
  417. struct cgroupfs_root *root;
  418. struct list_head sibling;
  419. /* used when use_id == true */
  420. struct idr idr;
  421. spinlock_t id_lock;
  422. /* list of cftype_sets */
  423. struct list_head cftsets;
  424. /* base cftypes, automatically [de]registered with subsys itself */
  425. struct cftype *base_cftypes;
  426. struct cftype_set base_cftset;
  427. /* should be defined only by modular subsystems */
  428. struct module *module;
  429. };
  430. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  431. #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  432. #include <linux/cgroup_subsys.h>
  433. #undef IS_SUBSYS_ENABLED
  434. #undef SUBSYS
  435. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  436. struct cgroup *cgrp, int subsys_id)
  437. {
  438. return cgrp->subsys[subsys_id];
  439. }
  440. /*
  441. * function to get the cgroup_subsys_state which allows for extra
  442. * rcu_dereference_check() conditions, such as locks used during the
  443. * cgroup_subsys::attach() methods.
  444. */
  445. #define task_subsys_state_check(task, subsys_id, __c) \
  446. rcu_dereference_check(task->cgroups->subsys[subsys_id], \
  447. lockdep_is_held(&task->alloc_lock) || \
  448. cgroup_lock_is_held() || (__c))
  449. static inline struct cgroup_subsys_state *
  450. task_subsys_state(struct task_struct *task, int subsys_id)
  451. {
  452. return task_subsys_state_check(task, subsys_id, false);
  453. }
  454. static inline struct cgroup* task_cgroup(struct task_struct *task,
  455. int subsys_id)
  456. {
  457. return task_subsys_state(task, subsys_id)->cgroup;
  458. }
  459. /**
  460. * cgroup_for_each_child - iterate through children of a cgroup
  461. * @pos: the cgroup * to use as the loop cursor
  462. * @cgroup: cgroup whose children to walk
  463. *
  464. * Walk @cgroup's children. Must be called under rcu_read_lock(). A child
  465. * cgroup which hasn't finished ->css_online() or already has finished
  466. * ->css_offline() may show up during traversal and it's each subsystem's
  467. * responsibility to verify that each @pos is alive.
  468. *
  469. * If a subsystem synchronizes against the parent in its ->css_online() and
  470. * before starting iterating, a cgroup which finished ->css_online() is
  471. * guaranteed to be visible in the future iterations.
  472. */
  473. #define cgroup_for_each_child(pos, cgroup) \
  474. list_for_each_entry_rcu(pos, &(cgroup)->children, sibling)
  475. struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
  476. struct cgroup *cgroup);
  477. /**
  478. * cgroup_for_each_descendant_pre - pre-order walk of a cgroup's descendants
  479. * @pos: the cgroup * to use as the loop cursor
  480. * @cgroup: cgroup whose descendants to walk
  481. *
  482. * Walk @cgroup's descendants. Must be called under rcu_read_lock(). A
  483. * descendant cgroup which hasn't finished ->css_online() or already has
  484. * finished ->css_offline() may show up during traversal and it's each
  485. * subsystem's responsibility to verify that each @pos is alive.
  486. *
  487. * If a subsystem synchronizes against the parent in its ->css_online() and
  488. * before starting iterating, and synchronizes against @pos on each
  489. * iteration, any descendant cgroup which finished ->css_offline() is
  490. * guaranteed to be visible in the future iterations.
  491. *
  492. * In other words, the following guarantees that a descendant can't escape
  493. * state updates of its ancestors.
  494. *
  495. * my_online(@cgrp)
  496. * {
  497. * Lock @cgrp->parent and @cgrp;
  498. * Inherit state from @cgrp->parent;
  499. * Unlock both.
  500. * }
  501. *
  502. * my_update_state(@cgrp)
  503. * {
  504. * Lock @cgrp;
  505. * Update @cgrp's state;
  506. * Unlock @cgrp;
  507. *
  508. * cgroup_for_each_descendant_pre(@pos, @cgrp) {
  509. * Lock @pos;
  510. * Verify @pos is alive and inherit state from @pos->parent;
  511. * Unlock @pos;
  512. * }
  513. * }
  514. *
  515. * As long as the inheriting step, including checking the parent state, is
  516. * enclosed inside @pos locking, double-locking the parent isn't necessary
  517. * while inheriting. The state update to the parent is guaranteed to be
  518. * visible by walking order and, as long as inheriting operations to the
  519. * same @pos are atomic to each other, multiple updates racing each other
  520. * still result in the correct state. It's guaranateed that at least one
  521. * inheritance happens for any cgroup after the latest update to its
  522. * parent.
  523. *
  524. * If checking parent's state requires locking the parent, each inheriting
  525. * iteration should lock and unlock both @pos->parent and @pos.
  526. *
  527. * Alternatively, a subsystem may choose to use a single global lock to
  528. * synchronize ->css_online() and ->css_offline() against tree-walking
  529. * operations.
  530. */
  531. #define cgroup_for_each_descendant_pre(pos, cgroup) \
  532. for (pos = cgroup_next_descendant_pre(NULL, (cgroup)); (pos); \
  533. pos = cgroup_next_descendant_pre((pos), (cgroup)))
  534. struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
  535. struct cgroup *cgroup);
  536. /**
  537. * cgroup_for_each_descendant_post - post-order walk of a cgroup's descendants
  538. * @pos: the cgroup * to use as the loop cursor
  539. * @cgroup: cgroup whose descendants to walk
  540. *
  541. * Similar to cgroup_for_each_descendant_pre() but performs post-order
  542. * traversal instead. Note that the walk visibility guarantee described in
  543. * pre-order walk doesn't apply the same to post-order walks.
  544. */
  545. #define cgroup_for_each_descendant_post(pos, cgroup) \
  546. for (pos = cgroup_next_descendant_post(NULL, (cgroup)); (pos); \
  547. pos = cgroup_next_descendant_post((pos), (cgroup)))
  548. /* A cgroup_iter should be treated as an opaque object */
  549. struct cgroup_iter {
  550. struct list_head *cg_link;
  551. struct list_head *task;
  552. };
  553. /*
  554. * To iterate across the tasks in a cgroup:
  555. *
  556. * 1) call cgroup_iter_start to initialize an iterator
  557. *
  558. * 2) call cgroup_iter_next() to retrieve member tasks until it
  559. * returns NULL or until you want to end the iteration
  560. *
  561. * 3) call cgroup_iter_end() to destroy the iterator.
  562. *
  563. * Or, call cgroup_scan_tasks() to iterate through every task in a
  564. * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
  565. * the test_task() callback, but not while calling the process_task()
  566. * callback.
  567. */
  568. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  569. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  570. struct cgroup_iter *it);
  571. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  572. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  573. int cgroup_attach_task(struct cgroup *, struct task_struct *);
  574. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
  575. /*
  576. * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
  577. * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
  578. * CSS ID is assigned at cgroup allocation (create) automatically
  579. * and removed when subsys calls free_css_id() function. This is because
  580. * the lifetime of cgroup_subsys_state is subsys's matter.
  581. *
  582. * Looking up and scanning function should be called under rcu_read_lock().
  583. * Taking cgroup_mutex is not necessary for following calls.
  584. * But the css returned by this routine can be "not populated yet" or "being
  585. * destroyed". The caller should check css and cgroup's status.
  586. */
  587. /*
  588. * Typically Called at ->destroy(), or somewhere the subsys frees
  589. * cgroup_subsys_state.
  590. */
  591. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
  592. /* Find a cgroup_subsys_state which has given ID */
  593. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
  594. /*
  595. * Get a cgroup whose id is greater than or equal to id under tree of root.
  596. * Returning a cgroup_subsys_state or NULL.
  597. */
  598. struct cgroup_subsys_state *css_get_next(struct cgroup_subsys *ss, int id,
  599. struct cgroup_subsys_state *root, int *foundid);
  600. /* Returns true if root is ancestor of cg */
  601. bool css_is_ancestor(struct cgroup_subsys_state *cg,
  602. const struct cgroup_subsys_state *root);
  603. /* Get id and depth of css */
  604. unsigned short css_id(struct cgroup_subsys_state *css);
  605. unsigned short css_depth(struct cgroup_subsys_state *css);
  606. struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
  607. #else /* !CONFIG_CGROUPS */
  608. static inline int cgroup_init_early(void) { return 0; }
  609. static inline int cgroup_init(void) { return 0; }
  610. static inline void cgroup_fork(struct task_struct *p) {}
  611. static inline void cgroup_fork_callbacks(struct task_struct *p) {}
  612. static inline void cgroup_post_fork(struct task_struct *p) {}
  613. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  614. static inline void cgroup_lock(void) {}
  615. static inline void cgroup_unlock(void) {}
  616. static inline int cgroupstats_build(struct cgroupstats *stats,
  617. struct dentry *dentry)
  618. {
  619. return -EINVAL;
  620. }
  621. /* No cgroups - nothing to do */
  622. static inline int cgroup_attach_task_all(struct task_struct *from,
  623. struct task_struct *t)
  624. {
  625. return 0;
  626. }
  627. #endif /* !CONFIG_CGROUPS */
  628. #endif /* _LINUX_CGROUP_H */