Skip to content

Commit 81f9662

Browse files
rostedtgregkh
authored andcommitted
tracing/fgraph: Fix set_graph_function from showing interrupts
commit 5cf99a0 upstream. The tracefs file set_graph_function is used to only function graph functions that are listed in that file (or all functions if the file is empty). The way this is implemented is that the function graph tracer looks at every function, and if the current depth is zero and the function matches something in the file then it will trace that function. When other functions are called, the depth will be greater than zero (because the original function will be at depth zero), and all functions will be traced where the depth is greater than zero. The issue is that when a function is first entered, and the handler that checks this logic is called, the depth is set to zero. If an interrupt comes in and a function in the interrupt handler is traced, its depth will be greater than zero and it will automatically be traced, even if the original function was not. But because the logic only looks at depth it may trace interrupts when it should not be. The recent design change of the function graph tracer to fix other bugs caused the depth to be zero while the function graph callback handler is being called for a longer time, widening the race of this happening. This bug was actually there for a longer time, but because the race window was so small it seldom happened. The Fixes tag below is for the commit that widen the race window, because that commit belongs to a series that will also help fix the original bug. Cc: stable@kernel.org Fixes: 39eb456 ("function_graph: Use new curr_ret_depth to manage depth instead of curr_ret_stack") Reported-by: Joe Lawrence <joe.lawrence@redhat.com> Tested-by: Joe Lawrence <joe.lawrence@redhat.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a990756 commit 81f9662

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎kernel/trace/trace.h‎

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,44 @@ enum {
512512
* can only be modified by current, we can reuse trace_recursion.
513513
*/
514514
TRACE_IRQ_BIT,
515+
516+
/* Set if the function is in the set_graph_function file */
517+
TRACE_GRAPH_BIT,
518+
519+
/*
520+
* In the very unlikely case that an interrupt came in
521+
* at a start of graph tracing, and we want to trace
522+
* the function in that interrupt, the depth can be greater
523+
* than zero, because of the preempted start of a previous
524+
* trace. In an even more unlikely case, depth could be 2
525+
* if a softirq interrupted the start of graph tracing,
526+
* followed by an interrupt preempting a start of graph
527+
* tracing in the softirq, and depth can even be 3
528+
* if an NMI came in at the start of an interrupt function
529+
* that preempted a softirq start of a function that
530+
* preempted normal context!!!! Luckily, it can't be
531+
* greater than 3, so the next two bits are a mask
532+
* of what the depth is when we set TRACE_GRAPH_BIT
533+
*/
534+
535+
TRACE_GRAPH_DEPTH_START_BIT,
536+
TRACE_GRAPH_DEPTH_END_BIT,
515537
};
516538

517539
#define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
518540
#define trace_recursion_clear(bit) do { (current)->trace_recursion &= ~(1<<(bit)); } while (0)
519541
#define trace_recursion_test(bit) ((current)->trace_recursion & (1<<(bit)))
520542

543+
#define trace_recursion_depth() \
544+
(((current)->trace_recursion >> TRACE_GRAPH_DEPTH_START_BIT) & 3)
545+
#define trace_recursion_set_depth(depth) \
546+
do { \
547+
current->trace_recursion &= \
548+
~(3 << TRACE_GRAPH_DEPTH_START_BIT); \
549+
current->trace_recursion |= \
550+
((depth) & 3) << TRACE_GRAPH_DEPTH_START_BIT; \
551+
} while (0)
552+
521553
#define TRACE_CONTEXT_BITS 4
522554

