Skip to content

Commit 726b3d3

Browse files
committed
ftrace: Handle tracing when switching between context
When an interrupt or NMI comes in and switches the context, there's a delay from when the preempt_count() shows the update. As the preempt_count() is used to detect recursion having each context have its own bit get set when tracing starts, and if that bit is already set, it is considered a recursion and the function exits. But if this happens in that section where context has changed but preempt_count() has not been updated, this will be incorrectly flagged as a recursion. To handle this case, create another bit call TRANSITION and test it if the current context bit is already set. Flag the call as a recursion if the TRANSITION bit is already set, and if not, set it and continue. The TRANSITION bit will be cleared normally on the return of the function that set it, or if the current context bit is clear, set it and clear the TRANSITION bit to allow for another transition between the current context and an even higher one. Cc: [email protected] Fixes: edc15ca ("tracing: Avoid unnecessary multiple recursion checks") Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent ee11b93 commit 726b3d3

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

kernel/trace/trace.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ enum {
637637
* function is called to clear it.
638638
*/
639639
TRACE_GRAPH_NOTRACE_BIT,
640+
641+
/*
642+
* When transitioning between context, the preempt_count() may
643+
* not be correct. Allow for a single recursion to cover this case.
644+
*/
645+
TRACE_TRANSITION_BIT,
640646
};
641647

642648
#define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
@@ -691,8 +697,21 @@ static __always_inline int trace_test_and_set_recursion(int start, int max)
691697
return 0;
692698

693699
bit = trace_get_context_bit() + start;
694-
if (unlikely(val & (1 << bit)))
695-
return -1;
700+
if (unlikely(val & (1 << bit))) {
701+
/*
702+
* It could be that preempt_count has not been updated during
703+
* a switch between contexts. Allow for a single recursion.
704+
*/
705+
bit = TRACE_TRANSITION_BIT;
706+
if (trace_recursion_test(bit))
707+
return -1;
708+
trace_recursion_set(bit);
709+
barrier();
710+
return bit + 1;
711+
}
712+
713+
/* Normal check passed, clear the transition to allow it again */
714+
trace_recursion_clear(TRACE_TRANSITION_BIT);
696715

697716
val |= 1 << bit;
698717
current->trace_recursion = val;

kernel/trace/trace_selftest.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,13 @@ trace_selftest_function_recursion(void)
492492
unregister_ftrace_function(&test_rec_probe);
493493

494494
ret = -1;
495-
if (trace_selftest_recursion_cnt != 1) {
496-
pr_cont("*callback not called once (%d)* ",
495+
/*
496+
* Recursion allows for transitions between context,
497+
* and may call the callback twice.
498+
*/
499+
if (trace_selftest_recursion_cnt != 1 &&
500+
trace_selftest_recursion_cnt != 2) {
501+
pr_cont("*callback not called once (or twice) (%d)* ",
497502
trace_selftest_recursion_cnt);
498503
goto out;
499504
}

0 commit comments

Comments
 (0)