All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rethook: Reject getting a rethook if RCU is not watching
@ 2022-05-07  4:46 Masami Hiramatsu
  2022-05-24 23:23 ` Steven Rostedt
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2022-05-07  4:46 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
  Cc: Masami Hiramatsu, Jiri Olsa, Steven Rostedt, linux-kernel, netdev, bpf

Since the rethook_recycle() will involve the call_rcu() for reclaiming
the rethook_instance, the rethook must be set up at the RCU available
context (non idle). This rethook_recycle() in the rethook trampoline
handler is inevitable, thus the RCU available check must be done before
setting the rethook trampoline.

This adds a rcu_is_watching() check in the rethook_try_get() so that
it will return NULL if it is called when !rcu_is_watching().

Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
index b56833700d23..c69d82273ce7 100644
--- a/kernel/trace/rethook.c
+++ b/kernel/trace/rethook.c
@@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
 	if (unlikely(!handler))
 		return NULL;
 
+	/*
+	 * This expects the caller will set up a rethook on a function entry.
+	 * When the function returns, the rethook will eventually be reclaimed
+	 * or released in the rethook_recycle() with call_rcu().
+	 * This means the caller must be run in the RCU-availabe context.
+	 */
+	if (unlikely(!rcu_is_watching()))
+		return NULL;
+
 	fn = freelist_try_get(&rh->pool);
 	if (!fn)
 		return NULL;


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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-07  4:46 [PATCH] rethook: Reject getting a rethook if RCU is not watching Masami Hiramatsu
@ 2022-05-24 23:23 ` Steven Rostedt
  2022-05-26 14:25   ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Steven Rostedt @ 2022-05-24 23:23 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Jiri Olsa,
	linux-kernel, netdev, bpf

On Sat,  7 May 2022 13:46:52 +0900
Masami Hiramatsu <mhiramat@kernel.org> wrote:

Is this expected to go through the BPF tree?

-- Steve


