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