kprobe: Optimize the performance of patching ss
Subsystems (3)
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
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
@@ -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
PATCH v2 2/3 csky/kprobe: Optimize the performance of patching single-step slot
In reply to Liao ChangSingle-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
@@ -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
PATCH v2 3/3 arm64/kprobe: Optimize the performance of patching single-step slot
In reply to Liao ChangSingle-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
@@ -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
Will Deacon
maintainerRe: [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.
Signed-off-by Liao Chang <[email protected]>--- arch/arm64/kernel/probes/kprobes.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)arch/arm64/kernel/probes/kprobes.c +0
-0@@ -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
-3kprobe_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
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.
Signed-off-by Liao Chang <[email protected]>--- arch/arm64/kernel/probes/kprobes.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)arch/arm64/kernel/probes/kprobes.c +0
-0@@ -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
-3kprobe_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 theflush_icache_range()call above.
Quoted 3 lines
I suppose the cleanest thing would be to drop the
flush_icache_range()call and then useaarch64_insn_patch_text_nosync()instead ofaarch64_insn_write()in your change.
OK,I will improve it in next version, thanks.
Quoted 3 lines
Will
.
Signature · 2 lines
BR, 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 slotarch/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]>