linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled
@ 2021-04-12  9:41 Jisheng Zhang
  2021-04-12 13:11 ` Masami Hiramatsu
  2021-04-13 17:49 ` Will Deacon
  0 siblings, 2 replies; 4+ messages in thread
From: Jisheng Zhang @ 2021-04-12  9:41 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Masami Hiramatsu
  Cc: linux-arm-kernel, linux-kernel, Liao Chang

If instruction being single stepped caused a page fault, the kprobes
is cancelled to let the page fault handler continue as a normal page
fault. But the local irqflags are disabled so cpu will restore pstate
with DAIF masked. After pagefault is serviced, the kprobes is
triggerred again, we overwrite the saved_irqflag by calling
kprobes_save_local_irqflag(). NOTE, DAIF is masked in this new saved
irqflag. After kprobes is serviced, the cpu pstate is retored with
DAIF masked.

This patch is inspired by one patch for riscv from Liao Chang.

Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com>
---
 arch/arm64/kernel/probes/kprobes.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
index 66aac2881ba8..85645b2b0c7a 100644
--- a/arch/arm64/kernel/probes/kprobes.c
+++ b/arch/arm64/kernel/probes/kprobes.c
@@ -267,10 +267,12 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
 		if (!instruction_pointer(regs))
 			BUG();
 
