blob: 1615b9c17e0272441906ddb0d68de09c7794f7f2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Tejun Heob4a04ab2015-05-13 15:38:40 -04002/*
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>
Elena Reshetova4b9502e62017-03-08 10:00:40 +020017#include <linux/refcount.h>
Tejun Heob4a04ab2015-05-13 15:38:40 -040018#include <linux/percpu-refcount.h>
Tejun Heo7d7efec2015-05-13 16:35:16 -040019#include <linux/percpu-rwsem.h>
Tejun Heo041cd642017-09-25 08:12:05 -070020#include <linux/u64_stats_sync.h>
Tejun Heob4a04ab2015-05-13 15:38:40 -040021#include <linux/workqueue.h>
Daniel Mack30070982016-11-23 16:52:26 +010022#include <linux/bpf-cgroup.h>
Johannes Weiner2ce71352018-10-26 15:06:31 -070023#include <linux/psi_types.h>
Tejun Heob4a04ab2015-05-13 15:38:40 -040024
25#ifdef CONFIG_CGROUPS
26
27struct cgroup;
28struct cgroup_root;
29struct cgroup_subsys;
30struct cgroup_taskset;
31struct kernfs_node;
32struct kernfs_ops;
33struct kernfs_open_file;
Arnd Bergmannc80ef9e2015-05-29 10:52:59 +020034struct seq_file;
Johannes Weinerdc505372019-03-05 15:45:48 -080035struct poll_table_struct;
Tejun Heob4a04ab2015-05-13 15:38:40 -040036
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,
43enum 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 */
50enum {
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 */
Tejun Heo88cb04b2016-03-03 09:57:58 -050054 CSS_VISIBLE = (1 << 3), /* css is visible to userland */
Waiman Long33c35aa2017-05-15 09:34:06 -040055 CSS_DYING = (1 << 4), /* css is dying */
Tejun Heob4a04ab2015-05-13 15:38:40 -040056};
57
58/* bits in struct cgroup flags field */
59enum {
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,
Roman Gushchin76f969e2019-04-19 10:03:04 -070068
69 /* Control group has to be frozen. */
70 CGRP_FREEZE,
71
72 /* Cgroup is frozen. */
73 CGRP_FROZEN,
Tejun Heob4a04ab2015-05-13 15:38:40 -040074};
75
76/* cgroup_root->flags */
77enum {
Tejun Heob4a04ab2015-05-13 15:38:40 -040078 CGRP_ROOT_NOPREFIX = (1 << 1), /* mounted subsystems have no named prefix */
79 CGRP_ROOT_XATTR = (1 << 2), /* supports extended attributes */
Tejun Heo5136f632017-06-27 14:30:28 -040080
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),
Waiman Longe1cba4b2017-08-17 15:33:09 -040087
88 /*
89 * Enable cpuset controller in v1 cgroup to use v2 behavior.
90 */
91 CGRP_ROOT_CPUSET_V2_MODE = (1 << 4),
Tejun Heob4a04ab2015-05-13 15:38:40 -040092};
93
94/* cftype->flags */
95enum {
96 CFTYPE_ONLY_ON_ROOT = (1 << 0), /* only create on root cgrp */
97 CFTYPE_NOT_ON_ROOT = (1 << 1), /* don't create on root cgrp */
Tejun Heo5136f632017-06-27 14:30:28 -040098 CFTYPE_NS_DELEGATABLE = (1 << 2), /* writeable beyond delegation boundaries */
99
Tejun Heob4a04ab2015-05-13 15:38:40 -0400100 CFTYPE_NO_PREFIX = (1 << 3), /* (DON'T USE FOR NEW FILES) no subsys prefix */
Tejun Heo7dbdb192015-09-18 17:54:23 -0400101 CFTYPE_WORLD_WRITABLE = (1 << 4), /* (DON'T USE FOR NEW FILES) S_IWUGO */
Waiman Long5cf81142018-11-08 10:08:46 -0500102 CFTYPE_DEBUG = (1 << 5), /* create when cgroup_debug */
Tejun Heob4a04ab2015-05-13 15:38:40 -0400103
104 /* internal flags, do not use outside cgroup core proper */
105 __CFTYPE_ONLY_ON_DFL = (1 << 16), /* only on default hierarchy */
106 __CFTYPE_NOT_ON_DFL = (1 << 17), /* not on default hierarchy */
107};
108
109/*
Tejun Heo6f60ead2015-09-18 17:54:23 -0400110 * cgroup_file is the handle for a file instance created in a cgroup which
111 * is used, for example, to generate file changed notifications. This can
112 * be obtained by setting cftype->file_offset.
113 */
114struct cgroup_file {
115 /* do not access any fields from outside cgroup core */
Tejun Heo6f60ead2015-09-18 17:54:23 -0400116 struct kernfs_node *kn;
Tejun Heob12e3582018-04-26 14:29:04 -0700117 unsigned long notified_at;
118 struct timer_list notify_timer;
Tejun Heo6f60ead2015-09-18 17:54:23 -0400119};
120
121/*
Tejun Heob4a04ab2015-05-13 15:38:40 -0400122 * Per-subsystem/per-cgroup state maintained by the system. This is the
123 * fundamental structural building block that controllers deal with.
124 *
125 * Fields marked with "PI:" are public and immutable and may be accessed
126 * directly without synchronization.
127 */
128struct cgroup_subsys_state {
129 /* PI: the cgroup that this css is attached to */
130 struct cgroup *cgroup;
131
132 /* PI: the cgroup subsystem that this css is attached to */
133 struct cgroup_subsys *ss;
134
135 /* reference count - access via css_[try]get() and css_put() */
136 struct percpu_ref refcnt;
137
Tejun Heob4a04ab2015-05-13 15:38:40 -0400138 /* siblings list anchored at the parent's ->children */
139 struct list_head sibling;
140 struct list_head children;
141
Tejun Heo8f534702018-04-26 14:29:05 -0700142 /* flush target list anchored at cgrp->rstat_css_list */
143 struct list_head rstat_css_node;
144
Tejun Heob4a04ab2015-05-13 15:38:40 -0400145 /*
146 * PI: Subsys-unique ID. 0 is unused and root is always 1. The
147 * matching css can be looked up using css_from_id().
148 */
149 int id;
150
151 unsigned int flags;
152
153 /*
154 * Monotonically increasing unique serial number which defines a
155 * uniform order among all csses. It's guaranteed that all
156 * ->children lists are in the ascending order of ->serial_nr and
157 * used to allow interrupting and resuming iterations.
158 */
159 u64 serial_nr;
160
Tejun Heoaa226ff2016-01-21 15:31:11 -0500161 /*
162 * Incremented by online self and children. Used to guarantee that
163 * parents are not offlined before their children.
164 */
165 atomic_t online_cnt;
166
Tejun Heob4a04ab2015-05-13 15:38:40 -0400167 /* percpu_ref killing and RCU release */
Tejun Heob4a04ab2015-05-13 15:38:40 -0400168 struct work_struct destroy_work;
Tejun Heo8f36aae2018-03-14 12:45:14 -0700169 struct rcu_work destroy_rwork;
Todd Poynorb8b1a2e2017-04-06 18:47:57 -0700170
171 /*
172 * PI: the parent css. Placed here for cache proximity to following
173 * fields of the containing structure.
174 */
175 struct cgroup_subsys_state *parent;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400176};
177
178/*
179 * A css_set is a structure holding pointers to a set of
180 * cgroup_subsys_state objects. This saves space in the task struct
181 * object and speeds up fork()/exit(), since a single inc/dec and a
182 * list_add()/del() can bump the reference count on the entire cgroup
183 * set for a task.
184 */
185struct css_set {
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500186 /*
187 * Set of subsystem states, one for each subsystem. This array is
188 * immutable after creation apart from the init_css_set during
189 * subsystem registration (at boot time).
190 */
191 struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
192
193 /* reference count */
Elena Reshetova4b9502e62017-03-08 10:00:40 +0200194 refcount_t refcount;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400195
Tejun Heo454000a2017-05-15 09:34:02 -0400196 /*
197 * For a domain cgroup, the following points to self. If threaded,
198 * to the matching cset of the nearest domain ancestor. The
199 * dom_cset provides access to the domain cgroup and its csses to
200 * which domain level resource consumptions should be charged.
201 */
202 struct css_set *dom_cset;
203
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500204 /* the default cgroup associated with this css_set */
205 struct cgroup *dfl_cgrp;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400206
Waiman Long73a72422017-06-13 17:18:01 -0400207 /* internal task count, protected by css_set_lock */
208 int nr_tasks;
209
Tejun Heob4a04ab2015-05-13 15:38:40 -0400210 /*
211 * Lists running through all tasks using this cgroup group.
212 * mg_tasks lists tasks which belong to this cset but are in the
213 * process of being migrated out or in. Protected by
214 * css_set_rwsem, but, during migration, once tasks are moved to
215 * mg_tasks, it can be read safely while holding cgroup_mutex.
216 */
217 struct list_head tasks;
218 struct list_head mg_tasks;
Tejun Heoc03cd772019-05-31 10:38:58 -0700219 struct list_head dying_tasks;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400220
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500221 /* all css_task_iters currently walking this cset */
222 struct list_head task_iters;
223
224 /*
225 * On the default hierarhcy, ->subsys[ssid] may point to a css
226 * attached to an ancestor instead of the cgroup this css_set is
227 * associated with. The following node is anchored at
228 * ->subsys[ssid]->cgroup->e_csets[ssid] and provides a way to
229 * iterate through all css's attached to a given cgroup.
230 */
231 struct list_head e_cset_node[CGROUP_SUBSYS_COUNT];
232
Tejun Heo454000a2017-05-15 09:34:02 -0400233 /* all threaded csets whose ->dom_cset points to this cset */
234 struct list_head threaded_csets;
235 struct list_head threaded_csets_node;
236
Tejun Heo5f617ebb2016-12-27 14:49:05 -0500237 /*
238 * List running through all cgroup groups in the same hash
239 * slot. Protected by css_set_lock
240 */
241 struct hlist_node hlist;
242
Tejun Heob4a04ab2015-05-13 15:38:40 -0400243 /*
244 * List of cgrp_cset_links pointing at cgroups referenced from this
245 * css_set. Protected by css_set_lock.
246 */
247 struct list_head cgrp_links;
248
Tejun Heob4a04ab2015-05-13 15:38:40 -0400249 /*
250 * List of csets participating in the on-going migration either as
251 * source or destination. Protected by cgroup_mutex.
252 */
253 struct list_head mg_preload_node;
254 struct list_head mg_node;
255
256 /*
257 * If this cset is acting as the source of migration the following
Tejun Heoe4857982016-03-08 11:51:26 -0500258 * two fields are set. mg_src_cgrp and mg_dst_cgrp are
259 * respectively the source and destination cgroups of the on-going
260 * migration. mg_dst_cset is the destination cset the target tasks
261 * on this cset should be migrated to. Protected by cgroup_mutex.
Tejun Heob4a04ab2015-05-13 15:38:40 -0400262 */
263 struct cgroup *mg_src_cgrp;
Tejun Heoe4857982016-03-08 11:51:26 -0500264 struct cgroup *mg_dst_cgrp;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400265 struct css_set *mg_dst_cset;
266
Tejun Heo2b021cb2016-03-15 20:43:04 -0400267 /* dead and being drained, ignore for migration */
268 bool dead;
269
Tejun Heob4a04ab2015-05-13 15:38:40 -0400270 /* For RCU-protected deletion */
271 struct rcu_head rcu_head;
272};
273
Tejun Heod4ff7492018-04-26 14:29:04 -0700274struct cgroup_base_stat {
275 struct task_cputime cputime;
276};
277
Tejun Heo041cd642017-09-25 08:12:05 -0700278/*
Tejun Heoc58632b2018-04-26 14:29:04 -0700279 * rstat - cgroup scalable recursive statistics. Accounting is done
280 * per-cpu in cgroup_rstat_cpu which is then lazily propagated up the
281 * hierarchy on reads.
Tejun Heo041cd642017-09-25 08:12:05 -0700282 *
Tejun Heoc58632b2018-04-26 14:29:04 -0700283 * When a stat gets updated, the cgroup_rstat_cpu and its ancestors are
Tejun Heo041cd642017-09-25 08:12:05 -0700284 * linked into the updated tree. On the following read, propagation only
285 * considers and consumes the updated tree. This makes reading O(the
286 * number of descendants which have been active since last read) instead of
287 * O(the total number of descendants).
288 *
289 * This is important because there can be a lot of (draining) cgroups which
290 * aren't active and stat may be read frequently. The combination can
291 * become very expensive. By propagating selectively, increasing reading
292 * frequency decreases the cost of each read.
Tejun Heod4ff7492018-04-26 14:29:04 -0700293 *
294 * This struct hosts both the fields which implement the above -
295 * updated_children and updated_next - and the fields which track basic
296 * resource statistics on top of it - bsync, bstat and last_bstat.
Tejun Heo041cd642017-09-25 08:12:05 -0700297 */
Tejun Heoc58632b2018-04-26 14:29:04 -0700298struct cgroup_rstat_cpu {
Tejun Heo041cd642017-09-25 08:12:05 -0700299 /*
Tejun Heod4ff7492018-04-26 14:29:04 -0700300 * ->bsync protects ->bstat. These are the only fields which get
301 * updated in the hot path.
Tejun Heo041cd642017-09-25 08:12:05 -0700302 */
Tejun Heod4ff7492018-04-26 14:29:04 -0700303 struct u64_stats_sync bsync;
304 struct cgroup_base_stat bstat;
Tejun Heo041cd642017-09-25 08:12:05 -0700305
306 /*
307 * Snapshots at the last reading. These are used to calculate the
308 * deltas to propagate to the global counters.
309 */
Tejun Heod4ff7492018-04-26 14:29:04 -0700310 struct cgroup_base_stat last_bstat;
Tejun Heo041cd642017-09-25 08:12:05 -0700311
312 /*
313 * Child cgroups with stat updates on this cpu since the last read
314 * are linked on the parent's ->updated_children through
315 * ->updated_next.
316 *
317 * In addition to being more compact, singly-linked list pointing
318 * to the cgroup makes it unnecessary for each per-cpu struct to
319 * point back to the associated cgroup.
320 *
Tejun Heoc58632b2018-04-26 14:29:04 -0700321 * Protected by per-cpu cgroup_rstat_cpu_lock.
Tejun Heo041cd642017-09-25 08:12:05 -0700322 */
323 struct cgroup *updated_children; /* terminated by self cgroup */
324 struct cgroup *updated_next; /* NULL iff not on the list */
325};
326
Roman Gushchin76f969e2019-04-19 10:03:04 -0700327struct cgroup_freezer_state {
328 /* Should the cgroup and its descendants be frozen. */
329 bool freeze;
330
331 /* Should the cgroup actually be frozen? */
332 int e_freeze;
333
334 /* Fields below are protected by css_set_lock */
335
336 /* Number of frozen descendant cgroups */
337 int nr_frozen_descendants;
338
339 /*
340 * Number of tasks, which are counted as frozen:
341 * frozen, SIGSTOPped, and PTRACEd.
342 */
343 int nr_frozen_tasks;
344};
345
Tejun Heob4a04ab2015-05-13 15:38:40 -0400346struct cgroup {
347 /* self css with NULL ->ss, points back to this cgroup */
348 struct cgroup_subsys_state self;
349
350 unsigned long flags; /* "unsigned long" so bitops work */
351
352 /*
353 * idr allocated in-hierarchy ID.
354 *
355 * ID 0 is not used, the ID of the root cgroup is always 1, and a
356 * new cgroup will be assigned with a smallest available ID.
357 *
358 * Allocating/Removing ID must be protected by cgroup_mutex.
359 */
360 int id;
361
362 /*
Tejun Heob11cfb52015-11-20 15:55:52 -0500363 * The depth this cgroup is at. The root is at depth zero and each
364 * step down the hierarchy increments the level. This along with
365 * ancestor_ids[] can determine whether a given cgroup is a
366 * descendant of another without traversing the hierarchy.
367 */
368 int level;
369
Roman Gushchin1a926e02017-07-28 18:28:44 +0100370 /* Maximum allowed descent tree depth */
371 int max_depth;
372
Tejun Heob11cfb52015-11-20 15:55:52 -0500373 /*
Roman Gushchin0679dee2017-08-02 17:55:29 +0100374 * Keep track of total numbers of visible and dying descent cgroups.
375 * Dying cgroups are cgroups which were deleted by a user,
376 * but are still existing because someone else is holding a reference.
Roman Gushchin1a926e02017-07-28 18:28:44 +0100377 * max_descendants is a maximum allowed number of descent cgroups.
Roman Gushchin4dcabec2019-04-19 10:03:03 -0700378 *
379 * nr_descendants and nr_dying_descendants are protected
380 * by cgroup_mutex and css_set_lock. It's fine to read them holding
381 * any of cgroup_mutex and css_set_lock; for writing both locks
382 * should be held.
Roman Gushchin0679dee2017-08-02 17:55:29 +0100383 */
384 int nr_descendants;
385 int nr_dying_descendants;
Roman Gushchin1a926e02017-07-28 18:28:44 +0100386 int max_descendants;
Roman Gushchin0679dee2017-08-02 17:55:29 +0100387
388 /*
Tejun Heo0de09422015-10-15 16:41:49 -0400389 * Each non-empty css_set associated with this cgroup contributes
Tejun Heo788b9502017-07-16 21:43:33 -0400390 * one to nr_populated_csets. The counter is zero iff this cgroup
391 * doesn't have any tasks.
392 *
393 * All children which have non-zero nr_populated_csets and/or
Tejun Heo454000a2017-05-15 09:34:02 -0400394 * nr_populated_children of their own contribute one to either
395 * nr_populated_domain_children or nr_populated_threaded_children
396 * depending on their type. Each counter is zero iff all cgroups
397 * of the type in the subtree proper don't have any tasks.
Tejun Heob4a04ab2015-05-13 15:38:40 -0400398 */
Tejun Heo788b9502017-07-16 21:43:33 -0400399 int nr_populated_csets;
Tejun Heo454000a2017-05-15 09:34:02 -0400400 int nr_populated_domain_children;
401 int nr_populated_threaded_children;
402
403 int nr_threaded_children; /* # of live threaded child cgroups */
Tejun Heob4a04ab2015-05-13 15:38:40 -0400404
405 struct kernfs_node *kn; /* cgroup kernfs entry */
Tejun Heo6f60ead2015-09-18 17:54:23 -0400406 struct cgroup_file procs_file; /* handle for "cgroup.procs" */
407 struct cgroup_file events_file; /* handle for "cgroup.events" */
Tejun Heob4a04ab2015-05-13 15:38:40 -0400408
409 /*
410 * The bitmask of subsystems enabled on the child cgroups.
411 * ->subtree_control is the one configured through
Tejun Heo8699b772016-02-22 22:25:46 -0500412 * "cgroup.subtree_control" while ->child_ss_mask is the effective
413 * one which may have more subsystems enabled. Controller knobs
414 * are made available iff it's enabled in ->subtree_control.
Tejun Heob4a04ab2015-05-13 15:38:40 -0400415 */
Tejun Heo6e5c8302016-02-22 22:25:47 -0500416 u16 subtree_control;
417 u16 subtree_ss_mask;
Tejun Heo15a27c32016-03-03 09:57:59 -0500418 u16 old_subtree_control;
419 u16 old_subtree_ss_mask;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400420
421 /* Private pointers for each registered subsystem */
422 struct cgroup_subsys_state __rcu *subsys[CGROUP_SUBSYS_COUNT];
423
424 struct cgroup_root *root;
425
426 /*
427 * List of cgrp_cset_links pointing at css_sets with tasks in this
428 * cgroup. Protected by css_set_lock.
429 */
430 struct list_head cset_links;
431
432 /*
433 * On the default hierarchy, a css_set for a cgroup with some
434 * susbsys disabled will point to css's which are associated with
435 * the closest ancestor which has the subsys enabled. The
436 * following lists all css_sets which point to this cgroup's css
437 * for the given subsystem.
438 */
439 struct list_head e_csets[CGROUP_SUBSYS_COUNT];
440
441 /*
Tejun Heo454000a2017-05-15 09:34:02 -0400442 * If !threaded, self. If threaded, it points to the nearest
443 * domain ancestor. Inside a threaded subtree, cgroups are exempt
444 * from process granularity and no-internal-task constraint.
445 * Domain level resource consumptions which aren't tied to a
446 * specific task are charged to the dom_cgrp.
447 */
448 struct cgroup *dom_cgrp;
Tejun Heo479adb82018-10-04 13:28:08 -0700449 struct cgroup *old_dom_cgrp; /* used while enabling threaded */
Tejun Heo454000a2017-05-15 09:34:02 -0400450
Tejun Heoc58632b2018-04-26 14:29:04 -0700451 /* per-cpu recursive resource statistics */
452 struct cgroup_rstat_cpu __percpu *rstat_cpu;
Tejun Heo8f534702018-04-26 14:29:05 -0700453 struct list_head rstat_css_list;
Tejun Heoc58632b2018-04-26 14:29:04 -0700454
Tejun Heo041cd642017-09-25 08:12:05 -0700455 /* cgroup basic resource statistics */
Tejun Heod4ff7492018-04-26 14:29:04 -0700456 struct cgroup_base_stat pending_bstat; /* pending from children */
457 struct cgroup_base_stat bstat;
458 struct prev_cputime prev_cputime; /* for printing out cputime */
Tejun Heo041cd642017-09-25 08:12:05 -0700459
Tejun Heo454000a2017-05-15 09:34:02 -0400460 /*
Tejun Heob4a04ab2015-05-13 15:38:40 -0400461 * list of pidlists, up to two for each namespace (one for procs, one
462 * for tasks); created on demand.
463 */
464 struct list_head pidlists;
465 struct mutex pidlist_mutex;
466
467 /* used to wait for offlining of csses */
468 wait_queue_head_t offline_waitq;
469
470 /* used to schedule release agent */
471 struct work_struct release_agent_work;
Tejun Heob11cfb52015-11-20 15:55:52 -0500472
Johannes Weiner2ce71352018-10-26 15:06:31 -0700473 /* used to track pressure stalls */
474 struct psi_group psi;
475
Daniel Mack30070982016-11-23 16:52:26 +0100476 /* used to store eBPF programs */
477 struct cgroup_bpf bpf;
478
Josef Bacikd09d8df2018-07-03 11:14:55 -0400479 /* If there is block congestion on this cgroup. */
480 atomic_t congestion_count;
481
Roman Gushchin76f969e2019-04-19 10:03:04 -0700482 /* Used to store internal freezer state */
483 struct cgroup_freezer_state freezer;
484
Tejun Heob11cfb52015-11-20 15:55:52 -0500485 /* ids of the ancestors at each level including self */
486 int ancestor_ids[];
Tejun Heob4a04ab2015-05-13 15:38:40 -0400487};
488
489/*
490 * A cgroup_root represents the root of a cgroup hierarchy, and may be
491 * associated with a kernfs_root to form an active hierarchy. This is
492 * internal to cgroup core. Don't access directly from controllers.
493 */
494struct cgroup_root {
495 struct kernfs_root *kf_root;
496
497 /* The bitmask of subsystems attached to this hierarchy */
498 unsigned int subsys_mask;
499
500 /* Unique id for this hierarchy. */
501 int hierarchy_id;
502
503 /* The root cgroup. Root is destroyed on its release. */
504 struct cgroup cgrp;
505
Tejun Heob11cfb52015-11-20 15:55:52 -0500506 /* for cgrp->ancestor_ids[0] */
507 int cgrp_ancestor_id_storage;
508
Tejun Heob4a04ab2015-05-13 15:38:40 -0400509 /* Number of cgroups in the hierarchy, used only for /proc/cgroups */
510 atomic_t nr_cgrps;
511
512 /* A list running through the active hierarchies */
513 struct list_head root_list;
514
515 /* Hierarchy-specific flags */
516 unsigned int flags;
517
518 /* IDs for cgroups in this hierarchy */
519 struct idr cgroup_idr;
520
521 /* The path to use for release notifications. */
522 char release_agent_path[PATH_MAX];
523
524 /* The name for this hierarchy - may be empty */
525 char name[MAX_CGROUP_ROOT_NAMELEN];
526};
527
528/*
529 * struct cftype: handler definitions for cgroup control files
530 *
531 * When reading/writing to a file:
532 * - the cgroup to use is file->f_path.dentry->d_parent->d_fsdata
533 * - the 'cftype' of the file is file->f_path.dentry->d_fsdata
534 */
535struct cftype {
536 /*
537 * By convention, the name should begin with the name of the
538 * subsystem, followed by a period. Zero length string indicates
539 * end of cftype array.
540 */
541 char name[MAX_CFTYPE_NAME];
Tejun Heo731a9812015-08-11 13:35:42 -0400542 unsigned long private;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400543
544 /*
545 * The maximum length of string, excluding trailing nul, that can
546 * be passed to write. If < PAGE_SIZE-1, PAGE_SIZE-1 is assumed.
547 */
548 size_t max_write_len;
549
550 /* CFTYPE_* flags */
551 unsigned int flags;
552
553 /*
Tejun Heo6f60ead2015-09-18 17:54:23 -0400554 * If non-zero, should contain the offset from the start of css to
555 * a struct cgroup_file field. cgroup will record the handle of
556 * the created file into it. The recorded handle can be used as
557 * long as the containing css remains accessible.
558 */
559 unsigned int file_offset;
560
561 /*
Tejun Heob4a04ab2015-05-13 15:38:40 -0400562 * Fields used for internal bookkeeping. Initialized automatically
563 * during registration.
564 */
565 struct cgroup_subsys *ss; /* NULL for cgroup core files */
566 struct list_head node; /* anchored at ss->cfts */
567 struct kernfs_ops *kf_ops;
568
Tejun Heoe90cbeb2016-12-27 14:49:03 -0500569 int (*open)(struct kernfs_open_file *of);
570 void (*release)(struct kernfs_open_file *of);
571
Tejun Heob4a04ab2015-05-13 15:38:40 -0400572 /*
573 * read_u64() is a shortcut for the common case of returning a
574 * single integer. Use it in place of read()
575 */
576 u64 (*read_u64)(struct cgroup_subsys_state *css, struct cftype *cft);
577 /*
578 * read_s64() is a signed version of read_u64()
579 */
580 s64 (*read_s64)(struct cgroup_subsys_state *css, struct cftype *cft);
581
582 /* generic seq_file read interface */
583 int (*seq_show)(struct seq_file *sf, void *v);
584
585 /* optional ops, implement all or none */
586 void *(*seq_start)(struct seq_file *sf, loff_t *ppos);
587 void *(*seq_next)(struct seq_file *sf, void *v, loff_t *ppos);
588 void (*seq_stop)(struct seq_file *sf, void *v);
589
590 /*
591 * write_u64() is a shortcut for the common case of accepting
592 * a single integer (as parsed by simple_strtoull) from
593 * userspace. Use in place of write(); return 0 or error.
594 */
595 int (*write_u64)(struct cgroup_subsys_state *css, struct cftype *cft,
596 u64 val);
597 /*
598 * write_s64() is a signed version of write_u64()
599 */
600 int (*write_s64)(struct cgroup_subsys_state *css, struct cftype *cft,
601 s64 val);
602
603 /*
604 * write() is the generic write callback which maps directly to
605 * kernfs write operation and overrides all other operations.
606 * Maximum write size is determined by ->max_write_len. Use
607 * of_css/cft() to access the associated css and cft.
608 */
609 ssize_t (*write)(struct kernfs_open_file *of,
610 char *buf, size_t nbytes, loff_t off);
611
Johannes Weinerdc505372019-03-05 15:45:48 -0800612 __poll_t (*poll)(struct kernfs_open_file *of,
613 struct poll_table_struct *pt);
614
Tejun Heob4a04ab2015-05-13 15:38:40 -0400615#ifdef CONFIG_DEBUG_LOCK_ALLOC
616 struct lock_class_key lockdep_key;
617#endif
618};
619
620/*
621 * Control Group subsystem type.
Matt Roper392536b2017-12-29 12:02:00 -0800622 * See Documentation/cgroup-v1/cgroups.txt for details
Tejun Heob4a04ab2015-05-13 15:38:40 -0400623 */
624struct cgroup_subsys {
625 struct cgroup_subsys_state *(*css_alloc)(struct cgroup_subsys_state *parent_css);
626 int (*css_online)(struct cgroup_subsys_state *css);
627 void (*css_offline)(struct cgroup_subsys_state *css);
628 void (*css_released)(struct cgroup_subsys_state *css);
629 void (*css_free)(struct cgroup_subsys_state *css);
630 void (*css_reset)(struct cgroup_subsys_state *css);
Tejun Heo8f534702018-04-26 14:29:05 -0700631 void (*css_rstat_flush)(struct cgroup_subsys_state *css, int cpu);
Tejun Heod41bf8c2017-10-23 16:18:27 -0700632 int (*css_extra_stat_show)(struct seq_file *seq,
633 struct cgroup_subsys_state *css);
Tejun Heob4a04ab2015-05-13 15:38:40 -0400634
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500635 int (*can_attach)(struct cgroup_taskset *tset);
636 void (*cancel_attach)(struct cgroup_taskset *tset);
637 void (*attach)(struct cgroup_taskset *tset);
Tejun Heo5cf1cac2016-04-21 19:06:48 -0400638 void (*post_attach)(void);
Oleg Nesterovb53202e2015-12-03 10:24:08 -0500639 int (*can_fork)(struct task_struct *task);
640 void (*cancel_fork)(struct task_struct *task);
641 void (*fork)(struct task_struct *task);
Tejun Heo2e91fa72015-10-15 16:41:53 -0400642 void (*exit)(struct task_struct *task);
Oleg Nesterov51bee5a2019-01-28 17:00:13 +0100643 void (*release)(struct task_struct *task);
Tejun Heob4a04ab2015-05-13 15:38:40 -0400644 void (*bind)(struct cgroup_subsys_state *root_css);
645
Tejun Heob38e42e2016-02-23 10:00:50 -0500646 bool early_init:1;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400647
648 /*
Tejun Heof6d635ad2016-03-08 11:51:26 -0500649 * If %true, the controller, on the default hierarchy, doesn't show
650 * up in "cgroup.controllers" or "cgroup.subtree_control", is
651 * implicitly enabled on all cgroups on the default hierarchy, and
652 * bypasses the "no internal process" constraint. This is for
653 * utility type controllers which is transparent to userland.
654 *
655 * An implicit controller can be stolen from the default hierarchy
656 * anytime and thus must be okay with offline csses from previous
657 * hierarchies coexisting with csses for the current one.
658 */
659 bool implicit_on_dfl:1;
660
661 /*
Tejun Heo8cfd8142017-07-21 11:14:51 -0400662 * If %true, the controller, supports threaded mode on the default
663 * hierarchy. In a threaded subtree, both process granularity and
664 * no-internal-process constraint are ignored and a threaded
665 * controllers should be able to handle that.
666 *
667 * Note that as an implicit controller is automatically enabled on
668 * all cgroups on the default hierarchy, it should also be
669 * threaded. implicit && !threaded is not supported.
670 */
671 bool threaded:1;
672
673 /*
Tejun Heob4a04ab2015-05-13 15:38:40 -0400674 * If %false, this subsystem is properly hierarchical -
675 * configuration, resource accounting and restriction on a parent
676 * cgroup cover those of its children. If %true, hierarchy support
677 * is broken in some ways - some subsystems ignore hierarchy
678 * completely while others are only implemented half-way.
679 *
680 * It's now disallowed to create nested cgroups if the subsystem is
681 * broken and cgroup core will emit a warning message on such
682 * cases. Eventually, all subsystems will be made properly
683 * hierarchical and this will go away.
684 */
Tejun Heob38e42e2016-02-23 10:00:50 -0500685 bool broken_hierarchy:1;
686 bool warned_broken_hierarchy:1;
Tejun Heob4a04ab2015-05-13 15:38:40 -0400687
688 /* the following two fields are initialized automtically during boot */
689 int id;
690 const char *name;
691
Tejun Heo3e1d2ee2015-08-18 13:58:16 -0700692 /* optional, initialized automatically during boot if not set */
693 const char *legacy_name;
694
Tejun Heob4a04ab2015-05-13 15:38:40 -0400695 /* link to parent, protected by cgroup_lock() */
696 struct cgroup_root *root;
697
698 /* idr for css->id */
699 struct idr css_idr;
700
701 /*
702 * List of cftypes. Each entry is the first entry of an array
703 * terminated by zero length name.
704 */
705 struct list_head cfts;
706
707 /*
708 * Base cftypes which are automatically registered. The two can
709 * point to the same array.
710 */
711 struct cftype *dfl_cftypes; /* for the default hierarchy */
712 struct cftype *legacy_cftypes; /* for the legacy hierarchies */
713
714 /*
715 * A subsystem may depend on other subsystems. When such subsystem
716 * is enabled on a cgroup, the depended-upon subsystems are enabled
717 * together if available. Subsystems enabled due to dependency are
718 * not visible to userland until explicitly enabled. The following
719 * specifies the mask of subsystems that this one depends on.
720 */
721 unsigned int depends_on;
722};
723
Tejun Heo1ed13282015-09-16 12:53:17 -0400724extern struct percpu_rw_semaphore cgroup_threadgroup_rwsem;
725
726/**
727 * cgroup_threadgroup_change_begin - threadgroup exclusion for cgroups
728 * @tsk: target task
729 *
Ingo Molnar780de9d2017-02-02 11:50:56 +0100730 * Allows cgroup operations to synchronize against threadgroup changes
731 * using a percpu_rw_semaphore.
Tejun Heo1ed13282015-09-16 12:53:17 -0400732 */
733static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
734{
735 percpu_down_read(&cgroup_threadgroup_rwsem);
736}
737
738/**
739 * cgroup_threadgroup_change_end - threadgroup exclusion for cgroups
740 * @tsk: target task
741 *
Ingo Molnar780de9d2017-02-02 11:50:56 +0100742 * Counterpart of cgroup_threadcgroup_change_begin().
Tejun Heo1ed13282015-09-16 12:53:17 -0400743 */
744static inline void cgroup_threadgroup_change_end(struct task_struct *tsk)
745{
746 percpu_up_read(&cgroup_threadgroup_rwsem);
747}
Tejun Heo7d7efec2015-05-13 16:35:16 -0400748
749#else /* CONFIG_CGROUPS */
750
Aleksa Saraicb4a3162015-06-06 10:02:14 +1000751#define CGROUP_SUBSYS_COUNT 0
752
Ingo Molnar780de9d2017-02-02 11:50:56 +0100753static inline void cgroup_threadgroup_change_begin(struct task_struct *tsk)
754{
755 might_sleep();
756}
757
Tejun Heo7d7efec2015-05-13 16:35:16 -0400758static inline void cgroup_threadgroup_change_end(struct task_struct *tsk) {}
759
Tejun Heob4a04ab2015-05-13 15:38:40 -0400760#endif /* CONFIG_CGROUPS */
Tejun Heo7d7efec2015-05-13 16:35:16 -0400761
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500762#ifdef CONFIG_SOCK_CGROUP_DATA
763
Tejun Heobd1060a2015-12-07 17:38:53 -0500764/*
765 * sock_cgroup_data is embedded at sock->sk_cgrp_data and contains
766 * per-socket cgroup information except for memcg association.
767 *
768 * On legacy hierarchies, net_prio and net_cls controllers directly set
769 * attributes on each sock which can then be tested by the network layer.
770 * On the default hierarchy, each sock is associated with the cgroup it was
771 * created in and the networking layer can match the cgroup directly.
772 *
773 * To avoid carrying all three cgroup related fields separately in sock,
774 * sock_cgroup_data overloads (prioidx, classid) and the cgroup pointer.
775 * On boot, sock_cgroup_data records the cgroup that the sock was created
776 * in so that cgroup2 matches can be made; however, once either net_prio or
777 * net_cls starts being used, the area is overriden to carry prioidx and/or
778 * classid. The two modes are distinguished by whether the lowest bit is
779 * set. Clear bit indicates cgroup pointer while set bit prioidx and
780 * classid.
781 *
782 * While userland may start using net_prio or net_cls at any time, once
783 * either is used, cgroup2 matching no longer works. There is no reason to
784 * mix the two and this is in line with how legacy and v2 compatibility is
785 * handled. On mode switch, cgroup references which are already being
786 * pointed to by socks may be leaked. While this can be remedied by adding
787 * synchronization around sock_cgroup_data, given that the number of leaked
788 * cgroups is bound and highly unlikely to be high, this seems to be the
789 * better trade-off.
790 */
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500791struct sock_cgroup_data {
Tejun Heobd1060a2015-12-07 17:38:53 -0500792 union {
793#ifdef __LITTLE_ENDIAN
794 struct {
795 u8 is_data;
796 u8 padding;
797 u16 prioidx;
798 u32 classid;
799 } __packed;
800#else
801 struct {
802 u32 classid;
803 u16 prioidx;
804 u8 padding;
805 u8 is_data;
806 } __packed;
807#endif
808 u64 val;
809 };
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500810};
811
Tejun Heobd1060a2015-12-07 17:38:53 -0500812/*
813 * There's a theoretical window where the following accessors race with
814 * updaters and return part of the previous pointer as the prioidx or
815 * classid. Such races are short-lived and the result isn't critical.
816 */
Eric Dumazet4dcb31d2018-03-14 09:04:16 -0700817static inline u16 sock_cgroup_prioidx(const struct sock_cgroup_data *skcd)
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500818{
Tejun Heobd1060a2015-12-07 17:38:53 -0500819 /* fallback to 1 which is always the ID of the root cgroup */
820 return (skcd->is_data & 1) ? skcd->prioidx : 1;
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500821}
822
Eric Dumazet4dcb31d2018-03-14 09:04:16 -0700823static inline u32 sock_cgroup_classid(const struct sock_cgroup_data *skcd)
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500824{
Tejun Heobd1060a2015-12-07 17:38:53 -0500825 /* fallback to 0 which is the unconfigured default classid */
826 return (skcd->is_data & 1) ? skcd->classid : 0;
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500827}
828
Tejun Heobd1060a2015-12-07 17:38:53 -0500829/*
830 * If invoked concurrently, the updaters may clobber each other. The
831 * caller is responsible for synchronization.
832 */
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500833static inline void sock_cgroup_set_prioidx(struct sock_cgroup_data *skcd,
834 u16 prioidx)
835{
Tejun Heoad2c8c72015-12-09 12:30:46 -0500836 struct sock_cgroup_data skcd_buf = {{ .val = READ_ONCE(skcd->val) }};
Tejun Heobd1060a2015-12-07 17:38:53 -0500837
838 if (sock_cgroup_prioidx(&skcd_buf) == prioidx)
839 return;
840
841 if (!(skcd_buf.is_data & 1)) {
842 skcd_buf.val = 0;
843 skcd_buf.is_data = 1;
844 }
845
846 skcd_buf.prioidx = prioidx;
847 WRITE_ONCE(skcd->val, skcd_buf.val); /* see sock_cgroup_ptr() */
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500848}
849
850static inline void sock_cgroup_set_classid(struct sock_cgroup_data *skcd,
851 u32 classid)
852{
Tejun Heoad2c8c72015-12-09 12:30:46 -0500853 struct sock_cgroup_data skcd_buf = {{ .val = READ_ONCE(skcd->val) }};
Tejun Heobd1060a2015-12-07 17:38:53 -0500854
855 if (sock_cgroup_classid(&skcd_buf) == classid)
856 return;
857
858 if (!(skcd_buf.is_data & 1)) {
859 skcd_buf.val = 0;
860 skcd_buf.is_data = 1;
861 }
862
863 skcd_buf.classid = classid;
864 WRITE_ONCE(skcd->val, skcd_buf.val); /* see sock_cgroup_ptr() */
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500865}
866
867#else /* CONFIG_SOCK_CGROUP_DATA */
868
869struct sock_cgroup_data {
870};
871
872#endif /* CONFIG_SOCK_CGROUP_DATA */
873
Tejun Heob4a04ab2015-05-13 15:38:40 -0400874#endif /* _LINUX_CGROUP_DEFS_H */