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