cgroup.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. #include <linux/fs.h>
  22. #ifdef CONFIG_CGROUPS
  23. struct cgroupfs_root;
  24. struct cgroup_subsys;
  25. struct inode;
  26. struct cgroup;
  27. struct css_id;
  28. struct eventfd_ctx;
  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 cgroup_load_subsys(struct cgroup_subsys *ss);
  37. extern void cgroup_unload_subsys(struct cgroup_subsys *ss);
  38. extern int proc_cgroup_show(struct seq_file *, void *);
  39. /*
  40. * Define the enumeration of all cgroup subsystems.
  41. *
  42. * We define ids for builtin subsystems and then modular ones.
  43. */
  44. #define SUBSYS(_x) _x ## _subsys_id,
  45. enum cgroup_subsys_id {
  46. #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  47. #include <linux/cgroup_subsys.h>
  48. #undef IS_SUBSYS_ENABLED
  49. CGROUP_BUILTIN_SUBSYS_COUNT,
  50. __CGROUP_SUBSYS_TEMP_PLACEHOLDER = CGROUP_BUILTIN_SUBSYS_COUNT - 1,
  51. #define IS_SUBSYS_ENABLED(option) IS_MODULE(option)
  52. #include <linux/cgroup_subsys.h>
  53. #undef IS_SUBSYS_ENABLED
  54. CGROUP_SUBSYS_COUNT,
  55. };
  56. #undef SUBSYS
  57. /* Per-subsystem/per-cgroup state maintained by the system. */
  58. struct cgroup_subsys_state {
  59. /*
  60. * The cgroup that this subsystem is attached to. Useful
  61. * for subsystems that want to know about the cgroup
  62. * hierarchy structure
  63. */
  64. struct cgroup *cgroup;
  65. /*
  66. * State maintained by the cgroup system to allow subsystems
  67. * to be "busy". Should be accessed via css_get(),
  68. * css_tryget() and css_put().
  69. */
  70. atomic_t refcnt;
  71. unsigned long flags;
  72. /* ID for this css, if possible */
  73. struct css_id __rcu *id;
  74. /* Used to put @cgroup->dentry on the last css_put() */
  75. struct work_struct dput_work;
  76. };
  77. /* bits in struct cgroup_subsys_state flags field */
  78. enum {
  79. CSS_ROOT = (1 << 0), /* this CSS is the root of the subsystem */
  80. CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
  81. };
  82. /**
  83. * css_get - obtain a reference on the specified css
  84. * @css: target css
  85. *
  86. * The caller must already have a reference.
  87. */
  88. static inline void css_get(struct cgroup_subsys_state *css)
  89. {
  90. /* We don't need to reference count the root state */
  91. if (!(css->flags & CSS_ROOT))
  92. atomic_inc(&css->refcnt);
  93. }
  94. extern bool __css_tryget(struct cgroup_subsys_state *css);
  95. /**
  96. * css_tryget - try to obtain a reference on the specified css
  97. * @css: target css
  98. *
  99. * Obtain a reference on @css if it's alive. The caller naturally needs to
  100. * ensure that @css is accessible but doesn't have to be holding a
  101. * reference on it - IOW, RCU protected access is good enough for this
  102. * function. Returns %true if a reference count was successfully obtained;
  103. * %false otherwise.
  104. */
  105. static inline bool css_tryget(struct cgroup_subsys_state *css)
  106. {
  107. if (css->flags & CSS_ROOT)
  108. return true;
  109. return __css_tryget(css);
  110. }
  111. extern void __css_put(struct cgroup_subsys_state *css);
  112. /**
  113. * css_put - put a css reference
  114. * @css: target css
  115. *
  116. * Put a reference obtained via css_get() and css_tryget().
  117. */
  118. static inline void css_put(struct cgroup_subsys_state *css)
  119. {
  120. if (!(css->flags & CSS_ROOT))
  121. __css_put(css);
  122. }
  123. /* bits in struct cgroup flags field */
  124. enum {
  125. /* Control Group is dead */
  126. CGRP_DEAD,
  127. /*
  128. * Control Group has previously had a child cgroup or a task,
  129. * but no longer (only if CGRP_NOTIFY_ON_RELEASE is set)
  130. */
  131. CGRP_RELEASABLE,
  132. /* Control Group requires release notifications to userspace */
  133. CGRP_NOTIFY_ON_RELEASE,
  134. /*
  135. * Clone the parent's configuration when creating a new child
  136. * cpuset cgroup. For historical reasons, this option can be
  137. * specified at mount time and thus is implemented here.
  138. */
  139. CGRP_CPUSET_CLONE_CHILDREN,
  140. /* see the comment above CGRP_ROOT_SANE_BEHAVIOR for details */
  141. CGRP_SANE_BEHAVIOR,
  142. };
  143. struct cgroup_name {
  144. struct rcu_head rcu_head;
  145. char name[];
  146. };
  147. struct cgroup {
  148. unsigned long flags; /* "unsigned long" so bitops work */
  149. /*
  150. * count users of this cgroup. >0 means busy, but doesn't
  151. * necessarily indicate the number of tasks in the cgroup
  152. */
  153. atomic_t count;
  154. int id; /* ida allocated in-hierarchy ID */
  155. /*
  156. * We link our 'sibling' struct into our parent's 'children'.
  157. * Our children link their 'sibling' into our 'children'.
  158. */
  159. struct list_head sibling; /* my parent's children */
  160. struct list_head children; /* my children */
  161. struct list_head files; /* my files */
  162. struct cgroup *parent; /* my parent */
  163. struct dentry *dentry; /* cgroup fs entry, RCU protected */
  164. /*
  165. * Monotonically increasing unique serial number which defines a
  166. * uniform order among all cgroups. It's guaranteed that all
  167. * ->children lists are in the ascending order of ->serial_nr.
  168. * It's used to allow interrupting and resuming iterations.
  169. */
  170. u64 serial_nr;
  171. /*
  172. * This is a copy of dentry->d_name, and it's needed because
  173. * we can't use dentry->d_name in cgroup_path().
  174. *
  175. * You must acquire rcu_read_lock() to access cgrp->name, and
  176. * the only place that can change it is rename(), which is
  177. * protected by parent dir's i_mutex.
  178. *
  179. * Normally you should use cgroup_name() wrapper rather than
  180. * access it directly.
  181. */
  182. struct cgroup_name __rcu *name;
  183. /* Private pointers for each registered subsystem */
  184. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  185. struct cgroupfs_root *root;
  186. /*
  187. * List of cgrp_cset_links pointing at css_sets with tasks in this
  188. * cgroup. Protected by css_set_lock.
  189. */
  190. struct list_head cset_links;
  191. struct list_head allcg_node; /* cgroupfs_root->allcg_list */
  192. struct list_head cft_q_node; /* used during cftype add/rm */
  193. /*
  194. * Linked list running through all cgroups that can
  195. * potentially be reaped by the release agent. Protected by
  196. * release_list_lock
  197. */
  198. struct list_head release_list;
  199. /*
  200. * list of pidlists, up to two for each namespace (one for procs, one
  201. * for tasks); created on demand.
  202. */
  203. struct list_head pidlists;
  204. struct mutex pidlist_mutex;
  205. /* For RCU-protected deletion */
  206. struct rcu_head rcu_head;
  207. struct work_struct free_work;
  208. /* List of events which userspace want to receive */
  209. struct list_head event_list;
  210. spinlock_t event_list_lock;
  211. /* directory xattrs */
  212. struct simple_xattrs xattrs;
  213. };
  214. #define MAX_CGROUP_ROOT_NAMELEN 64
  215. /* cgroupfs_root->flags */
  216. enum {
  217. /*
  218. * Unfortunately, cgroup core and various controllers are riddled
  219. * with idiosyncrasies and pointless options. The following flag,
  220. * when set, will force sane behavior - some options are forced on,
  221. * others are disallowed, and some controllers will change their
  222. * hierarchical or other behaviors.
  223. *
  224. * The set of behaviors affected by this flag are still being
  225. * determined and developed and the mount option for this flag is
  226. * prefixed with __DEVEL__. The prefix will be dropped once we
  227. * reach the point where all behaviors are compatible with the
  228. * planned unified hierarchy, which will automatically turn on this
  229. * flag.
  230. *
  231. * The followings are the behaviors currently affected this flag.
  232. *
  233. * - Mount options "noprefix" and "clone_children" are disallowed.
  234. * Also, cgroupfs file cgroup.clone_children is not created.
  235. *
  236. * - When mounting an existing superblock, mount options should
  237. * match.
  238. *
  239. * - Remount is disallowed.
  240. *
  241. * - memcg: use_hierarchy is on by default and the cgroup file for
  242. * the flag is not created.
  243. *
  244. * The followings are planned changes.
  245. *
  246. * - release_agent will be disallowed once replacement notification
  247. * mechanism is implemented.
  248. */
  249. CGRP_ROOT_SANE_BEHAVIOR = (1 << 0),
  250. CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
  251. CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
  252. };
  253. /*
  254. * A cgroupfs_root represents the root of a cgroup hierarchy, and may be
  255. * associated with a superblock to form an active hierarchy. This is
  256. * internal to cgroup core. Don't access directly from controllers.
  257. */
  258. struct cgroupfs_root {
  259. struct super_block *sb;
  260. /*
  261. * The bitmask of subsystems intended to be attached to this
  262. * hierarchy
  263. */
  264. unsigned long subsys_mask;
  265. /* Unique id for this hierarchy. */
  266. int hierarchy_id;
  267. /* The bitmask of subsystems currently attached to this hierarchy */
  268. unsigned long actual_subsys_mask;
  269. /* A list running through the attached subsystems */
  270. struct list_head subsys_list;
  271. /* The root cgroup for this hierarchy */
  272. struct cgroup top_cgroup;
  273. /* Tracks how many cgroups are currently defined in hierarchy.*/
  274. int number_of_cgroups;
  275. /* A list running through the active hierarchies */
  276. struct list_head root_list;
  277. /* All cgroups on this root, cgroup_mutex protected */
  278. struct list_head allcg_list;
  279. /* Hierarchy-specific flags */
  280. unsigned long flags;
  281. /* IDs for cgroups in this hierarchy */
  282. struct ida cgroup_ida;
  283. /* The path to use for release notifications. */
  284. char release_agent_path[PATH_MAX];
  285. /* The name for this hierarchy - may be empty */
  286. char name[MAX_CGROUP_ROOT_NAMELEN];
  287. };
  288. /*
  289. * A css_set is a structure holding pointers to a set of
  290. * cgroup_subsys_state objects. This saves space in the task struct
  291. * object and speeds up fork()/exit(), since a single inc/dec and a
  292. * list_add()/del() can bump the reference count on the entire cgroup
  293. * set for a task.
  294. */
  295. struct css_set {
  296. /* Reference count */
  297. atomic_t refcount;
  298. /*
  299. * List running through all cgroup groups in the same hash
  300. * slot. Protected by css_set_lock
  301. */
  302. struct hlist_node hlist;
  303. /*
  304. * List running through all tasks using this cgroup
  305. * group. Protected by css_set_lock
  306. */
  307. struct list_head tasks;
  308. /*
  309. * List of cgrp_cset_links pointing at cgroups referenced from this
  310. * css_set. Protected by css_set_lock.
  311. */
  312. struct list_head cgrp_links;
  313. /*
  314. * Set of subsystem states, one for each subsystem. This array
  315. * is immutable after creation apart from the init_css_set
  316. * during subsystem registration (at boot time) and modular subsystem
  317. * loading/unloading.
  318. */
  319. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  320. /* For RCU-protected deletion */
  321. struct rcu_head rcu_head;
  322. };
  323. /*
  324. * cgroup_map_cb is an abstract callback API for reporting map-valued
  325. * control files
  326. */
  327. struct cgroup_map_cb {
  328. int (*fill)(struct cgroup_map_cb *cb, const char *key, u64 value);
  329. void *state;
  330. };
  331. /*
  332. * struct cftype: handler definitions for cgroup control files
  333. *
  334. * When reading/writing to a file:
  335. * - the cgroup to use is file->f_dentry->d_parent->d_fsdata
  336. * - the 'cftype' of the file is file->f_dentry->d_fsdata
  337. */
  338. /* cftype->flags */
  339. #define CFTYPE_ONLY_ON_ROOT (1U << 0) /* only create on root cg */
  340. #define CFTYPE_NOT_ON_ROOT (1U << 1) /* don't create on root cg */
  341. #define CFTYPE_INSANE (1U << 2) /* don't create if sane_behavior */
  342. #define MAX_CFTYPE_NAME 64
  343. struct cftype {
  344. /*
  345. * By convention, the name should begin with the name of the
  346. * subsystem, followed by a period. Zero length string indicates
  347. * end of cftype array.
  348. */
  349. char name[MAX_CFTYPE_NAME];
  350. int private;
  351. /*
  352. * If not 0, file mode is set to this value, otherwise it will
  353. * be figured out automatically
  354. */
  355. umode_t mode;
  356. /*
  357. * If non-zero, defines the maximum length of string that can
  358. * be passed to write_string; defaults to 64
  359. */
  360. size_t max_write_len;
  361. /* CFTYPE_* flags */
  362. unsigned int flags;
  363. int (*open)(struct inode *inode, struct file *file);
  364. ssize_t (*read)(struct cgroup *cgrp, struct cftype *cft,
  365. struct file *file,
  366. char __user *buf, size_t nbytes, loff_t *ppos);
  367. /*
  368. * read_u64() is a shortcut for the common case of returning a
  369. * single integer. Use it in place of read()
  370. */
  371. u64 (*read_u64)(struct cgroup *cgrp, struct cftype *cft);
  372. /*
  373. * read_s64() is a signed version of read_u64()
  374. */
  375. s64 (*read_s64)(struct cgroup *cgrp, struct cftype *cft);
  376. /*
  377. * read_map() is used for defining a map of key/value
  378. * pairs. It should call cb->fill(cb, key, value) for each
  379. * entry. The key/value pairs (and their ordering) should not
  380. * change between reboots.
  381. */
  382. int (*read_map)(struct cgroup *cont, struct cftype *cft,
  383. struct cgroup_map_cb *cb);
  384. /*
  385. * read_seq_string() is used for outputting a simple sequence
  386. * using seqfile.
  387. */
  388. int (*read_seq_string)(struct cgroup *cont, struct cftype *cft,
  389. struct seq_file *m);
  390. ssize_t (*write)(struct cgroup *cgrp, struct cftype *cft,
  391. struct file *file,
  392. const char __user *buf, size_t nbytes, loff_t *ppos);
  393. /*
  394. * write_u64() is a shortcut for the common case of accepting
  395. * a single integer (as parsed by simple_strtoull) from
  396. * userspace. Use in place of write(); return 0 or error.
  397. */
  398. int (*write_u64)(struct cgroup *cgrp, struct cftype *cft, u64 val);
  399. /*
  400. * write_s64() is a signed version of write_u64()
  401. */
  402. int (*write_s64)(struct cgroup *cgrp, struct cftype *cft, s64 val);
  403. /*
  404. * write_string() is passed a nul-terminated kernelspace
  405. * buffer of maximum length determined by max_write_len.
  406. * Returns 0 or -ve error code.
  407. */
  408. int (*write_string)(struct cgroup *cgrp, struct cftype *cft,
  409. const char *buffer);
  410. /*
  411. * trigger() callback can be used to get some kick from the
  412. * userspace, when the actual string written is not important
  413. * at all. The private field can be used to determine the
  414. * kick type for multiplexing.
  415. */
  416. int (*trigger)(struct cgroup *cgrp, unsigned int event);
  417. int (*release)(struct inode *inode, struct file *file);
  418. /*
  419. * register_event() callback will be used to add new userspace
  420. * waiter for changes related to the cftype. Implement it if
  421. * you want to provide this functionality. Use eventfd_signal()
  422. * on eventfd to send notification to userspace.
  423. */
  424. int (*register_event)(struct cgroup *cgrp, struct cftype *cft,
  425. struct eventfd_ctx *eventfd, const char *args);
  426. /*
  427. * unregister_event() callback will be called when userspace
  428. * closes the eventfd or on cgroup removing.
  429. * This callback must be implemented, if you want provide
  430. * notification functionality.
  431. */
  432. void (*unregister_event)(struct cgroup *cgrp, struct cftype *cft,
  433. struct eventfd_ctx *eventfd);
  434. };
  435. /*
  436. * cftype_sets describe cftypes belonging to a subsystem and are chained at
  437. * cgroup_subsys->cftsets. Each cftset points to an array of cftypes
  438. * terminated by zero length name.
  439. */
  440. struct cftype_set {
  441. struct list_head node; /* chained at subsys->cftsets */
  442. struct cftype *cfts;
  443. };
  444. struct cgroup_scanner {
  445. struct cgroup *cg;
  446. int (*test_task)(struct task_struct *p, struct cgroup_scanner *scan);
  447. void (*process_task)(struct task_struct *p,
  448. struct cgroup_scanner *scan);
  449. struct ptr_heap *heap;
  450. void *data;
  451. };
  452. /*
  453. * See the comment above CGRP_ROOT_SANE_BEHAVIOR for details. This
  454. * function can be called as long as @cgrp is accessible.
  455. */
  456. static inline bool cgroup_sane_behavior(const struct cgroup *cgrp)
  457. {
  458. return cgrp->root->flags & CGRP_ROOT_SANE_BEHAVIOR;
  459. }
  460. /* Caller should hold rcu_read_lock() */
  461. static inline const char *cgroup_name(const struct cgroup *cgrp)
  462. {
  463. return rcu_dereference(cgrp->name)->name;
  464. }
  465. int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  466. int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
  467. bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor);
  468. int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen);
  469. int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
  470. char *buf, size_t buflen);
  471. int cgroup_task_count(const struct cgroup *cgrp);
  472. /*
  473. * Control Group taskset, used to pass around set of tasks to cgroup_subsys
  474. * methods.
  475. */
  476. struct cgroup_taskset;
  477. struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset);
  478. struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset);
  479. struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset);
  480. int cgroup_taskset_size(struct cgroup_taskset *tset);
  481. /**
  482. * cgroup_taskset_for_each - iterate cgroup_taskset
  483. * @task: the loop cursor
  484. * @skip_cgrp: skip if task's cgroup matches this, %NULL to iterate through all
  485. * @tset: taskset to iterate
  486. */
  487. #define cgroup_taskset_for_each(task, skip_cgrp, tset) \
  488. for ((task) = cgroup_taskset_first((tset)); (task); \
  489. (task) = cgroup_taskset_next((tset))) \
  490. if (!(skip_cgrp) || \
  491. cgroup_taskset_cur_cgroup((tset)) != (skip_cgrp))
  492. /*
  493. * Control Group subsystem type.
  494. * See Documentation/cgroups/cgroups.txt for details
  495. */
  496. struct cgroup_subsys {
  497. struct cgroup_subsys_state *(*css_alloc)(struct cgroup *cgrp);
  498. int (*css_online)(struct cgroup *cgrp);
  499. void (*css_offline)(struct cgroup *cgrp);
  500. void (*css_free)(struct cgroup *cgrp);
  501. int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
  502. void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
  503. void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
  504. void (*fork)(struct task_struct *task);
  505. void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
  506. struct task_struct *task);
  507. void (*bind)(struct cgroup *root);
  508. int subsys_id;
  509. int disabled;
  510. int early_init;
  511. /*
  512. * True if this subsys uses ID. ID is not available before cgroup_init()
  513. * (not available in early_init time.)
  514. */
  515. bool use_id;
  516. /*
  517. * If %false, this subsystem is properly hierarchical -
  518. * configuration, resource accounting and restriction on a parent
  519. * cgroup cover those of its children. If %true, hierarchy support
  520. * is broken in some ways - some subsystems ignore hierarchy
  521. * completely while others are only implemented half-way.
  522. *
  523. * It's now disallowed to create nested cgroups if the subsystem is
  524. * broken and cgroup core will emit a warning message on such
  525. * cases. Eventually, all subsystems will be made properly
  526. * hierarchical and this will go away.
  527. */
  528. bool broken_hierarchy;
  529. bool warned_broken_hierarchy;
  530. #define MAX_CGROUP_TYPE_NAMELEN 32
  531. const char *name;
  532. /*
  533. * Link to parent, and list entry in parent's children.
  534. * Protected by cgroup_lock()
  535. */
  536. struct cgroupfs_root *root;
  537. struct list_head sibling;
  538. /* used when use_id == true */
  539. struct idr idr;
  540. spinlock_t id_lock;
  541. /* list of cftype_sets */
  542. struct list_head cftsets;
  543. /* base cftypes, automatically [de]registered with subsys itself */
  544. struct cftype *base_cftypes;
  545. struct cftype_set base_cftset;
  546. /* should be defined only by modular subsystems */
  547. struct module *module;
  548. };
  549. #define SUBSYS(_x) extern struct cgroup_subsys _x ## _subsys;
  550. #define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
  551. #include <linux/cgroup_subsys.h>
  552. #undef IS_SUBSYS_ENABLED
  553. #undef SUBSYS
  554. static inline struct cgroup_subsys_state *cgroup_subsys_state(
  555. struct cgroup *cgrp, int subsys_id)
  556. {
  557. return cgrp->subsys[subsys_id];
  558. }
  559. /*
  560. * function to get the cgroup_subsys_state which allows for extra
  561. * rcu_dereference_check() conditions, such as locks used during the
  562. * cgroup_subsys::attach() methods.
  563. */
  564. #ifdef CONFIG_PROVE_RCU
  565. extern struct mutex cgroup_mutex;
  566. #define task_subsys_state_check(task, subsys_id, __c) \
  567. rcu_dereference_check((task)->cgroups->subsys[(subsys_id)], \
  568. lockdep_is_held(&(task)->alloc_lock) || \
  569. lockdep_is_held(&cgroup_mutex) || (__c))
  570. #else
  571. #define task_subsys_state_check(task, subsys_id, __c) \
  572. rcu_dereference((task)->cgroups->subsys[(subsys_id)])
  573. #endif
  574. static inline struct cgroup_subsys_state *
  575. task_subsys_state(struct task_struct *task, int subsys_id)
  576. {
  577. return task_subsys_state_check(task, subsys_id, false);
  578. }
  579. static inline struct cgroup* task_cgroup(struct task_struct *task,
  580. int subsys_id)
  581. {
  582. return task_subsys_state(task, subsys_id)->cgroup;
  583. }
  584. struct cgroup *cgroup_next_sibling(struct cgroup *pos);
  585. /**
  586. * cgroup_for_each_child - iterate through children of a cgroup
  587. * @pos: the cgroup * to use as the loop cursor
  588. * @cgrp: cgroup whose children to walk
  589. *
  590. * Walk @cgrp's children. Must be called under rcu_read_lock(). A child
  591. * cgroup which hasn't finished ->css_online() or already has finished
  592. * ->css_offline() may show up during traversal and it's each subsystem's
  593. * responsibility to verify that each @pos is alive.
  594. *
  595. * If a subsystem synchronizes against the parent in its ->css_online() and
  596. * before starting iterating, a cgroup which finished ->css_online() is
  597. * guaranteed to be visible in the future iterations.
  598. *
  599. * It is allowed to temporarily drop RCU read lock during iteration. The
  600. * caller is responsible for ensuring that @pos remains accessible until
  601. * the start of the next iteration by, for example, bumping the css refcnt.
  602. */
  603. #define cgroup_for_each_child(pos, cgrp) \
  604. for ((pos) = list_first_or_null_rcu(&(cgrp)->children, \
  605. struct cgroup, sibling); \
  606. (pos); (pos) = cgroup_next_sibling((pos)))
  607. struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
  608. struct cgroup *cgroup);
  609. struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos);
  610. /**
  611. * cgroup_for_each_descendant_pre - pre-order walk of a cgroup's descendants
  612. * @pos: the cgroup * to use as the loop cursor
  613. * @cgroup: cgroup whose descendants to walk
  614. *
  615. * Walk @cgroup's descendants. Must be called under rcu_read_lock(). A
  616. * descendant cgroup which hasn't finished ->css_online() or already has
  617. * finished ->css_offline() may show up during traversal and it's each
  618. * subsystem's responsibility to verify that each @pos is alive.
  619. *
  620. * If a subsystem synchronizes against the parent in its ->css_online() and
  621. * before starting iterating, and synchronizes against @pos on each
  622. * iteration, any descendant cgroup which finished ->css_online() is
  623. * guaranteed to be visible in the future iterations.
  624. *
  625. * In other words, the following guarantees that a descendant can't escape
  626. * state updates of its ancestors.
  627. *
  628. * my_online(@cgrp)
  629. * {
  630. * Lock @cgrp->parent and @cgrp;
  631. * Inherit state from @cgrp->parent;
  632. * Unlock both.
  633. * }
  634. *
  635. * my_update_state(@cgrp)
  636. * {
  637. * Lock @cgrp;
  638. * Update @cgrp's state;
  639. * Unlock @cgrp;
  640. *
  641. * cgroup_for_each_descendant_pre(@pos, @cgrp) {
  642. * Lock @pos;
  643. * Verify @pos is alive and inherit state from @pos->parent;
  644. * Unlock @pos;
  645. * }
  646. * }
  647. *
  648. * As long as the inheriting step, including checking the parent state, is
  649. * enclosed inside @pos locking, double-locking the parent isn't necessary
  650. * while inheriting. The state update to the parent is guaranteed to be
  651. * visible by walking order and, as long as inheriting operations to the
  652. * same @pos are atomic to each other, multiple updates racing each other
  653. * still result in the correct state. It's guaranateed that at least one
  654. * inheritance happens for any cgroup after the latest update to its
  655. * parent.
  656. *
  657. * If checking parent's state requires locking the parent, each inheriting
  658. * iteration should lock and unlock both @pos->parent and @pos.
  659. *
  660. * Alternatively, a subsystem may choose to use a single global lock to
  661. * synchronize ->css_online() and ->css_offline() against tree-walking
  662. * operations.
  663. *
  664. * It is allowed to temporarily drop RCU read lock during iteration. The
  665. * caller is responsible for ensuring that @pos remains accessible until
  666. * the start of the next iteration by, for example, bumping the css refcnt.
  667. */
  668. #define cgroup_for_each_descendant_pre(pos, cgroup) \
  669. for (pos = cgroup_next_descendant_pre(NULL, (cgroup)); (pos); \
  670. pos = cgroup_next_descendant_pre((pos), (cgroup)))
  671. struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
  672. struct cgroup *cgroup);
  673. /**
  674. * cgroup_for_each_descendant_post - post-order walk of a cgroup's descendants
  675. * @pos: the cgroup * to use as the loop cursor
  676. * @cgroup: cgroup whose descendants to walk
  677. *
  678. * Similar to cgroup_for_each_descendant_pre() but performs post-order
  679. * traversal instead. Note that the walk visibility guarantee described in
  680. * pre-order walk doesn't apply the same to post-order walks.
  681. */
  682. #define cgroup_for_each_descendant_post(pos, cgroup) \
  683. for (pos = cgroup_next_descendant_post(NULL, (cgroup)); (pos); \
  684. pos = cgroup_next_descendant_post((pos), (cgroup)))
  685. /* A cgroup_iter should be treated as an opaque object */
  686. struct cgroup_iter {
  687. struct list_head *cset_link;
  688. struct list_head *task;
  689. };
  690. /*
  691. * To iterate across the tasks in a cgroup:
  692. *
  693. * 1) call cgroup_iter_start to initialize an iterator
  694. *
  695. * 2) call cgroup_iter_next() to retrieve member tasks until it
  696. * returns NULL or until you want to end the iteration
  697. *
  698. * 3) call cgroup_iter_end() to destroy the iterator.
  699. *
  700. * Or, call cgroup_scan_tasks() to iterate through every task in a
  701. * cgroup - cgroup_scan_tasks() holds the css_set_lock when calling
  702. * the test_task() callback, but not while calling the process_task()
  703. * callback.
  704. */
  705. void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it);
  706. struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
  707. struct cgroup_iter *it);
  708. void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it);
  709. int cgroup_scan_tasks(struct cgroup_scanner *scan);
  710. int cgroup_attach_task_all(struct task_struct *from, struct task_struct *);
  711. int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from);
  712. /*
  713. * CSS ID is ID for cgroup_subsys_state structs under subsys. This only works
  714. * if cgroup_subsys.use_id == true. It can be used for looking up and scanning.
  715. * CSS ID is assigned at cgroup allocation (create) automatically
  716. * and removed when subsys calls free_css_id() function. This is because
  717. * the lifetime of cgroup_subsys_state is subsys's matter.
  718. *
  719. * Looking up and scanning function should be called under rcu_read_lock().
  720. * Taking cgroup_mutex is not necessary for following calls.
  721. * But the css returned by this routine can be "not populated yet" or "being
  722. * destroyed". The caller should check css and cgroup's status.
  723. */
  724. /*
  725. * Typically Called at ->destroy(), or somewhere the subsys frees
  726. * cgroup_subsys_state.
  727. */
  728. void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css);
  729. /* Find a cgroup_subsys_state which has given ID */
  730. struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id);
  731. /* Returns true if root is ancestor of cg */
  732. bool css_is_ancestor(struct cgroup_subsys_state *cg,
  733. const struct cgroup_subsys_state *root);
  734. /* Get id and depth of css */
  735. unsigned short css_id(struct cgroup_subsys_state *css);
  736. struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id);
  737. #else /* !CONFIG_CGROUPS */
  738. static inline int cgroup_init_early(void) { return 0; }
  739. static inline int cgroup_init(void) { return 0; }
  740. static inline void cgroup_fork(struct task_struct *p) {}
  741. static inline void cgroup_post_fork(struct task_struct *p) {}
  742. static inline void cgroup_exit(struct task_struct *p, int callbacks) {}
  743. static inline int cgroupstats_build(struct cgroupstats *stats,
  744. struct dentry *dentry)
  745. {
  746. return -EINVAL;
  747. }
  748. /* No cgroups - nothing to do */
  749. static inline int cgroup_attach_task_all(struct task_struct *from,
  750. struct task_struct *t)
  751. {
  752. return 0;
  753. }
  754. #endif /* !CONFIG_CGROUPS */
  755. #endif /* _LINUX_CGROUP_H */