xref: /linux/kernel/cgroup/rstat.c (revision 4f372263ef92ed2af55a8c226750b72021ff8d0f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include "cgroup-internal.h"
3 
4 #include <linux/sched/cputime.h>
5 
6 #include <linux/bpf.h>
7 #include <linux/btf.h>
8 #include <linux/btf_ids.h>
9 
10 #include <trace/events/cgroup.h>
11 
12 static DEFINE_SPINLOCK(rstat_base_lock);
13 static DEFINE_PER_CPU(raw_spinlock_t, rstat_base_cpu_lock);
14 
15 static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu);
16 
17 /*
18  * Determines whether a given css can participate in rstat.
19  * css's that are cgroup::self use rstat for base stats.
20  * Other css's associated with a subsystem use rstat only when
21  * they define the ss->css_rstat_flush callback.
22  */
23 static inline bool css_uses_rstat(struct cgroup_subsys_state *css)
24 {
25 	return css_is_self(css) || css->ss->css_rstat_flush != NULL;
26 }
27 
28 static struct css_rstat_cpu *css_rstat_cpu(
29 		struct cgroup_subsys_state *css, int cpu)
30 {
31 	return per_cpu_ptr(css->rstat_cpu, cpu);
32 }
33 
34 static struct cgroup_rstat_base_cpu *cgroup_rstat_base_cpu(
35 		struct cgroup *cgrp, int cpu)
36 {
37 	return per_cpu_ptr(cgrp->rstat_base_cpu, cpu);
38 }
39 
40 static spinlock_t *ss_rstat_lock(struct cgroup_subsys *ss)
41 {
42 	if (ss)
43 		return &ss->rstat_ss_lock;
44 
45 	return &rstat_base_lock;
46 }
47 
48 static raw_spinlock_t *ss_rstat_cpu_lock(struct cgroup_subsys *ss, int cpu)
49 {
50 	if (ss)
51 		return per_cpu_ptr(ss->rstat_ss_cpu_lock, cpu);
52 
53 	return per_cpu_ptr(&rstat_base_cpu_lock, cpu);
54 }
55 
56 /*
57  * Helper functions for rstat per CPU locks.
58  *
59  * This makes it easier to diagnose locking issues and contention in
60  * production environments. The parameter @fast_path determine the
61  * tracepoints being added, allowing us to diagnose "flush" related
62  * operations without handling high-frequency fast-path "update" events.
63  */
64 static __always_inline
65 unsigned long _css_rstat_cpu_lock(struct cgroup_subsys_state *css, int cpu,
66 		const bool fast_path)
67 {
68 	struct cgroup *cgrp = css->cgroup;
69 	raw_spinlock_t *cpu_lock;
70 	unsigned long flags;
71 	bool contended;
72 
73 	/*
74 	 * The _irqsave() is needed because the locks used for flushing are
75 	 * spinlock_t which is a sleeping lock on PREEMPT_RT. Acquiring this lock
76 	 * with the _irq() suffix only disables interrupts on a non-PREEMPT_RT
77 	 * kernel. The raw_spinlock_t below disables interrupts on both
78 	 * configurations. The _irqsave() ensures that interrupts are always
79 	 * disabled and later restored.
80 	 */
81 	cpu_lock = ss_rstat_cpu_lock(css->ss, cpu);
82 	contended = !raw_spin_trylock_irqsave(cpu_lock, flags);
83 	if (contended) {
84 		if (fast_path)
85 			trace_cgroup_rstat_cpu_lock_contended_fastpath(cgrp, cpu, contended);
86 		else
87 			trace_cgroup_rstat_cpu_lock_contended(cgrp, cpu, contended);
88 
89 		raw_spin_lock_irqsave(cpu_lock, flags);
90 	}
91 
92 	if (fast_path)
93 		trace_cgroup_rstat_cpu_locked_fastpath(cgrp, cpu, contended);
94 	else
95 		trace_cgroup_rstat_cpu_locked(cgrp, cpu, contended);
96 
97 	return flags;
98 }
99 
100 static __always_inline
101 void _css_rstat_cpu_unlock(struct cgroup_subsys_state *css, int cpu,
102 		unsigned long flags, const bool fast_path)
103 {
104 	struct cgroup *cgrp = css->cgroup;
105 	raw_spinlock_t *cpu_lock;
106 
107 	if (fast_path)
108 		trace_cgroup_rstat_cpu_unlock_fastpath(cgrp, cpu, false);
109 	else
110 		trace_cgroup_rstat_cpu_unlock(cgrp, cpu, false);
111 
112 	cpu_lock = ss_rstat_cpu_lock(css->ss, cpu);
113 	raw_spin_unlock_irqrestore(cpu_lock, flags);
114 }
115 
116 /**
117  * css_rstat_updated - keep track of updated rstat_cpu
118  * @css: target cgroup subsystem state
119  * @cpu: cpu on which rstat_cpu was updated
120  *
121  * @css's rstat_cpu on @cpu was updated. Put it on the parent's matching
122  * rstat_cpu->updated_children list. See the comment on top of
123  * css_rstat_cpu definition for details.
124  */
125 __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
126 {
127 	unsigned long flags;
128 
129 	/*
130 	 * Since bpf programs can call this function, prevent access to
131 	 * uninitialized rstat pointers.
132 	 */
133 	if (!css_uses_rstat(css))
134 		return;
135 
136 	/*
137 	 * Speculative already-on-list test. This may race leading to
138 	 * temporary inaccuracies, which is fine.
139 	 *
140 	 * Because @parent's updated_children is terminated with @parent
141 	 * instead of NULL, we can tell whether @css is on the list by
142 	 * testing the next pointer for NULL.
143 	 */
144 	if (data_race(css_rstat_cpu(css, cpu)->updated_next))
145 		return;
146 
147 	flags = _css_rstat_cpu_lock(css, cpu, true);
148 
149 	/* put @css and all ancestors on the corresponding updated lists */
150 	while (true) {
151 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
152 		struct cgroup_subsys_state *parent = css->parent;
153 		struct css_rstat_cpu *prstatc;
154 
155 		/*
156 		 * Both additions and removals are bottom-up.  If a cgroup
157 		 * is already in the tree, all ancestors are.
158 		 */
159 		if (rstatc->updated_next)
160 			break;
161 
162 		/* Root has no parent to link it to, but mark it busy */
163 		if (!parent) {
164 			rstatc->updated_next = css;
165 			break;
166 		}
167 
168 		prstatc = css_rstat_cpu(parent, cpu);
169 		rstatc->updated_next = prstatc->updated_children;
170 		prstatc->updated_children = css;
171 
172 		css = parent;
173 	}
174 
175 	_css_rstat_cpu_unlock(css, cpu, flags, true);
176 }
177 
178 /**
179  * css_rstat_push_children - push children css's into the given list
180  * @head: current head of the list (= subtree root)
181  * @child: first child of the root
182  * @cpu: target cpu
183  * Return: A new singly linked list of css's to be flushed
184  *
185  * Iteratively traverse down the css_rstat_cpu updated tree level by
186  * level and push all the parents first before their next level children
187  * into a singly linked list via the rstat_flush_next pointer built from the
188  * tail backward like "pushing" css's into a stack. The root is pushed by
189  * the caller.
190  */
191 static struct cgroup_subsys_state *css_rstat_push_children(
192 		struct cgroup_subsys_state *head,
193 		struct cgroup_subsys_state *child, int cpu)
194 {
195 	struct cgroup_subsys_state *cnext = child;	/* Next head of child css level */
196 	struct cgroup_subsys_state *ghead = NULL;	/* Head of grandchild css level */
197 	struct cgroup_subsys_state *parent, *grandchild;
198 	struct css_rstat_cpu *crstatc;
199 
200 	child->rstat_flush_next = NULL;
201 
202 	/*
203 	 * The subsystem rstat lock must be held for the whole duration from
204 	 * here as the rstat_flush_next list is being constructed to when
205 	 * it is consumed later in css_rstat_flush().
206 	 */
207 	lockdep_assert_held(ss_rstat_lock(head->ss));
208 
209 	/*
210 	 * Notation: -> updated_next pointer
211 	 *	     => rstat_flush_next pointer
212 	 *
213 	 * Assuming the following sample updated_children lists:
214 	 *  P: C1 -> C2 -> P
215 	 *  C1: G11 -> G12 -> C1
216 	 *  C2: G21 -> G22 -> C2
217 	 *
218 	 * After 1st iteration:
219 	 *  head => C2 => C1 => NULL
220 	 *  ghead => G21 => G11 => NULL
221 	 *
222 	 * After 2nd iteration:
223 	 *  head => G12 => G11 => G22 => G21 => C2 => C1 => NULL
224 	 */
225 next_level:
226 	while (cnext) {
227 		child = cnext;
228 		cnext = child->rstat_flush_next;
229 		parent = child->parent;
230 
231 		/* updated_next is parent cgroup terminated if !NULL */
232 		while (child != parent) {
233 			child->rstat_flush_next = head;
234 			head = child;
235 			crstatc = css_rstat_cpu(child, cpu);
236 			grandchild = crstatc->updated_children;
237 			if (grandchild != child) {
238 				/* Push the grand child to the next level */
239 				crstatc->updated_children = child;
240 				grandchild->rstat_flush_next = ghead;
241 				ghead = grandchild;
242 			}
243 			child = crstatc->updated_next;
244 			crstatc->updated_next = NULL;
245 		}
246 	}
247 
248 	if (ghead) {
249 		cnext = ghead;
250 		ghead = NULL;
251 		goto next_level;
252 	}
253 	return head;
254 }
255 
256 /**
257  * css_rstat_updated_list - build a list of updated css's to be flushed
258  * @root: root of the css subtree to traverse
259  * @cpu: target cpu
260  * Return: A singly linked list of css's to be flushed
261  *
262  * Walks the updated rstat_cpu tree on @cpu from @root.  During traversal,
263  * each returned css is unlinked from the updated tree.
264  *
265  * The only ordering guarantee is that, for a parent and a child pair
266  * covered by a given traversal, the child is before its parent in
267  * the list.
268  *
269  * Note that updated_children is self terminated and points to a list of
270  * child css's if not empty. Whereas updated_next is like a sibling link
271  * within the children list and terminated by the parent css. An exception
272  * here is the css root whose updated_next can be self terminated.
273  */
274 static struct cgroup_subsys_state *css_rstat_updated_list(
275 		struct cgroup_subsys_state *root, int cpu)
276 {
277 	struct css_rstat_cpu *rstatc = css_rstat_cpu(root, cpu);
278 	struct cgroup_subsys_state *head = NULL, *parent, *child;
279 	unsigned long flags;
280 
281 	flags = _css_rstat_cpu_lock(root, cpu, false);
282 
283 	/* Return NULL if this subtree is not on-list */
284 	if (!rstatc->updated_next)
285 		goto unlock_ret;
286 
287 	/*
288 	 * Unlink @root from its parent. As the updated_children list is
289 	 * singly linked, we have to walk it to find the removal point.
290 	 */
291 	parent = root->parent;
292 	if (parent) {
293 		struct css_rstat_cpu *prstatc;
294 		struct cgroup_subsys_state **nextp;
295 
296 		prstatc = css_rstat_cpu(parent, cpu);
297 		nextp = &prstatc->updated_children;
298 		while (*nextp != root) {
299 			struct css_rstat_cpu *nrstatc;
300 
301 			nrstatc = css_rstat_cpu(*nextp, cpu);
302 			WARN_ON_ONCE(*nextp == parent);
303 			nextp = &nrstatc->updated_next;
304 		}
305 		*nextp = rstatc->updated_next;
306 	}
307 
308 	rstatc->updated_next = NULL;
309 
310 	/* Push @root to the list first before pushing the children */
311 	head = root;
312 	root->rstat_flush_next = NULL;
313 	child = rstatc->updated_children;
314 	rstatc->updated_children = root;
315 	if (child != root)
316 		head = css_rstat_push_children(head, child, cpu);
317 unlock_ret:
318 	_css_rstat_cpu_unlock(root, cpu, flags, false);
319 	return head;
320 }
321 
322 /*
323  * A hook for bpf stat collectors to attach to and flush their stats.
324  * Together with providing bpf kfuncs for css_rstat_updated() and
325  * css_rstat_flush(), this enables a complete workflow where bpf progs that
326  * collect cgroup stats can integrate with rstat for efficient flushing.
327  *
328  * A static noinline declaration here could cause the compiler to optimize away
329  * the function. A global noinline declaration will keep the definition, but may
330  * optimize away the callsite. Therefore, __weak is needed to ensure that the
331  * call is still emitted, by telling the compiler that we don't know what the
332  * function might eventually be.
333  */
334 
335 __bpf_hook_start();
336 
337 __weak noinline void bpf_rstat_flush(struct cgroup *cgrp,
338 				     struct cgroup *parent, int cpu)
339 {
340 }
341 
342 __bpf_hook_end();
343 
344 /*
345  * Helper functions for locking.
346  *
347  * This makes it easier to diagnose locking issues and contention in
348  * production environments.  The parameter @cpu_in_loop indicate lock
349  * was released and re-taken when collection data from the CPUs. The
350  * value -1 is used when obtaining the main lock else this is the CPU
351  * number processed last.
352  */
353 static inline void __css_rstat_lock(struct cgroup_subsys_state *css,
354 		int cpu_in_loop)
355 	__acquires(ss_rstat_lock(css->ss))
356 {
357 	struct cgroup *cgrp = css->cgroup;
358 	spinlock_t *lock;
359 	bool contended;
360 
361 	lock = ss_rstat_lock(css->ss);
362 	contended = !spin_trylock_irq(lock);
363 	if (contended) {
364 		trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, contended);
365 		spin_lock_irq(lock);
366 	}
367 	trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended);
368 }
369 
370 static inline void __css_rstat_unlock(struct cgroup_subsys_state *css,
371 				      int cpu_in_loop)
372 	__releases(ss_rstat_lock(css->ss))
373 {
374 	struct cgroup *cgrp = css->cgroup;
375 	spinlock_t *lock;
376 
377 	lock = ss_rstat_lock(css->ss);
378 	trace_cgroup_rstat_unlock(cgrp, cpu_in_loop, false);
379 	spin_unlock_irq(lock);
380 }
381 
382 /**
383  * css_rstat_flush - flush stats in @css's rstat subtree
384  * @css: target cgroup subsystem state
385  *
386  * Collect all per-cpu stats in @css's subtree into the global counters
387  * and propagate them upwards. After this function returns, all rstat
388  * nodes in the subtree have up-to-date ->stat.
389  *
390  * This also gets all rstat nodes in the subtree including @css off the
391  * ->updated_children lists.
392  *
393  * This function may block.
394  */
395 __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)
396 {
397 	int cpu;
398 	bool is_self = css_is_self(css);
399 
400 	/*
401 	 * Since bpf programs can call this function, prevent access to
402 	 * uninitialized rstat pointers.
403 	 */
404 	if (!css_uses_rstat(css))
405 		return;
406 
407 	might_sleep();
408 	for_each_possible_cpu(cpu) {
409 		struct cgroup_subsys_state *pos;
410 
411 		/* Reacquire for each CPU to avoid disabling IRQs too long */
412 		__css_rstat_lock(css, cpu);
413 		pos = css_rstat_updated_list(css, cpu);
414 		for (; pos; pos = pos->rstat_flush_next) {
415 			if (is_self) {
416 				cgroup_base_stat_flush(pos->cgroup, cpu);
417 				bpf_rstat_flush(pos->cgroup,
418 						cgroup_parent(pos->cgroup), cpu);
419 			} else
420 				pos->ss->css_rstat_flush(pos, cpu);
421 		}
422 		__css_rstat_unlock(css, cpu);
423 		if (!cond_resched())
424 			cpu_relax();
425 	}
426 }
427 
428 int css_rstat_init(struct cgroup_subsys_state *css)
429 {
430 	struct cgroup *cgrp = css->cgroup;
431 	int cpu;
432 	bool is_self = css_is_self(css);
433 
434 	if (is_self) {
435 		/* the root cgrp has rstat_base_cpu preallocated */
436 		if (!cgrp->rstat_base_cpu) {
437 			cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu);
438 			if (!cgrp->rstat_base_cpu)
439 				return -ENOMEM;
440 		}
441 	} else if (css->ss->css_rstat_flush == NULL)
442 		return 0;
443 
444 	/* the root cgrp's self css has rstat_cpu preallocated */
445 	if (!css->rstat_cpu) {
446 		css->rstat_cpu = alloc_percpu(struct css_rstat_cpu);
447 		if (!css->rstat_cpu) {
448 			if (is_self)
449 				free_percpu(cgrp->rstat_base_cpu);
450 
451 			return -ENOMEM;
452 		}
453 	}
454 
455 	/* ->updated_children list is self terminated */
456 	for_each_possible_cpu(cpu) {
457 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
458 
459 		rstatc->updated_children = css;
460 
461 		if (is_self) {
462 			struct cgroup_rstat_base_cpu *rstatbc;
463 
464 			rstatbc = cgroup_rstat_base_cpu(cgrp, cpu);
465 			u64_stats_init(&rstatbc->bsync);
466 		}
467 	}
468 
469 	return 0;
470 }
471 
472 void css_rstat_exit(struct cgroup_subsys_state *css)
473 {
474 	int cpu;
475 
476 	if (!css_uses_rstat(css))
477 		return;
478 
479 	css_rstat_flush(css);
480 
481 	/* sanity check */
482 	for_each_possible_cpu(cpu) {
483 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
484 
485 		if (WARN_ON_ONCE(rstatc->updated_children != css) ||
486 		    WARN_ON_ONCE(rstatc->updated_next))
487 			return;
488 	}
489 
490 	if (css_is_self(css)) {
491 		struct cgroup *cgrp = css->cgroup;
492 
493 		free_percpu(cgrp->rstat_base_cpu);
494 		cgrp->rstat_base_cpu = NULL;
495 	}
496 
497 	free_percpu(css->rstat_cpu);
498 	css->rstat_cpu = NULL;
499 }
500 
501 /**
502  * ss_rstat_init - subsystem-specific rstat initialization
503  * @ss: target subsystem
504  *
505  * If @ss is NULL, the static locks associated with the base stats
506  * are initialized. If @ss is non-NULL, the subsystem-specific locks
507  * are initialized.
508  */
509 int __init ss_rstat_init(struct cgroup_subsys *ss)
510 {
511 	int cpu;
512 
513 #ifdef CONFIG_SMP
514 	/*
515 	 * On uniprocessor machines, arch_spinlock_t is defined as an empty
516 	 * struct. Avoid allocating a size of zero by having this block
517 	 * excluded in this case. It's acceptable to leave the subsystem locks
518 	 * unitialized since the associated lock functions are no-ops in the
519 	 * non-smp case.
520 	 */
521 	if (ss) {
522 		ss->rstat_ss_cpu_lock = alloc_percpu(raw_spinlock_t);
523 		if (!ss->rstat_ss_cpu_lock)
524 			return -ENOMEM;
525 	}
526 #endif
527 
528 	spin_lock_init(ss_rstat_lock(ss));
529 	for_each_possible_cpu(cpu)
530 		raw_spin_lock_init(ss_rstat_cpu_lock(ss, cpu));
531 
532 	return 0;
533 }
534 
535 /*
536  * Functions for cgroup basic resource statistics implemented on top of
537  * rstat.
538  */
539 static void cgroup_base_stat_add(struct cgroup_base_stat *dst_bstat,
540 				 struct cgroup_base_stat *src_bstat)
541 {
542 	dst_bstat->cputime.utime += src_bstat->cputime.utime;
543 	dst_bstat->cputime.stime += src_bstat->cputime.stime;
544 	dst_bstat->cputime.sum_exec_runtime += src_bstat->cputime.sum_exec_runtime;
545 #ifdef CONFIG_SCHED_CORE
546 	dst_bstat->forceidle_sum += src_bstat->forceidle_sum;
547 #endif
548 	dst_bstat->ntime += src_bstat->ntime;
549 }
550 
551 static void cgroup_base_stat_sub(struct cgroup_base_stat *dst_bstat,
552 				 struct cgroup_base_stat *src_bstat)
553 {
554 	dst_bstat->cputime.utime -= src_bstat->cputime.utime;
555 	dst_bstat->cputime.stime -= src_bstat->cputime.stime;
556 	dst_bstat->cputime.sum_exec_runtime -= src_bstat->cputime.sum_exec_runtime;
557 #ifdef CONFIG_SCHED_CORE
558 	dst_bstat->forceidle_sum -= src_bstat->forceidle_sum;
559 #endif
560 	dst_bstat->ntime -= src_bstat->ntime;
561 }
562 
563 static void cgroup_base_stat_flush(struct cgroup *cgrp, int cpu)
564 {
565 	struct cgroup_rstat_base_cpu *rstatbc = cgroup_rstat_base_cpu(cgrp, cpu);
566 	struct cgroup *parent = cgroup_parent(cgrp);
567 	struct cgroup_rstat_base_cpu *prstatbc;
568 	struct cgroup_base_stat delta;
569 	unsigned seq;
570 
571 	/* Root-level stats are sourced from system-wide CPU stats */
572 	if (!parent)
573 		return;
574 
575 	/* fetch the current per-cpu values */
576 	do {
577 		seq = __u64_stats_fetch_begin(&rstatbc->bsync);
578 		delta = rstatbc->bstat;
579 	} while (__u64_stats_fetch_retry(&rstatbc->bsync, seq));
580 
581 	/* propagate per-cpu delta to cgroup and per-cpu global statistics */
582 	cgroup_base_stat_sub(&delta, &rstatbc->last_bstat);
583 	cgroup_base_stat_add(&cgrp->bstat, &delta);
584 	cgroup_base_stat_add(&rstatbc->last_bstat, &delta);
585 	cgroup_base_stat_add(&rstatbc->subtree_bstat, &delta);
586 
587 	/* propagate cgroup and per-cpu global delta to parent (unless that's root) */
588 	if (cgroup_parent(parent)) {
589 		delta = cgrp->bstat;
590 		cgroup_base_stat_sub(&delta, &cgrp->last_bstat);
591 		cgroup_base_stat_add(&parent->bstat, &delta);
592 		cgroup_base_stat_add(&cgrp->last_bstat, &delta);
593 
594 		delta = rstatbc->subtree_bstat;
595 		prstatbc = cgroup_rstat_base_cpu(parent, cpu);
596 		cgroup_base_stat_sub(&delta, &rstatbc->last_subtree_bstat);
597 		cgroup_base_stat_add(&prstatbc->subtree_bstat, &delta);
598 		cgroup_base_stat_add(&rstatbc->last_subtree_bstat, &delta);
599 	}
600 }
601 
602 static struct cgroup_rstat_base_cpu *
603 cgroup_base_stat_cputime_account_begin(struct cgroup *cgrp, unsigned long *flags)
604 {
605 	struct cgroup_rstat_base_cpu *rstatbc;
606 
607 	rstatbc = get_cpu_ptr(cgrp->rstat_base_cpu);
608 	*flags = u64_stats_update_begin_irqsave(&rstatbc->bsync);
609 	return rstatbc;
610 }
611 
612 static void cgroup_base_stat_cputime_account_end(struct cgroup *cgrp,
613 						 struct cgroup_rstat_base_cpu *rstatbc,
614 						 unsigned long flags)
615 {
616 	u64_stats_update_end_irqrestore(&rstatbc->bsync, flags);
617 	css_rstat_updated(&cgrp->self, smp_processor_id());
618 	put_cpu_ptr(rstatbc);
619 }
620 
621 void __cgroup_account_cputime(struct cgroup *cgrp, u64 delta_exec)
622 {
623 	struct cgroup_rstat_base_cpu *rstatbc;
624 	unsigned long flags;
625 
626 	rstatbc = cgroup_base_stat_cputime_account_begin(cgrp, &flags);
627 	rstatbc->bstat.cputime.sum_exec_runtime += delta_exec;
628 	cgroup_base_stat_cputime_account_end(cgrp, rstatbc, flags);
629 }
630 
631 void __cgroup_account_cputime_field(struct cgroup *cgrp,
632 				    enum cpu_usage_stat index, u64 delta_exec)
633 {
634 	struct cgroup_rstat_base_cpu *rstatbc;
635 	unsigned long flags;
636 
637 	rstatbc = cgroup_base_stat_cputime_account_begin(cgrp, &flags);
638 
639 	switch (index) {
640 	case CPUTIME_NICE:
641 		rstatbc->bstat.ntime += delta_exec;
642 		fallthrough;
643 	case CPUTIME_USER:
644 		rstatbc->bstat.cputime.utime += delta_exec;
645 		break;
646 	case CPUTIME_SYSTEM:
647 	case CPUTIME_IRQ:
648 	case CPUTIME_SOFTIRQ:
649 		rstatbc->bstat.cputime.stime += delta_exec;
650 		break;
651 #ifdef CONFIG_SCHED_CORE
652 	case CPUTIME_FORCEIDLE:
653 		rstatbc->bstat.forceidle_sum += delta_exec;
654 		break;
655 #endif
656 	default:
657 		break;
658 	}
659 
660 	cgroup_base_stat_cputime_account_end(cgrp, rstatbc, flags);
661 }
662 
663 /*
664  * compute the cputime for the root cgroup by getting the per cpu data
665  * at a global level, then categorizing the fields in a manner consistent
666  * with how it is done by __cgroup_account_cputime_field for each bit of
667  * cpu time attributed to a cgroup.
668  */
669 static void root_cgroup_cputime(struct cgroup_base_stat *bstat)
670 {
671 	struct task_cputime *cputime = &bstat->cputime;
672 	int i;
673 
674 	memset(bstat, 0, sizeof(*bstat));
675 	for_each_possible_cpu(i) {
676 		struct kernel_cpustat kcpustat;
677 		u64 *cpustat = kcpustat.cpustat;
678 		u64 user = 0;
679 		u64 sys = 0;
680 
681 		kcpustat_cpu_fetch(&kcpustat, i);
682 
683 		user += cpustat[CPUTIME_USER];
684 		user += cpustat[CPUTIME_NICE];
685 		cputime->utime += user;
686 
687 		sys += cpustat[CPUTIME_SYSTEM];
688 		sys += cpustat[CPUTIME_IRQ];
689 		sys += cpustat[CPUTIME_SOFTIRQ];
690 		cputime->stime += sys;
691 
692 		cputime->sum_exec_runtime += user;
693 		cputime->sum_exec_runtime += sys;
694 
695 #ifdef CONFIG_SCHED_CORE
696 		bstat->forceidle_sum += cpustat[CPUTIME_FORCEIDLE];
697 #endif
698 		bstat->ntime += cpustat[CPUTIME_NICE];
699 	}
700 }
701 
702 
703 static void cgroup_force_idle_show(struct seq_file *seq, struct cgroup_base_stat *bstat)
704 {
705 #ifdef CONFIG_SCHED_CORE
706 	u64 forceidle_time = bstat->forceidle_sum;
707 
708 	do_div(forceidle_time, NSEC_PER_USEC);
709 	seq_printf(seq, "core_sched.force_idle_usec %llu\n", forceidle_time);
710 #endif
711 }
712 
713 void cgroup_base_stat_cputime_show(struct seq_file *seq)
714 {
715 	struct cgroup *cgrp = seq_css(seq)->cgroup;
716 	struct cgroup_base_stat bstat;
717 
718 	if (cgroup_parent(cgrp)) {
719 		css_rstat_flush(&cgrp->self);
720 		__css_rstat_lock(&cgrp->self, -1);
721 		bstat = cgrp->bstat;
722 		cputime_adjust(&cgrp->bstat.cputime, &cgrp->prev_cputime,
723 			       &bstat.cputime.utime, &bstat.cputime.stime);
724 		__css_rstat_unlock(&cgrp->self, -1);
725 	} else {
726 		root_cgroup_cputime(&bstat);
727 	}
728 
729 	do_div(bstat.cputime.sum_exec_runtime, NSEC_PER_USEC);
730 	do_div(bstat.cputime.utime, NSEC_PER_USEC);
731 	do_div(bstat.cputime.stime, NSEC_PER_USEC);
732 	do_div(bstat.ntime, NSEC_PER_USEC);
733 
734 	seq_printf(seq, "usage_usec %llu\n"
735 			"user_usec %llu\n"
736 			"system_usec %llu\n"
737 			"nice_usec %llu\n",
738 			bstat.cputime.sum_exec_runtime,
739 			bstat.cputime.utime,
740 			bstat.cputime.stime,
741 			bstat.ntime);
742 
743 	cgroup_force_idle_show(seq, &bstat);
744 }
745 
746 /* Add bpf kfuncs for css_rstat_updated() and css_rstat_flush() */
747 BTF_KFUNCS_START(bpf_rstat_kfunc_ids)
748 BTF_ID_FLAGS(func, css_rstat_updated)
749 BTF_ID_FLAGS(func, css_rstat_flush, KF_SLEEPABLE)
750 BTF_KFUNCS_END(bpf_rstat_kfunc_ids)
751 
752 static const struct btf_kfunc_id_set bpf_rstat_kfunc_set = {
753 	.owner          = THIS_MODULE,
754 	.set            = &bpf_rstat_kfunc_ids,
755 };
756 
757 static int __init bpf_rstat_kfunc_init(void)
758 {
759 	return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
760 					 &bpf_rstat_kfunc_set);
761 }
762 late_initcall(bpf_rstat_kfunc_init);
763