> Since the rethook_recycle() will involve the call_rcu() for reclaiming
> the rethook_instance, the rethook must be set up at the RCU available
> context (non idle). This rethook_recycle() in the rethook trampoline
> handler is inevitable, thus the RCU available check must be done before
> setting the rethook trampoline.
> 
> This adds a rcu_is_watching() check in the rethook_try_get() so that
> it will return NULL if it is called when !rcu_is_watching().
> 
> Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> ---
>  kernel/trace/rethook.c |    9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> index b56833700d23..c69d82273ce7 100644
> --- a/kernel/trace/rethook.c
> +++ b/kernel/trace/rethook.c
> @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
>  	if (unlikely(!handler))
>  		return NULL;
>  
> +	/*
> +	 * This expects the caller will set up a rethook on a function entry.
> +	 * When the function returns, the rethook will eventually be reclaimed
> +	 * or released in the rethook_recycle() with call_rcu().
> +	 * This means the caller must be run in the RCU-availabe context.
> +	 */
> +	if (unlikely(!rcu_is_watching()))
> +		return NULL;
> +
>  	fn = freelist_try_get(&rh->pool);
>  	if (!fn)
>  		return NULL;


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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-24 23:23 ` Steven Rostedt
@ 2022-05-26 14:25   ` Masami Hiramatsu
  2022-05-26 14:49     ` Jiri Olsa
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2022-05-26 14:25 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Jiri Olsa,
	linux-kernel, netdev, bpf

On Tue, 24 May 2022 19:23:01 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Sat,  7 May 2022 13:46:52 +0900
> Masami Hiramatsu <mhiramat@kernel.org> wrote:
> 
> Is this expected to go through the BPF tree?
> 

Yes, since rethook (fprobe) is currently used only from eBPF.
Jiri, can you check this is good for your test case?

Thank you,


> -- Steve
> 
> 
> > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > the rethook_instance, the rethook must be set up at the RCU available
> > context (non idle). This rethook_recycle() in the rethook trampoline
> > handler is inevitable, thus the RCU available check must be done before
> > setting the rethook trampoline.
> > 
> > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > it will return NULL if it is called when !rcu_is_watching().
> > 
> > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > ---
> >  kernel/trace/rethook.c |    9 +++++++++
> >  1 file changed, 9 insertions(+)
> > 
> > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > index b56833700d23..c69d82273ce7 100644
> > --- a/kernel/trace/rethook.c
> > +++ b/kernel/trace/rethook.c
> > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> >  	if (unlikely(!handler))
> >  		return NULL;
> >  
> > +	/*
> > +	 * This expects the caller will set up a rethook on a function entry.
> > +	 * When the function returns, the rethook will eventually be reclaimed
> > +	 * or released in the rethook_recycle() with call_rcu().
> > +	 * This means the caller must be run in the RCU-availabe context.
> > +	 */
> > +	if (unlikely(!rcu_is_watching()))
> > +		return NULL;
> > +
> >  	fn = freelist_try_get(&rh->pool);
> >  	if (!fn)
> >  		return NULL;
> 


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

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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-26 14:25   ` Masami Hiramatsu
@ 2022-05-26 14:49     ` Jiri Olsa
  2022-05-26 16:14       ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Jiri Olsa @ 2022-05-26 14:49 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Steven Rostedt, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-kernel, netdev, bpf, Paul E. McKenney

On Thu, May 26, 2022 at 11:25:30PM +0900, Masami Hiramatsu wrote:
> On Tue, 24 May 2022 19:23:01 -0400
> Steven Rostedt <rostedt@goodmis.org> wrote:
> 
> > On Sat,  7 May 2022 13:46:52 +0900
> > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > 
> > Is this expected to go through the BPF tree?
> > 
> 
> Yes, since rethook (fprobe) is currently used only from eBPF.
> Jiri, can you check this is good for your test case?

sure I'll test it.. can't see the original email,
perhaps I wasn't cc-ed.. but I'll find it

is this also related to tracing 'idle' functions,
as discussed in here?
  https://lore.kernel.org/bpf/20220515203653.4039075-1-jolsa@kernel.org/

because that's the one I can reproduce.. but I can
certainly try that with your change as well

jirka

> 
> Thank you,
> 
> 
> > -- Steve
> > 
> > 
> > > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > > the rethook_instance, the rethook must be set up at the RCU available
> > > context (non idle). This rethook_recycle() in the rethook trampoline
> > > handler is inevitable, thus the RCU available check must be done before
> > > setting the rethook trampoline.
> > > 
> > > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > > it will return NULL if it is called when !rcu_is_watching().
> > > 
> > > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > > ---
> > >  kernel/trace/rethook.c |    9 +++++++++
> > >  1 file changed, 9 insertions(+)
> > > 
> > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > > index b56833700d23..c69d82273ce7 100644
> > > --- a/kernel/trace/rethook.c
> > > +++ b/kernel/trace/rethook.c
> > > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> > >  	if (unlikely(!handler))
> > >  		return NULL;
> > >  
> > > +	/*
> > > +	 * This expects the caller will set up a rethook on a function entry.
> > > +	 * When the function returns, the rethook will eventually be reclaimed
> > > +	 * or released in the rethook_recycle() with call_rcu().
> > > +	 * This means the caller must be run in the RCU-availabe context.
> > > +	 */
> > > +	if (unlikely(!rcu_is_watching()))
> > > +		return NULL;
> > > +
> > >  	fn = freelist_try_get(&rh->pool);
> > >  	if (!fn)
> > >  		return NULL;
> > 
> 
> 
> -- 
> Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-26 14:49     ` Jiri Olsa
@ 2022-05-26 16:14       ` Masami Hiramatsu
  2022-05-27 22:10         ` Jiri Olsa
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2022-05-26 16:14 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Steven Rostedt, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-kernel, netdev, bpf, Paul E. McKenney

On Thu, 26 May 2022 16:49:26 +0200
Jiri Olsa <olsajiri@gmail.com> wrote:

> On Thu, May 26, 2022 at 11:25:30PM +0900, Masami Hiramatsu wrote:
> > On Tue, 24 May 2022 19:23:01 -0400
> > Steven Rostedt <rostedt@goodmis.org> wrote:
> > 
> > > On Sat,  7 May 2022 13:46:52 +0900
> > > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > > 
> > > Is this expected to go through the BPF tree?
> > > 
> > 
> > Yes, since rethook (fprobe) is currently used only from eBPF.
> > Jiri, can you check this is good for your test case?
> 
> sure I'll test it.. can't see the original email,
> perhaps I wasn't cc-ed.. but I'll find it

Here it is. I Cc-ed your @kernel.org address.
https://lore.kernel.org/all/165189881197.175864.14757002789194211860.stgit@devnote2/T/#u

