Skip to contentSkip to conversation

sparc64: kprobes: Make kprobes work on sun4v

PATCH In review Stian Halseth 3 participants 9 messages Nov 13 – Nov 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover sparc64: kprobes: Make kprobes work on sun4v

Any kprobe hit on a sun4v machine oopses. The probed instruction is single-stepped from memory that is not executable there. Patch 1 moves the copy into an instruction slot from get_insn_slot(), as other architectures do.

Once probes work, three older single-step bugs show up. A taken BPr or FP branch resumes at the wrong address (patch 2). So does a probe on a delay slot instruction (patch 3). An annulled branch can skip the break instruction after the copy (patch 4). Patch 2 overlaps Danish Khateeb's relbranch_fixup() fix from September. Whichever is applied first, the other is a small rebase.

Tested on a SPARC T7-1 and T4-1 (sun4v) and a Sun Fire V240 (sun4u):
entry and return probes, probes on all 50 instructions of __task_pid_nr_ns at once while checking getppid(), ps and /proc, probes on the SPARC AES routines during AF_ALG encryption (sun4v), and refusal of ba,a. Without the series the first probe hit oopses on sun4v. A probe on a taken brnz oopses on sun4u as well.

v2: make flush_insn_slot() a no-op in patch 1 (Masami). The slot is flushed in arch_prepare_kprobe() when it is written. A killed probe has a NULL slot pointer. init_aggr_kprobe() flushed through it. Patch 4 no longer touches the macro. Rebased onto 7.4-rc3. Build tested on the T7-1.
v1: https://lore.kernel.org/all/[email protected]/

Stian Halseth (4):
sparc64: kprobes: Single-step from an executable instruction slot
sparc64: kprobes: Fix up register and FP branches after single-step
sparc64: kprobes: Resume after a delay slot at the saved next PC
sparc64: kprobes: Handle annulled branches

 arch/sparc/include/asm/kprobes.h | 14 +++----
 arch/sparc/kernel/kprobes.c      | 69 +++++++++++++++++++++++++++-----
 2 files changed, 66 insertions(+), 17 deletions(-)

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 sparc64: kprobes: Single-step from an executable instruction slot

kprobes single-steps the probed instruction from kprobe->ainsn.insn[]. The array is embedded in struct kprobe, in the linear mapping. The sun4v linear mapping is not executable. Every probe hit oopses:

sun4v_insn_access_exception: ADDR[fff800005b0cd66c] CTX[0000] TYPE[0006], going.

Allocate the copy with get_insn_slot(), as other architectures do. The slot comes from executable memory in the module area.

flush_insn_slot() flushed the embedded copy after init_aggr_kprobe() had copied it into the aggregator. The aggregator now shares the slot by pointer. arch_prepare_kprobe() flushes the slot when it writes it. Make the macro a no-op, like x86 and arm64.

Fixes c4bce90ea2069 ("[SPARC64]: Deal with PTE layout differences in SUN4V.")
---
 arch/sparc/include/asm/kprobes.h | 12 ++++++------
 arch/sparc/kernel/kprobes.c      | 12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
arch/sparc/include/asm/kprobes.h +6 -6
index aec742cd898f2..d14206c063e25 100644
@@ -11,25 +11,25 @@
 #include <linux/types.h>
 #include <linux/percpu.h>
 
+#define __ARCH_WANT_KPROBES_INSN_SLOT
+
 typedef u32 kprobe_opcode_t;
 
 #define MAX_INSN_SIZE 2
 
 #define kretprobe_blacklist_size 0
 
-#define arch_remove_kprobe(p)	do {} while (0)
+struct kprobe;
+void arch_remove_kprobe(struct kprobe *p);
 
-#define flush_insn_slot(p)		\
-do { 	flushi(&(p)->ainsn.insn[0]);	\
-	flushi(&(p)->ainsn.insn[1]);	\
-} while (0)
+#define flush_insn_slot(p)	do { } while (0)
 
 void __kretprobe_trampoline(void);
 
 /* Architecture specific copy of original instruction*/
 struct arch_specific_insn {
 	/* copy of the original instruction */
-	kprobe_opcode_t insn[MAX_INSN_SIZE];
+	kprobe_opcode_t *insn;
 };
 
 struct prev_kprobe {
arch/sparc/kernel/kprobes.c +12 -0
index 191bbaca99212..608efda8cb948 100644
@@ -52,6 +52,10 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	if ((unsigned long) p->addr & 0x3UL)
 		return -EILSEQ;
 
+	p->ainsn.insn = get_insn_slot();
+	if (!p->ainsn.insn)
+		return -ENOMEM;
+
 	p->ainsn.insn[0] = *p->addr;
 	flushi(&p->ainsn.insn[0]);
 
@@ -62,6 +66,14 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	return 0;
 }
 
