Skip to contentSkip to conversation

tracing: Fix infinite loop in ftrace_update_pid_func()

PATCH In review Matt Fleming 4 participants 7 messages Sep 28, 2009 – Oct 1, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Fix infinite loop in ftrace_update_pid_func()

From: Matt Fleming <[email protected]>

When CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST is enabled __ftrace_trace_function contains the current trace function, not ftrace_trace_function. In ftrace_update_pid_func() we currently incorrectly assign the value of ftrace_trace_function to __ftrace_trace_funcion before returning.

Without this patch it is possible to execute an infinite loop whereby ftrace_test_stop_func() calls __ftrace_trace_function, which was assigned ftrace_test_stop_func() in ftrace_update_pid_func().

---
 kernel/trace/ftrace.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/trace/ftrace.c +4 -0
index 25edd5c..d9ba6d9 100644
@@ -225,7 +225,11 @@ static void ftrace_update_pid_func(void)
 	if (ftrace_trace_function == ftrace_stub)
 		return;
 
+#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
 	func = ftrace_trace_function;
+#else
+	func = __ftrace_trace_function;
+#endif
 
 	if (ftrace_pid_trace) {
 		set_ftrace_pid_function(func);
Signature · 1 line
1.6.3.GIT
#1 lore raw
Quoted 22 lines · Matt Fleming

From: Matt Fleming <[email protected]>

When CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST is enabled __ftrace_trace_function contains the current trace function, not ftrace_trace_function. In ftrace_update_pid_func() we currently incorrectly assign the value of ftrace_trace_function to __ftrace_trace_funcion before returning.

Without this patch it is possible to execute an infinite loop whereby ftrace_test_stop_func() calls __ftrace_trace_function, which was assigned ftrace_test_stop_func() in ftrace_update_pid_func().

---
 kernel/trace/ftrace.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/trace/ftrace.c +0 -0
index 25edd5c..d9ba6d9 100644

kernel/trace/ftrace.c static void ftrace_update_pid_func(void) · line 232

Quoted 8 lines
kernel/trace/ftrace.c +4 -0
@@ -225,7 +225,11 @@ static void ftrace_update_pid_func(void)
 	if (ftrace_trace_function == ftrace_stub)
 		return;
 
+#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
 	func = ftrace_trace_function;
+#else
+	func = __ftrace_trace_function;
+#endif

I don't understand how it can do that infinite loop. It doesn't seem the following can happen:

func = ftrace_trace_function //which is ftrace_test_stop_func

_ftrace_trace_function = func

Because we can sum up the path like that:

func = ftrace_trace_function;

...

#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
	ftrace_trace_function = func;
#else
	__ftrace_trace_function = func;

#endif

So if we are in CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST, that doesn't seem to harm, although it seems that what we want is

func = __ftrace_trace_function

...

__ftrace_trace_function = func

Because we want to always keep ftrace_test_stop_func as the top level wrapper. Which would be what does the above, plus the fact we avoid any recursivity.

But it's possible I'm missing the point...

Also, Steve, is this an option that is still useful? It's even not in Kconfig.

#2 lore raw
Quoted 37 lines · Frederic Weisbecker
Quoted 22 lines · Matt Fleming

From: Matt Fleming <[email protected]>

When CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST is enabled __ftrace_trace_function contains the current trace function, not ftrace_trace_function. In ftrace_update_pid_func() we currently incorrectly assign the value of ftrace_trace_function to __ftrace_trace_funcion before returning.

Without this patch it is possible to execute an infinite loop whereby ftrace_test_stop_func() calls __ftrace_trace_function, which was assigned ftrace_test_stop_func() in ftrace_update_pid_func().

---
 kernel/trace/ftrace.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/trace/ftrace.c +0 -0
index 25edd5c..d9ba6d9 100644

kernel/trace/ftrace.c static void ftrace_update_pid_func(void) · line 232

Quoted 8 lines
kernel/trace/ftrace.c +4 -0
@@ -225,7 +225,11 @@ static void ftrace_update_pid_func(void)
 	if (ftrace_trace_function == ftrace_stub)
 		return;
 
+#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
 	func = ftrace_trace_function;
+#else
+	func = __ftrace_trace_function;
+#endif

I don't understand how it can do that infinite loop. It doesn't seem the following can happen:

Quoted 4 lines

func = ftrace_trace_function //which is ftrace_test_stop_func

_ftrace_trace_function = func

Here is the unpatched version of ftrace_update_pid_func() from kernel/trace/ftrace.c

Before calling:
ftrace_trace_function = ftrace_test_stop_func __ftrace_trace_Function = real tracing function

static void ftrace_update_pid_func(void)

{
	ftrace_func_t func;

	if (ftrace_trace_function == ftrace_stub)
		return;

	func = ftrace_trace_function;

	if (ftrace_pid_trace) {
		set_ftrace_pid_function(func);
		func = ftrace_pid_func;
	} else {
		if (func == ftrace_pid_func)
			func = ftrace_pid_function;
	}
#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
	ftrace_trace_function = func;
#else
	__ftrace_trace_function = func;

#endif
}

After calling:
ftrace_trace_function = ftrace_test_stop_func __ftrace_trace_function = ftrace_test_stop_func

Then look what happens next time ftrace_test_stop_func() is called via ftrace_call,

#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
/*
* For those archs that do not test ftrace_trace_stop in their
* mcount call site, we need to do it from C.
*/
static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)

