Skip to contentSkip to conversation

kprobe: Optimize the performance of patching ss

PATCH Superseded Liao Chang 3 participants 7 messages Sep 13, 2022 – Sep 23, 2022

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Liao Chang

author
#0 lore raw

v2 cover kprobe: Optimize the performance of patching ss

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

v2:
Backport riscv patch to cksy and arm64.

Liao Chang (3):
riscv/kprobe: Optimize the performance of patching single-step slot csky/kprobe: Optimize the performance of patching single-step slot arm64/kprobe: Optimize the performance of patching single-step slot

 arch/arm64/kernel/probes/kprobes.c | 5 ++---
 arch/csky/kernel/probes/kprobes.c  | 6 +++++-
 arch/riscv/kernel/probes/kprobes.c | 8 +++++---
 3 files changed, 12 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/3 riscv/kprobe: Optimize the performance of patching single-step slot

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

---
 arch/riscv/kernel/probes/kprobes.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/riscv/kernel/probes/kprobes.c +5 -3
index e6e950b7cf32..bc1f39b96e41 100644
@@ -24,12 +24,14 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
 	unsigned long offset = GET_INSN_LENGTH(p->opcode);
+	kprobe_opcode_t slot[MAX_INSN_SIZE];
 
 	p->ainsn.api.restore = (unsigned long)p->addr + offset;
 
-	patch_text(p->ainsn.api.insn, p->opcode);
-	patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
-		   __BUG_INSN_32);
+	memcpy(slot, &p->opcode, offset);
+	*(kprobe_opcode_t *)((unsigned long)slot + offset) = __BUG_INSN_32;
+	patch_text_nosync(p->ainsn.api.insn, slot,
+			  offset + GET_INSN_LENGTH(__BUG_INSN_32));
 }
 
 static void __kprobes arch_prepare_simulate(struct kprobe *p)
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/3 csky/kprobe: Optimize the performance of patching single-step slot

In reply to Liao Chang

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

---
 arch/csky/kernel/probes/kprobes.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/csky/kernel/probes/kprobes.c +5 -1
index 3c6e5c725d81..4feb5ce16264 100644
@@ -57,7 +57,11 @@ static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 
 	p->ainsn.api.restore = (unsigned long)p->addr + offset;
 
-	patch_text(p->ainsn.api.insn, p->opcode);
+	memcpy(p->ainsn.api.insn, &p->opcode, offset);
+	dcache_wb_range((unsigned long)p->ainsn.api.insn,
+			(unsigned long)p->ainsn.api.insn + offset);
+	icache_inv_range((unsigned long)p->ainsn.api.insn,
+			 (unsigned long)p->ainsn.api.insn + offset);
 }
 
 static void __kprobes arch_prepare_simulate(struct kprobe *p)
Signature · 1 line
2.17.1
author
#3 lore raw

PATCH v2 3/3 arm64/kprobe: Optimize the performance of patching single-step slot

In reply to Liao Chang

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

---
 arch/arm64/kernel/probes/kprobes.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/kernel/probes/kprobes.c +2 -3
index d1d182320245..5902e33fd3b6 100644
@@ -44,11 +44,10 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {
 	kprobe_opcode_t *addr = p->ainsn.api.insn;
-	void *addrs[] = {addr, addr + 1};
-	u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
 
 	/* prepare insn slot */
-	aarch64_insn_patch_text(addrs, insns, 2);
+	aarch64_insn_write(addr, p->opcode);
+	aarch64_insn_write(addr + 1, BRK64_OPCODE_KPROBES_SS);
 
 	flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));
 
Signature · 1 line
2.17.1
maintainer
#4 lore raw

Re: [PATCH V2 3/3] arm64/kprobe: Optimize the performance of patching single-step slot

Quoted 16 lines · Liao Chang

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

---
 arch/arm64/kernel/probes/kprobes.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/kernel/probes/kprobes.c +0 -0
index d1d182320245..5902e33fd3b6 100644
@@ -44,11 +44,10 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {

arch/arm64/kernel/probes/kprobes.c post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *); · line 52

Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +2 -3
 	kprobe_opcode_t *addr = p->ainsn.api.insn;
-	void *addrs[] = {addr, addr + 1};
-	u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
 
 	/* prepare insn slot */
-	aarch64_insn_patch_text(addrs, insns, 2);
+	aarch64_insn_write(addr, p->opcode);
+	aarch64_insn_write(addr + 1, BRK64_OPCODE_KPROBES_SS);
 
 	flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));

Hmm, so it looks like prior to your change we were doing the cache maintebnance twice: once in aarch64_insn_patch_text() from stop machine context and then again in the flush_icache_range() call above.

I suppose the cleanest thing would be to drop the flush_icache_range() call and then use aarch64_insn_patch_text_nosync() instead of aarch64_insn_write() in your change.

Will

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw

Re: [PATCH V2 3/3] arm64/kprobe: Optimize the performance of patching single-step slot

Quoted 32 lines · Will Deacon
Quoted 16 lines · Liao Chang

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

---
 arch/arm64/kernel/probes/kprobes.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/arm64/kernel/probes/kprobes.c +0 -0
index d1d182320245..5902e33fd3b6 100644
@@ -44,11 +44,10 @@ post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
 {

arch/arm64/kernel/probes/kprobes.c post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *); · line 52

Quoted 10 lines
arch/arm64/kernel/probes/kprobes.c +2 -3
 	kprobe_opcode_t *addr = p->ainsn.api.insn;
-	void *addrs[] = {addr, addr + 1};
-	u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
 
 	/* prepare insn slot */
-	aarch64_insn_patch_text(addrs, insns, 2);
+	aarch64_insn_write(addr, p->opcode);
+	aarch64_insn_write(addr + 1, BRK64_OPCODE_KPROBES_SS);
 
 	flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));

Hmm, so it looks like prior to your change we were doing the cache maintebnance twice: once in aarch64_insn_patch_text() from stop machine context and then again in the flush_icache_range() call above.

Quoted 3 lines

I suppose the cleanest thing would be to drop the flush_icache_range() call and then use aarch64_insn_patch_text_nosync() instead of aarch64_insn_write() in your change.

OK,I will improve it in next version, thanks.

Quoted 3 lines

Will
.

Signature · 2 lines
BR,
Liao, Chang
Acked-by on the v2 series
#6 lore raw
In reply to Liao Chang
Quoted 3 lines · On Tue, 13 Sep 2022 11:34:51 +0800 Liao Chang <[email protected]> wrote:

Single-step slot would not be used until kprobe is enabled, that means no race condition occurs on it under SMP, hence it is safe to pacth ss slot without stopping machine.

All these patches look good to me, since single-step slot must not be executed until it is prepared.

for this series.

But I need Ack by each architecture maintainer.

Thank you,

Quoted 17 lines

v2:
Backport riscv patch to cksy and arm64.

Liao Chang (3):
riscv/kprobe: Optimize the performance of patching single-step slot csky/kprobe: Optimize the performance of patching single-step slot arm64/kprobe: Optimize the performance of patching single-step slot

 arch/arm64/kernel/probes/kprobes.c | 5 ++---
 arch/csky/kernel/probes/kprobes.c  | 6 +++++-
 arch/riscv/kernel/probes/kprobes.c | 8 +++++---
 3 files changed, 12 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>