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