bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] Make fprobe + rethook immune to recursion
@ 2023-05-16  7:18 Ze Gao
  2023-05-16  7:18 ` [PATCH v2 1/4] rethook: use preempt_{disable, enable}_notrace in rethook_trampoline_handler Ze Gao
                   ` (4 more replies)
  0 siblings, 5 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  7:18 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: Albert Ou, Alexander Gordeev, Alexei Starovoitov,
	Borislav Petkov, Christian Borntraeger, Dave Hansen,
	Heiko Carstens, H. Peter Anvin, Ingo Molnar, Palmer Dabbelt,
	Paul Walmsley, Sven Schnelle, Thomas Gleixner, Vasily Gorbik,
	x86, linux-kernel, linux-riscv, linux-s390, linux-trace-kernel,
	bpf, Conor Dooley, Jiri Olsa, Yonghong Song, 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/cover.1684120990.git.zegao@tencent.com/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(-)

-- 
2.40.1


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v2 1/4] rethook: use preempt_{disable, enable}_notrace in rethook_trampoline_handler
  2023-05-16  7:18 [PATCH v2 0/4] Make fprobe + rethook immune to recursion Ze Gao
@ 2023-05-16  7:18 ` Ze Gao
  2023-05-16  7:18 ` [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free Ze Gao
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  7:18 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: Albert Ou, Alexander Gordeev, Alexei Starovoitov,
	Borislav Petkov, Christian Borntraeger, Dave Hansen,
	Heiko Carstens, H. Peter Anvin, Ingo Molnar, Palmer Dabbelt,
	Paul Walmsley, Sven Schnelle, Thomas Gleixner, Vasily Gorbik,
	x86, linux-kernel, linux-riscv, linux-s390, linux-trace-kernel,
	bpf, Conor Dooley, Jiri Olsa, Yonghong Song, Ze Gao, stable

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")
Signed-off-by: Ze Gao <zegao@tencent.com>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: <stable@vger.kernel.org>
---
 kernel/trace/rethook.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
index 32c3dfdb4d6a..60f6cb2b486b 100644
--- a/kernel/trace/rethook.c
+++ b/kernel/trace/rethook.c
@@ -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;
 }
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  7:18 [PATCH v2 0/4] Make fprobe + rethook immune to recursion Ze Gao
  2023-05-16  7:18 ` [PATCH v2 1/4] rethook: use preempt_{disable, enable}_notrace in rethook_trampoline_handler Ze Gao
@ 2023-05-16  7:18 ` Ze Gao
  2023-05-16  9:15   ` Peter Zijlstra
  2023-05-16  9:18   ` Peter Zijlstra
  2023-05-16  7:18 ` [PATCH v2 3/4] fprobe: add recursion detection in fprobe_exit_handler Ze Gao
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  7:18 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: Albert Ou, Alexander Gordeev, Alexei Starovoitov,
	Borislav Petkov, Christian Borntraeger, Dave Hansen,
	Heiko Carstens, H. Peter Anvin, Ingo Molnar, Palmer Dabbelt,
	Paul Walmsley, Sven Schnelle, Thomas Gleixner, Vasily Gorbik,
	x86, linux-kernel, linux-riscv, linux-s390, linux-trace-kernel,
	bpf, Conor Dooley, Jiri Olsa, Yonghong Song, 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.

Signed-off-by: Ze Gao <zegao@tencent.com>
---
 kernel/trace/fprobe.c | 59 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 15 deletions(-)

diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c
index 9abb3905bc8e..097c740799ba 100644
--- a/kernel/trace/fprobe.c
+++ b/kernel/trace/fprobe.c
@@ -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,
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 3/4] fprobe: add recursion detection in fprobe_exit_handler
  2023-05-16  7:18 [PATCH v2 0/4] Make fprobe + rethook immune to recursion Ze Gao
  2023-05-16  7:18 ` [PATCH v2 1/4] rethook: use preempt_{disable, enable}_notrace in rethook_trampoline_handler Ze Gao
  2023-05-16  7:18 ` [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free Ze Gao
@ 2023-05-16  7:18 ` Ze Gao
  2023-05-16  7:18 ` [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions Ze Gao
  2023-05-16 21:42 ` [PATCH v2 0/4] Make fprobe + rethook immune to recursion Jiri Olsa
  4 siblings, 0 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  7:18 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: Albert Ou, Alexander Gordeev, Alexei Starovoitov,
	Borislav Petkov, Christian Borntraeger, Dave Hansen,
	Heiko Carstens, H. Peter Anvin, Ingo Molnar, Palmer Dabbelt,
	Paul Walmsley, Sven Schnelle, Thomas Gleixner, Vasily Gorbik,
	x86, linux-kernel, linux-riscv, linux-s390, linux-trace-kernel,
	bpf, Conor Dooley, Jiri Olsa, Yonghong Song, Ze Gao, stable

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")
Signed-off-by: Ze Gao <zegao@tencent.com>
Cc: stable@vger.kernel.org
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 kernel/trace/fprobe.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/kernel/trace/fprobe.c b/kernel/trace/fprobe.c
index 097c740799ba..a9580a88cc15 100644
--- a/kernel/trace/fprobe.c
+++ b/kernel/trace/fprobe.c
@@ -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);
 
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions
  2023-05-16  7:18 [PATCH v2 0/4] Make fprobe + rethook immune to recursion Ze Gao
                   ` (2 preceding siblings ...)
  2023-05-16  7:18 ` [PATCH v2 3/4] fprobe: add recursion detection in fprobe_exit_handler Ze Gao