+void __kprobes arch_remove_kprobe(struct kprobe *p)
+{
+	if (p->ainsn.insn) {
+		free_insn_slot(p->ainsn.insn, 0);
+		p->ainsn.insn = NULL;
+	}
+}
+
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
 	*p->addr = BREAKPOINT_INSTRUCTION;
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] sparc64: kprobes: Single-step from an executable instruction slot

Quoted 9 lines · Stian Halseth

kprobes single-steps the probed instruction from kprobe->ainsn.insn[]. The array is embedded in struct kprobe, in the linear mapping. The sun4v linear mapping is not executable. Every probe hit oopses:

sun4v_insn_access_exception: ADDR[fff800005b0cd66c] CTX[0000] TYPE[0006], going.

Allocate the copy with get_insn_slot(), as other architectures do. The slot comes from executable memory in the module area.

Quoted 8 lines

flush_insn_slot() flushed the embedded copy after init_aggr_kprobe() had copied it into the aggregator. The aggregator now shares the slot by pointer. arch_prepare_kprobe() flushes the slot when it writes it. Make the macro a no-op, like x86 and arm64.

Fixes c4bce90ea2069 ("[SPARC64]: Deal with PTE layout differences in SUN4V.")

Looks good to me from kprobe point of view.

Thanks,

Quoted 75 lines
---
 arch/sparc/include/asm/kprobes.h | 12 ++++++------
 arch/sparc/kernel/kprobes.c      | 12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
arch/sparc/include/asm/kprobes.h +6 -6
index aec742cd898f2..d14206c063e25 100644
@@ -11,25 +11,25 @@
 #include <linux/types.h>
 #include <linux/percpu.h>
 
+#define __ARCH_WANT_KPROBES_INSN_SLOT
+
 typedef u32 kprobe_opcode_t;
 
 #define MAX_INSN_SIZE 2
 
 #define kretprobe_blacklist_size 0
 
-#define arch_remove_kprobe(p)	do {} while (0)
+struct kprobe;
+void arch_remove_kprobe(struct kprobe *p);
 
-#define flush_insn_slot(p)		\
-do { 	flushi(&(p)->ainsn.insn[0]);	\
-	flushi(&(p)->ainsn.insn[1]);	\
-} while (0)
+#define flush_insn_slot(p)	do { } while (0)
 
 void __kretprobe_trampoline(void);
 
 /* Architecture specific copy of original instruction*/
 struct arch_specific_insn {
 	/* copy of the original instruction */
-	kprobe_opcode_t insn[MAX_INSN_SIZE];
+	kprobe_opcode_t *insn;
 };
 
 struct prev_kprobe {
arch/sparc/kernel/kprobes.c +12 -0
index 191bbaca99212..608efda8cb948 100644
@@ -52,6 +52,10 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	if ((unsigned long) p->addr & 0x3UL)
 		return -EILSEQ;
 
+	p->ainsn.insn = get_insn_slot();
+	if (!p->ainsn.insn)
+		return -ENOMEM;
+
 	p->ainsn.insn[0] = *p->addr;
 	flushi(&p->ainsn.insn[0]);
 
@@ -62,6 +66,14 @@ int __kprobes arch_prepare_kprobe(struct kprobe *p)
 	return 0;
 }
 
