[PATCH 3/5] sched: pause KCOV in __schedule()
From: Karl Mehltretter
Date: Fri Aug 07 2026 - 16:51:57 EST
kernel/sched/ is not instrumented, but callees such as sched_clock(),
architecture CPU-capacity helpers and profile_hits() are.
During preemption and schedule() calls, instrumented callees can add
nondeterministic scheduler coverage to current.
With CONFIG_KCOV_SELFTEST, the interrupt selftest fails on x86_64
defconfig under QEMU, detecting spurious coverage in
arch_scale_cpu_capacity(). On arm64 the same class of leak appears in
sched_clock(), once the separate arm64 interrupt-accounting leak is
suppressed.
Annotating each callee would spread exclusions across architectures.
Pause across __schedule() instead, extending the scheduler exclusion to
its callees.
KCOV_PAUSED remains set while a task is switched out. Its resumed
__schedule() frame restores the prior state.
Fixes: 5c9a8750a640 ("kernel: add kcov code coverage")
Assisted-by: Claude:claude-opus-4-8
Assisted-by: Claude:claude-fable-5
Signed-off-by: Karl Mehltretter <kmehltretter@xxxxxxxxx>
---
kernel/sched/core.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index b6a8fbbdd538..d5663df6c702 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -7069,10 +7069,14 @@ static void __sched notrace __schedule(int sched_mode)
bool is_switch = false;
unsigned long *switch_count;
unsigned long prev_state;
+ unsigned int kcov_paused;
struct rq_flags rf;
struct rq *rq;
int cpu;
+ /* KCOV: sched/ is uninstrumented but the __schedule() callees are not. */
+ kcov_paused = kcov_pause(current);
+
/* Trace preemptions consistently with task switches */
trace_sched_entry_tp(sched_mode == SM_PREEMPT);
@@ -7239,6 +7243,7 @@ static void __sched notrace __schedule(int sched_mode)
raw_spin_rq_unlock_irq(rq);
}
trace_sched_exit_tp(is_switch);
+ kcov_resume(current, kcov_paused);
}
void __noreturn do_task_dead(void)
--
2.53.0