riscv: uprobes: Add missing fence.i after building the XOL buffer
PATCH 1/1 riscv: uprobes: Add missing fence.i after building the XOL buffer
From: Björn Töpel <[email protected]>
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.
--- arch/riscv/kernel/probes/uprobes.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
arch/riscv/kernel/probes/uprobes.c +1 -8
@@ -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
Signature · 1 line
2.45.2
Quoted 11 lines · Björn Töpel
From: Björn Töpel <[email protected]>
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!
Quoted 31 lines
Fixes 74784081aac8 ("riscv: Add uprobes supported")Signed-off-by Björn Töpel <[email protected]>--- arch/riscv/kernel/probes/uprobes.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)arch/riscv/kernel/probes/uprobes.c +1
-8@@ -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
Signature · 2 lines
Best Regards Guo Ren
Hi Björn,
Quoted 22 lines · Björn Töpel
From: Björn Töpel <[email protected]>
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 <[email protected]>--- arch/riscv/kernel/probes/uprobes.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)arch/riscv/kernel/probes/uprobes.c +0
-0
arch/riscv/kernel/probes/uprobes.c void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, · line 179
Quoted 4 lines
arch/riscv/kernel/probes/uprobes.c +1
-0@@ -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
Quoted 12 lines
arch/riscv/kernel/probes/uprobes.c +0
-8kunmap_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
Quoted 28 lines · Samuel Holland
Hi Björn,
Quoted 25 lines · Björn Töpel
From: Björn Töpel <[email protected]>
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 <[email protected]>--- arch/riscv/kernel/probes/uprobes.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)arch/riscv/kernel/probes/uprobes.c +0
-0@@ -176,13 +176,6 @@ void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, *(uprobe_opcode_t *)dst = __BUG_INSN_32; }
Quoted 6 lines
arch/riscv/kernel/probes/uprobes.c void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, · line 179
Quoted 1 line
arch/riscv/kernel/probes/uprobes.c +1
-0+flush_icache_range ((unsigned long )dst, (unsigned long )dst + len);This works because
flush_icache_rangecurrently 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