1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Simple CPU accounting cgroup controller
4 */
5 #include "sched.h"
6 #include "walt.h"
7
8 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
9
10 /*
11 * There are no locks covering percpu hardirq/softirq time.
12 * They are only modified in vtime_account, on corresponding CPU
13 * with interrupts disabled. So, writes are safe.
14 * They are read and saved off onto struct rq in update_rq_clock().
15 * This may result in other CPU reading this CPU's irq time and can
16 * race with irq/vtime_account on this CPU. We would either get old
17 * or new value with a side effect of accounting a slice of irq time to wrong
18 * task when irq is in progress while we read rq->clock. That is a worthy
19 * compromise in place of having locks on each irq in account_system_time.
20 */
21 DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
22
23 static int sched_clock_irqtime;
24
enable_sched_clock_irqtime(void)25 void enable_sched_clock_irqtime(void)
26 {
27 sched_clock_irqtime = 1;
28 }
29
disable_sched_clock_irqtime(void)30 void disable_sched_clock_irqtime(void)
31 {
32 sched_clock_irqtime = 0;
33 }
34
irqtime_account_delta(struct irqtime * irqtime,u64 delta,enum cpu_usage_stat idx)35 static void irqtime_account_delta(struct irqtime *irqtime, u64 delta,
36 enum cpu_usage_stat idx)
37 {
38 u64 *cpustat = kcpustat_this_cpu->cpustat;
39
40 u64_stats_update_begin(&irqtime->sync);
41 cpustat[idx] += delta;
42 irqtime->total += delta;
43 irqtime->tick_delta += delta;
44 u64_stats_update_end(&irqtime->sync);
45 }
46
47 /*
48 * Called before incrementing preempt_count on {soft,}irq_enter
49 * and before decrementing preempt_count on {soft,}irq_exit.
50 */
irqtime_account_irq(struct task_struct * curr)51 void irqtime_account_irq(struct task_struct *curr)
52 {
53 struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
54 s64 delta;
55 int cpu;
56 #ifdef CONFIG_SCHED_WALT
57 u64 wallclock;
58 bool account = true;
59 #endif
60
61 if (!sched_clock_irqtime)
62 return;
63
64 cpu = smp_processor_id();
65 #ifdef CONFIG_SCHED_WALT
66 wallclock = sched_clock_cpu(cpu);
67 #endif
68 delta = sched_clock_cpu(cpu) - irqtime->irq_start_time;
69 irqtime->irq_start_time += delta;
70
71 /*
72 * We do not account for softirq time from ksoftirqd here.
73 * We want to continue accounting softirq time to ksoftirqd thread
74 * in that case, so as not to confuse scheduler with a special task
75 * that do not consume any time, but still wants to run.
76 */
77 if (hardirq_count())
78 irqtime_account_delta(irqtime, delta, CPUTIME_IRQ);
79 else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
80 irqtime_account_delta(irqtime, delta, CPUTIME_SOFTIRQ);
81 #ifdef CONFIG_SCHED_WALT
82 else
83 account = false;
84
85 if (account)
86 sched_account_irqtime(cpu, curr, delta, wallclock);
87 #endif
88 }
89 EXPORT_SYMBOL_GPL(irqtime_account_irq);
90
irqtime_tick_accounted(u64 maxtime)91 static u64 irqtime_tick_accounted(u64 maxtime)
92 {
93 struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
94 u64 delta;
95
96 delta = min(irqtime->tick_delta, maxtime);
97 irqtime->tick_delta -= delta;
98
99 return delta;
100 }
101
102 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
103
104 #define sched_clock_irqtime (0)
105
irqtime_tick_accounted(u64 dummy)106 static u64 irqtime_tick_accounted(u64 dummy)
107 {
108 return 0;
109 }
110
111 #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
112
task_group_account_field(struct task_struct * p,int index,u64 tmp)113 static inline void task_group_account_field(struct task_struct *p, int index,
114 u64 tmp)
115 {
116 /*
117 * Since all updates are sure to touch the root cgroup, we
118 * get ourselves ahead and touch it first. If the root cgroup
119 * is the only cgroup, then nothing else should be necessary.
120 *
121 */
122 __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
123
124 cgroup_account_cputime_field(p, index, tmp);
125 }
126
127 /*
128 * Account user CPU time to a process.
129 * @p: the process that the CPU time gets accounted to
130 * @cputime: the CPU time spent in user space since the last update
131 */
account_user_time(struct task_struct * p,u64 cputime)132 void account_user_time(struct task_struct *p, u64 cputime)
133 {
134 int index;
135
136 /* Add user time to process. */
137 p->utime += cputime;
138 account_group_user_time(p, cputime);
139
140 index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
141
142 /* Add user time to cpustat. */
143 task_group_account_field(p, index, cputime);
144
145 /* Account for user time used */
146 acct_account_cputime(p);
147 }
148
149 /*
150 * Account guest CPU time to a process.
151 * @p: the process that the CPU time gets accounted to
152 * @cputime: the CPU time spent in virtual machine since the last update
153 */
account_guest_time(struct task_struct * p,u64 cputime)154 void account_guest_time(struct task_struct *p, u64 cputime)
155 {
156 u64 *cpustat = kcpustat_this_cpu->cpustat;
157
158 /* Add guest time to process. */
159 p->utime += cputime;
160 account_group_user_time(p, cputime);
161 p->gtime += cputime;
162
163 /* Add guest time to cpustat. */
164 if (task_nice(p) > 0) {
165 task_group_account_field(p, CPUTIME_NICE, cputime);
166 cpustat[CPUTIME_GUEST_NICE] += cputime;
167 } else {
168 task_group_account_field(p, CPUTIME_USER, cputime);
169 cpustat[CPUTIME_GUEST] += cputime;
170 }
171 }
172
173 /*
174 * Account system CPU time to a process and desired cpustat field
175 * @p: the process that the CPU time gets accounted to
176 * @cputime: the CPU time spent in kernel space since the last update
177 * @index: pointer to cpustat field that has to be updated
178 */
account_system_index_time(struct task_struct * p,u64 cputime,enum cpu_usage_stat index)179 void account_system_index_time(struct task_struct *p,
180 u64 cputime, enum cpu_usage_stat index)
181 {
182 /* Add system time to process. */
183 p->stime += cputime;
184 account_group_system_time(p, cputime);
185
186 /* Add system time to cpustat. */
187 task_group_account_field(p, index, cputime);
188
189 /* Account for system time used */
190 acct_account_cputime(p);
191 }
192
193 /*
194 * Account system CPU time to a process.
195 * @p: the process that the CPU time gets accounted to
196 * @hardirq_offset: the offset to subtract from hardirq_count()
197 * @cputime: the CPU time spent in kernel space since the last update
198 */
account_system_time(struct task_struct * p,int hardirq_offset,u64 cputime)199 void account_system_time(struct task_struct *p, int hardirq_offset, u64 cputime)
200 {
201 int index;
202
203 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
204 account_guest_time(p, cputime);
205 return;
206 }
207
208 if (hardirq_count() - hardirq_offset)
209 index = CPUTIME_IRQ;
210 else if (in_serving_softirq())
211 index = CPUTIME_SOFTIRQ;
212 else
213 index = CPUTIME_SYSTEM;
214
215 account_system_index_time(p, cputime, index);
216 }
217
218 /*
219 * Account for involuntary wait time.
220 * @cputime: the CPU time spent in involuntary wait
221 */
account_steal_time(u64 cputime)222 void account_steal_time(u64 cputime)
223 {
224 u64 *cpustat = kcpustat_this_cpu->cpustat;
225
226 cpustat[CPUTIME_STEAL] += cputime;
227 }
228
229 /*
230 * Account for idle time.
231 * @cputime: the CPU time spent in idle wait
232 */
account_idle_time(u64 cputime)233 void account_idle_time(u64 cputime)
234 {
235 u64 *cpustat = kcpustat_this_cpu->cpustat;
236 struct rq *rq = this_rq();
237
238 if (atomic_read(&rq->nr_iowait) > 0)
239 cpustat[CPUTIME_IOWAIT] += cputime;
240 else
241 cpustat[CPUTIME_IDLE] += cputime;
242 }
243
244 /*
245 * When a guest is interrupted for a longer amount of time, missed clock
246 * ticks are not redelivered later. Due to that, this function may on
247 * occasion account more time than the calling functions think elapsed.
248 */
steal_account_process_time(u64 maxtime)249 static __always_inline u64 steal_account_process_time(u64 maxtime)
250 {
251 #ifdef CONFIG_PARAVIRT
252 if (static_key_false(¶virt_steal_enabled)) {
253 u64 steal;
254
255 steal = paravirt_steal_clock(smp_processor_id());
256 steal -= this_rq()->prev_steal_time;
257 steal = min(steal, maxtime);
258 account_steal_time(steal);
259 this_rq()->prev_steal_time += steal;
260
261 return steal;
262 }
263 #endif
264 return 0;
265 }
266
267 /*
268 * Account how much elapsed time was spent in steal, irq, or softirq time.
269 */
account_other_time(u64 max)270 static inline u64 account_other_time(u64 max)
271 {
272 u64 accounted;
273
274 lockdep_assert_irqs_disabled();
275
276 accounted = steal_account_process_time(max);
277
278 if (accounted < max)
279 accounted += irqtime_tick_accounted(max - accounted);
280
281 return accounted;
282 }
283
284 #ifdef CONFIG_64BIT
read_sum_exec_runtime(struct task_struct * t)285 static inline u64 read_sum_exec_runtime(struct task_struct *t)
286 {
287 return t->se.sum_exec_runtime;
288 }
289 #else
read_sum_exec_runtime(struct task_struct * t)290 static u64 read_sum_exec_runtime(struct task_struct *t)
291 {
292 u64 ns;
293 struct rq_flags rf;
294 struct rq *rq;
295
296 rq = task_rq_lock(t, &rf);
297 ns = t->se.sum_exec_runtime;
298 task_rq_unlock(rq, t, &rf);
299
300 return ns;
301 }
302 #endif
303
304 /*
305 * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
306 * tasks (sum on group iteration) belonging to @tsk's group.
307 */
thread_group_cputime(struct task_struct * tsk,struct task_cputime * times)308 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
309 {
310 struct signal_struct *sig = tsk->signal;
311 u64 utime, stime;
312 struct task_struct *t;
313 unsigned int seq, nextseq;
314 unsigned long flags;
315
316 /*
317 * Update current task runtime to account pending time since last
318 * scheduler action or thread_group_cputime() call. This thread group
319 * might have other running tasks on different CPUs, but updating
320 * their runtime can affect syscall performance, so we skip account
321 * those pending times and rely only on values updated on tick or
322 * other scheduler action.
323 */
324 if (same_thread_group(current, tsk))
325 (void) task_sched_runtime(current);
326
327 rcu_read_lock();
328 /* Attempt a lockless read on the first round. */
329 nextseq = 0;
330 do {
331 seq = nextseq;
332 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
333 times->utime = sig->utime;
334 times->stime = sig->stime;
335 times->sum_exec_runtime = sig->sum_sched_runtime;
336
337 for_each_thread(tsk, t) {
338 task_cputime(t, &utime, &stime);
339 times->utime += utime;
340 times->stime += stime;
341 times->sum_exec_runtime += read_sum_exec_runtime(t);
342 }
343 /* If lockless access failed, take the lock. */
344 nextseq = 1;
345 } while (need_seqretry(&sig->stats_lock, seq));
346 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
347 rcu_read_unlock();
348 }
349
350 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
351 /*
352 * Account a tick to a process and cpustat
353 * @p: the process that the CPU time gets accounted to
354 * @user_tick: is the tick from userspace
355 * @rq: the pointer to rq
356 *
357 * Tick demultiplexing follows the order
358 * - pending hardirq update
359 * - pending softirq update
360 * - user_time
361 * - idle_time
362 * - system time
363 * - check for guest_time
364 * - else account as system_time
365 *
366 * Check for hardirq is done both for system and user time as there is
367 * no timer going off while we are on hardirq and hence we may never get an
368 * opportunity to update it solely in system time.
369 * p->stime and friends are only updated on system time and not on irq
370 * softirq as those do not count in task exec_runtime any more.
371 */
irqtime_account_process_tick(struct task_struct * p,int user_tick,int ticks)372 static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
373 int ticks)
374 {
375 u64 other, cputime = TICK_NSEC * ticks;
376
377 /*
378 * When returning from idle, many ticks can get accounted at
379 * once, including some ticks of steal, irq, and softirq time.
380 * Subtract those ticks from the amount of time accounted to
381 * idle, or potentially user or system time. Due to rounding,
382 * other time can exceed ticks occasionally.
383 */
384 other = account_other_time(ULONG_MAX);
385 if (other >= cputime)
386 return;
387
388 cputime -= other;
389
390 if (this_cpu_ksoftirqd() == p) {
391 /*
392 * ksoftirqd time do not get accounted in cpu_softirq_time.
393 * So, we have to handle it separately here.
394 * Also, p->stime needs to be updated for ksoftirqd.
395 */
396 account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
397 } else if (user_tick) {
398 account_user_time(p, cputime);
399 } else if (p == this_rq()->idle) {
400 account_idle_time(cputime);
401 } else if (p->flags & PF_VCPU) { /* System time or guest time */
402 account_guest_time(p, cputime);
403 } else {
404 account_system_index_time(p, cputime, CPUTIME_SYSTEM);
405 }
406 }
407
irqtime_account_idle_ticks(int ticks)408 static void irqtime_account_idle_ticks(int ticks)
409 {
410 irqtime_account_process_tick(current, 0, ticks);
411 }
412 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
irqtime_account_idle_ticks(int ticks)413 static inline void irqtime_account_idle_ticks(int ticks) { }
irqtime_account_process_tick(struct task_struct * p,int user_tick,int nr_ticks)414 static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
415 int nr_ticks) { }
416 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
417
418 /*
419 * Use precise platform statistics if available:
420 */
421 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
422
423 # ifndef __ARCH_HAS_VTIME_TASK_SWITCH
vtime_task_switch(struct task_struct * prev)424 void vtime_task_switch(struct task_struct *prev)
425 {
426 if (is_idle_task(prev))
427 vtime_account_idle(prev);
428 else
429 vtime_account_kernel(prev);
430
431 vtime_flush(prev);
432 arch_vtime_task_switch(prev);
433 }
434 # endif
435
436 /*
437 * Archs that account the whole time spent in the idle task
438 * (outside irq) as idle time can rely on this and just implement
439 * vtime_account_kernel() and vtime_account_idle(). Archs that
440 * have other meaning of the idle time (s390 only includes the
441 * time spent by the CPU when it's in low power mode) must override
442 * vtime_account().
443 */
444 #ifndef __ARCH_HAS_VTIME_ACCOUNT
vtime_account_irq_enter(struct task_struct * tsk)445 void vtime_account_irq_enter(struct task_struct *tsk)
446 {
447 if (!in_interrupt() && is_idle_task(tsk))
448 vtime_account_idle(tsk);
449 else
450 vtime_account_kernel(tsk);
451 }
452 EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
453 #endif /* __ARCH_HAS_VTIME_ACCOUNT */
454
cputime_adjust(struct task_cputime * curr,struct prev_cputime * prev,u64 * ut,u64 * st)455 void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
456 u64 *ut, u64 *st)
457 {
458 *ut = curr->utime;
459 *st = curr->stime;
460 }
461
task_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)462 void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
463 {
464 *ut = p->utime;
465 *st = p->stime;
466 }
467 EXPORT_SYMBOL_GPL(task_cputime_adjusted);
468
thread_group_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)469 void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
470 {
471 struct task_cputime cputime;
472
473 thread_group_cputime(p, &cputime);
474
475 *ut = cputime.utime;
476 *st = cputime.stime;
477 }
478
479 #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE: */
480
481 /*
482 * Account a single tick of CPU time.
483 * @p: the process that the CPU time gets accounted to
484 * @user_tick: indicates if the tick is a user or a system tick
485 */
account_process_tick(struct task_struct * p,int user_tick)486 void account_process_tick(struct task_struct *p, int user_tick)
487 {
488 u64 cputime, steal;
489
490 if (vtime_accounting_enabled_this_cpu())
491 return;
492
493 if (sched_clock_irqtime) {
494 irqtime_account_process_tick(p, user_tick, 1);
495 return;
496 }
497
498 cputime = TICK_NSEC;
499 steal = steal_account_process_time(ULONG_MAX);
500
501 if (steal >= cputime)
502 return;
503
504 cputime -= steal;
505
506 if (user_tick)
507 account_user_time(p, cputime);
508 else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
509 account_system_time(p, HARDIRQ_OFFSET, cputime);
510 else
511 account_idle_time(cputime);
512 }
513
514 /*
515 * Account multiple ticks of idle time.
516 * @ticks: number of stolen ticks
517 */
account_idle_ticks(unsigned long ticks)518 void account_idle_ticks(unsigned long ticks)
519 {
520 u64 cputime, steal;
521
522 if (sched_clock_irqtime) {
523 irqtime_account_idle_ticks(ticks);
524 return;
525 }
526
527 cputime = ticks * TICK_NSEC;
528 steal = steal_account_process_time(ULONG_MAX);
529
530 if (steal >= cputime)
531 return;
532
533 cputime -= steal;
534 account_idle_time(cputime);
535 }
536
537 /*
538 * Adjust tick based cputime random precision against scheduler runtime
539 * accounting.
540 *
541 * Tick based cputime accounting depend on random scheduling timeslices of a
542 * task to be interrupted or not by the timer. Depending on these
543 * circumstances, the number of these interrupts may be over or
544 * under-optimistic, matching the real user and system cputime with a variable
545 * precision.
546 *
547 * Fix this by scaling these tick based values against the total runtime
548 * accounted by the CFS scheduler.
549 *
550 * This code provides the following guarantees:
551 *
552 * stime + utime == rtime
553 * stime_i+1 >= stime_i, utime_i+1 >= utime_i
554 *
555 * Assuming that rtime_i+1 >= rtime_i.
556 */
cputime_adjust(struct task_cputime * curr,struct prev_cputime * prev,u64 * ut,u64 * st)557 void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
558 u64 *ut, u64 *st)
559 {
560 u64 rtime, stime, utime;
561 unsigned long flags;
562
563 /* Serialize concurrent callers such that we can honour our guarantees */
564 raw_spin_lock_irqsave(&prev->lock, flags);
565 rtime = curr->sum_exec_runtime;
566
567 /*
568 * This is possible under two circumstances:
569 * - rtime isn't monotonic after all (a bug);
570 * - we got reordered by the lock.
571 *
572 * In both cases this acts as a filter such that the rest of the code
573 * can assume it is monotonic regardless of anything else.
574 */
575 if (prev->stime + prev->utime >= rtime)
576 goto out;
577
578 stime = curr->stime;
579 utime = curr->utime;
580
581 /*
582 * If either stime or utime are 0, assume all runtime is userspace.
583 * Once a task gets some ticks, the monotonicy code at 'update:'
584 * will ensure things converge to the observed ratio.
585 */
586 if (stime == 0) {
587 utime = rtime;
588 goto update;
589 }
590
591 if (utime == 0) {
592 stime = rtime;
593 goto update;
594 }
595
596 stime = mul_u64_u64_div_u64(stime, rtime, stime + utime);
597
598 update:
599 /*
600 * Make sure stime doesn't go backwards; this preserves monotonicity
601 * for utime because rtime is monotonic.
602 *
603 * utime_i+1 = rtime_i+1 - stime_i
604 * = rtime_i+1 - (rtime_i - utime_i)
605 * = (rtime_i+1 - rtime_i) + utime_i
606 * >= utime_i
607 */
608 if (stime < prev->stime)
609 stime = prev->stime;
610 utime = rtime - stime;
611
612 /*
613 * Make sure utime doesn't go backwards; this still preserves
614 * monotonicity for stime, analogous argument to above.
615 */
616 if (utime < prev->utime) {
617 utime = prev->utime;
618 stime = rtime - utime;
619 }
620
621 prev->stime = stime;
622 prev->utime = utime;
623 out:
624 *ut = prev->utime;
625 *st = prev->stime;
626 raw_spin_unlock_irqrestore(&prev->lock, flags);
627 }
628
task_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)629 void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
630 {
631 struct task_cputime cputime = {
632 .sum_exec_runtime = p->se.sum_exec_runtime,
633 };
634
635 task_cputime(p, &cputime.utime, &cputime.stime);
636 cputime_adjust(&cputime, &p->prev_cputime, ut, st);
637 }
638 EXPORT_SYMBOL_GPL(task_cputime_adjusted);
639
thread_group_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)640 void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
641 {
642 struct task_cputime cputime;
643
644 thread_group_cputime(p, &cputime);
645 cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
646 }
647 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
648
649 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
vtime_delta(struct vtime * vtime)650 static u64 vtime_delta(struct vtime *vtime)
651 {
652 unsigned long long clock;
653
654 clock = sched_clock();
655 if (clock < vtime->starttime)
656 return 0;
657
658 return clock - vtime->starttime;
659 }
660
get_vtime_delta(struct vtime * vtime)661 static u64 get_vtime_delta(struct vtime *vtime)
662 {
663 u64 delta = vtime_delta(vtime);
664 u64 other;
665
666 /*
667 * Unlike tick based timing, vtime based timing never has lost
668 * ticks, and no need for steal time accounting to make up for
669 * lost ticks. Vtime accounts a rounded version of actual
670 * elapsed time. Limit account_other_time to prevent rounding
671 * errors from causing elapsed vtime to go negative.
672 */
673 other = account_other_time(delta);
674 WARN_ON_ONCE(vtime->state == VTIME_INACTIVE);
675 vtime->starttime += delta;
676
677 return delta - other;
678 }
679
vtime_account_system(struct task_struct * tsk,struct vtime * vtime)680 static void vtime_account_system(struct task_struct *tsk,
681 struct vtime *vtime)
682 {
683 vtime->stime += get_vtime_delta(vtime);
684 if (vtime->stime >= TICK_NSEC) {
685 account_system_time(tsk, irq_count(), vtime->stime);
686 vtime->stime = 0;
687 }
688 }
689
vtime_account_guest(struct task_struct * tsk,struct vtime * vtime)690 static void vtime_account_guest(struct task_struct *tsk,
691 struct vtime *vtime)
692 {
693 vtime->gtime += get_vtime_delta(vtime);
694 if (vtime->gtime >= TICK_NSEC) {
695 account_guest_time(tsk, vtime->gtime);
696 vtime->gtime = 0;
697 }
698 }
699
__vtime_account_kernel(struct task_struct * tsk,struct vtime * vtime)700 static void __vtime_account_kernel(struct task_struct *tsk,
701 struct vtime *vtime)
702 {
703 /* We might have scheduled out from guest path */
704 if (vtime->state == VTIME_GUEST)
705 vtime_account_guest(tsk, vtime);
706 else
707 vtime_account_system(tsk, vtime);
708 }
709
vtime_account_kernel(struct task_struct * tsk)710 void vtime_account_kernel(struct task_struct *tsk)
711 {
712 struct vtime *vtime = &tsk->vtime;
713
714 if (!vtime_delta(vtime))
715 return;
716
717 write_seqcount_begin(&vtime->seqcount);
718 __vtime_account_kernel(tsk, vtime);
719 write_seqcount_end(&vtime->seqcount);
720 }
721
vtime_user_enter(struct task_struct * tsk)722 void vtime_user_enter(struct task_struct *tsk)
723 {
724 struct vtime *vtime = &tsk->vtime;
725
726 write_seqcount_begin(&vtime->seqcount);
727 vtime_account_system(tsk, vtime);
728 vtime->state = VTIME_USER;
729 write_seqcount_end(&vtime->seqcount);
730 }
731
vtime_user_exit(struct task_struct * tsk)732 void vtime_user_exit(struct task_struct *tsk)
733 {
734 struct vtime *vtime = &tsk->vtime;
735
736 write_seqcount_begin(&vtime->seqcount);
737 vtime->utime += get_vtime_delta(vtime);
738 if (vtime->utime >= TICK_NSEC) {
739 account_user_time(tsk, vtime->utime);
740 vtime->utime = 0;
741 }
742 vtime->state = VTIME_SYS;
743 write_seqcount_end(&vtime->seqcount);
744 }
745
vtime_guest_enter(struct task_struct * tsk)746 void vtime_guest_enter(struct task_struct *tsk)
747 {
748 struct vtime *vtime = &tsk->vtime;
749 /*
750 * The flags must be updated under the lock with
751 * the vtime_starttime flush and update.
752 * That enforces a right ordering and update sequence
753 * synchronization against the reader (task_gtime())
754 * that can thus safely catch up with a tickless delta.
755 */
756 write_seqcount_begin(&vtime->seqcount);
757 vtime_account_system(tsk, vtime);
758 tsk->flags |= PF_VCPU;
759 vtime->state = VTIME_GUEST;
760 write_seqcount_end(&vtime->seqcount);
761 }
762 EXPORT_SYMBOL_GPL(vtime_guest_enter);
763
vtime_guest_exit(struct task_struct * tsk)764 void vtime_guest_exit(struct task_struct *tsk)
765 {
766 struct vtime *vtime = &tsk->vtime;
767
768 write_seqcount_begin(&vtime->seqcount);
769 vtime_account_guest(tsk, vtime);
770 tsk->flags &= ~PF_VCPU;
771 vtime->state = VTIME_SYS;
772 write_seqcount_end(&vtime->seqcount);
773 }
774 EXPORT_SYMBOL_GPL(vtime_guest_exit);
775
vtime_account_idle(struct task_struct * tsk)776 void vtime_account_idle(struct task_struct *tsk)
777 {
778 account_idle_time(get_vtime_delta(&tsk->vtime));
779 }
780
vtime_task_switch_generic(struct task_struct * prev)781 void vtime_task_switch_generic(struct task_struct *prev)
782 {
783 struct vtime *vtime = &prev->vtime;
784
785 write_seqcount_begin(&vtime->seqcount);
786 if (vtime->state == VTIME_IDLE)
787 vtime_account_idle(prev);
788 else
789 __vtime_account_kernel(prev, vtime);
790 vtime->state = VTIME_INACTIVE;
791 vtime->cpu = -1;
792 write_seqcount_end(&vtime->seqcount);
793
794 vtime = ¤t->vtime;
795
796 write_seqcount_begin(&vtime->seqcount);
797 if (is_idle_task(current))
798 vtime->state = VTIME_IDLE;
799 else if (current->flags & PF_VCPU)
800 vtime->state = VTIME_GUEST;
801 else
802 vtime->state = VTIME_SYS;
803 vtime->starttime = sched_clock();
804 vtime->cpu = smp_processor_id();
805 write_seqcount_end(&vtime->seqcount);
806 }
807
vtime_init_idle(struct task_struct * t,int cpu)808 void vtime_init_idle(struct task_struct *t, int cpu)
809 {
810 struct vtime *vtime = &t->vtime;
811 unsigned long flags;
812
813 local_irq_save(flags);
814 write_seqcount_begin(&vtime->seqcount);
815 vtime->state = VTIME_IDLE;
816 vtime->starttime = sched_clock();
817 vtime->cpu = cpu;
818 write_seqcount_end(&vtime->seqcount);
819 local_irq_restore(flags);
820 }
821
task_gtime(struct task_struct * t)822 u64 task_gtime(struct task_struct *t)
823 {
824 struct vtime *vtime = &t->vtime;
825 unsigned int seq;
826 u64 gtime;
827
828 if (!vtime_accounting_enabled())
829 return t->gtime;
830
831 do {
832 seq = read_seqcount_begin(&vtime->seqcount);
833
834 gtime = t->gtime;
835 if (vtime->state == VTIME_GUEST)
836 gtime += vtime->gtime + vtime_delta(vtime);
837
838 } while (read_seqcount_retry(&vtime->seqcount, seq));
839
840 return gtime;
841 }
842
843 /*
844 * Fetch cputime raw values from fields of task_struct and
845 * add up the pending nohz execution time since the last
846 * cputime snapshot.
847 */
task_cputime(struct task_struct * t,u64 * utime,u64 * stime)848 void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
849 {
850 struct vtime *vtime = &t->vtime;
851 unsigned int seq;
852 u64 delta;
853
854 if (!vtime_accounting_enabled()) {
855 *utime = t->utime;
856 *stime = t->stime;
857 return;
858 }
859
860 do {
861 seq = read_seqcount_begin(&vtime->seqcount);
862
863 *utime = t->utime;
864 *stime = t->stime;
865
866 /* Task is sleeping or idle, nothing to add */
867 if (vtime->state < VTIME_SYS)
868 continue;
869
870 delta = vtime_delta(vtime);
871
872 /*
873 * Task runs either in user (including guest) or kernel space,
874 * add pending nohz time to the right place.
875 */
876 if (vtime->state == VTIME_SYS)
877 *stime += vtime->stime + delta;
878 else
879 *utime += vtime->utime + delta;
880 } while (read_seqcount_retry(&vtime->seqcount, seq));
881 }
882
vtime_state_fetch(struct vtime * vtime,int cpu)883 static int vtime_state_fetch(struct vtime *vtime, int cpu)
884 {
885 int state = READ_ONCE(vtime->state);
886
887 /*
888 * We raced against a context switch, fetch the
889 * kcpustat task again.
890 */
891 if (vtime->cpu != cpu && vtime->cpu != -1)
892 return -EAGAIN;
893
894 /*
895 * Two possible things here:
896 * 1) We are seeing the scheduling out task (prev) or any past one.
897 * 2) We are seeing the scheduling in task (next) but it hasn't
898 * passed though vtime_task_switch() yet so the pending
899 * cputime of the prev task may not be flushed yet.
900 *
901 * Case 1) is ok but 2) is not. So wait for a safe VTIME state.
902 */
903 if (state == VTIME_INACTIVE)
904 return -EAGAIN;
905
906 return state;
907 }
908
kcpustat_user_vtime(struct vtime * vtime)909 static u64 kcpustat_user_vtime(struct vtime *vtime)
910 {
911 if (vtime->state == VTIME_USER)
912 return vtime->utime + vtime_delta(vtime);
913 else if (vtime->state == VTIME_GUEST)
914 return vtime->gtime + vtime_delta(vtime);
915 return 0;
916 }
917
kcpustat_field_vtime(u64 * cpustat,struct task_struct * tsk,enum cpu_usage_stat usage,int cpu,u64 * val)918 static int kcpustat_field_vtime(u64 *cpustat,
919 struct task_struct *tsk,
920 enum cpu_usage_stat usage,
921 int cpu, u64 *val)
922 {
923 struct vtime *vtime = &tsk->vtime;
924 unsigned int seq;
925
926 do {
927 int state;
928
929 seq = read_seqcount_begin(&vtime->seqcount);
930
931 state = vtime_state_fetch(vtime, cpu);
932 if (state < 0)
933 return state;
934
935 *val = cpustat[usage];
936
937 /*
938 * Nice VS unnice cputime accounting may be inaccurate if
939 * the nice value has changed since the last vtime update.
940 * But proper fix would involve interrupting target on nice
941 * updates which is a no go on nohz_full (although the scheduler
942 * may still interrupt the target if rescheduling is needed...)
943 */
944 switch (usage) {
945 case CPUTIME_SYSTEM:
946 if (state == VTIME_SYS)
947 *val += vtime->stime + vtime_delta(vtime);
948 break;
949 case CPUTIME_USER:
950 if (task_nice(tsk) <= 0)
951 *val += kcpustat_user_vtime(vtime);
952 break;
953 case CPUTIME_NICE:
954 if (task_nice(tsk) > 0)
955 *val += kcpustat_user_vtime(vtime);
956 break;
957 case CPUTIME_GUEST:
958 if (state == VTIME_GUEST && task_nice(tsk) <= 0)
959 *val += vtime->gtime + vtime_delta(vtime);
960 break;
961 case CPUTIME_GUEST_NICE:
962 if (state == VTIME_GUEST && task_nice(tsk) > 0)
963 *val += vtime->gtime + vtime_delta(vtime);
964 break;
965 default:
966 break;
967 }
968 } while (read_seqcount_retry(&vtime->seqcount, seq));
969
970 return 0;
971 }
972
kcpustat_field(struct kernel_cpustat * kcpustat,enum cpu_usage_stat usage,int cpu)973 u64 kcpustat_field(struct kernel_cpustat *kcpustat,
974 enum cpu_usage_stat usage, int cpu)
975 {
976 u64 *cpustat = kcpustat->cpustat;
977 u64 val = cpustat[usage];
978 struct rq *rq;
979 int err;
980
981 if (!vtime_accounting_enabled_cpu(cpu))
982 return val;
983
984 rq = cpu_rq(cpu);
985
986 for (;;) {
987 struct task_struct *curr;
988
989 rcu_read_lock();
990 curr = rcu_dereference(rq->curr);
991 if (WARN_ON_ONCE(!curr)) {
992 rcu_read_unlock();
993 return cpustat[usage];
994 }
995
996 err = kcpustat_field_vtime(cpustat, curr, usage, cpu, &val);
997 rcu_read_unlock();
998
999 if (!err)
1000 return val;
1001
1002 cpu_relax();
1003 }
1004 }
1005 EXPORT_SYMBOL_GPL(kcpustat_field);
1006
kcpustat_cpu_fetch_vtime(struct kernel_cpustat * dst,const struct kernel_cpustat * src,struct task_struct * tsk,int cpu)1007 static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
1008 const struct kernel_cpustat *src,
1009 struct task_struct *tsk, int cpu)
1010 {
1011 struct vtime *vtime = &tsk->vtime;
1012 unsigned int seq;
1013
1014 do {
1015 u64 *cpustat;
1016 u64 delta;
1017 int state;
1018
1019 seq = read_seqcount_begin(&vtime->seqcount);
1020
1021 state = vtime_state_fetch(vtime, cpu);
1022 if (state < 0)
1023 return state;
1024
1025 *dst = *src;
1026 cpustat = dst->cpustat;
1027
1028 /* Task is sleeping, dead or idle, nothing to add */
1029 if (state < VTIME_SYS)
1030 continue;
1031
1032 delta = vtime_delta(vtime);
1033
1034 /*
1035 * Task runs either in user (including guest) or kernel space,
1036 * add pending nohz time to the right place.
1037 */
1038 if (state == VTIME_SYS) {
1039 cpustat[CPUTIME_SYSTEM] += vtime->stime + delta;
1040 } else if (state == VTIME_USER) {
1041 if (task_nice(tsk) > 0)
1042 cpustat[CPUTIME_NICE] += vtime->utime + delta;
1043 else
1044 cpustat[CPUTIME_USER] += vtime->utime + delta;
1045 } else {
1046 WARN_ON_ONCE(state != VTIME_GUEST);
1047 if (task_nice(tsk) > 0) {
1048 cpustat[CPUTIME_GUEST_NICE] += vtime->gtime + delta;
1049 cpustat[CPUTIME_NICE] += vtime->gtime + delta;
1050 } else {
1051 cpustat[CPUTIME_GUEST] += vtime->gtime + delta;
1052 cpustat[CPUTIME_USER] += vtime->gtime + delta;
1053 }
1054 }
1055 } while (read_seqcount_retry(&vtime->seqcount, seq));
1056
1057 return 0;
1058 }
1059
kcpustat_cpu_fetch(struct kernel_cpustat * dst,int cpu)1060 void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
1061 {
1062 const struct kernel_cpustat *src = &kcpustat_cpu(cpu);
1063 struct rq *rq;
1064 int err;
1065
1066 if (!vtime_accounting_enabled_cpu(cpu)) {
1067 *dst = *src;
1068 return;
1069 }
1070
1071 rq = cpu_rq(cpu);
1072
1073 for (;;) {
1074 struct task_struct *curr;
1075
1076 rcu_read_lock();
1077 curr = rcu_dereference(rq->curr);
1078 if (WARN_ON_ONCE(!curr)) {
1079 rcu_read_unlock();
1080 *dst = *src;
1081 return;
1082 }
1083
1084 err = kcpustat_cpu_fetch_vtime(dst, src, curr, cpu);
1085 rcu_read_unlock();
1086
1087 if (!err)
1088 return;
1089
1090 cpu_relax();
1091 }
1092 }
1093 EXPORT_SYMBOL_GPL(kcpustat_cpu_fetch);
1094
1095 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
1096