@ 2023-05-16  7:18 ` Ze Gao
  2023-05-16 14:20   ` Steven Rostedt
  2023-05-16 16:05   ` Masami Hiramatsu
  2023-05-16 21:42 ` [PATCH v2 0/4] Make fprobe + rethook immune to recursion Jiri Olsa
  4 siblings, 2 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  7:18 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu
  Cc: Albert Ou, Alexander Gordeev, Alexei Starovoitov,
	Borislav Petkov, Christian Borntraeger, Dave Hansen,
	Heiko Carstens, H. Peter Anvin, Ingo Molnar, Palmer Dabbelt,
	Paul Walmsley, Sven Schnelle, Thomas Gleixner, Vasily Gorbik,
	x86, linux-kernel, linux-riscv, linux-s390, linux-trace-kernel,
	bpf, Conor Dooley, Jiri Olsa, Yonghong Song, 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.

Signed-off-by: Ze Gao <zegao@tencent.com>
---
 arch/riscv/kernel/probes/Makefile | 2 ++
 arch/s390/kernel/Makefile         | 1 +
 arch/x86/kernel/Makefile          | 1 +
 3 files changed, 4 insertions(+)

diff --git a/arch/riscv/kernel/probes/Makefile b/arch/riscv/kernel/probes/Makefile
index c40139e9ca47..8265ff497977 100644
--- a/arch/riscv/kernel/probes/Makefile
+++ b/arch/riscv/kernel/probes/Makefile
@@ -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)
diff --git a/arch/s390/kernel/Makefile b/arch/s390/kernel/Makefile
index 8983837b3565..6b2a051e1f8a 100644
--- a/arch/s390/kernel/Makefile
+++ b/arch/s390/kernel/Makefile
@@ -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
 
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index dd61752f4c96..4070a01c11b7 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -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
-- 
2.40.1


^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  7:18 ` [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free Ze Gao
@ 2023-05-16  9:15   ` Peter Zijlstra
  2023-05-16  9:35     ` Ze Gao
  2023-05-16  9:18   ` Peter Zijlstra
  1 sibling, 1 reply; 21+ messages in thread
From: Peter Zijlstra @ 2023-05-16  9:15 UTC (permalink / raw)
  To: Ze Gao
  Cc: Steven Rostedt, Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:

> +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;
> +	}

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

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

Same for your next patch.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  7:18 ` [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free Ze Gao
  2023-05-16  9:15   ` Peter Zijlstra
@ 2023-05-16  9:18   ` Peter Zijlstra
  2023-05-16  9:47     ` Ze Gao
  1 sibling, 1 reply; 21+ messages in thread
