blob: 0d8cc065de20baca0e62ddafd921cc4db380ce7a [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
Srinath Sridharan3a73c962016-07-22 13:21:15 +010011#include "walt.h"
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020012
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 */
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020026DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020027
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020028static 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
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020040/*
41 * Called before incrementing preempt_count on {soft,}irq_enter
42 * and before decrementing preempt_count on {soft,}irq_exit.
43 */
Frederic Weisbecker3e1df4f52012-10-06 05:23:22 +020044void irqtime_account_irq(struct task_struct *curr)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020045{
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020046 struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020047 s64 delta;
48 int cpu;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070049 u64 wallclock;
50 bool account = true;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020051
52 if (!sched_clock_irqtime)
53 return;
54
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020055 cpu = smp_processor_id();
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070056 wallclock = sched_clock_cpu(cpu);
57 delta = wallclock - irqtime->irq_start_time;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020058 irqtime->irq_start_time += delta;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020059
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020060 u64_stats_update_begin(&irqtime->sync);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020061 /*
62 * We do not account for softirq time from ksoftirqd here.
63 * We want to continue accounting softirq time to ksoftirqd thread
64 * in that case, so as not to confuse scheduler with a special task
65 * that do not consume any time, but still wants to run.
66 */
67 if (hardirq_count())
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020068 irqtime->hardirq_time += delta;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020069 else if (in_serving_softirq() && curr != this_cpu_ksoftirqd())
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020070 irqtime->softirq_time += delta;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -070071 else
72 account = false;
73
74 if (account)
75 sched_account_irqtime(cpu, curr, delta, wallclock);
76 else if (curr != this_cpu_ksoftirqd())
77 sched_account_irqstart(cpu, curr, wallclock);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020078
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020079 u64_stats_update_end(&irqtime->sync);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020080}
Frederic Weisbecker3e1df4f52012-10-06 05:23:22 +020081EXPORT_SYMBOL_GPL(irqtime_account_irq);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020082
Frederic Weisbecker447976e2016-09-26 02:29:21 +020083static cputime_t irqtime_account_update(u64 irqtime, int idx, cputime_t maxtime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020084{
85 u64 *cpustat = kcpustat_this_cpu->cpustat;
Rik van Riel57430212016-07-13 16:50:01 +020086 cputime_t irq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020087
Frederic Weisbecker447976e2016-09-26 02:29:21 +020088 irq_cputime = nsecs_to_cputime64(irqtime) - cpustat[idx];
Rik van Riel57430212016-07-13 16:50:01 +020089 irq_cputime = min(irq_cputime, maxtime);
Frederic Weisbecker447976e2016-09-26 02:29:21 +020090 cpustat[idx] += irq_cputime;
Frederic Weisbecker2810f612016-09-26 02:29:18 +020091
Rik van Riel57430212016-07-13 16:50:01 +020092 return irq_cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +020093}
94
Frederic Weisbecker447976e2016-09-26 02:29:21 +020095static cputime_t irqtime_account_hi_update(cputime_t maxtime)
96{
97 return irqtime_account_update(__this_cpu_read(cpu_irqtime.hardirq_time),
98 CPUTIME_IRQ, maxtime);
99}
100
Rik van Riel57430212016-07-13 16:50:01 +0200101static cputime_t irqtime_account_si_update(cputime_t maxtime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200102{
Frederic Weisbecker447976e2016-09-26 02:29:21 +0200103 return irqtime_account_update(__this_cpu_read(cpu_irqtime.softirq_time),
104 CPUTIME_SOFTIRQ, maxtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200105}
106
107#else /* CONFIG_IRQ_TIME_ACCOUNTING */
108
109#define sched_clock_irqtime (0)
110
Rik van Riel57430212016-07-13 16:50:01 +0200111static cputime_t irqtime_account_hi_update(cputime_t dummy)
112{
113 return 0;
114}
115
116static cputime_t irqtime_account_si_update(cputime_t dummy)
117{
118 return 0;
119}
120
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200121#endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
122
123static inline void task_group_account_field(struct task_struct *p, int index,
124 u64 tmp)
125{
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200126 /*
127 * Since all updates are sure to touch the root cgroup, we
128 * get ourselves ahead and touch it first. If the root cgroup
129 * is the only cgroup, then nothing else should be necessary.
130 *
131 */
Christoph Lametera4f61cc2013-08-07 15:38:24 +0000132 __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200133
Li Zefan1966aaf2013-03-29 14:37:06 +0800134 cpuacct_account_field(p, index, tmp);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200135}
136
137/*
138 * Account user cpu time to a process.
139 * @p: the process that the cpu time gets accounted to
140 * @cputime: the cpu time spent in user space since the last update
141 * @cputime_scaled: cputime scaled by cpu frequency
142 */
143void account_user_time(struct task_struct *p, cputime_t cputime,
144 cputime_t cputime_scaled)
145{
146 int index;
147
148 /* Add user time to process. */
149 p->utime += cputime;
150 p->utimescaled += cputime_scaled;
151 account_group_user_time(p, cputime);
152
Dongsheng Yangd0ea0262014-01-27 22:00:45 -0500153 index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200154
155 /* Add user time to cpustat. */
156 task_group_account_field(p, index, (__force u64) cputime);
157
158 /* Account for user time used */
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100159 acct_account_cputime(p);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200160}
161
162/*
163 * Account guest cpu time to a process.
164 * @p: the process that the cpu time gets accounted to
165 * @cputime: the cpu time spent in virtual machine since the last update
166 * @cputime_scaled: cputime scaled by cpu frequency
167 */
168static void account_guest_time(struct task_struct *p, cputime_t cputime,
169 cputime_t cputime_scaled)
170{
171 u64 *cpustat = kcpustat_this_cpu->cpustat;
172
173 /* Add guest time to process. */
174 p->utime += cputime;
175 p->utimescaled += cputime_scaled;
176 account_group_user_time(p, cputime);
177 p->gtime += cputime;
178
179 /* Add guest time to cpustat. */
Dongsheng Yangd0ea0262014-01-27 22:00:45 -0500180 if (task_nice(p) > 0) {
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200181 cpustat[CPUTIME_NICE] += (__force u64) cputime;
182 cpustat[CPUTIME_GUEST_NICE] += (__force u64) cputime;
183 } else {
184 cpustat[CPUTIME_USER] += (__force u64) cputime;
185 cpustat[CPUTIME_GUEST] += (__force u64) cputime;
186 }
187}
188
189/*
190 * Account system cpu time to a process and desired cpustat field
191 * @p: the process that the cpu time gets accounted to
192 * @cputime: the cpu time spent in kernel space since the last update
193 * @cputime_scaled: cputime scaled by cpu frequency
194 * @target_cputime64: pointer to cpustat field that has to be updated
195 */
196static inline
197void __account_system_time(struct task_struct *p, cputime_t cputime,
198 cputime_t cputime_scaled, int index)
199{
200 /* Add system time to process. */
201 p->stime += cputime;
202 p->stimescaled += cputime_scaled;
203 account_group_system_time(p, cputime);
204
205 /* Add system time to cpustat. */
206 task_group_account_field(p, index, (__force u64) cputime);
207
208 /* Account for system time used */
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100209 acct_account_cputime(p);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200210}
211
212/*
213 * Account system cpu time to a process.
214 * @p: the process that the cpu time gets accounted to
215 * @hardirq_offset: the offset to subtract from hardirq_count()
216 * @cputime: the cpu time spent in kernel space since the last update
217 * @cputime_scaled: cputime scaled by cpu frequency
218 */
219void account_system_time(struct task_struct *p, int hardirq_offset,
220 cputime_t cputime, cputime_t cputime_scaled)
221{
222 int index;
223
224 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
225 account_guest_time(p, cputime, cputime_scaled);
226 return;
227 }
228
229 if (hardirq_count() - hardirq_offset)
230 index = CPUTIME_IRQ;
231 else if (in_serving_softirq())
232 index = CPUTIME_SOFTIRQ;
233 else
234 index = CPUTIME_SYSTEM;
235
236 __account_system_time(p, cputime, cputime_scaled, index);
237}
238
239/*
240 * Account for involuntary wait time.
241 * @cputime: the cpu time spent in involuntary wait
242 */
243void account_steal_time(cputime_t cputime)
244{
245 u64 *cpustat = kcpustat_this_cpu->cpustat;
246
247 cpustat[CPUTIME_STEAL] += (__force u64) cputime;
248}
249
250/*
251 * Account for idle time.
252 * @cputime: the cpu time spent in idle wait
253 */
254void account_idle_time(cputime_t cputime)
255{
256 u64 *cpustat = kcpustat_this_cpu->cpustat;
257 struct rq *rq = this_rq();
258
259 if (atomic_read(&rq->nr_iowait) > 0)
260 cpustat[CPUTIME_IOWAIT] += (__force u64) cputime;
261 else
262 cpustat[CPUTIME_IDLE] += (__force u64) cputime;
263}
264
Wanpeng Li03cbc732016-08-17 10:05:46 +0800265/*
266 * When a guest is interrupted for a longer amount of time, missed clock
267 * ticks are not redelivered later. Due to that, this function may on
268 * occasion account more time than the calling functions think elapsed.
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
Frederic Weisbecker2810f612016-09-26 02:29:18 +0200297 /* Shall be converted to a lockdep-enabled lightweight check */
298 WARN_ON_ONCE(!irqs_disabled());
299
Rik van Riel57430212016-07-13 16:50:01 +0200300 accounted = steal_account_process_time(max);
301
302 if (accounted < max)
303 accounted += irqtime_account_hi_update(max - accounted);
304
305 if (accounted < max)
306 accounted += irqtime_account_si_update(max - accounted);
307
308 return accounted;
309}
310
Stanislaw Gruszkaa1eb1412016-08-17 11:30:44 +0200311#ifdef CONFIG_64BIT
312static inline u64 read_sum_exec_runtime(struct task_struct *t)
313{
314 return t->se.sum_exec_runtime;
315}
316#else
317static u64 read_sum_exec_runtime(struct task_struct *t)
318{
319 u64 ns;
320 struct rq_flags rf;
321 struct rq *rq;
322
323 rq = task_rq_lock(t, &rf);
324 ns = t->se.sum_exec_runtime;
325 task_rq_unlock(rq, t, &rf);
326
327 return ns;
328}
329#endif
330
Rik van Riel57430212016-07-13 16:50:01 +0200331/*
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100332 * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
333 * tasks (sum on group iteration) belonging to @tsk's group.
334 */
335void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
336{
337 struct signal_struct *sig = tsk->signal;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100338 cputime_t utime, stime;
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100339 struct task_struct *t;
Rik van Riele78c3492014-08-16 13:40:10 -0400340 unsigned int seq, nextseq;
Rik van Riel9c368b52014-09-12 09:12:15 -0400341 unsigned long flags;
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100342
Stanislaw Gruszkaa1eb1412016-08-17 11:30:44 +0200343 /*
344 * Update current task runtime to account pending time since last
345 * scheduler action or thread_group_cputime() call. This thread group
346 * might have other running tasks on different CPUs, but updating
347 * their runtime can affect syscall performance, so we skip account
348 * those pending times and rely only on values updated on tick or
349 * other scheduler action.
350 */
351 if (same_thread_group(current, tsk))
352 (void) task_sched_runtime(current);
353
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100354 rcu_read_lock();
Rik van Riele78c3492014-08-16 13:40:10 -0400355 /* Attempt a lockless read on the first round. */
356 nextseq = 0;
357 do {
358 seq = nextseq;
Rik van Riel9c368b52014-09-12 09:12:15 -0400359 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
Rik van Riele78c3492014-08-16 13:40:10 -0400360 times->utime = sig->utime;
361 times->stime = sig->stime;
362 times->sum_exec_runtime = sig->sum_sched_runtime;
363
364 for_each_thread(tsk, t) {
365 task_cputime(t, &utime, &stime);
366 times->utime += utime;
367 times->stime += stime;
Stanislaw Gruszkaa1eb1412016-08-17 11:30:44 +0200368 times->sum_exec_runtime += read_sum_exec_runtime(t);
Rik van Riele78c3492014-08-16 13:40:10 -0400369 }
370 /* If lockless access failed, take the lock. */
371 nextseq = 1;
372 } while (need_seqretry(&sig->stats_lock, seq));
Rik van Riel9c368b52014-09-12 09:12:15 -0400373 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
Frederic Weisbeckera634f932012-11-21 15:55:59 +0100374 rcu_read_unlock();
375}
376
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200377#ifdef CONFIG_IRQ_TIME_ACCOUNTING
378/*
379 * Account a tick to a process and cpustat
380 * @p: the process that the cpu time gets accounted to
381 * @user_tick: is the tick from userspace
382 * @rq: the pointer to rq
383 *
384 * Tick demultiplexing follows the order
385 * - pending hardirq update
386 * - pending softirq update
387 * - user_time
388 * - idle_time
389 * - system time
390 * - check for guest_time
391 * - else account as system_time
392 *
393 * Check for hardirq is done both for system and user time as there is
394 * no timer going off while we are on hardirq and hence we may never get an
395 * opportunity to update it solely in system time.
396 * p->stime and friends are only updated on system time and not on irq
397 * softirq as those do not count in task exec_runtime any more.
398 */
399static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
Thomas Gleixner2d513862014-05-02 23:26:24 +0200400 struct rq *rq, int ticks)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200401{
Rik van Riel57430212016-07-13 16:50:01 +0200402 u64 cputime = (__force u64) cputime_one_jiffy * ticks;
403 cputime_t scaled, other;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200404
Rik van Riel57430212016-07-13 16:50:01 +0200405 /*
406 * When returning from idle, many ticks can get accounted at
407 * once, including some ticks of steal, irq, and softirq time.
408 * Subtract those ticks from the amount of time accounted to
409 * idle, or potentially user or system time. Due to rounding,
410 * other time can exceed ticks occasionally.
411 */
Wanpeng Li03cbc732016-08-17 10:05:46 +0800412 other = account_other_time(ULONG_MAX);
Rik van Riel57430212016-07-13 16:50:01 +0200413 if (other >= cputime)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200414 return;
Rik van Riel57430212016-07-13 16:50:01 +0200415 cputime -= other;
416 scaled = cputime_to_scaled(cputime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200417
Rik van Riel57430212016-07-13 16:50:01 +0200418 if (this_cpu_ksoftirqd() == p) {
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200419 /*
420 * ksoftirqd time do not get accounted in cpu_softirq_time.
421 * So, we have to handle it separately here.
422 * Also, p->stime needs to be updated for ksoftirqd.
423 */
Thomas Gleixner2d513862014-05-02 23:26:24 +0200424 __account_system_time(p, cputime, scaled, CPUTIME_SOFTIRQ);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200425 } else if (user_tick) {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200426 account_user_time(p, cputime, scaled);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200427 } else if (p == rq->idle) {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200428 account_idle_time(cputime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200429 } else if (p->flags & PF_VCPU) { /* System time or guest time */
Thomas Gleixner2d513862014-05-02 23:26:24 +0200430 account_guest_time(p, cputime, scaled);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200431 } else {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200432 __account_system_time(p, cputime, scaled, CPUTIME_SYSTEM);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200433 }
434}
435
436static void irqtime_account_idle_ticks(int ticks)
437{
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200438 struct rq *rq = this_rq();
439
Thomas Gleixner2d513862014-05-02 23:26:24 +0200440 irqtime_account_process_tick(current, 0, rq, ticks);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200441}
442#else /* CONFIG_IRQ_TIME_ACCOUNTING */
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +0200443static inline void irqtime_account_idle_ticks(int ticks) {}
444static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
Thomas Gleixner2d513862014-05-02 23:26:24 +0200445 struct rq *rq, int nr_ticks) {}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200446#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
447
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200448/*
449 * Use precise platform statistics if available:
450 */
451#ifdef CONFIG_VIRT_CPU_ACCOUNTING
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200452
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100453#ifndef __ARCH_HAS_VTIME_TASK_SWITCH
Frederic Weisbeckerb0493402013-07-12 03:10:15 +0200454void vtime_common_task_switch(struct task_struct *prev)
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100455{
456 if (is_idle_task(prev))
457 vtime_account_idle(prev);
458 else
459 vtime_account_system(prev);
460
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200461#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100462 vtime_account_user(prev);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200463#endif
Frederic Weisbeckere3942ba2012-11-14 00:24:25 +0100464 arch_vtime_task_switch(prev);
465}
466#endif
Frederic Weisbecker11113332012-10-24 18:05:51 +0200467
Frederic Weisbecker0cfdf9a2016-07-13 16:50:03 +0200468#endif /* CONFIG_VIRT_CPU_ACCOUNTING */
469
470
471#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200472/*
473 * Archs that account the whole time spent in the idle task
474 * (outside irq) as idle time can rely on this and just implement
Frederic Weisbeckerfd25b4c2012-11-13 18:21:22 +0100475 * vtime_account_system() and vtime_account_idle(). Archs that
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200476 * have other meaning of the idle time (s390 only includes the
477 * time spent by the CPU when it's in low power mode) must override
478 * vtime_account().
479 */
480#ifndef __ARCH_HAS_VTIME_ACCOUNT
Frederic Weisbecker0cfdf9a2016-07-13 16:50:03 +0200481void vtime_account_irq_enter(struct task_struct *tsk)
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200482{
Frederic Weisbecker0cfdf9a2016-07-13 16:50:03 +0200483 if (!in_interrupt() && is_idle_task(tsk))
484 vtime_account_idle(tsk);
485 else
486 vtime_account_system(tsk);
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200487}
Frederic Weisbecker0cfdf9a2016-07-13 16:50:03 +0200488EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200489#endif /* __ARCH_HAS_VTIME_ACCOUNT */
490
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100491void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200492{
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100493 *ut = p->utime;
494 *st = p->stime;
495}
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300496EXPORT_SYMBOL_GPL(task_cputime_adjusted);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200497
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100498void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
499{
500 struct task_cputime cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200501
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100502 thread_group_cputime(p, &cputime);
503
504 *ut = cputime.utime;
505 *st = cputime.stime;
506}
507#else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
508/*
509 * Account a single tick of cpu time.
510 * @p: the process that the cpu time gets accounted to
511 * @user_tick: indicates if the tick is a user or a system tick
512 */
513void account_process_tick(struct task_struct *p, int user_tick)
514{
Rik van Riel57430212016-07-13 16:50:01 +0200515 cputime_t cputime, scaled, steal;
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100516 struct rq *rq = this_rq();
517
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +0100518 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100519 return;
520
521 if (sched_clock_irqtime) {
Thomas Gleixner2d513862014-05-02 23:26:24 +0200522 irqtime_account_process_tick(p, user_tick, rq, 1);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100523 return;
524 }
525
Rik van Riel57430212016-07-13 16:50:01 +0200526 cputime = cputime_one_jiffy;
Wanpeng Li03cbc732016-08-17 10:05:46 +0800527 steal = steal_account_process_time(ULONG_MAX);
Rik van Riel57430212016-07-13 16:50:01 +0200528
529 if (steal >= cputime)
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100530 return;
531
Rik van Riel57430212016-07-13 16:50:01 +0200532 cputime -= steal;
533 scaled = cputime_to_scaled(cputime);
534
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100535 if (user_tick)
Rik van Riel57430212016-07-13 16:50:01 +0200536 account_user_time(p, cputime, scaled);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100537 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
Rik van Riel57430212016-07-13 16:50:01 +0200538 account_system_time(p, HARDIRQ_OFFSET, cputime, scaled);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200539 else
Rik van Riel57430212016-07-13 16:50:01 +0200540 account_idle_time(cputime);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100541}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200542
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100543/*
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100544 * Account multiple ticks of idle time.
545 * @ticks: number of stolen ticks
546 */
547void account_idle_ticks(unsigned long ticks)
548{
Wanpeng Lif9bcf1e2016-08-11 13:36:35 +0800549 cputime_t cputime, steal;
Frederic Weisbecker26f2c752016-08-11 14:58:24 +0200550
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100551 if (sched_clock_irqtime) {
552 irqtime_account_idle_ticks(ticks);
553 return;
554 }
555
Frederic Weisbecker26f2c752016-08-11 14:58:24 +0200556 cputime = jiffies_to_cputime(ticks);
Wanpeng Li03cbc732016-08-17 10:05:46 +0800557 steal = steal_account_process_time(ULONG_MAX);
Wanpeng Lif9bcf1e2016-08-11 13:36:35 +0800558
559 if (steal >= cputime)
560 return;
561
562 cputime -= steal;
563 account_idle_time(cputime);
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100564}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200565
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100566/*
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200567 * Perform (stime * rtime) / total, but avoid multiplication overflow by
568 * loosing precision when the numbers are big.
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100569 */
570static cputime_t scale_stime(u64 stime, u64 rtime, u64 total)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200571{
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200572 u64 scaled;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200573
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200574 for (;;) {
575 /* Make sure "rtime" is the bigger of stime/rtime */
Stanislaw Gruszka84f9f3a2013-05-02 15:34:33 +0200576 if (stime > rtime)
577 swap(rtime, stime);
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200578
579 /* Make sure 'total' fits in 32 bits */
580 if (total >> 32)
581 goto drop_precision;
582
583 /* Does rtime (and thus stime) fit in 32 bits? */
584 if (!(rtime >> 32))
585 break;
586
587 /* Can we just balance rtime/stime rather than dropping bits? */
588 if (stime >> 31)
589 goto drop_precision;
590
591 /* We can grow stime and shrink rtime and try to make them both fit */
592 stime <<= 1;
593 rtime >>= 1;
594 continue;
595
596drop_precision:
597 /* We drop from rtime, it has more bits than stime */
598 rtime >>= 1;
599 total >>= 1;
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100600 }
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200601
Stanislaw Gruszka55eaa7c2013-04-30 17:14:42 +0200602 /*
603 * Make sure gcc understands that this is a 32x32->64 multiply,
604 * followed by a 64/32->64 divide.
605 */
606 scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100607 return (__force cputime_t) scaled;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200608}
609
Frederic Weisbeckerfa092052012-11-28 17:00:57 +0100610/*
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200611 * Adjust tick based cputime random precision against scheduler runtime
612 * accounting.
Rik van Riel347abad2014-09-30 15:59:47 -0400613 *
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200614 * Tick based cputime accounting depend on random scheduling timeslices of a
615 * task to be interrupted or not by the timer. Depending on these
616 * circumstances, the number of these interrupts may be over or
617 * under-optimistic, matching the real user and system cputime with a variable
618 * precision.
619 *
620 * Fix this by scaling these tick based values against the total runtime
621 * accounted by the CFS scheduler.
622 *
623 * This code provides the following guarantees:
624 *
625 * stime + utime == rtime
626 * stime_i+1 >= stime_i, utime_i+1 >= utime_i
627 *
628 * Assuming that rtime_i+1 >= rtime_i.
Frederic Weisbeckerfa092052012-11-28 17:00:57 +0100629 */
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100630static void cputime_adjust(struct task_cputime *curr,
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200631 struct prev_cputime *prev,
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100632 cputime_t *ut, cputime_t *st)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200633{
Stanislaw Gruszka5a8e01f2013-09-04 15:16:03 +0200634 cputime_t rtime, stime, utime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200635 unsigned long flags;
Frederic Weisbeckerfa092052012-11-28 17:00:57 +0100636
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200637 /* Serialize concurrent callers such that we can honour our guarantees */
638 raw_spin_lock_irqsave(&prev->lock, flags);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100639 rtime = nsecs_to_cputime(curr->sum_exec_runtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200640
Stanislaw Gruszka772c8082013-04-30 11:35:05 +0200641 /*
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200642 * This is possible under two circumstances:
643 * - rtime isn't monotonic after all (a bug);
644 * - we got reordered by the lock.
645 *
646 * In both cases this acts as a filter such that the rest of the code
647 * can assume it is monotonic regardless of anything else.
Stanislaw Gruszka772c8082013-04-30 11:35:05 +0200648 */
649 if (prev->stime + prev->utime >= rtime)
650 goto out;
651
Stanislaw Gruszka5a8e01f2013-09-04 15:16:03 +0200652 stime = curr->stime;
653 utime = curr->utime;
654
Peter Zijlstra173be9a2016-08-15 18:38:42 +0200655 /*
656 * If either stime or both stime and utime are 0, assume all runtime is
657 * userspace. Once a task gets some ticks, the monotonicy code at
658 * 'update' will ensure things converge to the observed ratio.
659 */
660 if (stime == 0) {
661 utime = rtime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200662 goto update;
Frederic Weisbeckerd9a3c982013-02-20 18:54:55 +0100663 }
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200664
Peter Zijlstra173be9a2016-08-15 18:38:42 +0200665 if (utime == 0) {
666 stime = rtime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200667 goto update;
668 }
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200669
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200670 stime = scale_stime((__force u64)stime, (__force u64)rtime,
671 (__force u64)(stime + utime));
672
Peter Zijlstra173be9a2016-08-15 18:38:42 +0200673update:
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200674 /*
675 * Make sure stime doesn't go backwards; this preserves monotonicity
676 * for utime because rtime is monotonic.
677 *
678 * utime_i+1 = rtime_i+1 - stime_i
679 * = rtime_i+1 - (rtime_i - utime_i)
680 * = (rtime_i+1 - rtime_i) + utime_i
681 * >= utime_i
682 */
683 if (stime < prev->stime)
684 stime = prev->stime;
685 utime = rtime - stime;
686
687 /*
688 * Make sure utime doesn't go backwards; this still preserves
689 * monotonicity for stime, analogous argument to above.
690 */
691 if (utime < prev->utime) {
692 utime = prev->utime;
693 stime = rtime - utime;
694 }
695
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200696 prev->stime = stime;
697 prev->utime = utime;
Stanislaw Gruszka772c8082013-04-30 11:35:05 +0200698out:
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100699 *ut = prev->utime;
700 *st = prev->stime;
Peter Zijlstra9d7fb042015-06-30 11:30:54 +0200701 raw_spin_unlock_irqrestore(&prev->lock, flags);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100702}
703
704void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
705{
706 struct task_cputime cputime = {
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100707 .sum_exec_runtime = p->se.sum_exec_runtime,
708 };
709
Frederic Weisbecker6fac4822012-11-13 14:20:55 +0100710 task_cputime(p, &cputime.utime, &cputime.stime);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100711 cputime_adjust(&cputime, &p->prev_cputime, ut, st);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200712}
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300713EXPORT_SYMBOL_GPL(task_cputime_adjusted);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200714
Frederic Weisbeckere80d0a12012-11-21 16:26:44 +0100715void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200716{
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200717 struct task_cputime cputime;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200718
719 thread_group_cputime(p, &cputime);
Frederic Weisbeckerd37f761d2012-11-22 00:58:35 +0100720 cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +0200721}
Frederic Weisbecker9fbc42e2013-02-25 17:25:39 +0100722#endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200723
724#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Rik van Rielff9a9b42016-02-10 20:08:27 -0500725static cputime_t vtime_delta(struct task_struct *tsk)
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200726{
Rik van Rielff9a9b42016-02-10 20:08:27 -0500727 unsigned long now = READ_ONCE(jiffies);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200728
Rik van Rielff9a9b42016-02-10 20:08:27 -0500729 if (time_before(now, (unsigned long)tsk->vtime_snap))
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100730 return 0;
731
Rik van Rielff9a9b42016-02-10 20:08:27 -0500732 return jiffies_to_cputime(now - tsk->vtime_snap);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100733}
734
735static cputime_t get_vtime_delta(struct task_struct *tsk)
736{
Rik van Rielff9a9b42016-02-10 20:08:27 -0500737 unsigned long now = READ_ONCE(jiffies);
Rik van Rielb58c3582016-07-13 16:50:02 +0200738 cputime_t delta, other;
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100739
Wanpeng Li03cbc732016-08-17 10:05:46 +0800740 /*
741 * Unlike tick based timing, vtime based timing never has lost
742 * ticks, and no need for steal time accounting to make up for
743 * lost ticks. Vtime accounts a rounded version of actual
744 * elapsed time. Limit account_other_time to prevent rounding
745 * errors from causing elapsed vtime to go negative.
746 */
Rik van Riel57430212016-07-13 16:50:01 +0200747 delta = jiffies_to_cputime(now - tsk->vtime_snap);
Rik van Rielb58c3582016-07-13 16:50:02 +0200748 other = account_other_time(delta);
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +0100749 WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500750 tsk->vtime_snap = now;
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200751
Rik van Rielb58c3582016-07-13 16:50:02 +0200752 return delta - other;
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200753}
754
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100755static void __vtime_account_system(struct task_struct *tsk)
756{
757 cputime_t delta_cpu = get_vtime_delta(tsk);
758
759 account_system_time(tsk, irq_count(), delta_cpu, cputime_to_scaled(delta_cpu));
760}
761
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200762void vtime_account_system(struct task_struct *tsk)
763{
Rik van Rielff9a9b42016-02-10 20:08:27 -0500764 if (!vtime_delta(tsk))
765 return;
766
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100767 write_seqcount_begin(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100768 __vtime_account_system(tsk);
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100769 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100770}
771
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200772void vtime_account_user(struct task_struct *tsk)
773{
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +0200774 cputime_t delta_cpu;
775
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100776 write_seqcount_begin(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100777 tsk->vtime_snap_whence = VTIME_SYS;
Rik van Rielff9a9b42016-02-10 20:08:27 -0500778 if (vtime_delta(tsk)) {
779 delta_cpu = get_vtime_delta(tsk);
780 account_user_time(tsk, delta_cpu, cputime_to_scaled(delta_cpu));
781 }
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100782 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100783}
784
785void vtime_user_enter(struct task_struct *tsk)
786{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100787 write_seqcount_begin(&tsk->vtime_seqcount);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500788 if (vtime_delta(tsk))
789 __vtime_account_system(tsk);
Frederic Weisbeckeraf2350b2013-07-15 16:35:55 +0200790 tsk->vtime_snap_whence = VTIME_USER;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100791 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100792}
793
794void vtime_guest_enter(struct task_struct *tsk)
795{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200796 /*
797 * The flags must be updated under the lock with
798 * the vtime_snap flush and update.
799 * That enforces a right ordering and update sequence
800 * synchronization against the reader (task_gtime())
801 * that can thus safely catch up with a tickless delta.
802 */
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100803 write_seqcount_begin(&tsk->vtime_seqcount);
Rik van Rielff9a9b42016-02-10 20:08:27 -0500804 if (vtime_delta(tsk))
805 __vtime_account_system(tsk);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100806 current->flags |= PF_VCPU;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100807 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100808}
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200809EXPORT_SYMBOL_GPL(vtime_guest_enter);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100810
811void vtime_guest_exit(struct task_struct *tsk)
812{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100813 write_seqcount_begin(&tsk->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100814 __vtime_account_system(tsk);
815 current->flags &= ~PF_VCPU;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100816 write_seqcount_end(&tsk->vtime_seqcount);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200817}
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200818EXPORT_SYMBOL_GPL(vtime_guest_exit);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200819
820void vtime_account_idle(struct task_struct *tsk)
821{
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100822 cputime_t delta_cpu = get_vtime_delta(tsk);
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200823
824 account_idle_time(delta_cpu);
825}
Frederic Weisbecker3f4724e2012-07-16 18:00:34 +0200826
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100827void arch_vtime_task_switch(struct task_struct *prev)
828{
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100829 write_seqcount_begin(&prev->vtime_seqcount);
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +0100830 prev->vtime_snap_whence = VTIME_INACTIVE;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100831 write_seqcount_end(&prev->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100832
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100833 write_seqcount_begin(&current->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100834 current->vtime_snap_whence = VTIME_SYS;
Rik van Rielff9a9b42016-02-10 20:08:27 -0500835 current->vtime_snap = jiffies;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100836 write_seqcount_end(&current->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100837}
838
Frederic Weisbecker45eacc62013-05-15 22:16:32 +0200839void vtime_init_idle(struct task_struct *t, int cpu)
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100840{
841 unsigned long flags;
842
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100843 local_irq_save(flags);
844 write_seqcount_begin(&t->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100845 t->vtime_snap_whence = VTIME_SYS;
Rik van Rielff9a9b42016-02-10 20:08:27 -0500846 t->vtime_snap = jiffies;
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100847 write_seqcount_end(&t->vtime_seqcount);
848 local_irq_restore(flags);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100849}
850
851cputime_t task_gtime(struct task_struct *t)
852{
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100853 unsigned int seq;
854 cputime_t gtime;
855
Frederic Weisbeckere5925392015-11-19 16:47:33 +0100856 if (!vtime_accounting_enabled())
Hiroshi Shimamoto25411172015-11-19 16:47:28 +0100857 return t->gtime;
858
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100859 do {
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100860 seq = read_seqcount_begin(&t->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100861
862 gtime = t->gtime;
Frederic Weisbeckercab245d2015-11-19 16:47:31 +0100863 if (t->vtime_snap_whence == VTIME_SYS && t->flags & PF_VCPU)
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100864 gtime += vtime_delta(t);
865
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100866 } while (read_seqcount_retry(&t->vtime_seqcount, seq));
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100867
868 return gtime;
869}
870
871/*
872 * Fetch cputime raw values from fields of task_struct and
873 * add up the pending nohz execution time since the last
874 * cputime snapshot.
875 */
876static void
877fetch_task_cputime(struct task_struct *t,
878 cputime_t *u_dst, cputime_t *s_dst,
879 cputime_t *u_src, cputime_t *s_src,
880 cputime_t *udelta, cputime_t *sdelta)
881{
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100882 unsigned int seq;
883 unsigned long long delta;
884
885 do {
886 *udelta = 0;
887 *sdelta = 0;
888
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100889 seq = read_seqcount_begin(&t->vtime_seqcount);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100890
891 if (u_dst)
892 *u_dst = *u_src;
893 if (s_dst)
894 *s_dst = *s_src;
895
896 /* Task is sleeping, nothing to add */
Frederic Weisbecker7098c1e2015-11-19 16:47:30 +0100897 if (t->vtime_snap_whence == VTIME_INACTIVE ||
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100898 is_idle_task(t))
899 continue;
900
901 delta = vtime_delta(t);
902
903 /*
904 * Task runs either in user or kernel space, add pending nohz time to
905 * the right place.
906 */
907 if (t->vtime_snap_whence == VTIME_USER || t->flags & PF_VCPU) {
908 *udelta = delta;
909 } else {
910 if (t->vtime_snap_whence == VTIME_SYS)
911 *sdelta = delta;
912 }
Frederic Weisbeckerb7ce2272015-11-19 16:47:34 +0100913 } while (read_seqcount_retry(&t->vtime_seqcount, seq));
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100914}
915
916
917void task_cputime(struct task_struct *t, cputime_t *utime, cputime_t *stime)
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 (utime)
923 *utime = t->utime;
924 if (stime)
925 *stime = t->stime;
926 return;
927 }
928
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100929 fetch_task_cputime(t, utime, stime, &t->utime,
930 &t->stime, &udelta, &sdelta);
931 if (utime)
932 *utime += udelta;
933 if (stime)
934 *stime += sdelta;
935}
936
937void task_cputime_scaled(struct task_struct *t,
938 cputime_t *utimescaled, cputime_t *stimescaled)
939{
940 cputime_t udelta, sdelta;
941
Frederic Weisbeckere5925392015-11-19 16:47:33 +0100942 if (!vtime_accounting_enabled()) {
Hiroshi Shimamoto7877a0b2015-11-19 16:47:29 +0100943 if (utimescaled)
944 *utimescaled = t->utimescaled;
945 if (stimescaled)
946 *stimescaled = t->stimescaled;
947 return;
948 }
949
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100950 fetch_task_cputime(t, utimescaled, stimescaled,
951 &t->utimescaled, &t->stimescaled, &udelta, &sdelta);
952 if (utimescaled)
953 *utimescaled += cputime_to_scaled(udelta);
954 if (stimescaled)
955 *stimescaled += cputime_to_scaled(sdelta);
956}
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200957#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */