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