cgroup.h 26 KB

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