Skip to contentSkip to conversation

kcov: Defer PC canonicalization until tracing is enabled

PATCH New Xionggui Chen 1 participant 1 message Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kcov: Defer PC canonicalization until tracing is enabled

From: Xionggui Chen <[email protected]>

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")
---
 kernel/kcov.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kcov.c +2 -1
index 9402227bd7ba..9dc8ce9f7563 100644
@@ -216,13 +216,14 @@ void notrace __sanitizer_cov_trace_pc(void)
 {
 	struct task_struct *t;
 	unsigned long *area;
-	unsigned long ip = canonicalize_ip(_RET_IP_);
+	unsigned long ip;
 	unsigned long pos;
 
 	t = current;
 	if (!check_kcov_mode(KCOV_MODE_TRACE_PC, t))
 		return;
 
+	ip = canonicalize_ip(_RET_IP_);
 	area = t->kcov_area;
 	/* The first 64-bit word is the number of subsequent PCs. */
 	pos = READ_ONCE(area[0]) + 1;
Signature · 1 line
2.53.0