cgroup-defs.h 26 KB

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