+void __kprobes arch_remove_kprobe(struct kprobe *p)
+{
+	if (p->ainsn.insn) {
+		free_insn_slot(p->ainsn.insn, 0);
+		p->ainsn.insn = NULL;
+	}
+}
+
 void __kprobes arch_arm_kprobe(struct kprobe *p)
 {
 	*p->addr = BREAKPOINT_INSTRUCTION;
Signature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#3 lore raw

PATCH v2 2/4 sparc64: kprobes: Fix up register and FP branches after single-step

In reply to Stian Halseth

relbranch_fixup() moves the target of a PC-relative branch from the single-step slot back to the probed instruction. It only knows call, Bicc and BPcc. A taken BPr (brz, brnz, ...) or FBPfcc/FBfcc is left alone. Execution resumes at the target relative to the slot:

  Unable to handle kernel paging request at virtual address 000000000fffff98
  TPC: <0xfffff98>

Treat them like the other PC-relative branches.

---
 arch/sparc/kernel/kprobes.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/sparc/kernel/kprobes.c +6 -3
index 608efda8cb948..38e6d9b51a207 100644
@@ -219,12 +219,15 @@ static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
 	if (regs->tnpc == regs->tpc + 0x4UL)
 		return real_pc + 0x8UL;
 
-	/* The three cases are call, branch w/prediction,
-	 * and traditional branch.
+	/* The cases are call and the branches BPcc, Bicc, BPr, FBPfcc
+	 * and FBfcc.
 	 */
 	if ((insn & 0xc0000000) == 0x40000000 ||
 	    (insn & 0xc1c00000) == 0x00400000 ||
-	    (insn & 0xc1c00000) == 0x00800000) {
+	    (insn & 0xc1c00000) == 0x00800000 ||
+	    (insn & 0xc1c00000) == 0x00c00000 ||
+	    (insn & 0xc1c00000) == 0x01400000 ||
+	    (insn & 0xc1c00000) == 0x01800000) {
 		unsigned long ainsn_addr;
 
 		ainsn_addr = (unsigned long) &p->ainsn.insn[0];
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 3/4 sparc64: kprobes: Resume after a delay slot at the saved next PC

In reply to Stian Halseth

After single-stepping an instruction that does not branch, execution resumes at the saved next PC. The probed address plus 8 comes next. For an instruction in the delay slot of a taken branch the saved next PC is the branch target. Execution then goes back to the probed address plus 8. It should continue at the target plus 4.

Use the saved next PC plus 4. For any other instruction that is the probed address plus 8.

---
 arch/sparc/kernel/kprobes.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/sparc/kernel/kprobes.c +4 -3
index 38e6d9b51a207..327d83188a978 100644
@@ -211,13 +211,14 @@ static int __kprobes kprobe_handler(struct pt_regs *regs)
  * 
  */
 static unsigned long __kprobes relbranch_fixup(u32 insn, struct kprobe *p,
-					       struct pt_regs *regs)
+					       struct pt_regs *regs,
+					       struct kprobe_ctlblk *kcb)
 {
 	unsigned long real_pc = (unsigned long) p->addr;
 
 	/* Branch not taken, no mods necessary.  */
 	if (regs->tnpc == regs->tpc + 0x4UL)
-		return real_pc + 0x8UL;
+		return kcb->kprobe_orig_tnpc + 0x4UL;
 
 	/* The cases are call and the branches BPcc, Bicc, BPr, FBPfcc
 	 * and FBfcc.
@@ -294,7 +295,7 @@ static void __kprobes resume_execution(struct kprobe *p,
 {
 	u32 insn = p->ainsn.insn[0];
 
-	regs->tnpc = relbranch_fixup(insn, p, regs);
+	regs->tnpc = relbranch_fixup(insn, p, regs, kcb);
 
 	/* This assignment must occur after relbranch_fixup() */
 	regs->tpc = kcb->kprobe_orig_tnpc;
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v2 3/4] sparc64: kprobes: Resume after a delay slot at the saved next PC

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] sparc64: kprobes: missing Fixes trailer for relbranch_fixup() delay slot fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] sparc64: kprobes: missing Fixes trailer for relbranch_fixup() delay slot fix
--

commit a946035b56bae1c0df38ae8f1a539756818bbb78
Author: Stian Halseth <[email protected]>

Quoted 8 lines

sparc64: kprobes: Resume after a delay slot at the saved next PC

After single-stepping an instruction that does not branch, execution resumes at the saved next PC. The probed address plus 8 comes next. For an instruction in the delay slot of a taken branch the saved next PC is the branch target. Execution then goes back to the probed address plus 8. It should continue at the target plus 4.

Quoted 5 lines

Use the saved next PC plus 4. For any other instruction that is the probed address plus 8.

