mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yiyang Chen <cyyzero16@gmail.com>
To: Ingo Molnar <mingo@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Clark Williams <clrkwllms@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Frederic Weisbecker <frederic@kernel.org>,
	Thomas Gleixner <tglx@kernel.org>
Cc: Yiyang Chen <cyyzero16@gmail.com>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev
Subject: [PATCH] sched/cputime: Account for softirq preemption on PREEMPT_RT
Date: Wed,  2 Dec 2026 02:06:42 +0800	[thread overview]
Message-ID: <61e7928eb0fd6d946528fd3d067e6a0446fd2b78.1796148059.git.cyyzero16@gmail.com> (raw)

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@gmail.com>
---
 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


                 reply	other threads:[~2026-12-01 18:06 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=61e7928eb0fd6d946528fd3d067e6a0446fd2b78.1796148059.git.cyyzero16@gmail.com \
    --to=cyyzero16@gmail.com \
    --cc=bigeasy@linutronix.de \
    --cc=bsegall@google.com \
    --cc=clrkwllms@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=frederic@kernel.org \
    --cc=juri.lelli@redhat.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®