riscv: kprobes: reject probes inside LR/SC sequences
v7 cover riscv: kprobes: reject probes inside LR/SC sequences
A breakpoint trap taken in the middle of an LR/SC sequence clears the load reservation, so an SC following the probed instruction always fails and the enclosing retry loop re-enters the breakpoint, livelocking the CPU. This series makes the RISC-V kprobes implementation reject probes placed on an LR/SC instruction or between them, following arm64's approach for its equivalent exclusive load/store instructions.
Changes since v6:
- Fix an off-by-one in the LR/SC tracking window: a constrained loop holds at most 16 instructions (the LR, the SC, and up to 14 between them), and the walk now stops tracking after 14 intermediate instructions, as the spec requires.
- Use the existing RV_EXTRACT_FUNCT3()/RV_EXTRACT_RS2_REG() helpers and add RV_EXTRACT_FUNCT5() in riscv_insn_is_lr()/riscv_insn_is_sc() to avoid code duplication.
- Rename test_kprobes_lrsc_offsets[] and split it into separate test_kprobes_lrsc_allowed_addresses[]/test_kprobes_lrsc_rejected_ addresses[] so that adding an entry does not force a change in a parallel reject[] table.
- Add a KUnit boundary case pinning the 16-instruction tracking window:
the 15th instruction after an LR must be rejected and the 16th must be accepted (this fails without the off-by-one fix).
Verified by cross-compiling for RISC-V and running the kprobes KUnit test in QEMU (ok 2 kprobes_riscv).
Xiaofeng Yuan (2):
riscv: kprobes: reject probes inside LR/SC sequences
riscv: kprobes: add KUnit test for LR/SC sequence rejection
arch/riscv/include/asm/insn.h | 63 ++++++++++++++---- arch/riscv/kernel/probes/decode-insn.c | 2 + arch/riscv/kernel/probes/kprobes.c | 65 +++++++++++++++---- .../kernel/tests/kprobes/test-kprobes-asm.S | 60 +++++++++++++++++ .../riscv/kernel/tests/kprobes/test-kprobes.c | 34 ++++++++++ .../riscv/kernel/tests/kprobes/test-kprobes.h | 6 ++ 6 files changed, 207 insertions(+), 23 deletions(-)
Signature · 1 line
2.34.1
PATCH v7 1/2 riscv: kprobes: reject probes inside LR/SC sequences
A trap taken in the middle of an LR/SC sequence clears the load reservation, so an SC following the probed instruction always fails and the enclosing retry loop re-enters the breakpoint, livelocking the CPU. Reject probes of the LR/SC instructions themselves and of any address between them.
arm64's kprobes rejects its equivalent exclusive load/store instructions and, in arm_kprobe_decode_insn(), walks backward from the probe address (bounded by the enclosing symbol) to reject any address inside an exclusive sequence. RISC-V cannot walk backward because variable-length instructions make instruction boundaries ambiguous, so walk forward from the enclosing symbol, a known instruction boundary, up to the probe address and track whether an LR is still outstanding.
A Zalrsc-constrained LR/SC retry loop holds at most 16 instructions: the LR, the SC, and up to MAX_LRSC_GAP instructions between them. An LR MAX_LRSC_GAP or more instructions ahead of the probe cannot start a sequence containing it. Count instructions, not bytes: compressed instructions would make a byte window ambiguous.
The LR/SC instructions themselves are rejected in riscv_probe_decode_insn(), and an address inside a sequence is rejected in arch_check_kprobe().
--- arch/riscv/include/asm/insn.h | 63 ++++++++++++++++++++----- arch/riscv/kernel/probes/decode-insn.c | 2 + arch/riscv/kernel/probes/kprobes.c | 65 +++++++++++++++++++++----- 3 files changed, 107 insertions(+), 23 deletions(-)
arch/riscv/include/asm/insn.h +52 -11
@@ -10,6 +10,8 @@#define RV_INSN_FUNCT3_MASK GENMASK(14, 12)#define RV_INSN_FUNCT3_OPOFF 12 +#define RV_INSN_FUNCT5_MASK GENMASK(31, 27) +#define RV_INSN_FUNCT5_OPOFF 27#define RV_INSN_OPCODE_MASK GENMASK(6, 0)#define RV_INSN_OPCODE_OPOFF 0#define RV_INSN_FUNCT12_OPOFF 20
@@ -141,6 +143,7 @@#define RVG_OPCODE_JALR 0x67#define RVG_OPCODE_JAL 0x6f#define RVG_OPCODE_SYSTEM 0x73 +#define RVG_OPCODE_AMO 0x2f#define RVG_SYSTEM_CSR_OFF 20#define RVG_SYSTEM_CSR_MASK GENMASK(11, 0)
@@ -175,6 +178,8 @@#define RVG_FUNCT3_BGE 0x5#define RVG_FUNCT3_BLTU 0x6#define RVG_FUNCT3_BGEU 0x7 +#define RVG_FUNCT3_AMO_W 0x2 +#define RVG_FUNCT3_AMO_D 0x3/* parts of funct3 code for C extension*/ #define RVC_FUNCT3_C_BEQZ 0x6
@@ -263,6 +268,53 @@ __RISCV_INSN_FUNCS(ebreak, RVG_MASK_EBREAK, RVG_MATCH_EBREAK) __RISCV_INSN_FUNCS(sret, RVG_MASK_SRET, RVG_MATCH_SRET)__RISCV_INSN_FUNCS (fence,RVG_MASK_FENCE ,RVG_MATCH_FENCE ); +/* +* LR/SC are identified by the AMO opcode plus the funct3 width field +* (.w/.d) and the funct5 field (funct7[6:2]) distinguishing LR from SC. +* The aq/rl bits (funct7[1:0]) are ignored. For LR the rs2 field must +* encode x0. +*/ +#define RVG_AMO_FUNCT5_LR 0x02 +#define RVG_AMO_FUNCT5_SC 0x03 + +#define RV_X_MASK (X , s, mask) (((X) >> (s)) & (mask)) +#define RV_X (X , s, n) RV_X_MASK(X, s, ((1 << (n)) - 1)) + +#define RV_EXTRACT_FUNCT3 (x) \ + ({typeof(x) x_ = (x); \ + (RV_X_MASK(x_, RV_INSN_FUNCT3_OPOFF, \ + RV_INSN_FUNCT3_MASK >> RV_INSN_FUNCT3_OPOFF)); }) + +#define RV_EXTRACT_FUNCT5 (x) \ + ({typeof(x) x_ = (x); \ + (RV_X_MASK(x_, RV_INSN_FUNCT5_OPOFF, \ + RV_INSN_FUNCT5_MASK >> RV_INSN_FUNCT5_OPOFF)); }) + +#define RV_EXTRACT_RS2_REG (x) \ + ({typeof(x) x_ = (x); \ + (RV_X_MASK(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); }) + +static __always_inline bool riscv_insn_is_lr(u32 code) +{ +u32 funct5 =RV_EXTRACT_FUNCT5 (code); +u32 funct3 =RV_EXTRACT_FUNCT3 (code); + +return (code &RV_INSN_OPCODE_MASK ) ==RVG_OPCODE_AMO && + funct5 ==RVG_AMO_FUNCT5_LR && + (funct3 ==RVG_FUNCT3_AMO_W || funct3 ==RVG_FUNCT3_AMO_D ) && + !RV_EXTRACT_RS2_REG (code); +} + +static __always_inline bool riscv_insn_is_sc(u32 code) +{ +u32 funct5 =RV_EXTRACT_FUNCT5 (code); +u32 funct3 =RV_EXTRACT_FUNCT3 (code); + +return (code &RV_INSN_OPCODE_MASK ) ==RVG_OPCODE_AMO && + funct5 ==RVG_AMO_FUNCT5_SC && + (funct3 ==RVG_FUNCT3_AMO_W || funct3 ==RVG_FUNCT3_AMO_D ); +} +/* special case to catch _any_ system instruction */ static __always_inline bool riscv_insn_is_system(u32 code) {
@@ -432,8 +484,6 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)#define RV_IMM_SIGN (x) (-(((x) >> 31) & 1))#define RVC_IMM_SIGN (x) (-(((x) >> 12) & 1)) -#define RV_X_MASK (X , s, mask) (((X) >> (s)) & (mask)) -#define RV_X (X , s, n) RV_X_MASK(X, s, ((1 << (n)) - 1))#define RVC_LW_IMM (x) ((RV_X(x, 6, 1) << 2) | \ (RV_X(x, 10, 3) << 3) | \ (RV_X(x, 5, 1) << 6))
@@ -454,19 +504,10 @@ static __always_inline bool riscv_insn_is_c_jalr(u32 code)#define RVC_RS2 (insn) RV_X(insn, SH_RS2C, 5)#define RVC_X (X , s, mask) RV_X_MASK(X, s, mask) -#define RV_EXTRACT_FUNCT3 (x) \ - ({typeof(x) x_ = (x); \ - (RV_X_MASK(x_, RV_INSN_FUNCT3_OPOFF, \ - RV_INSN_FUNCT3_MASK >> RV_INSN_FUNCT3_OPOFF)); }) -#define RV_EXTRACT_RS1_REG (x) \ ({typeof(x) x_ = (x); \ (RV_X_MASK(x_, RVG_RS1_OPOFF, RVG_RS1_MASK)); }) -#define RV_EXTRACT_RS2_REG (x) \ - ({typeof(x) x_ = (x); \ - (RV_X_MASK(x_, RVG_RS2_OPOFF, RVG_RS2_MASK)); }) -#define RV_EXTRACT_RD_REG (x) \ ({typeof(x) x_ = (x); \ (RV_X_MASK(x_, RVG_RD_OPOFF, RVG_RD_MASK)); })
arch/riscv/kernel/probes/decode-insn.c +2 -0
@@ -23,6 +23,8 @@ riscv_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *api)*/ RISCV_INSN_REJECTED (system, insn);RISCV_INSN_REJECTED (fence, insn); +RISCV_INSN_REJECTED (lr, insn); +RISCV_INSN_REJECTED (sc, insn);/* * Simulate instructions list:
arch/riscv/kernel/probes/kprobes.c +53 -12
99 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v7 2/2 riscv: kprobes: add KUnit test for LR/SC sequence rejection
In reply to Xiaofeng YuanExtend the kprobes KUnit test with a function containing an LR/SC sequence and assert that register_kprobe() accepts a probe placed right before or right after the sequence, and rejects a probe placed on the LR, on an instruction inside the sequence, or on the SC.
Add a second, never-executed function that pins the 16-instruction tracking window: the 15th instruction after the LR (the last that can still be inside the loop) must be rejected, and the 16th must be accepted because it is either the SC (rejected during decoding) or a plain instruction outside the loop.
--- .../kernel/tests/kprobes/test-kprobes-asm.S | 60 +++++++++++++++++++ .../riscv/kernel/tests/kprobes/test-kprobes.c | 34 +++++++++++ .../riscv/kernel/tests/kprobes/test-kprobes.h | 6 ++ 3 files changed, 100 insertions(+)
arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S +60 -0
@@ -100,6 +100,49 @@ test_kprobes_branch_addr8: .option pop SYM_FUNC_END(test_kprobes_branch) +/* + * This function is never executed; it only provides the probe addresses + * in test_kprobes_lrsc_allowed_addresses[] and + * test_kprobes_lrsc_rejected_addresses[] for test_kprobe_lrsc(). The + * lr.w/sc.w use the invalid address 0(x0) and would fault if executed, + * so do not add this function to test_kprobes_functions[]. + */ +SYM_FUNC_START(test_kprobes_lrsc) +.Llrsc_before: + nop +1: +.Llrsc_lr: + lr.w t0, 0(x0) +.Llrsc_inside: + addi t0, t0, 1 +.Llrsc_sc: + sc.w t0, t0, 0(x0) + bnez t0, 1b +.Llrsc_after: + ret +SYM_FUNC_END(test_kprobes_lrsc) + +/* + * Also never executed; it pins the 16-instruction LR/SC tracking window. + * The LR is followed by 13 nops. The 15th instruction (counting the LR + * as the 1st) is the last one that can still be inside the loop and must + * be rejected. The 16th is accepted: it is either the SC, rejected later + * during decoding, or a plain instruction, which cannot lie inside a + * 16-instruction loop because that loop's 16th instruction must be the SC. + */ +SYM_FUNC_START(test_kprobes_lrsc_gap) +.Llrsc_gap_lr: + lr.w t0, 0(x0) + .rept 13 + nop + .endr +.Llrsc_gap_pos15: + nop +.Llrsc_gap_pos16: + nop + ret +SYM_FUNC_END(test_kprobes_lrsc_gap) + #ifdef CONFIG_RISCV_ISA_C SYM_FUNC_START(test_kprobes_c_j)
@@ -253,3 +296,20 @@ SYM_DATA_START(test_kprobes_functions) #endif /* CONFIG_RISCV_ISA_C */ RISCV_PTR 0 SYM_DATA_END(test_kprobes_functions) + +.section .rodata +SYM_DATA_START(test_kprobes_lrsc_allowed_addresses) + RISCV_PTR .Llrsc_before + RISCV_PTR .Llrsc_after + RISCV_PTR .Llrsc_gap_pos16 + RISCV_PTR 0 +SYM_DATA_END(test_kprobes_lrsc_allowed_addresses) + +.section .rodata +SYM_DATA_START(test_kprobes_lrsc_rejected_addresses) + RISCV_PTR .Llrsc_lr + RISCV_PTR .Llrsc_inside + RISCV_PTR .Llrsc_sc + RISCV_PTR .Llrsc_gap_pos15 + RISCV_PTR 0 +SYM_DATA_END(test_kprobes_lrsc_rejected_addresses)
arch/riscv/kernel/tests/kprobes/test-kprobes.c +34 -0
@@ -43,8 +43,42 @@ static void test_kprobe_riscv(struct kunit *test)kfree (kp); } +static void test_kprobe_lrsc (struct kunit *test) +{ +int i; + +/* probes outside the LR/SC sequence must be accepted */ +for (i =0 ; test_kprobes_lrsc_allowed_addresses[i]; i++) { +struct kprobe kp = {}; +int ret; + + kp.pre_handler = kprobe_dummy_handler; + kp.addr = test_kprobes_lrsc_allowed_addresses[i]; + + ret =register_kprobe (&kp); +KUNIT_EXPECT_EQ (test, ret,0 ); +if (!ret) +unregister_kprobe (&kp); + } + +/* probes on or inside the LR/SC sequence must be rejected */ +for (i =0 ; test_kprobes_lrsc_rejected_addresses[i]; i++) { +struct kprobe kp = {}; +int ret; + + kp.pre_handler = kprobe_dummy_handler; + kp.addr = test_kprobes_lrsc_rejected_addresses[i]; + + ret =register_kprobe (&kp); +KUNIT_EXPECT_LT (test, ret,0 ); +if (!ret) +unregister_kprobe (&kp); + } +} +static struct kunit_case kprobes_testcases[] = {KUNIT_CASE (test_kprobe_riscv), +KUNIT_CASE (test_kprobe_lrsc), {} };
arch/riscv/kernel/tests/kprobes/test-kprobes.h +6 -0
@@ -19,6 +19,12 @@ extern void *test_kprobes_addresses[];/* array of functions that return KPROBE_TEST_MAGIC */ extern long (*test_kprobes_functions[])(void ); +/* probe points that must be accepted, terminated by NULL */ +extern void *test_kprobes_lrsc_allowed_addresses[]; + +/* probe points that must be rejected, terminated by NULL */ +extern void *test_kprobes_lrsc_rejected_addresses[]; +#endif /* __ASSEMBLER__ */ #endif /* TEST_KPROBES_H */
Signature · 1 line
2.34.1
Gentle ping. Is there anything I still need to address on this series? Happy to rebase onto a current linux-next if that makes it easier to apply.