linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer
@ 2025-04-17  7:49 Björn Töpel
  2025-04-18  9:57 ` Guo Ren
  2025-04-18 16:18 ` Samuel Holland
  0 siblings, 2 replies; 4+ messages in thread
From: Björn Töpel @ 2025-04-17  7:49 UTC (permalink / raw)
  To: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, linux-riscv,
	Guo Ren
  Cc: Björn Töpel, Masami Hiramatsu, Oleg Nesterov,
	Peter Zijlstra, linux-kernel, linux-trace-kernel

From: Björn Töpel <bjorn@rivosinc.com>

The XOL (execute out-of-line) buffer is used to single-step the
replaced instruction(s) for uprobes. The RISC-V port was missing a
proper fence.i (i$ flushing) after constructing the XOL buffer, which
can result in incorrect execution of stale/broken instructions.

This was found running the BPF selftests "test_progs:
uprobe_autoattach, attach_probe" on the Spacemit K1/X60, where the
uprobes tests randomly blew up.

Fixes: 74784081aac8 ("riscv: Add uprobes supported")
Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
---
 arch/riscv/kernel/probes/uprobes.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
index 4b3dc8beaf77..4faef92dd771 100644
--- a/arch/riscv/kernel/probes/uprobes.c
+++ b/arch/riscv/kernel/probes/uprobes.c
@@ -176,13 +176,6 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
 		*(uprobe_opcode_t *)dst = __BUG_INSN_32;
 	}
 
+	flush_icache_range((unsigned long)dst, (unsigned long)dst + len);
 	kunmap_atomic(kaddr);
-
-	/*
-	 * We probably need flush_icache_user_page() but it needs vma.
-	 * This should work on most of architectures by default. If
-	 * architecture needs to do something different it can define
-	 * its own version of the function.
-	 */
-	flush_dcache_page(page);
 }

base-commit: 1a1d569a75f3ab2923cb62daf356d102e4df2b86
-- 
2.45.2


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

* Re: [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer
  2025-04-17  7:49 [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer Björn Töpel
@ 2025-04-18  9:57 ` Guo Ren
  2025-04-18 16:18 ` Samuel Holland
  1 sibling, 0 replies; 4+ messages in thread
From: Guo Ren @ 2025-04-18  9:57 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti, linux-riscv,
	Björn Töpel, Masami Hiramatsu, Oleg Nesterov,
	Peter Zijlstra, linux-kernel, linux-trace-kernel

On Thu, Apr 17, 2025 at 3:50 PM Björn Töpel <bjorn@kernel.org> wrote:
>
> From: Björn Töpel <bjorn@rivosinc.com>
>
> The XOL (execute out-of-line) buffer is used to single-step the
> replaced instruction(s) for uprobes. The RISC-V port was missing a
> proper fence.i (i$ flushing) after constructing the XOL buffer, which
> can result in incorrect execution of stale/broken instructions.
>
> This was found running the BPF selftests "test_progs:
> uprobe_autoattach, attach_probe" on the Spacemit K1/X60, where the
> uprobes tests randomly blew up.
Good catch, Thx!

Reviewed-by: Guo Ren <guoren@kernel.org>


>
> Fixes: 74784081aac8 ("riscv: Add uprobes supported")
> Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
> ---
>  arch/riscv/kernel/probes/uprobes.c | 9 +--------
>  1 file changed, 1 insertion(+), 8 deletions(-)
>
> diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
> index 4b3dc8beaf77..4faef92dd771 100644
> --- a/arch/riscv/kernel/probes/uprobes.c
> +++ b/arch/riscv/kernel/probes/uprobes.c
> @@ -176,13 +176,6 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
>                 *(uprobe_opcode_t *)dst = __BUG_INSN_32;
>         }
>
> +       flush_icache_range((unsigned long)dst, (unsigned long)dst + len);
>         kunmap_atomic(kaddr);
> -
> -       /*
> -        * We probably need flush_icache_user_page() but it needs vma.
> -        * This should work on most of architectures by default. If
> -        * architecture needs to do something different it can define
> -        * its own version of the function.
> -        */
> -       flush_dcache_page(page);
>  }
>
> base-commit: 1a1d569a75f3ab2923cb62daf356d102e4df2b86
> --
> 2.45.2
>


-- 
Best Regards
 Guo Ren

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

