cgroup-defs.h 16 KB

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