• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/workqueue.h>
21 #include <linux/bpf-cgroup.h>
22 #include <linux/psi_types.h>
23 
24 #ifdef CONFIG_CGROUPS
25 
26 struct cgroup;
27 struct cgroup_root;
28 struct cgroup_subsys;
29 struct cgroup_taskset;
30 struct kernfs_node;
31 struct kernfs_ops;
32 struct kernfs_open_file;
33 struct seq_file;
34 struct poll_table_struct;
35 
36 #define MAX_CGROUP_TYPE_NAMELEN 32
37 #define MAX_CGROUP_ROOT_NAMELEN 64
38 #define MAX_CFTYPE_NAME		64
39 
40 /* define the enumeration of all cgroup subsystems */
41 #define SUBSYS(_x) _x ## _cgrp_id,
42 enum cgroup_subsys_id {
43 #include <linux/cgroup_subsys.h>
44 	CGROUP_SUBSYS_COUNT,
45 };
46 #undef SUBSYS
47 
48 /* bits in struct cgroup_subsys_state flags field */
49 enum {
50 	CSS_NO_REF	= (1 << 0), /* no reference counting for this css */
51 	CSS_ONLINE	= (1 << 1), /* between ->css_online() and ->css_offline() */
52 	CSS_RELEASED	= (1 << 2), /* refcnt reached zero, released */
53 	CSS_VISIBLE	= (1 << 3), /* css is visible to userland */
54 	CSS_DYING	= (1 << 4), /* css is dying */
55 };
56 
57 /* bits in struct cgroup flags field */
58 enum {
59 	/* Control Group requires release notifications to userspace */
60 	CGRP_NOTIFY_ON_RELEASE,
61 	/*
62 	 * Clone the parent's configuration when creating a new child
63 	 * cpuset cgroup.  For historical reasons, this option can be
64 	 * specified at mount time and thus is implemented here.
65 	 */
66 	CGRP_CPUSET_CLONE_CHILDREN,
67 };
68 
69 /* cgroup_root->flags */
70 enum {
71 	CGRP_ROOT_NOPREFIX	= (1 << 1), /* mounted subsystems have no named prefix */
72 	CGRP_ROOT_XATTR		= (1 << 2), /* supports extended attributes */
73 
74 	/*
75 	 * Consider namespaces as delegation boundaries.  If this flag is
76 	 * set, controller specific interface files in a namespace root
77 	 * aren't writeable from inside the namespace.
78 	 */
79 	CGRP_ROOT_NS_DELEGATE	= (1 << 3),
80 
81 	/*
82 	 * Enable cpuset controller in v1 cgroup to use v2 behavior.
83 	 */
84 	CGRP_ROOT_CPUSET_V2_MODE = (1 << 4),
85 };
86 
87 /* cftype->flags */
88 enum {
89 	CFTYPE_ONLY_ON_ROOT	= (1 << 0),	/* only create on root cgrp */
90 	CFTYPE_NOT_ON_ROOT	= (1 << 1),	/* don't create on root cgrp */
91 	CFTYPE_NS_DELEGATABLE	= (1 << 2),	/* writeable beyond delegation boundaries */
92 
93 	CFTYPE_NO_PREFIX	= (1 << 3),	/* (DON'T USE FOR NEW FILES) no subsys prefix */
94 	CFTYPE_WORLD_WRITABLE	= (1 << 4),	/* (DON'T USE FOR NEW FILES) S_IWUGO */
95 
96 	/* internal flags, do not use outside cgroup core proper */
97 	__CFTYPE_ONLY_ON_DFL	= (1 << 16),	/* only on default hierarchy */
98 	__CFTYPE_NOT_ON_DFL	= (1 << 17),	/* not on default hierarchy */
99 };
100 
101 /*
102  * cgroup_file is the handle for a file instance created in a cgroup which
103  * is used, for example, to generate file changed notifications.  This can
104  * be obtained by setting cftype->file_offset.
105  */
106 struct cgroup_file {
107 	/* do not access any fields from outside cgroup core */
108 	struct kernfs_node *kn;
109 };
110 
111 /*
112  * Per-subsystem/per-cgroup state maintained by the system.  This is the
113  * fundamental structural building block that controllers deal with.
114  *
115  * Fields marked with "PI:" are public and immutable and may be accessed
116  * directly without synchronization.
117  */
118 struct cgroup_subsys_state {
119 	/* PI: the cgroup that this css is attached to */
120 	struct cgroup *cgroup;
121 
122 	/* PI: the cgroup subsystem that this css is attached to */
123 	struct cgroup_subsys *ss;
124 
125 	/* reference count - access via css_[try]get() and css_put() */
126 	struct percpu_ref refcnt;
127 
128 	/* siblings list anchored at the parent's ->children */
129 	struct list_head sibling;
130 	struct list_head children;
131 
132 	/*
133 	 * PI: Subsys-unique ID.  0 is unused and root is always 1.  The
134 	 * matching css can be looked up using css_from_id().
135 	 */
136 	int id;
137 
138 	unsigned int flags;
139 
140 	/*
141 	 * Monotonically increasing unique serial number which defines a
142 	 * uniform order among all csses.  It's guaranteed that all
143 	 * ->children lists are in the ascending order of ->serial_nr and
144 	 * used to allow interrupting and resuming iterations.
145 	 */
146 	u64 serial_nr;
147 
148 	/*
149 	 * Incremented by online self and children.  Used to guarantee that
150 	 * parents are not offlined before their children.
151 	 */
152 	atomic_t online_cnt;
153 
154 	/* percpu_ref killing and RCU release */
155 	struct rcu_head rcu_head;
156 	struct work_struct destroy_work;
157 
158 	/*
159 	 * PI: the parent css.	Placed here for cache proximity to following
160 	 * fields of the containing structure.
161 	 */
162 	struct cgroup_subsys_state *parent;
163 };
164 
165 /*
166  * A css_set is a structure holding pointers to a set of
167  * cgroup_subsys_state objects. This saves space in the task struct
168  * object and speeds up fork()/exit(), since a single inc/dec and a
169  * list_add()/del() can bump the reference count on the entire cgroup
170  * set for a task.
171  */
172 struct css_set {
173 	/*
174 	 * Set of subsystem states, one for each subsystem. This array is
175 	 * immutable after creation apart from the init_css_set during
176 	 * subsystem registration (at boot time).
177 	 */
178 	struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
179 
180 	/* reference count */
181 	refcount_t refcount;
182 
183 	/*
184 	 * For a domain cgroup, the following points to self.  If threaded,
185 	 * to the matching cset of the nearest domain ancestor.  The
186 	 * dom_cset provides access to the domain cgroup and its csses to
187 	 * which domain level resource consumptions should be charged.
188 	 */
189 	struct css_set *dom_cset;
190 
191 	/* the default cgroup associated with this css_set */
192 	struct cgroup *dfl_cgrp;
193 
194 	/* internal task count, protected by css_set_lock */
195 	int nr_tasks;
196 
197 	/*
198 	 * Lists running through all tasks using this cgroup group.
199 	 * mg_tasks lists tasks which belong to this cset but are in the
200 	 * process of being migrated out or in.  Protected by
201 	 * css_set_rwsem, but, during migration, once tasks are moved to
202 	 * mg_tasks, it can be read safely while holding cgroup_mutex.
203 	 */
204 	struct list_head tasks;
205 	struct list_head mg_tasks;
206 	struct list_head dying_tasks;
207 
208 	/* all css_task_iters currently walking this cset */
209 	struct list_head task_iters;
210 
211 	/*
212 	 * On the default hierarhcy, ->subsys[ssid] may point to a css
213 	 * attached to an ancestor instead of the cgroup this css_set is
214 	 * associated with.  The following node is anchored at
215 	 * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
216 	 * iterate through all css's attached to a given cgroup.
217 	 */
218 	struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
219 
220 	/* all threaded csets whose ->dom_cset points to this cset */
221 	struct list_head threaded_csets;
222 	struct list_head threaded_csets_node;
223 
224 	/*
225 	 * List running through all cgroup groups in the same hash
226 	 * slot. Protected by css_set_lock
227 	 */
228 	struct hlist_node hlist;
229 
230 	/*
231 	 * List of cgrp_cset_links pointing at cgroups referenced from this
232 	 * css_set.  Protected by css_set_lock.
233 	 */
234 	struct list_head cgrp_links;
235 
236 	/*
237 	 * List of csets participating in the on-going migration either as
238 	 * source or destination.  Protected by cgroup_mutex.
239 	 */
240 	struct list_head mg_preload_node;
241 	struct list_head mg_node;
242 
243 	/*
244 	 * If this cset is acting as the source of migration the following
245 	 * two fields are set.  mg_src_cgrp and mg_dst_cgrp are
246 	 * respectively the source and destination cgroups of the on-going
247 	 * migration.  mg_dst_cset is the destination cset the target tasks
248 	 * on this cset should be migrated to.  Protected by cgroup_mutex.
249 	 */
250 	struct cgroup *mg_src_cgrp;
251 	struct cgroup *mg_dst_cgrp;
252 	struct css_set *mg_dst_cset;
253 
254 	/* dead and being drained, ignore for migration */
255 	bool dead;
256 
257 	/* For RCU-protected deletion */
258 	struct rcu_head rcu_head;
259 };
260 
261 struct cgroup {
262 	/* self css with NULL ->ss, points back to this cgroup */
263 	struct cgroup_subsys_state self;
264 
265 	unsigned long flags;		/* "unsigned long" so bitops work */
266 
267 	/*
268 	 * idr allocated in-hierarchy ID.
269 	 *
270 	 * ID 0 is not used, the ID of the root cgroup is always 1, and a
271 	 * new cgroup will be assigned with a smallest available ID.
272 	 *
273 	 * Allocating/Removing ID must be protected by cgroup_mutex.
274 	 */
275 	int id;
276 
277 	/*
278 	 * The depth this cgroup is at.  The root is at depth zero and each
279 	 * step down the hierarchy increments the level.  This along with
280 	 * ancestor_ids[] can determine whether a given cgroup is a
281 	 * descendant of another without traversing the hierarchy.
282 	 */
283 	int level;
284 
285 	/* Maximum allowed descent tree depth */
286 	int max_depth;
287 
288 	/*
289 	 * Keep track of total numbers of visible and dying descent cgroups.
290 	 * Dying cgroups are cgroups which were deleted by a user,
291 	 * but are still existing because someone else is holding a reference.
292 	 * max_descendants is a maximum allowed number of descent cgroups.
293 	 *
294 	 * nr_descendants and nr_dying_descendants are protected
295 	 * by cgroup_mutex and css_set_lock. It's fine to read them holding
296 	 * any of cgroup_mutex and css_set_lock; for writing both locks
297 	 * should be held.
298 	 */
299 	int nr_descendants;
300 	int nr_dying_descendants;
301 	int max_descendants;
302 
303 	/*
304 	 * Each non-empty css_set associated with this cgroup contributes
305 	 * one to nr_populated_csets.  The counter is zero iff this cgroup
306 	 * doesn't have any tasks.
307 	 *
308 	 * All children which have non-zero nr_populated_csets and/or
309 	 * nr_populated_children of their own contribute one to either
310 	 * nr_populated_domain_children or nr_populated_threaded_children
311 	 * depending on their type.  Each counter is zero iff all cgroups
312 	 * of the type in the subtree proper don't have any tasks.
313 	 */
314 	int nr_populated_csets;
315 	int nr_populated_domain_children;
316 	int nr_populated_threaded_children;
317 
318 	int nr_threaded_children;	/* # of live threaded child cgroups */
319 
320 	struct kernfs_node *kn;		/* cgroup kernfs entry */
321 	struct cgroup_file procs_file;	/* handle for "cgroup.procs" */
322 	struct cgroup_file events_file;	/* handle for "cgroup.events" */
323 
324 	/*
325 	 * The bitmask of subsystems enabled on the child cgroups.
326 	 * ->subtree_control is the one configured through
327 	 * "cgroup.subtree_control" while ->child_ss_mask is the effective
328 	 * one which may have more subsystems enabled.  Controller knobs
329 	 * are made available iff it's enabled in ->subtree_control.
330 	 */
331 	u16 subtree_control;
332 	u16 subtree_ss_mask;
333 	u16 old_subtree_control;
334 	u16 old_subtree_ss_mask;
335 
336 	/* Private pointers for each registered subsystem */
337 	struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
338 
339 	struct cgroup_root *root;
340 
341 	/*
342 	 * List of cgrp_cset_links pointing at css_sets with tasks in this
343 	 * cgroup.  Protected by css_set_lock.
344 	 */
345 	struct list_head cset_links;
346 
347 	/*
348 	 * On the default hierarchy, a css_set for a cgroup with some
349 	 * susbsys disabled will point to css's which are associated with
350 	 * the closest ancestor which has the subsys enabled.  The
351 	 * following lists all css_sets which point to this cgroup's css
352 	 * for the given subsystem.
353 	 */
354 	struct list_head e_csets[CGROUP_SUBSYS_COUNT];
355 
356 	/*
357 	 * If !threaded, self.  If threaded, it points to the nearest
358 	 * domain ancestor.  Inside a threaded subtree, cgroups are exempt
359 	 * from process granularity and no-internal-task constraint.
360 	 * Domain level resource consumptions which aren't tied to a
361 	 * specific task are charged to the dom_cgrp.
362 	 */
363 	struct cgroup *dom_cgrp;
364 	struct cgroup *old_dom_cgrp;		/* used while enabling threaded */
365 
366 	/*
367 	 * list of pidlists, up to two for each namespace (one for procs, one
368 	 * for tasks); created on demand.
369 	 */
370 	struct list_head pidlists;
371 	struct mutex pidlist_mutex;
372 
373 	/* used to wait for offlining of csses */
374 	wait_queue_head_t offline_waitq;
375 
376 	/* used to schedule release agent */
377 	struct work_struct release_agent_work;
378 
379 	/* used to track pressure stalls */
380 	struct psi_group psi;
381 
382 	/* used to store eBPF programs */
383 	struct cgroup_bpf bpf;
384 
385 	/* ids of the ancestors at each level including self */
386 	int ancestor_ids[];
387 };
388 
389 /*
390  * A cgroup_root represents the root of a cgroup hierarchy, and may be
391  * associated with a kernfs_root to form an active hierarchy.  This is
392  * internal to cgroup core.  Don't access directly from controllers.
393  */
394 struct cgroup_root {
395 	struct kernfs_root *kf_root;
396 
397 	/* The bitmask of subsystems attached to this hierarchy */
398 	unsigned int subsys_mask;
399 
400 	/* Unique id for this hierarchy. */
401 	int hierarchy_id;
402 
403 	/* The root cgroup.  Root is destroyed on its release. */
404 	struct cgroup cgrp;
405 
406 	/* for cgrp->ancestor_ids[0] */
407 	int cgrp_ancestor_id_storage;
408 
409 	/* Number of cgroups in the hierarchy, used only for /proc/cgroups */
410 	atomic_t nr_cgrps;
411 
412 	/* A list running through the active hierarchies */
413 	struct list_head root_list;
414 
415 	/* Hierarchy-specific flags */
416 	unsigned int flags;
417 
418 	/* IDs for cgroups in this hierarchy */
419 	struct idr cgroup_idr;
420 
421 	/* The path to use for release notifications. */
422 	char release_agent_path[PATH_MAX];
423 
424 	/* The name for this hierarchy - may be empty */
425 	char name[MAX_CGROUP_ROOT_NAMELEN];
426 };
427 
428 /*
429  * struct cftype: handler definitions for cgroup control files
430  *
431  * When reading/writing to a file:
432  *	- the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
433  *	- the 'cftype' of the file is file->f_path.dentry->d_fsdata
434  */
435 struct cftype {
436 	/*
437 	 * By convention, the name should begin with the name of the
438 	 * subsystem, followed by a period.  Zero length string indicates
439 	 * end of cftype array.
440 	 */
441 	char name[MAX_CFTYPE_NAME];
442 	unsigned long private;
443 
444 	/*
445 	 * The maximum length of string, excluding trailing nul, that can
446 	 * be passed to write.  If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
447 	 */
448 	size_t max_write_len;
449 
450 	/* CFTYPE_* flags */
451 	unsigned int flags;
452 
453 	/*
454 	 * If non-zero, should contain the offset from the start of css to
455 	 * a struct cgroup_file field.  cgroup will record the handle of
456 	 * the created file into it.  The recorded handle can be used as
457 	 * long as the containing css remains accessible.
458 	 */
459 	unsigned int file_offset;
460 
461 	/*
462 	 * Fields used for internal bookkeeping.  Initialized automatically
463 	 * during registration.
464 	 */
465 	struct cgroup_subsys *ss;	/* NULL for cgroup core files */
466 	struct list_head node;		/* anchored at ss->cfts */
467 	struct kernfs_ops *kf_ops;
468 
469 	int (*open)(struct kernfs_open_file *of);
470 	void (*release)(struct kernfs_open_file *of);
471 
472 	/*
473 	 * read_u64() is a shortcut for the common case of returning a
474 	 * single integer. Use it in place of read()
475 	 */
476 	u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
477 	/*
478 	 * read_s64() is a signed version of read_u64()
479 	 */
480 	s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
481 
482 	/* generic seq_file read interface */
483 	int (*seq_show)(struct seq_file *sf, void *v);
484 
485 	/* optional ops, implement all or none */
486 	void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
487 	void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
488 	void (*seq_stop)(struct seq_file *sf, void *v);
489 
490 	/*
491 	 * write_u64() is a shortcut for the common case of accepting
492 	 * a single integer (as parsed by simple_strtoull) from
493 	 * userspace. Use in place of write(); return 0 or error.
494 	 */
495 	int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
496 			 u64 val);
497 	/*
498 	 * write_s64() is a signed version of write_u64()
499 	 */
500 	int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
501 			 s64 val);
502 
503 	/*
504 	 * write() is the generic write callback which maps directly to
505 	 * kernfs write operation and overrides all other operations.
506 	 * Maximum write size is determined by ->max_write_len.  Use
507 	 * of_css/cft() to access the associated css and cft.
508 	 */
509 	ssize_t (*write)(struct kernfs_open_file *of,
510 			 char *buf, size_t nbytes, loff_t off);
511 
512 	unsigned int (*poll)(struct kernfs_open_file *of,
513 			     struct poll_table_struct *pt);
514 
515 #ifdef CONFIG_DEBUG_LOCK_ALLOC
516 	struct lock_class_key	lockdep_key;
517 #endif
518 };
519 
520 /*
521  * Control Group subsystem type.
522  * See Documentation/cgroups/cgroups.txt for details
523  */
524 struct cgroup_subsys {
525 	struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
526 	int (*css_online)(struct cgroup_subsys_state *css);
527 	void (*css_offline)(struct cgroup_subsys_state *css);
528 	void (*css_released)(struct cgroup_subsys_state *css);
529 	void (*css_free)(struct cgroup_subsys_state *css);
530 	void (*css_reset)(struct cgroup_subsys_state *css);
531 
532 	int (*can_attach)(struct cgroup_taskset *tset);
533 	void (*cancel_attach)(struct cgroup_taskset *tset);
534 	void (*attach)(struct cgroup_taskset *tset);
535 	void (*post_attach)(void);
536 	int (*can_fork)(struct task_struct *task);
537 	void (*cancel_fork)(struct task_struct *task);
538 	void (*fork)(struct task_struct *task);
539 	void (*exit)(struct task_struct *task);
540 	void (*release)(struct task_struct *task);
541 	void (*bind)(struct cgroup_subsys_state *root_css);
542 
543 	bool early_init:1;
544 
545 	/*
546 	 * If %true, the controller, on the default hierarchy, doesn't show
547 	 * up in "cgroup.controllers" or "cgroup.subtree_control", is
548 	 * implicitly enabled on all cgroups on the default hierarchy, and
549 	 * bypasses the "no internal process" constraint.  This is for
550 	 * utility type controllers which is transparent to userland.
551 	 *
552 	 * An implicit controller can be stolen from the default hierarchy
553 	 * anytime and thus must be okay with offline csses from previous
554 	 * hierarchies coexisting with csses for the current one.
555 	 */
556 	bool implicit_on_dfl:1;
557 
558 	/*
559 	 * If %true, the controller, supports threaded mode on the default
560 	 * hierarchy.  In a threaded subtree, both process granularity and
561 	 * no-internal-process constraint are ignored and a threaded
562 	 * controllers should be able to handle that.
563 	 *
564 	 * Note that as an implicit controller is automatically enabled on
565 	 * all cgroups on the default hierarchy, it should also be
566 	 * threaded.  implicit && !threaded is not supported.
567 	 */
568 	bool threaded:1;
569 
570 	/*
571 	 * If %false, this subsystem is properly hierarchical -
572 	 * configuration, resource accounting and restriction on a parent
573 	 * cgroup cover those of its children.  If %true, hierarchy support
574 	 * is broken in some ways - some subsystems ignore hierarchy
575 	 * completely while others are only implemented half-way.
576 	 *
577 	 * It's now disallowed to create nested cgroups if the subsystem is
578 	 * broken and cgroup core will emit a warning message on such
579 	 * cases.  Eventually, all subsystems will be made properly
580 	 * hierarchical and this will go away.
581 	 */
582 	bool broken_hierarchy:1;
583 	bool warned_broken_hierarchy:1;
584 
585 	/* the following two fields are initialized automtically during boot */
586 	int id;
587 	const char *name;
588 
589 	/* optional, initialized automatically during boot if not set */
590 	const char *legacy_name;
591 
592 	/* link to parent, protected by cgroup_lock() */
593 	struct cgroup_root *root;
594 
595 	/* idr for css->id */
596 	struct idr css_idr;
597 
598 	/*
599 	 * List of cftypes.  Each entry is the first entry of an array
600 	 * terminated by zero length name.
601 	 */
602 	struct list_head cfts;
603 
604 	/*
605 	 * Base cftypes which are automatically registered.  The two can
606 	 * point to the same array.
607 	 */
608 	struct cftype *dfl_cftypes;	/* for the default hierarchy */
609 	struct cftype *legacy_cftypes;	/* for the legacy hierarchies */
610 
611 	/*
612 	 * A subsystem may depend on other subsystems.  When such subsystem
613 	 * is enabled on a cgroup, the depended-upon subsystems are enabled
614 	 * together if available.  Subsystems enabled due to dependency are
615 	 * not visible to userland until explicitly enabled.  The following
616 	 * specifies the mask of subsystems that this one depends on.
617 	 */
618 	unsigned int depends_on;
619 };
620 
621 extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
622 
623 /**
624  * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
625  * @tsk: target task
626  *
627  * Allows cgroup operations to synchronize against threadgroup changes
628  * using a percpu_rw_semaphore.
629  */
cgroup_threadgroup_change_begin(struct task_struct * tsk)630 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
631 {
632 	percpu_down_read(&cgroup_threadgroup_rwsem);
633 }
634 
635 /**
636  * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
637  * @tsk: target task
638  *
639  * Counterpart of cgroup_threadcgroup_change_begin().
640  */
cgroup_threadgroup_change_end(struct task_struct * tsk)641 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
642 {
643 	percpu_up_read(&cgroup_threadgroup_rwsem);
644 }
645 
646 #else	/* CONFIG_CGROUPS */
647 
648 #define CGROUP_SUBSYS_COUNT 0
649 
cgroup_threadgroup_change_begin(struct task_struct * tsk)650 static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
651 {
652 	might_sleep();
653 }
654 
cgroup_threadgroup_change_end(struct task_struct * tsk)655 static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
656 
657 #endif	/* CONFIG_CGROUPS */
658 
659 #ifdef CONFIG_SOCK_CGROUP_DATA
660 
661 /*
662  * sock_cgroup_data is embedded at sock->sk_cgrp_data and contains
663  * per-socket cgroup information except for memcg association.
664  *
665  * On legacy hierarchies, net_prio and net_cls controllers directly set
666  * attributes on each sock which can then be tested by the network layer.
667  * On the default hierarchy, each sock is associated with the cgroup it was
668  * created in and the networking layer can match the cgroup directly.
669  *
670  * To avoid carrying all three cgroup related fields separately in sock,
671  * sock_cgroup_data overloads (prioidx, classid) and the cgroup pointer.
672  * On boot, sock_cgroup_data records the cgroup that the sock was created
673  * in so that cgroup2 matches can be made; however, once either net_prio or
674  * net_cls starts being used, the area is overriden to carry prioidx and/or
675  * classid.  The two modes are distinguished by whether the lowest bit is
676  * set.  Clear bit indicates cgroup pointer while set bit prioidx and
677  * classid.
678  *
679  * While userland may start using net_prio or net_cls at any time, once
680  * either is used, cgroup2 matching no longer works.  There is no reason to
681  * mix the two and this is in line with how legacy and v2 compatibility is
682  * handled.  On mode switch, cgroup references which are already being
683  * pointed to by socks may be leaked.  While this can be remedied by adding
684  * synchronization around sock_cgroup_data, given that the number of leaked
685  * cgroups is bound and highly unlikely to be high, this seems to be the
686  * better trade-off.
687  */
688 struct sock_cgroup_data {
689 	union {
690 #ifdef __LITTLE_ENDIAN
691 		struct {
692 			u8	is_data;
693 			u8	padding;
694 			u16	prioidx;
695 			u32	classid;
696 		} __packed;
697 #else
698 		struct {
699 			u32	classid;
700 			u16	prioidx;
701 			u8	padding;
702 			u8	is_data;
703 		} __packed;
704 #endif
705 		u64		val;
706 	};
707 };
708 
709 /*
710  * There's a theoretical window where the following accessors race with
711  * updaters and return part of the previous pointer as the prioidx or
712  * classid.  Such races are short-lived and the result isn't critical.
713  */
sock_cgroup_prioidx(const struct sock_cgroup_data * skcd)714 static inline u16 sock_cgroup_prioidx(const struct sock_cgroup_data *skcd)
715 {
716 	/* fallback to 1 which is always the ID of the root cgroup */
717 	return (skcd->is_data & 1) ? skcd->prioidx : 1;
718 }
719 
sock_cgroup_classid(const struct sock_cgroup_data * skcd)720 static inline u32 sock_cgroup_classid(const struct sock_cgroup_data *skcd)
721 {
722 	/* fallback to 0 which is the unconfigured default classid */
723 	return (skcd->is_data & 1) ? skcd->classid : 0;
724 }
725 
726 /*
727  * If invoked concurrently, the updaters may clobber each other.  The
728  * caller is responsible for synchronization.
729  */
sock_cgroup_set_prioidx(struct sock_cgroup_data * skcd,u16 prioidx)730 static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd,
731 					   u16 prioidx)
732 {
733 	struct sock_cgroup_data skcd_buf = {{ .val = READ_ONCE(skcd->val) }};
734 
735 	if (sock_cgroup_prioidx(&skcd_buf) == prioidx)
736 		return;
737 
738 	if (!(skcd_buf.is_data & 1)) {
739 		skcd_buf.val = 0;
740 		skcd_buf.is_data = 1;
741 	}
742 
743 	skcd_buf.prioidx = prioidx;
744 	WRITE_ONCE(skcd->val, skcd_buf.val);	/* see sock_cgroup_ptr() */
745 }
746 
sock_cgroup_set_classid(struct sock_cgroup_data * skcd,u32 classid)747 static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd,
748 					   u32 classid)
749 {
750 	struct sock_cgroup_data skcd_buf = {{ .val = READ_ONCE(skcd->val) }};
751 
752 	if (sock_cgroup_classid(&skcd_buf) == classid)
753 		return;
754 
755 	if (!(skcd_buf.is_data & 1)) {
756 		skcd_buf.val = 0;
757 		skcd_buf.is_data = 1;
758 	}
759 
760 	skcd_buf.classid = classid;
761 	WRITE_ONCE(skcd->val, skcd_buf.val);	/* see sock_cgroup_ptr() */
762 }
763 
764 #else	/* CONFIG_SOCK_CGROUP_DATA */
765 
766 struct sock_cgroup_data {
767 };
768 
769 #endif	/* CONFIG_SOCK_CGROUP_DATA */
770 
771 #endif	/* _LINUX_CGROUP_DEFS_H */
772