cgroup.h 23 KB

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