cgroup.h 28 KB

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