• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Simple CPU accounting cgroup controller
4  */
5 #include <linux/cpufreq_times.h>
6 #include "sched.h"
7 #include <trace/hooks/sched.h>
8 
9 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
10 
11 /*
12  * There are no locks covering percpu hardirq/softirq time.
13  * They are only modified in vtime_account, on corresponding CPU
14  * with interrupts disabled. So, writes are safe.
15  * They are read and saved off onto struct rq in update_rq_clock().
16  * This may result in other CPU reading this CPU's irq time and can
17  * race with irq/vtime_account on this CPU. We would either get old
18  * or new value with a side effect of accounting a slice of irq time to wrong
19  * task when irq is in progress while we read rq->clock. That is a worthy
20  * compromise in place of having locks on each irq in account_system_time.
21  */
22 DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
23 EXPORT_PER_CPU_SYMBOL_GPL(cpu_irqtime);
24 
25 static int sched_clock_irqtime;
26 
enable_sched_clock_irqtime(void)27 void enable_sched_clock_irqtime(void)
28 {
29 	sched_clock_irqtime = 1;
30 }
31 
disable_sched_clock_irqtime(void)32 void disable_sched_clock_irqtime(void)
33 {
34 	sched_clock_irqtime = 0;
35 }
36 
irqtime_account_delta(struct irqtime * irqtime,u64 delta,enum cpu_usage_stat idx)37 static void irqtime_account_delta(struct irqtime *irqtime, u64 delta,
38 				  enum cpu_usage_stat idx)
39 {
40 	u64 *cpustat = kcpustat_this_cpu->cpustat;
41 
42 	u64_stats_update_begin(&irqtime->sync);
43 	cpustat[idx] += delta;
44 	irqtime->total += delta;
45 	irqtime->tick_delta += delta;
46 	u64_stats_update_end(&irqtime->sync);
47 }
48 
49 /*
50  * Called after incrementing preempt_count on {soft,}irq_enter
51  * and before decrementing preempt_count on {soft,}irq_exit.
52  */
irqtime_account_irq(struct task_struct * curr,unsigned int offset)53 void irqtime_account_irq(struct task_struct *curr, unsigned int offset)
54 {
55 	struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
56 	unsigned int pc;
57 	s64 delta;
58 	int cpu;
59 	bool irq_start = true;
60 
61 	if (!sched_clock_irqtime)
62 		return;
63 
64 	cpu = smp_processor_id();
65 	delta = sched_clock_cpu(cpu) - irqtime->irq_start_time;
66 	irqtime->irq_start_time += delta;
67 	pc = irq_count() - offset;
68 
69 	/*
70 	 * We do not account for softirq time from ksoftirqd here.
71 	 * We want to continue accounting softirq time to ksoftirqd thread
72 	 * in that case, so as not to confuse scheduler with a special task
73 	 * that do not consume any time, but still wants to run.
74 	 */
75 	if (pc & HARDIRQ_MASK) {
76 		irqtime_account_delta(irqtime, delta, CPUTIME_IRQ);
77 		irq_start = false;
78 	} else if ((pc & SOFTIRQ_OFFSET) && curr != this_cpu_ksoftirqd()) {
79 		irqtime_account_delta(irqtime, delta, CPUTIME_SOFTIRQ);
80 		irq_start = false;
81 	}
82 
83 	trace_android_rvh_account_irq(curr, cpu, delta);
84 
85 	if (irq_start)
86 		trace_android_rvh_account_irq_start(curr, cpu, delta);
87 	else
88 		trace_android_rvh_account_irq_end(curr, cpu, delta);
89 }
90 
irqtime_tick_accounted(u64 maxtime)91 static u64 irqtime_tick_accounted(u64 maxtime)
92 {
93 	struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
94 	u64 delta;
95 
96 	delta = min(irqtime->tick_delta, maxtime);
97 	irqtime->tick_delta -= delta;
98 
99 	return delta;
100 }
101 
102 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
103 
104 #define sched_clock_irqtime	(0)
105 
irqtime_tick_accounted(u64 dummy)106 static u64 irqtime_tick_accounted(u64 dummy)
107 {
108 	return 0;
109 }
110 
111 #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
112 
task_group_account_field(struct task_struct * p,int index,u64 tmp)113 static inline void task_group_account_field(struct task_struct *p, int index,
114 					    u64 tmp)
115 {
116 	/*
117 	 * Since all updates are sure to touch the root cgroup, we
118 	 * get ourselves ahead and touch it first. If the root cgroup
119 	 * is the only cgroup, then nothing else should be necessary.
120 	 *
121 	 */
122 	__this_cpu_add(kernel_cpustat.cpustat[index], tmp);
123 
124 	cgroup_account_cputime_field(p, index, tmp);
125 }
126 
127 /*
128  * Account user CPU time to a process.
129  * @p: the process that the CPU time gets accounted to
130  * @cputime: the CPU time spent in user space since the last update
131  */
account_user_time(struct task_struct * p,u64 cputime)132 void account_user_time(struct task_struct *p, u64 cputime)
133 {
134 	int index;
135 
136 	/* Add user time to process. */
137 	p->utime += cputime;
138 	account_group_user_time(p, cputime);
139 
140 	index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
141 
142 	/* Add user time to cpustat. */
143 	task_group_account_field(p, index, cputime);
144 
145 	/* Account for user time used */
146 	acct_account_cputime(p);
147 
148 	/* Account power usage for user time */
149 	cpufreq_acct_update_power(p, cputime);
150 }
151 
152 /*
153  * Account guest CPU time to a process.
154  * @p: the process that the CPU time gets accounted to
155  * @cputime: the CPU time spent in virtual machine since the last update
156  */
account_guest_time(struct task_struct * p,u64 cputime)157 void account_guest_time(struct task_struct *p, u64 cputime)
158 {
159 	u64 *cpustat = kcpustat_this_cpu->cpustat;
160 
161 	/* Add guest time to process. */
162 	p->utime += cputime;
163 	account_group_user_time(p, cputime);
164 	p->gtime += cputime;
165 
166 	/* Add guest time to cpustat. */
167 	if (task_nice(p) > 0) {
168 		task_group_account_field(p, CPUTIME_NICE, cputime);
169 		cpustat[CPUTIME_GUEST_NICE] += cputime;
170 	} else {
171 		task_group_account_field(p, CPUTIME_USER, cputime);
172 		cpustat[CPUTIME_GUEST] += cputime;
173 	}
174 }
175 
176 /*
177  * Account system CPU time to a process and desired cpustat field
178  * @p: the process that the CPU time gets accounted to
179  * @cputime: the CPU time spent in kernel space since the last update
180  * @index: pointer to cpustat field that has to be updated
181  */
account_system_index_time(struct task_struct * p,u64 cputime,enum cpu_usage_stat index)182 void account_system_index_time(struct task_struct *p,
183 			       u64 cputime, enum cpu_usage_stat index)
184 {
185 	/* Add system time to process. */
186 	p->stime += cputime;
187 	account_group_system_time(p, cputime);
188 
189 	/* Add system time to cpustat. */
190 	task_group_account_field(p, index, cputime);
191 
192 	/* Account for system time used */
193 	acct_account_cputime(p);
194 
195 	/* Account power usage for system time */
196 	cpufreq_acct_update_power(p, cputime);
197 }
198 
199 /*
200  * Account system CPU time to a process.
201  * @p: the process that the CPU time gets accounted to
202  * @hardirq_offset: the offset to subtract from hardirq_count()
203  * @cputime: the CPU time spent in kernel space since the last update
204  */
account_system_time(struct task_struct * p,int hardirq_offset,u64 cputime)205 void account_system_time(struct task_struct *p, int hardirq_offset, u64 cputime)
206 {
207 	int index;
208 
209 	if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
210 		account_guest_time(p, cputime);
211 		return;
212 	}
213 
214 	if (hardirq_count() - hardirq_offset)
215 		index = CPUTIME_IRQ;
216 	else if (in_serving_softirq())
217 		index = CPUTIME_SOFTIRQ;
218 	else
219 		index = CPUTIME_SYSTEM;
220 
221 	account_system_index_time(p, cputime, index);
222 }
223 
224 /*
225  * Account for involuntary wait time.
226  * @cputime: the CPU time spent in involuntary wait
227  */
account_steal_time(u64 cputime)228 void account_steal_time(u64 cputime)
229 {
230 	u64 *cpustat = kcpustat_this_cpu->cpustat;
231 
232 	cpustat[CPUTIME_STEAL] += cputime;
233 }
234 
235 /*
236  * Account for idle time.
237  * @cputime: the CPU time spent in idle wait
238  */
account_idle_time(u64 cputime)239 void account_idle_time(u64 cputime)
240 {
241 	u64 *cpustat = kcpustat_this_cpu->cpustat;
242 	struct rq *rq = this_rq();
243 
244 	if (atomic_read(&rq->nr_iowait) > 0)
245 		cpustat[CPUTIME_IOWAIT] += cputime;
246 	else
247 		cpustat[CPUTIME_IDLE] += cputime;
248 }
249 
250 /*
251  * When a guest is interrupted for a longer amount of time, missed clock
252  * ticks are not redelivered later. Due to that, this function may on
253  * occasion account more time than the calling functions think elapsed.
254  */
steal_account_process_time(u64 maxtime)255 static __always_inline u64 steal_account_process_time(u64 maxtime)
256 {
257 #ifdef CONFIG_PARAVIRT
258 	if (static_key_false(&paravirt_steal_enabled)) {
259 		u64 steal;
260 
261 		steal = paravirt_steal_clock(smp_processor_id());
262 		steal -= this_rq()->prev_steal_time;
263 		steal = min(steal, maxtime);
264 		account_steal_time(steal);
265 		this_rq()->prev_steal_time += steal;
266 
267 		return steal;
268 	}
269 #endif
270 	return 0;
271 }
272 
273 /*
274  * Account how much elapsed time was spent in steal, irq, or softirq time.
275  */
account_other_time(u64 max)276 static inline u64 account_other_time(u64 max)
277 {
278 	u64 accounted;
279 
280 	lockdep_assert_irqs_disabled();
281 
282 	accounted = steal_account_process_time(max);
283 
284 	if (accounted < max)
285 		accounted += irqtime_tick_accounted(max - accounted);
286 
287 	return accounted;
288 }
289 
290 #ifdef CONFIG_64BIT
read_sum_exec_runtime(struct task_struct * t)291 static inline u64 read_sum_exec_runtime(struct task_struct *t)
292 {
293 	return t->se.sum_exec_runtime;
294 }
295 #else
read_sum_exec_runtime(struct task_struct * t)296 static u64 read_sum_exec_runtime(struct task_struct *t)
297 {
298 	u64 ns;
299 	struct rq_flags rf;
300 	struct rq *rq;
301 
302 	rq = task_rq_lock(t, &rf);
303 	ns = t->se.sum_exec_runtime;
304 	task_rq_unlock(rq, t, &rf);
305 
306 	return ns;
307 }
308 #endif
309 
310 /*
311  * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
312  * tasks (sum on group iteration) belonging to @tsk's group.
313  */
thread_group_cputime(struct task_struct * tsk,struct task_cputime * times)314 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
315 {
316 	struct signal_struct *sig = tsk->signal;
317 	u64 utime, stime;
318 	struct task_struct *t;
319 	unsigned int seq, nextseq;
320 	unsigned long flags;
321 
322 	/*
323 	 * Update current task runtime to account pending time since last
324 	 * scheduler action or thread_group_cputime() call. This thread group
325 	 * might have other running tasks on different CPUs, but updating
326 	 * their runtime can affect syscall performance, so we skip account
327 	 * those pending times and rely only on values updated on tick or
328 	 * other scheduler action.
329 	 */
330 	if (same_thread_group(current, tsk))
331 		(void) task_sched_runtime(current);
332 
333 	rcu_read_lock();
334 	/* Attempt a lockless read on the first round. */
335 	nextseq = 0;
336 	do {
337 		seq = nextseq;
338 		flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
339 		times->utime = sig->utime;
340 		times->stime = sig->stime;
341 		times->sum_exec_runtime = sig->sum_sched_runtime;
342 
343 		for_each_thread(tsk, t) {
344 			task_cputime(t, &utime, &stime);
345 			times->utime += utime;
346 			times->stime += stime;
347 			times->sum_exec_runtime += read_sum_exec_runtime(t);
348 		}
349 		/* If lockless access failed, take the lock. */
350 		nextseq = 1;
351 	} while (need_seqretry(&sig->stats_lock, seq));
352 	done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
353 	rcu_read_unlock();
354 }
355 
356 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
357 /*
358  * Account a tick to a process and cpustat
359  * @p: the process that the CPU time gets accounted to
360  * @user_tick: is the tick from userspace
361  * @rq: the pointer to rq
362  *
363  * Tick demultiplexing follows the order
364  * - pending hardirq update
365  * - pending softirq update
366  * - user_time
367  * - idle_time
368  * - system time
369  *   - check for guest_time
370  *   - else account as system_time
371  *
372  * Check for hardirq is done both for system and user time as there is
373  * no timer going off while we are on hardirq and hence we may never get an
374  * opportunity to update it solely in system time.
375  * p->stime and friends are only updated on system time and not on irq
376  * softirq as those do not count in task exec_runtime any more.
377  */
irqtime_account_process_tick(struct task_struct * p,int user_tick,int ticks)378 static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
379 					 int ticks)
380 {
381 	u64 other, cputime = TICK_NSEC * ticks;
382 
383 	/*
384 	 * When returning from idle, many ticks can get accounted at
385 	 * once, including some ticks of steal, irq, and softirq time.
386 	 * Subtract those ticks from the amount of time accounted to
387 	 * idle, or potentially user or system time. Due to rounding,
388 	 * other time can exceed ticks occasionally.
389 	 */
390 	other = account_other_time(ULONG_MAX);
391 	if (other >= cputime)
392 		return;
393 
394 	cputime -= other;
395 
396 	if (this_cpu_ksoftirqd() == p) {
397 		/*
398 		 * ksoftirqd time do not get accounted in cpu_softirq_time.
399 		 * So, we have to handle it separately here.
400 		 * Also, p->stime needs to be updated for ksoftirqd.
401 		 */
402 		account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
403 	} else if (user_tick) {
404 		account_user_time(p, cputime);
405 	} else if (p == this_rq()->idle) {
406 		account_idle_time(cputime);
407 	} else if (p->flags & PF_VCPU) { /* System time or guest time */
408 		account_guest_time(p, cputime);
409 	} else {
410 		account_system_index_time(p, cputime, CPUTIME_SYSTEM);
411 	}
412 	trace_android_vh_irqtime_account_process_tick(p, this_rq(), user_tick, ticks);
413 }
414 
irqtime_account_idle_ticks(int ticks)415 static void irqtime_account_idle_ticks(int ticks)
416 {
417 	irqtime_account_process_tick(current, 0, ticks);
418 }
419 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
irqtime_account_idle_ticks(int ticks)420 static inline void irqtime_account_idle_ticks(int ticks) { }
irqtime_account_process_tick(struct task_struct * p,int user_tick,int nr_ticks)421 static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
422 						int nr_ticks) { }
423 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
424 
425 /*
426  * Use precise platform statistics if available:
427  */
428 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
429 
430 # ifndef __ARCH_HAS_VTIME_TASK_SWITCH
vtime_task_switch(struct task_struct * prev)431 void vtime_task_switch(struct task_struct *prev)
432 {
433 	if (is_idle_task(prev))
434 		vtime_account_idle(prev);
435 	else
436 		vtime_account_kernel(prev);
437 
438 	vtime_flush(prev);
439 	arch_vtime_task_switch(prev);
440 }
441 # endif
442 
vtime_account_irq(struct task_struct * tsk,unsigned int offset)443 void vtime_account_irq(struct task_struct *tsk, unsigned int offset)
444 {
445 	unsigned int pc = irq_count() - offset;
446 
447 	if (pc & HARDIRQ_OFFSET) {
448 		vtime_account_hardirq(tsk);
449 	} else if (pc & SOFTIRQ_OFFSET) {
450 		vtime_account_softirq(tsk);
451 	} else if (!IS_ENABLED(CONFIG_HAVE_VIRT_CPU_ACCOUNTING_IDLE) &&
452 		   is_idle_task(tsk)) {
453 		vtime_account_idle(tsk);
454 	} else {
455 		vtime_account_kernel(tsk);
456 	}
457 }
458 
cputime_adjust(struct task_cputime * curr,struct prev_cputime * prev,u64 * ut,u64 * st)459 void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
460 		    u64 *ut, u64 *st)
461 {
462 	*ut = curr->utime;
463 	*st = curr->stime;
464 }
465 
task_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)466 void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
467 {
468 	*ut = p->utime;
469 	*st = p->stime;
470 }
471 EXPORT_SYMBOL_GPL(task_cputime_adjusted);
472 
thread_group_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)473 void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
474 {
475 	struct task_cputime cputime;
476 
477 	thread_group_cputime(p, &cputime);
478 
479 	*ut = cputime.utime;
480 	*st = cputime.stime;
481 }
482 EXPORT_SYMBOL_GPL(thread_group_cputime_adjusted);
483 
484 #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE: */
485 
486 /*
487  * Account a single tick of CPU time.
488  * @p: the process that the CPU time gets accounted to
489  * @user_tick: indicates if the tick is a user or a system tick
490  */
account_process_tick(struct task_struct * p,int user_tick)491 void account_process_tick(struct task_struct *p, int user_tick)
492 {
493 	u64 cputime, steal;
494 
495 	if (vtime_accounting_enabled_this_cpu())
496 		return;
497 	trace_android_vh_account_task_time(p, this_rq(), user_tick);
498 
499 	if (sched_clock_irqtime) {
500 		irqtime_account_process_tick(p, user_tick, 1);
501 		return;
502 	}
503 
504 	cputime = TICK_NSEC;
505 	steal = steal_account_process_time(ULONG_MAX);
506 
507 	if (steal >= cputime)
508 		return;
509 
510 	cputime -= steal;
511 
512 	if (user_tick)
513 		account_user_time(p, cputime);
514 	else if ((p != this_rq()->idle) || (irq_count() != HARDIRQ_OFFSET))
515 		account_system_time(p, HARDIRQ_OFFSET, cputime);
516 	else
517 		account_idle_time(cputime);
518 }
519 
520 /*
521  * Account multiple ticks of idle time.
522  * @ticks: number of stolen ticks
523  */
account_idle_ticks(unsigned long ticks)524 void account_idle_ticks(unsigned long ticks)
525 {
526 	u64 cputime, steal;
527 
528 	if (sched_clock_irqtime) {
529 		irqtime_account_idle_ticks(ticks);
530 		return;
531 	}
532 
533 	cputime = ticks * TICK_NSEC;
534 	steal = steal_account_process_time(ULONG_MAX);
535 
536 	if (steal >= cputime)
537 		return;
538 
539 	cputime -= steal;
540 	account_idle_time(cputime);
541 }
542 
543 /*
544  * Adjust tick based cputime random precision against scheduler runtime
545  * accounting.
546  *
547  * Tick based cputime accounting depend on random scheduling timeslices of a
548  * task to be interrupted or not by the timer.  Depending on these
549  * circumstances, the number of these interrupts may be over or
550  * under-optimistic, matching the real user and system cputime with a variable
551  * precision.
552  *
553  * Fix this by scaling these tick based values against the total runtime
554  * accounted by the CFS scheduler.
555  *
556  * This code provides the following guarantees:
557  *
558  *   stime + utime == rtime
559  *   stime_i+1 >= stime_i, utime_i+1 >= utime_i
560  *
561  * Assuming that rtime_i+1 >= rtime_i.
562  */
cputime_adjust(struct task_cputime * curr,struct prev_cputime * prev,u64 * ut,u64 * st)563 void cputime_adjust(struct task_cputime *curr, struct prev_cputime *prev,
564 		    u64 *ut, u64 *st)
565 {
566 	u64 rtime, stime, utime;
567 	unsigned long flags;
568 
569 	/* Serialize concurrent callers such that we can honour our guarantees */
570 	raw_spin_lock_irqsave(&prev->lock, flags);
571 	rtime = curr->sum_exec_runtime;
572 
573 	/*
574 	 * This is possible under two circumstances:
575 	 *  - rtime isn't monotonic after all (a bug);
576 	 *  - we got reordered by the lock.
577 	 *
578 	 * In both cases this acts as a filter such that the rest of the code
579 	 * can assume it is monotonic regardless of anything else.
580 	 */
581 	if (prev->stime + prev->utime >= rtime)
582 		goto out;
583 
584 	stime = curr->stime;
585 	utime = curr->utime;
586 
587 	/*
588 	 * If either stime or utime are 0, assume all runtime is userspace.
589 	 * Once a task gets some ticks, the monotonicity code at 'update:'
590 	 * will ensure things converge to the observed ratio.
591 	 */
592 	if (stime == 0) {
593 		utime = rtime;
594 		goto update;
595 	}
596 
597 	if (utime == 0) {
598 		stime = rtime;
599 		goto update;
600 	}
601 
602 	stime = mul_u64_u64_div_u64(stime, rtime, stime + utime);
603 
604 update:
605 	/*
606 	 * Make sure stime doesn't go backwards; this preserves monotonicity
607 	 * for utime because rtime is monotonic.
608 	 *
609 	 *  utime_i+1 = rtime_i+1 - stime_i
610 	 *            = rtime_i+1 - (rtime_i - utime_i)
611 	 *            = (rtime_i+1 - rtime_i) + utime_i
612 	 *            >= utime_i
613 	 */
614 	if (stime < prev->stime)
615 		stime = prev->stime;
616 	utime = rtime - stime;
617 
618 	/*
619 	 * Make sure utime doesn't go backwards; this still preserves
620 	 * monotonicity for stime, analogous argument to above.
621 	 */
622 	if (utime < prev->utime) {
623 		utime = prev->utime;
624 		stime = rtime - utime;
625 	}
626 
627 	prev->stime = stime;
628 	prev->utime = utime;
629 out:
630 	*ut = prev->utime;
631 	*st = prev->stime;
632 	raw_spin_unlock_irqrestore(&prev->lock, flags);
633 }
634 
task_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)635 void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
636 {
637 	struct task_cputime cputime = {
638 		.sum_exec_runtime = p->se.sum_exec_runtime,
639 	};
640 
641 	task_cputime(p, &cputime.utime, &cputime.stime);
642 	cputime_adjust(&cputime, &p->prev_cputime, ut, st);
643 }
644 EXPORT_SYMBOL_GPL(task_cputime_adjusted);
645 
thread_group_cputime_adjusted(struct task_struct * p,u64 * ut,u64 * st)646 void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
647 {
648 	struct task_cputime cputime;
649 
650 	thread_group_cputime(p, &cputime);
651 	cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
652 }
653 EXPORT_SYMBOL_GPL(thread_group_cputime_adjusted);
654 
655 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
656 
657 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
vtime_delta(struct vtime * vtime)658 static u64 vtime_delta(struct vtime *vtime)
659 {
660 	unsigned long long clock;
661 
662 	clock = sched_clock();
663 	if (clock < vtime->starttime)
664 		return 0;
665 
666 	return clock - vtime->starttime;
667 }
668 
get_vtime_delta(struct vtime * vtime)669 static u64 get_vtime_delta(struct vtime *vtime)
670 {
671 	u64 delta = vtime_delta(vtime);
672 	u64 other;
673 
674 	/*
675 	 * Unlike tick based timing, vtime based timing never has lost
676 	 * ticks, and no need for steal time accounting to make up for
677 	 * lost ticks. Vtime accounts a rounded version of actual
678 	 * elapsed time. Limit account_other_time to prevent rounding
679 	 * errors from causing elapsed vtime to go negative.
680 	 */
681 	other = account_other_time(delta);
682 	WARN_ON_ONCE(vtime->state == VTIME_INACTIVE);
683 	vtime->starttime += delta;
684 
685 	return delta - other;
686 }
687 
vtime_account_system(struct task_struct * tsk,struct vtime * vtime)688 static void vtime_account_system(struct task_struct *tsk,
689 				 struct vtime *vtime)
690 {
691 	vtime->stime += get_vtime_delta(vtime);
692 	if (vtime->stime >= TICK_NSEC) {
693 		account_system_time(tsk, irq_count(), vtime->stime);
694 		vtime->stime = 0;
695 	}
696 }
697 
vtime_account_guest(struct task_struct * tsk,struct vtime * vtime)698 static void vtime_account_guest(struct task_struct *tsk,
699 				struct vtime *vtime)
700 {
701 	vtime->gtime += get_vtime_delta(vtime);
702 	if (vtime->gtime >= TICK_NSEC) {
703 		account_guest_time(tsk, vtime->gtime);
704 		vtime->gtime = 0;
705 	}
706 }
707 
__vtime_account_kernel(struct task_struct * tsk,struct vtime * vtime)708 static void __vtime_account_kernel(struct task_struct *tsk,
709 				   struct vtime *vtime)
710 {
711 	/* We might have scheduled out from guest path */
712 	if (vtime->state == VTIME_GUEST)
713 		vtime_account_guest(tsk, vtime);
714 	else
715 		vtime_account_system(tsk, vtime);
716 }
717 
vtime_account_kernel(struct task_struct * tsk)718 void vtime_account_kernel(struct task_struct *tsk)
719 {
720 	struct vtime *vtime = &tsk->vtime;
721 
722 	if (!vtime_delta(vtime))
723 		return;
724 
725 	write_seqcount_begin(&vtime->seqcount);
726 	__vtime_account_kernel(tsk, vtime);
727 	write_seqcount_end(&vtime->seqcount);
728 }
729 
vtime_user_enter(struct task_struct * tsk)730 void vtime_user_enter(struct task_struct *tsk)
731 {
732 	struct vtime *vtime = &tsk->vtime;
733 
734 	write_seqcount_begin(&vtime->seqcount);
735 	vtime_account_system(tsk, vtime);
736 	vtime->state = VTIME_USER;
737 	write_seqcount_end(&vtime->seqcount);
738 }
739 
vtime_user_exit(struct task_struct * tsk)740 void vtime_user_exit(struct task_struct *tsk)
741 {
742 	struct vtime *vtime = &tsk->vtime;
743 
744 	write_seqcount_begin(&vtime->seqcount);
745 	vtime->utime += get_vtime_delta(vtime);
746 	if (vtime->utime >= TICK_NSEC) {
747 		account_user_time(tsk, vtime->utime);
748 		vtime->utime = 0;
749 	}
750 	vtime->state = VTIME_SYS;
751 	write_seqcount_end(&vtime->seqcount);
752 }
753 
vtime_guest_enter(struct task_struct * tsk)754 void vtime_guest_enter(struct task_struct *tsk)
755 {
756 	struct vtime *vtime = &tsk->vtime;
757 	/*
758 	 * The flags must be updated under the lock with
759 	 * the vtime_starttime flush and update.
760 	 * That enforces a right ordering and update sequence
761 	 * synchronization against the reader (task_gtime())
762 	 * that can thus safely catch up with a tickless delta.
763 	 */
764 	write_seqcount_begin(&vtime->seqcount);
765 	vtime_account_system(tsk, vtime);
766 	tsk->flags |= PF_VCPU;
767 	vtime->state = VTIME_GUEST;
768 	write_seqcount_end(&vtime->seqcount);
769 }
770 EXPORT_SYMBOL_GPL(vtime_guest_enter);
771 
vtime_guest_exit(struct task_struct * tsk)772 void vtime_guest_exit(struct task_struct *tsk)
773 {
774 	struct vtime *vtime = &tsk->vtime;
775 
776 	write_seqcount_begin(&vtime->seqcount);
777 	vtime_account_guest(tsk, vtime);
778 	tsk->flags &= ~PF_VCPU;
779 	vtime->state = VTIME_SYS;
780 	write_seqcount_end(&vtime->seqcount);
781 }
782 EXPORT_SYMBOL_GPL(vtime_guest_exit);
783 
vtime_account_idle(struct task_struct * tsk)784 void vtime_account_idle(struct task_struct *tsk)
785 {
786 	account_idle_time(get_vtime_delta(&tsk->vtime));
787 }
788 
vtime_task_switch_generic(struct task_struct * prev)789 void vtime_task_switch_generic(struct task_struct *prev)
790 {
791 	struct vtime *vtime = &prev->vtime;
792 
793 	write_seqcount_begin(&vtime->seqcount);
794 	if (vtime->state == VTIME_IDLE)
795 		vtime_account_idle(prev);
796 	else
797 		__vtime_account_kernel(prev, vtime);
798 	vtime->state = VTIME_INACTIVE;
799 	vtime->cpu = -1;
800 	write_seqcount_end(&vtime->seqcount);
801 
802 	vtime = &current->vtime;
803 
804 	write_seqcount_begin(&vtime->seqcount);
805 	if (is_idle_task(current))
806 		vtime->state = VTIME_IDLE;
807 	else if (current->flags & PF_VCPU)
808 		vtime->state = VTIME_GUEST;
809 	else
810 		vtime->state = VTIME_SYS;
811 	vtime->starttime = sched_clock();
812 	vtime->cpu = smp_processor_id();
813 	write_seqcount_end(&vtime->seqcount);
814 }
815 
vtime_init_idle(struct task_struct * t,int cpu)816 void vtime_init_idle(struct task_struct *t, int cpu)
817 {
818 	struct vtime *vtime = &t->vtime;
819 	unsigned long flags;
820 
821 	local_irq_save(flags);
822 	write_seqcount_begin(&vtime->seqcount);
823 	vtime->state = VTIME_IDLE;
824 	vtime->starttime = sched_clock();
825 	vtime->cpu = cpu;
826 	write_seqcount_end(&vtime->seqcount);
827 	local_irq_restore(flags);
828 }
829 
task_gtime(struct task_struct * t)830 u64 task_gtime(struct task_struct *t)
831 {
832 	struct vtime *vtime = &t->vtime;
833 	unsigned int seq;
834 	u64 gtime;
835 
836 	if (!vtime_accounting_enabled())
837 		return t->gtime;
838 
839 	do {
840 		seq = read_seqcount_begin(&vtime->seqcount);
841 
842 		gtime = t->gtime;
843 		if (vtime->state == VTIME_GUEST)
844 			gtime += vtime->gtime + vtime_delta(vtime);
845 
846 	} while (read_seqcount_retry(&vtime->seqcount, seq));
847 
848 	return gtime;
849 }
850 
851 /*
852  * Fetch cputime raw values from fields of task_struct and
853  * add up the pending nohz execution time since the last
854  * cputime snapshot.
855  */
task_cputime(struct task_struct * t,u64 * utime,u64 * stime)856 void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
857 {
858 	struct vtime *vtime = &t->vtime;
859 	unsigned int seq;
860 	u64 delta;
861 
862 	if (!vtime_accounting_enabled()) {
863 		*utime = t->utime;
864 		*stime = t->stime;
865 		return;
866 	}
867 
868 	do {
869 		seq = read_seqcount_begin(&vtime->seqcount);
870 
871 		*utime = t->utime;
872 		*stime = t->stime;
873 
874 		/* Task is sleeping or idle, nothing to add */
875 		if (vtime->state < VTIME_SYS)
876 			continue;
877 
878 		delta = vtime_delta(vtime);
879 
880 		/*
881 		 * Task runs either in user (including guest) or kernel space,
882 		 * add pending nohz time to the right place.
883 		 */
884 		if (vtime->state == VTIME_SYS)
885 			*stime += vtime->stime + delta;
886 		else
887 			*utime += vtime->utime + delta;
888 	} while (read_seqcount_retry(&vtime->seqcount, seq));
889 }
890 
vtime_state_fetch(struct vtime * vtime,int cpu)891 static int vtime_state_fetch(struct vtime *vtime, int cpu)
892 {
893 	int state = READ_ONCE(vtime->state);
894 
895 	/*
896 	 * We raced against a context switch, fetch the
897 	 * kcpustat task again.
898 	 */
899 	if (vtime->cpu != cpu && vtime->cpu != -1)
900 		return -EAGAIN;
901 
902 	/*
903 	 * Two possible things here:
904 	 * 1) We are seeing the scheduling out task (prev) or any past one.
905 	 * 2) We are seeing the scheduling in task (next) but it hasn't
906 	 *    passed though vtime_task_switch() yet so the pending
907 	 *    cputime of the prev task may not be flushed yet.
908 	 *
909 	 * Case 1) is ok but 2) is not. So wait for a safe VTIME state.
910 	 */
911 	if (state == VTIME_INACTIVE)
912 		return -EAGAIN;
913 
914 	return state;
915 }
916 
kcpustat_user_vtime(struct vtime * vtime)917 static u64 kcpustat_user_vtime(struct vtime *vtime)
918 {
919 	if (vtime->state == VTIME_USER)
920 		return vtime->utime + vtime_delta(vtime);
921 	else if (vtime->state == VTIME_GUEST)
922 		return vtime->gtime + vtime_delta(vtime);
923 	return 0;
924 }
925 
kcpustat_field_vtime(u64 * cpustat,struct task_struct * tsk,enum cpu_usage_stat usage,int cpu,u64 * val)926 static int kcpustat_field_vtime(u64 *cpustat,
927 				struct task_struct *tsk,
928 				enum cpu_usage_stat usage,
929 				int cpu, u64 *val)
930 {
931 	struct vtime *vtime = &tsk->vtime;
932 	unsigned int seq;
933 
934 	do {
935 		int state;
936 
937 		seq = read_seqcount_begin(&vtime->seqcount);
938 
939 		state = vtime_state_fetch(vtime, cpu);
940 		if (state < 0)
941 			return state;
942 
943 		*val = cpustat[usage];
944 
945 		/*
946 		 * Nice VS unnice cputime accounting may be inaccurate if
947 		 * the nice value has changed since the last vtime update.
948 		 * But proper fix would involve interrupting target on nice
949 		 * updates which is a no go on nohz_full (although the scheduler
950 		 * may still interrupt the target if rescheduling is needed...)
951 		 */
952 		switch (usage) {
953 		case CPUTIME_SYSTEM:
954 			if (state == VTIME_SYS)
955 				*val += vtime->stime + vtime_delta(vtime);
956 			break;
957 		case CPUTIME_USER:
958 			if (task_nice(tsk) <= 0)
959 				*val += kcpustat_user_vtime(vtime);
960 			break;
961 		case CPUTIME_NICE:
962 			if (task_nice(tsk) > 0)
963 				*val += kcpustat_user_vtime(vtime);
964 			break;
965 		case CPUTIME_GUEST:
966 			if (state == VTIME_GUEST && task_nice(tsk) <= 0)
967 				*val += vtime->gtime + vtime_delta(vtime);
968 			break;
969 		case CPUTIME_GUEST_NICE:
970 			if (state == VTIME_GUEST && task_nice(tsk) > 0)
971 				*val += vtime->gtime + vtime_delta(vtime);
972 			break;
973 		default:
974 			break;
975 		}
976 	} while (read_seqcount_retry(&vtime->seqcount, seq));
977 
978 	return 0;
979 }
980 
kcpustat_field(struct kernel_cpustat * kcpustat,enum cpu_usage_stat usage,int cpu)981 u64 kcpustat_field(struct kernel_cpustat *kcpustat,
982 		   enum cpu_usage_stat usage, int cpu)
983 {
984 	u64 *cpustat = kcpustat->cpustat;
985 	u64 val = cpustat[usage];
986 	struct rq *rq;
987 	int err;
988 
989 	if (!vtime_accounting_enabled_cpu(cpu))
990 		return val;
991 
992 	rq = cpu_rq(cpu);
993 
994 	for (;;) {
995 		struct task_struct *curr;
996 
997 		rcu_read_lock();
998 		curr = rcu_dereference(rq->curr);
999 		if (WARN_ON_ONCE(!curr)) {
1000 			rcu_read_unlock();
1001 			return cpustat[usage];
1002 		}
1003 
1004 		err = kcpustat_field_vtime(cpustat, curr, usage, cpu, &val);
1005 		rcu_read_unlock();
1006 
1007 		if (!err)
1008 			return val;
1009 
1010 		cpu_relax();
1011 	}
1012 }
1013 EXPORT_SYMBOL_GPL(kcpustat_field);
1014 
kcpustat_cpu_fetch_vtime(struct kernel_cpustat * dst,const struct kernel_cpustat * src,struct task_struct * tsk,int cpu)1015 static int kcpustat_cpu_fetch_vtime(struct kernel_cpustat *dst,
1016 				    const struct kernel_cpustat *src,
1017 				    struct task_struct *tsk, int cpu)
1018 {
1019 	struct vtime *vtime = &tsk->vtime;
1020 	unsigned int seq;
1021 
1022 	do {
1023 		u64 *cpustat;
1024 		u64 delta;
1025 		int state;
1026 
1027 		seq = read_seqcount_begin(&vtime->seqcount);
1028 
1029 		state = vtime_state_fetch(vtime, cpu);
1030 		if (state < 0)
1031 			return state;
1032 
1033 		*dst = *src;
1034 		cpustat = dst->cpustat;
1035 
1036 		/* Task is sleeping, dead or idle, nothing to add */
1037 		if (state < VTIME_SYS)
1038 			continue;
1039 
1040 		delta = vtime_delta(vtime);
1041 
1042 		/*
1043 		 * Task runs either in user (including guest) or kernel space,
1044 		 * add pending nohz time to the right place.
1045 		 */
1046 		if (state == VTIME_SYS) {
1047 			cpustat[CPUTIME_SYSTEM] += vtime->stime + delta;
1048 		} else if (state == VTIME_USER) {
1049 			if (task_nice(tsk) > 0)
1050 				cpustat[CPUTIME_NICE] += vtime->utime + delta;
1051 			else
1052 				cpustat[CPUTIME_USER] += vtime->utime + delta;
1053 		} else {
1054 			WARN_ON_ONCE(state != VTIME_GUEST);
1055 			if (task_nice(tsk) > 0) {
1056 				cpustat[CPUTIME_GUEST_NICE] += vtime->gtime + delta;
1057 				cpustat[CPUTIME_NICE] += vtime->gtime + delta;
1058 			} else {
1059 				cpustat[CPUTIME_GUEST] += vtime->gtime + delta;
1060 				cpustat[CPUTIME_USER] += vtime->gtime + delta;
1061 			}
1062 		}
1063 	} while (read_seqcount_retry(&vtime->seqcount, seq));
1064 
1065 	return 0;
1066 }
1067 
kcpustat_cpu_fetch(struct kernel_cpustat * dst,int cpu)1068 void kcpustat_cpu_fetch(struct kernel_cpustat *dst, int cpu)
1069 {
1070 	const struct kernel_cpustat *src = &kcpustat_cpu(cpu);
1071 	struct rq *rq;
1072 	int err;
1073 
1074 	if (!vtime_accounting_enabled_cpu(cpu)) {
1075 		*dst = *src;
1076 		return;
1077 	}
1078 
1079 	rq = cpu_rq(cpu);
1080 
1081 	for (;;) {
1082 		struct task_struct *curr;
1083 
1084 		rcu_read_lock();
1085 		curr = rcu_dereference(rq->curr);
1086 		if (WARN_ON_ONCE(!curr)) {
1087 			rcu_read_unlock();
1088 			*dst = *src;
1089 			return;
1090 		}
1091 
1092 		err = kcpustat_cpu_fetch_vtime(dst, src, curr, cpu);
1093 		rcu_read_unlock();
1094 
1095 		if (!err)
1096 			return;
1097 
1098 		cpu_relax();
1099 	}
1100 }
1101 EXPORT_SYMBOL_GPL(kcpustat_cpu_fetch);
1102 
1103 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
1104