Skip to contentSkip to conversation

arm64: kprobes: Restore local irqflag if kprobes is cancelled

PATCH Applied Jisheng Zhang 3 participants 4 messages Apr 12, 2021 – Apr 13, 2021 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: kprobes: Restore local irqflag if kprobes is cancelled

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.

---
 arch/arm64/kernel/probes/kprobes.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kernel/probes/kprobes.c +4 -2
index 66aac2881ba8..85645b2b0c7a 100644
@@ -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:
Signature · 7 lines
2.31.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw

Hi Jisheng,

Quoted 10 lines · On Mon, 12 Apr 2021 17:41:01 +0800 Jisheng Zhang <[email protected]>…

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,

Quoted 28 lines
---
 arch/arm64/kernel/probes/kprobes.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kernel/probes/kprobes.c +4 -2
index 66aac2881ba8..85645b2b0c7a 100644
@@ -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:
Signature · 1 line
2.31.0
Signature · 1 line
Masami Hiramatsu <[email protected]>
#2 lore raw
Quoted 14 lines · On Mon, 12 Apr 2021 22:11:44 +0900 Masami Hiramatsu <[email protected]> wrote:

Hi Jisheng,

Quoted 9 lines · On Mon, 12 Apr 2021 17:41:01 +0800 Jisheng Zhang <[email protected]>…

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.

Quoted 7 lines
Quoted 1 line

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.

Thanks!

Quoted 37 lines

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

Thank you,

Quoted 28 lines
---
 arch/arm64/kernel/probes/kprobes.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/arm64/kernel/probes/kprobes.c +4 -2
index 66aac2881ba8..85645b2b0c7a 100644
@@ -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:
Signature · 1 line
2.31.0
Signature · 1 line
Masami Hiramatsu <[email protected]>
Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer Applied
#3 lore raw
In reply to Jisheng Zhang
Quoted 10 lines · Jisheng Zhang

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,

Signature · 5 lines
Will

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