-		if (kcb->kprobe_status == KPROBE_REENTER)
+		if (kcb->kprobe_status == KPROBE_REENTER) {
 			restore_previous_kprobe(kcb);
-		else
+		} else {
+			kprobes_restore_local_irqflag(kcb, regs);
 			reset_current_kprobe();
+		}
 
 		break;
 	case KPROBE_HIT_ACTIVE:
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled
  2021-04-12  9:41 [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled Jisheng Zhang
@ 2021-04-12 13:11 ` Masami Hiramatsu
  2021-04-12 14:46   ` Masami Hiramatsu
  2021-04-13 17:49 ` Will Deacon
  1 sibling, 1 reply; 4+ messages in thread
From: Masami Hiramatsu @ 2021-04-12 13:11 UTC (permalink / raw)
  To: Jisheng Zhang
  Cc: Catalin Marinas, Will Deacon, linux-arm-kernel, linux-kernel, Liao Chang

Hi Jisheng,

On Mon, 12 Apr 2021 17:41:01 +0800
Jisheng Zhang <Jisheng.Zhang@synaptics.com> wrote:

> If instruction being single stepped caused a page fault, the kprobes
> is cancelled to let the page fault handler continue as a normal page
> fault. But the local irqflags are disabled so cpu will restore pstate
> with DAIF masked. After pagefault is serviced, the kprobes is
> triggerred again, we overwrite the saved_irqflag by calling
> kprobes_save_local_irqflag(). NOTE, DAIF is masked in this new saved
> irqflag. After kprobes is serviced, the cpu pstate is retored with
> DAIF masked.
> 
> This patch is inspired by one patch for riscv from Liao Chang.

Thanks for pointing it out. But I think kprobes_restore_local_irqflag()
is also needed for kcb->kprobe_status == KPROBE_REENTER case...no.
This is more complicated. In the reenter case, I think we have to retry
the kpreprobe_fault_handler() with recovered previous kprobes so that
it can handle page fault in its handler.

Hmm, RISC-V and other code also needs same fix.

Thank you,

> 
> Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com>
> ---
>  arch/arm64/kernel/probes/kprobes.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
> index 66aac2881ba8..85645b2b0c7a 100644
> --- a/arch/arm64/kernel/probes/kprobes.c
> +++ b/arch/arm64/kernel/probes/kprobes.c
> @@ -267,10 +267,12 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
>  		if (!instruction_pointer(regs))
>  			BUG();
>  
> -		if (kcb->kprobe_status == KPROBE_REENTER)
> +		if (kcb->kprobe_status == KPROBE_REENTER) {
>  			restore_previous_kprobe(kcb);
> -		else
> +		} else {
> +			kprobes_restore_local_irqflag(kcb, regs);
>  			reset_current_kprobe();
> +		}
>  
>  		break;
>  	case KPROBE_HIT_ACTIVE:
> -- 
> 2.31.0
> 


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled
  2021-04-12 13:11 ` Masami Hiramatsu
@ 2021-04-12 14:46   ` Masami Hiramatsu
  0 siblings, 0 replies; 4+ messages in thread
From: Masami Hiramatsu @ 2021-04-12 14:46 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Jisheng Zhang, Catalin Marinas, Will Deacon, linux-arm-kernel,
	linux-kernel, Liao Chang

On Mon, 12 Apr 2021 22:11:44 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:

> Hi Jisheng,
> 
> On Mon, 12 Apr 2021 17:41:01 +0800
> Jisheng Zhang <Jisheng.Zhang@synaptics.com> wrote:
> 
> > If instruction being single stepped caused a page fault, the kprobes
> > is cancelled to let the page fault handler continue as a normal page
> > fault. But the local irqflags are disabled so cpu will restore pstate
> > with DAIF masked. After pagefault is serviced, the kprobes is
> > triggerred again, we overwrite the saved_irqflag by calling
> > kprobes_save_local_irqflag(). NOTE, DAIF is masked in this new saved
> > irqflag. After kprobes is serviced, the cpu pstate is retored with
> > DAIF masked.
> > 
> > This patch is inspired by one patch for riscv from Liao Chang.
> 
> Thanks for pointing it out. But I think kprobes_restore_local_irqflag()
> is also needed for kcb->kprobe_status == KPROBE_REENTER case...no.
> This is more complicated. In the reenter case, I think we have to retry
> the kpreprobe_fault_handler() with recovered previous kprobes so that
> it can handle page fault in its handler.

Ah, this is another issue, and needs another fix.

So this patch itself is good to me.

Acked-by: Masami Hiramatsu <mhiramat@kernel.org>

Thanks!

> 
> Hmm, RISC-V and other code also needs same fix.
> 
> Thank you,
> 
> > 
> > Signed-off-by: Jisheng Zhang <Jisheng.Zhang@synaptics.com>
> > ---
> >  arch/arm64/kernel/probes/kprobes.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/arm64/kernel/probes/kprobes.c b/arch/arm64/kernel/probes/kprobes.c
> > index 66aac2881ba8..85645b2b0c7a 100644
> > --- a/arch/arm64/kernel/probes/kprobes.c
> > +++ b/arch/arm64/kernel/probes/kprobes.c
> > @@ -267,10 +267,12 @@ int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
> >  		if (!instruction_pointer(regs))
> >  			BUG();
> >  
> > -		if (kcb->kprobe_status == KPROBE_REENTER)
> > +		if (kcb->kprobe_status == KPROBE_REENTER) {
> >  			restore_previous_kprobe(kcb);
> > -		else
> > +		} else {
> > +			kprobes_restore_local_irqflag(kcb, regs);
> >  			reset_current_kprobe();
> > +		}
> >  
> >  		break;
> >  	case KPROBE_HIT_ACTIVE:
> > -- 
> > 2.31.0
> > 
> 
> 
> -- 
> Masami Hiramatsu <mhiramat@kernel.org>


-- 
Masami Hiramatsu <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled
  2021-04-12  9:41 [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled Jisheng Zhang
  2021-04-12 13:11 ` Masami Hiramatsu
@ 2021-04-13 17:49 ` Will Deacon
  1 sibling, 0 replies; 4+ messages in thread
From: Will Deacon @ 2021-04-13 17:49 UTC (permalink / raw)
  To: Masami Hiramatsu, Catalin Marinas, Jisheng Zhang
  Cc: kernel-team, Will Deacon, linux-arm-kernel, linux-kernel, Liao Chang

On Mon, 12 Apr 2021 17:41:01 +0800, Jisheng Zhang wrote:
> If instruction being single stepped caused a page fault, the kprobes
> is cancelled to let the page fault handler continue as a normal page
> fault. But the local irqflags are disabled so cpu will restore pstate
> with DAIF masked. After pagefault is serviced, the kprobes is
> triggerred again, we overwrite the saved_irqflag by calling
> kprobes_save_local_irqflag(). NOTE, DAIF is masked in this new saved
> irqflag. After kprobes is serviced, the cpu pstate is retored with
> DAIF masked.
> 
> [...]

Applied to arm64 (for-next/fixes), thanks!

[1/1] arm64: kprobes: Restore local irqflag if kprobes is cancelled
      https://git.kernel.org/arm64/c/738fa58ee132

Cheers,
-- 
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-04-13 17:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-12  9:41 [PATCH] arm64: kprobes: Restore local irqflag if kprobes is cancelled Jisheng Zhang
2021-04-12 13:11 ` Masami Hiramatsu
2021-04-12 14:46   ` Masami Hiramatsu
2021-04-13 17:49 ` Will Deacon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).