fix & prevent the missing preemption disabling
v2 cover fix & prevent the missing preemption disabling
The testing show that perf_ftrace_function_call() are using smp_processor_id() with preemption enabled, all the checking on CPU could be wrong after preemption.
As Peter point out, the section between ftrace_test_recursion_trylock/unlock() pair require the preemption to be disabled as 'Documentation/trace/ftrace-uses.rst' explained, but currently the work is done outside of the helpers.
Patch 1/2 will make sure preemption disabled after trylock() succeed, patch 2/2 will do smp_processor_id() checking after trylock to address the issue.
v1: https://lore.kernel.org/all/[email protected]/
Michael Wang (2):
ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
ftrace: do CPU checking after preemption disabled
arch/csky/kernel/probes/ftrace.c | 2 -- arch/parisc/kernel/ftrace.c | 2 -- arch/powerpc/kernel/kprobes-ftrace.c | 2 -- arch/riscv/kernel/probes/ftrace.c | 2 -- arch/x86/kernel/kprobes/ftrace.c | 2 -- include/linux/trace_recursion.h | 22 +++++++++++++++++++++- kernel/livepatch/patch.c | 6 ------ kernel/trace/trace_event_perf.c | 6 +++--- kernel/trace/trace_functions.c | 5 ----- 9 files changed, 24 insertions(+), 25 deletions(-)
Signature · 1 line
1.8.3.1
PATCH RESEND v2 1/2 ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
As the documentation explained, ftrace_test_recursion_trylock() and ftrace_test_recursion_unlock() were supposed to disable and enable preemption properly, however currently this work is done outside of the function, which could be missing by mistake.
This path will make sure the preemption was disabled when trylock() succeed, and the unlock() will enable the preemption if previously enabled.
--- arch/csky/kernel/probes/ftrace.c | 2 -- arch/parisc/kernel/ftrace.c | 2 -- arch/powerpc/kernel/kprobes-ftrace.c | 2 -- arch/riscv/kernel/probes/ftrace.c | 2 -- arch/x86/kernel/kprobes/ftrace.c | 2 -- include/linux/trace_recursion.h | 22 +++++++++++++++++++++- kernel/livepatch/patch.c | 6 ------ kernel/trace/trace_functions.c | 5 ----- 8 files changed, 21 insertions(+), 22 deletions(-)
arch/csky/kernel/probes/ftrace.c +0 -2
@@ -17,7 +17,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,return ; regs =ftrace_get_regs (fregs); -preempt_disable_notrace (); p =get_kprobe ((kprobe_opcode_t *)ip);if (!p) { p =get_kprobe ((kprobe_opcode_t *)(ip -MCOUNT_INSN_SIZE ));
@@ -57,7 +56,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,__this_cpu_write (current_kprobe,NULL ); } out: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }NOKPROBE_SYMBOL (kprobe_ftrace_handler);
arch/parisc/kernel/ftrace.c +0 -2
@@ -216,7 +216,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,return ; regs =ftrace_get_regs (fregs); -preempt_disable_notrace (); p =get_kprobe ((kprobe_opcode_t *)ip);if (unlikely (!p) ||kprobe_disabled (p)) goto out;
@@ -245,7 +244,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, }__this_cpu_write (current_kprobe,NULL ); out: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }NOKPROBE_SYMBOL (kprobe_ftrace_handler);
arch/powerpc/kernel/kprobes-ftrace.c +0 -2
@@ -26,7 +26,6 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,return ; regs =ftrace_get_regs (fregs); -preempt_disable_notrace (); p =get_kprobe ((kprobe_opcode_t *)nip);if (unlikely (!p) ||kprobe_disabled (p)) goto out;
@@ -61,7 +60,6 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,__this_cpu_write (current_kprobe,NULL ); } out: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }NOKPROBE_SYMBOL (kprobe_ftrace_handler);
arch/riscv/kernel/probes/ftrace.c +0 -2
@@ -15,7 +15,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,if (bit <0 )return ; -preempt_disable_notrace (); p =get_kprobe ((kprobe_opcode_t *)ip);if (unlikely (!p) ||kprobe_disabled (p)) goto out;
@@ -52,7 +51,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,__this_cpu_write (current_kprobe,NULL ); } out: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }NOKPROBE_SYMBOL (kprobe_ftrace_handler);
arch/x86/kernel/kprobes/ftrace.c +0 -2
@@ -25,7 +25,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,if (bit <0 )return ; -preempt_disable_notrace (); p =get_kprobe ((kprobe_opcode_t *)ip);if (unlikely (!p) ||kprobe_disabled (p)) goto out;
@@ -59,7 +58,6 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,__this_cpu_write (current_kprobe,NULL ); } out: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }NOKPROBE_SYMBOL (kprobe_ftrace_handler);
include/linux/trace_recursion.h +21 -1
@@ -208,13 +208,29 @@ static __always_inline void trace_clear_recursion(int bit)* Use this for ftrace callbacks. This will detect if the function * tracing recursed in the same context (normal vs interrupt), * +* The ftrace_test_recursion_trylock() will disable preemption, +* which is required for the variant of synchronize_rcu() that is +* used to allow patching functions where RCU is not watching. +* See klp_synchronize_transition() for more details. +* * Returns: -1 if a recursion happened. * >= 0 if no recursion */ static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,unsigned long parent_ip) { -return trace_test_and_set_recursion (ip, parent_ip,TRACE_FTRACE_START ,TRACE_FTRACE_MAX ); +int bit; + + bit =trace_test_and_set_recursion (ip, parent_ip,TRACE_FTRACE_START ,TRACE_FTRACE_MAX ); +/* +* The zero bit indicate we are nested +* in another trylock(), which means the +* preemption already disabled. +*/ +if (bit >0 ) +preempt_disable_notrace (); + +return bit; }/**
@@ -222,9 +238,13 @@ static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,* @bit: The return of a successful ftrace_test_recursion_trylock() * * This is used at the end of a ftrace callback. +* +* Preemption will be enabled (if it was previously enabled). */ static __always_inline void ftrace_test_recursion_unlock(int bit) { +if (bit) +preempt_enable_notrace ();trace_clear_recursion (bit); }
kernel/livepatch/patch.c +0 -6
@@ -52,11 +52,6 @@ static void notrace klp_ftrace_handler(unsigned long ip, bit =ftrace_test_recursion_trylock (ip, parent_ip);if (WARN_ON_ONCE (bit <0 ))return ; -/* -* A variant of synchronize_rcu() is used to allow patching functions -* where RCU is not watching, see klp_synchronize_transition(). -*/ -preempt_disable_notrace (); func = list_first_or_null_rcu(&ops->func_stack, struct klp_func, stack_node);
@@ -120,7 +115,6 @@ static void notrace klp_ftrace_handler(unsigned long ip,klp_arch_set_pc (fregs, (unsigned long )func->new_func); unlock: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }
kernel/trace/trace_functions.c +0 -5
@@ -186,7 +186,6 @@ static void function_trace_start(struct trace_array *tr)return ; trace_ctx =tracing_gen_ctx (); -preempt_disable_notrace (); cpu =smp_processor_id (); data =per_cpu_ptr (tr->array_buffer.data, cpu);
@@ -194,7 +193,6 @@ static void function_trace_start(struct trace_array *tr)trace_function (tr, ip, parent_ip, trace_ctx);ftrace_test_recursion_unlock (bit); -preempt_enable_notrace (); }#ifdef CONFIG_UNWINDER_ORC
@@ -298,8 +296,6 @@ static inline void process_repeats(struct trace_array *tr,if (bit <0 )return ; -preempt_disable_notrace (); - cpu =smp_processor_id (); data =per_cpu_ptr (tr->array_buffer.data, cpu);if (atomic_read (&data->disabled))
@@ -324,7 +320,6 @@ static inline void process_repeats(struct trace_array *tr, out:ftrace_test_recursion_unlock (bit); -preempt_enable_notrace (); } static void
Signature · 1 line
1.8.3.1
Miroslav Benes
maintainerRe: [RESEND PATCH v2 1/2] ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
include/linux/trace_recursion.h static __always_inline void trace_clear_recursion(int bit) · line 215
Quoted 13 lines
include/linux/trace_recursion.h +5
-0@@ -208,13 +208,29 @@ static __always_inline void trace_clear_recursion(int bit)* Use this for ftrace callbacks. This will detect if the function * tracing recursed in the same context (normal vs interrupt), * +* The ftrace_test_recursion_trylock() will disable preemption, +* which is required for the variant of synchronize_rcu() that is +* used to allow patching functions where RCU is not watching. +* See klp_synchronize_transition() for more details. +*
I think that you misunderstood. Steven proposed to put the comment before ftrace_test_recursion_trylock() call site in klp_ftrace_handler().
Quoted 20 lines
include/linux/trace_recursion.h +12
-1* Returns: -1 if a recursion happened. * >= 0 if no recursion */ static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,unsigned long parent_ip) { -return trace_test_and_set_recursion (ip, parent_ip,TRACE_FTRACE_START ,TRACE_FTRACE_MAX ); +int bit; + + bit =trace_test_and_set_recursion (ip, parent_ip,TRACE_FTRACE_START ,TRACE_FTRACE_MAX ); +/* +* The zero bit indicate we are nested +* in another trylock(), which means the +* preemption already disabled. +*/ +if (bit >0 ) +preempt_disable_notrace (); + +return bit; }
[...]
kernel/livepatch/patch.c static void notrace klp_ftrace_handler(unsigned long ip,
Quoted 5 lines
kernel/livepatch/patch.c +0
-0@@ -52,11 +52,6 @@ static void notrace klp_ftrace_handler(unsigned long ip,
Here
Quoted 12 lines
kernel/livepatch/patch.c +0
-5bit =ftrace_test_recursion_trylock (ip, parent_ip);if (WARN_ON_ONCE (bit <0 ))return ; -/* -* A variant of synchronize_rcu() is used to allow patching functions -* where RCU is not watching, see klp_synchronize_transition(). -*/ -preempt_disable_notrace (); func = list_first_or_null_rcu(&ops->func_stack, struct klp_func, stack_node);
kernel/livepatch/patch.c static void notrace klp_ftrace_handler(unsigned long ip, · line 119
Quoted 6 lines
kernel/livepatch/patch.c +0
-1@@ -120,7 +115,6 @@ static void notrace klp_ftrace_handler(unsigned long ip,klp_arch_set_pc (fregs, (unsigned long )func->new_func); unlock: -preempt_enable_notrace ();ftrace_test_recursion_unlock (bit); }
Side note... the comment will eventually conflict with peterz's
https://lore.kernel.org/all/[email protected]/.
Miroslav
Re: [RESEND PATCH v2 1/2] ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
Quoted 8 lines · Miroslav Benes
Quoted 8 lines
include/linux/trace_recursion.h +0
-0@@ -208,13 +208,29 @@ static __always_inline void trace_clear_recursion(int bit)* Use this for ftrace callbacks. This will detect if the function * tracing recursed in the same context (normal vs interrupt), *
Quoted 8 lines
include/linux/trace_recursion.h static __always_inline void trace_clear_recursion(int bit) · line 215
Quoted 5 lines
include/linux/trace_recursion.h +5
-0+* The ftrace_test_recursion_trylock() will disable preemption, +* which is required for the variant of synchronize_rcu() that is +* used to allow patching functions where RCU is not watching. +* See klp_synchronize_transition() for more details. +* I think that you misunderstood. Steven proposed to put the comment before
ftrace_test_recursion_trylock()call site inklp_ftrace_handler().
Oh, I see... thanks for pointing out :-)
Quoted 2 lines
Quoted 2 lines
include/linux/trace_recursion.h +0
-0* Returns: -1 if a recursion happened.
[snip]
Quoted 4 lines
Quoted 1 line
}
Side note... the comment will eventually conflict with peterz's
https://lore.kernel.org/all/[email protected]/.
Steven, would you like to share your opinion on this patch?
If klp_synchronize_transition() will be removed anyway, the comments will be meaningless and we can just drop it :-P
Regards,
Michael Wang
Quoted 3 lines
Miroslav
Miroslav Benes
maintainerRe: [RESEND PATCH v2 1/2] ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
Quoted 7 lines
Quoted 2 lines
Side note... the comment will eventually conflict with peterz's
https://lore.kernel.org/all/[email protected]/.Steven, would you like to share your opinion on this patch?
If
klp_synchronize_transition()will be removed anyway, the comments will be meaningless and we can just drop it :-P
The comment will still be needed in some form. We will handle it depending on what gets merged first. peterz's patches are not ready yet.
Miroslav
Re: [RESEND PATCH v2 1/2] ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
Quoted 10 lines · Miroslav Benes
Quoted 7 lines
Quoted 2 lines
Side note... the comment will eventually conflict with peterz's
https://lore.kernel.org/all/[email protected]/.Steven, would you like to share your opinion on this patch?
If
klp_synchronize_transition()will be removed anyway, the comments will be meaningless and we can just drop it :-PThe comment will still be needed in some form. We will handle it depending on what gets merged first. peterz's patches are not ready yet.
Ok, then I'll move it before trylock() inside klp_ftrace_handler() anyway.
Regards,
Michael Wang
Quoted 3 lines
Miroslav
Steven Rostedt
maintainerRe: [RESEND PATCH v2 1/2] ftrace: disable preemption between ftrace_test_recursion_trylock/unlock()
Quoted 6 lines · On Wed, 13 Oct 2021 16:34:53 +0800 王贇 <[email protected]> wrote:
Quoted 5 lines · Miroslav Benes
Quoted 5 lines
Quoted 2 lines
Side note... the comment will eventually conflict with peterz's
https://lore.kernel.org/all/[email protected]/.Steven, would you like to share your opinion on this patch?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
If
klp_synchronize_transition()will be removed anyway, the comments will be meaningless and we can just drop it :-PThe comment will still be needed in some form. We will handle it depending on what gets merged first. peterz's patches are not ready yet.
Ok, then I'll move it before
trylock()insideklp_ftrace_handler()anyway.
+1
-- Steve
PATCH RESEND v2 2/2 ftrace: do CPU checking after preemption disabled
In reply to 王贇With CONFIG_DEBUG_PREEMPT we observed reports like:
BUG: using smp_processor_id() in preemptible caller is perf_ftrace_function_call+0x6f/0x2e0 CPU: 1 PID: 680 Comm: a.out Not tainted Call Trace: <TASK> dump_stack_lvl+0x8d/0xcf check_preemption_disabled+0x104/0x110 ? optimize_nops.isra.7+0x230/0x230 ? text_poke_bp_batch+0x9f/0x310 perf_ftrace_function_call+0x6f/0x2e0 ... __text_poke+0x5/0x620 text_poke_bp_batch+0x9f/0x310
This telling us the CPU could be changed after task is preempted, and the checking on CPU before preemption will be invalid.
Since now ftrace_test_recursion_trylock() will help to disable the preemption, this patch just do the checking after trylock() to address the issue.
--- kernel/trace/trace_event_perf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace_event_perf.c +3 -3
@@ -441,13 +441,13 @@ void perf_trace_buf_update(void *record, u16 type)if (!rcu_is_watching ())return ; -if ((unsigned long )ops->private !=smp_processor_id ()) -return ; - bit =ftrace_test_recursion_trylock (ip, parent_ip);if (bit <0 )return ; +if ((unsigned long )ops->private !=smp_processor_id ()) + goto out; + event = container_of(ops, struct perf_event, ftrace_ops);/*
Signature · 1 line
1.8.3.1