cgroup-defs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * linux/cgroup-defs.h - basic definitions for cgroup
  3. *
  4. * This file provides basic type and interface. Include this file directly
  5. * only if necessary to avoid cyclic dependencies.
  6. */
  7. #ifndef _LINUX_CGROUP_DEFS_H
  8. #define _LINUX_CGROUP_DEFS_H
  9. #include <linux/limits.h>
  10. #include <linux/list.h>
  11. #include <linux/idr.h>
  12. #include <linux/wait.h>
  13. #include <linux/mutex.h>
  14. #include <linux/rcupdate.h>
  15. #include <linux/percpu-refcount.h>
  16. #include <linux/percpu-rwsem.h>
  17. #include <linux/workqueue.h>
  18. #ifdef CONFIG_CGROUPS
  19. struct cgroup;
  20. struct cgroup_root;
  21. struct cgroup_subsys;
  22. struct cgroup_taskset;
  23. struct kernfs_node;
  24. struct kernfs_ops;
  25. struct kernfs_open_file;
  26. struct seq_file;
  27. #define MAX_CGROUP_TYPE_NAMELEN 32
  28. #define MAX_CGROUP_ROOT_NAMELEN 64
  29. #define MAX_CFTYPE_NAME 64
  30. /* define the enumeration of all cgroup subsystems */
  31. #define SUBSYS(_x) _x ## _cgrp_id,
  32. #define SUBSYS_TAG(_t) CGROUP_ ## _t, \
  33. __unused_tag_ ## _t = CGROUP_ ## _t - 1,
  34. enum cgroup_subsys_id {
  35. #include <linux/cgroup_subsys.h>
  36. CGROUP_SUBSYS_COUNT,
  37. };
  38. #undef SUBSYS_TAG
  39. #undef SUBSYS
  40. #define CGROUP_CANFORK_COUNT (CGROUP_CANFORK_END - CGROUP_CANFORK_START)
  41. /* bits in struct cgroup_subsys_state flags field */
  42. enum {
  43. CSS_NO_REF = (1 << 0), /* no reference counting for this css */
  44. CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
  45. CSS_RELEASED = (1 << 2), /* refcnt reached zero, released */
  46. };
  47. /* bits in struct cgroup flags field */
  48. enum {
  49. /* Control Group requires release notifications to userspace */
  50. CGRP_NOTIFY_ON_RELEASE,
  51. /*
  52. * Clone the parent's configuration when creating a new child
  53. * cpuset cgroup. For historical reasons, this option can be
  54. * specified at mount time and thus is implemented here.
  55. */
  56. CGRP_CPUSET_CLONE_CHILDREN,
  57. };
  58. /* cgroup_root->flags */
  59. enum {
  60. CGRP_ROOT_SANE_BEHAVIOR = (1 << 0), /* __DEVEL__sane_behavior specified */
  61. CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
  62. CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
  63. };
  64. /* cftype->flags */
  65. enum {
  66. CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
  67. CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
  68. CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
  69. /* internal flags, do not use outside cgroup core proper */
  70. __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
  71. __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */
  72. };
  73. /*
  74. * Per-subsystem/per-cgroup state maintained by the system. This is the
  75. * fundamental structural building block that controllers deal with.
  76. *
  77. * Fields marked with "PI:" are public and immutable and may be accessed
  78. * directly without synchronization.
  79. */
  80. struct cgroup_subsys_state {
  81. /* PI: the cgroup that this css is attached to */
  82. struct cgroup *cgroup;
  83. /* PI: the cgroup subsystem that this css is attached to */
  84. struct cgroup_subsys *ss;
  85. /* reference count - access via css_[try]get() and css_put() */
  86. struct percpu_ref refcnt;
  87. /* PI: the parent css */
  88. struct cgroup_subsys_state *parent;
  89. /* siblings list anchored at the parent's ->children */
  90. struct list_head sibling;
  91. struct list_head children;
  92. /*
  93. * PI: Subsys-unique ID. 0 is unused and root is always 1. The
  94. * matching css can be looked up using css_from_id().
  95. */
  96. int id;
  97. unsigned int flags;
  98. /*
  99. * Monotonically increasing unique serial number which defines a
  100. * uniform order among all csses. It's guaranteed that all
  101. * ->children lists are in the ascending order of ->serial_nr and
  102. * used to allow interrupting and resuming iterations.
  103. */
  104. u64 serial_nr;
  105. /* percpu_ref killing and RCU release */
  106. struct rcu_head rcu_head;
  107. struct work_struct destroy_work;
  108. };
  109. /*
  110. * A css_set is a structure holding pointers to a set of
  111. * cgroup_subsys_state objects. This saves space in the task struct
  112. * object and speeds up fork()/exit(), since a single inc/dec and a
  113. * list_add()/del() can bump the reference count on the entire cgroup
  114. * set for a task.
  115. */
  116. struct css_set {
  117. /* Reference count */
  118. atomic_t refcount;
  119. /*
  120. * List running through all cgroup groups in the same hash
  121. * slot. Protected by css_set_lock
  122. */
  123. struct hlist_node hlist;
  124. /*
  125. * Lists running through all tasks using this cgroup group.
  126. * mg_tasks lists tasks which belong to this cset but are in the
  127. * process of being migrated out or in. Protected by
  128. * css_set_rwsem, but, during migration, once tasks are moved to
  129. * mg_tasks, it can be read safely while holding cgroup_mutex.
  130. */
  131. struct list_head tasks;
  132. struct list_head mg_tasks;
  133. /*
  134. * List of cgrp_cset_links pointing at cgroups referenced from this
  135. * css_set. Protected by css_set_lock.
  136. */
  137. struct list_head cgrp_links;
  138. /* the default cgroup associated with this css_set */
  139. struct cgroup *dfl_cgrp;
  140. /*
  141. * Set of subsystem states, one for each subsystem. This array is
  142. * immutable after creation apart from the init_css_set during
  143. * subsystem registration (at boot time).
  144. */
  145. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  146. /*
  147. * List of csets participating in the on-going migration either as
  148. * source or destination. Protected by cgroup_mutex.
  149. */
  150. struct list_head mg_preload_node;
  151. struct list_head mg_node;
  152. /*
  153. * If this cset is acting as the source of migration the following
  154. * two fields are set. mg_src_cgrp is the source cgroup of the
  155. * on-going migration and mg_dst_cset is the destination cset the
  156. * target tasks on this cset should be migrated to. Protected by
  157. * cgroup_mutex.
  158. */
  159. struct cgroup *mg_src_cgrp;
  160. struct css_set *mg_dst_cset;
  161. /*
  162. * On the default hierarhcy, ->subsys[ssid] may point to a css
  163. * attached to an ancestor instead of the cgroup this css_set is
  164. * associated with. The following node is anchored at
  165. * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
  166. * iterate through all css's attached to a given cgroup.
  167. */
  168. struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
  169. /* For RCU-protected deletion */
  170. struct rcu_head rcu_head;
  171. };
  172. struct cgroup {
  173. /* self css with NULL ->ss, points back to this cgroup */
  174. struct cgroup_subsys_state self;
  175. unsigned long flags; /* "unsigned long" so bitops work */
  176. /*
  177. * idr allocated in-hierarchy ID.
  178. *
  179. * ID 0 is not used, the ID of the root cgroup is always 1, and a
  180. * new cgroup will be assigned with a smallest available ID.
  181. *
  182. * Allocating/Removing ID must be protected by cgroup_mutex.
  183. */
  184. int id;
  185. /*
  186. * If this cgroup contains any tasks, it contributes one to
  187. * populated_cnt. All children with non-zero popuplated_cnt of
  188. * their own contribute one. The count is zero iff there's no task
  189. * in this cgroup or its subtree.
  190. */
  191. int populated_cnt;
  192. struct kernfs_node *kn; /* cgroup kernfs entry */
  193. struct kernfs_node *procs_kn; /* kn for "cgroup.procs" */
  194. struct kernfs_node *populated_kn; /* kn for "cgroup.subtree_populated" */
  195. /*
  196. * The bitmask of subsystems enabled on the child cgroups.
  197. * ->subtree_control is the one configured through
  198. * "cgroup.subtree_control" while ->child_subsys_mask is the
  199. * effective one which may have more subsystems enabled.
  200. * Controller knobs are made available iff it's enabled in
  201. * ->subtree_control.
  202. */
  203. unsigned int subtree_control;
  204. unsigned int child_subsys_mask;
  205. /* Private pointers for each registered subsystem */
  206. struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
  207. struct cgroup_root *root;
  208. /*
  209. * List of cgrp_cset_links pointing at css_sets with tasks in this
  210. * cgroup. Protected by css_set_lock.
  211. */
  212. struct list_head cset_links;
  213. /*
  214. * On the default hierarchy, a css_set for a cgroup with some
  215. * susbsys disabled will point to css's which are associated with
  216. * the closest ancestor which has the subsys enabled. The
  217. * following lists all css_sets which point to this cgroup's css
  218. * for the given subsystem.
  219. */
  220. struct list_head e_csets[CGROUP_SUBSYS_COUNT];
  221. /*
  222. * list of pidlists, up to two for each namespace (one for procs, one
  223. * for tasks); created on demand.
  224. */
  225. struct list_head pidlists;
  226. struct mutex pidlist_mutex;
  227. /* used to wait for offlining of csses */
  228. wait_queue_head_t offline_waitq;
  229. /* used to schedule release agent */
  230. struct work_struct release_agent_work;
  231. };
  232. /*
  233. * A cgroup_root represents the root of a cgroup hierarchy, and may be
  234. * associated with a kernfs_root to form an active hierarchy. This is
  235. * internal to cgroup core. Don't access directly from controllers.
  236. */
  237. struct cgroup_root {
  238. struct kernfs_root *kf_root;
  239. /* The bitmask of subsystems attached to this hierarchy */
  240. unsigned int subsys_mask;
  241. /* Unique id for this hierarchy. */
  242. int hierarchy_id;
  243. /* The root cgroup. Root is destroyed on its release. */
  244. struct cgroup cgrp;
  245. /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
  246. atomic_t nr_cgrps;
  247. /* A list running through the active hierarchies */
  248. struct list_head root_list;
  249. /* Hierarchy-specific flags */
  250. unsigned int flags;
  251. /* IDs for cgroups in this hierarchy */
  252. struct idr cgroup_idr;
  253. /* The path to use for release notifications. */
  254. char release_agent_path[PATH_MAX];
  255. /* The name for this hierarchy - may be empty */
  256. char name[MAX_CGROUP_ROOT_NAMELEN];
  257. };
  258. /*
  259. * struct cftype: handler definitions for cgroup control files
  260. *
  261. * When reading/writing to a file:
  262. * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
  263. * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
  264. */
  265. struct cftype {
  266. /*
  267. * By convention, the name should begin with the name of the
  268. * subsystem, followed by a period. Zero length string indicates
  269. * end of cftype array.
  270. */
  271. char name[MAX_CFTYPE_NAME];
  272. unsigned long private;
  273. /*
  274. * If not 0, file mode is set to this value, otherwise it will
  275. * be figured out automatically
  276. */
  277. umode_t mode;
  278. /*
  279. * The maximum length of string, excluding trailing nul, that can
  280. * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
  281. */
  282. size_t max_write_len;
  283. /* CFTYPE_* flags */
  284. unsigned int flags;
  285. /*
  286. * Fields used for internal bookkeeping. Initialized automatically
  287. * during registration.
  288. */
  289. struct cgroup_subsys *ss; /* NULL for cgroup core files */
  290. struct list_head node; /* anchored at ss->cfts */
  291. struct kernfs_ops *kf_ops;
  292. /*
  293. * read_u64() is a shortcut for the common case of returning a
  294. * single integer. Use it in place of read()
  295. */
  296. u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
  297. /*
  298. * read_s64() is a signed version of read_u64()
  299. */
  300. s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
  301. /* generic seq_file read interface */
  302. int (*seq_show)(struct seq_file *sf, void *v);
  303. /* optional ops, implement all or none */
  304. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  305. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  306. void (*seq_stop)(struct seq_file *sf, void *v);
  307. /*
  308. * write_u64() is a shortcut for the common case of accepting
  309. * a single integer (as parsed by simple_strtoull) from
  310. * userspace. Use in place of write(); return 0 or error.
  311. */
  312. int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
  313. u64 val);
  314. /*
  315. * write_s64() is a signed version of write_u64()
  316. */
  317. int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
  318. s64 val);
  319. /*
  320. * write() is the generic write callback which maps directly to
  321. * kernfs write operation and overrides all other operations.
  322. * Maximum write size is determined by ->max_write_len. Use
  323. * of_css/cft() to access the associated css and cft.
  324. */
  325. ssize_t (*write)(struct kernfs_open_file *of,
  326. char *buf, size_t nbytes, loff_t off);
  327. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  328. struct lock_class_key lockdep_key;
  329. #endif
  330. };
  331. /*
  332. * Control Group subsystem type.
  333. * See Documentation/cgroups/cgroups.txt for details
  334. */
  335. struct cgroup_subsys {
  336. struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
  337. int (*css_online)(struct cgroup_subsys_state *css);
  338. void (*css_offline)(struct cgroup_subsys_state *css);
  339. void (*css_released)(struct cgroup_subsys_state *css);
  340. void (*css_free)(struct cgroup_subsys_state *css);
  341. void (*css_reset)(struct cgroup_subsys_state *css);
  342. void (*css_e_css_changed)(struct cgroup_subsys_state *css);
  343. int (*can_attach)(struct cgroup_subsys_state *css,
  344. struct cgroup_taskset *tset);
  345. void (*cancel_attach)(struct cgroup_subsys_state *css,
  346. struct cgroup_taskset *tset);
  347. void (*attach)(struct cgroup_subsys_state *css,
  348. struct cgroup_taskset *tset);
  349. int (*can_fork)(struct task_struct *task, void **priv_p);
  350. void (*cancel_fork)(struct task_struct *task, void *priv);
  351. void (*fork)(struct task_struct *task, void *priv);
  352. void (*exit)(struct cgroup_subsys_state *css,
  353. struct cgroup_subsys_state *old_css,
  354. struct task_struct *task);
  355. void (*bind)(struct cgroup_subsys_state *root_css);
  356. int disabled;
  357. int early_init;
  358. /*
  359. * If %false, this subsystem is properly hierarchical -
  360. * configuration, resource accounting and restriction on a parent
  361. * cgroup cover those of its children. If %true, hierarchy support
  362. * is broken in some ways - some subsystems ignore hierarchy
  363. * completely while others are only implemented half-way.
  364. *
  365. * It's now disallowed to create nested cgroups if the subsystem is
  366. * broken and cgroup core will emit a warning message on such
  367. * cases. Eventually, all subsystems will be made properly
  368. * hierarchical and this will go away.
  369. */
  370. bool broken_hierarchy;
  371. bool warned_broken_hierarchy;
  372. /* the following two fields are initialized automtically during boot */
  373. int id;
  374. const char *name;
  375. /* optional, initialized automatically during boot if not set */
  376. const char *legacy_name;
  377. /* link to parent, protected by cgroup_lock() */
  378. struct cgroup_root *root;
  379. /* idr for css->id */
  380. struct idr css_idr;
  381. /*
  382. * List of cftypes. Each entry is the first entry of an array
  383. * terminated by zero length name.
  384. */
  385. struct list_head cfts;
  386. /*
  387. * Base cftypes which are automatically registered. The two can
  388. * point to the same array.
  389. */
  390. struct cftype *dfl_cftypes; /* for the default hierarchy */
  391. struct cftype *legacy_cftypes; /* for the legacy hierarchies */
  392. /*
  393. * A subsystem may depend on other subsystems. When such subsystem
  394. * is enabled on a cgroup, the depended-upon subsystems are enabled
  395. * together if available. Subsystems enabled due to dependency are
  396. * not visible to userland until explicitly enabled. The following
  397. * specifies the mask of subsystems that this one depends on.
  398. */
  399. unsigned int depends_on;
  400. };
  401. void cgroup_threadgroup_change_begin(struct task_struct *tsk);
  402. void cgroup_threadgroup_change_end(struct task_struct *tsk);
  403. #else /* CONFIG_CGROUPS */
  404. #define CGROUP_CANFORK_COUNT 0
  405. #define CGROUP_SUBSYS_COUNT 0
  406. static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) {}
  407. static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
  408. #endif /* CONFIG_CGROUPS */
  409. #endif /* _LINUX_CGROUP_DEFS_H */