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