Skip to contentSkip to conversation

rethook: Reject getting a rethook if RCU is not watching

PATCH Superseded Masami Hiramatsu 4 participants 9 messages May 7, 2022 – Jun 6, 2022

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

PATCH 1/1 rethook: Reject getting a rethook if RCU is not watching

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
maintainer
#1 lore raw

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> wrote:

Is this expected to go through the BPF tree?

-- Steve

Quoted 35 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
author maintainer
#2 lore raw
Quoted 5 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> 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,

Quoted 39 lines

-- Steve

Quoted 35 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#3 lore raw
Quoted 11 lines · Masami Hiramatsu
Quoted 5 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> 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/[email protected]/

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

jirka

Quoted 47 lines

Thank you,

Quoted 39 lines

-- Steve

Quoted 35 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#4 lore raw
Quoted 7 lines · On Thu, 26 May 2022 16:49:26 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

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

Quoted 4 lines

is this also related to tracing 'idle' functions, as discussed in here?
https://lore.kernel.org/bpf/[email protected]/

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.

Quoted 3 lines

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

Thank you!

Quoted 50 lines

jirka

Quoted 47 lines

Thank you,

Quoted 39 lines

-- Steve

Quoted 35 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#5 lore raw
Quoted 27 lines · Masami Hiramatsu
Quoted 7 lines · On Thu, 26 May 2022 16:49:26 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

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

Quoted 4 lines

is this also related to tracing 'idle' functions, as discussed in here?
https://lore.kernel.org/bpf/[email protected]/

Quoted 8 lines

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.

Quoted 3 lines

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

Quoted 55 lines
Quoted 51 lines

jirka

Quoted 47 lines

Thank you,

Quoted 39 lines

-- Steve

Quoted 35 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
author maintainer
#6 lore raw
Quoted 32 lines · On Sat, 28 May 2022 00:10:08 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 31 lines · Masami Hiramatsu
Quoted 7 lines · On Thu, 26 May 2022 16:49:26 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

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

Quoted 4 lines

is this also related to tracing 'idle' functions, as discussed in here?
https://lore.kernel.org/bpf/[email protected]/

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.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

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,

Quoted 58 lines

jirka

Quoted 55 lines
Quoted 51 lines

jirka

Quoted 47 lines

Thank you,

Quoted 39 lines

-- Steve

Quoted 35 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +9 -0
index b56833700d23..c69d82273ce7 100644
@@ -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;
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#7 lore raw
Quoted 41 lines · Masami Hiramatsu
Quoted 37 lines · On Sat, 28 May 2022 00:10:08 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 35 lines · Masami Hiramatsu
Quoted 7 lines · On Thu, 26 May 2022 16:49:26 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

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

Quoted 4 lines

is this also related to tracing 'idle' functions, as discussed in here?
https://lore.kernel.org/bpf/[email protected]/

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.

Quoted 3 lines

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

Thank you!

Quoted 9 lines
Quoted 2 lines

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?

Quoted 64 lines

Thank you,

Quoted 58 lines

jirka

Quoted 55 lines
Quoted 51 lines

jirka

Quoted 47 lines

Thank you,

Quoted 26 lines

-- Steve

Quoted 23 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +0 -0
index b56833700d23..c69d82273ce7 100644
@@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
    if (unlikely(!handler))
            return NULL;
 
Quoted 13 lines

kernel/trace/rethook.c struct rethook_node *rethook_try_get(struct rethook *rh) · line 168

Quoted 12 lines
kernel/trace/rethook.c +9 -0
+   /*
+    * 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) <[email protected]>

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

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

author maintainer
#8 lore raw
Quoted 53 lines · On Fri, 3 Jun 2022 12:21:19 -0700 Andrii Nakryiko <[email protected]>…
Quoted 41 lines · Masami Hiramatsu
Quoted 37 lines · On Sat, 28 May 2022 00:10:08 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 35 lines · Masami Hiramatsu
Quoted 7 lines · On Thu, 26 May 2022 16:49:26 +0200 Jiri Olsa <[email protected]> wrote:
Quoted 6 lines · Masami Hiramatsu
Quoted 3 lines · On Tue, 24 May 2022 19:23:01 -0400 Steven Rostedt <[email protected]> wrote:

On Sat, 7 May 2022 13:46:52 +0900
Masami Hiramatsu <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

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

Quoted 4 lines

is this also related to tracing 'idle' functions, as discussed in here?
https://lore.kernel.org/bpf/[email protected]/

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.

Quoted 3 lines

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

Thank you!

Quoted 9 lines
Quoted 2 lines

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.

Quoted 4 lines

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,

Quoted 66 lines
Quoted 66 lines

Thank you,

Quoted 58 lines

jirka

Quoted 55 lines
Quoted 51 lines

jirka

Quoted 47 lines

Thank you,

Quoted 26 lines

-- Steve

Quoted 23 lines

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")
---
 kernel/trace/rethook.c |    9 +++++++++
 1 file changed, 9 insertions(+)
kernel/trace/rethook.c +0 -0
index b56833700d23..c69d82273ce7 100644
@@ -154,6 +154,15 @@ struct rethook_node *rethook_try_get(struct rethook *rh)
    if (unlikely(!handler))
            return NULL;
 
Quoted 13 lines

kernel/trace/rethook.c struct rethook_node *rethook_try_get(struct rethook *rh) · line 168

Quoted 12 lines
kernel/trace/rethook.c +9 -0
+   /*
+    * 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) <[email protected]>

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

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

Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>