bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable}
@ 2025-08-15  6:18 Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable Menglong Dong
                   ` (6 more replies)
  0 siblings, 7 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

migrate_disable() and rcu_read_lock() are used to together in many case in
bpf. However, when PREEMPT_RCU is not enabled, rcu_read_lock() indicate
migrate_disable(), so we don't need to call it in this case.

In this series, we introduce rcu_migrate_disable and rcu_migrate_enable,
which will call migrate_disable and migrate_enable only when PREEMPT_RCU
enabled. And replace the migrate_enable/migrate_disable with
rcu_migrate_enable/rcu_migrate_disable in bpf.

Menglong Dong (7):
  rcu: add rcu_migrate_enable and rcu_migrate_disable
  bpf: use rcu_migrate_* for bpf_cgrp_storage_free()
  bpf: use rcu_migrate_* for bpf_inode_storage_free()
  bpf: use rcu_migrate_* for bpf_iter_run_prog()
  bpf: use rcu_migrate_* for bpf_task_storage_free()
  bpf: use rcu_migrate_* for bpf_prog_run_array_cg()
  bpf: use rcu_migrate_* for trampoline.c

 include/linux/rcupdate.h       | 18 ++++++++++++++++++
 kernel/bpf/bpf_cgrp_storage.c  |  4 ++--
 kernel/bpf/bpf_inode_storage.c |  4 ++--
 kernel/bpf/bpf_iter.c          |  4 ++--
 kernel/bpf/bpf_task_storage.c  |  4 ++--
 kernel/bpf/cgroup.c            |  4 ++--
 kernel/bpf/trampoline.c        | 12 ++++++------
 7 files changed, 34 insertions(+), 16 deletions(-)

-- 
2.50.1


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

* [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  2025-08-15 13:02   ` Alexei Starovoitov
  2025-08-15  6:18 ` [PATCH bpf-next 2/7] bpf: use rcu_migrate_* for bpf_cgrp_storage_free() Menglong Dong
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

migrate_disable() is called to disable migration in the kernel, and it is
used togather with rcu_read_lock() oftenly.

However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
will disable preemption, which will also disable migration.

Introduce rcu_migrate_enable() and rcu_migrate_disable(), which will do
the migration enable and disable only when the rcu_read_lock() can't do
it.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 include/linux/rcupdate.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 120536f4c6eb..0d9dbd90d025 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -72,6 +72,16 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
 void __rcu_read_lock(void);
 void __rcu_read_unlock(void);
 
+static inline void rcu_migrate_enable(void)
+{
+	migrate_enable();
+}
+
+static inline void rcu_migrate_disable(void)
+{
+	migrate_disable();
+}
+
 /*
  * Defined as a macro as it is a very low level header included from
  * areas that don't even know about current.  This gives the rcu_read_lock()
@@ -105,6 +115,14 @@ static inline int rcu_preempt_depth(void)
 	return 0;
 }
 
+static inline void rcu_migrate_enable(void)
+{
+}
+
+static inline void rcu_migrate_disable(void)
+{
+}
+
 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
 
 #ifdef CONFIG_RCU_LAZY
-- 
2.50.1


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

* [PATCH bpf-next 2/7] bpf: use rcu_migrate_* for bpf_cgrp_storage_free()
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 3/7] bpf: use rcu_migrate_* for bpf_inode_storage_free() Menglong Dong
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Replace the migrate_disable/migrate_enable with
rcu_migrate_disable/rcu_migrate_enable in bpf_cgrp_storage_free to obtain
better performance when PREEMPT_RCU is not enabled.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 kernel/bpf/bpf_cgrp_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/bpf_cgrp_storage.c b/kernel/bpf/bpf_cgrp_storage.c
index 148da8f7ff36..9bbe74cdf918 100644
--- a/kernel/bpf/bpf_cgrp_storage.c
+++ b/kernel/bpf/bpf_cgrp_storage.c
@@ -45,7 +45,7 @@ void bpf_cgrp_storage_free(struct cgroup *cgroup)
 {
 	struct bpf_local_storage *local_storage;
 
-	migrate_disable();
+	rcu_migrate_disable();
 	rcu_read_lock();
 	local_storage = rcu_dereference(cgroup->bpf_cgrp_storage);
 	if (!local_storage)
@@ -56,7 +56,7 @@ void bpf_cgrp_storage_free(struct cgroup *cgroup)
 	bpf_cgrp_storage_unlock();
 out:
 	rcu_read_unlock();
-	migrate_enable();
+	rcu_migrate_enable();
 }
 
 static struct bpf_local_storage_data *
-- 
2.50.1


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

* [PATCH bpf-next 3/7] bpf: use rcu_migrate_* for bpf_inode_storage_free()
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 2/7] bpf: use rcu_migrate_* for bpf_cgrp_storage_free() Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 4/7] bpf: use rcu_migrate_* for bpf_iter_run_prog() Menglong Dong
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Replace the migrate_disable/migrate_enable with
rcu_migrate_disable/rcu_migrate_enable in bpf_inode_storage_free to obtain
better performance when PREEMPT_RCU is not enabled.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 kernel/bpf/bpf_inode_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/bpf_inode_storage.c b/kernel/bpf/bpf_inode_storage.c
index 15a3eb9b02d9..548530feb4da 100644
--- a/kernel/bpf/bpf_inode_storage.c
+++ b/kernel/bpf/bpf_inode_storage.c
@@ -62,7 +62,7 @@ void bpf_inode_storage_free(struct inode *inode)
 	if (!bsb)
 		return;
 
-	migrate_disable();
+	rcu_migrate_disable();
 	rcu_read_lock();
 
 	local_storage = rcu_dereference(bsb->storage);
@@ -72,7 +72,7 @@ void bpf_inode_storage_free(struct inode *inode)
 	bpf_local_storage_destroy(local_storage);
 out:
 	rcu_read_unlock();
-	migrate_enable();
+	rcu_migrate_enable();
 }
 
 static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
-- 
2.50.1


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

* [PATCH bpf-next 4/7] bpf: use rcu_migrate_* for bpf_iter_run_prog()
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
                   ` (2 preceding siblings ...)
  2025-08-15  6:18 ` [PATCH bpf-next 3/7] bpf: use rcu_migrate_* for bpf_inode_storage_free() Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 5/7] bpf: use rcu_migrate_* for bpf_task_storage_free() Menglong Dong
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Replace the migrate_disable/migrate_enable with
rcu_migrate_disable/rcu_migrate_enable in bpf_iter_run_prog to obtain
better performance when PREEMPT_RCU is not enabled.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 kernel/bpf/bpf_iter.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c
index 0cbcae727079..25feb93d44a9 100644
--- a/kernel/bpf/bpf_iter.c
+++ b/kernel/bpf/bpf_iter.c
@@ -706,11 +706,11 @@ int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx)
 		rcu_read_unlock_trace();
 	} else {
 		rcu_read_lock();
-		migrate_disable();
+		rcu_migrate_disable();
 		old_run_ctx = bpf_set_run_ctx(&run_ctx);
 		ret = bpf_prog_run(prog, ctx);
 		bpf_reset_run_ctx(old_run_ctx);
-		migrate_enable();
+		rcu_migrate_enable();
 		rcu_read_unlock();
 	}
 
-- 
2.50.1


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

* [PATCH bpf-next 5/7] bpf: use rcu_migrate_* for bpf_task_storage_free()
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
                   ` (3 preceding siblings ...)
  2025-08-15  6:18 ` [PATCH bpf-next 4/7] bpf: use rcu_migrate_* for bpf_iter_run_prog() Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 6/7] bpf: use rcu_migrate_* for bpf_prog_run_array_cg() Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 7/7] bpf: use rcu_migrate_* for trampoline.c Menglong Dong
  6 siblings, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Replace the migrate_disable/migrate_enable with
rcu_migrate_disable/rcu_migrate_enable in bpf_task_storage_free to obtain
better performance when PREEMPT_RCU is not enabled.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 kernel/bpf/bpf_task_storage.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/bpf_task_storage.c b/kernel/bpf/bpf_task_storage.c
index 1109475953c0..cbbf1b72eece 100644
--- a/kernel/bpf/bpf_task_storage.c
+++ b/kernel/bpf/bpf_task_storage.c
@@ -70,7 +70,7 @@ void bpf_task_storage_free(struct task_struct *task)
 {
 	struct bpf_local_storage *local_storage;
 
-	migrate_disable();
+	rcu_migrate_disable();
 	rcu_read_lock();
 
 	local_storage = rcu_dereference(task->bpf_storage);
@@ -82,7 +82,7 @@ void bpf_task_storage_free(struct task_struct *task)
 	bpf_task_storage_unlock();
 out:
 	rcu_read_unlock();
-	migrate_enable();
+	rcu_migrate_enable();
 }
 
 static void *bpf_pid_task_storage_lookup_elem(struct bpf_map *map, void *key)
-- 
2.50.1


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

* [PATCH bpf-next 6/7] bpf: use rcu_migrate_* for bpf_prog_run_array_cg()
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
                   ` (4 preceding siblings ...)
  2025-08-15  6:18 ` [PATCH bpf-next 5/7] bpf: use rcu_migrate_* for bpf_task_storage_free() Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  2025-08-15  6:18 ` [PATCH bpf-next 7/7] bpf: use rcu_migrate_* for trampoline.c Menglong Dong
  6 siblings, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Replace the migrate_disable/migrate_enable with
