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/sched/cputime.h>
10 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
13 * There are no locks covering percpu hardirq/softirq time.
14 * They are only modified in vtime_account, on corresponding CPU
15 * with interrupts disabled. So, writes are safe.
16 * They are read and saved off onto struct rq in update_rq_clock().
17 * This may result in other CPU reading this CPU's irq time and can
18 * race with irq/vtime_account on this CPU. We would either get old
19 * or new value with a side effect of accounting a slice of irq time to wrong
20 * task when irq is in progress while we read rq->clock. That is a worthy
21 * compromise in place of having locks on each irq in account_system_time.
23 DEFINE_PER_CPU(struct irqtime, cpu_irqtime);
25 static int sched_clock_irqtime;
27 void enable_sched_clock_irqtime(void)
29 sched_clock_irqtime = 1;
32 void disable_sched_clock_irqtime(void)
34 sched_clock_irqtime = 0;
38 * Called before incrementing preempt_count on {soft,}irq_enter
39 * and before decrementing preempt_count on {soft,}irq_exit.
41 void irqtime_account_irq(struct task_struct *curr)
43 struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
44 u64 *cpustat = kcpustat_this_cpu->cpustat;
48 if (!sched_clock_irqtime)
51 cpu = smp_processor_id();
52 delta = sched_clock_cpu(cpu) - irqtime->irq_start_time;
53 irqtime->irq_start_time += delta;
55 u64_stats_update_begin(&irqtime->sync);
57 * We do not account for softirq time from ksoftirqd here.
58 * We want to continue accounting softirq time to ksoftirqd thread
59 * in that case, so as not to confuse scheduler with a special task
60 * that do not consume any time, but still wants to run.
62 if (hardirq_count()) {
63 cpustat[CPUTIME_IRQ] += delta;
64 irqtime->tick_delta += delta;
65 } else if (in_serving_softirq() && curr != this_cpu_ksoftirqd()) {
66 cpustat[CPUTIME_SOFTIRQ] += delta;
67 irqtime->tick_delta += delta;
70 u64_stats_update_end(&irqtime->sync);
72 EXPORT_SYMBOL_GPL(irqtime_account_irq);
74 static u64 irqtime_tick_accounted(u64 maxtime)
76 struct irqtime *irqtime = this_cpu_ptr(&cpu_irqtime);
79 delta = min(irqtime->tick_delta, maxtime);
80 irqtime->tick_delta -= delta;
85 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
87 #define sched_clock_irqtime (0)
89 static u64 irqtime_tick_accounted(u64 dummy)
94 #endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
96 static inline void task_group_account_field(struct task_struct *p, int index,
100 * Since all updates are sure to touch the root cgroup, we
101 * get ourselves ahead and touch it first. If the root cgroup
102 * is the only cgroup, then nothing else should be necessary.
105 __this_cpu_add(kernel_cpustat.cpustat[index], tmp);
107 cpuacct_account_field(p, index, tmp);
111 * Account user cpu time to a process.
112 * @p: the process that the cpu time gets accounted to
113 * @cputime: the cpu time spent in user space since the last update
115 void account_user_time(struct task_struct *p, u64 cputime)
119 /* Add user time to process. */
121 account_group_user_time(p, cputime);
123 index = (task_nice(p) > 0) ? CPUTIME_NICE : CPUTIME_USER;
125 /* Add user time to cpustat. */
126 task_group_account_field(p, index, cputime);
128 /* Account for user time used */
129 acct_account_cputime(p);
133 * Account guest cpu time to a process.
134 * @p: the process that the cpu time gets accounted to
135 * @cputime: the cpu time spent in virtual machine since the last update
137 void account_guest_time(struct task_struct *p, u64 cputime)
139 u64 *cpustat = kcpustat_this_cpu->cpustat;
141 /* Add guest time to process. */
143 account_group_user_time(p, cputime);
146 /* Add guest time to cpustat. */
147 if (task_nice(p) > 0) {
148 cpustat[CPUTIME_NICE] += cputime;
149 cpustat[CPUTIME_GUEST_NICE] += cputime;
151 cpustat[CPUTIME_USER] += cputime;
152 cpustat[CPUTIME_GUEST] += cputime;
157 * Account system cpu time to a process and desired cpustat field
158 * @p: the process that the cpu time gets accounted to
159 * @cputime: the cpu time spent in kernel space since the last update
160 * @index: pointer to cpustat field that has to be updated
162 void account_system_index_time(struct task_struct *p,
163 u64 cputime, enum cpu_usage_stat index)
165 /* Add system time to process. */
167 account_group_system_time(p, cputime);
169 /* Add system time to cpustat. */
170 task_group_account_field(p, index, cputime);
172 /* Account for system time used */
173 acct_account_cputime(p);
177 * Account system cpu time to a process.
178 * @p: the process that the cpu time gets accounted to
179 * @hardirq_offset: the offset to subtract from hardirq_count()
180 * @cputime: the cpu time spent in kernel space since the last update
182 void account_system_time(struct task_struct *p, int hardirq_offset, u64 cputime)
186 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
187 account_guest_time(p, cputime);
191 if (hardirq_count() - hardirq_offset)
193 else if (in_serving_softirq())
194 index = CPUTIME_SOFTIRQ;
196 index = CPUTIME_SYSTEM;
198 account_system_index_time(p, cputime, index);
202 * Account for involuntary wait time.
203 * @cputime: the cpu time spent in involuntary wait
205 void account_steal_time(u64 cputime)
207 u64 *cpustat = kcpustat_this_cpu->cpustat;
209 cpustat[CPUTIME_STEAL] += cputime;
213 * Account for idle time.
214 * @cputime: the cpu time spent in idle wait
216 void account_idle_time(u64 cputime)
218 u64 *cpustat = kcpustat_this_cpu->cpustat;
219 struct rq *rq = this_rq();
221 if (atomic_read(&rq->nr_iowait) > 0)
222 cpustat[CPUTIME_IOWAIT] += cputime;
224 cpustat[CPUTIME_IDLE] += cputime;
228 * When a guest is interrupted for a longer amount of time, missed clock
229 * ticks are not redelivered later. Due to that, this function may on
230 * occasion account more time than the calling functions think elapsed.
232 static __always_inline u64 steal_account_process_time(u64 maxtime)
234 #ifdef CONFIG_PARAVIRT
235 if (static_key_false(¶virt_steal_enabled)) {
238 steal = paravirt_steal_clock(smp_processor_id());
239 steal -= this_rq()->prev_steal_time;
240 steal = min(steal, maxtime);
241 account_steal_time(steal);
242 this_rq()->prev_steal_time += steal;
251 * Account how much elapsed time was spent in steal, irq, or softirq time.
253 static inline u64 account_other_time(u64 max)
257 /* Shall be converted to a lockdep-enabled lightweight check */
258 WARN_ON_ONCE(!irqs_disabled());
260 accounted = steal_account_process_time(max);
263 accounted += irqtime_tick_accounted(max - accounted);
269 static inline u64 read_sum_exec_runtime(struct task_struct *t)
271 return t->se.sum_exec_runtime;
274 static u64 read_sum_exec_runtime(struct task_struct *t)
280 rq = task_rq_lock(t, &rf);
281 ns = t->se.sum_exec_runtime;
282 task_rq_unlock(rq, t, &rf);
289 * Accumulate raw cputime values of dead tasks (sig->[us]time) and live
290 * tasks (sum on group iteration) belonging to @tsk's group.
292 void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
294 struct signal_struct *sig = tsk->signal;
296 struct task_struct *t;
297 unsigned int seq, nextseq;
301 * Update current task runtime to account pending time since last
302 * scheduler action or thread_group_cputime() call. This thread group
303 * might have other running tasks on different CPUs, but updating
304 * their runtime can affect syscall performance, so we skip account
305 * those pending times and rely only on values updated on tick or
306 * other scheduler action.
308 if (same_thread_group(current, tsk))
309 (void) task_sched_runtime(current);
312 /* Attempt a lockless read on the first round. */
316 flags = read_seqbegin_or_lock_irqsave(&sig->stats_lock, &seq);
317 times->utime = sig->utime;
318 times->stime = sig->stime;
319 times->sum_exec_runtime = sig->sum_sched_runtime;
321 for_each_thread(tsk, t) {
322 task_cputime(t, &utime, &stime);
323 times->utime += utime;
324 times->stime += stime;
325 times->sum_exec_runtime += read_sum_exec_runtime(t);
327 /* If lockless access failed, take the lock. */
329 } while (need_seqretry(&sig->stats_lock, seq));
330 done_seqretry_irqrestore(&sig->stats_lock, seq, flags);
334 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
336 * Account a tick to a process and cpustat
337 * @p: the process that the cpu time gets accounted to
338 * @user_tick: is the tick from userspace
339 * @rq: the pointer to rq
341 * Tick demultiplexing follows the order
342 * - pending hardirq update
343 * - pending softirq update
347 * - check for guest_time
348 * - else account as system_time
350 * Check for hardirq is done both for system and user time as there is
351 * no timer going off while we are on hardirq and hence we may never get an
352 * opportunity to update it solely in system time.
353 * p->stime and friends are only updated on system time and not on irq
354 * softirq as those do not count in task exec_runtime any more.
356 static void irqtime_account_process_tick(struct task_struct *p, int user_tick,
357 struct rq *rq, int ticks)
359 u64 other, cputime = TICK_NSEC * ticks;
362 * When returning from idle, many ticks can get accounted at
363 * once, including some ticks of steal, irq, and softirq time.
364 * Subtract those ticks from the amount of time accounted to
365 * idle, or potentially user or system time. Due to rounding,
366 * other time can exceed ticks occasionally.
368 other = account_other_time(ULONG_MAX);
369 if (other >= cputime)
374 if (this_cpu_ksoftirqd() == p) {
376 * ksoftirqd time do not get accounted in cpu_softirq_time.
377 * So, we have to handle it separately here.
378 * Also, p->stime needs to be updated for ksoftirqd.
380 account_system_index_time(p, cputime, CPUTIME_SOFTIRQ);
381 } else if (user_tick) {
382 account_user_time(p, cputime);
383 } else if (p == rq->idle) {
384 account_idle_time(cputime);
385 } else if (p->flags & PF_VCPU) { /* System time or guest time */
386 account_guest_time(p, cputime);
388 account_system_index_time(p, cputime, CPUTIME_SYSTEM);
392 static void irqtime_account_idle_ticks(int ticks)
394 struct rq *rq = this_rq();
396 irqtime_account_process_tick(current, 0, rq, ticks);
398 #else /* CONFIG_IRQ_TIME_ACCOUNTING */
399 static inline void irqtime_account_idle_ticks(int ticks) {}
400 static inline void irqtime_account_process_tick(struct task_struct *p, int user_tick,
401 struct rq *rq, int nr_ticks) {}
402 #endif /* CONFIG_IRQ_TIME_ACCOUNTING */
405 * Use precise platform statistics if available:
407 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
409 #ifndef __ARCH_HAS_VTIME_TASK_SWITCH
410 void vtime_common_task_switch(struct task_struct *prev)
412 if (is_idle_task(prev))
413 vtime_account_idle(prev);
415 vtime_account_system(prev);
418 arch_vtime_task_switch(prev);
422 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
425 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
427 * Archs that account the whole time spent in the idle task
428 * (outside irq) as idle time can rely on this and just implement
429 * vtime_account_system() and vtime_account_idle(). Archs that
430 * have other meaning of the idle time (s390 only includes the
431 * time spent by the CPU when it's in low power mode) must override
434 #ifndef __ARCH_HAS_VTIME_ACCOUNT
435 void vtime_account_irq_enter(struct task_struct *tsk)
437 if (!in_interrupt() && is_idle_task(tsk))
438 vtime_account_idle(tsk);
440 vtime_account_system(tsk);
442 EXPORT_SYMBOL_GPL(vtime_account_irq_enter);
443 #endif /* __ARCH_HAS_VTIME_ACCOUNT */
445 void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
450 EXPORT_SYMBOL_GPL(task_cputime_adjusted);
452 void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
454 struct task_cputime cputime;
456 thread_group_cputime(p, &cputime);
461 #else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
463 * Account a single tick of cpu time.
464 * @p: the process that the cpu time gets accounted to
465 * @user_tick: indicates if the tick is a user or a system tick
467 void account_process_tick(struct task_struct *p, int user_tick)
470 struct rq *rq = this_rq();
472 if (vtime_accounting_cpu_enabled())
475 if (sched_clock_irqtime) {
476 irqtime_account_process_tick(p, user_tick, rq, 1);
481 steal = steal_account_process_time(ULONG_MAX);
483 if (steal >= cputime)
489 account_user_time(p, cputime);
490 else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
491 account_system_time(p, HARDIRQ_OFFSET, cputime);
493 account_idle_time(cputime);
497 * Account multiple ticks of idle time.
498 * @ticks: number of stolen ticks
500 void account_idle_ticks(unsigned long ticks)
504 if (sched_clock_irqtime) {
505 irqtime_account_idle_ticks(ticks);
509 cputime = ticks * TICK_NSEC;
510 steal = steal_account_process_time(ULONG_MAX);
512 if (steal >= cputime)
516 account_idle_time(cputime);
520 * Perform (stime * rtime) / total, but avoid multiplication overflow by
521 * loosing precision when the numbers are big.
523 static u64 scale_stime(u64 stime, u64 rtime, u64 total)
528 /* Make sure "rtime" is the bigger of stime/rtime */
532 /* Make sure 'total' fits in 32 bits */
536 /* Does rtime (and thus stime) fit in 32 bits? */
540 /* Can we just balance rtime/stime rather than dropping bits? */
544 /* We can grow stime and shrink rtime and try to make them both fit */
550 /* We drop from rtime, it has more bits than stime */
556 * Make sure gcc understands that this is a 32x32->64 multiply,
557 * followed by a 64/32->64 divide.
559 scaled = div_u64((u64) (u32) stime * (u64) (u32) rtime, (u32)total);
564 * Adjust tick based cputime random precision against scheduler runtime
567 * Tick based cputime accounting depend on random scheduling timeslices of a
568 * task to be interrupted or not by the timer. Depending on these
569 * circumstances, the number of these interrupts may be over or
570 * under-optimistic, matching the real user and system cputime with a variable
573 * Fix this by scaling these tick based values against the total runtime
574 * accounted by the CFS scheduler.
576 * This code provides the following guarantees:
578 * stime + utime == rtime
579 * stime_i+1 >= stime_i, utime_i+1 >= utime_i
581 * Assuming that rtime_i+1 >= rtime_i.
583 static void cputime_adjust(struct task_cputime *curr,
584 struct prev_cputime *prev,
587 u64 rtime, stime, utime;
590 /* Serialize concurrent callers such that we can honour our guarantees */
591 raw_spin_lock_irqsave(&prev->lock, flags);
592 rtime = curr->sum_exec_runtime;
595 * This is possible under two circumstances:
596 * - rtime isn't monotonic after all (a bug);
597 * - we got reordered by the lock.
599 * In both cases this acts as a filter such that the rest of the code
600 * can assume it is monotonic regardless of anything else.
602 if (prev->stime + prev->utime >= rtime)
609 * If either stime or both stime and utime are 0, assume all runtime is
610 * userspace. Once a task gets some ticks, the monotonicy code at
611 * 'update' will ensure things converge to the observed ratio.
623 stime = scale_stime(stime, rtime, stime + utime);
627 * Make sure stime doesn't go backwards; this preserves monotonicity
628 * for utime because rtime is monotonic.
630 * utime_i+1 = rtime_i+1 - stime_i
631 * = rtime_i+1 - (rtime_i - utime_i)
632 * = (rtime_i+1 - rtime_i) + utime_i
635 if (stime < prev->stime)
637 utime = rtime - stime;
640 * Make sure utime doesn't go backwards; this still preserves
641 * monotonicity for stime, analogous argument to above.
643 if (utime < prev->utime) {
645 stime = rtime - utime;
653 raw_spin_unlock_irqrestore(&prev->lock, flags);
656 void task_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
658 struct task_cputime cputime = {
659 .sum_exec_runtime = p->se.sum_exec_runtime,
662 task_cputime(p, &cputime.utime, &cputime.stime);
663 cputime_adjust(&cputime, &p->prev_cputime, ut, st);
665 EXPORT_SYMBOL_GPL(task_cputime_adjusted);
667 void thread_group_cputime_adjusted(struct task_struct *p, u64 *ut, u64 *st)
669 struct task_cputime cputime;
671 thread_group_cputime(p, &cputime);
672 cputime_adjust(&cputime, &p->signal->prev_cputime, ut, st);
674 #endif /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
676 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
677 static u64 vtime_delta(struct task_struct *tsk)
679 unsigned long now = READ_ONCE(jiffies);
681 if (time_before(now, (unsigned long)tsk->vtime_snap))
684 return jiffies_to_nsecs(now - tsk->vtime_snap);
687 static u64 get_vtime_delta(struct task_struct *tsk)
689 unsigned long now = READ_ONCE(jiffies);
693 * Unlike tick based timing, vtime based timing never has lost
694 * ticks, and no need for steal time accounting to make up for
695 * lost ticks. Vtime accounts a rounded version of actual
696 * elapsed time. Limit account_other_time to prevent rounding
697 * errors from causing elapsed vtime to go negative.
699 delta = jiffies_to_nsecs(now - tsk->vtime_snap);
700 other = account_other_time(delta);
701 WARN_ON_ONCE(tsk->vtime_snap_whence == VTIME_INACTIVE);
702 tsk->vtime_snap = now;
704 return delta - other;
707 static void __vtime_account_system(struct task_struct *tsk)
709 account_system_time(tsk, irq_count(), get_vtime_delta(tsk));
712 void vtime_account_system(struct task_struct *tsk)
714 if (!vtime_delta(tsk))
717 write_seqcount_begin(&tsk->vtime_seqcount);
718 __vtime_account_system(tsk);
719 write_seqcount_end(&tsk->vtime_seqcount);
722 void vtime_account_user(struct task_struct *tsk)
724 write_seqcount_begin(&tsk->vtime_seqcount);
725 tsk->vtime_snap_whence = VTIME_SYS;
726 if (vtime_delta(tsk))
727 account_user_time(tsk, get_vtime_delta(tsk));
728 write_seqcount_end(&tsk->vtime_seqcount);
731 void vtime_user_enter(struct task_struct *tsk)
733 write_seqcount_begin(&tsk->vtime_seqcount);
734 if (vtime_delta(tsk))
735 __vtime_account_system(tsk);
736 tsk->vtime_snap_whence = VTIME_USER;
737 write_seqcount_end(&tsk->vtime_seqcount);
740 void vtime_guest_enter(struct task_struct *tsk)
743 * The flags must be updated under the lock with
744 * the vtime_snap flush and update.
745 * That enforces a right ordering and update sequence
746 * synchronization against the reader (task_gtime())
747 * that can thus safely catch up with a tickless delta.
749 write_seqcount_begin(&tsk->vtime_seqcount);
750 if (vtime_delta(tsk))
751 __vtime_account_system(tsk);
752 current->flags |= PF_VCPU;
753 write_seqcount_end(&tsk->vtime_seqcount);
755 EXPORT_SYMBOL_GPL(vtime_guest_enter);
757 void vtime_guest_exit(struct task_struct *tsk)
759 write_seqcount_begin(&tsk->vtime_seqcount);
760 __vtime_account_system(tsk);
761 current->flags &= ~PF_VCPU;
762 write_seqcount_end(&tsk->vtime_seqcount);
764 EXPORT_SYMBOL_GPL(vtime_guest_exit);
766 void vtime_account_idle(struct task_struct *tsk)
768 account_idle_time(get_vtime_delta(tsk));
771 void arch_vtime_task_switch(struct task_struct *prev)
773 write_seqcount_begin(&prev->vtime_seqcount);
774 prev->vtime_snap_whence = VTIME_INACTIVE;
775 write_seqcount_end(&prev->vtime_seqcount);
777 write_seqcount_begin(¤t->vtime_seqcount);
778 current->vtime_snap_whence = VTIME_SYS;
779 current->vtime_snap = jiffies;
780 write_seqcount_end(¤t->vtime_seqcount);
783 void vtime_init_idle(struct task_struct *t, int cpu)
787 local_irq_save(flags);
788 write_seqcount_begin(&t->vtime_seqcount);
789 t->vtime_snap_whence = VTIME_SYS;
790 t->vtime_snap = jiffies;
791 write_seqcount_end(&t->vtime_seqcount);
792 local_irq_restore(flags);
795 u64 task_gtime(struct task_struct *t)
800 if (!vtime_accounting_enabled())
804 seq = read_seqcount_begin(&t->vtime_seqcount);
807 if (t->vtime_snap_whence == VTIME_SYS && t->flags & PF_VCPU)
808 gtime += vtime_delta(t);
810 } while (read_seqcount_retry(&t->vtime_seqcount, seq));
816 * Fetch cputime raw values from fields of task_struct and
817 * add up the pending nohz execution time since the last
820 void task_cputime(struct task_struct *t, u64 *utime, u64 *stime)
825 if (!vtime_accounting_enabled()) {
832 seq = read_seqcount_begin(&t->vtime_seqcount);
837 /* Task is sleeping, nothing to add */
838 if (t->vtime_snap_whence == VTIME_INACTIVE || is_idle_task(t))
841 delta = vtime_delta(t);
844 * Task runs either in user or kernel space, add pending nohz time to
847 if (t->vtime_snap_whence == VTIME_USER || t->flags & PF_VCPU)
849 else if (t->vtime_snap_whence == VTIME_SYS)
851 } while (read_seqcount_retry(&t->vtime_seqcount, seq));
853 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */