[PATCH v5 3/6] hrtimer: Pause KCOV during deferred rearm
From: Karl Mehltretter
Date: Sat Nov 07 2026 - 08:17:49 EST
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.
With CONFIG_KCOV_SELFTEST added to x86_64 defconfig, the interrupt
selftest fails under QEMU, detecting spurious coverage in
__hrtimer_rearm_deferred(). The same happens on s390, RISC-V and LoongArch,
which also enable HRTIMER_REARM_DEFERRED.
Excluding the involved files instead would cost their coverage on real
task-context paths, e.g. the hrtimer and timekeeping syscalls.
Pause KCOV with kcov_pause_scope() in an __always_inline wrapper around
__hrtimer_rearm_deferred(), and map the wrapper directly to
__hrtimer_rearm_deferred() when CONFIG_KCOV=n. Use it at all call sites,
including hrtick_schedule_exit(). Callers that may run with KCOV enabled
for current are built without KCOV instrumentation or marked noinstr.
HAVE_NOINSTR_HACK covers pre-GCC-12 x86. The other affected architectures
restrict KCOV to GCC 12 or Clang through ARCH_WANTS_NO_INSTR. This avoids
relying on __no_sanitize_coverage, which is empty before GCC 12. Tested
with GCC 8.1 and 15 on x86_64.
A later patch also pauses __schedule(); keeping hrtick_schedule_exit()
guarded here makes the deferred-rearm fix independent of that scheduler
change.
Fixes: 15dd3a948855 ("hrtimer: Push reprogramming timers into the interrupt return path")
Signed-off-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
---
This patch depends on patch 2, "kcov: Add a kcov_pause guard".
include/linux/hrtimer_rearm.h | 23 +++++++++++++++++++++--
kernel/sched/core.c | 2 +-
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/include/linux/hrtimer_rearm.h b/include/linux/hrtimer_rearm.h
index 12e42721c8e0..b0fd7aaff538 100644
--- a/include/linux/hrtimer_rearm.h
+++ b/include/linux/hrtimer_rearm.h
@@ -12,6 +12,24 @@
void __hrtimer_rearm_deferred(void);
+#ifdef CONFIG_KCOV
+#include <linux/kcov.h>
+
+/*
+ * KCOV: Pause outside __hrtimer_rearm_deferred() to suppress entry coverage.
+ * Callers with KCOV enabled for current must be built without KCOV
+ * instrumentation.
+ */
+static __always_inline void hrtimer_rearm_deferred_kcov_paused(void)
+{
+ kcov_pause_scope();
+
+ __hrtimer_rearm_deferred();
+}
+#else
+#define hrtimer_rearm_deferred_kcov_paused() __hrtimer_rearm_deferred()
+#endif
+
/*
* This is purely CPU local, so check the TIF bit first to avoid the overhead of
* the atomic test_and_clear_bit() operation for the common case where the bit
@@ -43,7 +61,7 @@ hrtimer_rearm_deferred_user_irq(unsigned long *tif_work, const unsigned long tif
*/
if (unlikely((*tif_work & TIF_REARM_MASK) == _TIF_HRTIMER_REARM)) {
clear_thread_flag(TIF_HRTIMER_REARM);
- __hrtimer_rearm_deferred();
+ hrtimer_rearm_deferred_kcov_paused();
/* Don't go into the loop if HRTIMER_REARM was the only flag */
*tif_work &= ~_TIF_HRTIMER_REARM;
return !*tif_work;
@@ -55,7 +73,7 @@ hrtimer_rearm_deferred_user_irq(unsigned long *tif_work, const unsigned long tif
static __always_inline void hrtimer_rearm_deferred_tif(unsigned long tif_work)
{
if (hrtimer_test_and_clear_rearm_deferred_tif(tif_work))
- __hrtimer_rearm_deferred();
+ hrtimer_rearm_deferred_kcov_paused();
}
/*
@@ -78,6 +96,7 @@ static __always_inline bool hrtimer_test_and_clear_rearm_deferred(void)
#else /* CONFIG_HRTIMER_REARM_DEFERRED */
static __always_inline void __hrtimer_rearm_deferred(void) { }
+#define hrtimer_rearm_deferred_kcov_paused() __hrtimer_rearm_deferred()
static __always_inline void hrtimer_rearm_deferred(void) { }
static __always_inline void hrtimer_rearm_deferred_tif(unsigned long tif_work) { }
static __always_inline bool
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 04b62c16fa8c..3f13170c2939 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -1020,7 +1020,7 @@ static inline void hrtick_schedule_exit(struct rq *rq)
}
if (rq->hrtick_sched & HRTICK_SCHED_REARM_HRTIMER)
- __hrtimer_rearm_deferred();
+ hrtimer_rearm_deferred_kcov_paused();
rq->hrtick_sched = HRTICK_SCHED_NONE;
}
--
2.39.5