rcu_migrate_disable/rcu_migrate_enable in bpf_prog_run_array_cg to obtain
better performance when PREEMPT_RCU is not enabled.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 kernel/bpf/cgroup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 180b630279b9..694635699d46 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -71,7 +71,7 @@ bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
 	u32 func_ret;
 
 	run_ctx.retval = retval;
-	migrate_disable();
+	rcu_migrate_disable();
 	rcu_read_lock();
 	array = rcu_dereference(cgrp->effective[atype]);
 	item = &array->items[0];
@@ -89,7 +89,7 @@ bpf_prog_run_array_cg(const struct cgroup_bpf *cgrp,
 	}
 	bpf_reset_run_ctx(old_run_ctx);
 	rcu_read_unlock();
-	migrate_enable();
+	rcu_migrate_enable();
 	return run_ctx.retval;
 }
 
-- 
2.50.1


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

* [PATCH bpf-next 7/7] bpf: use rcu_migrate_* for trampoline.c
  2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
                   ` (5 preceding siblings ...)
  2025-08-15  6:18 ` [PATCH bpf-next 6/7] bpf: use rcu_migrate_* for bpf_prog_run_array_cg() Menglong Dong
@ 2025-08-15  6:18 ` Menglong Dong
  6 siblings, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-15  6:18 UTC (permalink / raw)
  To: ast
  Cc: daniel, john.fastabend, andrii, martin.lau, eddyz87, song,
	yonghong.song, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel

Replace the migrate_disable/migrate_enable with
rcu_migrate_disable/rcu_migrate_enable in trampoline.c to obtain better
performance when PREEMPT_RCU is not enabled.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
 kernel/bpf/trampoline.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index 0e364614c3a2..a0608152c394 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -900,7 +900,7 @@ static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tram
 	__acquires(RCU)
 {
 	rcu_read_lock();
-	migrate_disable();
+	rcu_migrate_disable();
 
 	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
 
@@ -949,7 +949,7 @@ static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start,
 
 	update_prog_stats(prog, start);
 	this_cpu_dec(*(prog->active));
-	migrate_enable();
+	rcu_migrate_enable();
 	rcu_read_unlock();
 }
 
@@ -961,7 +961,7 @@ static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
 	 * programs, not the shims.
 	 */
 	rcu_read_lock();
-	migrate_disable();
+	rcu_migrate_disable();
 
 	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
 
@@ -974,7 +974,7 @@ static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
 {
 	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
 
-	migrate_enable();
+	rcu_migrate_enable();
 	rcu_read_unlock();
 }
 
@@ -1034,7 +1034,7 @@ static u64 notrace __bpf_prog_enter(struct bpf_prog *prog,
 	__acquires(RCU)
 {
 	rcu_read_lock();
-	migrate_disable();
+	rcu_migrate_disable();
 
 	run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
 
@@ -1048,7 +1048,7 @@ static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start,
 	bpf_reset_run_ctx(run_ctx->saved_run_ctx);
 
 	update_prog_stats(prog, start);
-	migrate_enable();
+	rcu_migrate_enable();
 	rcu_read_unlock();
 }
 
-- 
2.50.1


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

* Re: [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable
  2025-08-15  6:18 ` [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable Menglong Dong
@ 2025-08-15 13:02   ` Alexei Starovoitov
  2025-08-15 15:31     ` Paul E. McKenney
  2025-08-17  1:55     ` Menglong Dong
  0 siblings, 2 replies; 13+ messages in thread
From: Alexei Starovoitov @ 2025-08-15 13:02 UTC (permalink / raw)
  To: Menglong Dong, Paul E. McKenney
  Cc: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
	Andrii Nakryiko, Martin KaFai Lau, Eduard, Song Liu,
	Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	bpf, LKML

On Fri, Aug 15, 2025 at 9:18 AM Menglong Dong <menglong8.dong@gmail.com> wrote:
>
> migrate_disable() is called to disable migration in the kernel, and it is
> used togather with rcu_read_lock() oftenly.
>
> However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> will disable preemption, which will also disable migration.
>
> Introduce rcu_migrate_enable() and rcu_migrate_disable(), which will do
> the migration enable and disable only when the rcu_read_lock() can't do
> it.
>
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> ---
>  include/linux/rcupdate.h | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
>
> diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> index 120536f4c6eb..0d9dbd90d025 100644
> --- a/include/linux/rcupdate.h
> +++ b/include/linux/rcupdate.h
> @@ -72,6 +72,16 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
>  void __rcu_read_lock(void);
>  void __rcu_read_unlock(void);
>
> +static inline void rcu_migrate_enable(void)
> +{
> +       migrate_enable();
> +}

Interesting idea.
I think it has to be combined with rcu_read_lock(), since this api
makes sense only when used together.

rcu_read_lock_dont_migrate() ?

It will do rcu_read_lock() + migrate_disalbe() in PREEMPT_RCU
and rcu_read_lock() + preempt_disable() otherwise?

Also I'm not sure we can rely on rcu_read_lock()
disabling preemption in all !PREEMPT_RCU cases.
iirc it's more nuanced than that.

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

* Re: [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable
  2025-08-15 13:02   ` Alexei Starovoitov
@ 2025-08-15 15:31     ` Paul E. McKenney
  2025-08-17  2:01       ` Menglong Dong
  2025-08-17  1:55     ` Menglong Dong
  1 sibling, 1 reply; 13+ messages in thread
From: Paul E. McKenney @ 2025-08-15 15:31 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Menglong Dong, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Martin KaFai Lau, Eduard,
	Song Liu, Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo,
	Jiri Olsa, bpf, LKML

On Fri, Aug 15, 2025 at 04:02:14PM +0300, Alexei Starovoitov wrote:
> On Fri, Aug 15, 2025 at 9:18 AM Menglong Dong <menglong8.dong@gmail.com> wrote:
> >
> > migrate_disable() is called to disable migration in the kernel, and it is
> > used togather with rcu_read_lock() oftenly.
> >
> > However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> > will disable preemption, which will also disable migration.
> >
> > Introduce rcu_migrate_enable() and rcu_migrate_disable(), which will do
> > the migration enable and disable only when the rcu_read_lock() can't do
> > it.
> >
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > ---
> >  include/linux/rcupdate.h | 18 ++++++++++++++++++
> >  1 file changed, 18 insertions(+)
> >
> > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > index 120536f4c6eb..0d9dbd90d025 100644
> > --- a/include/linux/rcupdate.h
> > +++ b/include/linux/rcupdate.h
> > @@ -72,6 +72,16 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
> >  void __rcu_read_lock(void);
> >  void __rcu_read_unlock(void);
> >
> > +static inline void rcu_migrate_enable(void)
> > +{
> > +       migrate_enable();
> > +}
> 
> Interesting idea.
> I think it has to be combined with rcu_read_lock(), since this api
> makes sense only when used together.
> 
> rcu_read_lock_dont_migrate() ?
> 
> It will do rcu_read_lock() + migrate_disalbe() in PREEMPT_RCU
> and rcu_read_lock() + preempt_disable() otherwise?

That could easily be provided.  Or just make one, and if it starts
having enough use cases, it could be pulled into RCU proper.

> Also I'm not sure we can rely on rcu_read_lock()
> disabling preemption in all !PREEMPT_RCU cases.
> iirc it's more nuanced than that.

For once, something about RCU is non-nuanced.  But don't worry, it won't
happen again.  ;-)

In all !PREEMPT_RCU, preemption must be disabled across all RCU read-side
critical sections in order for RCU to work correctly.

							Thanx, Paul

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

* Re: [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable
  2025-08-15 13:02   ` Alexei Starovoitov
  2025-08-15 15:31     ` Paul E. McKenney
@ 2025-08-17  1:55     ` Menglong Dong
  1 sibling, 0 replies; 13+ messages in thread
From: Menglong Dong @ 2025-08-17  1:55 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Paul E. McKenney, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Martin KaFai Lau, Eduard,
	Song Liu, Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo,
	Jiri Olsa, bpf, LKML

On Fri, Aug 15, 2025 at 9:02 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Fri, Aug 15, 2025 at 9:18 AM Menglong Dong <menglong8.dong@gmail.com> wrote:
> >
> > migrate_disable() is called to disable migration in the kernel, and it is
> > used togather with rcu_read_lock() oftenly.
> >
> > However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> > will disable preemption, which will also disable migration.
> >
> > Introduce rcu_migrate_enable() and rcu_migrate_disable(), which will do
> > the migration enable and disable only when the rcu_read_lock() can't do
> > it.
> >
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > ---
> >  include/linux/rcupdate.h | 18 ++++++++++++++++++
> >  1 file changed, 18 insertions(+)
> >
> > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > index 120536f4c6eb..0d9dbd90d025 100644
> > --- a/include/linux/rcupdate.h
> > +++ b/include/linux/rcupdate.h
> > @@ -72,6 +72,16 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
> >  void __rcu_read_lock(void);
> >  void __rcu_read_unlock(void);
> >
> > +static inline void rcu_migrate_enable(void)
> > +{
> > +       migrate_enable();
> > +}
>
> Interesting idea.
> I think it has to be combined with rcu_read_lock(), since this api
> makes sense only when used together.
>
> rcu_read_lock_dont_migrate() ?

Yeah, it looks much better. So we can introduce:

  rcu_read_lock_dont_migrate()
  rcu_read_unlock_do_ migrate()

>
> It will do rcu_read_lock() + migrate_disalbe() in PREEMPT_RCU
> and rcu_read_lock() + preempt_disable() otherwise?
>
> Also I'm not sure we can rely on rcu_read_lock()
> disabling preemption in all !PREEMPT_RCU cases.
> iirc it's more nuanced than that.

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

* Re: [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable
  2025-08-15 15:31     ` Paul E. McKenney
@ 2025-08-17  2:01       ` Menglong Dong
  2025-08-19 21:34         ` Paul E. McKenney
  0 siblings, 1 reply; 13+ messages in thread
From: Menglong Dong @ 2025-08-17  2:01 UTC (permalink / raw)
  To: paulmck
  Cc: Alexei Starovoitov, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Martin KaFai Lau, Eduard,
	Song Liu, Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo,
	Jiri Olsa, bpf, LKML

On Fri, Aug 15, 2025 at 11:31 PM Paul E. McKenney <paulmck@kernel.org> wrote:
>
> On Fri, Aug 15, 2025 at 04:02:14PM +0300, Alexei Starovoitov wrote:
> > On Fri, Aug 15, 2025 at 9:18 AM Menglong Dong <menglong8.dong@gmail.com> wrote:
> > >
> > > migrate_disable() is called to disable migration in the kernel, and it is
> > > used togather with rcu_read_lock() oftenly.
> > >
> > > However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> > > will disable preemption, which will also disable migration.
> > >
> > > Introduce rcu_migrate_enable() and rcu_migrate_disable(), which will do
> > > the migration enable and disable only when the rcu_read_lock() can't do
> > > it.
> > >
> > > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > > ---
> > >  include/linux/rcupdate.h | 18 ++++++++++++++++++
> > >  1 file changed, 18 insertions(+)
> > >
> > > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > > index 120536f4c6eb..0d9dbd90d025 100644
> > > --- a/include/linux/rcupdate.h
> > > +++ b/include/linux/rcupdate.h
> > > @@ -72,6 +72,16 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
> > >  void __rcu_read_lock(void);
> > >  void __rcu_read_unlock(void);
> > >
> > > +static inline void rcu_migrate_enable(void)
> > > +{
> > > +       migrate_enable();
> > > +}
> >
> > Interesting idea.
> > I think it has to be combined with rcu_read_lock(), since this api
> > makes sense only when used together.
> >
> > rcu_read_lock_dont_migrate() ?
> >
> > It will do rcu_read_lock() + migrate_disalbe() in PREEMPT_RCU
> > and rcu_read_lock() + preempt_disable() otherwise?
>
> That could easily be provided.  Or just make one, and if it starts
> having enough use cases, it could be pulled into RCU proper.

Hi, do you mean that we should start with a single
use case? In this series, I started it with the BPF
subsystem. Most of the situations are similar, which will
call rcu_read_lock+migrate_disable and run bpf prog.

>
> > Also I'm not sure we can rely on rcu_read_lock()
> > disabling preemption in all !PREEMPT_RCU cases.
> > iirc it's more nuanced than that.
>
> For once, something about RCU is non-nuanced.  But don't worry, it won't
> happen again.  ;-)
>
> In all !PREEMPT_RCU, preemption must be disabled across all RCU read-side
> critical sections in order for RCU to work correctly.

Great! I worried about this part too.

Thanks!
Menglong Dong

>
>                                                         Thanx, Paul

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

* Re: [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable
  2025-08-17  2:01       ` Menglong Dong
@ 2025-08-19 21:34         ` Paul E. McKenney
  0 siblings, 0 replies; 13+ messages in thread
From: Paul E. McKenney @ 2025-08-19 21:34 UTC (permalink / raw)
  To: Menglong Dong
  Cc: Alexei Starovoitov, Alexei Starovoitov, Daniel Borkmann,
	John Fastabend, Andrii Nakryiko, Martin KaFai Lau, Eduard,
	Song Liu, Yonghong Song, KP Singh, Stanislav Fomichev, Hao Luo,
	Jiri Olsa, bpf, LKML

On Sun, Aug 17, 2025 at 10:01:23AM +0800, Menglong Dong wrote:
> On Fri, Aug 15, 2025 at 11:31 PM Paul E. McKenney <paulmck@kernel.org> wrote:
> >
> > On Fri, Aug 15, 2025 at 04:02:14PM +0300, Alexei Starovoitov wrote:
> > > On Fri, Aug 15, 2025 at 9:18 AM Menglong Dong <menglong8.dong@gmail.com> wrote:
> > > >
> > > > migrate_disable() is called to disable migration in the kernel, and it is
> > > > used togather with rcu_read_lock() oftenly.
> > > >
> > > > However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> > > > will disable preemption, which will also disable migration.
> > > >
> > > > Introduce rcu_migrate_enable() and rcu_migrate_disable(), which will do
> > > > the migration enable and disable only when the rcu_read_lock() can't do
> > > > it.
> > > >
> > > > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
> > > > ---
> > > >  include/linux/rcupdate.h | 18 ++++++++++++++++++
> > > >  1 file changed, 18 insertions(+)
> > > >
> > > > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > > > index 120536f4c6eb..0d9dbd90d025 100644
> > > > --- a/include/linux/rcupdate.h
> > > > +++ b/include/linux/rcupdate.h
> > > > @@ -72,6 +72,16 @@ static inline bool same_state_synchronize_rcu(unsigned long oldstate1, unsigned
> > > >  void __rcu_read_lock(void);
> > > >  void __rcu_read_unlock(void);
> > > >
> > > > +static inline void rcu_migrate_enable(void)
> > > > +{
> > > > +       migrate_enable();
> > > > +}
> > >
> > > Interesting idea.
> > > I think it has to be combined with rcu_read_lock(), since this api
> > > makes sense only when used together.
> > >
> > > rcu_read_lock_dont_migrate() ?
> > >
> > > It will do rcu_read_lock() + migrate_disalbe() in PREEMPT_RCU
> > > and rcu_read_lock() + preempt_disable() otherwise?
> >
> > That could easily be provided.  Or just make one, and if it starts
> > having enough use cases, it could be pulled into RCU proper.
> 
> Hi, do you mean that we should start with a single
> use case? In this series, I started it with the BPF
> subsystem. Most of the situations are similar, which will
> call rcu_read_lock+migrate_disable and run bpf prog.

Other than my wanting more compact code, what you did in your patch
series is fine.

							Thanx, Paul

> > > Also I'm not sure we can rely on rcu_read_lock()
> > > disabling preemption in all !PREEMPT_RCU cases.
> > > iirc it's more nuanced than that.
> >
> > For once, something about RCU is non-nuanced.  But don't worry, it won't
> > happen again.  ;-)
> >
> > In all !PREEMPT_RCU, preemption must be disabled across all RCU read-side
> > critical sections in order for RCU to work correctly.
> 
> Great! I worried about this part too.
> 
> Thanks!
> Menglong Dong
> 
> >
> >                                                         Thanx, Paul

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

end of thread, other threads:[~2025-08-19 21:34 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-15  6:18 [PATCH bpf-next 0/7] bpf: introduce and use rcu_migrate_{enable,disable} Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 1/7] rcu: add rcu_migrate_enable and rcu_migrate_disable Menglong Dong
2025-08-15 13:02   ` Alexei Starovoitov
2025-08-15 15:31     ` Paul E. McKenney
2025-08-17  2:01       ` Menglong Dong
2025-08-19 21:34         ` Paul E. McKenney
2025-08-17  1:55     ` Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 2/7] bpf: use rcu_migrate_* for bpf_cgrp_storage_free() Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 3/7] bpf: use rcu_migrate_* for bpf_inode_storage_free() Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 4/7] bpf: use rcu_migrate_* for bpf_iter_run_prog() Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 5/7] bpf: use rcu_migrate_* for bpf_task_storage_free() Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 6/7] bpf: use rcu_migrate_* for bpf_prog_run_array_cg() Menglong Dong
2025-08-15  6:18 ` [PATCH bpf-next 7/7] bpf: use rcu_migrate_* for trampoline.c Menglong Dong

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).