cgroup.h 28 KB

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