cgroup-defs.h 15 KB

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