Skip to contentSkip to conversation

tracing/kprobes: Fix to check notrace function with correct range

PATCH In review Masami Hiramatsu 2 participants 5 messages Aug 21, 2018 – Aug 23, 2018 1 message not in the archive Tracing

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Masami Hiramatsu

author maintainer
#1 lore raw

PATCH 1/1 tracing/kprobes: Fix to check notrace function with correct range

Fix within_notrace_func() to check notrace function correctly.

Since the ftrace_location_range(start, end) function checks the range inclusively (start <= ftrace-loc <= end), the end address must not include the entry address of next function.

However, within_notrace_func() uses kallsyms_lookup_size_offset() to get the function size and calculate the end address from adding the size to the entry address. This means the end address is the entry address of the next function.

In the result, within_notrace_func() fails to find notrace function if the next function of the target function is ftraced.

Let's subtract 1 from the end address so that ftrace_location_range() can check it correctly.

Fixes commit 45408c4f9250 ("tracing: kprobes: Prohibit probing on notrace function")
---
 kernel/trace/trace_kprobe.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
kernel/trace/trace_kprobe.c +8 -1
index 65a4157af851..ad384b31fe01 100644
@@ -513,7 +513,14 @@ static bool within_notrace_func(struct trace_kprobe *tk)
 	if (!addr || !kallsyms_lookup_size_offset(addr, &size, &offset))
 		return false;
 
-	return !ftrace_location_range(addr - offset, addr - offset + size);
+	/* Get the entry address of the target function */
+	addr -= offset;
+
+	/*
+	 * Since ftrace_location_range() does inclusive range check, we need
+	 * to subtract 1 byte from the end address.
+	 */
+	return !ftrace_location_range(addr, addr + size - 1);
 }
 #else
 #define within_notrace_func(tk)	(false)
maintainer
#2 lore raw
Quoted 15 lines · On Tue, 21 Aug 2018 22:04:57 +0900 Masami Hiramatsu <[email protected]> wrote:

Fix within_notrace_func() to check notrace function correctly.

Since the ftrace_location_range(start, end) function checks the range inclusively (start <= ftrace-loc <= end), the end address must not include the entry address of next function.

However, within_notrace_func() uses kallsyms_lookup_size_offset() to get the function size and calculate the end address from adding the size to the entry address. This means the end address is the entry address of the next function.

In the result, within_notrace_func() fails to find notrace function if the next function of the target function is ftraced.

Quoted 8 lines

Let's subtract 1 from the end address so that ftrace_location_range() can check it correctly.

Fixes commit 45408c4f9250 ("tracing: kprobes: Prohibit probing on notrace function")

---

Applied. Thanks Masami! I'll start testing this and send it upstream when it's finished.

-- Steve

maintainer
#3 lore raw
Quoted 21 lines · On Tue, 21 Aug 2018 09:42:49 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 18 lines · On Tue, 21 Aug 2018 22:04:57 +0900 Masami Hiramatsu <[email protected]> wrote:

Fix within_notrace_func() to check notrace function correctly.

Since the ftrace_location_range(start, end) function checks the range inclusively (start <= ftrace-loc <= end), the end address must not include the entry address of next function.

However, within_notrace_func() uses kallsyms_lookup_size_offset() to get the function size and calculate the end address from adding the size to the entry address. This means the end address is the entry address of the next function.

In the result, within_notrace_func() fails to find notrace function if the next function of the target function is ftraced.

Let's subtract 1 from the end address so that ftrace_location_range() can check it correctly.

Quoted 9 lines
Quoted 5 lines
Fixes commit 45408c4f9250 ("tracing: kprobes: Prohibit probing on notrace function")

---

Applied. Thanks Masami! I'll start testing this and send it upstream when it's finished.

Hmm, this fix shows the extent of not tracing functions with notrace attched much deeper. For one thing, we can't hook kprobes to the __schedule() function (which is now what all the main schedule functions call). One of my tests used this function to test kprobes and it failed.

