Subject: [PATCH 25/28] bpf: Use RCU in all users of __module_text_address().
From: Sebastian Andrzej Siewior <[email-masked]>
Date: Wed, 8 Jan 2025 10:04:54 +0100

__module_address() can be invoked within a RCU section, there is no
requirement to have preemption disabled.

Replace the preempt_disable() section around __module_address() with
RCU.

Cc: Alexei Starovoitov <[email-masked]>
Cc: Andrii Nakryiko <[email-masked]>
Cc: Daniel Borkmann <[email-masked]>
Cc: Eduard Zingerman <[email-masked]>
Cc: Hao Luo <[email-masked]>
Cc: Jiri Olsa <[email-masked]>
Cc: John Fastabend <[email-masked]>
Cc: KP Singh <[email-masked]>
Cc: Martin KaFai Lau <[email-masked]>
Cc: Masami Hiramatsu <[email-masked]>
Cc: Mathieu Desnoyers <[email-masked]>
Cc: Matt Bobrowski <[email-masked]>
Cc: Song Liu <[email-masked]>
Cc: Stanislav Fomichev <[email-masked]>
Cc: Steven Rostedt <[email-masked]>
Cc: Yonghong Song <[email-masked]>
Cc: [email-masked]
Cc: [email-masked]
Acked-by: Peter Zijlstra (Intel) <[email-masked]>
Link: https://lore.kernel.org/all/[email-masked]
Acked-by: Alexei Starovoitov <[email-masked]>
Signed-off-by: Sebastian Andrzej Siewior <[email-masked]>
---
 kernel/trace/bpf_trace.c |   24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -2345,10 +2345,9 @@ void bpf_put_raw_tracepoint(struct bpf_r
 {
 	struct module *mod;
 
-	preempt_disable();
+	guard(rcu)();
 	mod = __module_address((unsigned long)btp);
 	module_put(mod);
-	preempt_enable();
 }
 
 static __always_inline
@@ -2932,18 +2931,21 @@ static int get_modules_for_addrs(struct
 	u32 i, err = 0;
 
 	for (i = 0; i < addrs_cnt; i++) {
+		bool skip_add = false;
 		struct module *mod;
 
-		preempt_disable();
-		mod = __module_address(addrs[i]);
-		/* Either no module or we it's already stored  */
-		if (!mod || has_module(&arr, mod)) {
-			preempt_enable();
-			continue;
+		scoped_guard(rcu) {
+			mod = __module_address(addrs[i]);
+			/* Either no module or it's already stored  */
+			if (!mod || has_module(&arr, mod)) {
+				skip_add = true;
+				break; /* scoped_guard */
+			}
+			if (!try_module_get(mod))
+				err = -EINVAL;
 		}
-		if (!try_module_get(mod))
-			err = -EINVAL;
-		preempt_enable();
+		if (skip_add)
+			continue;
 		if (err)
 			break;
 		err = add_module(&arr, mod);