From: Peter Zijlstra @ 2023-05-16  9:18 UTC (permalink / raw)
  To: Ze Gao
  Cc: Steven Rostedt, Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> 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?

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  9:15   ` Peter Zijlstra
@ 2023-05-16  9:35     ` Ze Gao
  0 siblings, 0 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  9:35 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

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

Regards,
Ze

On Tue, May 16, 2023 at 5:16 PM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
>
> > +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;
> > +     }
>
> Please don't use this comment style; multi line comments go like:
>
>         /*
>          * Multi line comment ...
>          *                    ... is symmetric.
>          */
>
> Same for your next patch.

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  9:18   ` Peter Zijlstra
@ 2023-05-16  9:47     ` Ze Gao
  2023-05-16  9:51       ` Ze Gao
  2023-05-16 16:03       ` Masami Hiramatsu
  0 siblings, 2 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  9:47 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	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/20230516132516.c902edcf21028874a74fb868@kernel.org/

Regards,
Ze

On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> > 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?

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  9:47     ` Ze Gao
@ 2023-05-16  9:51       ` Ze Gao
  2023-05-16 16:03       ` Masami Hiramatsu
  1 sibling, 0 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-16  9:51 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Steven Rostedt, Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

Sorry for paste the wrong link, it's this one instead:
  Link: https://lore.kernel.org/bpf/20230513001757.75ae0d1b@rorschach.local.home/

It's the original discussions of this problem.

Regards,
Ze

On Tue, May 16, 2023 at 5:47 PM Ze Gao <zegao2021@gmail.com> wrote:
>
> 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/20230516132516.c902edcf21028874a74fb868@kernel.org/
>
> Regards,
> Ze
>
> On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> > > 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?

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions
  2023-05-16  7:18 ` [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions Ze Gao
@ 2023-05-16 14:20   ` Steven Rostedt
  2023-05-17  2:00     ` Ze Gao
  2023-05-16 16:05   ` Masami Hiramatsu
  1 sibling, 1 reply; 21+ messages in thread
From: Steven Rostedt @ 2023-05-16 14:20 UTC (permalink / raw)
  To: Ze Gao
  Cc: Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

On Tue, 16 May 2023 15:18:30 +0800
Ze Gao <zegao2021@gmail.com> wrote:

>  CFLAGS_REMOVE_early.o		= $(CC_FLAGS_FTRACE)
> +CFLAGS_REMOVE_rethook.o		= $(CC_FLAGS_FTRACE)
>  
>  endif
>  
> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> index dd61752f4c96..4070a01c11b7 100644
> --- a/arch/x86/kernel/Makefile
> +++ b/arch/x86/kernel/Makefile
> @@ -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


>  endif
>  
>  KASAN_SANITIZE_head$(BITS).o				:= n
> -- 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16  9:47     ` Ze Gao
  2023-05-16  9:51       ` Ze Gao
@ 2023-05-16 16:03       ` Masami Hiramatsu
  2023-05-17  1:54         ` Ze Gao
  1 sibling, 1 reply; 21+ messages in thread
From: Masami Hiramatsu @ 2023-05-16 16:03 UTC (permalink / raw)
  To: Ze Gao
  Cc: Peter Zijlstra, Steven Rostedt, Masami Hiramatsu, Albert Ou,
	Alexander Gordeev, Alexei Starovoitov, Borislav Petkov,
	Christian Borntraeger, Dave Hansen, Heiko Carstens,
	H. Peter Anvin, Ingo Molnar, Palmer Dabbelt, Paul Walmsley,
	Sven Schnelle, Thomas Gleixner, Vasily Gorbik, x86, linux-kernel,
	linux-riscv, linux-s390, linux-trace-kernel, bpf, Conor Dooley,
	Jiri Olsa, Yonghong Song, Ze Gao

On Tue, 16 May 2023 17:47:52 +0800
Ze Gao <zegao2021@gmail.com> 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,

> I will refine the commit message to make it clear.
> 
> FYI, details can checked out here:
>     Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/
> 
> Regards,
> Ze
> 
> On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> > > 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) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions
  2023-05-16  7:18 ` [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions Ze Gao
  2023-05-16 14:20   ` Steven Rostedt
@ 2023-05-16 16:05   ` Masami Hiramatsu
  1 sibling, 0 replies; 21+ messages in thread
From: Masami Hiramatsu @ 2023-05-16 16:05 UTC (permalink / raw)
  To: Ze Gao
  Cc: Steven Rostedt, Albert Ou, Alexander Gordeev, Alexei Starovoitov,
	Borislav Petkov, Christian Borntraeger, Dave Hansen,
	Heiko Carstens, H. Peter Anvin, Ingo Molnar, Palmer Dabbelt,
	Paul Walmsley, Sven Schnelle, Thomas Gleixner, Vasily Gorbik,
	x86, linux-kernel, linux-riscv, linux-s390, linux-trace-kernel,
	bpf, Conor Dooley, Jiri Olsa, Yonghong Song, Ze Gao

On Tue, 16 May 2023 15:18:30 +0800
Ze Gao <zegao2021@gmail.com> 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.
> 
> Signed-off-by: Ze Gao <zegao@tencent.com>

Looks good to me.

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thank you!

> ---
>  arch/riscv/kernel/probes/Makefile | 2 ++
>  arch/s390/kernel/Makefile         | 1 +
>  arch/x86/kernel/Makefile          | 1 +
>  3 files changed, 4 insertions(+)
> 
> diff --git a/arch/riscv/kernel/probes/Makefile b/arch/riscv/kernel/probes/Makefile
> index c40139e9ca47..8265ff497977 100644
> --- a/arch/riscv/kernel/probes/Makefile
> +++ b/arch/riscv/kernel/probes/Makefile
> @@ -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)
> diff --git a/arch/s390/kernel/Makefile b/arch/s390/kernel/Makefile
> index 8983837b3565..6b2a051e1f8a 100644
> --- a/arch/s390/kernel/Makefile
> +++ b/arch/s390/kernel/Makefile
> @@ -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
>  
> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> index dd61752f4c96..4070a01c11b7 100644
> --- a/arch/x86/kernel/Makefile
> +++ b/arch/x86/kernel/Makefile
> @@ -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
> -- 
> 2.40.1
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 0/4] Make fprobe + rethook immune to recursion
  2023-05-16  7:18 [PATCH v2 0/4] Make fprobe + rethook immune to recursion Ze Gao
                   ` (3 preceding siblings ...)
  2023-05-16  7:18 ` [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions Ze Gao
@ 2023-05-16 21:42 ` Jiri Olsa
  4 siblings, 0 replies; 21+ messages in thread
From: Jiri Olsa @ 2023-05-16 21:42 UTC (permalink / raw)
  To: Ze Gao
  Cc: Steven Rostedt, Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Yonghong Song, Ze Gao

On Tue, May 16, 2023 at 03:18:26PM +0800, Ze Gao wrote:
> 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/cover.1684120990.git.zegao@tencent.com/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

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

thanks,
jirka

> 
>  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(-)
> 
> -- 
> 2.40.1
> 

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-16 16:03       ` Masami Hiramatsu
@ 2023-05-17  1:54         ` Ze Gao
  2023-05-17  2:54           ` Masami Hiramatsu
  0 siblings, 1 reply; 21+ messages in thread
From: Ze Gao @ 2023-05-17  1:54 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Peter Zijlstra, Steven Rostedt, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

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

On Wed, May 17, 2023 at 12:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> On Tue, 16 May 2023 17:47:52 +0800
> Ze Gao <zegao2021@gmail.com> 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,
>
> > I will refine the commit message to make it clear.
> >
> > FYI, details can checked out here:
> >     Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/
> >
> > Regards,
> > Ze
> >
> > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote:
> > >
> > > On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> > > > 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) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions
  2023-05-16 14:20   ` Steven Rostedt
@ 2023-05-17  2:00     ` Ze Gao
  0 siblings, 0 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-17  2:00 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Masami Hiramatsu, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

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

