Skip to contentSkip to conversation

Make fprobe + rethook immune to recursion

PATCH Superseded Ze Gao 5 participants 21 messages May 16, 2023 – May 17, 2023

Subsystems (6)
Stable branch x86 BPF RISC-V +2 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ze Gao

author
#0 lore raw

v2 cover Make fprobe + rethook immune to recursion

Hi all,

This is the 2nd version of patch series to fix the ftrace rethook recursion problem.

v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/T/ +++#md4c0bae6a6cae28dadf2a2c6105ff140b35fddea

As Steven suggested, this version removes unnecessary notrace annotations from fprobe and rethook functions from v1 [PATCH 2,3,4/4] and replaces with makefile changes to filter out compiler flags which ftrace depends upon for rethook related objects.

Ze Gao (4):
rethook: use preempt_{disable, enable}_notrace in
rethook_trampoline_handler
fprobe: make fprobe_kprobe_handler recursion free
fprobe: add recursion detection in fprobe_exit_handler rehook, fprobe: do not trace rethook related functions

 arch/riscv/kernel/probes/Makefile |  2 +
 arch/s390/kernel/Makefile         |  1 +
 arch/x86/kernel/Makefile          |  1 +
 kernel/trace/fprobe.c             | 72 ++++++++++++++++++++++++-------
 kernel/trace/rethook.c            |  4 +-
 5 files changed, 63 insertions(+), 17 deletions(-)
Signature · 1 line
2.40.1
author
#1 lore raw

PATCH v2 1/4 rethook: use preempt_{disable, enable}_notrace in rethook_trampoline_handler

This patch replaces preempt_{disable, enable} with its corresponding notrace version in rethook_trampoline_handler so no worries about stack recursion or overflow introduced by preempt_count_{add, sub} under fprobe + rethook context.

Fixes 54ecbe6f1ed5 ("rethook: Add a generic return hook")
---
 kernel/trace/rethook.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/trace/rethook.c +2 -2
