From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7D7DF31714B for ; Fri, 31 Jul 2026 15:38:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785512328; cv=none; b=tX5yhZ1lz9LkYqLZ3o30qm/o/RuEu6DwHysIvpMz1/O4h7ltTCJMGYz8RRbkpvIqra67sYP6ps1Z3RtYTRPAkkviINPFQXw/YGQAcDkdZxiJWIenwG2VJVfwtaSeyWqukg4sjQO4tNvYH6vu1yPl4houVKZivyRyd55AmLJR1lA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785512328; c=relaxed/simple; bh=a9x4qgIz9Q92tBcQfHvV6I1uFGZab7g0356wUEs9xyU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=No+77bylE6ZOgCeBkEfLaXYdxlBFF//vdBnzXVAkpQkruOMi9QvDMGVVdLILRExqBBH2mqH0ukEHGfyJUed3K1urK58gNo0qGtnCmiKUNyMLbCpzVdIismgdUdbW7N6/My3nfLhwrAl71zHjSIVq7nIyHz2Njcf9W0tZA7iXG3w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b=NIU2U+X/; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.b="NIU2U+X/" Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id DC561204C; Fri, 31 Jul 2026 08:38:41 -0700 (PDT) Received: from J2N7QTR9R3 (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6D0673F66F; Fri, 31 Jul 2026 08:38:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1785512325; bh=a9x4qgIz9Q92tBcQfHvV6I1uFGZab7g0356wUEs9xyU=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NIU2U+X/bzvr6WkhvujPnTYkA2mvpYvPNj89tr79Uz29bb40kfPowWxu2x7+Ez7h5 xPybGNrR5qy0D7sdMCdJTQzQgL4ttnbGKJ7RlBR25CsOmsmBejIBle+UQvCRZI3LcR 2av30pEzSlfnRJVDUsWWEzQWzO4jBgL2ukT7BOPI= Date: Fri, 31 Jul 2026 16:38:41 +0100 From: Mark Rutland To: Hongyan Xia Cc: Will Deacon , Masami Hiramatsu , Catalin Marinas , Jiazi Li , Pu Hu , "linux-kernel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , Ada Couprie Diaz Subject: Re: [RFC PATCH 4/9] arm64/kprobes: Make the single-step machinery noinstr Message-ID: References: <6be9ac705b5dea5f03855e667f14a4967b55e5e2.1785153469.git.hongyan.xia@transsion.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6be9ac705b5dea5f03855e667f14a4967b55e5e2.1785153469.git.hongyan.xia@transsion.com> On Mon, Jul 27, 2026 at 12:25:39PM +0000, Hongyan Xia wrote: > From: Hongyan Xia > > 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? How does that work for other architectures? > - 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()? > - 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 > --- > 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 > #include > #include > +#include > #include > #include > #include > @@ -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 >