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