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