Regards,
Ze

On Tue, May 16, 2023 at 10:20 PM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Tue, 16 May 2023 15:18:30 +0800
> Ze Gao <zegao2021@gmail.com> wrote:
>
> >  CFLAGS_REMOVE_early.o                = $(CC_FLAGS_FTRACE)
> > +CFLAGS_REMOVE_rethook.o              = $(CC_FLAGS_FTRACE)
> >
> >  endif
> >
> > diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> > index dd61752f4c96..4070a01c11b7 100644
> > --- a/arch/x86/kernel/Makefile
> > +++ b/arch/x86/kernel/Makefile
> > @@ -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
>
>
> >  endif
> >
> >  KASAN_SANITIZE_head$(BITS).o                         := n
> > --

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-17  1:54         ` Ze Gao
@ 2023-05-17  2:54           ` Masami Hiramatsu
  2023-05-17  3:10             ` Ze Gao
  0 siblings, 1 reply; 21+ messages in thread
From: Masami Hiramatsu @ 2023-05-17  2:54 UTC (permalink / raw)
  To: Ze Gao
  Cc: Peter Zijlstra, Steven Rostedt, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

On Wed, 17 May 2023 09:54:53 +0800
Ze Gao <zegao2021@gmail.com> 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.

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

Thank you,

> 
> Thanks for your review.
> 
> I'll rebase onto the latest tree and send v3 ASAP.
> 
> Regards,
> Ze
> 
> On Wed, May 17, 2023 at 12:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote:
> >
> > On Tue, 16 May 2023 17:47:52 +0800
> > Ze Gao <zegao2021@gmail.com> 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,
> >
> > > I will refine the commit message to make it clear.
> > >
> > > FYI, details can checked out here:
> > >     Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/
> > >
> > > Regards,
> > > Ze
> > >
> > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote:
> > > >
> > > > On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> > > > > 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) <mhiramat@kernel.org>


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-17  2:54           ` Masami Hiramatsu
@ 2023-05-17  3:10             ` Ze Gao
  2023-05-17  3:25               ` Steven Rostedt
  0 siblings, 1 reply; 21+ messages in thread
