[PATCH v5 0/3] riscv: probes: check for LR/SC sequence

Vladimir Kondratiev posted 3 patches an hour ago
arch/riscv/include/asm/insn.h                      |  29 ++++
arch/riscv/kernel/probes/decode-insn.c             | 171 +++++++++++++++++++++
arch/riscv/kernel/probes/decode-insn.h             |   7 +
arch/riscv/kernel/probes/kprobes.c                 |  39 +++--
arch/riscv/kernel/probes/uprobes.c                 |  31 ++++
arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S | 148 ++++++++++++++++++
arch/riscv/kernel/tests/kprobes/test-kprobes.c     |  32 ++++
arch/riscv/kernel/tests/kprobes/test-kprobes.h     |   6 +
8 files changed, 453 insertions(+), 10 deletions(-)
[PATCH v5 0/3] riscv: probes: check for LR/SC sequence
Posted by Vladimir Kondratiev an hour ago
Patch 1 fixes page fault reported by sashiko-bot@kernel.org that
existed before this patch series

Patch 2 rejects probes inside the LR/SC sequence. This commit was
initially submitted as patch 1 of the series [1].
Detaching it from that series because it is not depending on other stuff.
Do a Call Flow Graph analysis to correctly detect reachable SC
instructions

Patch 3 adds KUnit test, suggested by
Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com> in [2]

[1] https://patch.msgid.link/20261110-lrsc-only-v6-0-bafc3cc0fcae@mobileye.com
[2] https://lore.kernel.org/linux-riscv/20261019134121.1763-3-yuanxiaofeng@eswincomputing.com/

---
Changes in v5:
- add KUnit tests for text literals inside code and exit branches in the
  LR/SC loop, both forward and backward (patch 3)
- Do a Control Flow Graph analysis to correctly detect reachable SC
  (patch 2)
- allow backward branch inside LR/SC sequence, it can be on exit
  path of the valid LR/SC sequence
- fix LR/SC loop maximum size (patches 2 and 3), reported by
  Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>
- Link to v4: https://patch.msgid.link/20261115-probes-in-lrsc-v4-0-48b9b036bd1d@mobileye.com

Changes in v4:
- Add KUnit test as suggested by Guo Ren <guoren@kernel.org>, based on
  work by Xiaofeng Yuan <yuanxiaofeng@eswincomputing.com>
- Scan for SC up to maximum possible sequence length. CPU does not know
  about symbols, it is not correct to stop scanning at the end of
  symbol. Globally visible symbol placed within LR/SC sequence should
  not turn probe within the sequence into acceptable.
- Link to v3: https://patch.msgid.link/20261112-probes-in-lrsc-v3-0-a95089852a17@mobileye.com

Changes in v3:
- Fix lr/sc loop detection logic for lr at probe position
- Add "Fixes" trailers as suggested by sashiko-bot@kernel.org
- Link to v2: https://patch.msgid.link/20261111-probes-in-lrsc-v2-0-b2525b7e1e8e@mobileye.com

Changes in v2:
- Allow kprobe on the LR itself unless it is part of the LR/SC loop.
- Fix error (reported by sashiko-bot@kernel.org) where
  arch_prepare_kprobe() returns -EILSEQ for LR/SC probes.
  Make codes returned by arch_check_kprobe to indicate -EINVAL
  if probe falls within the LR/SC loop, consistent with the case when
  INSN_REJECTED returned by riscv_probe_decode_insn
- add patch fixing out-of-bounds read in riscv_probe_decode_insn()
  reported by sashiko-bot@kernel.org (as patch 1)
- Link to v1: https://patch.msgid.link/20261111-probes-in-lrsc-v1-1-4f99ddea5f68@mobileye.com

To: Paul Walmsley <pjw@kernel.org>
To: Palmer Dabbelt <palmer@dabbelt.com>
To: Albert Ou <aou@eecs.berkeley.edu>
To: Alexandre Ghiti <alex@ghiti.fr>
To: Masami Hiramatsu <mhiramat@kernel.org>
To: Oleg Nesterov <oleg@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>
To: Guo Ren <guoren@kernel.org>
To: Pekka Enberg <penberg@kernel.org>
To: Patrick Stählin <me@packi.ch>
Cc: linux-riscv@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
Cc: linux-trace-kernel@vger.kernel.org
Cc: Palmer Dabbelt <palmerdabbelt@google.com>
Signed-off-by: Vladimir Kondratiev <vladimir.kondratiev@mobileye.com>

---
Vladimir Kondratiev (3):
      riscv: kprobes: fix out-of-bounds read in riscv_probe_decode_insn
      riscv: probes: reject probes inside an LR/SC sequence
      riscv: kprobes: add KUnit test for LR/SC sequence rejection

 arch/riscv/include/asm/insn.h                      |  29 ++++
 arch/riscv/kernel/probes/decode-insn.c             | 171 +++++++++++++++++++++
 arch/riscv/kernel/probes/decode-insn.h             |   7 +
 arch/riscv/kernel/probes/kprobes.c                 |  39 +++--
 arch/riscv/kernel/probes/uprobes.c                 |  31 ++++
 arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S | 148 ++++++++++++++++++
 arch/riscv/kernel/tests/kprobes/test-kprobes.c     |  32 ++++
 arch/riscv/kernel/tests/kprobes/test-kprobes.h     |   6 +
 8 files changed, 453 insertions(+), 10 deletions(-)
---
base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
change-id: 20261111-probes-in-lrsc-4ccde6cbf436

Best regards,
--  
Vladimir Kondratiev <vladimir.kondratiev@mobileye.com>