[PATCH] sched/cputime: Account for softirq preemption on PREEMPT_RT
From: Yiyang Chen
Date: Tue Dec 01 2026 - 13:06:56 EST
IRQ time accounting uses a per-CPU timestamp and assumes that a task
serving a softirq cannot be scheduled out. On PREEMPT_RT, softirq
execution is preemptible, so another task can run on the CPU without
an intervening irqtime update.
If the softirq task resumes without an intervening hardirq or softirq,
time spent in the other task is charged to CPUTIME_SOFTIRQ. Conversely,
if a hardirq arrives while the other task runs, IRQ entry sees that the
interrupted task is not serving a softirq and advances the timestamp
without charging CPUTIME_SOFTIRQ, losing the prior softirq time.
Account the elapsed softirq time on entry to __schedule(), before
update_rq_clock() reads the IRQ time total. Advance the timestamp
before local interrupts are re-enabled when resuming a task serving a
softirq. Do this also when the scheduler picks the same task, so that
time spent selecting it is excluded from softirq time.
Fixes: 8b1c04acad08 ("softirq: Make softirq control and processing RT aware")
Signed-off-by: Yiyang Chen <cyyzero16@xxxxxxxxx>
---
include/linux/vtime.h | 17 +++++++++++++++++
kernel/sched/core.c | 3 +++
2 files changed, 20 insertions(+)
diff --git a/include/linux/vtime.h b/include/linux/vtime.h
index 82825e775499..d8fe56bc9af6 100644
--- a/include/linux/vtime.h
+++ b/include/linux/vtime.h
@@ -176,4 +176,21 @@ static inline void account_hardirq_exit(struct task_struct *tsk)
irqtime_account_irq(tsk, 0);
}
+#ifdef CONFIG_PREEMPT_RT
+static inline void account_softirq_sched_out(void)
+{
+ if (unlikely(in_serving_softirq()))
+ account_softirq_exit(current);
+}
+
+static inline void account_softirq_sched_in(void)
+{
+ if (unlikely(in_serving_softirq()))
+ account_softirq_enter(current);
+}
+#else
+static inline void account_softirq_sched_out(void) { }
+static inline void account_softirq_sched_in(void) { }
+#endif
+
#endif /* _LINUX_KERNEL_VTIME_H */
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 5ed660f099a5..13cf84e7a83d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5430,6 +5430,7 @@ static struct rq *finish_task_switch(struct task_struct *prev)
perf_event_task_sched_in(prev, current);
finish_task(prev);
tick_nohz_task_switch();
+ account_softirq_sched_in();
finish_lock_switch(rq);
finish_arch_post_lock_switch();
kcov_finish_switch(current);
@@ -7235,6 +7236,7 @@ static void __sched notrace __schedule(int sched_mode)
klp_sched_try_switch(prev);
local_irq_disable();
+ account_softirq_sched_out();
rcu_note_context_switch(preempt);
migrate_disable_switch(rq, prev);
@@ -7386,6 +7388,7 @@ static void __sched notrace __schedule(int sched_mode)
rq = context_switch(rq, prev, next, &rf);
} else {
rq_unpin_lock(rq, &rf);
+ account_softirq_sched_in();
__balance_callbacks(rq, NULL);
hrtick_schedule_exit(rq);
raw_spin_rq_unlock_irq(rq);
--
2.43.0