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