[Severity: Low]
This fixes the real_pc + 0x8UL return value in relbranch_fixup(). Should this include:

Fixes f08825896664 ("[SPARC64]: Fix several kprobes bugs.")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#6 lore raw

Re: [PATCH v2 3/4] sparc64: kprobes: Resume after a delay slot at the saved next PC

In reply to Stian Halseth
Quoted 11 lines · Stian Halseth

After single-stepping an instruction that does not branch, execution resumes at the saved next PC. The probed address plus 8 comes next. For an instruction in the delay slot of a taken branch the saved next PC is the branch target. Execution then goes back to the probed address plus 8. It should continue at the target plus 4.

Use the saved next PC plus 4. For any other instruction that is the probed address plus 8.

Fixes f08825896664 ("[SPARC64]: Fix several kprobes bugs.")

Andreas, please add it when applying, or I'll fold it into a v3 if one is needed.

/Stian

#7 lore raw

PATCH v2 4/4 sparc64: kprobes: Handle annulled branches

In reply to Stian Halseth

The single-stepped copy of a branch has a break instruction in its delay slot. An annulled branch that is not taken skips that slot and runs past the copy. A taken ba,a or cbcond branches away without reaching it.

Put a second break instruction after the delay slot. When it is hit, resume after the original delay slot. Refuse probes on ba,a, fba,a and cbcond.

---
 arch/sparc/include/asm/kprobes.h |  2 +-
 arch/sparc/kernel/kprobes.c      | 43 ++++++++++++++++++++++++++++----
 2 files changed, 39 insertions(+), 6 deletions(-)
arch/sparc/include/asm/kprobes.h +1 -1
index d14206c063e25..e15fd84c88681 100644
@@ -15,7 +15,7 @@
 
 typedef u32 kprobe_opcode_t;
 
-#define MAX_INSN_SIZE 2
+#define MAX_INSN_SIZE 3
 
 #define kretprobe_blacklist_size 0
 
arch/sparc/kernel/kprobes.c +38 -5

74 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer Acked-by on the v2 series
#8 lore raw
In reply to Stian Halseth
Quoted 26 lines · Stian Halseth

Any kprobe hit on a sun4v machine oopses. The probed instruction is single-stepped from memory that is not executable there. Patch 1 moves the copy into an instruction slot from get_insn_slot(), as other architectures do.

Once probes work, three older single-step bugs show up. A taken BPr or FP branch resumes at the wrong address (patch 2). So does a probe on a delay slot instruction (patch 3). An annulled branch can skip the break instruction after the copy (patch 4). Patch 2 overlaps Danish Khateeb's relbranch_fixup() fix from September. Whichever is applied first, the other is a small rebase.

Tested on a SPARC T7-1 and T4-1 (sun4v) and a Sun Fire V240 (sun4u):
entry and return probes, probes on all 50 instructions of __task_pid_nr_ns at once while checking getppid(), ps and /proc, probes on the SPARC AES routines during AF_ALG encryption (sun4v), and refusal of ba,a. Without the series the first probe hit oopses on sun4v. A probe on a taken brnz oopses on sun4u as well.

v2: make flush_insn_slot() a no-op in patch 1 (Masami). The slot is flushed in arch_prepare_kprobe() when it is written. A killed probe has a NULL slot pointer. init_aggr_kprobe() flushed through it. Patch 4 no longer touches the macro. Rebased onto 7.4-rc3. Build tested on the T7-1.
v1: https://lore.kernel.org/all/[email protected]/

Quoted 6 lines

Stian Halseth (4):
sparc64: kprobes: Single-step from an executable instruction slot
sparc64: kprobes: Fix up register and FP branches after single-step
sparc64: kprobes: Resume after a delay slot at the saved next PC
sparc64: kprobes: Handle annulled branches

This series looks good to me from kprobes point of view.

David, Andreas, can you pick this series?

Thank you,

Quoted 11 lines
 arch/sparc/include/asm/kprobes.h | 14 +++----
 arch/sparc/kernel/kprobes.c      | 69 +++++++++++++++++++++++++++-----
 2 files changed, 66 insertions(+), 17 deletions(-)

base-commit: b3dfbde62cf1981c4638939c159dfd087b7a72e9

Signature · 1 line
2.43.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>