cgroup-defs.h 16 KB

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