{
	if (function_trace_stop)
		return;

	__ftrace_trace_function(ip, parent_ip);
}

#endif

Here we're going to recurse infinitely because,
__ftrace_trace_function = ftrace_test_stop_func

maintainer
#3 lore raw
In reply to Frederic Weisbecker
Quoted 63 lines · Frederic Weisbecker
Quoted 22 lines · Matt Fleming

From: Matt Fleming <[email protected]>

When CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST is enabled __ftrace_trace_function contains the current trace function, not ftrace_trace_function. In ftrace_update_pid_func() we currently incorrectly assign the value of ftrace_trace_function to __ftrace_trace_funcion before returning.

Without this patch it is possible to execute an infinite loop whereby ftrace_test_stop_func() calls __ftrace_trace_function, which was assigned ftrace_test_stop_func() in ftrace_update_pid_func().

---
 kernel/trace/ftrace.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/trace/ftrace.c +0 -0
index 25edd5c..d9ba6d9 100644

kernel/trace/ftrace.c static void ftrace_update_pid_func(void) · line 232

Quoted 8 lines
kernel/trace/ftrace.c +4 -0
@@ -225,7 +225,11 @@ static void ftrace_update_pid_func(void)
 	if (ftrace_trace_function == ftrace_stub)
 		return;
 
+#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
 	func = ftrace_trace_function;
+#else
+	func = __ftrace_trace_function;
+#endif

I don't understand how it can do that infinite loop. It doesn't seem the following can happen:

func = ftrace_trace_function //which is ftrace_test_stop_func

_ftrace_trace_function = func

Because we can sum up the path like that:

func = ftrace_trace_function;

...

#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
	ftrace_trace_function = func;
#else
	__ftrace_trace_function = func;

#endif

So if we are in CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST, that doesn't seem to harm, although it seems that what we want is

func = __ftrace_trace_function

...

__ftrace_trace_function = func

Quoted 9 lines

Because we want to always keep ftrace_test_stop_func as the top level wrapper. Which would be what does the above, plus the fact we avoid any recursivity.

But it's possible I'm missing the point...

Also, Steve, is this an option that is still useful? It's even not in Kconfig.

Yeah, Matt found a real bug, and yes it will infinitely recurse. The thing is that the function can call itself which will continue to do so.

As for the Kconfig, it's something that currently only x86 selects, because x86 does the test in entry.S.

Matt,

Thanks, I'll pull this in as soon as I get back from Dresden.

-- Steve

#4 lore raw
Quoted 6 lines · Steven Rostedt

Matt,

Thanks, I'll pull this in as soon as I get back from Dresden.

-- Steve

Thanks Steve!

#5 lore raw
In reply to Matt Fleming
Quoted 8 lines · Matt Fleming

From: Matt Fleming <[email protected]>

When CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST is enabled __ftrace_trace_function contains the current trace function, not ftrace_trace_function. In ftrace_update_pid_func() we currently incorrectly assign the value of ftrace_trace_function to __ftrace_trace_funcion before returning.

Quoted 4 lines

Without this patch it is possible to execute an infinite loop whereby ftrace_test_stop_func() calls __ftrace_trace_function, which was assigned ftrace_test_stop_func() in ftrace_update_pid_func().

Oops..

I just realised I've used the phrase "infinite loop" when what I meant to say was "infinite recursion". Sorry, brain fart.

#6 lore raw

[tip:tracing/urgent] tracing: Fix infinite recursion in ftrace_update_pid_func()

In reply to Matt Fleming
Commit-ID:  33974093c024f08caadd2fc71a83bd811ed1831d
Gitweb:     http://git.kernel.org/tip/33974093c024f08caadd2fc71a83bd811ed1831d
Author:     Matt Fleming <[email protected]>
AuthorDate: Mon, 28 Sep 2009 16:43:01 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 1 Oct 2009 08:19:24 +0200

tracing: Fix infinite recursion in ftrace_update_pid_func()

When CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST is enabled __ftrace_trace_function contains the current trace function, not ftrace_trace_function.

In ftrace_update_pid_func() we currently incorrectly assign the value of ftrace_trace_function to __ftrace_trace_funcion before returning.

Without this patch it is possible to execute an infinite recursion whereby ftrace_test_stop_func() calls __ftrace_trace_function, which was assigned ftrace_test_stop_func() in ftrace_update_pid_func().

LKML-Reference: <[email protected]>

---
 kernel/trace/ftrace.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
kernel/trace/ftrace.c +4 -0
index 46592fe..3724756 100644
@@ -225,7 +225,11 @@ static void ftrace_update_pid_func(void)
 	if (ftrace_trace_function == ftrace_stub)
 		return;
 
+#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
 	func = ftrace_trace_function;
+#else
+	func = __ftrace_trace_function;
+#endif
 
 	if (ftrace_pid_trace) {
 		set_ftrace_pid_function(func);