I'll push this to Linus, but I'm wondering if we want to perhaps add a white list of functions marked "notrace" but still kprobes can trace?

-- Steve

author maintainer
#4 lore raw
Quoted 34 lines · On Wed, 22 Aug 2018 08:58:09 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 21 lines · On Tue, 21 Aug 2018 09:42:49 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 18 lines · On Tue, 21 Aug 2018 22:04:57 +0900 Masami Hiramatsu <[email protected]> wrote:

Fix within_notrace_func() to check notrace function correctly.

Since the ftrace_location_range(start, end) function checks the range inclusively (start <= ftrace-loc <= end), the end address must not include the entry address of next function.

However, within_notrace_func() uses kallsyms_lookup_size_offset() to get the function size and calculate the end address from adding the size to the entry address. This means the end address is the entry address of the next function.

In the result, within_notrace_func() fails to find notrace function if the next function of the target function is ftraced.

Let's subtract 1 from the end address so that ftrace_location_range() can check it correctly.

Quoted 9 lines
Quoted 5 lines
Fixes commit 45408c4f9250 ("tracing: kprobes: Prohibit probing on notrace function")

---

Applied. Thanks Masami! I'll start testing this and send it upstream when it's finished.

Quoted 8 lines

Hmm, this fix shows the extent of not tracing functions with notrace attched much deeper. For one thing, we can't hook kprobes to the __schedule() function (which is now what all the main schedule functions call). One of my tests used this function to test kprobes and it failed.

I'll push this to Linus, but I'm wondering if we want to perhaps add a white list of functions marked "notrace" but still kprobes can trace?

Yes, that is what I'm thinking about. Maybe most of notrace function is safe for kprobes (like __schedule()). And if it is really critical, those functions must be marked by NOKPROBE_SYMBOL().

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
maintainer
#5 lore raw
Quoted 43 lines · On Thu, 23 Aug 2018 10:18:31 +0900 Masami Hiramatsu <[email protected]> wrote:
Quoted 40 lines · On Wed, 22 Aug 2018 08:58:09 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 21 lines · On Tue, 21 Aug 2018 09:42:49 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 18 lines · On Tue, 21 Aug 2018 22:04:57 +0900 Masami Hiramatsu <[email protected]> wrote:

Fix within_notrace_func() to check notrace function correctly.

Since the ftrace_location_range(start, end) function checks the range inclusively (start <= ftrace-loc <= end), the end address must not include the entry address of next function.

However, within_notrace_func() uses kallsyms_lookup_size_offset() to get the function size and calculate the end address from adding the size to the entry address. This means the end address is the entry address of the next function.

In the result, within_notrace_func() fails to find notrace function if the next function of the target function is ftraced.

Let's subtract 1 from the end address so that ftrace_location_range() can check it correctly.

Quoted 9 lines
Quoted 5 lines
Fixes commit 45408c4f9250 ("tracing: kprobes: Prohibit probing on notrace function")

---

Applied. Thanks Masami! I'll start testing this and send it upstream when it's finished.

Hmm, this fix shows the extent of not tracing functions with notrace attched much deeper. For one thing, we can't hook kprobes to the __schedule() function (which is now what all the main schedule functions call). One of my tests used this function to test kprobes and it failed.

Quoted 7 lines
Quoted 2 lines

I'll push this to Linus, but I'm wondering if we want to perhaps add a white list of functions marked "notrace" but still kprobes can trace?

Yes, that is what I'm thinking about. Maybe most of notrace function is safe for kprobes (like __schedule()). And if it is really critical, those functions must be marked by NOKPROBE_SYMBOL().

Correct. This notrace was added because we want to not trace any function call between preempt_schedule() and where NEED_RESCHED is cleared. Because the function tracer does a preempt_enable_notrace() which triggers the preempt_schedule() call again, and we can end up in a recursive loop. But that is fixed, but by tracing __schedule() we do a song and dance of calling preempt_schedule_notrace and makes the trace output weird.

See commit 499d79559ffe4.

-- Steve