Linux real-time development
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Karl Mehltretter <kmehltretter@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Andrey Konovalov <andreyknvl@gmail.com>,
	Alexander Potapenko <glider@google.com>,
	Dmitry Vyukov <dvyukov@google.com>,
	Marco Elver <elver@google.com>,
	Bradley Morgan <include@grrlz.net>,
	Anna-Maria Behnsen <anna-maria@linutronix.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	Clark Williams <clrkwllms@kernel.org>,
	linux-rt-devel@lists.linux.dev, kasan-dev@googlegroups.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 3/6] hrtimer: Pause KCOV during deferred rearm
Date: Thu, 13 Aug 2026 15:08:26 +0200	[thread overview]
Message-ID: <20260813130826.GW687043@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <an1dEVKGlwbusXcS@gmail.com>

On Thu, Aug 13, 2026 at 07:59:57AM +0200, Karl Mehltretter wrote:
> On Wed, Aug 12, 2026 at 12:21:00PM +0100, Peter Zijlstra wrote:
> > > Deferred hrtimer rearm can run after HARDIRQ_OFFSET is dropped. in_task()
> > > is then true, so KCOV attributes the instrumented timer-reprogramming
> > > subtree to current.
> > 
> > But that is clearly noinstr code; there should be no kcov calls in
> > there.
> > 
> > If kcov is emitted inside noinstr, then kcov is a broken piece of crap
> > and needs to die.
> > 
> > NAK
> 
> Thanks for the review!
> 
> By "instrumented" I meant KCOV-instrumented. The selftest callback comes
> from __hrtimer_rearm_deferred() in ordinary .text, not .noinstr.text.
> 
> On x86, irq_exit_rcu() runs in an instrumentable IDT-entry region.
> __irq_exit_rcu() subtracts hardirq offset before calling

DEFINE_IDTENTRY_IRQ(func)
__visible noinstr void func(regs, error_code)
  run_irq_on_irqstack_cond(__func, regs, vector)
    irq_enter_rcu()
    func
    irq_exit_rcu()

Gah, that is the softirq thing and is indeed just inside the
instrumented code :-( My memory had all the preempt_count fiddling in
the noinst code.

> hrtimer_rearm_deferred(), so check_kcov_mode() sees in_task() and records
> callee coverage for current.
> 
> This is the same class of failure as 477d81a1c47a ("x86/entry: Remove
> unwanted instrumentation in common_interrupt()"). There the hardirq offset
> had not yet been added, here it has already been removed. Its callee
> could be inlined. 

That one was a lot simpler, it really wanted to be noinstr.

> Deferred rearm instead reaches shared hrtimer, tick,
> clockevent and architecture code. Statically excluding the graph
> would be pervasive and also lose coverage from ordinary task context.
> 
> Do you want deferred rearm and its complete call graph converted to
> noinstr, or merely built without KCOV instrumentation?

Bah, so the only reason this one pops is because it is outside of the
softirq code, same for those two wakeups I suppose.

Would something crazy like this work? That closes the holes in the
preempt_count munging around there.

*completely* untested and all that

---
diff --git a/kernel/softirq.c b/kernel/softirq.c
index 7980a4a232f9..42a1b24c4a0c 100644
--- a/kernel/softirq.c
+++ b/kernel/softirq.c
@@ -481,14 +481,14 @@ void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
 }
 EXPORT_SYMBOL(__local_bh_enable_ip);
 
-static inline void softirq_handle_begin(void)
+static inline void softirq_handle_begin(bool ksirqd)
 {
-	__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
+	__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET - (!ksirqd)*HARDIRQ_OFFSET);
 }
 
-static inline void softirq_handle_end(void)
+static inline void softirq_handle_end(bool ksirqd)
 {
-	__local_bh_enable(SOFTIRQ_OFFSET);
+	__local_bh_enable(SOFTIRQ_OFFSET - (!ksirqd)*HARDIRQ_OFFSET);
 	WARN_ON_ONCE(in_interrupt());
 }
 
@@ -618,7 +618,7 @@ static void handle_softirqs(bool ksirqd)
 
 	pending = local_softirq_pending();
 
-	softirq_handle_begin();
+	softirq_handle_begin(ksirqd);
 	in_hardirq = lockdep_softirq_start();
 	account_softirq_enter(current);
 
