Skip to content

Commit 773c167

Browse files
committed
ftrace: Add recording of functions that caused recursion
This adds CONFIG_FTRACE_RECORD_RECURSION that will record to a file "recursed_functions" all the functions that caused recursion while a callback to the function tracer was running. Link: https://lkml.kernel.org/r/20201106023548.102375687@goodmis.org Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Guo Ren <guoren@kernel.org> Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com> Cc: Helge Deller <deller@gmx.de> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: Christian Borntraeger <borntraeger@de.ibm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Borislav Petkov <bp@alien8.de> Cc: x86@kernel.org Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Kees Cook <keescook@chromium.org> Cc: Anton Vorontsov <anton@enomsg.org> Cc: Colin Cross <ccross@android.com> Cc: Tony Luck <tony.luck@intel.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Jiri Kosina <jikos@kernel.org> Cc: Miroslav Benes <mbenes@suse.cz> Cc: Petr Mladek <pmladek@suse.com> Cc: Joe Lawrence <joe.lawrence@redhat.com> Cc: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com> Cc: Mauro Carvalho Chehab <mchehab+huawei@kernel.org> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: linux-doc@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-csky@vger.kernel.org Cc: linux-parisc@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-s390@vger.kernel.org Cc: live-patching@vger.kernel.org Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent a25d036 commit 773c167

17 files changed

Lines changed: 306 additions & 20 deletions

File tree

‎Documentation/trace/ftrace-uses.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ can help in this regard. If you start your code with:
118118

119119
int bit;
120120

121-
bit = ftrace_test_recursion_trylock();
121+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
122122
if (bit < 0)
123123
return;
124124

@@ -130,7 +130,9 @@ The code in between will be safe to use, even if it ends up calling a
130130
function that the callback is tracing. Note, on success,
131131
ftrace_test_recursion_trylock() will disable preemption, and the
132132
ftrace_test_recursion_unlock() will enable it again (if it was previously
133-
enabled).
133+
enabled). The instruction pointer (ip) and its parent (parent_ip) is passed to
134+
ftrace_test_recursion_trylock() to record where the recursion happened
135+
(if CONFIG_FTRACE_RECORD_RECURSION is set).
134136

135137
Alternatively, if the FTRACE_OPS_FL_RECURSION flag is set on the ftrace_ops
136138
(as explained below), then a helper trampoline will be used to test

‎arch/csky/kernel/probes/ftrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
1818
struct kprobe *p;
1919
struct kprobe_ctlblk *kcb;
2020

21-
bit = ftrace_test_recursion_trylock();
21+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
2222
if (bit < 0)
2323
return;
2424

‎arch/parisc/kernel/ftrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
210210
struct kprobe *p;
211211
int bit;
212212

213-
bit = ftrace_test_recursion_trylock();
213+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
214214
if (bit < 0)
215215
return;
216216

