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