Re: [PATCH v4 0/3] riscv: probes: reject probes inside an LR/SC sequence
From: Vladimir Kondratiev
Date: Wed Nov 18 2026 - 10:44:26 EST
On 11/18/26 12:52, 袁晓峰 wrote:
> Hi Vladimir, Nam, Masami,
> Vladimir, your forward scan also covers uprobes
> and does not suffer from the lone-lr false rejection that our window-based check had,
> so your approach is the better base; I would rather continue the fix on top of
> your series than push ours.
I discovered fundamental flaw in this simple forward scan approach. For
example it can't detect a literal inside the code. In the next version I
implemented a Control Flow Graph analysis.
> 1) Window size
>
> Spec 13.3 defines the constrained LR/SC loop as the LR/SC sequence plus
> the code to retry it, at most 16 instructions in total. That leaves at
> most 14 instructions between the lr and the sc (fewer once the retry
> branch is counted). RISCV_LRSC_MAX_BODY=16 is larger than that; could we
> align the bound with the spec? We had an off-by-one round on this with Nam
> in our series.
Thanks for this. I misread the spec in a way there's at max 16
instructions between LR and SC, this is wrong. Spec says, reduced quote:
---cut---
The loop comprises only an LR/SC sequence and code to retry the sequence
in the case of failure, and must comprise at most 16 instructions placed
sequentially in memory.
An LR/SC sequence begins with an LR instruction and ends with an SC
instruction. [...]
The code to retry a failing LR/SC sequence can contain backwards jumps
and/or branches to repeat the LR/SC sequence [...]
---cut---
There's 16 instructions limit for the
- LR
- dynamic code between LR/SC
- SC
- code to retry
I will update my defines. I can't analyze "code to retry" and don't know
where is the LR instruction, effectively maintaining 64-byte window to
scan for the SC. Assumption is only valid constrained LR/SC loops are
used, unconstrained LR/SC loops not supported.
> Three smaller things:
>
> - KUNIT_EXPECT_LT(ret, 0) also accepts -EILSEQ (or any other negative
> error), so it can pass even when the LR/SC rejection did not happen.
> -EINVAL would pin the specific reason.
Good point.
> - There is no allow-direction boundary, so a window that is too wide
> would still pass. A case just outside the (spec-correct) window,
> expected to be accepted, would pin the bound from both sides.
This is because I can't find LR; this is correct for both kprobes and
uprobes. For uprobes - because I have no symbolic information; for
kprobes - if one put global label inside the function, scan from the
nearest symbol won't work anymore. But assuming every SC belongs to
valid constrained LR/SC loop, I don't need it. This code will not detect
badly formed LR/SC loop.
> - The max-length case is lr + 16 nops + sc, which is neither a loop nor
> within the 16-instruction spec bound. A real loop (lr + 13 instructions
> + sc + retry branch) would test what the spec actually constrains.
Yes, will fix
One more note, I have unanswered question, it can alter part of the
riscv_insn_is_lrsc_body that deals with branches:
https://lore.kernel.org/all/a971d355-a7bd-41a8-83ba-a250e77f78f1@xxxxxxxxxxxx/
Thanks, Vladimir