cgroup.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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/fs.h>
  21. #include <linux/percpu-refcount.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/kernfs.h>
  24. #ifdef CONFIG_CGROUPS
  25. struct cgroupfs_root;
  26. struct cgroup_subsys;
  27. struct inode;
  28. struct cgroup;
  29. extern int cgroup_init_early(void);
  30. extern int cgroup_init(void);
  31. extern void cgroup_fork(struct task_struct *p);
  32. extern void cgroup_post_fork(struct task_struct *p);
  33. extern void cgroup_exit(struct task_struct *p, int run_callbacks);
  34. extern int cgroupstats_build(struct cgroupstats *stats,
  35. struct dentry *dentry);
  36. extern int proc_cgroup_show(struct seq_file *, void *);
  37. /* define the enumeration of all cgroup subsystems */
  38. #define SUBSYS(_x) _x ## _cgrp_id,
  39. enum cgroup_subsys_id {
  40. #include <linux/cgroup_subsys.h>
  41. CGROUP_SUBSYS_COUNT,
  42. };
  43. #undef SUBSYS
  44. /* Per-subsystem/per-cgroup state maintained by the system. */
  45. struct cgroup_subsys_state {
  46. /* the cgroup that this css is attached to */
  47. struct cgroup *cgroup;
  48. /* the cgroup subsystem that this css is attached to */
  49. struct cgroup_subsys *ss;
  50. /* reference count - access via css_[try]get() and css_put() */
  51. struct percpu_ref refcnt;
  52. /* the parent css */
  53. struct cgroup_subsys_state *parent;
  54. unsigned long flags;
  55. /* percpu_ref killing and RCU release */
  56. struct rcu_head rcu_head;
  57. struct work_struct destroy_work;
  58. };
  59. /* bits in struct cgroup_subsys_state flags field */
  60. enum {
  61. CSS_ROOT = (1 << 0), /* this CSS is the root of the subsystem */
  62. CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
  63. };
  64. /**
  65. * css_get - obtain a reference on the specified css
  66. * @css: target css
  67. *
  68. * The caller must already have a reference.
  69. */
  70. static inline void css_get(struct cgroup_subsys_state *css)
  71. {
  72. /* We don't need to reference count the root state */
  73. if (!(css->flags & CSS_ROOT))
  74. percpu_ref_get(&css->refcnt);
  75. }
  76. /**
  77. * css_tryget - try to obtain a reference on the specified css
  78. * @css: target css
  79. *
  80. * Obtain a reference on @css if it's alive. The caller naturally needs to
  81. * ensure that @css is accessible but doesn't have to be holding a
  82. * reference on it - IOW, RCU protected access is good enough for this
  83. * function. Returns %true if a reference count was successfully obtained;
  84. * %false otherwise.
  85. */
  86. static inline bool css_tryget(struct cgroup_subsys_state *css)
  87. {
  88. if (css->flags & CSS_ROOT)
  89. return true;
  90. return percpu_ref_tryget(&css->refcnt);
  91. }
  92. /**
  93. * css_put - put a css reference
  94. * @css: target css
  95. *
  96. * Put a reference obtained via css_get() and css_tryget().
  97. */
  98. static inline void css_put(struct cgroup_subsys_state *css)
  99. {
  100. if (!(css->flags & CSS_ROOT))
  101. percpu_ref_put(&css->refcnt);
  102. }
  103. /* bits in struct cgroup flags field */
  104. enum {
  105. /* Control Group is dead */
  106. CGRP_DEAD,
  107. /*
  108. * Control Group has previously had a child cgroup or a task,
  109. * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
  110. */
  111. CGRP_RELEASABLE,
  112. /* Control Group requires release notifications to userspace */
  113. CGRP_NOTIFY_ON_RELEASE,
  114. /*
  115. * Clone the parent's configuration when creating a new child
  116. * cpuset cgroup. For historical reasons, this option can be
  117. * specified at mount time and thus is implemented here.
  118. */
  119. CGRP_CPUSET_CLONE_CHILDREN,
  120. /* see the comment above CGRP_ROOT_SANE_BEHAVIOR for details */
  121. CGRP_SANE_BEHAVIOR,
  122. };
  123. struct cgroup {
  124. unsigned long flags; /* "unsigned long" so bitops work */
  125. /*
  126. * idr allocated in-hierarchy ID.
  127. *
  128. * The ID of the root cgroup is always 0, and a new cgroup
  129. * will be assigned with a smallest available ID.
  130. *
  131. * Allocating/Removing ID must be protected by cgroup_mutex.
  132. */
  133. int id;
  134. /* the number of attached css's */
  135. int nr_css;
  136. atomic_t refcnt;
  137. /*
  138. * We link our 'sibling' struct into our parent's 'children'.
  139. * Our children link their 'sibling' into our 'children'.
  140. */
  141. struct list_head sibling; /* my parent's children */
  142. struct list_head children; /* my children */
  143. struct cgroup *parent; /* my parent */
  144. struct kernfs_node *kn; /* cgroup kernfs entry */
  145. /*
  146. * Monotonically increasing unique serial number which defines a
  147. * uniform order among all cgroups. It's guaranteed that all
  148. * ->children lists are in the ascending order of ->serial_nr.
  149. * It's used to allow interrupting and resuming iterations.
  150. */
  151. u64 serial_nr;
  152. /* Private pointers for each registered subsystem */
  153. struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
  154. struct cgroupfs_root *root;
  155. /*
  156. * List of cgrp_cset_links pointing at css_sets with tasks in this
  157. * cgroup. Protected by css_set_lock.
  158. */
  159. struct list_head cset_links;
  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. /* dummy css with NULL ->ss, points back to this cgroup */
  173. struct cgroup_subsys_state dummy_css;
  174. /* For css percpu_ref killing and RCU-protected deletion */
  175. struct rcu_head rcu_head;
  176. struct work_struct destroy_work;
  177. };
  178. #define MAX_CGROUP_ROOT_NAMELEN 64
  179. /* cgroupfs_root->flags */
  180. enum {
  181. /*
  182. * Unfortunately, cgroup core and various controllers are riddled
  183. * with idiosyncrasies and pointless options. The following flag,
  184. * when set, will force sane behavior - some options are forced on,
  185. * others are disallowed, and some controllers will change their
  186. * hierarchical or other behaviors.
  187. *
  188. * The set of behaviors affected by this flag are still being
  189. * determined and developed and the mount option for this flag is
  190. * prefixed with __DEVEL__. The prefix will be dropped once we
  191. * reach the point where all behaviors are compatible with the
  192. * planned unified hierarchy, which will automatically turn on this
  193. * flag.
  194. *
  195. * The followings are the behaviors currently affected this flag.
  196. *
  197. * - Mount options "noprefix" and "clone_children" are disallowed.
  198. * Also, cgroupfs file cgroup.clone_children is not created.
  199. *
  200. * - When mounting an existing superblock, mount options should
  201. * match.
  202. *
  203. * - Remount is disallowed.
  204. *
  205. * - rename(2) is disallowed.
  206. *
  207. * - "tasks" is removed. Everything should be at process
  208. * granularity. Use "cgroup.procs" instead.
  209. *
  210. * - "cgroup.procs" is not sorted. pids will be unique unless they
  211. * got recycled inbetween reads.
  212. *
  213. * - "release_agent" and "notify_on_release" are removed.
  214. * Replacement notification mechanism will be implemented.
  215. *
  216. * - "xattr" mount option is deprecated. kernfs always enables it.
  217. *
  218. * - cpuset: tasks will be kept in empty cpusets when hotplug happens
  219. * and take masks of ancestors with non-empty cpus/mems, instead of
  220. * being moved to an ancestor.
  221. *
  222. * - cpuset: a task can be moved into an empty cpuset, and again it
  223. * takes masks of ancestors.
  224. *
  225. * - memcg: use_hierarchy is on by default and the cgroup file for
  226. * the flag is not created.
  227. *
  228. * - blkcg: blk-throttle becomes properly hierarchical.
  229. */
  230. CGRP_ROOT_SANE_BEHAVIOR = (1 << 0),
  231. CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
  232. CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
  233. /* mount options live below bit 16 */
  234. CGRP_ROOT_OPTION_MASK = (1 << 16) - 1,
  235. CGRP_ROOT_SUBSYS_BOUND = (1 << 16), /* subsystems finished binding */
  236. };
  237. /*
  238. * A cgroupfs_root represents the root of a cgroup hierarchy, and may be
  239. * associated with a kernfs_root to form an active hierarchy. This is
  240. * internal to cgroup core. Don't access directly from controllers.
  241. */
  242. struct cgroupfs_root {
  243. struct kernfs_root *kf_root;
  244. /* The bitmask of subsystems attached to this hierarchy */
  245. unsigned long subsys_mask;
  246. atomic_t refcnt;
  247. /* Unique id for this hierarchy. */
  248. int hierarchy_id;
  249. /* The root cgroup for this hierarchy */
  250. struct cgroup top_cgroup;
  251. /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
  252. atomic_t nr_cgrps;
  253. /* A list running through the active hierarchies */
  254. struct list_head root_list;
  255. /* Hierarchy-specific flags */
  256. unsigned long flags;
  257. /* IDs for cgroups in this hierarchy */
  258. struct idr cgroup_idr;
  259. /* The path to use for release notifications. */
  260. char release_agent_path[PATH_MAX];
  261. /* The name for this hierarchy - may be empty */
  262. char name[MAX_CGROUP_ROOT_NAMELEN];
  263. };
  264. /*
  265. * A css_set is a structure holding pointers to a set of
  266. * cgroup_subsys_state objects. This saves space in the task struct
  267. * object and speeds up fork()/exit(), since a single inc/dec and a
  268. * list_add()/del() can bump the reference count on the entire cgroup
  269. * set for a task.
  270. */
  271. struct css_set {
  272. /* Reference count */
  273. atomic_t refcount;
  274. /*
  275. * List running through all cgroup groups in the same hash
  276. * slot. Protected by css_set_lock
  277. */
  278. struct hlist_node hlist;
  279. /*
  280. * List running through all tasks using this cgroup
  281. * group. Protected by css_set_lock
  282. */
  283. struct list_head tasks;
  284. /*
  285. * List of cgrp_cset_links pointing at cgroups referenced from this
  286. * css_set. Protected by css_set_lock.
  287. */
  288. struct list_head cgrp_links;
  289. /*
  290. * Set of subsystem states, one for each subsystem. This array is
  291. * immutable after creation apart from the init_css_set during
  292. * subsystem registration (at boot time).
  293. */
  294. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  295. /* For RCU-protected deletion */
  296. struct rcu_head rcu_head;
  297. };
  298. /*
  299. * struct cftype: handler definitions for cgroup control files
  300. *
  301. * When reading/writing to a file:
  302. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  303. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  304. */
  305. /* cftype->flags */
  306. enum {
  307. CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
  308. CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
  309. CFTYPE_INSANE = (1 << 2), /* don't create if sane_behavior */
  310. CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
  311. };
  312. #define MAX_CFTYPE_NAME 64
  313. struct cftype {
  314. /*
  315. * By convention, the name should begin with the name of the
  316. * subsystem, followed by a period. Zero length string indicates
  317. * end of cftype array.
  318. */
  319. char name[MAX_CFTYPE_NAME];
  320. int private;
  321. /*
  322. * If not 0, file mode is set to this value, otherwise it will
  323. * be figured out automatically
  324. */
  325. umode_t mode;
  326. /*
  327. * The maximum length of string, excluding trailing nul, that can
  328. * be passed to write_string. If < PAGE_SIZE-1, PAGE_SIZE-1 is
  329. * assumed.
  330. */
  331. size_t max_write_len;
  332. /* CFTYPE_* flags */
  333. unsigned int flags;
  334. /*
  335. * Fields used for internal bookkeeping. Initialized automatically
  336. * during registration.
  337. */
  338. struct cgroup_subsys *ss; /* NULL for cgroup core files */
  339. struct list_head node; /* anchored at ss->cfts */
  340. struct kernfs_ops *kf_ops;
  341. /*
  342. * read_u64() is a shortcut for the common case of returning a
  343. * single integer. Use it in place of read()
  344. */
  345. u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
  346. /*
  347. * read_s64() is a signed version of read_u64()
  348. */
  349. s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
  350. /* generic seq_file read interface */
  351. int (*seq_show)(struct seq_file *sf, void *v);
  352. /* optional ops, implement all or none */
  353. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  354. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  355. void (*seq_stop)(struct seq_file *sf, void *v);
  356. /*
  357. * write_u64() is a shortcut for the common case of accepting
  358. * a single integer (as parsed by simple_strtoull) from
  359. * userspace. Use in place of write(); return 0 or error.
  360. */
  361. int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
  362. u64 val);
  363. /*
  364. * write_s64() is a signed version of write_u64()
  365. */
  366. int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
  367. s64 val);
  368. /*
  369. * write_string() is passed a nul-terminated kernelspace
  370. * buffer of maximum length determined by max_write_len.
  371. * Returns 0 or -ve error code.
  372. */
  373. int (*write_string)(struct cgroup_subsys_state *css, struct cftype *cft,
  374. const char *buffer);
  375. /*
  376. * trigger() callback can be used to get some kick from the
  377. * userspace, when the actual string written is not important
  378. * at all. The private field can be used to determine the
  379. * kick type for multiplexing.
  380. */
  381. int (*trigger)(struct cgroup_subsys_state *css, unsigned int event);
  382. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  383. struct lock_class_key lockdep_key;
  384. #endif
  385. };
  386. /*
  387. * See the comment above CGRP_ROOT_SANE_BEHAVIOR for details. This
  388. * function can be called as long as @cgrp is accessible.
  389. */
  390. static inline bool cgroup_sane_behavior(const struct cgroup *cgrp)
  391. {
  392. return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR;
  393. }
  394. /* returns ino associated with a cgroup, 0 indicates unmounted root */
  395. static inline ino_t cgroup_ino(struct cgroup *cgrp)
  396. {
  397. if (cgrp->kn)
  398. return cgrp->kn->ino;
  399. else
  400. return 0;
  401. }
  402. static inline struct cftype *seq_cft(struct seq_file *seq)
  403. {
  404. struct kernfs_open_file *of = seq->private;
  405. return of->kn->priv;
  406. }
  407. struct cgroup_subsys_state *seq_css(struct seq_file *seq);
  408. /*
  409. * Name / path handling functions. All are thin wrappers around the kernfs
  410. * counterparts and can be called under any context.
  411. */
  412. static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
  413. {
  414. return kernfs_name(cgrp->kn, buf, buflen);
  415. }
  416. static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
  417. size_t buflen)
  418. {
  419. return kernfs_path(cgrp->kn, buf, buflen);
  420. }
  421. static inline void pr_cont_cgroup_name(struct cgroup *cgrp)
  422. {
  423. /* dummy_top doesn't have a kn associated */
  424. if (cgrp->kn)
  425. pr_cont_kernfs_name(cgrp->kn);
  426. else
  427. pr_cont("/");
  428. }
  429. static inline void pr_cont_cgroup_path(struct cgroup *cgrp)
  430. {
  431. /* dummy_top doesn't have a kn associated */
  432. if (cgrp->kn)
  433. pr_cont_kernfs_path(cgrp->kn);
  434. else
  435. pr_cont("/");
  436. }
  437. char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
  438. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  439. int cgroup_rm_cftypes(struct cftype *cfts);
  440. bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
  441. int cgroup_task_count(const struct cgroup *cgrp);
  442. /*
  443. * Control Group taskset, used to pass around set of tasks to cgroup_subsys
  444. * methods.
  445. */
  446. struct cgroup_taskset;
  447. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
  448. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
  449. struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
  450. int subsys_id);
  451. int cgroup_taskset_size(struct cgroup_taskset *tset);
  452. /**
  453. * cgroup_taskset_for_each - iterate cgroup_taskset
  454. * @task: the loop cursor
  455. * @skip_css: skip if task's css matches this, %NULL to iterate through all
  456. * @tset: taskset to iterate
  457. */
  458. #define cgroup_taskset_for_each(task, skip_css, tset) \
  459. for ((task) = cgroup_taskset_first((tset)); (task); \
  460. (task) = cgroup_taskset_next((tset))) \
  461. if (!(skip_css) || \
  462. cgroup_taskset_cur_css((tset), \
  463. (skip_css)->ss->id) != (skip_css))
  464. /*
  465. * Control Group subsystem type.
  466. * See Documentation/cgroups/cgroups.txt for details
  467. */
  468. struct cgroup_subsys {
  469. struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
  470. int (*css_online)(struct cgroup_subsys_state *css);
  471. void (*css_offline)(struct cgroup_subsys_state *css);
  472. void (*css_free)(struct cgroup_subsys_state *css);
  473. int (*can_attach)(struct cgroup_subsys_state *css,
  474. struct cgroup_taskset *tset);
  475. void (*cancel_attach)(struct cgroup_subsys_state *css,
  476. struct cgroup_taskset *tset);
  477. void (*attach)(struct cgroup_subsys_state *css,
  478. struct cgroup_taskset *tset);
  479. void (*fork)(struct task_struct *task);
  480. void (*exit)(struct cgroup_subsys_state *css,
  481. struct cgroup_subsys_state *old_css,
  482. struct task_struct *task);
  483. void (*bind)(struct cgroup_subsys_state *root_css);
  484. int disabled;
  485. int early_init;
  486. /*
  487. * If %false, this subsystem is properly hierarchical -
  488. * configuration, resource accounting and restriction on a parent
  489. * cgroup cover those of its children. If %true, hierarchy support
  490. * is broken in some ways - some subsystems ignore hierarchy
  491. * completely while others are only implemented half-way.
  492. *
  493. * It's now disallowed to create nested cgroups if the subsystem is
  494. * broken and cgroup core will emit a warning message on such
  495. * cases. Eventually, all subsystems will be made properly
  496. * hierarchical and this will go away.
  497. */
  498. bool broken_hierarchy;
  499. bool warned_broken_hierarchy;
  500. /* the following two fields are initialized automtically during boot */
  501. int id;
  502. #define MAX_CGROUP_TYPE_NAMELEN 32
  503. const char *name;
  504. /* link to parent, protected by cgroup_lock() */
  505. struct cgroupfs_root *root;
  506. /*
  507. * List of cftypes. Each entry is the first entry of an array
  508. * terminated by zero length name.
  509. */
  510. struct list_head cfts;
  511. /* base cftypes, automatically registered with subsys itself */
  512. struct cftype *base_cftypes;
  513. };
  514. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys;
  515. #include <linux/cgroup_subsys.h>
  516. #undef SUBSYS
  517. /**
  518. * css_parent - find the parent css
  519. * @css: the target cgroup_subsys_state
  520. *
  521. * Return the parent css of @css. This function is guaranteed to return
  522. * non-NULL parent as long as @css isn't the root.
  523. */
  524. static inline
  525. struct cgroup_subsys_state *css_parent(struct cgroup_subsys_state *css)
  526. {
  527. return css->parent;
  528. }
  529. /**
  530. * task_css_set_check - obtain a task's css_set with extra access conditions
  531. * @task: the task to obtain css_set for
  532. * @__c: extra condition expression to be passed to rcu_dereference_check()
  533. *
  534. * A task's css_set is RCU protected, initialized and exited while holding
  535. * task_lock(), and can only be modified while holding both cgroup_mutex
  536. * and task_lock() while the task is alive. This macro verifies that the
  537. * caller is inside proper critical section and returns @task's css_set.
  538. *
  539. * The caller can also specify additional allowed conditions via @__c, such
  540. * as locks used during the cgroup_subsys::attach() methods.
  541. */
  542. #ifdef CONFIG_PROVE_RCU
  543. extern struct mutex cgroup_mutex;
  544. #define task_css_set_check(task, __c) \
  545. rcu_dereference_check((task)->cgroups, \
  546. lockdep_is_held(&(task)->alloc_lock) || \
  547. lockdep_is_held(&cgroup_mutex) || (__c))
  548. #else
  549. #define task_css_set_check(task, __c) \
  550. rcu_dereference((task)->cgroups)
  551. #endif
  552. /**
  553. * task_css_check - obtain css for (task, subsys) w/ extra access conds
  554. * @task: the target task
  555. * @subsys_id: the target subsystem ID
  556. * @__c: extra condition expression to be passed to rcu_dereference_check()
  557. *
  558. * Return the cgroup_subsys_state for the (@task, @subsys_id) pair. The
  559. * synchronization rules are the same as task_css_set_check().
  560. */
  561. #define task_css_check(task, subsys_id, __c) \
  562. task_css_set_check((task), (__c))->subsys[(subsys_id)]
  563. /**
  564. * task_css_set - obtain a task's css_set
  565. * @task: the task to obtain css_set for
  566. *
  567. * See task_css_set_check().
  568. */
  569. static inline struct css_set *task_css_set(struct task_struct *task)
  570. {
  571. return task_css_set_check(task, false);
  572. }
  573. /**
  574. * task_css - obtain css for (task, subsys)
  575. * @task: the target task
  576. * @subsys_id: the target subsystem ID
  577. *
  578. * See task_css_check().
  579. */
  580. static inline struct cgroup_subsys_state *task_css(struct task_struct *task,
  581. int subsys_id)
  582. {
  583. return task_css_check(task, subsys_id, false);
  584. }
  585. static inline struct cgroup *task_cgroup(struct task_struct *task,
  586. int subsys_id)
  587. {
  588. return task_css(task, subsys_id)->cgroup;
  589. }
  590. struct cgroup_subsys_state *css_next_child(struct cgroup_subsys_state *pos,
  591. struct cgroup_subsys_state *parent);
  592. struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss);
  593. /**
  594. * css_for_each_child - iterate through children of a css
  595. * @pos: the css * to use as the loop cursor
  596. * @parent: css whose children to walk
  597. *
  598. * Walk @parent's children. Must be called under rcu_read_lock(). A child
  599. * css which hasn't finished ->css_online() or already has finished
  600. * ->css_offline() may show up during traversal and it's each subsystem's
  601. * responsibility to verify that each @pos is alive.
  602. *
  603. * If a subsystem synchronizes against the parent in its ->css_online() and
  604. * before starting iterating, a css which finished ->css_online() is
  605. * guaranteed to be visible in the future iterations.
  606. *
  607. * It is allowed to temporarily drop RCU read lock during iteration. The
  608. * caller is responsible for ensuring that @pos remains accessible until
  609. * the start of the next iteration by, for example, bumping the css refcnt.
  610. */
  611. #define css_for_each_child(pos, parent) \
  612. for ((pos) = css_next_child(NULL, (parent)); (pos); \
  613. (pos) = css_next_child((pos), (parent)))
  614. struct cgroup_subsys_state *
  615. css_next_descendant_pre(struct cgroup_subsys_state *pos,
  616. struct cgroup_subsys_state *css);
  617. struct cgroup_subsys_state *
  618. css_rightmost_descendant(struct cgroup_subsys_state *pos);
  619. /**
  620. * css_for_each_descendant_pre - pre-order walk of a css's descendants
  621. * @pos: the css * to use as the loop cursor
  622. * @root: css whose descendants to walk
  623. *
  624. * Walk @root's descendants. @root is included in the iteration and the
  625. * first node to be visited. Must be called under rcu_read_lock(). A
  626. * descendant css which hasn't finished ->css_online() or already has
  627. * finished ->css_offline() may show up during traversal and it's each
  628. * subsystem's responsibility to verify that each @pos is alive.
  629. *
  630. * If a subsystem synchronizes against the parent in its ->css_online() and
  631. * before starting iterating, and synchronizes against @pos on each
  632. * iteration, any descendant css which finished ->css_online() is
  633. * guaranteed to be visible in the future iterations.
  634. *
  635. * In other words, the following guarantees that a descendant can't escape
  636. * state updates of its ancestors.
  637. *
  638. * my_online(@css)
  639. * {
  640. * Lock @css's parent and @css;
  641. * Inherit state from the parent;
  642. * Unlock both.
  643. * }
  644. *
  645. * my_update_state(@css)
  646. * {
  647. * css_for_each_descendant_pre(@pos, @css) {
  648. * Lock @pos;
  649. * if (@pos == @css)
  650. * Update @css's state;
  651. * else
  652. * Verify @pos is alive and inherit state from its parent;
  653. * Unlock @pos;
  654. * }
  655. * }
  656. *
  657. * As long as the inheriting step, including checking the parent state, is
  658. * enclosed inside @pos locking, double-locking the parent isn't necessary
  659. * while inheriting. The state update to the parent is guaranteed to be
  660. * visible by walking order and, as long as inheriting operations to the
  661. * same @pos are atomic to each other, multiple updates racing each other
  662. * still result in the correct state. It's guaranateed that at least one
  663. * inheritance happens for any css after the latest update to its parent.
  664. *
  665. * If checking parent's state requires locking the parent, each inheriting
  666. * iteration should lock and unlock both @pos->parent and @pos.
  667. *
  668. * Alternatively, a subsystem may choose to use a single global lock to
  669. * synchronize ->css_online() and ->css_offline() against tree-walking
  670. * operations.
  671. *
  672. * It is allowed to temporarily drop RCU read lock during iteration. The
  673. * caller is responsible for ensuring that @pos remains accessible until
  674. * the start of the next iteration by, for example, bumping the css refcnt.
  675. */
  676. #define css_for_each_descendant_pre(pos, css) \
  677. for ((pos) = css_next_descendant_pre(NULL, (css)); (pos); \
  678. (pos) = css_next_descendant_pre((pos), (css)))
  679. struct cgroup_subsys_state *
  680. css_next_descendant_post(struct cgroup_subsys_state *pos,
  681. struct cgroup_subsys_state *css);
  682. /**
  683. * css_for_each_descendant_post - post-order walk of a css's descendants
  684. * @pos: the css * to use as the loop cursor
  685. * @css: css whose descendants to walk
  686. *
  687. * Similar to css_for_each_descendant_pre() but performs post-order
  688. * traversal instead. @root is included in the iteration and the last
  689. * node to be visited. Note that the walk visibility guarantee described
  690. * in pre-order walk doesn't apply the same to post-order walks.
  691. */
  692. #define css_for_each_descendant_post(pos, css) \
  693. for ((pos) = css_next_descendant_post(NULL, (css)); (pos); \
  694. (pos) = css_next_descendant_post((pos), (css)))
  695. /* A css_task_iter should be treated as an opaque object */
  696. struct css_task_iter {
  697. struct cgroup_subsys_state *origin_css;
  698. struct list_head *cset_link;
  699. struct list_head *task;
  700. };
  701. void css_task_iter_start(struct cgroup_subsys_state *css,
  702. struct css_task_iter *it);
  703. struct task_struct *css_task_iter_next(struct css_task_iter *it);
  704. void css_task_iter_end(struct css_task_iter *it);
  705. int css_scan_tasks(struct cgroup_subsys_state *css,
  706. bool (*test)(struct task_struct *, void *),
  707. void (*process)(struct task_struct *, void *),
  708. void *data, struct ptr_heap *heap);
  709. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
  710. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
  711. struct cgroup_subsys_state *css_tryget_from_dir(struct dentry *dentry,
  712. struct cgroup_subsys *ss);
  713. #else /* !CONFIG_CGROUPS */
  714. static inline int cgroup_init_early(void) { return 0; }
  715. static inline int cgroup_init(void) { return 0; }
  716. static inline void cgroup_fork(struct task_struct *p) {}
  717. static inline void cgroup_post_fork(struct task_struct *p) {}
  718. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  719. static inline int cgroupstats_build(struct cgroupstats *stats,
  720. struct dentry *dentry)
  721. {
  722. return -EINVAL;
  723. }
  724. /* No cgroups - nothing to do */
  725. static inline int cgroup_attach_task_all(struct task_struct *from,
  726. struct task_struct *t)
  727. {
  728. return 0;
  729. }
  730. #endif /* !CONFIG_CGROUPS */
  731. #endif /* _LINUX_CGROUP_H */