cgroup.h 30 KB

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