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