> 
> is this also related to tracing 'idle' functions,
> as discussed in here?
>   https://lore.kernel.org/bpf/20220515203653.4039075-1-jolsa@kernel.org/

Ah, yes. So this may not happen with the above patch, but for the
hardening (ensuring it is always safe), I would like to add this.

> 
> because that's the one I can reproduce.. but I can
> certainly try that with your change as well

Thank you!

> 
> jirka
> 
> > 
> > Thank you,
> > 
> > 
> > > -- Steve
> > > 
> > > 
> > > > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > > > the rethook_instance, the rethook must be set up at the RCU available
> > > > context (non idle). This rethook_recycle() in the rethook trampoline
> > > > handler is inevitable, thus the RCU available check must be done before
> > > > setting the rethook trampoline.
> > > > 
> > > > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > > > it will return NULL if it is called when !rcu_is_watching().
> > > > 
> > > > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > > > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > > > ---
> > > >  kernel/trace/rethook.c |    9 +++++++++
> > > >  1 file changed, 9 insertions(+)
> > > > 
> > > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > > > index b56833700d23..c69d82273ce7 100644
> > > > --- a/kernel/trace/rethook.c
> > > > +++ b/kernel/trace/rethook.c
> > > > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> > > >  	if (unlikely(!handler))
> > > >  		return NULL;
> > > >  
> > > > +	/*
> > > > +	 * This expects the caller will set up a rethook on a function entry.
> > > > +	 * When the function returns, the rethook will eventually be reclaimed
> > > > +	 * or released in the rethook_recycle() with call_rcu().
> > > > +	 * This means the caller must be run in the RCU-availabe context.
> > > > +	 */
> > > > +	if (unlikely(!rcu_is_watching()))
> > > > +		return NULL;
> > > > +
> > > >  	fn = freelist_try_get(&rh->pool);
> > > >  	if (!fn)
> > > >  		return NULL;
> > > 
> > 
> > 
> > -- 
> > Masami Hiramatsu (Google) <mhiramat@kernel.org>


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

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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-26 16:14       ` Masami Hiramatsu
@ 2022-05-27 22:10         ` Jiri Olsa
  2022-05-28  1:19           ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Jiri Olsa @ 2022-05-27 22:10 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Jiri Olsa, Steven Rostedt, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-kernel, netdev, bpf, Paul E. McKenney

On Fri, May 27, 2022 at 01:14:34AM +0900, Masami Hiramatsu wrote:
> On Thu, 26 May 2022 16:49:26 +0200
> Jiri Olsa <olsajiri@gmail.com> wrote:
> 
> > On Thu, May 26, 2022 at 11:25:30PM +0900, Masami Hiramatsu wrote:
> > > On Tue, 24 May 2022 19:23:01 -0400
> > > Steven Rostedt <rostedt@goodmis.org> wrote:
> > > 
> > > > On Sat,  7 May 2022 13:46:52 +0900
> > > > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > > > 
> > > > Is this expected to go through the BPF tree?
> > > > 
> > > 
> > > Yes, since rethook (fprobe) is currently used only from eBPF.
> > > Jiri, can you check this is good for your test case?
> > 
> > sure I'll test it.. can't see the original email,
> > perhaps I wasn't cc-ed.. but I'll find it
> 
> Here it is. I Cc-ed your @kernel.org address.
> https://lore.kernel.org/all/165189881197.175864.14757002789194211860.stgit@devnote2/T/#u
> 
> > 
> > is this also related to tracing 'idle' functions,
> > as discussed in here?
> >   https://lore.kernel.org/bpf/20220515203653.4039075-1-jolsa@kernel.org/
> 
> Ah, yes. So this may not happen with the above patch, but for the
> hardening (ensuring it is always safe), I would like to add this.
> 
> > 
> > because that's the one I can reproduce.. but I can
> > certainly try that with your change as well
> 
> Thank you!

it did not help the idle warning as expected, but I did not
see any problems running bpf tests on top of this

jirka

