All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kcov: Defer PC canonicalization until tracing is enabled
@ 2026-11-25  3:36 ` Xionggui Chen
  0 siblings, 0 replies; 4+ messages in thread
From: Xionggui Chen @ 2026-11-25  3:36 UTC (permalink / raw)
  To: Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov
  Cc: Andrew Morton, kasan-dev, linux-kernel, linux-riscv,
	Xionggui Chen

From: Xionggui Chen <chenxionggui@lanxincomputing.com>

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@lanxincomputing.com>
---
 kernel/kcov.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/kcov.c b/kernel/kcov.c
index 9402227bd7ba..9dc8ce9f7563 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -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;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH] kcov: Defer PC canonicalization until tracing is enabled
@ 2026-11-25  3:36 ` Xionggui Chen
  0 siblings, 0 replies; 4+ messages in thread
From: Xionggui Chen @ 2026-11-25  3:36 UTC (permalink / raw)
  To: Andrey Konovalov, Alexander Potapenko, Dmitry Vyukov
  Cc: Andrew Morton, kasan-dev, linux-kernel, linux-riscv,
	Xionggui Chen

From: Xionggui Chen <chenxionggui@lanxincomputing.com>

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@lanxincomputing.com>
---
 kernel/kcov.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/kcov.c b/kernel/kcov.c
index 9402227bd7ba..9dc8ce9f7563 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -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;
-- 
2.53.0


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* + kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch added to mm-nonmm-unstable branch
  2026-11-25  3:36 ` Xionggui Chen
  (?)
@ 2026-11-26  6:46 ` Andrew Morton
  2026-11-26 18:30   ` Bradley Morgan
  -1 siblings, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2026-11-26  6:46 UTC (permalink / raw)
  To: mm-commits, glider, dvyukov, andreyknvl, chenxionggui, akpm


The patch titled
     Subject: kcov: defer PC canonicalization until tracing is enabled
has been added to the -mm mm-nonmm-unstable branch.  Its filename is
     kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch

This patch will later appear in the mm-nonmm-unstable branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Xionggui Chen <chenxionggui@lanxincomputing.com>
Subject: kcov: defer PC canonicalization until tracing is enabled
Date: Wed, 25 Nov 2026 03:36:35 +0000

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.

Link: https://patch.msgid.link/20261125033635.3990-1-evers_chen@163.com
Fixes: ded97d2c2b2c ("kcov: support comparison operands collection")
Signed-off-by: Xionggui Chen <chenxionggui@lanxincomputing.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>

 kernel/kcov.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

--- a/kernel/kcov.c~kcov-defer-pc-canonicalization-until-tracing-is-enabled
+++ a/kernel/kcov.c
@@ -216,13 +216,14 @@ void notrace __sanitizer_cov_trace_pc(vo
 {
 	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;
_

Patches currently in -mm which might be from chenxionggui@lanxincomputing.com are

selftests-mm-handle-implicit-read-permissions-in-madv_populate.patch
kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: + kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch added to mm-nonmm-unstable branch
  2026-11-26  6:46 ` + kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch added to mm-nonmm-unstable branch Andrew Morton
@ 2026-11-26 18:30   ` Bradley Morgan
  0 siblings, 0 replies; 4+ messages in thread
From: Bradley Morgan @ 2026-11-26 18:30 UTC (permalink / raw)
  To: akpm; +Cc: andreyknvl, chenxionggui, dvyukov, glider, mm-commits

On 26 November 2026 06:46:48 GMT, Andrew Morton <akpm@linux-foundation.org>
wrote:
>
>The patch titled
>     Subject: kcov: defer PC canonicalization until tracing is enabled
>has been added to the -mm mm-nonmm-unstable branch.  Its filename is
>     kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch
>
>This patch will shortly appear at
>     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch
>
>This patch will later appear in the mm-nonmm-unstable branch at
>    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
>
>Before you just go and hit "reply", please:
>   a) Consider who else should be cc'ed
>   b) Prefer to cc a suitable mailing list as well
>   c) Ideally: find the original patch on the mailing list and do a
>      reply-to-all to that, adding suitable additional cc's
>
>*** Remember to use Documentation/process/submit-checklist.rst when
>testing your code ***
>
>The -mm tree is included into linux-next via various
>branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
>and is updated there most days
>
>------------------------------------------------------
>From: Xionggui Chen <chenxionggui@lanxincomputing.com>
>Subject: kcov: defer PC canonicalization until tracing is enabled
>Date: Wed, 25 Nov 2026 03:36:35 +0000
>
>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.
>
>Link: https://patch.msgid.link/20261125033635.3990-1-evers_chen@163.com
>Fixes: ded97d2c2b2c ("kcov: support comparison operands collection")
>Signed-off-by: Xionggui Chen <chenxionggui@lanxincomputing.com>
>Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
>Cc: Andrey Konovalov <andreyknvl@gmail.com>
>Cc: Alexander Potapenko <glider@google.com>

Sorry, forgot to review on my roll.

Reviewed-by: Bradley Morgan <bradley.morgan@linux.dev>


>Cc: Dmitry Vyukov <dvyukov@google.com>
>
> kernel/kcov.c |    3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
>--- a/kernel/kcov.c~kcov-defer-pc-canonicalization-until-tracing-is-enabled
>+++ a/kernel/kcov.c
>@@ -216,13 +216,14 @@ void notrace __sanitizer_cov_trace_pc(vo
> {
> 	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;
>_
>
>Patches currently in -mm which might be from
>chenxionggui@lanxincomputing.com are
>
>selftests-mm-handle-implicit-read-permissions-in-madv_populate.patch
>kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch
>
>
>

--- Thanks!

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-26 18:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  3:36 [PATCH] kcov: Defer PC canonicalization until tracing is enabled Xionggui Chen
2026-11-25  3:36 ` Xionggui Chen
2026-11-26  6:46 ` + kcov-defer-pc-canonicalization-until-tracing-is-enabled.patch added to mm-nonmm-unstable branch Andrew Morton
2026-11-26 18:30   ` Bradley Morgan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.