* Re: [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer
  2025-04-17  7:49 [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer Björn Töpel
  2025-04-18  9:57 ` Guo Ren
@ 2025-04-18 16:18 ` Samuel Holland
  2025-04-19 10:18   ` Björn Töpel
  1 sibling, 1 reply; 4+ messages in thread
From: Samuel Holland @ 2025-04-18 16:18 UTC (permalink / raw)
  To: Björn Töpel, Paul Walmsley, Palmer Dabbelt,
	Alexandre Ghiti, linux-riscv, Guo Ren
  Cc: Björn Töpel, Masami Hiramatsu, Oleg Nesterov,
	Peter Zijlstra, linux-kernel, linux-trace-kernel

Hi Björn,

On 2025-04-17 2:49 AM, Björn Töpel wrote:
> From: Björn Töpel <bjorn@rivosinc.com>
> 
> The XOL (execute out-of-line) buffer is used to single-step the
> replaced instruction(s) for uprobes. The RISC-V port was missing a
> proper fence.i (i$ flushing) after constructing the XOL buffer, which
> can result in incorrect execution of stale/broken instructions.
> 
> This was found running the BPF selftests "test_progs:
> uprobe_autoattach, attach_probe" on the Spacemit K1/X60, where the
> uprobes tests randomly blew up.
> 
> Fixes: 74784081aac8 ("riscv: Add uprobes supported")
> Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
> ---
>  arch/riscv/kernel/probes/uprobes.c | 9 +--------
>  1 file changed, 1 insertion(+), 8 deletions(-)
> 
> diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
> index 4b3dc8beaf77..4faef92dd771 100644
> --- a/arch/riscv/kernel/probes/uprobes.c
> +++ b/arch/riscv/kernel/probes/uprobes.c
> @@ -176,13 +176,6 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
>  		*(uprobe_opcode_t *)dst = __BUG_INSN_32;
>  	}
>  
> +	flush_icache_range((unsigned long)dst, (unsigned long)dst + len);

This works because flush_icache_range currently ignores the range, but
semantically is not quite right, because of the line just above the context that
increments dst. If the range was respected, this would only flush the ebreak,
not the preceding single-stepped instruction.

Regards,
Samuel

>  	kunmap_atomic(kaddr);
> -
> -	/*
> -	 * We probably need flush_icache_user_page() but it needs vma.
> -	 * This should work on most of architectures by default. If
> -	 * architecture needs to do something different it can define
> -	 * its own version of the function.
> -	 */
> -	flush_dcache_page(page);
>  }
> 
> base-commit: 1a1d569a75f3ab2923cb62daf356d102e4df2b86


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

* Re: [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer
  2025-04-18 16:18 ` Samuel Holland
@ 2025-04-19 10:18   ` Björn Töpel
  0 siblings, 0 replies; 4+ messages in thread
From: Björn Töpel @ 2025-04-19 10:18 UTC (permalink / raw)
  To: Samuel Holland, Paul Walmsley, Palmer Dabbelt, Alexandre Ghiti,
	linux-riscv, Guo Ren
  Cc: Björn Töpel, Masami Hiramatsu, Oleg Nesterov,
	Peter Zijlstra, linux-kernel, linux-trace-kernel

Samuel Holland <samuel.holland@sifive.com> writes:

> Hi Björn,
>
> On 2025-04-17 2:49 AM, Björn Töpel wrote:
>> From: Björn Töpel <bjorn@rivosinc.com>
>> 
>> The XOL (execute out-of-line) buffer is used to single-step the
>> replaced instruction(s) for uprobes. The RISC-V port was missing a
>> proper fence.i (i$ flushing) after constructing the XOL buffer, which
>> can result in incorrect execution of stale/broken instructions.
>> 
>> This was found running the BPF selftests "test_progs:
>> uprobe_autoattach, attach_probe" on the Spacemit K1/X60, where the
>> uprobes tests randomly blew up.
>> 
>> Fixes: 74784081aac8 ("riscv: Add uprobes supported")
>> Signed-off-by: Björn Töpel <bjorn@rivosinc.com>
>> ---
>>  arch/riscv/kernel/probes/uprobes.c | 9 +--------
>>  1 file changed, 1 insertion(+), 8 deletions(-)
>> 
>> diff --git a/arch/riscv/kernel/probes/uprobes.c b/arch/riscv/kernel/probes/uprobes.c
>> index 4b3dc8beaf77..4faef92dd771 100644
>> --- a/arch/riscv/kernel/probes/uprobes.c
>> +++ b/arch/riscv/kernel/probes/uprobes.c
>> @@ -176,13 +176,6 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
>>  		*(uprobe_opcode_t *)dst = __BUG_INSN_32;
>>  	}
>>  
>> +	flush_icache_range((unsigned long)dst, (unsigned long)dst + len);
>
> This works because flush_icache_range currently ignores the range, but
> semantically is not quite right, because of the line just above the context that
> increments dst. If the range was respected, this would only flush the ebreak,
> not the preceding single-stepped instruction.

Indeed! That was sloppy! I'll spin a v2!

Thank you!
Björn

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

end of thread, other threads:[~2025-04-19 10:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-17  7:49 [PATCH fixes] riscv: uprobes: Add missing fence.i after building the XOL buffer Björn Töpel
2025-04-18  9:57 ` Guo Ren
2025-04-18 16:18 ` Samuel Holland
2025-04-19 10:18   ` Björn Töpel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).