‎arch/powerpc/kernel/kprobes-ftrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void kprobe_ftrace_handler(unsigned long nip, unsigned long parent_nip,
2020
struct kprobe_ctlblk *kcb;
2121
int bit;
2222

23-
bit = ftrace_test_recursion_trylock();
23+
bit = ftrace_test_recursion_trylock(nip, parent_nip);
2424
if (bit < 0)
2525
return;
2626

‎arch/s390/kernel/ftrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
204204
struct kprobe *p;
205205
int bit;
206206

207-
bit = ftrace_test_recursion_trylock();
207+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
208208
if (bit < 0)
209209
return;
210210

‎arch/x86/kernel/kprobes/ftrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
2020
struct kprobe_ctlblk *kcb;
2121
int bit;
2222

23-
bit = ftrace_test_recursion_trylock();
23+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
2424
if (bit < 0)
2525
return;
2626

‎fs/pstore/ftrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void notrace pstore_ftrace_call(unsigned long ip,
4141
if (unlikely(oops_in_progress))
4242
return;
4343

44-
bit = ftrace_test_recursion_trylock();
44+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
4545
if (bit < 0)
4646
return;
4747

‎include/linux/trace_recursion.h‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ enum {
9191
* not be correct. Allow for a single recursion to cover this case.
9292
*/
9393
TRACE_TRANSITION_BIT,
94+
95+
/* Used to prevent recursion recording from recursing. */
96+
TRACE_RECORD_RECURSION_BIT,
9497
};
9598

9699
#define trace_recursion_set(bit) do { (current)->trace_recursion |= (1<<(bit)); } while (0)
@@ -142,7 +145,22 @@ static __always_inline int trace_get_context_bit(void)
142145
pc & HARDIRQ_MASK ? TRACE_CTX_IRQ : TRACE_CTX_SOFTIRQ;
143146
}
144147

145-
static __always_inline int trace_test_and_set_recursion(int start, int max)
148+
#ifdef CONFIG_FTRACE_RECORD_RECURSION
149+
extern void ftrace_record_recursion(unsigned long ip, unsigned long parent_ip);
150+
# define do_ftrace_record_recursion(ip, pip) \
151+
do { \
152+
if (!trace_recursion_test(TRACE_RECORD_RECURSION_BIT)) { \
153+
trace_recursion_set(TRACE_RECORD_RECURSION_BIT); \
154+
ftrace_record_recursion(ip, pip); \
155+
trace_recursion_clear(TRACE_RECORD_RECURSION_BIT); \
156+
} \
157+
} while (0)
158+
#else
159+
# define do_ftrace_record_recursion(ip, pip) do { } while (0)
160+
#endif
161+
162+
static __always_inline int trace_test_and_set_recursion(unsigned long ip, unsigned long pip,
163+
int start, int max)
146164
{
147165
unsigned int val = current->trace_recursion;
148166
int bit;
@@ -158,8 +176,10 @@ static __always_inline int trace_test_and_set_recursion(int start, int max)
158176
* a switch between contexts. Allow for a single recursion.
159177
*/
160178
bit = TRACE_TRANSITION_BIT;
161-
if (trace_recursion_test(bit))
179+
if (trace_recursion_test(bit)) {
180+
do_ftrace_record_recursion(ip, pip);
162181
return -1;
182+
}
163183
trace_recursion_set(bit);
164184
barrier();
165185
return bit + 1;
@@ -199,9 +219,10 @@ static __always_inline void trace_clear_recursion(int bit)
199219
* Returns: -1 if a recursion happened.
200220
* >= 0 if no recursion
201221
*/
202-
static __always_inline int ftrace_test_recursion_trylock(void)
222+
static __always_inline int ftrace_test_recursion_trylock(unsigned long ip,
223+
unsigned long parent_ip)
203224
{
204-
return trace_test_and_set_recursion(TRACE_FTRACE_START, TRACE_FTRACE_MAX);
225+
return trace_test_and_set_recursion(ip, parent_ip, TRACE_FTRACE_START, TRACE_FTRACE_MAX);
205226
}
206227

207228
/**

‎kernel/livepatch/patch.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static void notrace klp_ftrace_handler(unsigned long ip,
4949

5050
ops = container_of(fops, struct klp_ops, fops);
5151

52-
bit = ftrace_test_recursion_trylock();
52+
bit = ftrace_test_recursion_trylock(ip, parent_ip);
5353
if (WARN_ON_ONCE(bit < 0))
5454
return;
5555
/*

‎kernel/trace/Kconfig‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,31 @@ config TRACE_EVAL_MAP_FILE
727727

728728
If unsure, say N.
729729

730+
config FTRACE_RECORD_RECURSION
731+
bool "Record functions that recurse in function tracing"
732+
depends on FUNCTION_TRACER
733+
help
734+
All callbacks that attach to the function tracing have some sort
735+
of protection against recursion. Even though the protection exists,
736+
it adds overhead. This option will create a file in the tracefs
737+
file system called "recursed_functions" that will list the functions
738+
that triggered a recursion.
739+
740+
This will add more overhead to cases that have recursion.
741+
742+
If unsure, say N
743+
744+
config FTRACE_RECORD_RECURSION_SIZE
745+
int "Max number of recursed functions to record"
746+
default 128
747+
depends on FTRACE_RECORD_RECURSION
748+
help
749+
This defines the limit of number of functions that can be
750+
listed in the "recursed_functions" file, that lists all
751+
the functions that caused a recursion to happen.
752+
This file can be reset, but the limit can not change in
753+
size at runtime.
754+
730755
config GCOV_PROFILE_FTRACE
731756
bool "Enable GCOV profiling on ftrace subsystem"
732757
depends on GCOV_KERNEL

0 commit comments

Comments
 (0)