@@ -670,7 +670,7 @@ static void handle_softirqs(bool ksirqd)
 
 	account_softirq_exit(current);
 	lockdep_softirq_end(in_hardirq);
-	softirq_handle_end();
+	softirq_handle_end(ksirqd);
 	current_restore_flags(old_flags, PF_MEMALLOC);
 }
 
@@ -740,6 +740,9 @@ static inline void wake_timersd(void) { }
 
 #endif
 
+#define IRQ_EXIT_TIMERS  (NMI_MASK | HARDIRQ_MASK)
+#define IRQ_EXIT_SOFTIRQ (IRQ_EXIT_TIMERS | HARDIRQ_DISABLE_MASK | SOFTIRQ_MASK)
+
 static inline void __irq_exit_rcu(void)
 {
 #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
@@ -748,7 +751,6 @@ static inline void __irq_exit_rcu(void)
 	lockdep_assert_irqs_disabled();
 #endif
 	account_hardirq_exit(current);
-	preempt_count_sub(HARDIRQ_OFFSET);
 	/*
 	 * Interrupts may happen between hardirq_disable_enter() and
 	 * local_irq_save() in local_interrupt_disable(), if irq_exit() invokes
@@ -757,7 +759,7 @@ static inline void __irq_exit_rcu(void)
 	 * hardirq disabling count is already 1, hence we need to prevent
 	 * invoking softirq when a local_interrupt_disable() is ongoing.
 	 */
-	if (!in_interrupt() && !hardirq_disable_count() &&
+	if ((preempt_count() & IRQ_EXIT_SOFTIRQ) == HARDIRQ_OFFSET &&
 	    local_softirq_pending()) {
 		/*
 		 * If we left hrtimers unarmed, make sure to arm them now,
@@ -768,9 +770,11 @@ static inline void __irq_exit_rcu(void)
 	}
 
 	if (IS_ENABLED(CONFIG_IRQ_FORCED_THREADING) && force_irqthreads() &&
-	    local_timers_pending_force_th() && !(in_nmi() | in_hardirq()))
+	    local_timers_pending_force_th() &&
+	    (preempt_count() & IRQ_EXIT_TIMERS) == HARDIRQ_OFFSET)
 		wake_timersd();
 
+	preempt_count_sub(HARDIRQ_OFFSET);
 	tick_irq_exit();
 }
 

  reply	other threads:[~2026-08-13 13:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-11 15:41 [PATCH v2 0/6] kcov: Suppress timer and scheduler coverage leaks Karl Mehltretter
2026-08-11 15:41 ` [PATCH v2 1/6] kcov: Use unsigned int for kcov_start() mode parameter Karl Mehltretter
2026-08-11 15:41 ` [PATCH v2 2/6] kcov: Add a kcov_pause guard Karl Mehltretter
2026-08-11 15:41 ` [PATCH v2 3/6] hrtimer: Pause KCOV during deferred rearm Karl Mehltretter
2026-08-12 10:21   ` Peter Zijlstra
2026-08-13  5:59     ` Karl Mehltretter
2026-08-13 13:08       ` Peter Zijlstra [this message]
2026-08-11 15:41 ` [PATCH v2 4/6] sched/core: Pause KCOV in __schedule() Karl Mehltretter
2026-08-12 10:35   ` Peter Zijlstra
2026-08-11 15:41 ` [PATCH v2 5/6] sched/core: Pause KCOV in try_to_wake_up() Karl Mehltretter
2026-08-12 10:35   ` Peter Zijlstra
2026-08-11 15:41 ` [PATCH v2 6/6] sched/core: Pause KCOV in wake_up_new_task() Karl Mehltretter
2026-08-12 10:36   ` Peter Zijlstra
2026-08-11 19:59 ` [PATCH v2 0/6] kcov: Suppress timer and scheduler coverage leaks Bradley Morgan
2026-08-12 10:36   ` Peter Zijlstra

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=20260813130826.GW687043@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@gmail.com \
    --cc=anna-maria@linutronix.de \
    --cc=bigeasy@linutronix.de \
    --cc=bsegall@google.com \
    --cc=clrkwllms@kernel.org \
    --cc=dietmar.eggemann@arm.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=frederic@kernel.org \
    --cc=glider@google.com \
    --cc=include@grrlz.net \
    --cc=juri.lelli@redhat.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=kmehltretter@gmail.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=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