index 32c3dfdb4d6a..60f6cb2b486b 100644
@@ -288,7 +288,7 @@ unsigned long rethook_trampoline_handler(struct pt_regs *regs,
 	 * These loops must be protected from rethook_free_rcu() because those
 	 * are accessing 'rhn->rethook'.
 	 */
-	preempt_disable();
+	preempt_disable_notrace();
 
 	/*
 	 * Run the handler on the shadow stack. Do not unlink the list here because
@@ -321,7 +321,7 @@ unsigned long rethook_trampoline_handler(struct pt_regs *regs,
 		first = first->next;
 		rethook_recycle(rhn);
 	}
-	preempt_enable();
+	preempt_enable_notrace();
 
 	return correct_ret_addr;
 }
Signature · 1 line
2.40.1
author
#2 lore raw

PATCH v2 2/4 fprobe: make fprobe_kprobe_handler recursion free

In reply to Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Refactor the common part out of fprobe_kprobe_handler and fprobe_ handler and call ftrace recursion detection at the very beginning, so that the whole fprobe_kprobe_handler is free from recursion.

---
 kernel/trace/fprobe.c | 59 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 15 deletions(-)
kernel/trace/fprobe.c +44 -15
index 9abb3905bc8e..097c740799ba 100644
@@ -20,30 +20,22 @@ struct fprobe_rethook_node {
 	char data[];
 };
 
-static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
-			   struct ftrace_ops *ops, struct ftrace_regs *fregs)
+static inline void __fprobe_handler(unsigned long ip, unsigned long
+		parent_ip, struct ftrace_ops *ops, struct ftrace_regs *fregs)
 {
 	struct fprobe_rethook_node *fpr;
 	struct rethook_node *rh = NULL;
 	struct fprobe *fp;
 	void *entry_data = NULL;
-	int bit, ret;
+	int ret;
 
 	fp = container_of(ops, struct fprobe, ops);
-	if (fprobe_disabled(fp))
-		return;
-
-	bit = ftrace_test_recursion_trylock(ip, parent_ip);
-	if (bit < 0) {
-		fp->nmissed++;
-		return;
-	}
 
 	if (fp->exit_handler) {
 		rh = rethook_try_get(fp->rethook);
 		if (!rh) {
 			fp->nmissed++;
-			goto out;
+			return;
 		}
 		fpr = container_of(rh, struct fprobe_rethook_node, node);
 		fpr->entry_ip = ip;
@@ -61,23 +53,60 @@ static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
 		else
 			rethook_hook(rh, ftrace_get_regs(fregs), true);
 	}
-out:
+}
+
+static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
+		struct ftrace_ops *ops, struct ftrace_regs *fregs)
+{
+	struct fprobe *fp;
+	int bit;
+
+	fp = container_of(ops, struct fprobe, ops);
+	if (fprobe_disabled(fp))
+		return;
+
+	/* recursion detection has to go before any traceable function and
+	 * all functions before this point should be marked as notrace
+	 */
+	bit = ftrace_test_recursion_trylock(ip, parent_ip);
+	if (bit < 0) {
+		fp->nmissed++;
+		return;
+	}
+	__fprobe_handler(ip, parent_ip, ops, fregs);
 	ftrace_test_recursion_unlock(bit);
+
 }
 NOKPROBE_SYMBOL(fprobe_handler);
 
 static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
 				  struct ftrace_ops *ops, struct ftrace_regs *fregs)
 {
-	struct fprobe *fp = container_of(ops, struct fprobe, ops);
+	struct fprobe *fp;
+	int bit;
+
+	fp = container_of(ops, struct fprobe, ops);
+	if (fprobe_disabled(fp))
+		return;
+
+	/* recursion detection has to go before any traceable function and
+	 * all functions called before this point should be marked as notrace
+	 */
+	bit = ftrace_test_recursion_trylock(ip, parent_ip);
+	if (bit < 0) {
+		fp->nmissed++;
+		return;
+	}
 
 	if (unlikely(kprobe_running())) {
 		fp->nmissed++;
 		return;
 	}
+
 	kprobe_busy_begin();
-	fprobe_handler(ip, parent_ip, ops, fregs);
+	__fprobe_handler(ip, parent_ip, ops, fregs);
 	kprobe_busy_end();
+	ftrace_test_recursion_unlock(bit);
 }
 
 static void fprobe_exit_handler(struct rethook_node *rh, void *data,
Signature · 1 line
2.40.1
#3 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Quoted 33 lines · Ze Gao
kernel/trace/fprobe.c +25 -1
+static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
+		struct ftrace_ops *ops, struct ftrace_regs *fregs)
+{
+	struct fprobe *fp;
+	int bit;
+
+	fp = container_of(ops, struct fprobe, ops);
+	if (fprobe_disabled(fp))
+		return;
+
+	/* recursion detection has to go before any traceable function and
+	 * all functions before this point should be marked as notrace
+	 */
+	bit = ftrace_test_recursion_trylock(ip, parent_ip);
+	if (bit < 0) {
+		fp->nmissed++;
+		return;
+	}
+	__fprobe_handler(ip, parent_ip, ops, fregs);
 	ftrace_test_recursion_unlock(bit);
+
 }
 NOKPROBE_SYMBOL(fprobe_handler);
 
 static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
 				  struct ftrace_ops *ops, struct ftrace_regs *fregs)
 {
-	struct fprobe *fp = container_of(ops, struct fprobe, ops);
+	struct fprobe *fp;
+	int bit;
+
+	fp = container_of(ops, struct fprobe, ops);
+	if (fprobe_disabled(fp))

kernel/trace/fprobe.c static void fprobe_handler(unsigned long ip, unsigned long parent_ip, · line 99

Quoted 10 lines
kernel/trace/fprobe.c +10 -0
+		return;
+
+	/* recursion detection has to go before any traceable function and
+	 * all functions called before this point should be marked as notrace
+	 */
+	bit = ftrace_test_recursion_trylock(ip, parent_ip);
+	if (bit < 0) {
+		fp->nmissed++;
+		return;
+	}

Please don't use this comment style; multi line comments go like:

	/*
	 * Multi line comment ...
	 *                    ... is symmetric.
	 */

Same for your next patch.

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#4 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Thanks for pointing this out, I'll get it all fixed ASAP.

Regards,
Ze

Quoted 55 lines · Peter Zijlstra
Quoted 33 lines · Ze Gao
kernel/trace/fprobe.c +25 -1
+static void fprobe_handler(unsigned long ip, unsigned long parent_ip,
+             struct ftrace_ops *ops, struct ftrace_regs *fregs)
+{
+     struct fprobe *fp;
+     int bit;
+
+     fp = container_of(ops, struct fprobe, ops);
+     if (fprobe_disabled(fp))
+             return;
+
+     /* recursion detection has to go before any traceable function and
+      * all functions before this point should be marked as notrace
+      */
+     bit = ftrace_test_recursion_trylock(ip, parent_ip);
+     if (bit < 0) {
+             fp->nmissed++;
+             return;
+     }
+     __fprobe_handler(ip, parent_ip, ops, fregs);
      ftrace_test_recursion_unlock(bit);
+
 }
 NOKPROBE_SYMBOL(fprobe_handler);
 
 static void fprobe_kprobe_handler(unsigned long ip, unsigned long parent_ip,
                                struct ftrace_ops *ops, struct ftrace_regs *fregs)
 {
-     struct fprobe *fp = container_of(ops, struct fprobe, ops);
+     struct fprobe *fp;
+     int bit;
+
+     fp = container_of(ops, struct fprobe, ops);
+     if (fprobe_disabled(fp))

kernel/trace/fprobe.c static void fprobe_handler(unsigned long ip, unsigned long parent_ip, · line 99

Quoted 10 lines
kernel/trace/fprobe.c +10 -0
+             return;
+
+     /* recursion detection has to go before any traceable function and
+      * all functions called before this point should be marked as notrace
+      */
+     bit = ftrace_test_recursion_trylock(ip, parent_ip);
+     if (bit < 0) {
+             fp->nmissed++;
+             return;
+     }

Please don't use this comment style; multi line comments go like:

/*
* Multi line comment ...
* ... is symmetric.
*/

Same for your next patch.

#5 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

In reply to Ze Gao
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author
#6 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve.
I will refine the commit message to make it clear.

FYI, details can checked out here:

    Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Regards,
Ze

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

author
#7 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Sorry for paste the wrong link, it's this one instead:

It's the original discussions of this problem.

Regards,
Ze

Quoted 21 lines · Ze Gao

Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve.
I will refine the commit message to make it clear.

FYI, details can checked out here:

    Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Regards,
Ze

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

#8 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

In reply to Ze Gao
Quoted 2 lines · On Tue, 16 May 2023 17:47:52 +0800 Ze Gao <[email protected]> wrote:

Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve.

Note that kprobe_busy_{begin,end} don't need to use notrace version because kprobe itself prohibits probing on preempt_count_{add,sub}.

Thank you,

Quoted 18 lines

I will refine the commit message to make it clear.

FYI, details can checked out here:

    Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Regards,
Ze

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#9 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Oops, I misunderstood your comments before.

Yes, it's not necessary to do this reordering as regards to kprobe.

Thanks for your review.

I'll rebase onto the latest tree and send v3 ASAP.

Regards,
Ze

Quoted 34 lines · Masami Hiramatsu
Quoted 2 lines · On Tue, 16 May 2023 17:47:52 +0800 Ze Gao <[email protected]> wrote:

Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve.

Note that kprobe_busy_{begin,end} don't need to use notrace version because kprobe itself prohibits probing on preempt_count_{add,sub}.

Thank you,

Quoted 18 lines

I will refine the commit message to make it clear.

FYI, details can checked out here:

    Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Regards,
Ze

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

--
Masami Hiramatsu (Google) <[email protected]>

maintainer Acked-by
#10 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Quoted 3 lines · On Wed, 17 May 2023 09:54:53 +0800 Ze Gao <[email protected]> wrote:

Oops, I misunderstood your comments before.

Yes, it's not necessary to do this reordering as regards to kprobe.

Let me confirm, I meant that your current patch is correct. I just mentioned that kprobe_busy_{begin,end} will continue use standard version because kprobe itself handles that. Please update only the patch description and add my ack.

If you add Steve's call graph for the explanation, it will help us to understand what will be fixed.

Thank you,

Quoted 43 lines

Thanks for your review.

I'll rebase onto the latest tree and send v3 ASAP.

Regards,
Ze

Quoted 34 lines · Masami Hiramatsu
Quoted 2 lines · On Tue, 16 May 2023 17:47:52 +0800 Ze Gao <[email protected]> wrote:

Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve.

Note that kprobe_busy_{begin,end} don't need to use notrace version because kprobe itself prohibits probing on preempt_count_{add,sub}.

Thank you,

Quoted 18 lines

I will refine the commit message to make it clear.

FYI, details can checked out here:

    Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Regards,
Ze

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

--
Masami Hiramatsu (Google) <[email protected]>

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#11 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Got it! :)

I will improve the commit message and send v3 ASAP.

BTW, which tree should I rebase those patches onto? Is that the for-next branch of git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw Jiri had troubles
applying those since these works are based on v6.4.0.

THX,
Ze

Quoted 67 lines · Masami Hiramatsu
Quoted 3 lines · On Wed, 17 May 2023 09:54:53 +0800 Ze Gao <[email protected]> wrote:

Oops, I misunderstood your comments before.

Yes, it's not necessary to do this reordering as regards to kprobe.

Let me confirm, I meant that your current patch is correct. I just mentioned that kprobe_busy_{begin,end} will continue use standard version because kprobe itself handles that. Please update only the patch description and add my ack.

If you add Steve's call graph for the explanation, it will help us to understand what will be fixed.

Thank you,

Quoted 43 lines

Thanks for your review.

I'll rebase onto the latest tree and send v3 ASAP.

Regards,
Ze

Quoted 34 lines · Masami Hiramatsu
Quoted 2 lines · On Tue, 16 May 2023 17:47:52 +0800 Ze Gao <[email protected]> wrote:

Precisely, these that are called within kprobe_busy_{begin, end}, which the previous patch does not resolve.

Note that kprobe_busy_{begin,end} don't need to use notrace version because kprobe itself prohibits probing on preempt_count_{add,sub}.

Thank you,

Quoted 18 lines

I will refine the commit message to make it clear.

FYI, details can checked out here:

    Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/

Regards,
Ze

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Ze Gao

Current implementation calls kprobe related functions before doing ftrace recursion check in fprobe_kprobe_handler, which opens door to kernel crash due to stack recursion if preempt_count_{add, sub} is traceable.

Which preempt_count*() are you referring to? The ones you just made _notrace in the previous patch?

--
Masami Hiramatsu (Google) <[email protected]>

--
Masami Hiramatsu (Google) <[email protected]>

maintainer
#12 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Quoted 10 lines · On Wed, 17 May 2023 11:10:21 +0800 Ze Gao <[email protected]> wrote:

Got it! :)

I will improve the commit message and send v3 ASAP.

BTW, which tree should I rebase those patches onto? Is that the for-next branch of git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw Jiri had troubles
applying those since these works are based on v6.4.0.

You can submit against 6.4-rc1. We haven't updated the for-next branch yet. Which will be rebased off of one of the 6.4 rc's.

-- Steve

#13 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

Quoted 14 lines · On Tue, 16 May 2023 23:25:45 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines · On Wed, 17 May 2023 11:10:21 +0800 Ze Gao <[email protected]> wrote:

Got it! :)

I will improve the commit message and send v3 ASAP.

BTW, which tree should I rebase those patches onto? Is that the for-next branch of git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw Jiri had troubles
applying those since these works are based on v6.4.0.

Quoted 2 lines

You can submit against 6.4-rc1. We haven't updated the for-next branch yet. Which will be rebased off of one of the 6.4 rc's.

Yeah, I would like to pick it to probes/fixes for 6.4 and stable branches.

Thank you,

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author
#14 lore raw

Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free

In reply to Steven Rostedt

Hi Jiri,
This is the latest version against 6.4-rc1, and you can apply without trouble.

     https://lore.kernel.org/linux-trace-kernel/[email protected]/T/#t

Regards,
Ze

Quoted 19 lines · Steven Rostedt
Quoted 10 lines · On Wed, 17 May 2023 11:10:21 +0800 Ze Gao <[email protected]> wrote:

Got it! :)

I will improve the commit message and send v3 ASAP.

BTW, which tree should I rebase those patches onto? Is that the for-next branch of git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git. I saw Jiri had troubles
applying those since these works are based on v6.4.0.

You can submit against 6.4-rc1. We haven't updated the for-next branch yet. Which will be rebased off of one of the 6.4 rc's.

-- Steve

author
#15 lore raw

PATCH v2 3/4 fprobe: add recursion detection in fprobe_exit_handler

In reply to Ze Gao

fprobe_hander and fprobe_kprobe_handler has guarded ftrace recursion detection but fprobe_exit_handler has not, which possibly introduce recursive calls if the fprobe exit callback calls any traceable functions. Checking in fprobe_hander or fprobe_kprobe_handler is not enough and misses this case.

So add recursion free guard the same way as fprobe_hander. Since ftrace recursion check does not employ ip(s), so here use entry_ip and entry_parent_ip the same as fprobe_handler.

Fixes 5b0ab78998e3 ("fprobe: Add exit_handler support")
---
 kernel/trace/fprobe.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
kernel/trace/fprobe.c +13 -0
index 097c740799ba..a9580a88cc15 100644
@@ -17,6 +17,7 @@
 struct fprobe_rethook_node {
 	struct rethook_node node;
 	unsigned long entry_ip;
+	unsigned long entry_parent_ip;
 	char data[];
 };
 
@@ -39,6 +40,7 @@ static inline void __fprobe_handler(unsigned long ip, unsigned long
 		}
 		fpr = container_of(rh, struct fprobe_rethook_node, node);
 		fpr->entry_ip = ip;
+		fpr->entry_parent_ip = parent_ip;
 		if (fp->entry_data_size)
 			entry_data = fpr->data;
 	}
