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