From: Ze Gao @ 2023-05-17  3:10 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Peter Zijlstra, Steven Rostedt, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

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

On Wed, May 17, 2023 at 10:54 AM Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> On Wed, 17 May 2023 09:54:53 +0800
> Ze Gao <zegao2021@gmail.com> 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.
>
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
>
> If you add Steve's call graph for the explanation, it will help us to
> understand what will be fixed.
>
> Thank you,
>
> >
> > Thanks for your review.
> >
> > I'll rebase onto the latest tree and send v3 ASAP.
> >
> > Regards,
> > Ze
> >
> > On Wed, May 17, 2023 at 12:03 AM Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > >
> > > On Tue, 16 May 2023 17:47:52 +0800
> > > Ze Gao <zegao2021@gmail.com> 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,
> > >
> > > > I will refine the commit message to make it clear.
> > > >
> > > > FYI, details can checked out here:
> > > >     Link: https://lore.kernel.org/linux-trace-kernel/20230516132516.c902edcf21028874a74fb868@kernel.org/
> > > >
> > > > Regards,
> > > > Ze
> > > >
> > > > On Tue, May 16, 2023 at 5:18 PM Peter Zijlstra <peterz@infradead.org> wrote:
> > > > >
> > > > > On Tue, May 16, 2023 at 03:18:28PM +0800, Ze Gao wrote:
> > > > > > 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) <mhiramat@kernel.org>
>
>
> --
> Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-17  3:10             ` Ze Gao
@ 2023-05-17  3:25               ` Steven Rostedt
  2023-05-17  3:51                 ` Masami Hiramatsu
  2023-05-17  4:51                 ` Ze Gao
  0 siblings, 2 replies; 21+ messages in thread
From: Steven Rostedt @ 2023-05-17  3:25 UTC (permalink / raw)
  To: Ze Gao
  Cc: Masami Hiramatsu, Peter Zijlstra, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Jiri Olsa, Yonghong Song,
	Ze Gao

