cgroup.h 22 KB

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