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