Re: [PATCH] kcov: Defer PC canonicalization until tracing is enabled
From: Alexander Potapenko
Date: Thu Dec 03 2026 - 04:36:41 EST
On Wed, Nov 25, 2026 at 4:36 AM Xionggui Chen <evers_chen@xxxxxxx> wrote:
>
> From: Xionggui Chen <chenxionggui@xxxxxxxxxxxxxxxxxxx>
>
> When CONFIG_KCOV_INSTRUMENT_ALL and CONFIG_RANDOMIZE_BASE are
> enabled, __sanitizer_cov_trace_pc() may be called during early
> boot before the kernel's virtual memory mappings are fully
> initialized.
>
> Currently, canonicalize_ip() is called before checking whether
> KCOV tracing is enabled. On RISC-V, canonicalize_ip() calls
> kaslr_offset(), which accesses kernel mapping information that
> may not yet be accessible.
>
> This causes an early boot load access fault even when KCOV
> tracing is disabled.
>
> Move canonicalize_ip() after check_kcov_mode() to avoid accessing
> the KASLR offset when KCOV tracing is disabled.
>
> The comparison tracing path already performs this check before
> canonicalizing the instruction pointer.
>
> The issue was reproduced on RISC-V with Linux 7.4-rc1 using
> CONFIG_KCOV_INSTRUMENT_ALL=y and CONFIG_RANDOMIZE_BASE=y.
>
> The fix was verified on QEMU and RISC-V hardware. KCOV PC
> tracing collected 124 entries, and comparison tracing collected
> 63 entries on the RISC-V hardware.
>
> Fixes: ded97d2c2b2c ("kcov: support comparison operands collection")
> Signed-off-by: Xionggui Chen <chenxionggui@xxxxxxxxxxxxxxxxxxx>
Reviewed-by: Alexander Potapenko <glider@xxxxxxxxxx>