cgroup-defs.h 15 KB

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