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