From: Hongyan Xia <hongyan.xia@transsion.com>
To: Mark Rutland <mark.rutland@arm.com>
Cc: Pu Hu <hupu@transsion.com>, Jiazi Li <jiazi.li@transsion.com>,
Catalin Marinas <catalin.marinas@arm.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Will Deacon <will@kernel.org>,
"linux-arm-kernel@lists.infradead.org"
<linux-arm-kernel@lists.infradead.org>
Subject: Re: [RFC PATCH 4/9] arm64/kprobes: Make the single-step machinery noinstr
Date: Mon, 3 Aug 2026 04:31:33 +0000 [thread overview]
Message-ID: <7f5a827f-1716-4509-8705-20a67ee22f88@transsion.com> (raw)
In-Reply-To: <amzBgQTasz9u9OA2@J2N7QTR9R3>
On 7/31/2026 11:38 PM, Mark Rutland wrote:
> On Mon, Jul 27, 2026 at 12:25:39PM +0000, Hongyan Xia wrote:
>> From: Hongyan Xia <hongyan.xia@transsion.com>
>>
>> Convert the core of the arm64 kprobe single-step flow to noinstr:
>> save_previous_kprobe(), restore_previous_kprobe(), set_current_kprobe(),
>> kprobes_save_local_irqflag(), kprobes_restore_local_irqflag(),
>> setup_singlestep(), reenter_kprobe() and post_kprobe_handler().
>>
>> These functions only touch per-cpu state, pt_regs and DAIF, except for:
>>
>> - kprobes_inc_nmissed_count(), which is generic and instrumentable; add
>> an arm64-local wrapper that calls it bounded by
>> instrumentation_begin()/end();
>
> Why is it safe to call kprobes_inc_nmissed_count() if it can be
> instrumented?
There is list_for_each_entry_rcu() in that function. With RCU debugging
or proving configs on, messy things like lockdep get in the way. When I
looked at this issue before, my conclusion was it was not possible to
make the kprobes_inc_nmissed_count() path noinstr.
>
> How does that work for other architectures?
I think (other maintainers please correct me if I'm very wrong on this)
arm64 is the first arch to actually attempt Kprobe noinstr. For example,
x86 does Kprobe in a big int3 instrumentation window, so they genuinely
need KPROBE_SS re-entry support and Kprobe page fault check, because
instrumentation can happen. This series is in the other direction:
Instead of patching all the possibilities each time we find one, make
Kprobe noinstr to avoid the whole complexity.
>> - the instruction simulation path in setup_singlestep(), whose decode
>> handlers are instrumentable; bound arch_simulate_insn() with an
>> instrumentation window;
>
> I don't think that's sufficient. If we can instrument that code, then we
> can have unbounded recursion, unless I'm missing something?
>
> I think we need to fix that code to be noinstr safe.
>
> Ada Cc'd was looking into making the insn code generally noinstr-safe,
> but that's a big job. Maybe it's possible to clean up the subset that's
> necessary for arch_simulate_insn()?
That would be fantastic. Yes, I believe this is the correct way to do
noinstr here.
I'll investigate what that subset could be.
>> - the unrecoverable-reentry pr_warn()/dump_kprobe()/BUG() path and the
>> default WARN_ON(), both bounded with instrumentation windows.
>
> That sounds fine.
>
> Mark.
>
>> The __kprobes attribute (notrace + .kprobes.text) is replaced by
>> noinstr, which is a strict superset for these functions.
>>
>> Signed-off-by: Hongyan Xia <hongyan.xia@transsion.com>
>> ---
>> arch/arm64/kernel/probes/kprobes.c | 39 ++++++++++++++++++------------
>> 1 file changed, 24 insertions(+), 15 deletions(-)
>>
>> diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
>> index 4e0efad5caf2..1b12341b2af3 100644
>> --- a/arch/arm64/kernel/probes/kprobes.c
>> +++ b/arch/arm64/kernel/probes/kprobes.c
>> @@ -14,6 +14,7 @@
>> #include <linux/extable.h>
>> #include <linux/kasan.h>
>> #include <linux/kernel.h>
>> +#include <linux/instrumentation.h>
>> #include <linux/kprobes.h>
>> #include <linux/sched/debug.h>
>> #include <linux/set_memory.h>
>> @@ -39,7 +40,7 @@
>> DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
>> DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
>>
>> -static void __kprobes
>> +static void noinstr
>> post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
>>
>> void *alloc_insn_page(void)
>> @@ -170,7 +171,7 @@ void __kprobes arch_remove_kprobe(struct kprobe *p)
>> }
>> }
>>
>> -static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
>> +static void noinstr save_previous_kprobe(struct kprobe_ctlblk *kcb)
>> {
>> kcb->prev_kprobe.kp = kprobe_running();
>> kcb->prev_kprobe.status = kcb->kprobe_status;
>> @@ -184,7 +185,7 @@ static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
>> kcb->prev_kprobe.saved_irqflag = kcb->saved_irqflag;
>> }
>>
>> -static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
>> +static void noinstr restore_previous_kprobe(struct kprobe_ctlblk *kcb)
>> {
>> __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
>> kcb->kprobe_status = kcb->prev_kprobe.status;
>> @@ -197,7 +198,7 @@ static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
>> kcb->saved_irqflag = kcb->prev_kprobe.saved_irqflag;
>> }
>>
>> -static void __kprobes set_current_kprobe(struct kprobe *p)
>> +static void noinstr set_current_kprobe(struct kprobe *p)
>> {
>> __this_cpu_write(current_kprobe, p);
>> }
>> @@ -207,23 +208,23 @@ static void __kprobes set_current_kprobe(struct kprobe *p)
>> * simple and avoid nesting exceptions. Interrupts do have to be disabled since
>> * the kprobe state is per-CPU and doesn't get migrated.
>> */
>> -static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
>> - struct pt_regs *regs)
>> +static void noinstr kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
>> + struct pt_regs *regs)
>> {
>> kcb->saved_irqflag = regs->pstate & DAIF_MASK;
>> regs->pstate |= DAIF_MASK;
>> }
>>
>> -static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
>> - struct pt_regs *regs)
>> +static void noinstr kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
>> + struct pt_regs *regs)
>> {
>> regs->pstate &= ~DAIF_MASK;
>> regs->pstate |= kcb->saved_irqflag;
>> }
>>
>> -static void __kprobes setup_singlestep(struct kprobe *p,
>> - struct pt_regs *regs,
>> - struct kprobe_ctlblk *kcb, int reenter)
>> +static void noinstr setup_singlestep(struct kprobe *p,
>> + struct pt_regs *regs,
>> + struct kprobe_ctlblk *kcb, int reenter)
>> {
>> unsigned long slot;
>>
>> @@ -244,13 +245,15 @@ static void __kprobes setup_singlestep(struct kprobe *p,
>> instruction_pointer_set(regs, slot);
>> } else {
>> /* insn simulation */
>> + instrumentation_begin();
>> arch_simulate_insn(p, regs);
>> + instrumentation_end();
>> }
>> }
>>
>> -static int __kprobes reenter_kprobe(struct kprobe *p,
>> - struct pt_regs *regs,
>> - struct kprobe_ctlblk *kcb)
>> +static int noinstr reenter_kprobe(struct kprobe *p,
>> + struct pt_regs *regs,
>> + struct kprobe_ctlblk *kcb)
>> {
>> switch (kcb->kprobe_status) {
>> case KPROBE_HIT_SSDONE:
>> @@ -262,23 +265,29 @@ static int __kprobes reenter_kprobe(struct kprobe *p,
>> * recoverable one-level reentry, so handle it in the same way as
>> * reentry from KPROBE_HIT_ACTIVE or KPROBE_HIT_SSDONE.
>> */
>> + instrumentation_begin();
>> kprobes_inc_nmissed_count(p);
>> + instrumentation_end();
>> setup_singlestep(p, regs, kcb, 1);
>> break;
>> case KPROBE_REENTER:
>> + instrumentation_begin();
>> pr_warn("Failed to recover from reentered kprobes.\n");
>> dump_kprobe(p);
>> BUG();
>> + instrumentation_end();
>> break;
>> default:
>> + instrumentation_begin();
>> WARN_ON(1);
>> + instrumentation_end();
>> return 0;
>> }
>>
>> return 1;
>> }
>>
>> -static void __kprobes
>> +static void noinstr
>> post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
>> {
>> /* return addr restore if non-branching insn */
>> --
>> 2.47.3
>>
next prev parent reply other threads:[~2026-08-03 4:32 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1785153469.git.hongyan.xia@transsion.com>
2026-07-27 12:25 ` [RFC PATCH 1/9] arm64/entry: Bound certain debug exception paths in instrumentation windows Hongyan Xia
2026-07-31 14:41 ` Mark Rutland
2026-08-03 3:42 ` Hongyan Xia
2026-07-27 12:25 ` [RFC PATCH 2/9] arm64/entry: Make debug_exception_enter/exit() noinstr Hongyan Xia
2026-07-31 14:43 ` Mark Rutland
2026-07-27 12:25 ` [RFC PATCH 3/9] arm64/debug-monitors: Make do_el1_brk64()/do_el1_softstep() noinstr Hongyan Xia
2026-07-31 15:25 ` Mark Rutland
2026-08-03 3:54 ` Hongyan Xia
2026-07-27 12:25 ` [RFC PATCH 4/9] arm64/kprobes: Make the single-step machinery noinstr Hongyan Xia
2026-07-31 15:38 ` Mark Rutland
2026-08-03 4:31 ` Hongyan Xia [this message]
2026-07-27 12:25 ` [RFC PATCH 5/9] arm64/kprobes: Invoke pre/post handlers inside instrumentation Hongyan Xia
2026-07-31 15:44 ` Mark Rutland
2026-08-03 6:11 ` Hongyan Xia
2026-07-27 12:25 ` [RFC PATCH 6/9] arm64/kprobes: Make kprobe_fault_handler() noinstr Hongyan Xia
2026-07-31 15:57 ` Mark Rutland
2026-08-03 6:40 ` Hongyan Xia
2026-07-27 12:25 ` [RFC PATCH 7/9] arm64/kprobes: Drop the KPROBE_HIT_SS reentry special case Hongyan Xia
2026-07-27 12:25 ` [RFC PATCH 8/9] arm64/kprobes: Drop the XOL single-step fault PC check Hongyan Xia
2026-07-31 16:12 ` Mark Rutland
2026-07-27 12:25 ` [RFC PATCH 9/9] arm64/debug: Mark debug exception helpers __always_inline Hongyan Xia
2026-07-27 19:22 ` Nick Desaulniers
2026-07-27 21:49 ` Will Deacon
2026-07-28 2:03 ` Hongyan Xia
2026-07-29 18:08 ` Steven Rostedt
2026-07-30 0:03 ` Masami Hiramatsu
2026-07-30 11:50 ` Hongyan Xia
2026-07-31 16:15 ` Mark Rutland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7f5a827f-1716-4509-8705-20a67ee22f88@transsion.com \
--to=hongyan.xia@transsion.com \
--cc=catalin.marinas@arm.com \
--cc=hupu@transsion.com \
--cc=jiazi.li@transsion.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox