powerpc/kprobes: preempt related changes and cleanups
Subsystems (3)
cover powerpc/kprobes: preempt related changes and cleanups
This series attempts to address some of the concerns raised in
https://github.com/linuxppc/issues/issues/440
The last two patches are minor cleanups in related kprobes code.
- Naveen
Naveen N. Rao (5):
powerpc/kprobes: Remove preempt disable around call to get_kprobe() in
arch_prepare_kprobe()
powerpc/kprobes: Have optimized_callback() use preempt_enable() powerpc/kprobes: Use preempt_enable() rather than the no_resched
variant
powerpc/kprobes: Setup consistent pt_regs across kprobes, optprobes
and KPROBES_ON_FTRACE
powerpc/kprobes: Remove unnecessary headers from kprobes
arch/powerpc/kernel/kprobes-ftrace.c | 4 ---- arch/powerpc/kernel/kprobes.c | 16 ++++++---------- arch/powerpc/kernel/optprobes.c | 2 +- arch/powerpc/kernel/optprobes_head.S | 5 +---- arch/powerpc/kernel/trace/ftrace_mprofile.S | 6 ++++++ 5 files changed, 14 insertions(+), 19 deletions(-)
base-commit: 7dc2a00fdd44a4d0c3bac9fd10558b3933586a0c
Signature · 1 line
2.38.0
PATCH 1/5 powerpc/kprobes: Remove preempt disable around call to get_kprobe() in arch_prepare_kprobe()
arch_prepare_kprobe() is called from register_kprobe() via prepare_kprobe(), or through register_aggr_kprobe(), both with the kprobe_mutex held. Per the comment for get_kprobe():
/*
* This routine is called either:
* - under the 'kprobe_mutex' - during kprobe_[un]register().
* OR
* - with preemption disabled - from architecture specific code.
*/
As such, there is no need to disable preemption around the call to get_kprobe(). Drop the same.
--- arch/powerpc/kernel/kprobes.c | 2 -- 1 file changed, 2 deletions(-)
arch/powerpc/kernel/kprobes.c +0 -2
@@ -158,9 +158,7 @@ int arch_prepare_kprobe(struct kprobe *p)printk ("Cannot register a kprobe on the second word of prefixed instruction\n" ); ret = -EINVAL ; } -preempt_disable (); prev =get_kprobe (p->addr -1 ); -preempt_enable_no_resched ();/* * When prev is a ftrace-based kprobe, we don't have an insn, and it
Signature · 1 line
2.38.0
Re: [PATCH 1/5] powerpc/kprobes: Remove preempt disable around call to get_kprobe() in arch_prepare_kprobe()
Quoted 9 lines · Naveen N. Rao
arch_prepare_kprobe()is called fromregister_kprobe()viaprepare_kprobe(), or throughregister_aggr_kprobe(), both with thekprobe_mutexheld. Per the comment forget_kprobe():
/*
* This routine is called either:
* - under the 'kprobe_mutex' - during kprobe_[un]register().
* OR
* - with preemption disabled - from architecture specific code.
*/
That comment should read [un]register_kprobe(), right?
Quoted 3 lines
As such, there is no need to disable preemption around the call to
get_kprobe(). Drop the same.
And prepare_kprobe() and register_aggr_kprobe() are both called with kprobe_mutex held so you rely on the same protection. This seems to fix a lost-resched bug with preempt kernels too.
Quoted 23 lines
Reported-by Nicholas Piggin <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/kernel/kprobes.c | 2 -- 1 file changed, 2 deletions(-)arch/powerpc/kernel/kprobes.c +0
-2@@ -158,9 +158,7 @@ int arch_prepare_kprobe(struct kprobe *p)printk ("Cannot register a kprobe on the second word of prefixed instruction\n" ); ret = -EINVAL ; } -preempt_disable (); prev =get_kprobe (p->addr -1 ); -preempt_enable_no_resched ();/* * When prev is a ftrace-based kprobe, we don't have an insn, and it Signature · 1 line
2.38.0
Re: [PATCH 1/5] powerpc/kprobes: Remove preempt disable around call to get_kprobe() in arch_prepare_kprobe()
Quoted 11 lines · Nicholas Piggin
Quoted 9 lines · Naveen N. Rao
arch_prepare_kprobe()is called fromregister_kprobe()viaprepare_kprobe(), or throughregister_aggr_kprobe(), both with thekprobe_mutexheld. Per the comment forget_kprobe():
/*
* This routine is called either:
* - under the 'kprobe_mutex' - during kprobe_[un]register().
* OR
* - with preemption disabled - from architecture specific code.
*/
Quoted 1 line
That comment should read [un]register_kprobe(), right?
Ugh, yes!
Quoted 10 lines
Quoted 4 lines
As such, there is no need to disable preemption around the call to
get_kprobe(). Drop the same.And
prepare_kprobe()andregister_aggr_kprobe()are both called withkprobe_mutexheld so you rely on the same protection. This seems to fix a lost-resched bug with preempt kernels too.Reviewed-by Nicholas Piggin <[email protected]>
Thanks,
Naveen
PATCH 2/5 powerpc/kprobes: Have optimized_callback() use preempt_enable()
In reply to Naveen N. RaoSimilar to x86 commit 2e62024c265aa6 ("kprobes/x86: Use preempt_enable() in optimized_callback()"), change powerpc optprobes to use preempt_enable() rather than preempt_enable_no_resched() since powerpc also removed irq disabling for optprobes in commit f72180cc93a2c6 ("powerpc/kprobes: Do not disable interrupts for optprobes and kprobes_on_ftrace").
--- arch/powerpc/kernel/optprobes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kernel/optprobes.c +1 -1
@@ -112,7 +112,7 @@ static void optimized_callback(struct optimized_kprobe *op,__this_cpu_write (current_kprobe,NULL ); } -preempt_enable_no_resched (); +preempt_enable (); }NOKPROBE_SYMBOL (optimized_callback);
Signature · 1 line
2.38.0
Re: [PATCH 2/5] powerpc/kprobes: Have optimized_callback() use preempt_enable()
Quoted 6 lines · Naveen N. Rao
Similar to x86 commit
2e62024c265aa6("kprobes/x86: Usepreempt_enable()inoptimized_callback()"), change powerpc optprobes to usepreempt_enable()rather thanpreempt_enable_no_resched()since powerpc also removed irq disabling for optprobes in commitf72180cc93a2c6("powerpc/kprobes: Do not disable interrupts for optprobes andkprobes_on_ftrace").
Looks okay. Even if we did have irqs disabled here, we should just use preempt_enable(), which nests properly inside or outside local irqs.
Quoted 22 lines
Reported-by Nicholas Piggin <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/kernel/optprobes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/powerpc/kernel/optprobes.c +1
-1@@ -112,7 +112,7 @@ static void optimized_callback(struct optimized_kprobe *op,__this_cpu_write (current_kprobe,NULL ); } -preempt_enable_no_resched (); +preempt_enable (); }NOKPROBE_SYMBOL (optimized_callback);Signature · 1 line
2.38.0
PATCH 3/5 powerpc/kprobes: Use preempt_enable() rather than the no_resched variant
In reply to Naveen N. Raopreempt_enable_no_resched() is just the same as preempt_enable() when we are in a irqs disabled context. kprobe_handler() and the post/fault handlers are all called with irqs disabled. As such, convert those to just use preempt_enable().
--- arch/powerpc/kernel/kprobes.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
arch/powerpc/kernel/kprobes.c +6 -6
@@ -369,7 +369,7 @@ int kprobe_handler(struct pt_regs *regs)if (ret >0 ) {restore_previous_kprobe (kcb); -preempt_enable_no_resched (); +preempt_enable ();return 1 ; } }
@@ -382,7 +382,7 @@ int kprobe_handler(struct pt_regs *regs)if (p->pre_handler && p->pre_handler (p, regs)) {/* handler changed execution path, so skip ss setup */ reset_current_kprobe (); -preempt_enable_no_resched (); +preempt_enable ();return 1 ; }
@@ -395,7 +395,7 @@ int kprobe_handler(struct pt_regs *regs) kcb->kprobe_status =KPROBE_HIT_SSDONE ;reset_current_kprobe (); -preempt_enable_no_resched (); +preempt_enable ();return 1 ; } }
@@ -404,7 +404,7 @@ int kprobe_handler(struct pt_regs *regs)return 1 ; no_kprobe: -preempt_enable_no_resched (); +preempt_enable ();return ret; }NOKPROBE_SYMBOL (kprobe_handler);
@@ -490,7 +490,7 @@ int kprobe_post_handler(struct pt_regs *regs) }reset_current_kprobe (); out: -preempt_enable_no_resched (); +preempt_enable ();/* * if somebody else is singlestepping across a probe point, msr
@@ -529,7 +529,7 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)restore_previous_kprobe (kcb); elsereset_current_kprobe (); -preempt_enable_no_resched (); +preempt_enable ();break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :
Signature · 1 line
2.38.0
Re: [PATCH 3/5] powerpc/kprobes: Use preempt_enable() rather than the no_resched variant
Quoted 4 lines · Naveen N. Rao
preempt_enable_no_resched()is just the same aspreempt_enable()when we are in a irqs disabled context.kprobe_handler()and the post/fault handlers are all called with irqs disabled. As such, convert those to just usepreempt_enable().
Quoted 67 lines
Reported-by Nicholas Piggin <[email protected]>Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/kernel/kprobes.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)arch/powerpc/kernel/kprobes.c +6
-6@@ -369,7 +369,7 @@ int kprobe_handler(struct pt_regs *regs)if (ret >0 ) {restore_previous_kprobe (kcb); -preempt_enable_no_resched (); +preempt_enable ();return 1 ; } }@@ -382,7 +382,7 @@ int kprobe_handler(struct pt_regs *regs)if (p->pre_handler && p->pre_handler (p, regs)) {/* handler changed execution path, so skip ss setup */ reset_current_kprobe (); -preempt_enable_no_resched (); +preempt_enable ();return 1 ; }@@ -395,7 +395,7 @@ int kprobe_handler(struct pt_regs *regs) kcb->kprobe_status =KPROBE_HIT_SSDONE ;reset_current_kprobe (); -preempt_enable_no_resched (); +preempt_enable ();return 1 ; } }@@ -404,7 +404,7 @@ int kprobe_handler(struct pt_regs *regs)return 1 ; no_kprobe: -preempt_enable_no_resched (); +preempt_enable ();return ret; }NOKPROBE_SYMBOL (kprobe_handler);@@ -490,7 +490,7 @@ int kprobe_post_handler(struct pt_regs *regs) }reset_current_kprobe (); out: -preempt_enable_no_resched (); +preempt_enable ();/* * if somebody else is singlestepping across a probe point, msr @@ -529,7 +529,7 @@ int kprobe_fault_handler(struct pt_regs *regs, int trapnr)restore_previous_kprobe (kcb); elsereset_current_kprobe (); -preempt_enable_no_resched (); +preempt_enable ();break ;case KPROBE_HIT_ACTIVE :case KPROBE_HIT_SSDONE :Signature · 1 line
2.38.0
PATCH 4/5 powerpc/kprobes: Setup consistent pt_regs across kprobes, optprobes and KPROBES_ON_FTRACE
In reply to Naveen N. RaoEnsure a more consistent pt_regs across kprobes, optprobes and KPROBES_ON_FTRACE:
- Drop setting trap to 0x700 under optprobes. This is not accurate and is unnecessary. Instead, zero it out for both optprobes and KPROBES_ON_FTRACE.
- Save irq soft mask in the ftrace handler, similar to what we do in optprobes and trap-based kprobes.
- Drop setting orig_gpr3 and result to zero in optprobes. These are not relevant under kprobes and should not be used by the handlers.
--- arch/powerpc/kernel/optprobes_head.S | 5 +---- arch/powerpc/kernel/trace/ftrace_mprofile.S | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-)
arch/powerpc/kernel/optprobes_head.S +1 -4
@@ -49,11 +49,8 @@ optprobe_template_entry: /* Save SPRS */ mfmsr r5 PPC_STL r5,_MSR(r1) - li r5,0x700 - PPC_STL r5,_TRAP(r1) li r5,0 - PPC_STL r5,ORIG_GPR3(r1) - PPC_STL r5,RESULT(r1) + PPC_STL r5,_TRAP(r1) mfctr r5 PPC_STL r5,_CTR(r1) mflr r5
arch/powerpc/kernel/trace/ftrace_mprofile.S +6 -0
@@ -107,6 +107,12 @@ PPC_STL r9, _CTR(r1) PPC_STL r10, _XER(r1) PPC_STL r11, _CCR(r1) +#ifdef CONFIG_PPC64 + lbz r7, PACAIRQSOFTMASK(r13) + std r7, SOFTE(r1) +#endif + li r8, 0 + PPC_STL r8, _TRAP(r1) .endif /* Load &pt_regs in r6 for call below */
Signature · 1 line
2.38.0
Re: [PATCH 4/5] powerpc/kprobes: Setup consistent pt_regs across kprobes, optprobes and KPROBES_ON_FTRACE
Quoted 5 lines · Naveen N. Rao
Ensure a more consistent
pt_regsacross kprobes, optprobes andKPROBES_ON_FTRACE:
- Drop setting trap to0x700under optprobes. This is not accurate and is unnecessary. Instead, zero it out for both optprobes andKPROBES_ON_FTRACE.
Okay I think.
Quoted 2 lines
- Save irq soft mask in the ftrace handler, similar to what we do in optprobes and trap-based kprobes.
This advertises the irqs status of regs correctly, whereas previously it was uninitialised.
Quoted 2 lines
- Drop setting
orig_gpr3and result to zero in optprobes. These are not relevant under kprobes and should not be used by the handlers.
This is for CFAR, which we can't get anyway because we just branched here. I would rather zero it explicitly though.
Otherwise,
Quoted 43 lines
Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/kernel/optprobes_head.S | 5 +---- arch/powerpc/kernel/trace/ftrace_mprofile.S | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-)arch/powerpc/kernel/optprobes_head.S +1
-4@@ -49,11 +49,8 @@ optprobe_template_entry: /* Save SPRS */ mfmsr r5 PPC_STL r5,_MSR(r1) - li r5,0x700 - PPC_STL r5,_TRAP(r1) li r5,0 - PPC_STL r5,ORIG_GPR3(r1) - PPC_STL r5,RESULT(r1) + PPC_STL r5,_TRAP(r1) mfctr r5 PPC_STL r5,_CTR(r1) mflr r5arch/powerpc/kernel/trace/ftrace_mprofile.S +6
-0@@ -107,6 +107,12 @@ PPC_STL r9, _CTR(r1) PPC_STL r10, _XER(r1) PPC_STL r11, _CCR(r1) +#ifdef CONFIG_PPC64 + lbz r7, PACAIRQSOFTMASK(r13) + std r7, SOFTE(r1) +#endif + li r8, 0 + PPC_STL r8, _TRAP(r1) .endif /* Load &pt_regs in r6 for call below */Signature · 1 line
2.38.0
Re: [PATCH 4/5] powerpc/kprobes: Setup consistent pt_regs across kprobes, optprobes and KPROBES_ON_FTRACE
Quoted 12 lines · Nicholas Piggin
Quoted 5 lines · Naveen N. Rao
Ensure a more consistent
pt_regsacross kprobes, optprobes andKPROBES_ON_FTRACE:
- Drop setting trap to0x700under optprobes. This is not accurate and is unnecessary. Instead, zero it out for both optprobes andKPROBES_ON_FTRACE.Okay I think.
Quoted 2 lines
- Save irq soft mask in the ftrace handler, similar to what we do in optprobes and trap-based kprobes.
Quoted 8 lines
This advertises the irqs status of regs correctly, whereas previously it was uninitialised.
Quoted 2 lines
- Drop setting
orig_gpr3and result to zero in optprobes. These are not relevant under kprobes and should not be used by the handlers.This is for CFAR, which we can't get anyway because we just branched here. I would rather zero it explicitly though.
Is there a strong reason to zero those out?
The reason I dropped zero'ing of orig_gpr3 and result is to make optprobes consistent with KPROBES_ON_FTRACE. If we want to retain zero'ing orig_gpr3/result for optprobes, I think we should then go ahead and zero those out in ftrace_regs_caller too.
Thanks,
Naveen
PATCH 5/5 powerpc/kprobes: Remove unnecessary headers from kprobes
In reply to Naveen N. RaoMany of these headers are not necessary since those are included indirectly, or the code using those headers has been removed.
--- arch/powerpc/kernel/kprobes-ftrace.c | 4 ---- arch/powerpc/kernel/kprobes.c | 2 -- 2 files changed, 6 deletions(-)
arch/powerpc/kernel/kprobes-ftrace.c +0 -4
@@ -7,10 +7,6 @@* IBM Corporation */ #include <linux/kprobes.h> -#include <linux/ptrace.h> -#include <linux/hardirq.h> -#include <linux/preempt.h> -#include <linux/ftrace.h> /* Ftrace callback handler for kprobes */ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,
arch/powerpc/kernel/kprobes.c +0 -2
@@ -14,8 +14,6 @@*/ #include <linux/kprobes.h> -#include <linux/ptrace.h> -#include <linux/preempt.h> #include <linux/extable.h> #include <linux/kdebug.h> #include <linux/slab.h>
Signature · 1 line
2.38.0
Re: [PATCH 5/5] powerpc/kprobes: Remove unnecessary headers from kprobes
Le 20/10/2022 à 19:29, Naveen N. Rao a écrit :
Quoted 2 lines
Many of these headers are not necessary since those are included indirectly, or the code using those headers has been removed.
It is usually not a good idea to not include headers because they are already included indirectly. If one day for some reason de indirect link gets removed, then your file doesn't build anymore.
Quoted 35 lines
Signed-off-by Naveen N. Rao <[email protected]>--- arch/powerpc/kernel/kprobes-ftrace.c | 4 ---- arch/powerpc/kernel/kprobes.c | 2 -- 2 files changed, 6 deletions(-)arch/powerpc/kernel/kprobes-ftrace.c +0
-4@@ -7,10 +7,6 @@* IBM Corporation */ #include <linux/kprobes.h> -#include <linux/ptrace.h> -#include <linux/hardirq.h> -#include <linux/preempt.h> -#include <linux/ftrace.h> /* Ftrace callback handler for kprobes */ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,arch/powerpc/kernel/kprobes.c +0
-2@@ -14,8 +14,6 @@*/ #include <linux/kprobes.h> -#include <linux/ptrace.h> -#include <linux/preempt.h> #include <linux/extable.h> #include <linux/kdebug.h> #include <linux/slab.h>
Re: [PATCH 5/5] powerpc/kprobes: Remove unnecessary headers from kprobes
Quoted 7 lines · Christophe Leroy
Le 20/10/2022 à 19:29, Naveen N. Rao a écrit :
Quoted 2 lines
Many of these headers are not necessary since those are included indirectly, or the code using those headers has been removed.
It is usually not a good idea to not include headers because they are already included indirectly. If one day for some reason de indirect link gets removed, then your file doesn't build anymore.
I agree with that in general.
The motivation for this patch was to remove <linux/preempt.h> from kprobes-ftrace.c since we got rid of explicit preempt disable/enable calls in the past. We have also removed explicit irq disable from this path. <linux/kprobes.h> includes <linux/ftrace.h> and <asm/kprobes.h> includes <linux/ptrace.h> so those could go too.
I don't expect those header dependencies to change, so I think this patch is ok.
- Naveen
Nicholas Piggin
reviewer+linux-arch
FYI most preempt_enable_no_resched() calls outside of core scheduler code come from kprobes copy-and-paste, and can possibly be fixed in similar ways as this series. It's a bit of a footgun API that should be banished from outside kernel/sched/, at least without an explicit comment for each case that explains the need and how the missed resched is resolved or not applicable.
Thanks,
Nick
Quoted 29 lines · Naveen N. Rao
This series attempts to address some of the concerns raised in
https://github.com/linuxppc/issues/issues/440The last two patches are minor cleanups in related kprobes code.
- Naveen
Naveen N. Rao (5):
powerpc/kprobes: Remove preempt disable around call toget_kprobe()in
arch_prepare_kprobe()
powerpc/kprobes: Haveoptimized_callback()usepreempt_enable()powerpc/kprobes: Usepreempt_enable()rather than theno_resched
variant
powerpc/kprobes: Setup consistentpt_regsacross kprobes, optprobes
andKPROBES_ON_FTRACE
powerpc/kprobes: Remove unnecessary headers from kprobesarch/powerpc/kernel/kprobes-ftrace.c | 4 ---- arch/powerpc/kernel/kprobes.c | 16 ++++++---------- arch/powerpc/kernel/optprobes.c | 2 +- arch/powerpc/kernel/optprobes_head.S | 5 +---- arch/powerpc/kernel/trace/ftrace_mprofile.S | 6 ++++++ 5 files changed, 14 insertions(+), 19 deletions(-)base-commit:
7dc2a00fdd44a4d0c3bac9fd10558b3933586a0cSignature · 1 line
2.38.0
Michael Ellerman
AppliedRe: (subset) [PATCH 0/5] powerpc/kprobes: preempt related changes and cleanups
In reply to Naveen N. RaoQuoted 8 lines · Naveen N. Rao
This series attempts to address some of the concerns raised in
https://github.com/linuxppc/issues/issues/440The last two patches are minor cleanups in related kprobes code.
- Naveen
[...]
Applied to powerpc/next.
[1/5] powerpc/kprobes: Remove preempt disable around call to get_kprobe() in arch_prepare_kprobe()
https://git.kernel.org/powerpc/c/2fa9482334b0593b7edc371a13c0cca81daaa89e [2/5] powerpc/kprobes: Have optimized_callback() use preempt_enable() https://git.kernel.org/powerpc/c/04ec5d5782fb346c291a05a2efe59483d8ada4c4 [3/5] powerpc/kprobes: Use preempt_enable() rather than the no_resched variant https://git.kernel.org/powerpc/c/266b1991a433cd55bb86a933216b3f6762737d47
cheers