523555
#define TRACE_FTRACE_START TRACE_FTRACE_BIT
@@ -843,8 +875,9 @@ extern void __trace_graph_return(struct trace_array *tr,
843875
extern struct ftrace_hash *ftrace_graph_hash;
844876
extern struct ftrace_hash *ftrace_graph_notrace_hash;
845877

846-
static inline int ftrace_graph_addr(unsigned long addr)
878+
static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
847879
{
880+
unsigned long addr = trace->func;
848881
int ret = 0;
849882

850883
preempt_disable_notrace();
@@ -855,6 +888,14 @@ static inline int ftrace_graph_addr(unsigned long addr)
855888
}
856889

857890
if (ftrace_lookup_ip(ftrace_graph_hash, addr)) {
891+
892+
/*
893+
* This needs to be cleared on the return functions
894+
* when the depth is zero.
895+
*/
896+
trace_recursion_set(TRACE_GRAPH_BIT);
897+
trace_recursion_set_depth(trace->depth);
898+
858899
/*
859900
* If no irqs are to be traced, but a set_graph_function
860901
* is set, and called by an interrupt handler, we still
@@ -872,6 +913,13 @@ static inline int ftrace_graph_addr(unsigned long addr)
872913
return ret;
873914
}
874915

916+
static inline void ftrace_graph_addr_finish(struct ftrace_graph_ret *trace)
917+
{
918+
if (trace_recursion_test(TRACE_GRAPH_BIT) &&
919+
trace->depth == trace_recursion_depth())
920+
trace_recursion_clear(TRACE_GRAPH_BIT);
921+
}
922+
875923
static inline int ftrace_graph_notrace_addr(unsigned long addr)
876924
{
877925
int ret = 0;
@@ -885,7 +933,7 @@ static inline int ftrace_graph_notrace_addr(unsigned long addr)
885933
return ret;
886934
}
887935
#else
888-
static inline int ftrace_graph_addr(unsigned long addr)
936+
static inline int ftrace_graph_addr(struct ftrace_graph_ent *trace)
889937
{
890938
return 1;
891939
}
@@ -894,14 +942,17 @@ static inline int ftrace_graph_notrace_addr(unsigned long addr)
894942
{
895943
return 0;
896944
}
945+
static inline void ftrace_graph_addr_finish(struct ftrace_graph_ret *trace)
946+
{ }
897947
#endif /* CONFIG_DYNAMIC_FTRACE */
898948

899949
extern unsigned int fgraph_max_depth;
900950

901951
static inline bool ftrace_graph_ignore_func(struct ftrace_graph_ent *trace)
902952
{
903953
/* trace it when it is-nested-in or is a function enabled. */
904-
return !(trace->depth || ftrace_graph_addr(trace->func)) ||
954+
return !(trace_recursion_test(TRACE_GRAPH_BIT) ||
955+
ftrace_graph_addr(trace)) ||
905956
(trace->depth < 0) ||
906957
(fgraph_max_depth && trace->depth >= fgraph_max_depth);
907958
}

‎kernel/trace/trace_functions_graph.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ void trace_graph_return(struct ftrace_graph_ret *trace)
509509
int cpu;
510510
int pc;
511511

512+
ftrace_graph_addr_finish(trace);
513+
512514
local_irq_save(flags);
513515
cpu = raw_smp_processor_id();
514516
data = per_cpu_ptr(tr->trace_buffer.data, cpu);
@@ -532,6 +534,8 @@ void set_graph_array(struct trace_array *tr)
532534

533535
static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
534536
{
537+
ftrace_graph_addr_finish(trace);
538+
535539
if (tracing_thresh &&
536540
(trace->rettime - trace->calltime < tracing_thresh))
537541
return;

‎kernel/trace/trace_irqsoff.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ static void irqsoff_graph_return(struct ftrace_graph_ret *trace)
208208
unsigned long flags;
209209
int pc;
210210

211+
ftrace_graph_addr_finish(trace);
212+
211213
if (!func_prolog_dec(tr, &data, &flags))
212214
return;
213215

‎kernel/trace/trace_sched_wakeup.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ static void wakeup_graph_return(struct ftrace_graph_ret *trace)
270270
unsigned long flags;
271271
int pc;
272272

273+
ftrace_graph_addr_finish(trace);
274+
273275
if (!func_prolog_preempt_disable(tr, &data, &pc))
274276
return;
275277

0 commit comments

Comments
 (0)