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