cgroup-defs.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. /* all css_task_iters currently walking this cset */
  183. struct list_head task_iters;
  184. /* For RCU-protected deletion */
  185. struct rcu_head rcu_head;
  186. };
  187. struct cgroup {
  188. /* self css with NULL ->ss, points back to this cgroup */
  189. struct cgroup_subsys_state self;
  190. unsigned long flags; /* "unsigned long" so bitops work */
  191. /*
  192. * idr allocated in-hierarchy ID.
  193. *
  194. * ID 0 is not used, the ID of the root cgroup is always 1, and a
  195. * new cgroup will be assigned with a smallest available ID.
  196. *
  197. * Allocating/Removing ID must be protected by cgroup_mutex.
  198. */
  199. int id;
  200. /*
  201. * The depth this cgroup is at. The root is at depth zero and each
  202. * step down the hierarchy increments the level. This along with
  203. * ancestor_ids[] can determine whether a given cgroup is a
  204. * descendant of another without traversing the hierarchy.
  205. */
  206. int level;
  207. /*
  208. * Each non-empty css_set associated with this cgroup contributes
  209. * one to populated_cnt. All children with non-zero popuplated_cnt
  210. * of their own contribute one. The count is zero iff there's no
  211. * task in this cgroup or its subtree.
  212. */
  213. int populated_cnt;
  214. struct kernfs_node *kn; /* cgroup kernfs entry */
  215. struct cgroup_file procs_file; /* handle for "cgroup.procs" */
  216. struct cgroup_file events_file; /* handle for "cgroup.events" */
  217. /*
  218. * The bitmask of subsystems enabled on the child cgroups.
  219. * ->subtree_control is the one configured through
  220. * "cgroup.subtree_control" while ->child_subsys_mask is the
  221. * effective one which may have more subsystems enabled.
  222. * Controller knobs are made available iff it's enabled in
  223. * ->subtree_control.
  224. */
  225. unsigned int subtree_control;
  226. unsigned int child_subsys_mask;
  227. /* Private pointers for each registered subsystem */
  228. struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
  229. struct cgroup_root *root;
  230. /*
  231. * List of cgrp_cset_links pointing at css_sets with tasks in this
  232. * cgroup. Protected by css_set_lock.
  233. */
  234. struct list_head cset_links;
  235. /*
  236. * On the default hierarchy, a css_set for a cgroup with some
  237. * susbsys disabled will point to css's which are associated with
  238. * the closest ancestor which has the subsys enabled. The
  239. * following lists all css_sets which point to this cgroup's css
  240. * for the given subsystem.
  241. */
  242. struct list_head e_csets[CGROUP_SUBSYS_COUNT];
  243. /*
  244. * list of pidlists, up to two for each namespace (one for procs, one
  245. * for tasks); created on demand.
  246. */
  247. struct list_head pidlists;
  248. struct mutex pidlist_mutex;
  249. /* used to wait for offlining of csses */
  250. wait_queue_head_t offline_waitq;
  251. /* used to schedule release agent */
  252. struct work_struct release_agent_work;
  253. /* ids of the ancestors at each level including self */
  254. int ancestor_ids[];
  255. };
  256. /*
  257. * A cgroup_root represents the root of a cgroup hierarchy, and may be
  258. * associated with a kernfs_root to form an active hierarchy. This is
  259. * internal to cgroup core. Don't access directly from controllers.
  260. */
  261. struct cgroup_root {
  262. struct kernfs_root *kf_root;
  263. /* The bitmask of subsystems attached to this hierarchy */
  264. unsigned int subsys_mask;
  265. /* Unique id for this hierarchy. */
  266. int hierarchy_id;
  267. /* The root cgroup. Root is destroyed on its release. */
  268. struct cgroup cgrp;
  269. /* for cgrp->ancestor_ids[0] */
  270. int cgrp_ancestor_id_storage;
  271. /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
  272. atomic_t nr_cgrps;
  273. /* A list running through the active hierarchies */
  274. struct list_head root_list;
  275. /* Hierarchy-specific flags */
  276. unsigned int flags;
  277. /* IDs for cgroups in this hierarchy */
  278. struct idr cgroup_idr;
  279. /* The path to use for release notifications. */
  280. char release_agent_path[PATH_MAX];
  281. /* The name for this hierarchy - may be empty */
  282. char name[MAX_CGROUP_ROOT_NAMELEN];
  283. };
  284. /*
  285. * struct cftype: handler definitions for cgroup control files
  286. *
  287. * When reading/writing to a file:
  288. * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
  289. * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
  290. */
  291. struct cftype {
  292. /*
  293. * By convention, the name should begin with the name of the
  294. * subsystem, followed by a period. Zero length string indicates
  295. * end of cftype array.
  296. */
  297. char name[MAX_CFTYPE_NAME];
  298. unsigned long private;
  299. /*
  300. * The maximum length of string, excluding trailing nul, that can
  301. * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
  302. */
  303. size_t max_write_len;
  304. /* CFTYPE_* flags */
  305. unsigned int flags;
  306. /*
  307. * If non-zero, should contain the offset from the start of css to
  308. * a struct cgroup_file field. cgroup will record the handle of
  309. * the created file into it. The recorded handle can be used as
  310. * long as the containing css remains accessible.
  311. */
  312. unsigned int file_offset;
  313. /*
  314. * Fields used for internal bookkeeping. Initialized automatically
  315. * during registration.
  316. */
  317. struct cgroup_subsys *ss; /* NULL for cgroup core files */
  318. struct list_head node; /* anchored at ss->cfts */
  319. struct kernfs_ops *kf_ops;
  320. /*
  321. * read_u64() is a shortcut for the common case of returning a
  322. * single integer. Use it in place of read()
  323. */
  324. u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
  325. /*
  326. * read_s64() is a signed version of read_u64()
  327. */
  328. s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
  329. /* generic seq_file read interface */
  330. int (*seq_show)(struct seq_file *sf, void *v);
  331. /* optional ops, implement all or none */
  332. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  333. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  334. void (*seq_stop)(struct seq_file *sf, void *v);
  335. /*
  336. * write_u64() is a shortcut for the common case of accepting
  337. * a single integer (as parsed by simple_strtoull) from
  338. * userspace. Use in place of write(); return 0 or error.
  339. */
  340. int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
  341. u64 val);
  342. /*
  343. * write_s64() is a signed version of write_u64()
  344. */
  345. int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
  346. s64 val);
  347. /*
  348. * write() is the generic write callback which maps directly to
  349. * kernfs write operation and overrides all other operations.
  350. * Maximum write size is determined by ->max_write_len. Use
  351. * of_css/cft() to access the associated css and cft.
  352. */
  353. ssize_t (*write)(struct kernfs_open_file *of,
  354. char *buf, size_t nbytes, loff_t off);
  355. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  356. struct lock_class_key lockdep_key;
  357. #endif
  358. };
  359. /*
  360. * Control Group subsystem type.
  361. * See Documentation/cgroups/cgroups.txt for details
  362. */
  363. struct cgroup_subsys {
  364. struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
  365. int (*css_online)(struct cgroup_subsys_state *css);
  366. void (*css_offline)(struct cgroup_subsys_state *css);
  367. void (*css_released)(struct cgroup_subsys_state *css);
  368. void (*css_free)(struct cgroup_subsys_state *css);
  369. void (*css_reset)(struct cgroup_subsys_state *css);
  370. void (*css_e_css_changed)(struct cgroup_subsys_state *css);
  371. int (*can_attach)(struct cgroup_subsys_state *css,
  372. struct cgroup_taskset *tset);
  373. void (*cancel_attach)(struct cgroup_subsys_state *css,
  374. struct cgroup_taskset *tset);
  375. void (*attach)(struct cgroup_subsys_state *css,
  376. struct cgroup_taskset *tset);
  377. int (*can_fork)(struct task_struct *task, void **priv_p);
  378. void (*cancel_fork)(struct task_struct *task, void *priv);
  379. void (*fork)(struct task_struct *task, void *priv);
  380. void (*exit)(struct task_struct *task);
  381. void (*free)(struct task_struct *task);
  382. void (*bind)(struct cgroup_subsys_state *root_css);
  383. int early_init;
  384. /*
  385. * If %false, this subsystem is properly hierarchical -
  386. * configuration, resource accounting and restriction on a parent
  387. * cgroup cover those of its children. If %true, hierarchy support
  388. * is broken in some ways - some subsystems ignore hierarchy
  389. * completely while others are only implemented half-way.
  390. *
  391. * It's now disallowed to create nested cgroups if the subsystem is
  392. * broken and cgroup core will emit a warning message on such
  393. * cases. Eventually, all subsystems will be made properly
  394. * hierarchical and this will go away.
  395. */
  396. bool broken_hierarchy;
  397. bool warned_broken_hierarchy;
  398. /* the following two fields are initialized automtically during boot */
  399. int id;
  400. const char *name;
  401. /* optional, initialized automatically during boot if not set */
  402. const char *legacy_name;
  403. /* link to parent, protected by cgroup_lock() */
  404. struct cgroup_root *root;
  405. /* idr for css->id */
  406. struct idr css_idr;
  407. /*
  408. * List of cftypes. Each entry is the first entry of an array
  409. * terminated by zero length name.
  410. */
  411. struct list_head cfts;
  412. /*
  413. * Base cftypes which are automatically registered. The two can
  414. * point to the same array.
  415. */
  416. struct cftype *dfl_cftypes; /* for the default hierarchy */
  417. struct cftype *legacy_cftypes; /* for the legacy hierarchies */
  418. /*
  419. * A subsystem may depend on other subsystems. When such subsystem
  420. * is enabled on a cgroup, the depended-upon subsystems are enabled
  421. * together if available. Subsystems enabled due to dependency are
  422. * not visible to userland until explicitly enabled. The following
  423. * specifies the mask of subsystems that this one depends on.
  424. */
  425. unsigned int depends_on;
  426. };
  427. extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
  428. /**
  429. * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
  430. * @tsk: target task
  431. *
  432. * Called from threadgroup_change_begin() and allows cgroup operations to
  433. * synchronize against threadgroup changes using a percpu_rw_semaphore.
  434. */
  435. static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
  436. {
  437. percpu_down_read(&cgroup_threadgroup_rwsem);
  438. }
  439. /**
  440. * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
  441. * @tsk: target task
  442. *
  443. * Called from threadgroup_change_end(). Counterpart of
  444. * cgroup_threadcgroup_change_begin().
  445. */
  446. static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
  447. {
  448. percpu_up_read(&cgroup_threadgroup_rwsem);
  449. }
  450. #else /* CONFIG_CGROUPS */
  451. #define CGROUP_CANFORK_COUNT 0
  452. #define CGROUP_SUBSYS_COUNT 0
  453. static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk) {}
  454. static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
  455. #endif /* CONFIG_CGROUPS */
  456. #ifdef CONFIG_SOCK_CGROUP_DATA
  457. struct sock_cgroup_data {
  458. u16 prioidx;
  459. u32 classid;
  460. };
  461. static inline u16 sock_cgroup_prioidx(struct sock_cgroup_data *skcd)
  462. {
  463. return skcd->prioidx;
  464. }
  465. static inline u32 sock_cgroup_classid(struct sock_cgroup_data *skcd)
  466. {
  467. return skcd->classid;
  468. }
  469. static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd,
  470. u16 prioidx)
  471. {
  472. skcd->prioidx = prioidx;
  473. }
  474. static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd,
  475. u32 classid)
  476. {
  477. skcd->classid = classid;
  478. }
  479. #else /* CONFIG_SOCK_CGROUP_DATA */
  480. struct sock_cgroup_data {
  481. };
  482. #endif /* CONFIG_SOCK_CGROUP_DATA */
  483. #endif /* _LINUX_CGROUP_DEFS_H */