blob: db82ae12cf0196c29eb8813507d6d394e3e06006 [file] [log] [blame]
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001#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>
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +02006#include <linux/context_tracking.h>
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02007#include "sched.h"
Stefano Stabellini1fe7c4e2015-11-10 12:36:46 +00008#ifdef CONFIG_PARAVIRT
9#include <asm/paravirt.h>
10#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020011
12
13#ifdef CONFIG_IRQ_TIME_ACCOUNTING
14
15/*
16 * There are no locks covering percpu hardirq/softirq time.
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +020017 * They are only modified in vtime_account, on corresponding CPU
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020018 * 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
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +020021 * race with irq/vtime_account on this CPU. We would either get old
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020022 * 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 */
26DEFINE_PER_CPU(u64, cpu_hardirq_time);
27DEFINE_PER_CPU(u64, cpu_softirq_time);
28
29static DEFINE_PER_CPU(u64, irq_start_time);
30static int sched_clock_irqtime;
31
32void enable_sched_clock_irqtime(void)
33{
34 sched_clock_irqtime = 1;
35}
36
37void disable_sched_clock_irqtime(void)
38{
39 sched_clock_irqtime = 0;
40}
41
42#ifndef CONFIG_64BIT
43DEFINE_PER_CPU(seqcount_t, irq_time_seq);
44#endif /* CONFIG_64BIT */
45
46/*
47 * Called before incrementing preempt_count on {soft,}irq_enter
48 * and before decrementing preempt_count on {soft,}irq_exit.
49 */
Frederic Weisbecker3e1df4f52012-10-06 05:23:22 +020050void irqtime_account_irq(struct task_struct *curr)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020051{
52 unsigned long flags;
53 s64 delta;
54 int cpu;
55
56 if (!sched_clock_irqtime)
57 return;
58
59 local_irq_save(flags);
60
61 cpu = smp_processor_id();
62 delta = sched_clock_cpu(cpu) - __this_cpu_read(irq_start_time);
63 __this_cpu_add(irq_start_time, delta);
64
65 irq_time_write_begin();
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 __this_cpu_add(cpu_hardirq_time, delta);
74 else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
75 __this_cpu_add(cpu_softirq_time, delta);
76
77 irq_time_write_end();
78 local_irq_restore(flags);
79}
Frederic Weisbecker3e1df4f52012-10-06 05:23:22 +020080EXPORT_SYMBOL_GPL(irqtime_account_irq);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020081
Rik van Riel57430212016-07-13 16:50:01 +020082static cputime_t irqtime_account_hi_update(cputime_t maxtime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020083{
84 u64 *cpustat = kcpustat_this_cpu->cpustat;
85 unsigned long flags;
Rik van Riel57430212016-07-13 16:50:01 +020086 cputime_t irq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020087
88 local_irq_save(flags);
Rik van Riel57430212016-07-13 16:50:01 +020089 irq_cputime = nsecs_to_cputime64(this_cpu_read(cpu_hardirq_time)) -
90 cpustat[CPUTIME_IRQ];
91 irq_cputime = min(irq_cputime, maxtime);
92 cpustat[CPUTIME_IRQ] += irq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020093 local_irq_restore(flags);
Rik van Riel57430212016-07-13 16:50:01 +020094 return irq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020095}
96
Rik van Riel57430212016-07-13 16:50:01 +020097static cputime_t irqtime_account_si_update(cputime_t maxtime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020098{
99 u64 *cpustat = kcpustat_this_cpu->cpustat;
100 unsigned long flags;
Rik van Riel57430212016-07-13 16:50:01 +0200101 cputime_t softirq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200102
103 local_irq_save(flags);
Rik van Riel57430212016-07-13 16:50:01 +0200104 softirq_cputime = nsecs_to_cputime64(this_cpu_read(cpu_softirq_time)) -
105 cpustat[CPUTIME_SOFTIRQ];
106 softirq_cputime = min(softirq_cputime, maxtime);
107 cpustat[CPUTIME_SOFTIRQ] += softirq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200108 local_irq_restore(flags);
Rik van Riel57430212016-07-13 16:50:01 +0200109 return softirq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200110}
111
112#else /* CONFIG_IRQ_TIME_ACCOUNTING */
113
114#define sched_clock_irqtime (0)
115
Rik van Riel57430212016-07-13 16:50:01 +0200116static cputime_t irqtime_account_hi_update(cputime_t dummy)
117{
118 return 0;
119}
120
121static cputime_t irqtime_account_si_update(cputime_t dummy)
122{
123 return 0;
124}
125
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200126#endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
127
128static inline void task_group_account_field(struct task_struct *p, int index,
129 u64 tmp)
130{
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200131 /*
132 * Since all updates are sure to touch the root cgroup, we
133 * get ourselves ahead and touch it first. If the root cgroup
134 * is the only cgroup, then nothing else should be necessary.
135 *
136 */
Christoph Lametera4f61cc2013-08-07 15:38:24 +0000137 __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200138
Li Zefan1966aaf2013-03-29 14:37:06 +0800139 cpuacct_account_field(p, index, tmp);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200140}
141
142/*
143 * Account user cpu time to a process.
144 * @p: the process that the cpu time gets accounted to
145 * @cputime: the cpu time spent in user space since the last update
146 * @cputime_scaled: cputime scaled by cpu frequency
147 */
148void account_user_time(struct task_struct *p, cputime_t cputime,
149 cputime_t cputime_scaled)
150{
151 int index;
152
153 /* Add user time to process. */
154 p->utime += cputime;
155 p->utimescaled += cputime_scaled;
156 account_group_user_time(p, cputime);
157
Dongsheng Yangd0ea0262014-01-27 22:00:45 -0500158 index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200159
160 /* Add user time to cpustat. */
161 task_group_account_field(p, index, (__force u64) cputime);
162
163 /* Account for user time used */
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100164 acct_account_cputime(p);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200165}
166
167/*
168 * Account guest cpu time to a process.
169 * @p: the process that the cpu time gets accounted to
170 * @cputime: the cpu time spent in virtual machine since the last update
171 * @cputime_scaled: cputime scaled by cpu frequency
172 */
173static void account_guest_time(struct task_struct *p, cputime_t cputime,
174 cputime_t cputime_scaled)
175{
176 u64 *cpustat = kcpustat_this_cpu->cpustat;
177
178 /* Add guest time to process. */
179 p->utime += cputime;
180 p->utimescaled += cputime_scaled;
181 account_group_user_time(p, cputime);
182 p->gtime += cputime;
183
184 /* Add guest time to cpustat. */
Dongsheng Yangd0ea0262014-01-27 22:00:45 -0500185 if (task_nice(p) > 0) {
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200186 cpustat[CPUTIME_NICE] += (__force u64) cputime;
187 cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
188 } else {
189 cpustat[CPUTIME_USER] += (__force u64) cputime;
190 cpustat[CPUTIME_GUEST] += (__force u64) cputime;
191 }
192}
193
194/*
195 * Account system cpu time to a process and desired cpustat field
196 * @p: the process that the cpu time gets accounted to
197 * @cputime: the cpu time spent in kernel space since the last update
198 * @cputime_scaled: cputime scaled by cpu frequency
199 * @target_cputime64: pointer to cpustat field that has to be updated
200 */
201static inline
202void __account_system_time(struct task_struct *p, cputime_t cputime,
203 cputime_t cputime_scaled, int index)
204{
205 /* Add system time to process. */
206 p->stime += cputime;
207 p->stimescaled += cputime_scaled;
208 account_group_system_time(p, cputime);
209
210 /* Add system time to cpustat. */
211 task_group_account_field(p, index, (__force u64) cputime);
212
213 /* Account for system time used */
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100214 acct_account_cputime(p);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200215}
216
217/*
218 * Account system cpu time to a process.
219 * @p: the process that the cpu time gets accounted to
220 * @hardirq_offset: the offset to subtract from hardirq_count()
221 * @cputime: the cpu time spent in kernel space since the last update
222 * @cputime_scaled: cputime scaled by cpu frequency
223 */
224void account_system_time(struct task_struct *p, int hardirq_offset,
225 cputime_t cputime, cputime_t cputime_scaled)
226{
227 int index;
228
229 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
230 account_guest_time(p, cputime, cputime_scaled);
231 return;
232 }
233
234 if (hardirq_count() - hardirq_offset)
235 index = CPUTIME_IRQ;
236 else if (in_serving_softirq())
237 index = CPUTIME_SOFTIRQ;
238 else
239 index = CPUTIME_SYSTEM;
240
241 __account_system_time(p, cputime, cputime_scaled, index);
242}
243
244/*
245 * Account for involuntary wait time.
246 * @cputime: the cpu time spent in involuntary wait
247 */
248void account_steal_time(cputime_t cputime)
249{
250 u64 *cpustat = kcpustat_this_cpu->cpustat;
251
252 cpustat[CPUTIME_STEAL] += (__force u64) cputime;
253}
254
255/*
256 * Account for idle time.
257 * @cputime: the cpu time spent in idle wait
258 */
259void account_idle_time(cputime_t cputime)
260{
261 u64 *cpustat = kcpustat_this_cpu->cpustat;
262 struct rq *rq = this_rq();
263
264 if (atomic_read(&rq->nr_iowait) > 0)
265 cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
266 else
267 cpustat[CPUTIME_IDLE] += (__force u64) cputime;
268}
269
Rik van Riel57430212016-07-13 16:50:01 +0200270static __always_inline cputime_t steal_account_process_time(cputime_t maxtime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200271{
272#ifdef CONFIG_PARAVIRT
273 if (static_key_false(&paravirt_steal_enabled)) {
Rik van Riel57430212016-07-13 16:50:01 +0200274 cputime_t steal_cputime;
Frederic Weisbeckerdee08a72014-03-05 17:02:22 +0100275 u64 steal;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200276
277 steal = paravirt_steal_clock(smp_processor_id());
278 steal -= this_rq()->prev_steal_time;
279
Rik van Riel57430212016-07-13 16:50:01 +0200280 steal_cputime = min(nsecs_to_cputime(steal), maxtime);
281 account_steal_time(steal_cputime);
282 this_rq()->prev_steal_time += cputime_to_nsecs(steal_cputime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200283
Rik van Riel57430212016-07-13 16:50:01 +0200284 return steal_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200285 }
286#endif
Wanpeng Li807e5b82016-06-13 18:32:46 +0800287 return 0;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200288}
289
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100290/*
Rik van Riel57430212016-07-13 16:50:01 +0200291 * Account how much elapsed time was spent in steal, irq, or softirq time.
292 */
293static inline cputime_t account_other_time(cputime_t max)
294{
295 cputime_t accounted;
296
297 accounted = steal_account_process_time(max);
298
299 if (accounted < max)
300 accounted += irqtime_account_hi_update(max - accounted);
301
302 if (accounted < max)
303 accounted += irqtime_account_si_update(max - accounted);
304
305 return accounted;
306}
307
308/*
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100309 * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
310 * tasks (sum on group iteration) belonging to @tsk's group.
311 */
312void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
313{
314 struct signal_struct *sig = tsk->signal;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100315 cputime_t utime, stime;
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100316 struct task_struct *t;
Rik van Riele78c3492014-08-16 13:40:10 -0400317 unsigned int seq, nextseq;
Rik van Riel9c368b52014-09-12 09:12:15 -0400318 unsigned long flags;
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100319
320 rcu_read_lock();
Rik van Riele78c3492014-08-16 13:40:10 -0400321 /* Attempt a lockless read on the first round. */
322 nextseq = 0;
323 do {
324 seq = nextseq;
Rik van Riel9c368b52014-09-12 09:12:15 -0400325 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
Rik van Riele78c3492014-08-16 13:40:10 -0400326 times->utime = sig->utime;
327 times->stime = sig->stime;
328 times->sum_exec_runtime = sig->sum_sched_runtime;
329
330 for_each_thread(tsk, t) {
331 task_cputime(t, &utime, &stime);
332 times->utime += utime;
333 times->stime += stime;
334 times->sum_exec_runtime += task_sched_runtime(t);
335 }
336 /* If lockless access failed, take the lock. */
337 nextseq = 1;
338 } while (need_seqretry(&sig->stats_lock, seq));
Rik van Riel9c368b52014-09-12 09:12:15 -0400339 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100340 rcu_read_unlock();
341}
342
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200343#ifdef CONFIG_IRQ_TIME_ACCOUNTING
344/*
345 * Account a tick to a process and cpustat
346 * @p: the process that the cpu time gets accounted to
347 * @user_tick: is the tick from userspace
348 * @rq: the pointer to rq
349 *
350 * Tick demultiplexing follows the order
351 * - pending hardirq update
352 * - pending softirq update
353 * - user_time
354 * - idle_time
355 * - system time
356 * - check for guest_time
357 * - else account as system_time
358 *
359 * Check for hardirq is done both for system and user time as there is
360 * no timer going off while we are on hardirq and hence we may never get an
361 * opportunity to update it solely in system time.
362 * p->stime and friends are only updated on system time and not on irq
363 * softirq as those do not count in task exec_runtime any more.
364 */
365static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
Thomas Gleixner2d513862014-05-02 23:26:24 +0200366 struct rq *rq, int ticks)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200367{
Rik van Riel57430212016-07-13 16:50:01 +0200368 u64 cputime = (__force u64) cputime_one_jiffy * ticks;
369 cputime_t scaled, other;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200370
Rik van Riel57430212016-07-13 16:50:01 +0200371 /*
372 * When returning from idle, many ticks can get accounted at
373 * once, including some ticks of steal, irq, and softirq time.
374 * Subtract those ticks from the amount of time accounted to
375 * idle, or potentially user or system time. Due to rounding,
376 * other time can exceed ticks occasionally.
377 */
378 other = account_other_time(cputime);
379 if (other >= cputime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200380 return;
Rik van Riel57430212016-07-13 16:50:01 +0200381 cputime -= other;
382 scaled = cputime_to_scaled(cputime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200383
Rik van Riel57430212016-07-13 16:50:01 +0200384 if (this_cpu_ksoftirqd() == p) {
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200385 /*
386 * ksoftirqd time do not get accounted in cpu_softirq_time.
387 * So, we have to handle it separately here.
388 * Also, p->stime needs to be updated for ksoftirqd.
389 */
Thomas Gleixner2d513862014-05-02 23:26:24 +0200390 __account_system_time(p, cputime, scaled, CPUTIME_SOFTIRQ);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200391 } else if (user_tick) {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200392 account_user_time(p, cputime, scaled);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200393 } else if (p == rq->idle) {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200394 account_idle_time(cputime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200395 } else if (p->flags & PF_VCPU) { /* System time or guest time */
Thomas Gleixner2d513862014-05-02 23:26:24 +0200396 account_guest_time(p, cputime, scaled);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200397 } else {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200398 __account_system_time(p, cputime, scaled, CPUTIME_SYSTEM);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200399 }
400}
401
402static void irqtime_account_idle_ticks(int ticks)
403{
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200404 struct rq *rq = this_rq();
405
Thomas Gleixner2d513862014-05-02 23:26:24 +0200406 irqtime_account_process_tick(current, 0, rq, ticks);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200407}
408#else /* CONFIG_IRQ_TIME_ACCOUNTING */
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +0200409static inline void irqtime_account_idle_ticks(int ticks) {}
410static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
Thomas Gleixner2d513862014-05-02 23:26:24 +0200411 struct rq *rq, int nr_ticks) {}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200412#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
413
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200414/*
415 * Use precise platform statistics if available:
416 */
417#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200418
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100419#ifndef __ARCH_HAS_VTIME_TASK_SWITCH
Frederic Weisbeckerb0493402013-07-12 03:10:15 +0200420void vtime_common_task_switch(struct task_struct *prev)
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100421{
422 if (is_idle_task(prev))
423 vtime_account_idle(prev);
424 else
425 vtime_account_system(prev);
426
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200427#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100428 vtime_account_user(prev);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200429#endif
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100430 arch_vtime_task_switch(prev);
431}
432#endif
Frederic Weisbecker11113332012-10-24 18:05:51 +0200433
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200434/*
435 * Archs that account the whole time spent in the idle task
436 * (outside irq) as idle time can rely on this and just implement
Frederic Weisbeckerfd25b4c2012-11-13 18:21:22 +0100437 * vtime_account_system() and vtime_account_idle(). Archs that
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200438 * have other meaning of the idle time (s390 only includes the
439 * time spent by the CPU when it's in low power mode) must override
440 * vtime_account().
441 */
442#ifndef __ARCH_HAS_VTIME_ACCOUNT
Frederic Weisbeckerb0493402013-07-12 03:10:15 +0200443void vtime_common_account_irq_enter(struct task_struct *tsk)
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200444{
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200445 if (!in_interrupt()) {
446 /*
447 * If we interrupted user, context_tracking_in_user()
448 * is 1 because the context tracking don't hook
449 * on irq entry/exit. This way we know if
450 * we need to flush user time on kernel entry.
451 */
452 if (context_tracking_in_user()) {
453 vtime_account_user(tsk);
454 return;
455 }
456
457 if (is_idle_task(tsk)) {
458 vtime_account_idle(tsk);
459 return;
460 }
461 }
462 vtime_account_system(tsk);
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200463}
Frederic Weisbeckerb0493402013-07-12 03:10:15 +0200464EXPORT_SYMBOL_GPL(vtime_common_account_irq_enter);
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200465#endif /* __ARCH_HAS_VTIME_ACCOUNT */
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100466#endif /* CONFIG_VIRT_CPU_ACCOUNTING */
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200467
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200468
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100469#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
470void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200471{
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100472 *ut = p->utime;
473 *st = p->stime;
474}
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300475EXPORT_SYMBOL_GPL(task_cputime_adjusted);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200476
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100477void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
478{
479 struct task_cputime cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200480
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100481 thread_group_cputime(p, &cputime);
482
483 *ut = cputime.utime;
484 *st = cputime.stime;
485}
486#else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
487/*
488 * Account a single tick of cpu time.
489 * @p: the process that the cpu time gets accounted to
490 * @user_tick: indicates if the tick is a user or a system tick
491 */
492void account_process_tick(struct task_struct *p, int user_tick)
493{
Rik van Riel57430212016-07-13 16:50:01 +0200494 cputime_t cputime, scaled, steal;
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100495 struct rq *rq = this_rq();
496
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +0100497 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100498 return;
499
500 if (sched_clock_irqtime) {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200501 irqtime_account_process_tick(p, user_tick, rq, 1);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100502 return;
503 }
504
Rik van Riel57430212016-07-13 16:50:01 +0200505 cputime = cputime_one_jiffy;
506 steal = steal_account_process_time(cputime);
507
508 if (steal >= cputime)
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100509 return;
510
Rik van Riel57430212016-07-13 16:50:01 +0200511 cputime -= steal;
512 scaled = cputime_to_scaled(cputime);
513
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100514 if (user_tick)
Rik van Riel57430212016-07-13 16:50:01 +0200515 account_user_time(p, cputime, scaled);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100516 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Rik van Riel57430212016-07-13 16:50:01 +0200517 account_system_time(p, HARDIRQ_OFFSET, cputime, scaled);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200518 else
Rik van Riel57430212016-07-13 16:50:01 +0200519 account_idle_time(cputime);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100520}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200521
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100522/*
523 * Account multiple ticks of steal time.
524 * @p: the process from which the cpu time has been stolen
525 * @ticks: number of stolen ticks
526 */
527void account_steal_ticks(unsigned long ticks)
528{
529 account_steal_time(jiffies_to_cputime(ticks));
530}
531
532/*
533 * Account multiple ticks of idle time.
534 * @ticks: number of stolen ticks
535 */
536void account_idle_ticks(unsigned long ticks)
537{
538
539 if (sched_clock_irqtime) {
540 irqtime_account_idle_ticks(ticks);
541 return;
542 }
543
544 account_idle_time(jiffies_to_cputime(ticks));
545}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200546
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100547/*
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200548 * Perform (stime * rtime) / total, but avoid multiplication overflow by
549 * loosing precision when the numbers are big.
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100550 */
551static cputime_t scale_stime(u64 stime, u64 rtime, u64 total)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200552{
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200553 u64 scaled;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200554
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200555 for (;;) {
556 /* Make sure "rtime" is the bigger of stime/rtime */
Stanislaw Gruszka84f9f3a2013-05-02 15:34:33 +0200557 if (stime > rtime)
558 swap(rtime, stime);
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200559
560 /* Make sure 'total' fits in 32 bits */
561 if (total >> 32)
562 goto drop_precision;
563
564 /* Does rtime (and thus stime) fit in 32 bits? */
565 if (!(rtime >> 32))
566 break;
567
568 /* Can we just balance rtime/stime rather than dropping bits? */
569 if (stime >> 31)
570 goto drop_precision;
571
572 /* We can grow stime and shrink rtime and try to make them both fit */
573 stime <<= 1;
574 rtime >>= 1;
575 continue;
576
577drop_precision:
578 /* We drop from rtime, it has more bits than stime */
579 rtime >>= 1;
580 total >>= 1;
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100581 }
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200582
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200583 /*
584 * Make sure gcc understands that this is a 32x32->64 multiply,
585 * followed by a 64/32->64 divide.
586 */
587 scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100588 return (__force cputime_t) scaled;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200589}
590
Frederic Weisbeckerfa092052012-11-28 17:00:57 +0100591/*
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200592 * Adjust tick based cputime random precision against scheduler runtime
593 * accounting.
Rik van Riel347abad2014-09-30 15:59:47 -0400594 *
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200595 * Tick based cputime accounting depend on random scheduling timeslices of a
596 * task to be interrupted or not by the timer. Depending on these
597 * circumstances, the number of these interrupts may be over or
598 * under-optimistic, matching the real user and system cputime with a variable
599 * precision.
600 *
601 * Fix this by scaling these tick based values against the total runtime
602 * accounted by the CFS scheduler.
603 *
604 * This code provides the following guarantees:
605 *
606 * stime + utime == rtime
607 * stime_i+1 >= stime_i, utime_i+1 >= utime_i
608 *
609 * Assuming that rtime_i+1 >= rtime_i.
Frederic Weisbeckerfa092052012-11-28 17:00:57 +0100610 */
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100611static void cputime_adjust(struct task_cputime *curr,
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200612 struct prev_cputime *prev,
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100613 cputime_t *ut, cputime_t *st)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200614{
Stanislaw Gruszka5a8e01f2013-09-04 15:16:03 +0200615 cputime_t rtime, stime, utime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200616 unsigned long flags;
Frederic Weisbeckerfa092052012-11-28 17:00:57 +0100617
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200618 /* Serialize concurrent callers such that we can honour our guarantees */
619 raw_spin_lock_irqsave(&prev->lock, flags);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100620 rtime = nsecs_to_cputime(curr->sum_exec_runtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200621
Stanislaw Gruszka772c8082013-04-30 11:35:05 +0200622 /*
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200623 * This is possible under two circumstances:
624 * - rtime isn't monotonic after all (a bug);
625 * - we got reordered by the lock.
626 *
627 * In both cases this acts as a filter such that the rest of the code
628 * can assume it is monotonic regardless of anything else.
Stanislaw Gruszka772c8082013-04-30 11:35:05 +0200629 */
630 if (prev->stime + prev->utime >= rtime)
631 goto out;
632
Stanislaw Gruszka5a8e01f2013-09-04 15:16:03 +0200633 stime = curr->stime;
634 utime = curr->utime;
635
636 if (utime == 0) {
637 stime = rtime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200638 goto update;
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100639 }
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200640
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200641 if (stime == 0) {
642 utime = rtime;
643 goto update;
644 }
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200645
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200646 stime = scale_stime((__force u64)stime, (__force u64)rtime,
647 (__force u64)(stime + utime));
648
649 /*
650 * Make sure stime doesn't go backwards; this preserves monotonicity
651 * for utime because rtime is monotonic.
652 *
653 * utime_i+1 = rtime_i+1 - stime_i
654 * = rtime_i+1 - (rtime_i - utime_i)
655 * = (rtime_i+1 - rtime_i) + utime_i
656 * >= utime_i
657 */
658 if (stime < prev->stime)
659 stime = prev->stime;
660 utime = rtime - stime;
661
662 /*
663 * Make sure utime doesn't go backwards; this still preserves
664 * monotonicity for stime, analogous argument to above.
665 */
666 if (utime < prev->utime) {
667 utime = prev->utime;
668 stime = rtime - utime;
669 }
670
671update:
672 prev->stime = stime;
673 prev->utime = utime;
Stanislaw Gruszka772c8082013-04-30 11:35:05 +0200674out:
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100675 *ut = prev->utime;
676 *st = prev->stime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200677 raw_spin_unlock_irqrestore(&prev->lock, flags);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100678}
679
680void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
681{
682 struct task_cputime cputime = {
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100683 .sum_exec_runtime = p->se.sum_exec_runtime,
684 };
685
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100686 task_cputime(p, &cputime.utime, &cputime.stime);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100687 cputime_adjust(&cputime, &p->prev_cputime, ut, st);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200688}
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300689EXPORT_SYMBOL_GPL(task_cputime_adjusted);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200690
Frederic Weisbeckere80d0a1a2012-11-21 16:26:44 +0100691void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200692{
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200693 struct task_cputime cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200694
695 thread_group_cputime(p, &cputime);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100696 cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200697}
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100698#endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200699
700#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Rik van Rielff9a9b42016-02-10 20:08:27 -0500701static cputime_t vtime_delta(struct task_struct *tsk)
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200702{
Rik van Rielff9a9b42016-02-10 20:08:27 -0500703 unsigned long now = READ_ONCE(jiffies);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200704
Rik van Rielff9a9b42016-02-10 20:08:27 -0500705 if (time_before(now, (unsigned long)tsk->vtime_snap))
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100706 return 0;
707
Rik van Rielff9a9b42016-02-10 20:08:27 -0500708 return jiffies_to_cputime(now - tsk->vtime_snap);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100709}
710
711static cputime_t get_vtime_delta(struct task_struct *tsk)
712{
Rik van Rielff9a9b42016-02-10 20:08:27 -0500713 unsigned long now = READ_ONCE(jiffies);
Rik van Riel57430212016-07-13 16:50:01 +0200714 cputime_t delta, steal;
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100715
Rik van Riel57430212016-07-13 16:50:01 +0200716 delta = jiffies_to_cputime(now - tsk->vtime_snap);
717 steal = steal_account_process_time(delta);
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +0100718 WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500719 tsk->vtime_snap = now;
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200720
Rik van Riel57430212016-07-13 16:50:01 +0200721 return delta - steal;
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200722}
723
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100724static void __vtime_account_system(struct task_struct *tsk)
725{
726 cputime_t delta_cpu = get_vtime_delta(tsk);
727
728 account_system_time(tsk, irq_count(), delta_cpu, cputime_to_scaled(delta_cpu));
729}
730
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200731void vtime_account_system(struct task_struct *tsk)
732{
Rik van Rielff9a9b42016-02-10 20:08:27 -0500733 if (!vtime_delta(tsk))
734 return;
735
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100736 write_seqcount_begin(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100737 __vtime_account_system(tsk);
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100738 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100739}
740
Frederic Weisbeckerb0493402013-07-12 03:10:15 +0200741void vtime_gen_account_irq_exit(struct task_struct *tsk)
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100742{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100743 write_seqcount_begin(&tsk->vtime_seqcount);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500744 if (vtime_delta(tsk))
745 __vtime_account_system(tsk);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100746 if (context_tracking_in_user())
747 tsk->vtime_snap_whence = VTIME_USER;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100748 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200749}
750
751void vtime_account_user(struct task_struct *tsk)
752{
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +0200753 cputime_t delta_cpu;
754
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100755 write_seqcount_begin(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100756 tsk->vtime_snap_whence = VTIME_SYS;
Rik van Rielff9a9b42016-02-10 20:08:27 -0500757 if (vtime_delta(tsk)) {
758 delta_cpu = get_vtime_delta(tsk);
759 account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
760 }
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100761 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100762}
763
764void vtime_user_enter(struct task_struct *tsk)
765{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100766 write_seqcount_begin(&tsk->vtime_seqcount);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500767 if (vtime_delta(tsk))
768 __vtime_account_system(tsk);
Frederic Weisbeckeraf2350b2013-07-15 16:35:55 +0200769 tsk->vtime_snap_whence = VTIME_USER;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100770 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100771}
772
773void vtime_guest_enter(struct task_struct *tsk)
774{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200775 /*
776 * The flags must be updated under the lock with
777 * the vtime_snap flush and update.
778 * That enforces a right ordering and update sequence
779 * synchronization against the reader (task_gtime())
780 * that can thus safely catch up with a tickless delta.
781 */
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100782 write_seqcount_begin(&tsk->vtime_seqcount);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500783 if (vtime_delta(tsk))
784 __vtime_account_system(tsk);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100785 current->flags |= PF_VCPU;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100786 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100787}
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200788EXPORT_SYMBOL_GPL(vtime_guest_enter);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100789
790void vtime_guest_exit(struct task_struct *tsk)
791{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100792 write_seqcount_begin(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100793 __vtime_account_system(tsk);
794 current->flags &= ~PF_VCPU;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100795 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200796}
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200797EXPORT_SYMBOL_GPL(vtime_guest_exit);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200798
799void vtime_account_idle(struct task_struct *tsk)
800{
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100801 cputime_t delta_cpu = get_vtime_delta(tsk);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200802
803 account_idle_time(delta_cpu);
804}
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +0200805
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100806void arch_vtime_task_switch(struct task_struct *prev)
807{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100808 write_seqcount_begin(&prev->vtime_seqcount);
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +0100809 prev->vtime_snap_whence = VTIME_INACTIVE;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100810 write_seqcount_end(&prev->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100811
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100812 write_seqcount_begin(&current->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100813 current->vtime_snap_whence = VTIME_SYS;
Rik van Rielff9a9b42016-02-10 20:08:27 -0500814 current->vtime_snap = jiffies;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100815 write_seqcount_end(&current->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100816}
817
Frederic Weisbecker45eacc62013-05-15 22:16:32 +0200818void vtime_init_idle(struct task_struct *t, int cpu)
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100819{
820 unsigned long flags;
821
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100822 local_irq_save(flags);
823 write_seqcount_begin(&t->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100824 t->vtime_snap_whence = VTIME_SYS;
Rik van Rielff9a9b42016-02-10 20:08:27 -0500825 t->vtime_snap = jiffies;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100826 write_seqcount_end(&t->vtime_seqcount);
827 local_irq_restore(flags);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100828}
829
830cputime_t task_gtime(struct task_struct *t)
831{
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100832 unsigned int seq;
833 cputime_t gtime;
834
Frederic Weisbeckere5925392015-11-19 16:47:33 +0100835 if (!vtime_accounting_enabled())
Hiroshi Shimamoto25411172015-11-19 16:47:28 +0100836 return t->gtime;
837
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100838 do {
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100839 seq = read_seqcount_begin(&t->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100840
841 gtime = t->gtime;
Frederic Weisbeckercab245d2015-11-19 16:47:31 +0100842 if (t->vtime_snap_whence == VTIME_SYS && t->flags & PF_VCPU)
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100843 gtime += vtime_delta(t);
844
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100845 } while (read_seqcount_retry(&t->vtime_seqcount, seq));
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100846
847 return gtime;
848}
849
850/*
851 * Fetch cputime raw values from fields of task_struct and
852 * add up the pending nohz execution time since the last
853 * cputime snapshot.
854 */
855static void
856fetch_task_cputime(struct task_struct *t,
857 cputime_t *u_dst, cputime_t *s_dst,
858 cputime_t *u_src, cputime_t *s_src,
859 cputime_t *udelta, cputime_t *sdelta)
860{
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100861 unsigned int seq;
862 unsigned long long delta;
863
864 do {
865 *udelta = 0;
866 *sdelta = 0;
867
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100868 seq = read_seqcount_begin(&t->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100869
870 if (u_dst)
871 *u_dst = *u_src;
872 if (s_dst)
873 *s_dst = *s_src;
874
875 /* Task is sleeping, nothing to add */
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +0100876 if (t->vtime_snap_whence == VTIME_INACTIVE ||
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100877 is_idle_task(t))
878 continue;
879
880 delta = vtime_delta(t);
881
882 /*
883 * Task runs either in user or kernel space, add pending nohz time to
884 * the right place.
885 */
886 if (t->vtime_snap_whence == VTIME_USER || t->flags & PF_VCPU) {
887 *udelta = delta;
888 } else {
889 if (t->vtime_snap_whence == VTIME_SYS)
890 *sdelta = delta;
891 }
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100892 } while (read_seqcount_retry(&t->vtime_seqcount, seq));
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100893}
894
895
896void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime)
897{
898 cputime_t udelta, sdelta;
899
Frederic Weisbeckere5925392015-11-19 16:47:33 +0100900 if (!vtime_accounting_enabled()) {
Hiroshi Shimamoto7877a0b2015-11-19 16:47:29 +0100901 if (utime)
902 *utime = t->utime;
903 if (stime)
904 *stime = t->stime;
905 return;
906 }
907
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100908 fetch_task_cputime(t, utime, stime, &t->utime,
909 &t->stime, &udelta, &sdelta);
910 if (utime)
911 *utime += udelta;
912 if (stime)
913 *stime += sdelta;
914}
915
916void task_cputime_scaled(struct task_struct *t,
917 cputime_t *utimescaled, cputime_t *stimescaled)
918{
919 cputime_t udelta, sdelta;
920
Frederic Weisbeckere5925392015-11-19 16:47:33 +0100921 if (!vtime_accounting_enabled()) {
Hiroshi Shimamoto7877a0b2015-11-19 16:47:29 +0100922 if (utimescaled)
923 *utimescaled = t->utimescaled;
924 if (stimescaled)
925 *stimescaled = t->stimescaled;
926 return;
927 }
928
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100929 fetch_task_cputime(t, utimescaled, stimescaled,
930 &t->utimescaled, &t->stimescaled, &udelta, &sdelta);
931 if (utimescaled)
932 *utimescaled += cputime_to_scaled(udelta);
933 if (stimescaled)
934 *stimescaled += cputime_to_scaled(sdelta);
935}
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200936#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */