xref: /linux/kernel/cgroup/rstat.c (revision 7b8e9264f55a9c320f398e337d215e68cca50131)
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(struct llist_head, rstat_backlog_list);
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 inline struct llist_head *ss_lhead_cpu(struct cgroup_subsys *ss, int cpu)
49 {
50 	if (ss)
51 		return per_cpu_ptr(ss->lhead, cpu);
52 	return per_cpu_ptr(&rstat_backlog_list, cpu);
53 }
54 
55 /**
56  * css_rstat_updated - keep track of updated rstat_cpu
57  * @css: target cgroup subsystem state
58  * @cpu: cpu on which rstat_cpu was updated
59  *
60  * Atomically inserts the css in the ss's llist for the given cpu. This is
61  * reentrant safe i.e. safe against softirq, hardirq and nmi. The ss's llist
62  * will be processed at the flush time to create the update tree.
63  *
64  * NOTE: if the user needs the guarantee that the updater either add itself in
65  * the lockless list or the concurrent flusher flushes its updated stats, a
66  * memory barrier is needed before the call to css_rstat_updated() i.e. a
67  * barrier after updating the per-cpu stats and before calling
68  * css_rstat_updated().
69  */
70 __bpf_kfunc void css_rstat_updated(struct cgroup_subsys_state *css, int cpu)
71 {
72 	struct llist_head *lhead;
73 	struct css_rstat_cpu *rstatc;
74 	struct llist_node *self;
75 
76 	/*
77 	 * Since bpf programs can call this function, prevent access to
78 	 * uninitialized rstat pointers.
79 	 */
80 	if (!css_uses_rstat(css))
81 		return;
82 
83 	lockdep_assert_preemption_disabled();
84 
85 	/*
86 	 * For archs withnot nmi safe cmpxchg or percpu ops support, ignore
87 	 * the requests from nmi context.
88 	 */
89 	if ((!IS_ENABLED(CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG) ||
90 	     !IS_ENABLED(CONFIG_ARCH_HAS_NMI_SAFE_THIS_CPU_OPS)) && in_nmi())
91 		return;
92 
93 	rstatc = css_rstat_cpu(css, cpu);
94 	/*
95 	 * If already on list return. This check is racy and smp_mb() is needed
96 	 * to pair it with the smp_mb() in css_process_update_tree() if the
97 	 * guarantee that the updated stats are visible to concurrent flusher is
98 	 * needed.
99 	 */
100 	if (llist_on_list(&rstatc->lnode))
101 		return;
102 
103 	/*
104 	 * This function can be renentered by irqs and nmis for the same cgroup
105 	 * and may try to insert the same per-cpu lnode into the llist. Note
106 	 * that llist_add() does not protect against such scenarios. In addition
107 	 * this same per-cpu lnode can be modified through init_llist_node()
108 	 * from css_rstat_flush() running on a different CPU.
109 	 *
110 	 * To protect against such stacked contexts of irqs/nmis, we use the
111 	 * fact that lnode points to itself when not on a list and then use
112 	 * try_cmpxchg() to atomically set to NULL to select the winner
113 	 * which will call llist_add(). The losers can assume the insertion is
114 	 * successful and the winner will eventually add the per-cpu lnode to
115 	 * the llist.
116 	 *
117 	 * Please note that we can not use this_cpu_cmpxchg() here as on some
118 	 * archs it is not safe against modifications from multiple CPUs.
119 	 */
120 	self = &rstatc->lnode;
121 	if (!try_cmpxchg(&rstatc->lnode.next, &self, NULL))
122 		return;
123 
124 	lhead = ss_lhead_cpu(css->ss, cpu);
125 	llist_add(&rstatc->lnode, lhead);
126 }
127 
128 static void __css_process_update_tree(struct cgroup_subsys_state *css, int cpu)
129 {
130 	/* put @css and all ancestors on the corresponding updated lists */
131 	while (true) {
132 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
133 		struct cgroup_subsys_state *parent = css->parent;
134 		struct css_rstat_cpu *prstatc;
135 
136 		/*
137 		 * Both additions and removals are bottom-up.  If a cgroup
138 		 * is already in the tree, all ancestors are.
139 		 */
140 		if (rstatc->updated_next)
141 			break;
142 
143 		/* Root has no parent to link it to, but mark it busy */
144 		if (!parent) {
145 			rstatc->updated_next = css;
146 			break;
147 		}
148 
149 		prstatc = css_rstat_cpu(parent, cpu);
150 		rstatc->updated_next = prstatc->updated_children;
151 		prstatc->updated_children = css;
152 
153 		css = parent;
154 	}
155 }
156 
157 static void css_process_update_tree(struct cgroup_subsys *ss, int cpu)
158 {
159 	struct llist_head *lhead = ss_lhead_cpu(ss, cpu);
160 	struct llist_node *lnode;
161 
162 	while ((lnode = llist_del_first_init(lhead))) {
163 		struct css_rstat_cpu *rstatc;
164 
165 		/*
166 		 * smp_mb() is needed here (more specifically in between
167 		 * init_llist_node() and per-cpu stats flushing) if the
168 		 * guarantee is required by a rstat user where etiher the
169 		 * updater should add itself on the lockless list or the
170 		 * flusher flush the stats updated by the updater who have
171 		 * observed that they are already on the list. The
172 		 * corresponding barrier pair for this one should be before
173 		 * css_rstat_updated() by the user.
174 		 *
175 		 * For now, there aren't any such user, so not adding the
176 		 * barrier here but if such a use-case arise, please add
177 		 * smp_mb() here.
178 		 */
179 
180 		rstatc = container_of(lnode, struct css_rstat_cpu, lnode);
181 		__css_process_update_tree(rstatc->owner, cpu);
182 	}
183 }
184 
185 /**
186  * css_rstat_push_children - push children css's into the given list
187  * @head: current head of the list (= subtree root)
188  * @child: first child of the root
189  * @cpu: target cpu
190  * Return: A new singly linked list of css's to be flushed
191  *
192  * Iteratively traverse down the css_rstat_cpu updated tree level by
193  * level and push all the parents first before their next level children
194  * into a singly linked list via the rstat_flush_next pointer built from the
195  * tail backward like "pushing" css's into a stack. The root is pushed by
196  * the caller.
197  */
198 static struct cgroup_subsys_state *css_rstat_push_children(
199 		struct cgroup_subsys_state *head,
200 		struct cgroup_subsys_state *child, int cpu)
201 {
202 	struct cgroup_subsys_state *cnext = child;	/* Next head of child css level */
203 	struct cgroup_subsys_state *ghead = NULL;	/* Head of grandchild css level */
204 	struct cgroup_subsys_state *parent, *grandchild;
205 	struct css_rstat_cpu *crstatc;
206 
207 	child->rstat_flush_next = NULL;
208 
209 	/*
210 	 * The subsystem rstat lock must be held for the whole duration from
211 	 * here as the rstat_flush_next list is being constructed to when
212 	 * it is consumed later in css_rstat_flush().
213 	 */
214 	lockdep_assert_held(ss_rstat_lock(head->ss));
215 
216 	/*
217 	 * Notation: -> updated_next pointer
218 	 *	     => rstat_flush_next pointer
219 	 *
220 	 * Assuming the following sample updated_children lists:
221 	 *  P: C1 -> C2 -> P
222 	 *  C1: G11 -> G12 -> C1
223 	 *  C2: G21 -> G22 -> C2
224 	 *
225 	 * After 1st iteration:
226 	 *  head => C2 => C1 => NULL
227 	 *  ghead => G21 => G11 => NULL
228 	 *
229 	 * After 2nd iteration:
230 	 *  head => G12 => G11 => G22 => G21 => C2 => C1 => NULL
231 	 */
232 next_level:
233 	while (cnext) {
234 		child = cnext;
235 		cnext = child->rstat_flush_next;
236 		parent = child->parent;
237 
238 		/* updated_next is parent cgroup terminated if !NULL */
239 		while (child != parent) {
240 			child->rstat_flush_next = head;
241 			head = child;
242 			crstatc = css_rstat_cpu(child, cpu);
243 			grandchild = crstatc->updated_children;
244 			if (grandchild != child) {
245 				/* Push the grand child to the next level */
246 				crstatc->updated_children = child;
247 				grandchild->rstat_flush_next = ghead;
248 				ghead = grandchild;
249 			}
250 			child = crstatc->updated_next;
251 			crstatc->updated_next = NULL;
252 		}
253 	}
254 
255 	if (ghead) {
256 		cnext = ghead;
257 		ghead = NULL;
258 		goto next_level;
259 	}
260 	return head;
261 }
262 
263 /**
264  * css_rstat_updated_list - build a list of updated css's to be flushed
265  * @root: root of the css subtree to traverse
266  * @cpu: target cpu
267  * Return: A singly linked list of css's to be flushed
268  *
269  * Walks the updated rstat_cpu tree on @cpu from @root.  During traversal,
270  * each returned css is unlinked from the updated tree.
271  *
272  * The only ordering guarantee is that, for a parent and a child pair
273  * covered by a given traversal, the child is before its parent in
274  * the list.
275  *
276  * Note that updated_children is self terminated and points to a list of
277  * child css's if not empty. Whereas updated_next is like a sibling link
278  * within the children list and terminated by the parent css. An exception
279  * here is the css root whose updated_next can be self terminated.
280  */
281 static struct cgroup_subsys_state *css_rstat_updated_list(
282 		struct cgroup_subsys_state *root, int cpu)
283 {
284 	struct css_rstat_cpu *rstatc = css_rstat_cpu(root, cpu);
285 	struct cgroup_subsys_state *head = NULL, *parent, *child;
286 
287 	css_process_update_tree(root->ss, cpu);
288 
289 	/* Return NULL if this subtree is not on-list */
290 	if (!rstatc->updated_next)
291 		return NULL;
292 
293 	/*
294 	 * Unlink @root from its parent. As the updated_children list is
295 	 * singly linked, we have to walk it to find the removal point.
296 	 */
297 	parent = root->parent;
298 	if (parent) {
299 		struct css_rstat_cpu *prstatc;
300 		struct cgroup_subsys_state **nextp;
301 
302 		prstatc = css_rstat_cpu(parent, cpu);
303 		nextp = &prstatc->updated_children;
304 		while (*nextp != root) {
305 			struct css_rstat_cpu *nrstatc;
306 
307 			nrstatc = css_rstat_cpu(*nextp, cpu);
308 			WARN_ON_ONCE(*nextp == parent);
309 			nextp = &nrstatc->updated_next;
310 		}
311 		*nextp = rstatc->updated_next;
312 	}
313 
314 	rstatc->updated_next = NULL;
315 
316 	/* Push @root to the list first before pushing the children */
317 	head = root;
318 	root->rstat_flush_next = NULL;
319 	child = rstatc->updated_children;
320 	rstatc->updated_children = root;
321 	if (child != root)
322 		head = css_rstat_push_children(head, child, cpu);
323 
324 	return head;
325 }
326 
327 /*
328  * A hook for bpf stat collectors to attach to and flush their stats.
329  * Together with providing bpf kfuncs for css_rstat_updated() and
330  * css_rstat_flush(), this enables a complete workflow where bpf progs that
331  * collect cgroup stats can integrate with rstat for efficient flushing.
332  *
333  * A static noinline declaration here could cause the compiler to optimize away
334  * the function. A global noinline declaration will keep the definition, but may
335  * optimize away the callsite. Therefore, __weak is needed to ensure that the
336  * call is still emitted, by telling the compiler that we don't know what the
337  * function might eventually be.
338  */
339 
340 __bpf_hook_start();
341 
342 __weak noinline void bpf_rstat_flush(struct cgroup *cgrp,
343 				     struct cgroup *parent, int cpu)
344 {
345 }
346 
347 __bpf_hook_end();
348 
349 /*
350  * Helper functions for locking.
351  *
352  * This makes it easier to diagnose locking issues and contention in
353  * production environments.  The parameter @cpu_in_loop indicate lock
354  * was released and re-taken when collection data from the CPUs. The
355  * value -1 is used when obtaining the main lock else this is the CPU
356  * number processed last.
357  */
358 static inline void __css_rstat_lock(struct cgroup_subsys_state *css,
359 		int cpu_in_loop)
360 	__acquires(ss_rstat_lock(css->ss))
361 {
362 	struct cgroup *cgrp = css->cgroup;
363 	spinlock_t *lock;
364 	bool contended;
365 
366 	lock = ss_rstat_lock(css->ss);
367 	contended = !spin_trylock_irq(lock);
368 	if (contended) {
369 		trace_cgroup_rstat_lock_contended(cgrp, cpu_in_loop, contended);
370 		spin_lock_irq(lock);
371 	}
372 	trace_cgroup_rstat_locked(cgrp, cpu_in_loop, contended);
373 }
374 
375 static inline void __css_rstat_unlock(struct cgroup_subsys_state *css,
376 				      int cpu_in_loop)
377 	__releases(ss_rstat_lock(css->ss))
378 {
379 	struct cgroup *cgrp = css->cgroup;
380 	spinlock_t *lock;
381 
382 	lock = ss_rstat_lock(css->ss);
383 	trace_cgroup_rstat_unlock(cgrp, cpu_in_loop, false);
384 	spin_unlock_irq(lock);
385 }
386 
387 /**
388  * css_rstat_flush - flush stats in @css's rstat subtree
389  * @css: target cgroup subsystem state
390  *
391  * Collect all per-cpu stats in @css's subtree into the global counters
392  * and propagate them upwards. After this function returns, all rstat
393  * nodes in the subtree have up-to-date ->stat.
394  *
395  * This also gets all rstat nodes in the subtree including @css off the
396  * ->updated_children lists.
397  *
398  * This function may block.
399  */
400 __bpf_kfunc void css_rstat_flush(struct cgroup_subsys_state *css)
401 {
402 	int cpu;
403 	bool is_self = css_is_self(css);
404 
405 	/*
406 	 * Since bpf programs can call this function, prevent access to
407 	 * uninitialized rstat pointers.
408 	 */
409 	if (!css_uses_rstat(css))
410 		return;
411 
412 	might_sleep();
413 	for_each_possible_cpu(cpu) {
414 		struct cgroup_subsys_state *pos;
415 
416 		/* Reacquire for each CPU to avoid disabling IRQs too long */
417 		__css_rstat_lock(css, cpu);
418 		pos = css_rstat_updated_list(css, cpu);
419 		for (; pos; pos = pos->rstat_flush_next) {
420 			if (is_self) {
421 				cgroup_base_stat_flush(pos->cgroup, cpu);
422 				bpf_rstat_flush(pos->cgroup,
423 						cgroup_parent(pos->cgroup), cpu);
424 			} else
425 				pos->ss->css_rstat_flush(pos, cpu);
426 		}
427 		__css_rstat_unlock(css, cpu);
428 		if (!cond_resched())
429 			cpu_relax();
430 	}
431 }
432 
433 int css_rstat_init(struct cgroup_subsys_state *css)
434 {
435 	struct cgroup *cgrp = css->cgroup;
436 	int cpu;
437 	bool is_self = css_is_self(css);
438 
439 	if (is_self) {
440 		/* the root cgrp has rstat_base_cpu preallocated */
441 		if (!cgrp->rstat_base_cpu) {
442 			cgrp->rstat_base_cpu = alloc_percpu(struct cgroup_rstat_base_cpu);
443 			if (!cgrp->rstat_base_cpu)
444 				return -ENOMEM;
445 		}
446 	} else if (css->ss->css_rstat_flush == NULL)
447 		return 0;
448 
449 	/* the root cgrp's self css has rstat_cpu preallocated */
450 	if (!css->rstat_cpu) {
451 		css->rstat_cpu = alloc_percpu(struct css_rstat_cpu);
452 		if (!css->rstat_cpu) {
453 			if (is_self)
454 				free_percpu(cgrp->rstat_base_cpu);
455 
456 			return -ENOMEM;
457 		}
458 	}
459 
460 	/* ->updated_children list is self terminated */
461 	for_each_possible_cpu(cpu) {
462 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
463 
464 		rstatc->owner = rstatc->updated_children = css;
465 		init_llist_node(&rstatc->lnode);
466 
467 		if (is_self) {
468 			struct cgroup_rstat_base_cpu *rstatbc;
469 
470 			rstatbc = cgroup_rstat_base_cpu(cgrp, cpu);
471 			u64_stats_init(&rstatbc->bsync);
472 		}
473 	}
474 
475 	return 0;
476 }
477 
478 void css_rstat_exit(struct cgroup_subsys_state *css)
479 {
480 	int cpu;
481 
482 	if (!css_uses_rstat(css))
483 		return;
484 
485 	if (!css->rstat_cpu)
486 		return;
487 
488 	css_rstat_flush(css);
489 
490 	/* sanity check */
491 	for_each_possible_cpu(cpu) {
492 		struct css_rstat_cpu *rstatc = css_rstat_cpu(css, cpu);
493 
494 		if (WARN_ON_ONCE(rstatc->updated_children != css) ||
495 		    WARN_ON_ONCE(rstatc->updated_next))
496 			return;
497 	}
498 
499 	if (css_is_self(css)) {
500 		struct cgroup *cgrp = css->cgroup;
501 
502 		free_percpu(cgrp->rstat_base_cpu);
503 		cgrp->rstat_base_cpu = NULL;
504 	}
505 
506 	free_percpu(css->rstat_cpu);
507 	css->rstat_cpu = NULL;
508 }
509 
510 /**
511  * ss_rstat_init - subsystem-specific rstat initialization
512  * @ss: target subsystem
513  *
514  * If @ss is NULL, the static locks associated with the base stats
515  * are initialized. If @ss is non-NULL, the subsystem-specific locks
516  * are initialized.
517  */
518 int __init ss_rstat_init(struct cgroup_subsys *ss)
519 {
520 	int cpu;
521 
522 	if (ss) {
523 		ss->lhead = alloc_percpu(struct llist_head);
524 		if (!ss->lhead)
525 			return -ENOMEM;
526 	}
527 
528 	spin_lock_init(ss_rstat_lock(ss));
529 	for_each_possible_cpu(cpu)
530 		init_llist_head(ss_lhead_cpu(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