cgroup.h 22 KB

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