On Wed, 17 May 2023 11:10:21 +0800
Ze Gao <zegao2021@gmail.com> 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-17  3:25               ` Steven Rostedt
@ 2023-05-17  3:51                 ` Masami Hiramatsu
  2023-05-17  4:51                 ` Ze Gao
  1 sibling, 0 replies; 21+ messages in thread
From: Masami Hiramatsu @ 2023-05-17  3:51 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Ze Gao, Masami Hiramatsu, Peter Zijlstra, Albert Ou,
	Alexander Gordeev, Alexei Starovoitov, Borislav Petkov,
	Christian Borntraeger, Dave Hansen, Heiko Carstens,
	H. Peter Anvin, Ingo Molnar, Palmer Dabbelt, Paul Walmsley,
	Sven Schnelle, Thomas Gleixner, Vasily Gorbik, x86, linux-kernel,
	linux-riscv, linux-s390, linux-trace-kernel, bpf, Conor Dooley,
	Jiri Olsa, Yonghong Song, Ze Gao

On Tue, 16 May 2023 23:25:45 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Wed, 17 May 2023 11:10:21 +0800
> Ze Gao <zegao2021@gmail.com> 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.

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

Thank you,


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free
  2023-05-17  3:25               ` Steven Rostedt
  2023-05-17  3:51                 ` Masami Hiramatsu
@ 2023-05-17  4:51                 ` Ze Gao
  1 sibling, 0 replies; 21+ messages in thread
From: Ze Gao @ 2023-05-17  4:51 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Masami Hiramatsu, Peter Zijlstra, Albert Ou, Alexander Gordeev,
	Alexei Starovoitov, Borislav Petkov, Christian Borntraeger,
	Dave Hansen, Heiko Carstens, H. Peter Anvin, Ingo Molnar,
	Palmer Dabbelt, Paul Walmsley, Sven Schnelle, Thomas Gleixner,
	Vasily Gorbik, x86, linux-kernel, linux-riscv, linux-s390,
	linux-trace-kernel, bpf, Conor Dooley, Yonghong Song, Ze Gao,
	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/20230517034510.15639-1-zegao@tencent.com/T/#t

Regards,
Ze

On Wed, May 17, 2023 at 11:25 AM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Wed, 17 May 2023 11:10:21 +0800
> Ze Gao <zegao2021@gmail.com> 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

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2023-05-17  4:52 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-16  7:18 [PATCH v2 0/4] Make fprobe + rethook immune to recursion Ze Gao
2023-05-16  7:18 ` [PATCH v2 1/4] rethook: use preempt_{disable, enable}_notrace in rethook_trampoline_handler Ze Gao
2023-05-16  7:18 ` [PATCH v2 2/4] fprobe: make fprobe_kprobe_handler recursion free Ze Gao
2023-05-16  9:15   ` Peter Zijlstra
2023-05-16  9:35     ` Ze Gao
2023-05-16  9:18   ` Peter Zijlstra
2023-05-16  9:47     ` Ze Gao
2023-05-16  9:51       ` Ze Gao
2023-05-16 16:03       ` Masami Hiramatsu
2023-05-17  1:54         ` Ze Gao
2023-05-17  2:54           ` Masami Hiramatsu
2023-05-17  3:10             ` Ze Gao
2023-05-17  3:25               ` Steven Rostedt
2023-05-17  3:51                 ` Masami Hiramatsu
2023-05-17  4:51                 ` Ze Gao
2023-05-16  7:18 ` [PATCH v2 3/4] fprobe: add recursion detection in fprobe_exit_handler Ze Gao
2023-05-16  7:18 ` [PATCH v2 4/4] rehook, fprobe: do not trace rethook related functions Ze Gao
2023-05-16 14:20   ` Steven Rostedt
2023-05-17  2:00     ` Ze Gao
2023-05-16 16:05   ` Masami Hiramatsu
2023-05-16 21:42 ` [PATCH v2 0/4] Make fprobe + rethook immune to recursion Jiri Olsa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).