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