> 
> > 
> > jirka
> > 
> > > 
> > > Thank you,
> > > 
> > > 
> > > > -- Steve
> > > > 
> > > > 
> > > > > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > > > > the rethook_instance, the rethook must be set up at the RCU available
> > > > > context (non idle). This rethook_recycle() in the rethook trampoline
> > > > > handler is inevitable, thus the RCU available check must be done before
> > > > > setting the rethook trampoline.
> > > > > 
> > > > > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > > > > it will return NULL if it is called when !rcu_is_watching().
> > > > > 
> > > > > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > > > > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > > > > ---
> > > > >  kernel/trace/rethook.c |    9 +++++++++
> > > > >  1 file changed, 9 insertions(+)
> > > > > 
> > > > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > > > > index b56833700d23..c69d82273ce7 100644
> > > > > --- a/kernel/trace/rethook.c
> > > > > +++ b/kernel/trace/rethook.c
> > > > > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> > > > >  	if (unlikely(!handler))
> > > > >  		return NULL;
> > > > >  
> > > > > +	/*
> > > > > +	 * This expects the caller will set up a rethook on a function entry.
> > > > > +	 * When the function returns, the rethook will eventually be reclaimed
> > > > > +	 * or released in the rethook_recycle() with call_rcu().
> > > > > +	 * This means the caller must be run in the RCU-availabe context.
> > > > > +	 */
> > > > > +	if (unlikely(!rcu_is_watching()))
> > > > > +		return NULL;
> > > > > +
> > > > >  	fn = freelist_try_get(&rh->pool);
> > > > >  	if (!fn)
> > > > >  		return NULL;
> > > > 
> > > 
> > > 
> > > -- 
> > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
> 
> 
> -- 
> Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-27 22:10         ` Jiri Olsa
@ 2022-05-28  1:19           ` Masami Hiramatsu
  2022-06-03 19:21             ` Andrii Nakryiko
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2022-05-28  1:19 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Steven Rostedt, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, linux-kernel, netdev, bpf, Paul E. McKenney

On Sat, 28 May 2022 00:10:08 +0200
Jiri Olsa <olsajiri@gmail.com> wrote:

> On Fri, May 27, 2022 at 01:14:34AM +0900, Masami Hiramatsu wrote:
> > On Thu, 26 May 2022 16:49:26 +0200
> > Jiri Olsa <olsajiri@gmail.com> wrote:
> > 
> > > On Thu, May 26, 2022 at 11:25:30PM +0900, Masami Hiramatsu wrote:
> > > > On Tue, 24 May 2022 19:23:01 -0400
> > > > Steven Rostedt <rostedt@goodmis.org> wrote:
> > > > 
> > > > > On Sat,  7 May 2022 13:46:52 +0900
> > > > > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > > > > 
> > > > > Is this expected to go through the BPF tree?
> > > > > 
> > > > 
> > > > Yes, since rethook (fprobe) is currently used only from eBPF.
> > > > Jiri, can you check this is good for your test case?
> > > 
> > > sure I'll test it.. can't see the original email,
> > > perhaps I wasn't cc-ed.. but I'll find it
> > 
> > Here it is. I Cc-ed your @kernel.org address.
> > https://lore.kernel.org/all/165189881197.175864.14757002789194211860.stgit@devnote2/T/#u
> > 
> > > 
> > > is this also related to tracing 'idle' functions,
> > > as discussed in here?
> > >   https://lore.kernel.org/bpf/20220515203653.4039075-1-jolsa@kernel.org/
> > 
> > Ah, yes. So this may not happen with the above patch, but for the
> > hardening (ensuring it is always safe), I would like to add this.
> > 
> > > 
> > > because that's the one I can reproduce.. but I can
> > > certainly try that with your change as well
> > 
> > Thank you!
> 
> it did not help the idle warning as expected, but I did not
> see any problems running bpf tests on top of this

Oops, right. I forgot this is only for the rethook, not protect the
fprobe handlers, since fprobe code doesn't involve the RCU code (it
depends on ftrace's check). Sorry about that.
Hmm, I need to add a test code for this issue, but that could be
solved by your noninstr patch.

Thank you,

> 
> jirka
> 
> > 
> > > 
> > > jirka
> > > 
> > > > 
> > > > Thank you,
> > > > 
> > > > 
> > > > > -- Steve
> > > > > 
> > > > > 
> > > > > > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > > > > > the rethook_instance, the rethook must be set up at the RCU available
> > > > > > context (non idle). This rethook_recycle() in the rethook trampoline
> > > > > > handler is inevitable, thus the RCU available check must be done before
> > > > > > setting the rethook trampoline.
> > > > > > 
> > > > > > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > > > > > it will return NULL if it is called when !rcu_is_watching().
> > > > > > 
> > > > > > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > > > > > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > > > > > ---
> > > > > >  kernel/trace/rethook.c |    9 +++++++++
> > > > > >  1 file changed, 9 insertions(+)
> > > > > > 
> > > > > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > > > > > index b56833700d23..c69d82273ce7 100644
> > > > > > --- a/kernel/trace/rethook.c
> > > > > > +++ b/kernel/trace/rethook.c
> > > > > > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> > > > > >  	if (unlikely(!handler))
> > > > > >  		return NULL;
> > > > > >  
> > > > > > +	/*
> > > > > > +	 * This expects the caller will set up a rethook on a function entry.
> > > > > > +	 * When the function returns, the rethook will eventually be reclaimed
> > > > > > +	 * or released in the rethook_recycle() with call_rcu().
> > > > > > +	 * This means the caller must be run in the RCU-availabe context.
> > > > > > +	 */
> > > > > > +	if (unlikely(!rcu_is_watching()))
> > > > > > +		return NULL;
> > > > > > +
> > > > > >  	fn = freelist_try_get(&rh->pool);
> > > > > >  	if (!fn)
> > > > > >  		return NULL;
> > > > > 
> > > > 
> > > > 
> > > > -- 
> > > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > 
> > 
> > -- 
> > Masami Hiramatsu (Google) <mhiramat@kernel.org>


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

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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-05-28  1:19           ` Masami Hiramatsu
@ 2022-06-03 19:21             ` Andrii Nakryiko
  2022-06-06 16:02               ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Andrii Nakryiko @ 2022-06-03 19:21 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Jiri Olsa, Steven Rostedt, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, open list, Networking, bpf, Paul E. McKenney

On Fri, May 27, 2022 at 6:19 PM Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> On Sat, 28 May 2022 00:10:08 +0200
> Jiri Olsa <olsajiri@gmail.com> wrote:
>
> > On Fri, May 27, 2022 at 01:14:34AM +0900, Masami Hiramatsu wrote:
> > > On Thu, 26 May 2022 16:49:26 +0200
> > > Jiri Olsa <olsajiri@gmail.com> wrote:
> > >
> > > > On Thu, May 26, 2022 at 11:25:30PM +0900, Masami Hiramatsu wrote:
> > > > > On Tue, 24 May 2022 19:23:01 -0400
> > > > > Steven Rostedt <rostedt@goodmis.org> wrote:
> > > > >
> > > > > > On Sat,  7 May 2022 13:46:52 +0900
> > > > > > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > > > > >
> > > > > > Is this expected to go through the BPF tree?
> > > > > >
> > > > >
> > > > > Yes, since rethook (fprobe) is currently used only from eBPF.
> > > > > Jiri, can you check this is good for your test case?
> > > >
> > > > sure I'll test it.. can't see the original email,
> > > > perhaps I wasn't cc-ed.. but I'll find it
> > >
> > > Here it is. I Cc-ed your @kernel.org address.
> > > https://lore.kernel.org/all/165189881197.175864.14757002789194211860.stgit@devnote2/T/#u
> > >
> > > >
> > > > is this also related to tracing 'idle' functions,
> > > > as discussed in here?
> > > >   https://lore.kernel.org/bpf/20220515203653.4039075-1-jolsa@kernel.org/
> > >
> > > Ah, yes. So this may not happen with the above patch, but for the
> > > hardening (ensuring it is always safe), I would like to add this.
> > >
> > > >
> > > > because that's the one I can reproduce.. but I can
> > > > certainly try that with your change as well
> > >
> > > Thank you!
> >
> > it did not help the idle warning as expected, but I did not
> > see any problems running bpf tests on top of this
>
> Oops, right. I forgot this is only for the rethook, not protect the
> fprobe handlers, since fprobe code doesn't involve the RCU code (it
> depends on ftrace's check). Sorry about that.
> Hmm, I need to add a test code for this issue, but that could be
> solved by your noninstr patch.
>


Masami,

It's not clear to me, do you intend to send a new revision with some
more tests or this patch as is ready to go into bpf tree?


> Thank you,
>
> >
> > jirka
> >
> > >
> > > >
> > > > jirka
> > > >
> > > > >
> > > > > Thank you,
> > > > >
> > > > >
> > > > > > -- Steve
> > > > > >
> > > > > >
> > > > > > > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > > > > > > the rethook_instance, the rethook must be set up at the RCU available
> > > > > > > context (non idle). This rethook_recycle() in the rethook trampoline
> > > > > > > handler is inevitable, thus the RCU available check must be done before
> > > > > > > setting the rethook trampoline.
> > > > > > >
> > > > > > > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > > > > > > it will return NULL if it is called when !rcu_is_watching().
> > > > > > >
> > > > > > > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > > > > > > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > > > > > > ---
> > > > > > >  kernel/trace/rethook.c |    9 +++++++++
> > > > > > >  1 file changed, 9 insertions(+)
> > > > > > >
> > > > > > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > > > > > > index b56833700d23..c69d82273ce7 100644
> > > > > > > --- a/kernel/trace/rethook.c
> > > > > > > +++ b/kernel/trace/rethook.c
> > > > > > > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> > > > > > >     if (unlikely(!handler))
> > > > > > >             return NULL;
> > > > > > >
> > > > > > > +   /*
> > > > > > > +    * This expects the caller will set up a rethook on a function entry.
> > > > > > > +    * When the function returns, the rethook will eventually be reclaimed
> > > > > > > +    * or released in the rethook_recycle() with call_rcu().
> > > > > > > +    * This means the caller must be run in the RCU-availabe context.
> > > > > > > +    */
> > > > > > > +   if (unlikely(!rcu_is_watching()))
> > > > > > > +           return NULL;
> > > > > > > +
> > > > > > >     fn = freelist_try_get(&rh->pool);
> > > > > > >     if (!fn)
> > > > > > >             return NULL;
> > > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > >
> > >
> > > --
> > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
>
>
> --
> Masami Hiramatsu (Google) <mhiramat@kernel.org>

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

* Re: [PATCH] rethook: Reject getting a rethook if RCU is not watching
  2022-06-03 19:21             ` Andrii Nakryiko
