[PATCH bpf-next v2 1/7] rcu: add rcu_read_lock_dont_migrate()

Menglong Dong posted 7 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCH bpf-next v2 1/7] rcu: add rcu_read_lock_dont_migrate()
Posted by Menglong Dong 1 month, 2 weeks ago
migrate_disable() is called to disable migration in the kernel, and it is
often used together with rcu_read_lock().

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

Introduce rcu_read_lock_dont_migrate() and rcu_read_unlock_migrate(),
which will do the migration enable and disable only when !PREEMPT_RCU.

Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
---
v2:
- introduce rcu_read_lock_dont_migrate() instead of rcu_migrate_disable()
---
 include/linux/rcupdate.h | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 120536f4c6eb..8918b911911f 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -962,6 +962,30 @@ static inline notrace void rcu_read_unlock_sched_notrace(void)
 	preempt_enable_notrace();
 }
 
+#ifdef CONFIG_PREEMPT_RCU
+static __always_inline void rcu_read_lock_dont_migrate(void)
+{
+	migrate_disable();
+	rcu_read_lock();
+}
+
+static inline void rcu_read_unlock_migrate(void)
+{
+	rcu_read_unlock();
+	migrate_enable();
+}
+#else
+static __always_inline void rcu_read_lock_dont_migrate(void)
+{
+	rcu_read_lock();
+}
+
+static inline void rcu_read_unlock_migrate(void)
+{
+	rcu_read_unlock();
+}
+#endif
+
 /**
  * RCU_INIT_POINTER() - initialize an RCU protected pointer
  * @p: The pointer to be initialized.
-- 
2.50.1
Re: [PATCH bpf-next v2 1/7] rcu: add rcu_read_lock_dont_migrate()
Posted by Paul E. McKenney 1 month, 2 weeks ago
On Tue, Aug 19, 2025 at 05:34:18PM +0800, Menglong Dong wrote:
> migrate_disable() is called to disable migration in the kernel, and it is
> often used together with rcu_read_lock().
> 
> However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> will always disable preemption, which will also disable migration.
> 
> Introduce rcu_read_lock_dont_migrate() and rcu_read_unlock_migrate(),
> which will do the migration enable and disable only when !PREEMPT_RCU.
> 
> Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>

This works, but could be made much more compact with no performance
degradation.  Please see below.

						Thanx, Paul

> ---
> v2:
> - introduce rcu_read_lock_dont_migrate() instead of rcu_migrate_disable()
> ---
>  include/linux/rcupdate.h | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
> 
> diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> index 120536f4c6eb..8918b911911f 100644
> --- a/include/linux/rcupdate.h
> +++ b/include/linux/rcupdate.h
> @@ -962,6 +962,30 @@ static inline notrace void rcu_read_unlock_sched_notrace(void)
>  	preempt_enable_notrace();
>  }
>  
> +#ifdef CONFIG_PREEMPT_RCU
> +static __always_inline void rcu_read_lock_dont_migrate(void)
> +{

Why not use IS_ENABLED(CONFIG_PREEMPT_RCU) to collapse the two sets of
definitions together?

> +	migrate_disable();
> +	rcu_read_lock();
> +}
> +
> +static inline void rcu_read_unlock_migrate(void)
> +{
> +	rcu_read_unlock();
> +	migrate_enable();
> +}
> +#else
> +static __always_inline void rcu_read_lock_dont_migrate(void)
> +{
> +	rcu_read_lock();
> +}
> +
> +static inline void rcu_read_unlock_migrate(void)
> +{
> +	rcu_read_unlock();
> +}
> +#endif
> +
>  /**
>   * RCU_INIT_POINTER() - initialize an RCU protected pointer
>   * @p: The pointer to be initialized.
> -- 
> 2.50.1
>
Re: [PATCH bpf-next v2 1/7] rcu: add rcu_read_lock_dont_migrate()
Posted by Menglong Dong 1 month, 2 weeks ago
On Tue, Aug 19, 2025 at 10:58 PM Paul E. McKenney <paulmck@kernel.org> wrote:
>
> On Tue, Aug 19, 2025 at 05:34:18PM +0800, Menglong Dong wrote:
> > migrate_disable() is called to disable migration in the kernel, and it is
> > often used together with rcu_read_lock().
> >
> > However, with PREEMPT_RCU disabled, it's unnecessary, as rcu_read_lock()
> > will always disable preemption, which will also disable migration.
> >
> > Introduce rcu_read_lock_dont_migrate() and rcu_read_unlock_migrate(),
> > which will do the migration enable and disable only when !PREEMPT_RCU.
> >
> > Signed-off-by: Menglong Dong <dongml2@chinatelecom.cn>
>
> This works, but could be made much more compact with no performance
> degradation.  Please see below.
>
>                                                 Thanx, Paul
>
> > ---
> > v2:
> > - introduce rcu_read_lock_dont_migrate() instead of rcu_migrate_disable()
> > ---
> >  include/linux/rcupdate.h | 24 ++++++++++++++++++++++++
> >  1 file changed, 24 insertions(+)
> >
> > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > index 120536f4c6eb..8918b911911f 100644
> > --- a/include/linux/rcupdate.h
> > +++ b/include/linux/rcupdate.h
> > @@ -962,6 +962,30 @@ static inline notrace void rcu_read_unlock_sched_notrace(void)
> >       preempt_enable_notrace();
> >  }
> >
> > +#ifdef CONFIG_PREEMPT_RCU
> > +static __always_inline void rcu_read_lock_dont_migrate(void)
> > +{
>
> Why not use IS_ENABLED(CONFIG_PREEMPT_RCU) to collapse the two sets of
> definitions together?

Yeah, that's a good idea, which makes the code much simpler.

Thanks!
Menglong Dong

>
> > +     migrate_disable();
> > +     rcu_read_lock();
> > +}
> > +
> > +static inline void rcu_read_unlock_migrate(void)
> > +{
> > +     rcu_read_unlock();
> > +     migrate_enable();
> > +}
> > +#else
> > +static __always_inline void rcu_read_lock_dont_migrate(void)
> > +{
> > +     rcu_read_lock();
> > +}
> > +
> > +static inline void rcu_read_unlock_migrate(void)
> > +{
> > +     rcu_read_unlock();
> > +}
> > +#endif
> > +
> >  /**
> >   * RCU_INIT_POINTER() - initialize an RCU protected pointer
> >   * @p: The pointer to be initialized.
> > --
> > 2.50.1
> >