@@ -114,14 +116,25 @@ static void fprobe_exit_handler(struct rethook_node *rh, void *data,
 {
 	struct fprobe *fp = (struct fprobe *)data;
 	struct fprobe_rethook_node *fpr;
+	int bit;
 
 	if (!fp || fprobe_disabled(fp))
 		return;
 
 	fpr = container_of(rh, struct fprobe_rethook_node, node);
 
+	/* we need to assure no calls to traceable functions in-between the
+	 * end of fprobe_handler and the beginning of fprobe_exit_handler.
+	 */
+	bit = ftrace_test_recursion_trylock(fpr->entry_ip, fpr->entry_parent_ip);
+	if (bit < 0) {
+		fp->nmissed++;
+		return;
+	}
+
 	fp->exit_handler(fp, fpr->entry_ip, regs,
 			 fp->entry_data_size ? (void *)fpr->data : NULL);
+	ftrace_test_recursion_unlock(bit);
 }
 NOKPROBE_SYMBOL(fprobe_exit_handler);
 
Signature · 1 line
2.40.1
author
#16 lore raw

PATCH v2 4/4 rehook, fprobe: do not trace rethook related functions

In reply to Ze Gao

These functions are already marked as NOKPROBE to prevent recursion and we have the same reason to blacklist them if rethook is used with fprobe, since they are beyond the recursion-free region ftrace can guard.

---
 arch/riscv/kernel/probes/Makefile | 2 ++
 arch/s390/kernel/Makefile         | 1 +
 arch/x86/kernel/Makefile          | 1 +
 3 files changed, 4 insertions(+)
arch/riscv/kernel/probes/Makefile +2 -0
index c40139e9ca47..8265ff497977 100644
@@ -4,3 +4,5 @@ obj-$(CONFIG_RETHOOK)		+= rethook.o rethook_trampoline.o
 obj-$(CONFIG_KPROBES_ON_FTRACE)	+= ftrace.o
 obj-$(CONFIG_UPROBES)		+= uprobes.o decode-insn.o simulate-insn.o
 CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_rethook.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_rethook_trampoline.o = $(CC_FLAGS_FTRACE)
arch/s390/kernel/Makefile +1 -0
index 8983837b3565..6b2a051e1f8a 100644
@@ -10,6 +10,7 @@ CFLAGS_REMOVE_ftrace.o		= $(CC_FLAGS_FTRACE)
 
 # Do not trace early setup code
 CFLAGS_REMOVE_early.o		= $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_rethook.o		= $(CC_FLAGS_FTRACE)
 
 endif
 
arch/x86/kernel/Makefile +1 -0
index dd61752f4c96..4070a01c11b7 100644
@@ -17,6 +17,7 @@ CFLAGS_REMOVE_ftrace.o = -pg
 CFLAGS_REMOVE_early_printk.o = -pg
 CFLAGS_REMOVE_head64.o = -pg
 CFLAGS_REMOVE_sev.o = -pg
+CFLAGS_REMOVE_rethook.o = -pg
 endif
 
 KASAN_SANITIZE_head$(BITS).o				:= n
Signature · 1 line
2.40.1
maintainer
#17 lore raw

Re: [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions

Quoted 10 lines · On Tue, 16 May 2023 15:18:30 +0800 Ze Gao <[email protected]> wrote:

CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE)

arch/s390/kernel/Makefile +1 -0
+CFLAGS_REMOVE_rethook.o		= $(CC_FLAGS_FTRACE)
 
 endif
arch/x86/kernel/Makefile +0 -0
index dd61752f4c96..4070a01c11b7 100644

arch/x86/kernel/Makefile CFLAGS_REMOVE_ftrace.o = -pg · line 20

Quoted 4 lines
arch/x86/kernel/Makefile +1 -0
@@ -17,6 +17,7 @@ CFLAGS_REMOVE_ftrace.o = -pg
 CFLAGS_REMOVE_early_printk.o = -pg
 CFLAGS_REMOVE_head64.o = -pg
 CFLAGS_REMOVE_sev.o = -pg
+CFLAGS_REMOVE_rethook.o = -pg

Unrelated to this patch, but someday we need to change the -pg above to $(CC_FLAGS_FTRACE).

-- Steve

Quoted 4 lines
arch/x86/kernel/Makefile +0 -0
 endif
 
 KASAN_SANITIZE_head$(BITS).o				:= n
author
#18 lore raw

Re: [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions

Got it! Thank you, Steevn. Maybe I can give it a try later :)

Regards,
Ze

Quoted 29 lines · Steven Rostedt
Quoted 10 lines · On Tue, 16 May 2023 15:18:30 +0800 Ze Gao <[email protected]> wrote:

CFLAGS_REMOVE_early.o = $(CC_FLAGS_FTRACE)

arch/s390/kernel/Makefile +1 -0
+CFLAGS_REMOVE_rethook.o              = $(CC_FLAGS_FTRACE)
 
 endif
arch/x86/kernel/Makefile +0 -0
index dd61752f4c96..4070a01c11b7 100644

arch/x86/kernel/Makefile CFLAGS_REMOVE_ftrace.o = -pg · line 20

Quoted 4 lines
arch/x86/kernel/Makefile +1 -0
@@ -17,6 +17,7 @@ CFLAGS_REMOVE_ftrace.o = -pg
 CFLAGS_REMOVE_early_printk.o = -pg
 CFLAGS_REMOVE_head64.o = -pg
 CFLAGS_REMOVE_sev.o = -pg
+CFLAGS_REMOVE_rethook.o = -pg

Unrelated to this patch, but someday we need to change the -pg above to $(CC_FLAGS_FTRACE).

-- Steve

Quoted 4 lines
arch/x86/kernel/Makefile +0 -1
 endif
 
 KASAN_SANITIZE_head$(BITS).o                         := n
--
maintainer Acked-by on v2 4/4
#19 lore raw

Re: [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions

In reply to Ze Gao
Quoted 5 lines · On Tue, 16 May 2023 15:18:30 +0800 Ze Gao <[email protected]> wrote:

These functions are already marked as NOKPROBE to prevent recursion and we have the same reason to blacklist them if rethook is used with fprobe, since they are beyond the recursion-free region ftrace can guard.

Looks good to me.

Thank you!

Quoted 43 lines
---
 arch/riscv/kernel/probes/Makefile | 2 ++
 arch/s390/kernel/Makefile         | 1 +
 arch/x86/kernel/Makefile          | 1 +
 3 files changed, 4 insertions(+)
arch/riscv/kernel/probes/Makefile +2 -0
index c40139e9ca47..8265ff497977 100644
@@ -4,3 +4,5 @@ obj-$(CONFIG_RETHOOK)		+= rethook.o rethook_trampoline.o
 obj-$(CONFIG_KPROBES_ON_FTRACE)	+= ftrace.o
 obj-$(CONFIG_UPROBES)		+= uprobes.o decode-insn.o simulate-insn.o
 CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_rethook.o = $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_rethook_trampoline.o = $(CC_FLAGS_FTRACE)
arch/s390/kernel/Makefile +1 -0
index 8983837b3565..6b2a051e1f8a 100644
@@ -10,6 +10,7 @@ CFLAGS_REMOVE_ftrace.o		= $(CC_FLAGS_FTRACE)
 
 # Do not trace early setup code
 CFLAGS_REMOVE_early.o		= $(CC_FLAGS_FTRACE)
+CFLAGS_REMOVE_rethook.o		= $(CC_FLAGS_FTRACE)
 
 endif
 
arch/x86/kernel/Makefile +1 -0
index dd61752f4c96..4070a01c11b7 100644
@@ -17,6 +17,7 @@ CFLAGS_REMOVE_ftrace.o = -pg
 CFLAGS_REMOVE_early_printk.o = -pg
 CFLAGS_REMOVE_head64.o = -pg
 CFLAGS_REMOVE_sev.o = -pg
+CFLAGS_REMOVE_rethook.o = -pg
 endif
 
 KASAN_SANITIZE_head$(BITS).o				:= n
Signature · 1 line
2.40.1
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#20 lore raw
In reply to Ze Gao
Quoted 10 lines · Ze Gao

Hi all,

This is the 2nd version of patch series to fix the ftrace rethook recursion problem.

v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/T/ +++#md4c0bae6a6cae28dadf2a2c6105ff140b35fddea

As Steven suggested, this version removes unnecessary notrace annotations from fprobe and rethook functions from v1 [PATCH 2,3,4/4] and replaces with makefile changes to filter out compiler flags which ftrace depends upon for rethook related objects.

Quoted 6 lines

Ze Gao (4):
rethook: use preempt_{disable, enable}_notrace in
rethook_trampoline_handler
fprobe: make fprobe_kprobe_handler recursion free
fprobe: add recursion detection in fprobe_exit_handler rehook, fprobe: do not trace rethook related functions

hi,
what tree is this based on? I have troubles to apply that

thanks,
jirka

Quoted 11 lines
 arch/riscv/kernel/probes/Makefile |  2 +
 arch/s390/kernel/Makefile         |  1 +
 arch/x86/kernel/Makefile          |  1 +
 kernel/trace/fprobe.c             | 72 ++++++++++++++++++++++++-------
 kernel/trace/rethook.c            |  4 +-
 5 files changed, 63 insertions(+), 17 deletions(-)
Signature · 1 line
2.40.1