cgroup-defs.h 25 KB

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