cgroup-defs.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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/refcount.h>
  16. #include <linux/percpu-refcount.h>
  17. #include <linux/percpu-rwsem.h>
  18. #include <linux/u64_stats_sync.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/bpf-cgroup.h>
  21. #ifdef CONFIG_CGROUPS
  22. struct cgroup;
  23. struct cgroup_root;
  24. struct cgroup_subsys;
  25. struct cgroup_taskset;
  26. struct kernfs_node;
  27. struct kernfs_ops;
  28. struct kernfs_open_file;
  29. struct seq_file;
  30. #define MAX_CGROUP_TYPE_NAMELEN 32
  31. #define MAX_CGROUP_ROOT_NAMELEN 64
  32. #define MAX_CFTYPE_NAME 64
  33. /* define the enumeration of all cgroup subsystems */
  34. #define SUBSYS(_x) _x ## _cgrp_id,
  35. enum cgroup_subsys_id {
  36. #include <linux/cgroup_subsys.h>
  37. CGROUP_SUBSYS_COUNT,
  38. };
  39. #undef SUBSYS
  40. /* bits in struct cgroup_subsys_state flags field */
  41. enum {
  42. CSS_NO_REF = (1 << 0), /* no reference counting for this css */
  43. CSS_ONLINE = (1 << 1), /* between ->css_online() and ->css_offline() */
  44. CSS_RELEASED = (1 << 2), /* refcnt reached zero, released */
  45. CSS_VISIBLE = (1 << 3), /* css is visible to userland */
  46. CSS_DYING = (1 << 4), /* css is dying */
  47. };
  48. /* bits in struct cgroup flags field */
  49. enum {
  50. /* Control Group requires release notifications to userspace */
  51. CGRP_NOTIFY_ON_RELEASE,
  52. /*
  53. * Clone the parent's configuration when creating a new child
  54. * cpuset cgroup. For historical reasons, this option can be
  55. * specified at mount time and thus is implemented here.
  56. */
  57. CGRP_CPUSET_CLONE_CHILDREN,
  58. };
  59. /* cgroup_root->flags */
  60. enum {
  61. CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
  62. CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
  63. /*
  64. * Consider namespaces as delegation boundaries. If this flag is
  65. * set, controller specific interface files in a namespace root
  66. * aren't writeable from inside the namespace.
  67. */
  68. CGRP_ROOT_NS_DELEGATE = (1 << 3),
  69. /*
  70. * Enable cpuset controller in v1 cgroup to use v2 behavior.
  71. */
  72. CGRP_ROOT_CPUSET_V2_MODE = (1 << 4),
  73. };
  74. /* cftype->flags */
  75. enum {
  76. CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
  77. CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
  78. CFTYPE_NS_DELEGATABLE = (1 << 2), /* writeable beyond delegation boundaries */
  79. CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
  80. CFTYPE_WORLD_WRITABLE = (1 << 4), /* (DON'T USE FOR NEW FILES) S_IWUGO */
  81. /* internal flags, do not use outside cgroup core proper */
  82. __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
  83. __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */
  84. };
  85. /*
  86. * cgroup_file is the handle for a file instance created in a cgroup which
  87. * is used, for example, to generate file changed notifications. This can
  88. * be obtained by setting cftype->file_offset.
  89. */
  90. struct cgroup_file {
  91. /* do not access any fields from outside cgroup core */
  92. struct kernfs_node *kn;
  93. };
  94. /*
  95. * Per-subsystem/per-cgroup state maintained by the system. This is the
  96. * fundamental structural building block that controllers deal with.
  97. *
  98. * Fields marked with "PI:" are public and immutable and may be accessed
  99. * directly without synchronization.
  100. */
  101. struct cgroup_subsys_state {
  102. /* PI: the cgroup that this css is attached to */
  103. struct cgroup *cgroup;
  104. /* PI: the cgroup subsystem that this css is attached to */
  105. struct cgroup_subsys *ss;
  106. /* reference count - access via css_[try]get() and css_put() */
  107. struct percpu_ref refcnt;
  108. /* siblings list anchored at the parent's ->children */
  109. struct list_head sibling;
  110. struct list_head children;
  111. /*
  112. * PI: Subsys-unique ID. 0 is unused and root is always 1. The
  113. * matching css can be looked up using css_from_id().
  114. */
  115. int id;
  116. unsigned int flags;
  117. /*
  118. * Monotonically increasing unique serial number which defines a
  119. * uniform order among all csses. It's guaranteed that all
  120. * ->children lists are in the ascending order of ->serial_nr and
  121. * used to allow interrupting and resuming iterations.
  122. */
  123. u64 serial_nr;
  124. /*
  125. * Incremented by online self and children. Used to guarantee that
  126. * parents are not offlined before their children.
  127. */
  128. atomic_t online_cnt;
  129. /* percpu_ref killing and RCU release */
  130. struct rcu_head rcu_head;
  131. struct work_struct destroy_work;
  132. /*
  133. * PI: the parent css. Placed here for cache proximity to following
  134. * fields of the containing structure.
  135. */
  136. struct cgroup_subsys_state *parent;
  137. };
  138. /*
  139. * A css_set is a structure holding pointers to a set of
  140. * cgroup_subsys_state objects. This saves space in the task struct
  141. * object and speeds up fork()/exit(), since a single inc/dec and a
  142. * list_add()/del() can bump the reference count on the entire cgroup
  143. * set for a task.
  144. */
  145. struct css_set {
  146. /*
  147. * Set of subsystem states, one for each subsystem. This array is
  148. * immutable after creation apart from the init_css_set during
  149. * subsystem registration (at boot time).
  150. */
  151. struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
  152. /* reference count */
  153. refcount_t refcount;
  154. /*
  155. * For a domain cgroup, the following points to self. If threaded,
  156. * to the matching cset of the nearest domain ancestor. The
  157. * dom_cset provides access to the domain cgroup and its csses to
  158. * which domain level resource consumptions should be charged.
  159. */
  160. struct css_set *dom_cset;
  161. /* the default cgroup associated with this css_set */
  162. struct cgroup *dfl_cgrp;
  163. /* internal task count, protected by css_set_lock */
  164. int nr_tasks;
  165. /*
  166. * Lists running through all tasks using this cgroup group.
  167. * mg_tasks lists tasks which belong to this cset but are in the
  168. * process of being migrated out or in. Protected by
  169. * css_set_rwsem, but, during migration, once tasks are moved to
  170. * mg_tasks, it can be read safely while holding cgroup_mutex.
  171. */
  172. struct list_head tasks;
  173. struct list_head mg_tasks;
  174. /* all css_task_iters currently walking this cset */
  175. struct list_head task_iters;
  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 threaded csets whose ->dom_cset points to this cset */
  185. struct list_head threaded_csets;
  186. struct list_head threaded_csets_node;
  187. /*
  188. * List running through all cgroup groups in the same hash
  189. * slot. Protected by css_set_lock
  190. */
  191. struct hlist_node hlist;
  192. /*
  193. * List of cgrp_cset_links pointing at cgroups referenced from this
  194. * css_set. Protected by css_set_lock.
  195. */
  196. struct list_head cgrp_links;
  197. /*
  198. * List of csets participating in the on-going migration either as
  199. * source or destination. Protected by cgroup_mutex.
  200. */
  201. struct list_head mg_preload_node;
  202. struct list_head mg_node;
  203. /*
  204. * If this cset is acting as the source of migration the following
  205. * two fields are set. mg_src_cgrp and mg_dst_cgrp are
  206. * respectively the source and destination cgroups of the on-going
  207. * migration. mg_dst_cset is the destination cset the target tasks
  208. * on this cset should be migrated to. Protected by cgroup_mutex.
  209. */
  210. struct cgroup *mg_src_cgrp;
  211. struct cgroup *mg_dst_cgrp;
  212. struct css_set *mg_dst_cset;
  213. /* dead and being drained, ignore for migration */
  214. bool dead;
  215. /* For RCU-protected deletion */
  216. struct rcu_head rcu_head;
  217. };
  218. /*
  219. * cgroup basic resource usage statistics. Accounting is done per-cpu in
  220. * cgroup_cpu_stat which is then lazily propagated up the hierarchy on
  221. * reads.
  222. *
  223. * When a stat gets updated, the cgroup_cpu_stat and its ancestors are
  224. * linked into the updated tree. On the following read, propagation only
  225. * considers and consumes the updated tree. This makes reading O(the
  226. * number of descendants which have been active since last read) instead of
  227. * O(the total number of descendants).
  228. *
  229. * This is important because there can be a lot of (draining) cgroups which
  230. * aren't active and stat may be read frequently. The combination can
  231. * become very expensive. By propagating selectively, increasing reading
  232. * frequency decreases the cost of each read.
  233. */
  234. struct cgroup_cpu_stat {
  235. /*
  236. * ->sync protects all the current counters. These are the only
  237. * fields which get updated in the hot path.
  238. */
  239. struct u64_stats_sync sync;
  240. struct task_cputime cputime;
  241. /*
  242. * Snapshots at the last reading. These are used to calculate the
  243. * deltas to propagate to the global counters.
  244. */
  245. struct task_cputime last_cputime;
  246. /*
  247. * Child cgroups with stat updates on this cpu since the last read
  248. * are linked on the parent's ->updated_children through
  249. * ->updated_next.
  250. *
  251. * In addition to being more compact, singly-linked list pointing
  252. * to the cgroup makes it unnecessary for each per-cpu struct to
  253. * point back to the associated cgroup.
  254. *
  255. * Protected by per-cpu cgroup_cpu_stat_lock.
  256. */
  257. struct cgroup *updated_children; /* terminated by self cgroup */
  258. struct cgroup *updated_next; /* NULL iff not on the list */
  259. };
  260. struct cgroup_stat {
  261. /* per-cpu statistics are collected into the folowing global counters */
  262. struct task_cputime cputime;
  263. struct prev_cputime prev_cputime;
  264. };
  265. struct cgroup {
  266. /* self css with NULL ->ss, points back to this cgroup */
  267. struct cgroup_subsys_state self;
  268. unsigned long flags; /* "unsigned long" so bitops work */
  269. /*
  270. * idr allocated in-hierarchy ID.
  271. *
  272. * ID 0 is not used, the ID of the root cgroup is always 1, and a
  273. * new cgroup will be assigned with a smallest available ID.
  274. *
  275. * Allocating/Removing ID must be protected by cgroup_mutex.
  276. */
  277. int id;
  278. /*
  279. * The depth this cgroup is at. The root is at depth zero and each
  280. * step down the hierarchy increments the level. This along with
  281. * ancestor_ids[] can determine whether a given cgroup is a
  282. * descendant of another without traversing the hierarchy.
  283. */
  284. int level;
  285. /* Maximum allowed descent tree depth */
  286. int max_depth;
  287. /*
  288. * Keep track of total numbers of visible and dying descent cgroups.
  289. * Dying cgroups are cgroups which were deleted by a user,
  290. * but are still existing because someone else is holding a reference.
  291. * max_descendants is a maximum allowed number of descent cgroups.
  292. */
  293. int nr_descendants;
  294. int nr_dying_descendants;
  295. int max_descendants;
  296. /*
  297. * Each non-empty css_set associated with this cgroup contributes
  298. * one to nr_populated_csets. The counter is zero iff this cgroup
  299. * doesn't have any tasks.
  300. *
  301. * All children which have non-zero nr_populated_csets and/or
  302. * nr_populated_children of their own contribute one to either
  303. * nr_populated_domain_children or nr_populated_threaded_children
  304. * depending on their type. Each counter is zero iff all cgroups
  305. * of the type in the subtree proper don't have any tasks.
  306. */
  307. int nr_populated_csets;
  308. int nr_populated_domain_children;
  309. int nr_populated_threaded_children;
  310. int nr_threaded_children; /* # of live threaded child cgroups */
  311. struct kernfs_node *kn; /* cgroup kernfs entry */
  312. struct cgroup_file procs_file; /* handle for "cgroup.procs" */
  313. struct cgroup_file events_file; /* handle for "cgroup.events" */
  314. /*
  315. * The bitmask of subsystems enabled on the child cgroups.
  316. * ->subtree_control is the one configured through
  317. * "cgroup.subtree_control" while ->child_ss_mask is the effective
  318. * one which may have more subsystems enabled. Controller knobs
  319. * are made available iff it's enabled in ->subtree_control.
  320. */
  321. u16 subtree_control;
  322. u16 subtree_ss_mask;
  323. u16 old_subtree_control;
  324. u16 old_subtree_ss_mask;
  325. /* Private pointers for each registered subsystem */
  326. struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
  327. struct cgroup_root *root;
  328. /*
  329. * List of cgrp_cset_links pointing at css_sets with tasks in this
  330. * cgroup. Protected by css_set_lock.
  331. */
  332. struct list_head cset_links;
  333. /*
  334. * On the default hierarchy, a css_set for a cgroup with some
  335. * susbsys disabled will point to css's which are associated with
  336. * the closest ancestor which has the subsys enabled. The
  337. * following lists all css_sets which point to this cgroup's css
  338. * for the given subsystem.
  339. */
  340. struct list_head e_csets[CGROUP_SUBSYS_COUNT];
  341. /*
  342. * If !threaded, self. If threaded, it points to the nearest
  343. * domain ancestor. Inside a threaded subtree, cgroups are exempt
  344. * from process granularity and no-internal-task constraint.
  345. * Domain level resource consumptions which aren't tied to a
  346. * specific task are charged to the dom_cgrp.
  347. */
  348. struct cgroup *dom_cgrp;
  349. /* cgroup basic resource statistics */
  350. struct cgroup_cpu_stat __percpu *cpu_stat;
  351. struct cgroup_stat pending_stat; /* pending from children */
  352. struct cgroup_stat stat;
  353. /*
  354. * list of pidlists, up to two for each namespace (one for procs, one
  355. * for tasks); created on demand.
  356. */
  357. struct list_head pidlists;
  358. struct mutex pidlist_mutex;
  359. /* used to wait for offlining of csses */
  360. wait_queue_head_t offline_waitq;
  361. /* used to schedule release agent */
  362. struct work_struct release_agent_work;
  363. /* used to store eBPF programs */
  364. struct cgroup_bpf bpf;
  365. /* ids of the ancestors at each level including self */
  366. int ancestor_ids[];
  367. };
  368. /*
  369. * A cgroup_root represents the root of a cgroup hierarchy, and may be
  370. * associated with a kernfs_root to form an active hierarchy. This is
  371. * internal to cgroup core. Don't access directly from controllers.
  372. */
  373. struct cgroup_root {
  374. struct kernfs_root *kf_root;
  375. /* The bitmask of subsystems attached to this hierarchy */
  376. unsigned int subsys_mask;
  377. /* Unique id for this hierarchy. */
  378. int hierarchy_id;
  379. /* The root cgroup. Root is destroyed on its release. */
  380. struct cgroup cgrp;
  381. /* for cgrp->ancestor_ids[0] */
  382. int cgrp_ancestor_id_storage;
  383. /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
  384. atomic_t nr_cgrps;
  385. /* A list running through the active hierarchies */
  386. struct list_head root_list;
  387. /* Hierarchy-specific flags */
  388. unsigned int flags;
  389. /* IDs for cgroups in this hierarchy */
  390. struct idr cgroup_idr;
  391. /* The path to use for release notifications. */
  392. char release_agent_path[PATH_MAX];
  393. /* The name for this hierarchy - may be empty */
  394. char name[MAX_CGROUP_ROOT_NAMELEN];
  395. };
  396. /*
  397. * struct cftype: handler definitions for cgroup control files
  398. *
  399. * When reading/writing to a file:
  400. * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
  401. * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
  402. */
  403. struct cftype {
  404. /*
  405. * By convention, the name should begin with the name of the
  406. * subsystem, followed by a period. Zero length string indicates
  407. * end of cftype array.
  408. */
  409. char name[MAX_CFTYPE_NAME];
  410. unsigned long private;
  411. /*
  412. * The maximum length of string, excluding trailing nul, that can
  413. * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
  414. */
  415. size_t max_write_len;
  416. /* CFTYPE_* flags */
  417. unsigned int flags;
  418. /*
  419. * If non-zero, should contain the offset from the start of css to
  420. * a struct cgroup_file field. cgroup will record the handle of
  421. * the created file into it. The recorded handle can be used as
  422. * long as the containing css remains accessible.
  423. */
  424. unsigned int file_offset;
  425. /*
  426. * Fields used for internal bookkeeping. Initialized automatically
  427. * during registration.
  428. */
  429. struct cgroup_subsys *ss; /* NULL for cgroup core files */
  430. struct list_head node; /* anchored at ss->cfts */
  431. struct kernfs_ops *kf_ops;
  432. int (*open)(struct kernfs_open_file *of);
  433. void (*release)(struct kernfs_open_file *of);
  434. /*
  435. * read_u64() is a shortcut for the common case of returning a
  436. * single integer. Use it in place of read()
  437. */
  438. u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
  439. /*
  440. * read_s64() is a signed version of read_u64()
  441. */
  442. s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
  443. /* generic seq_file read interface */
  444. int (*seq_show)(struct seq_file *sf, void *v);
  445. /* optional ops, implement all or none */
  446. void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
  447. void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
  448. void (*seq_stop)(struct seq_file *sf, void *v);
  449. /*
  450. * write_u64() is a shortcut for the common case of accepting
  451. * a single integer (as parsed by simple_strtoull) from
  452. * userspace. Use in place of write(); return 0 or error.
  453. */
  454. int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
  455. u64 val);
  456. /*
  457. * write_s64() is a signed version of write_u64()
  458. */
  459. int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
  460. s64 val);
  461. /*
  462. * write() is the generic write callback which maps directly to
  463. * kernfs write operation and overrides all other operations.
  464. * Maximum write size is determined by ->max_write_len. Use
  465. * of_css/cft() to access the associated css and cft.
  466. */
  467. ssize_t (*write)(struct kernfs_open_file *of,
  468. char *buf, size_t nbytes, loff_t off);
  469. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  470. struct lock_class_key lockdep_key;
  471. #endif
  472. };
  473. /*
  474. * Control Group subsystem type.
  475. * See Documentation/cgroups/cgroups.txt for details
  476. */
  477. struct cgroup_subsys {
  478. struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
  479. int (*css_online)(struct cgroup_subsys_state *css);
  480. void (*css_offline)(struct cgroup_subsys_state *css);
  481. void (*css_released)(struct cgroup_subsys_state *css);
  482. void (*css_free)(struct cgroup_subsys_state *css);
  483. void (*css_reset)(struct cgroup_subsys_state *css);
  484. int (*can_attach)(struct cgroup_taskset *tset);
  485. void (*cancel_attach)(struct cgroup_taskset *tset);
  486. void (*attach)(struct cgroup_taskset *tset);
  487. void (*post_attach)(void);
  488. int (*can_fork)(struct task_struct *task);
  489. void (*cancel_fork)(struct task_struct *task);
  490. void (*fork)(struct task_struct *task);
  491. void (*exit)(struct task_struct *task);
  492. void (*free)(struct task_struct *task);
  493. void (*bind)(struct cgroup_subsys_state *root_css);
  494. bool early_init:1;
  495. /*
  496. * If %true, the controller, on the default hierarchy, doesn't show
  497. * up in "cgroup.controllers" or "cgroup.subtree_control", is
  498. * implicitly enabled on all cgroups on the default hierarchy, and
  499. * bypasses the "no internal process" constraint. This is for
  500. * utility type controllers which is transparent to userland.
  501. *
  502. * An implicit controller can be stolen from the default hierarchy
  503. * anytime and thus must be okay with offline csses from previous
  504. * hierarchies coexisting with csses for the current one.
  505. */
  506. bool implicit_on_dfl:1;
  507. /*
  508. * If %true, the controller, supports threaded mode on the default
  509. * hierarchy. In a threaded subtree, both process granularity and
  510. * no-internal-process constraint are ignored and a threaded
  511. * controllers should be able to handle that.
  512. *
  513. * Note that as an implicit controller is automatically enabled on
  514. * all cgroups on the default hierarchy, it should also be
  515. * threaded. implicit && !threaded is not supported.
  516. */
  517. bool threaded:1;
  518. /*
  519. * If %false, this subsystem is properly hierarchical -
  520. * configuration, resource accounting and restriction on a parent
  521. * cgroup cover those of its children. If %true, hierarchy support
  522. * is broken in some ways - some subsystems ignore hierarchy
  523. * completely while others are only implemented half-way.
  524. *
  525. * It's now disallowed to create nested cgroups if the subsystem is
  526. * broken and cgroup core will emit a warning message on such
  527. * cases. Eventually, all subsystems will be made properly
  528. * hierarchical and this will go away.
  529. */
  530. bool broken_hierarchy:1;
  531. bool warned_broken_hierarchy:1;
  532. /* the following two fields are initialized automtically during boot */
  533. int id;
  534. const char *name;
  535. /* optional, initialized automatically during boot if not set */
  536. const char *legacy_name;
  537. /* link to parent, protected by cgroup_lock() */
  538. struct cgroup_root *root;
  539. /* idr for css->id */
  540. struct idr css_idr;
  541. /*
  542. * List of cftypes. Each entry is the first entry of an array
  543. * terminated by zero length name.
  544. */
  545. struct list_head cfts;
  546. /*
  547. * Base cftypes which are automatically registered. The two can
  548. * point to the same array.
  549. */
  550. struct cftype *dfl_cftypes; /* for the default hierarchy */
  551. struct cftype *legacy_cftypes; /* for the legacy hierarchies */
  552. /*
  553. * A subsystem may depend on other subsystems. When such subsystem
  554. * is enabled on a cgroup, the depended-upon subsystems are enabled
  555. * together if available. Subsystems enabled due to dependency are
  556. * not visible to userland until explicitly enabled. The following
  557. * specifies the mask of subsystems that this one depends on.
  558. */
  559. unsigned int depends_on;
  560. };
  561. extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
  562. /**
  563. * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
  564. * @tsk: target task
  565. *
  566. * Allows cgroup operations to synchronize against threadgroup changes
  567. * using a percpu_rw_semaphore.
  568. */
  569. static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
  570. {
  571. percpu_down_read(&cgroup_threadgroup_rwsem);
  572. }
  573. /**
  574. * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
  575. * @tsk: target task
  576. *
  577. * Counterpart of cgroup_threadcgroup_change_begin().
  578. */
  579. static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
  580. {
  581. percpu_up_read(&cgroup_threadgroup_rwsem);
  582. }
  583. #else /* CONFIG_CGROUPS */
  584. #define CGROUP_SUBSYS_COUNT 0
  585. static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
  586. {
  587. might_sleep();
  588. }
  589. static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
  590. #endif /* CONFIG_CGROUPS */
  591. #ifdef CONFIG_SOCK_CGROUP_DATA
  592. /*
  593. * sock_cgroup_data is embedded at sock->sk_cgrp_data and contains
  594. * per-socket cgroup information except for memcg association.
  595. *
  596. * On legacy hierarchies, net_prio and net_cls controllers directly set
  597. * attributes on each sock which can then be tested by the network layer.
  598. * On the default hierarchy, each sock is associated with the cgroup it was
  599. * created in and the networking layer can match the cgroup directly.
  600. *
  601. * To avoid carrying all three cgroup related fields separately in sock,
  602. * sock_cgroup_data overloads (prioidx, classid) and the cgroup pointer.
  603. * On boot, sock_cgroup_data records the cgroup that the sock was created
  604. * in so that cgroup2 matches can be made; however, once either net_prio or
  605. * net_cls starts being used, the area is overriden to carry prioidx and/or
  606. * classid. The two modes are distinguished by whether the lowest bit is
  607. * set. Clear bit indicates cgroup pointer while set bit prioidx and
  608. * classid.
  609. *
  610. * While userland may start using net_prio or net_cls at any time, once
  611. * either is used, cgroup2 matching no longer works. There is no reason to
  612. * mix the two and this is in line with how legacy and v2 compatibility is
  613. * handled. On mode switch, cgroup references which are already being
  614. * pointed to by socks may be leaked. While this can be remedied by adding
  615. * synchronization around sock_cgroup_data, given that the number of leaked
  616. * cgroups is bound and highly unlikely to be high, this seems to be the
  617. * better trade-off.
  618. */
  619. struct sock_cgroup_data {
  620. union {
  621. #ifdef __LITTLE_ENDIAN
  622. struct {
  623. u8 is_data;
  624. u8 padding;
  625. u16 prioidx;
  626. u32 classid;
  627. } __packed;
  628. #else
  629. struct {
  630. u32 classid;
  631. u16 prioidx;
  632. u8 padding;
  633. u8 is_data;
  634. } __packed;
  635. #endif
  636. u64 val;
  637. };
  638. };
  639. /*
  640. * There's a theoretical window where the following accessors race with
  641. * updaters and return part of the previous pointer as the prioidx or
  642. * classid. Such races are short-lived and the result isn't critical.
  643. */
  644. static inline u16 sock_cgroup_prioidx(struct sock_cgroup_data *skcd)
  645. {
  646. /* fallback to 1 which is always the ID of the root cgroup */
  647. return (skcd->is_data & 1) ? skcd->prioidx : 1;
  648. }
  649. static inline u32 sock_cgroup_classid(struct sock_cgroup_data *skcd)
  650. {
  651. /* fallback to 0 which is the unconfigured default classid */
  652. return (skcd->is_data & 1) ? skcd->classid : 0;
  653. }
  654. /*
  655. * If invoked concurrently, the updaters may clobber each other. The
  656. * caller is responsible for synchronization.
  657. */
  658. static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd,
  659. u16 prioidx)
  660. {
  661. struct sock_cgroup_data skcd_buf = {{ .val = READ_ONCE(skcd->val) }};
  662. if (sock_cgroup_prioidx(&skcd_buf) == prioidx)
  663. return;
  664. if (!(skcd_buf.is_data & 1)) {
  665. skcd_buf.val = 0;
  666. skcd_buf.is_data = 1;
  667. }
  668. skcd_buf.prioidx = prioidx;
  669. WRITE_ONCE(skcd->val, skcd_buf.val); /* see sock_cgroup_ptr() */
  670. }
  671. static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd,
  672. u32 classid)
  673. {
  674. struct sock_cgroup_data skcd_buf = {{ .val = READ_ONCE(skcd->val) }};
  675. if (sock_cgroup_classid(&skcd_buf) == classid)
  676. return;
  677. if (!(skcd_buf.is_data & 1)) {
  678. skcd_buf.val = 0;
  679. skcd_buf.is_data = 1;
  680. }
  681. skcd_buf.classid = classid;
  682. WRITE_ONCE(skcd->val, skcd_buf.val); /* see sock_cgroup_ptr() */
  683. }
  684. #else /* CONFIG_SOCK_CGROUP_DATA */
  685. struct sock_cgroup_data {
  686. };
  687. #endif /* CONFIG_SOCK_CGROUP_DATA */
  688. #endif /* _LINUX_CGROUP_DEFS_H */