@ 2022-06-06 16:02               ` Masami Hiramatsu
  0 siblings, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2022-06-06 16:02 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Jiri Olsa, Steven Rostedt, Alexei Starovoitov, Daniel Borkmann,
	Andrii Nakryiko, open list, Networking, bpf, Paul E. McKenney

On Fri, 3 Jun 2022 12:21:19 -0700
Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:

> On Fri, May 27, 2022 at 6:19 PM Masami Hiramatsu <mhiramat@kernel.org> wrote:
> >
> > On Sat, 28 May 2022 00:10:08 +0200
> > Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > > On Fri, May 27, 2022 at 01:14:34AM +0900, Masami Hiramatsu wrote:
> > > > On Thu, 26 May 2022 16:49:26 +0200
> > > > Jiri Olsa <olsajiri@gmail.com> wrote:
> > > >
> > > > > On Thu, May 26, 2022 at 11:25:30PM +0900, Masami Hiramatsu wrote:
> > > > > > On Tue, 24 May 2022 19:23:01 -0400
> > > > > > Steven Rostedt <rostedt@goodmis.org> wrote:
> > > > > >
> > > > > > > On Sat,  7 May 2022 13:46:52 +0900
> > > > > > > Masami Hiramatsu <mhiramat@kernel.org> wrote:
> > > > > > >
> > > > > > > Is this expected to go through the BPF tree?
> > > > > > >
> > > > > >
> > > > > > Yes, since rethook (fprobe) is currently used only from eBPF.
> > > > > > Jiri, can you check this is good for your test case?
> > > > >
> > > > > sure I'll test it.. can't see the original email,
> > > > > perhaps I wasn't cc-ed.. but I'll find it
> > > >
> > > > Here it is. I Cc-ed your @kernel.org address.
> > > > https://lore.kernel.org/all/165189881197.175864.14757002789194211860.stgit@devnote2/T/#u
> > > >
> > > > >
> > > > > is this also related to tracing 'idle' functions,
> > > > > as discussed in here?
> > > > >   https://lore.kernel.org/bpf/20220515203653.4039075-1-jolsa@kernel.org/
> > > >
> > > > Ah, yes. So this may not happen with the above patch, but for the
> > > > hardening (ensuring it is always safe), I would like to add this.
> > > >
> > > > >
> > > > > because that's the one I can reproduce.. but I can
> > > > > certainly try that with your change as well
> > > >
> > > > Thank you!
> > >
> > > it did not help the idle warning as expected, but I did not
> > > see any problems running bpf tests on top of this
> >
> > Oops, right. I forgot this is only for the rethook, not protect the
> > fprobe handlers, since fprobe code doesn't involve the RCU code (it
> > depends on ftrace's check). Sorry about that.
> > Hmm, I need to add a test code for this issue, but that could be
> > solved by your noninstr patch.
> >
> 
> 
> Masami,
> 
> It's not clear to me, do you intend to send a new revision with some
> more tests or this patch as is ready to go into bpf tree?

OK, let me make a test code against this issue. This may need a raw
fprobe test code (not a test case because it depends on that we can
trace the "arch_cpu_idle()"), but that test code won't work after
the "arch_cpu_idle()" is marked as noinstr (thus the test code will
only for the kernel which doesn't have the noinstr patch).
I want to add this check for the case if someone accidentally add
a function which is not covered by RCU and that is tracable by
fprobe (ftrace).
Thus this is a kind of preventative fix.

Thank you,

> 
> 
> > Thank you,
> >
> > >
> > > jirka
> > >
> > > >
> > > > >
> > > > > jirka
> > > > >
> > > > > >
> > > > > > Thank you,
> > > > > >
> > > > > >
> > > > > > > -- Steve
> > > > > > >
> > > > > > >
> > > > > > > > Since the rethook_recycle() will involve the call_rcu() for reclaiming
> > > > > > > > the rethook_instance, the rethook must be set up at the RCU available
> > > > > > > > context (non idle). This rethook_recycle() in the rethook trampoline
> > > > > > > > handler is inevitable, thus the RCU available check must be done before
> > > > > > > > setting the rethook trampoline.
> > > > > > > >
> > > > > > > > This adds a rcu_is_watching() check in the rethook_try_get() so that
> > > > > > > > it will return NULL if it is called when !rcu_is_watching().
> > > > > > > >
> > > > > > > > Fixes: 54ecbe6f1ed5 ("rethook: Add a generic return hook")
> > > > > > > > Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
> > > > > > > > ---
> > > > > > > >  kernel/trace/rethook.c |    9 +++++++++
> > > > > > > >  1 file changed, 9 insertions(+)
> > > > > > > >
> > > > > > > > diff --git a/kernel/trace/rethook.c b/kernel/trace/rethook.c
> > > > > > > > index b56833700d23..c69d82273ce7 100644
> > > > > > > > --- a/kernel/trace/rethook.c
> > > > > > > > +++ b/kernel/trace/rethook.c
> > > > > > > > @@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
> > > > > > > >     if (unlikely(!handler))
> > > > > > > >             return NULL;
> > > > > > > >
> > > > > > > > +   /*
> > > > > > > > +    * This expects the caller will set up a rethook on a function entry.
> > > > > > > > +    * When the function returns, the rethook will eventually be reclaimed
> > > > > > > > +    * or released in the rethook_recycle() with call_rcu().
> > > > > > > > +    * This means the caller must be run in the RCU-availabe context.
> > > > > > > > +    */
> > > > > > > > +   if (unlikely(!rcu_is_watching()))
> > > > > > > > +           return NULL;
> > > > > > > > +
> > > > > > > >     fn = freelist_try_get(&rh->pool);
> > > > > > > >     if (!fn)
> > > > > > > >             return NULL;
> > > > > > >
> > > > > >
> > > > > >
> > > > > > --
> > > > > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > > >
> > > >
> > > > --
> > > > Masami Hiramatsu (Google) <mhiramat@kernel.org>
> >
> >
> > --
> > Masami Hiramatsu (Google) <mhiramat@kernel.org>


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

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

end of thread, other threads:[~2022-06-06 16:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-07  4:46 [PATCH] rethook: Reject getting a rethook if RCU is not watching Masami Hiramatsu
2022-05-24 23:23 ` Steven Rostedt
2022-05-26 14:25   ` Masami Hiramatsu
2022-05-26 14:49     ` Jiri Olsa
2022-05-26 16:14       ` Masami Hiramatsu
2022-05-27 22:10         ` Jiri Olsa
2022-05-28  1:19           ` Masami Hiramatsu
2022-06-03 19:21             ` Andrii Nakryiko
2022-06-06 16:02               ` Masami Hiramatsu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.