kcov: Suppress timer and scheduler coverage leaks
Subsystems (3)
v2 cover kcov: Suppress timer and scheduler coverage leaks
KCOV aims to exclude interrupt and scheduler coverage so syscall coverage stays input-dependent. Instrumented callees can still record when uninstrumented timer and scheduler paths run with in_task() true.
With the diagnostic patch in [1] applied, CONFIG_KCOV_SELFTEST exposes three cases on x86-64: deferred hrtimer rearm, __schedule() callees and PREEMPT_RT wakeups. Task-context wakeups and new-task enqueue also add scheduler coverage to ordinary syscalls.
Add a nestable KCOV_PAUSED bit and a kcov_pause guard. Use the guard for deferred hrtimer rearm, __schedule(), the try_to_wake_up() wakeup body and wake_up_new_task(). This suppresses their instrumented callees without excluding those callees from task-context coverage.
Changes in v2:
- Add patch 1 to make kcov_start()'s mode parameter unsigned int. No functional change.
- Rework patch 2 around a guard-only API with private current-only helpers (Bradley Morgan).
- Use the guard in patches 3-6 and reword the pause comments.
v2 testing:
- GCC builds on x86-64, arm32, arm64, MIPS32/64, PowerPC 32/64, s390, RISC-V 32/64, LoongArch, Xtensa and UML.
- PREEMPT_RT builds on x86-64, arm32, arm64, RISC-V 32/64 and LoongArch.
- x86-64 builds with GCC 8.1 and Clang 22.1. Both kernels passed a KCOV selftest boot.
- x86-64 CONFIG_KCOV=n build, with no KCOV or pause references.
- KCOV selftest, 10/10 x86-64 boots with and without PREEMPT_RT. A fresh non-RT boot passed after the helper-only rework.
- 40 dummy_hcd/g_zero remote-KCOV cycles on x86-64 and arm64.
- 400 repeated fork() calls on x86-64 PREEMPT_RT.
Three one-hour syzkaller A/B pairs were run. Each baseline and patched run used four 2-vCPU PREEMPT_RT VMs. The patched kernel completed 22-51% more executions than base. At matched execution counts, corpus size grew 42-54% and coverage 14-19%. No run produced a report.
With KCOV disabled, the pause sections compile away. With KCOV enabled on x86-64, GCC 15.2 grows __schedule() by 117 bytes, try_to_wake_up() by 94 bytes and wake_up_new_task() by 88 bytes relative to the base commit.
[1] https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
Karl Mehltretter (6):
kcov: Use unsigned int for kcov_start() mode parameter
kcov: Add a kcov_pause guard
hrtimer: Pause KCOV during deferred rearm sched/core: Pause KCOV in __schedule() sched/core: Pause KCOV in try_to_wake_up() sched/core: Pause KCOV in wake_up_new_task()
include/linux/hrtimer_rearm.h | 17 +++++++++++++-- include/linux/kcov.h | 40 ++++++++++++++++++++++++++++++++++- kernel/kcov.c | 4 ++-- kernel/sched/core.c | 11 +++++++++- 4 files changed, 66 insertions(+), 6 deletions(-)
Range-diff:
-: ------------- > 1: f60b858edad96 kcov: Use unsigned int for kcov_start() mode parameter
1: a67095eb565de ! 2: 4415cac41ca43 kcov: add kcov_pause()/kcov_resume() helpers
@@ Metadata
Author: Karl Mehltretter <[email protected]>
## Commit message ##
- kcov: add kcov_pause()/kcov_resume() helpers
+ kcov: Add a kcov_pause guard
Interrupt-return work can run after HARDIRQ_OFFSET is dropped, when
in_task() is true. KCOV then attributes instrumented callees to the
interrupted task.
Add a KCOV_PAUSED bit next to KCOV_IN_CTXSW and mask both in
- kcov_mode_enabled(). The context switch suppression keeps its own bit:
- kcov_prepare_switch() runs on the previous task and kcov_finish_switch()
- on the one switched in, so its lifetime is not a pause section.
+ kcov_mode_enabled(). The coverage callbacks need no new check because
+ check_kcov_mode()'s exact comparison rejects modes with KCOV_PAUSED set.
- Sections nest by passing the state returned by kcov_pause() to
- kcov_resume(). Both operate on current. When task KCOV is active, remote
- softirq sections save and restore the complete mode, preserving the
- pause state.
+ The context switch suppression keeps its own bit: kcov_prepare_switch()
+ runs on the previous task and kcov_finish_switch() on the one switched
+ in, so its lifetime is not a pause section.
- The helpers are __always_inline, and the caller must be uninstrumented:
+ Provide a kcov_pause guard backed by internal helpers that operate on
+ current. The guard saves the previous pause state and restores it at
+ scope exit, so sections nest. When KCOV is enabled for current, remote
+ softirq sections save and restore the complete mode, preserving the pause
+ state.
+
+ The helpers are __always_inline, and guard users must be uninstrumented:
inlining does not remove the caller's own coverage callbacks.
- Assisted-by: Claude:claude-opus-4-8
Assisted-by: Claude:claude-fable-5
Signed-off-by: Karl Mehltretter <[email protected]>
@@ include/linux/kcov.h
#define _LINUX_KCOV_H
+#include <linux/bits.h>
++#include <linux/cleanup.h>
#include <linux/sched.h>
#include <uapi/linux/kcov.h>
@@ include/linux/kcov.h: do { \
} while (0)
+/*
-+ * Pause coverage for current. Pass the returned state to kcov_resume().
-+ * Callers must be uninstrumented.
++ * Pause coverage for current. Callers must be uninstrumented.
++ * Pass the returned state to __kcov_resume().
+ */
-+static __always_inline unsigned int kcov_pause(struct task_struct *t)
++static __always_inline unsigned int __kcov_pause(void)
+{
+ unsigned int paused;
+
-+ paused = t->kcov_mode & KCOV_PAUSED;
-+ t->kcov_mode |= KCOV_PAUSED;
++ paused = current->kcov_mode & KCOV_PAUSED;
++ current->kcov_mode |= KCOV_PAUSED;
+ return paused;
+}
+
-+static __always_inline void kcov_resume(struct task_struct *t, unsigned int paused)
++static __always_inline void __kcov_resume(unsigned int paused)
+{
+ if (!paused)
-+ t->kcov_mode &= ~KCOV_PAUSED;
++ current->kcov_mode &= ~KCOV_PAUSED;
+}
+
/* See Documentation/dev-tools/kcov.rst for usage details. */
@@ include/linux/kcov.h: void __sanitizer_cov_trace_switch(kcov_u64 val, void *case
static inline void kcov_task_init(struct task_struct *t) {}
static inline void kcov_task_exit(struct task_struct *t) {}
-+static inline unsigned int kcov_pause(struct task_struct *t) { return 0; }
-+static inline void kcov_resume(struct task_struct *t, unsigned int paused) {}
++static inline unsigned int __kcov_pause(void) { return 0; }
++static inline void __kcov_resume(unsigned int paused) {}
static inline void kcov_prepare_switch(struct task_struct *t) {}
static inline void kcov_finish_switch(struct task_struct *t) {}
static inline void kcov_remote_start(u64 handle) {}
+@@ include/linux/kcov.h: static inline void kcov_remote_start_usb_softirq(u64 id) {}
+ static inline void kcov_remote_stop_softirq(void) {}
+
+ #endif /* CONFIG_KCOV */
++
++/*
++ * Scope-based KCOV pause:
++ *
++ * guard(kcov_pause)();
++ *
++ * pauses coverage for current until the end of the scope. Callers must be
++ * uninstrumented.
++ */
++DEFINE_LOCK_GUARD_0(kcov_pause,
++ _T->paused = __kcov_pause(),
++ __kcov_resume(_T->paused),
++ unsigned int paused)
++
+ #endif /* _LINUX_KCOV_H */
## kernel/kcov.c ##
@@ kernel/kcov.c: static const struct file_operations kcov_fops = {2: 2d64b45a316c8 ! 3: c891839993a1e hrtimer: pause KCOV during deferred rearm
@@ Metadata
Author: Karl Mehltretter <[email protected]>
## Commit message ##
- hrtimer: pause KCOV during deferred rearm
+ hrtimer: Pause KCOV during deferred rearm
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, the interrupt selftest fails on x86_64
- defconfig under QEMU, detecting spurious coverage in
- __hrtimer_rearm_deferred(). The same happens on s390, RISC-V and
- LoongArch, which also enable HRTIMER_REARM_DEFERRED.
+ 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 in the __always_inline wrappers, including hrtick_schedule_exit().
- Call sites where task KCOV can be active are KCOV-disabled or 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.
+ Take the kcov_pause guard in an __always_inline wrapper around
+ __hrtimer_rearm_deferred(). 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.
- Assisted-by: Claude:claude-opus-4-8
@@ include/linux/hrtimer_rearm.h
void __hrtimer_rearm_deferred(void);
+/*
-+ * Pause outside __hrtimer_rearm_deferred() to suppress its entry coverage. -+ * Call sites where task KCOV can be active are uninstrumented.
++ * KCOV: Pause outside __hrtimer_rearm_deferred() to suppress entry coverage. ++ * Callers with KCOV enabled for current must be uninstrumented.
+ */
-+static __always_inline void hrtimer_rearm_deferred_paused(void) ++static __always_inline void hrtimer_rearm_deferred_kcov_paused(void)
+{
-+ unsigned int kcov_paused = kcov_pause(current);
++ guard(kcov_pause)();
+
+ __hrtimer_rearm_deferred();
-+ kcov_resume(current, kcov_paused);
+}
+
/*
@@ include/linux/hrtimer_rearm.h: hrtimer_rearm_deferred_user_irq(unsigned long *ti
if (unlikely((*tif_work & TIF_REARM_MASK) == _TIF_HRTIMER_REARM)) {
clear_thread_flag(TIF_HRTIMER_REARM);
- __hrtimer_rearm_deferred();
-+ hrtimer_rearm_deferred_paused();
++ 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;
@@ include/linux/hrtimer_rearm.h: hrtimer_rearm_deferred_user_irq(unsigned long *ti
{
if (hrtimer_test_and_clear_rearm_deferred_tif(tif_work))
- __hrtimer_rearm_deferred();
-+ hrtimer_rearm_deferred_paused();
++ hrtimer_rearm_deferred_kcov_paused();
}
/*
@@ include/linux/hrtimer_rearm.h: static __always_inline bool hrtimer_test_and_clea
#else /* CONFIG_HRTIMER_REARM_DEFERRED */
static __always_inline void __hrtimer_rearm_deferred(void) { }
-+static __always_inline void hrtimer_rearm_deferred_paused(void) { } ++static __always_inline void hrtimer_rearm_deferred_kcov_paused(void) { }
static __always_inline void hrtimer_rearm_deferred(void) { }
static __always_inline void hrtimer_rearm_deferred_tif(unsigned long tif_work) { } static __always_inline bool
@@ kernel/sched/core.c: static inline void hrtick_schedule_exit(struct rq *rq)
if (rq->hrtick_sched & HRTICK_SCHED_REARM_HRTIMER)
- __hrtimer_rearm_deferred();
-+ hrtimer_rearm_deferred_paused();
++ hrtimer_rearm_deferred_kcov_paused();
rq->hrtick_sched = HRTICK_SCHED_NONE;
}
3: dc4bdcf8adacd ! 4: 63657f2c7ef08 sched: pause KCOV in __schedule()
@@ Metadata
Author: Karl Mehltretter <[email protected]>
## Commit message ##
- sched: pause KCOV in __schedule()
+ sched/core: Pause KCOV in __schedule()
kernel/sched/ is not instrumented, but callees such as sched_clock(), architecture CPU-capacity helpers and profile_hits() are.
@@ Commit message
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.
+ With CONFIG_KCOV_SELFTEST added to x86_64 defconfig, the interrupt
+ selftest fails under QEMU, detecting spurious coverage in
+ arch_scale_cpu_capacity().
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.
+ KCOV_PAUSED remains set while a task is switched out. The guard in its
+ resumed __schedule() frame restores the prior state.
- Assisted-by: Claude:claude-opus-4-8
## kernel/sched/core.c ##
@@ kernel/sched/core.c: 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);
++ /* Instrumented callees would leak coverage into current. */
++ guard(kcov_pause)();
+
/* Trace preemptions consistently with task switches */ trace_sched_entry_tp(sched_mode == SM_PREEMPT);
-@@ kernel/sched/core.c: 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)
4: b8e96cc1903de ! 5: 5cf8497b8a0ab sched: pause KCOV in try_to_wake_up()
@@ Metadata
Author: Karl Mehltretter <[email protected]>
## Commit message ##
- sched: pause KCOV in try_to_wake_up()
+ sched/core: Pause KCOV in try_to_wake_up()
try_to_wake_up() is uninstrumented, but it calls instrumented helpers such as kthread_is_per_cpu(), CPU capacity helpers and SCHED_HRTICK
@@ Commit message
selftest's spin. The same helpers leak into non-RT syscall wakeups such as a pipe write waking a reader.
- Pause all of try_to_wake_up(). Wrapping only select_task_rq() would miss
- SCHED_HRTICK arming during enqueue.
+ Pause the wakeup body with the kcov_pause guard. Wrapping only
+ select_task_rq() would miss SCHED_HRTICK arming during enqueue.
- Assisted-by: Claude:claude-opus-4-8
+ Assisted-by: Claude:claude-fable-5
## kernel/sched/core.c ##
@@ kernel/sched/core.c: int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
- {
guard(preempt)();
int cpu, success = 0;
-+ /* KCOV: sched/ is uninstrumented but the wakeup callees are not. */
-+ unsigned int kcov_paused = kcov_pause(current);
++ /* Instrumented callees would leak coverage into current. */
++ guard(kcov_pause)();
++
wake_flags |= WF_TTWU;
-@@ kernel/sched/core.c: int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
- if (success)
- ttwu_stat(p, task_cpu(p), wake_flags);
-
-+ kcov_resume(current, kcov_paused);
- return success;
- }
-
+ if (p == current) {
5: f91a7644a15e8 ! 6: a00870853f5a1 sched: pause KCOV in wake_up_new_task()
@@ Metadata
Author: Karl Mehltretter <[email protected]>
## Commit message ##
- sched: pause KCOV in wake_up_new_task()
+ sched/core: Pause KCOV in wake_up_new_task()
wake_up_new_task() is uninstrumented, but CPU selection and enqueue call instrumented helpers. During a KCOV-enabled fork, they can record
@@ Commit message
scheduler exclusion to new-task wakeups.
- Assisted-by: Claude:claude-opus-4-8
+ Assisted-by: Claude:claude-fable-5
## kernel/sched/core.c ##
@@ kernel/sched/core.c: void wake_up_new_task(struct task_struct *p)
- {
- struct rq_flags rf;
struct rq *rq;
-+ unsigned int kcov_paused;
int wake_flags = WF_FORK;
-+ /* KCOV: sched/ is uninstrumented but the wakeup callees are not. */
-+ kcov_paused = kcov_pause(current);
++ /* Instrumented callees would leak coverage into current. */
++ guard(kcov_pause)();
+
raw_spin_lock_irqsave(&p->pi_lock, rf.flags); WRITE_ONCE(p->__state, TASK_RUNNING);
/*
-@@ kernel/sched/core.c: void wake_up_new_task(struct task_struct *p)
- rq_repin_lock(rq, &rf);
- }
- task_rq_unlock(rq, p, &rf);
-+ kcov_resume(current, kcov_paused);
- }
-
- #ifdef CONFIG_PREEMPT_NOTIFIERS
base-commit: 8ba098e6b6ff0db8edf28528d1552be261af30d4
Signature · 1 line
2.53.0
PATCH v2 1/6 kcov: Use unsigned int for kcov_start() mode parameter
task_struct::kcov_mode usually holds an enum kcov_mode value, but it can also carry flag bits such as KCOV_IN_CTXSW, so the field is unsigned int.
kcov_remote_softirq_stop() passes the saved raw value through kcov_start()'s enum kcov_mode parameter before kcov_start() stores it back into the unsigned int field. The parameter type therefore does not match the values it receives.
Type the parameter unsigned int, like the field and the saved copy. No functional change.
---
Notes:
v2: new patch
kernel/kcov.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/kcov.c +1 -1
@@ -354,7 +354,7 @@ EXPORT_SYMBOL(__sanitizer_cov_trace_switch);#endif /* ifdef CONFIG_KCOV_ENABLE_COMPARISONS */ static void kcov_start (struct task_struct *t,struct kcov *kcov, -unsigned int size,void *area,enum kcov_mode mode, +unsigned int size,void *area,unsigned int mode,int sequence) {kcov_debug ("t = %px, size = %u, area = %px\n" , t, size, area);
Signature · 1 line
2.53.0
Re: [PATCH v2 1/6] kcov: Use unsigned int for kcov_start() mode parameter
Quoted 9 lines · Karl Mehltretter
task_struct::kcov_modeusually holds an enumkcov_modevalue, but it can also carry flag bits such asKCOV_IN_CTXSW, so the field is unsigned int.
kcov_remote_softirq_stop()passes the saved raw value through kcov_start()'s enumkcov_modeparameter beforekcov_start()stores it back into the unsigned int field. The parameter type therefore does not match the values it receives.
Quoted 5 lines
Type the parameter unsigned int, like the field and the saved copy. No functional change.
Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>
PATCH v2 2/6 kcov: Add a kcov_pause guard
In reply to Karl MehltretterInterrupt-return work can run after HARDIRQ_OFFSET is dropped, when in_task() is true. KCOV then attributes instrumented callees to the interrupted task.
Add a KCOV_PAUSED bit next to KCOV_IN_CTXSW and mask both in kcov_mode_enabled(). The coverage callbacks need no new check because check_kcov_mode()'s exact comparison rejects modes with KCOV_PAUSED set.
The context switch suppression keeps its own bit: kcov_prepare_switch() runs on the previous task and kcov_finish_switch() on the one switched in, so its lifetime is not a pause section.
Provide a kcov_pause guard backed by internal helpers that operate on current. The guard saves the previous pause state and restores it at scope exit, so sections nest. When KCOV is enabled for current, remote softirq sections save and restore the complete mode, preserving the pause state.
The helpers are __always_inline, and guard users must be uninstrumented:
inlining does not remove the caller's own coverage callbacks.
---
Notes:
v2:
- add guard(kcov_pause)() backed by private current-only helpers
include/linux/kcov.h | 40 +++++++++++++++++++++++++++++++++++++++- kernel/kcov.c | 2 +- 2 files changed, 40 insertions(+), 2 deletions(-)
include/linux/kcov.h +39 -1
@@ -2,6 +2,8 @@#ifndef _LINUX_KCOV_H#define _LINUX_KCOV_H +#include <linux/bits.h> +#include <linux/cleanup.h> #include <linux/sched.h> #include <uapi/linux/kcov.h>
@@ -23,7 +25,8 @@ enum kcov_mode { KCOV_MODE_TRACE_CMP = 3, }; -#define KCOV_IN_CTXSW (1 << 30) +#define KCOV_IN_CTXSW BIT(30) +#define KCOV_PAUSED BIT(29)void kcov_task_init (struct task_struct *t);void kcov_task_exit (struct task_struct *t);
@@ -38,6 +41,25 @@ do { \ (t)->kcov_mode &= ~KCOV_IN_CTXSW ; \ }while (0 ) +/* +* Pause coverage for current. Callers must be uninstrumented. +* Pass the returned state to __kcov_resume(). +*/ +static __always_inline unsigned int __kcov_pause(void) +{ +unsigned int paused; + + paused = current->kcov_mode &KCOV_PAUSED ; + current->kcov_mode |=KCOV_PAUSED ; +return paused; +} + +static __always_inline void __kcov_resume(unsigned int paused) +{ +if (!paused) + current->kcov_mode &= ~KCOV_PAUSED ; +} +/* See Documentation/dev-tools/kcov.rst for usage details. */ void kcov_remote_start (u64 handle);void kcov_remote_stop (void );
@@ -93,6 +115,8 @@ void __sanitizer_cov_trace_switch(kcov_u64 val, void *cases);static inline void kcov_task_init (struct task_struct *t) {}static inline void kcov_task_exit (struct task_struct *t) {} +static inline unsigned int __kcov_pause (void ) {return 0 ; } +static inline void __kcov_resume (unsigned int paused) {}static inline void kcov_prepare_switch (struct task_struct *t) {}static inline void kcov_finish_switch (struct task_struct *t) {}static inline void kcov_remote_start (u64 handle) {}
@@ -107,4 +131,18 @@ static inline void kcov_remote_start_usb_softirq(u64 id) {}static inline void kcov_remote_stop_softirq (void ) {}#endif /* CONFIG_KCOV */ + +/* +* Scope-based KCOV pause: +* +* guard(kcov_pause)(); +* +* pauses coverage for current until the end of the scope. Callers must be +* uninstrumented. +*/ +DEFINE_LOCK_GUARD_0 (kcov_pause, + _T->paused =__kcov_pause (), +__kcov_resume (_T->paused), + unsigned int paused) +#endif /* _LINUX_KCOV_H */
kernel/kcov.c +1 -1
@@ -830,7 +830,7 @@ static const struct file_operations kcov_fops = {static inline bool kcov_mode_enabled (unsigned int mode) { -return (mode & ~KCOV_IN_CTXSW ) !=KCOV_MODE_DISABLED ; +return (mode & ~(KCOV_IN_CTXSW |KCOV_PAUSED )) !=KCOV_MODE_DISABLED ; }static void kcov_remote_softirq_start (struct task_struct *t)
Signature · 1 line
2.53.0
Alexander Potapenko
reviewerRe: [PATCH v2 2/6] kcov: Add a kcov_pause guard
Quoted 61 lines · Karl Mehltretter
Interrupt-return work can run after
HARDIRQ_OFFSETis dropped, whenin_task()is true. KCOV then attributes instrumented callees to the interrupted task.Add a
KCOV_PAUSEDbit next toKCOV_IN_CTXSWand mask both inkcov_mode_enabled(). The coverage callbacks need no new check because check_kcov_mode()'s exact comparison rejects modes withKCOV_PAUSEDset.The context switch suppression keeps its own bit:
kcov_prepare_switch()runs on the previous task andkcov_finish_switch()on the one switched in, so its lifetime is not a pause section.Provide a
kcov_pauseguard backed by internal helpers that operate on current. The guard saves the previous pause state and restores it at scope exit, so sections nest. When KCOV is enabled for current, remote softirq sections save and restore the complete mode, preserving the pause state.The helpers are
__always_inline, and guard users must be uninstrumented:
inlining does not remove the caller's own coverage callbacks.Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>---
Notes:
v2:
- add guard(kcov_pause)() backed by private current-only helpersinclude/linux/kcov.h | 40 +++++++++++++++++++++++++++++++++++++++- kernel/kcov.c | 2 +- 2 files changed, 40 insertions(+), 2 deletions(-)include/linux/kcov.h +5
-1@@ -2,6 +2,8 @@#ifndef _LINUX_KCOV_H#define _LINUX_KCOV_H +#include <linux/bits.h> +#include <linux/cleanup.h> #include <linux/sched.h> #include <uapi/linux/kcov.h> @@ -23,7 +25,8 @@ enum kcov_mode { KCOV_MODE_TRACE_CMP = 3, }; -#define KCOV_IN_CTXSW (1 << 30) +#define KCOV_IN_CTXSW BIT(30) +#define KCOV_PAUSED BIT(29)void kcov_task_init (struct task_struct *t);void kcov_task_exit (struct task_struct *t);@@ -38,6 +41,25 @@ do { \ (t)->kcov_mode &= ~KCOV_IN_CTXSW ; \ }while (0 ) +/*
include/linux/kcov.h do { \ · line 54
Quoted 10 lines
include/linux/kcov.h +10
-0+* Pause coverage for current. Callers must be uninstrumented. +* Pass the returned state to __kcov_resume(). +*/ +static __always_inline unsigned int __kcov_pause(void) +{ +unsigned int paused; + + paused = current->kcov_mode &KCOV_PAUSED ; + current->kcov_mode |=KCOV_PAUSED ; +return paused;
I think these two function can be consolidated with kcov_prepare_switch()/kcov_finish_switch():
static __always_inline unsigned int __kcov_set_flag(struct task_struct *t, unsigned int flag)
{
unsigned int mode = READ_ONCE(t->kcov_mode);
unsigned int prev_flag = mode & flag;
if (!prev_flag && kcov_mode_enabled(mode)) {
WRITE_ONCE(t->kcov_mode, mode | flag);
barrier();
}
return prev_flag;
}#define kcov_prepare_switch(t) __kcov_set_flag((t), KCOV_IN_CTXSW)
static __always_inline unsigned int __kcov_pause(void)
{
return __kcov_set_flag(current, KCOV_PAUSED);
}(note the READ_ONCE()/WRITE_ONCE() around kcov_mode, plus the compiler barrier).
PATCH v2 3/6 hrtimer: Pause KCOV during deferred rearm
In reply to Karl MehltretterDeferred 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.
Take the kcov_pause guard in an __always_inline wrapper around __hrtimer_rearm_deferred(). 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.
---
Notes:
v2:
- take guard(kcov_pause)() in an inline wrapper
- clarify the test configuration and caller instrumentation requirements
include/linux/hrtimer_rearm.h | 17 +++++++++++++++-- kernel/sched/core.c | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-)
include/linux/hrtimer_rearm.h +15 -2
@@ -3,10 +3,22 @@#define _LINUX_HRTIMER_REARM_H#ifdef CONFIG_HRTIMER_REARM_DEFERRED +#include <linux/kcov.h> #include <linux/thread_info.h> void __hrtimer_rearm_deferred (void ); +/* +* KCOV: Pause outside __hrtimer_rearm_deferred() to suppress entry coverage. +* Callers with KCOV enabled for current must be uninstrumented. +*/ +static __always_inline void hrtimer_rearm_deferred_kcov_paused(void) +{ +guard (kcov_pause)(); + +__hrtimer_rearm_deferred (); +} +/* * 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
@@ -38,7 +50,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;
@@ -50,7 +62,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 (); }/*
@@ -73,6 +85,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) { } +static __always_inline void hrtimer_rearm_deferred_kcov_paused(void) { } static __always_inline void hrtimer_rearm_deferred(void) { } static __always_inline void hrtimer_rearm_deferred_tif(unsigned long tif_work) { }static __always_inline bool
kernel/sched/core.c +1 -1
@@ -1010,7 +1010,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 ; }
Signature · 1 line
2.53.0
Re: [PATCH v2 3/6] hrtimer: Pause KCOV during deferred rearm
Quoted 3 lines · Karl Mehltretter
Deferred hrtimer rearm can run after
HARDIRQ_OFFSETis 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
Re: [PATCH v2 3/6] hrtimer: Pause KCOV during deferred rearm
Quoted 11 lines · Peter Zijlstra
Quoted 3 lines
Deferred hrtimer rearm can run after
HARDIRQ_OFFSETis 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 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.
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?
Thanks,
Karl
Peter Zijlstra
maintainerRe: [PATCH v2 3/6] hrtimer: Pause KCOV during deferred rearm
Quoted 13 lines · Karl Mehltretter
Quoted 11 lines · Peter Zijlstra
Quoted 3 lines
Deferred hrtimer rearm can run after
HARDIRQ_OFFSETis 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
Quoted 7 lines
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.
Quoted 7 lines
hrtimer_rearm_deferred(), socheck_kcov_mode()seesin_task()and records callee coverage for current.This is the same class of failure as
477d81a1c47a("x86/entry: Remove unwanted instrumentation incommon_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.
Quoted 6 lines
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
---
kernel/softirq.c +13 -9
@@ -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 (); }
Re: [PATCH v2 3/6] hrtimer: Pause KCOV during deferred rearm
Quoted 8 lines · Peter Zijlstra
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_countmunging around there.*completely* untested and all that
Thanks, I tested this.
The idea helps, but the ksirqd check is not enough.
__do_softirq() also runs from task context and ktimerd with ksirqd=false. Subtracting HARDIRQ_OFFSET there corrupts preempt_count.
In the direct IRQ-exit path, __local_bh_disable_ip() also warns because HARDIRQ_OFFSET is still set.
My version below checks in_hardirq() in handle_softirqs(). For direct IRQ-exit dispatch it replaces HARDIRQ_OFFSET with SOFTIRQ_OFFSET, then restores it.
Other callers keep the normal local-BH accounting. tick_irq_exit() still runs after HARDIRQ_OFFSET is dropped.
In a 400-round KCOV test, flaky PCs fell from 140 to 129. The average trace size fell from 147.7 to 141.6 PCs. But measured syzcaller coverage did not improve significantly.
I also ran a targeted KCSAN test that intentionally produced 58 reports on each kernel. Baseline classified all IRQ-exit accesses as "by task". With this patch, all were "by interrupt". Both passed the KCSAN selftest. Lockdep and IRQ-tracing boots also passed.
This patch does not replace the KCOV pause series. It only covers work dispatched directly from IRQ exit. Deferred rearm outside this path and task-context scheduler leaks remain.
Karl
---
kernel/softirq.c +37 -12
@@ -350,8 +350,8 @@ static inline void ksoftirqd_run_end(void)local_irq_enable (); } -static inline void softirq_handle_begin (void ) { } -static inline void softirq_handle_end (void ) { } +static inline bool softirq_handle_begin (void ) {return false; } +static inline void softirq_handle_end (bool from_hardirq) { }static inline bool should_wake_ksoftirqd (void ) {
@@ -481,15 +481,35 @@ 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 bool softirq_handle_begin (void ) { -__local_bh_disable_ip (_RET_IP_,SOFTIRQ_OFFSET ); +bool from_hardirq =in_hardirq (); + +if (!from_hardirq) { +__local_bh_disable_ip (_RET_IP_,SOFTIRQ_OFFSET ); +return false; + } + +/* Replace the retained hardirq context with normal softirq context. */ +__preempt_count_add ((int )SOFTIRQ_OFFSET - (int )HARDIRQ_OFFSET ); +if (softirq_count () ==SOFTIRQ_OFFSET ) +lockdep_softirqs_off (_RET_IP_); + +return true; } -static inline void softirq_handle_end (void ) +static inline void softirq_handle_end (bool from_hardirq) { -__local_bh_enable (SOFTIRQ_OFFSET ); -WARN_ON_ONCE (in_interrupt ()); +if (!from_hardirq) { +__local_bh_enable (SOFTIRQ_OFFSET ); +WARN_ON_ONCE (in_interrupt ()); +return ; + } + +if (softirq_count () ==SOFTIRQ_OFFSET ) +lockdep_softirqs_on (_RET_IP_); +__preempt_count_sub ((int )SOFTIRQ_OFFSET - (int )HARDIRQ_OFFSET ); +WARN_ON_ONCE (!in_hardirq ()); }static inline void ksoftirqd_run_begin (void )
@@ -605,6 +625,7 @@ static void handle_softirqs(bool ksirqd)unsigned long old_flags = current->flags;int max_restart =MAX_SOFTIRQ_RESTART ;struct softirq_action *h; +bool from_hardirq;bool in_hardirq;__u32 pending;int softirq_bit;
@@ -618,7 +639,7 @@ static void handle_softirqs(bool ksirqd) pending =local_softirq_pending (); -softirq_handle_begin (); + from_hardirq =softirq_handle_begin (); in_hardirq =lockdep_softirq_start ();account_softirq_enter (current);
@@ -670,7 +691,7 @@ static void handle_softirqs(bool ksirqd)account_softirq_exit (current);lockdep_softirq_end (in_hardirq); -softirq_handle_end (); +softirq_handle_end (from_hardirq);current_restore_flags (old_flags,PF_MEMALLOC ); }
@@ -740,6 +761,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 +772,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 +780,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 +791,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 (); }
PATCH v2 4/6 sched/core: Pause KCOV in __schedule()
In reply to Karl Mehltretterkernel/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 added to x86_64 defconfig, the interrupt selftest fails under QEMU, detecting spurious coverage in arch_scale_cpu_capacity().
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. The guard in its resumed __schedule() frame restores the prior state.
---
Notes:
v2:
- take guard(kcov_pause)()
- clarify the test configuration and reword the pause comment
kernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/core.c +3 -0
@@ -7073,6 +7073,9 @@ static void __sched notrace __schedule(int sched_mode)struct rq *rq;int cpu; +/* Instrumented callees would leak coverage into current. */ +guard (kcov_pause)(); +/* Trace preemptions consistently with task switches */ trace_sched_entry_tp (sched_mode ==SM_PREEMPT );
Signature · 1 line
2.53.0
Peter Zijlstra
maintainerRe: [PATCH v2 4/6] sched/core: Pause KCOV in __schedule()
Quoted 1 line · Karl Mehltretter
kernel/sched/is not instrumented,
It bloody well is; there is no noinstr here, just a notrace.
Quoted 34 lines
but callees such as
sched_clock(),
architecture CPU-capacity helpers andprofile_hits()are.During preemption and
schedule()calls, instrumented callees can add nondeterministic scheduler coverage to current.With
CONFIG_KCOV_SELFTESTadded tox86_64defconfig, the interrupt selftest fails under QEMU, detecting spurious coverage inarch_scale_cpu_capacity().Annotating each callee would spread exclusions across architectures. Pause across
__schedule()instead, extending the scheduler exclusion to its callees.
KCOV_PAUSEDremains set while a task is switched out. The guard in its resumed__schedule()frame restores the prior state.Fixes 5c9a8750a640 ("kernel: add kcov code coverage")Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>---
Notes:
v2:
- take guard(kcov_pause)()
- clarify the test configuration and reword the pause commentkernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/core.c +0
-0
kernel/sched/core.c static void __sched notrace __schedule(int sched_mode) · line 7076
Quoted 4 lines
kernel/sched/core.c +1
-0@@ -7073,6 +7073,9 @@ static void __sched notrace __schedule(int sched_mode)struct rq *rq;int cpu; +/* Instrumented callees would leak coverage into current. */
This comment makes about as much sense as your Changelog.
Quoted 7 lines
kernel/sched/core.c +2
-0+guard (kcov_pause)(); +/* Trace preemptions consistently with task switches */ trace_sched_entry_tp (sched_mode ==SM_PREEMPT );Signature · 1 line
2.53.0
PATCH v2 5/6 sched/core: Pause KCOV in try_to_wake_up()
In reply to Karl Mehltrettertry_to_wake_up() is uninstrumented, but it calls instrumented helpers such as kthread_is_per_cpu(), CPU capacity helpers and SCHED_HRTICK arming. They can record into current while in_task() is true.
CONFIG_KCOV_SELFTEST exposes this under PREEMPT_RT. The interrupt selftest fails on x86_64 in kthread_is_per_cpu(): RT runs the timer softirq in a thread, so the wakeup runs in task context during the selftest's spin. The same helpers leak into non-RT syscall wakeups such as a pipe write waking a reader.
Pause the wakeup body with the kcov_pause guard. Wrapping only select_task_rq() would miss SCHED_HRTICK arming during enqueue.
---
Notes:
v2:
- take guard(kcov_pause)()
- describe the paused wakeup body precisely
kernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/core.c +3 -0
@@ -4253,6 +4253,9 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)guard (preempt)();int cpu, success =0 ; +/* Instrumented callees would leak coverage into current. */ +guard (kcov_pause)(); + wake_flags |=WF_TTWU ;if (p == current) {
Signature · 1 line
2.53.0
Peter Zijlstra
maintainerRe: [PATCH v2 5/6] sched/core: Pause KCOV in try_to_wake_up()
Quoted 30 lines · Karl Mehltretter
try_to_wake_up()is uninstrumented, but it calls instrumented helpers such askthread_is_per_cpu(), CPU capacity helpers andSCHED_HRTICKarming. They can record into current whilein_task()is true.
CONFIG_KCOV_SELFTESTexposes this underPREEMPT_RT. The interrupt selftest fails onx86_64inkthread_is_per_cpu(): RT runs the timer softirq in a thread, so the wakeup runs in task context during the selftest's spin. The same helpers leak into non-RT syscall wakeups such as a pipe write waking a reader.Pause the wakeup body with the
kcov_pauseguard. Wrapping onlyselect_task_rq()would missSCHED_HRTICKarming during enqueue.Fixes 5c9a8750a640 ("kernel: add kcov code coverage")Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>---
Notes:
v2:
- take guard(kcov_pause)()
- describe the paused wakeup body preciselykernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/core.c +0
-0
kernel/sched/core.c int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) · line 4257
Quoted 5 lines
kernel/sched/core.c +2
-0@@ -4253,6 +4253,9 @@ int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)guard (preempt)();int cpu, success =0 ; +/* Instrumented callees would leak coverage into current. */ +guard (kcov_pause)();
Same problem again. All of this code is instrumented.
PATCH v2 6/6 sched/core: Pause KCOV in wake_up_new_task()
In reply to Karl Mehltretterwake_up_new_task() is uninstrumented, but CPU selection and enqueue call instrumented helpers. During a KCOV-enabled fork, they can record scheduler, hrtimer and clockevent coverage into the parent.
The paths depend on runqueue and CPU state, so coverage varies between identical forks. Pause KCOV for the whole function, extending the scheduler exclusion to new-task wakeups.
---
Notes:
v2:
- take guard(kcov_pause)()
- reword the pause comment
kernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/sched/core.c +3 -0
@@ -4947,6 +4947,9 @@ void wake_up_new_task(struct task_struct *p)struct rq *rq;int wake_flags =WF_FORK ; +/* Instrumented callees would leak coverage into current. */ +guard (kcov_pause)(); +raw_spin_lock_irqsave (&p->pi_lock, rf.flags);WRITE_ONCE (p->__state,TASK_RUNNING );/*
Signature · 1 line
2.53.0
Peter Zijlstra
maintainerRe: [PATCH v2 6/6] sched/core: Pause KCOV in wake_up_new_task()
Quoted 25 lines · Karl Mehltretter
wake_up_new_task()is uninstrumented, but CPU selection and enqueue call instrumented helpers. During a KCOV-enabled fork, they can record scheduler, hrtimer and clockevent coverage into the parent.The paths depend on runqueue and CPU state, so coverage varies between identical forks. Pause KCOV for the whole function, extending the scheduler exclusion to new-task wakeups.
Fixes 5c9a8750a640 ("kernel: add kcov code coverage")Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>---
Notes:
v2:
- take guard(kcov_pause)()
- reword the pause commentkernel/sched/core.c | 3 +++ 1 file changed, 3 insertions(+)kernel/sched/core.c +0
-0
kernel/sched/core.c void wake_up_new_task(struct task_struct *p) · line 4951
Quoted 5 lines
kernel/sched/core.c +2
-0@@ -4947,6 +4947,9 @@ void wake_up_new_task(struct task_struct *p)struct rq *rq;int wake_flags =WF_FORK ; +/* Instrumented callees would leak coverage into current. */ +guard (kcov_pause)();
I'm sure you know what I'm about to say now, right?
Quoted 39 lines · Karl Mehltretter
KCOV aims to exclude interrupt and scheduler coverage so syscall coverage stays input-dependent. Instrumented callees can still record when uninstrumented timer and scheduler paths run with
in_task()true.With the diagnostic patch in [1] applied,
CONFIG_KCOV_SELFTESTexposes three cases on x86-64: deferred hrtimer rearm,__schedule()callees andPREEMPT_RTwakeups. Task-context wakeups and new-task enqueue also add scheduler coverage to ordinary syscalls.Add a nestable
KCOV_PAUSEDbit and akcov_pauseguard. Use the guard for deferred hrtimer rearm,__schedule(), thetry_to_wake_up()wakeup body andwake_up_new_task(). This suppresses their instrumented callees without excluding those callees from task-context coverage.Changes in v2:
- Add patch 1 to make kcov_start()'s mode parameter unsigned int. No functional change.
- Rework patch 2 around a guard-only API with private current-only helpers (Bradley Morgan).
- Use the guard in patches 3-6 and reword the pause comments.v2 testing:
- GCC builds on x86-64, arm32, arm64, MIPS32/64, PowerPC 32/64, s390, RISC-V 32/64, LoongArch, Xtensa and UML.
-PREEMPT_RTbuilds on x86-64, arm32, arm64, RISC-V 32/64 and LoongArch.
- x86-64 builds with GCC 8.1 and Clang 22.1. Both kernels passed a KCOV selftest boot.
- x86-64CONFIG_KCOV=nbuild, with no KCOV or pause references.
- KCOV selftest, 10/10 x86-64 boots with and withoutPREEMPT_RT. A fresh non-RT boot passed after the helper-only rework.
- 40 dummy_hcd/g_zero remote-KCOV cycles on x86-64 and arm64.
- 400 repeatedfork()calls on x86-64PREEMPT_RT.Three one-hour syzkaller A/B pairs were run. Each baseline and patched run used four 2-vCPU
PREEMPT_RTVMs. The patched kernel completed 22-51% more executions than base. At matched execution counts, corpus size grew 42-54% and coverage 14-19%. No run produced a report.
Quoted 8 lines
With KCOV disabled, the pause sections compile away. With KCOV enabled on x86-64, GCC 15.2 grows
__schedule()by 117 bytes,try_to_wake_up()by 94 bytes andwake_up_new_task()by 88 bytes relative to the base commit.
On all 6 patches
Quoted 382 lines
v1: https://lore.kernel.org/r/[email protected]
Karl Mehltretter (6):
kcov: Use unsigned int forkcov_start()mode parameter
kcov: Add akcov_pauseguard
hrtimer: Pause KCOV during deferred rearm sched/core: Pause KCOV in__schedule()sched/core: Pause KCOV intry_to_wake_up()sched/core: Pause KCOV inwake_up_new_task()include/linux/hrtimer_rearm.h | 17 +++++++++++++-- include/linux/kcov.h | 40 ++++++++++++++++++++++++++++++++++- kernel/kcov.c | 4 ++-- kernel/sched/core.c | 11 +++++++++- 4 files changed, 66 insertions(+), 6 deletions(-)Range-diff:
-: ------------- > 1: f60b858edad96 kcov: Use unsigned int for kcov_start() mode parameter 1: a67095eb565de ! 2: 4415cac41ca43 kcov: add kcov_pause()/kcov_resume() helpers@@ Metadata
Author: Karl Mehltretter <[email protected]>## Commit message ##
- kcov: add kcov_pause()/kcov_resume() helpers
+ kcov: Add akcov_pauseguardInterrupt-return work can run after
HARDIRQ_OFFSETis dropped, whenin_task()is true. KCOV then attributes instrumented callees to the interrupted task.Add a
KCOV_PAUSEDbit next toKCOV_IN_CTXSWand mask both in- kcov_mode_enabled(). The context switch suppression keeps its own bit: - kcov_prepare_switch() runs on the previous task and kcov_finish_switch() - on the one switched in, so its lifetime is not a pause section. + kcov_mode_enabled(). The coverage callbacks need no new check because + check_kcov_mode()'s exact comparison rejects modes with KCOV_PAUSED set.- Sections nest by passing the state returned by kcov_pause() to - kcov_resume(). Both operate on current. When task KCOV is active, remote - softirq sections save and restore the complete mode, preserving the - pause state. + The context switch suppression keeps its own bit: kcov_prepare_switch() + runs on the previous task and kcov_finish_switch() on the one switched + in, so its lifetime is not a pause section.- The helpers are __always_inline, and the caller must be uninstrumented: + Provide a kcov_pause guard backed by internal helpers that operate on + current. The guard saves the previous pause state and restores it at + scope exit, so sections nest. When KCOV is enabled for current, remote + softirq sections save and restore the complete mode, preserving the pause + state.+
+ The helpers are__always_inline, and guard users must be uninstrumented:
inlining does not remove the caller's own coverage callbacks.- Assisted-by: Claude:claude-opus-4-8 Assisted-by: Claude:claude-fable-5 Signed-off-by: Karl Mehltretter <[email protected]> @@ include/linux/kcov.h #define _LINUX_KCOV_H +#include <linux/bits.h> ++#include <linux/cleanup.h> #include <linux/sched.h> #include <uapi/linux/kcov.h> @@ include/linux/kcov.h: do { \ } while (0) +/* -+ * Pause coverage for current. Pass the returned state to kcov_resume(). -+ * Callers must be uninstrumented. ++ * Pause coverage for current. Callers must be uninstrumented. ++ * Pass the returned state to __kcov_resume(). + */ -+static __always_inline unsigned int kcov_pause(struct task_struct *t) ++static __always_inline unsigned int __kcov_pause(void) +{ + unsigned int paused; + -+ paused = t->kcov_mode & KCOV_PAUSED; -+ t->kcov_mode |= KCOV_PAUSED; ++ paused = current->kcov_mode & KCOV_PAUSED; ++ current->kcov_mode |= KCOV_PAUSED; + return paused; +} + -+static __always_inline void kcov_resume(struct task_struct *t, unsigned int paused) ++static __always_inline void __kcov_resume(unsigned int paused) +{ + if (!paused) -+ t->kcov_mode &= ~KCOV_PAUSED; ++ current->kcov_mode &= ~KCOV_PAUSED; +} + /* See Documentation/dev-tools/kcov.rst for usage details. */ @@ include/linux/kcov.h: void __sanitizer_cov_trace_switch(kcov_u64 val, void *case static inline void kcov_task_init(struct task_struct *t) {} static inline void kcov_task_exit(struct task_struct *t) {} -+static inline unsigned int kcov_pause(struct task_struct *t) { return 0; } -+static inline void kcov_resume(struct task_struct *t, unsigned int paused) {} ++static inline unsigned int __kcov_pause(void) { return 0; } ++static inline void __kcov_resume(unsigned int paused) {} static inline void kcov_prepare_switch(struct task_struct *t) {} static inline void kcov_finish_switch(struct task_struct *t) {} static inline void kcov_remote_start(u64 handle) {} +@@ include/linux/kcov.h: static inline void kcov_remote_start_usb_softirq(u64 id) {} + static inline void kcov_remote_stop_softirq(void) {} + + #endif /* CONFIG_KCOV */ ++ ++/* ++ * Scope-based KCOV pause: ++ * ++ * guard(kcov_pause)(); ++ * ++ * pauses coverage for current until the end of the scope. Callers must be ++ * uninstrumented. ++ */ ++DEFINE_LOCK_GUARD_0(kcov_pause, ++ _T->paused = __kcov_pause(), ++ __kcov_resume(_T->paused), ++ unsigned int paused) ++ + #endif /* _LINUX_KCOV_H */ ## kernel/kcov.c ## @@ kernel/kcov.c: static const struct file_operations kcov_fops = {2:
2d64b45a316c8! 3:c891839993a1ehrtimer: pause KCOV during deferred rearm
@@ Metadata
Author: Karl Mehltretter <[email protected]>## Commit message ##
- hrtimer: pause KCOV during deferred rearm
+ hrtimer: Pause KCOV during deferred rearmDeferred hrtimer rearm can run after
HARDIRQ_OFFSETis dropped.in_task()is then true, so KCOV attributes the instrumented timer-reprogramming subtree to current.- With CONFIG_KCOV_SELFTEST, the interrupt selftest fails on x86_64 - defconfig under QEMU, detecting spurious coverage in - __hrtimer_rearm_deferred(). The same happens on s390, RISC-V and - LoongArch, which also enable HRTIMER_REARM_DEFERRED. + 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 in the __always_inline wrappers, including hrtick_schedule_exit(). - Call sites where task KCOV can be active are KCOV-disabled or 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. + Take the kcov_pause guard in an __always_inline wrapper around + __hrtimer_rearm_deferred(). 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.Fixes 15dd3a948855 ("hrtimer: Push reprogramming timers into the interrupt return path")- Assisted-by: Claude:claude-opus-4-8
Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>@@ include/linux/hrtimer_rearm.h void __hrtimer_rearm_deferred(void);+/* -+ * Pause outside __hrtimer_rearm_deferred() to suppress its entry coverage. -+ * Call sites where task KCOV can be active are uninstrumented. ++ * KCOV: Pause outside __hrtimer_rearm_deferred() to suppress entry coverage. ++ * Callers with KCOV enabled for current must be uninstrumented. + */ -+static __always_inline void hrtimer_rearm_deferred_paused(void) ++static __always_inline void hrtimer_rearm_deferred_kcov_paused(void) +{ -+ unsigned int kcov_paused = kcov_pause(current); ++ guard(kcov_pause)(); + + __hrtimer_rearm_deferred(); -+ kcov_resume(current, kcov_paused); +} + /* @@ include/linux/hrtimer_rearm.h: hrtimer_rearm_deferred_user_irq(unsigned long *ti if (unlikely((*tif_work & TIF_REARM_MASK) == _TIF_HRTIMER_REARM)) { clear_thread_flag(TIF_HRTIMER_REARM); - __hrtimer_rearm_deferred(); -+ hrtimer_rearm_deferred_paused(); ++ 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; @@ include/linux/hrtimer_rearm.h: hrtimer_rearm_deferred_user_irq(unsigned long *ti { if (hrtimer_test_and_clear_rearm_deferred_tif(tif_work)) - __hrtimer_rearm_deferred(); -+ hrtimer_rearm_deferred_paused(); ++ hrtimer_rearm_deferred_kcov_paused(); } /* @@ include/linux/hrtimer_rearm.h: static __always_inline bool hrtimer_test_and_clea #else /* CONFIG_HRTIMER_REARM_DEFERRED */ static __always_inline void __hrtimer_rearm_deferred(void) { } -+static __always_inline void hrtimer_rearm_deferred_paused(void) { } ++static __always_inline void hrtimer_rearm_deferred_kcov_paused(void) { } static __always_inline void hrtimer_rearm_deferred(void) { } static __always_inline void hrtimer_rearm_deferred_tif(unsigned long tif_work) { } static __always_inline bool @@ kernel/sched/core.c: static inline void hrtick_schedule_exit(struct rq *rq) if (rq->hrtick_sched & HRTICK_SCHED_REARM_HRTIMER) - __hrtimer_rearm_deferred(); -+ hrtimer_rearm_deferred_paused(); ++ hrtimer_rearm_deferred_kcov_paused(); rq->hrtick_sched = HRTICK_SCHED_NONE; }3:
dc4bdcf8adacd! 4:63657f2c7ef08sched: pause KCOV in__schedule()
@@ Metadata
Author: Karl Mehltretter <[email protected]>## Commit message ##
- sched: pause KCOV in__schedule()
+ sched/core: Pause KCOV in__schedule()
kernel/sched/is not instrumented, but callees such assched_clock(), architecture CPU-capacity helpers andprofile_hits()are.
@@ Commit message
During preemption andschedule()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. + With CONFIG_KCOV_SELFTEST added to x86_64 defconfig, the interrupt + selftest fails under QEMU, detecting spurious coverage in + arch_scale_cpu_capacity().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. + KCOV_PAUSED remains set while a task is switched out. The guard in 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-5Signed-off-by Karl Mehltretter <[email protected]>##
kernel/sched/core.c##
@@kernel/sched/core.c: static void__schednotrace__schedule(int sched_mode)
- boolis_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); ++ /* Instrumented callees would leak coverage into current. */ ++ guard(kcov_pause)(); + /* Trace preemptions consistently with task switches */ trace_sched_entry_tp(sched_mode == SM_PREEMPT); -@@ kernel/sched/core.c: 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)4:
b8e96cc1903de! 5:5cf8497b8a0absched: pause KCOV intry_to_wake_up()
@@ Metadata
Author: Karl Mehltretter <[email protected]>## Commit message ##
- sched: pause KCOV intry_to_wake_up()
+ sched/core: Pause KCOV intry_to_wake_up()
try_to_wake_up()is uninstrumented, but it calls instrumented helpers such askthread_is_per_cpu(), CPU capacity helpers andSCHED_HRTICK
@@ Commit message
selftest's spin. The same helpers leak into non-RT syscall wakeups such as a pipe write waking a reader.- Pause all of try_to_wake_up(). Wrapping only select_task_rq() would miss - SCHED_HRTICK arming during enqueue. + Pause the wakeup body with the kcov_pause guard. Wrapping only + select_task_rq() would miss SCHED_HRTICK arming during enqueue.Fixes: 5c9a8750a640 ("kernel: add kcov code coverage")- Assisted-by: Claude:claude-opus-4-8
+ Assisted-by: Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>##
kernel/sched/core.c##
@@kernel/sched/core.c: inttry_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)- { guard(preempt)(); int cpu, success = 0;-+ /* KCOV: sched/ is uninstrumented but the wakeup callees are not. */
-+ unsigned intkcov_paused=kcov_pause(current);++ /* Instrumented callees would leak coverage into current. */ ++ guard(kcov_pause)(); ++ wake_flags |= WF_TTWU;-@@
kernel/sched/core.c: inttry_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
- if (success)
-ttwu_stat(p, task_cpu(p), wake_flags);
-
-+kcov_resume(current, kcov_paused);
- return success;
- }
-
+ if (p == current) {
5:f91a7644a15e8! 6:a00870853f5a1sched: pause KCOV inwake_up_new_task()
@@ Metadata
Author: Karl Mehltretter <[email protected]>## Commit message ##
- sched: pause KCOV inwake_up_new_task()
+ sched/core: Pause KCOV inwake_up_new_task()
wake_up_new_task()is uninstrumented, but CPU selection and enqueue call instrumented helpers. During a KCOV-enabled fork, they can record
@@ Commit message
scheduler exclusion to new-task wakeups.Fixes 5c9a8750a640 ("kernel: add kcov code coverage")- Assisted-by: Claude:claude-opus-4-8
+ Assisted-by: Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>##
kernel/sched/core.c##
@@kernel/sched/core.c: voidwake_up_new_task(struct task_struct *p)- { - struct rq_flags rf; struct rq *rq; -+ unsigned int kcov_paused; int wake_flags = WF_FORK; -+ /* KCOV: sched/ is uninstrumented but the wakeup callees are not. */ -+ kcov_paused = kcov_pause(current); ++ /* Instrumented callees would leak coverage into current. */ ++ guard(kcov_pause)(); + raw_spin_lock_irqsave(&p->pi_lock, rf.flags); WRITE_ONCE(p->__state, TASK_RUNNING); /* -@@ kernel/sched/core.c: void wake_up_new_task(struct task_struct *p) - rq_repin_lock(rq, &rf); - } - task_rq_unlock(rq, p, &rf); -+ kcov_resume(current, kcov_paused); - } - - #ifdef CONFIG_PREEMPT_NOTIFIERS
Quoted 2 lines
base-commit:
8ba098e6b6ff0db8edf28528d1552be261af30d4
Thanks!
Peter Zijlstra
maintainerQuoted 46 lines · Bradley Morgan
Quoted 44 lines · Karl Mehltretter
KCOV aims to exclude interrupt and scheduler coverage so syscall coverage stays input-dependent. Instrumented callees can still record when uninstrumented timer and scheduler paths run with
in_task()true.With the diagnostic patch in [1] applied,
CONFIG_KCOV_SELFTESTexposes three cases on x86-64: deferred hrtimer rearm,__schedule()callees andPREEMPT_RTwakeups. Task-context wakeups and new-task enqueue also add scheduler coverage to ordinary syscalls.Add a nestable
KCOV_PAUSEDbit and akcov_pauseguard. Use the guard for deferred hrtimer rearm,__schedule(), thetry_to_wake_up()wakeup body andwake_up_new_task(). This suppresses their instrumented callees without excluding those callees from task-context coverage.Changes in v2:
- Add patch 1 to make kcov_start()'s mode parameter unsigned int. No functional change.
- Rework patch 2 around a guard-only API with private current-only helpers (Bradley Morgan).
- Use the guard in patches 3-6 and reword the pause comments.v2 testing:
- GCC builds on x86-64, arm32, arm64, MIPS32/64, PowerPC 32/64, s390, RISC-V 32/64, LoongArch, Xtensa and UML.
-PREEMPT_RTbuilds on x86-64, arm32, arm64, RISC-V 32/64 and LoongArch.
- x86-64 builds with GCC 8.1 and Clang 22.1. Both kernels passed a KCOV selftest boot.
- x86-64CONFIG_KCOV=nbuild, with no KCOV or pause references.
- KCOV selftest, 10/10 x86-64 boots with and withoutPREEMPT_RT. A fresh non-RT boot passed after the helper-only rework.
- 40 dummy_hcd/g_zero remote-KCOV cycles on x86-64 and arm64.
- 400 repeatedfork()calls on x86-64PREEMPT_RT.Three one-hour syzkaller A/B pairs were run. Each baseline and patched run used four 2-vCPU
PREEMPT_RTVMs. The patched kernel completed 22-51% more executions than base. At matched execution counts, corpus size grew 42-54% and coverage 14-19%. No run produced a report.With KCOV disabled, the pause sections compile away. With KCOV enabled on x86-64, GCC 15.2 grows
__schedule()by 117 bytes,try_to_wake_up()by 94 bytes andwake_up_new_task()by 88 bytes relative to the base commit.
Quoted 6 lines
Quoted 3 lines
Reviewed-by Bradley Morgan <[email protected]>
Plonk
Quoted 6 lines · Karl Mehltretter
Add a nestable
KCOV_PAUSEDbit and akcov_pauseguard. Use the guard for deferred hrtimer rearm,__schedule(), thetry_to_wake_up()wakeup body andwake_up_new_task(). This suppresses their instrumented callees without excluding those callees from task-context coverage.
Andrey, Alexander, Dmitry,
Could you please take a look at this series from the KCOV side?
It fixes the CONFIG_KCOV_SELFTEST failures covered by this series. Some architectures still need further architecture-specific KCOV instrumentation exclusions.
The three syzkaller A/B pairs from the cover letter also suggest better coverage: at matched execution counts, the patched kernels had 14-19% more coverage and 42-54% larger corpora.
Do you think the pause mechanism and the places where it is used make sense? Do you want me to continue with a v3?
I also looked at the Sashiko dashboard report:
https://sashiko.dev/#/patchset/[email protected]
I don't think the remaining warning about reordering or removing the kcov_mode stores is real for these call sites.
One small change opportunity for v3: In try_to_wake_up(), I would take the kcov_pause guard before the preempt guard. Cleanup then runs preempt_enable() while KCOV is still paused and resumes KCOV afterwards.
I would also replace "uninstrumented" with "built without KCOV instrumentation" to avoid the ambiguity Peter pointed out.
Separately, I will look at turning Peter's IRQ-exit preempt-count proposal into a standalone patch for instrumentation in general, especially KCSAN. It does not replace this KCOV series, since it only covers work run directly from IRQ exit.
Thanks,
Karl
Alexander Potapenko
reviewerQuoted 9 lines · Karl Mehltretter
Quoted 6 lines · Karl Mehltretter
Add a nestable
KCOV_PAUSEDbit and akcov_pauseguard. Use the guard for deferred hrtimer rearm,__schedule(), thetry_to_wake_up()wakeup body andwake_up_new_task(). This suppresses their instrumented callees without excluding those callees from task-context coverage.
Quoted 3 lines
Andrey, Alexander, Dmitry,
Could you please take a look at this series from the KCOV side?
Hi Karl,
Quoted 11 lines
It fixes the
CONFIG_KCOV_SELFTESTfailures covered by this series. Some architectures still need further architecture-specific KCOV instrumentation exclusions.The three syzkaller A/B pairs from the cover letter also suggest better coverage: at matched execution counts, the patched kernels had 14-19% more coverage and 42-54% larger corpora.
Do you think the pause mechanism and the places where it is used make sense? Do you want me to continue with a v3?
I think it makes sense, although it would be nice to try to minimize the number of points where we insert kcov_pause()/kcov_resume() Perhaps we could piggyback on some common scheduler action (e.g. locking) to do that?
If Peter is fine with these annotations being open-coded in kernel/sched/core.c, then I'm also fine.
Quoted 6 lines
I also looked at the Sashiko dashboard report:
https://sashiko.dev/#/patchset/[email protected]
I don't think the remaining warning about reordering or removing the
kcov_modestores is real for these call sites.
I think consistently using READ_ONCE/WRITE_ONCE here is better. See my suggestion of converging the flag functions in "kcov: Add a kcov_pause guard".
Quoted 3 lines
One small change opportunity for v3: In
try_to_wake_up(), I would take thekcov_pauseguard before the preempt guard. Cleanup then runspreempt_enable()while KCOV is still paused and resumes KCOV afterwards.
Agreed.
Quoted 2 lines
I would also replace "uninstrumented" with "built without KCOV instrumentation" to avoid the ambiguity Peter pointed out.
Ack, this is indeed clearer.
Quoted 4 lines
Separately, I will look at turning Peter's IRQ-exit preempt-count proposal into a standalone patch for instrumentation in general, especially KCSAN. It does not replace this KCOV series, since it only covers work run directly from IRQ exit.
Fixing the IRQ exit should also help with some KMSAN false positives, so I am all for it.