[PATCH 08/10] srcu: Add __srcu_clone_read_lock()

Peter Zijlstra posted 10 patches 1 year, 7 months ago
There is a newer version of this series
[PATCH 08/10] srcu: Add __srcu_clone_read_lock()
Posted by Peter Zijlstra 1 year, 7 months ago
In order to support carrying an srcu_read_lock() section across fork,
where both the parent and child process will do: srcu_read_unlock(),
it is needed to account for the extra decrement with an extra
increment at fork time.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 include/linux/srcu.h     |    1 +
 include/linux/srcutiny.h |   10 ++++++++++
 kernel/rcu/srcutree.c    |    5 +++++
 3 files changed, 16 insertions(+)

--- a/include/linux/srcu.h
+++ b/include/linux/srcu.h
@@ -55,6 +55,7 @@ void call_srcu(struct srcu_struct *ssp,
 		void (*func)(struct rcu_head *head));
 void cleanup_srcu_struct(struct srcu_struct *ssp);
 int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
+void __srcu_clone_read_lock(struct srcu_struct *ssp, int idx);
 void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp);
 void synchronize_srcu(struct srcu_struct *ssp);
 unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
--- a/include/linux/srcutiny.h
+++ b/include/linux/srcutiny.h
@@ -71,6 +71,16 @@ static inline int __srcu_read_lock(struc
 	return idx;
 }
 
+static inline void __srcu_clone_read_lock(struct srcu_struct *ssp, int idx)
+{
+	int newval;
+
+	preempt_disable();  // Needed for PREEMPT_AUTO
+	newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) + 1;
+	WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
+	preempt_enable();
+}
+
 static inline void synchronize_srcu_expedited(struct srcu_struct *ssp)
 {
 	synchronize_srcu(ssp);
--- a/kernel/rcu/srcutree.c
+++ b/kernel/rcu/srcutree.c
@@ -720,6 +720,11 @@ int __srcu_read_lock(struct srcu_struct
 }
 EXPORT_SYMBOL_GPL(__srcu_read_lock);
 
+void __srcu_clone_read_lock(struct srcu_struct *ssp, int idx)
+{
+	this_cpu_inc(ssp->sda->srcu_lock_count[idx].counter);
+}
+
 /*
  * Removes the count for the old reader from the appropriate per-CPU
  * element of the srcu_struct.  Note that this may well be a different
Re: [PATCH 08/10] srcu: Add __srcu_clone_read_lock()
Posted by Boqun Feng 1 year, 7 months ago
On Mon, Jul 08, 2024 at 11:12:49AM +0200, Peter Zijlstra wrote:
> In order to support carrying an srcu_read_lock() section across fork,
> where both the parent and child process will do: srcu_read_unlock(),
> it is needed to account for the extra decrement with an extra
> increment at fork time.
> 

We also need to dup the per-task lock held stack in order to maintain
consistent data for lockdep, right?

Regards,
Boqun

> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  include/linux/srcu.h     |    1 +
>  include/linux/srcutiny.h |   10 ++++++++++
>  kernel/rcu/srcutree.c    |    5 +++++
>  3 files changed, 16 insertions(+)
> 
> --- a/include/linux/srcu.h
> +++ b/include/linux/srcu.h
> @@ -55,6 +55,7 @@ void call_srcu(struct srcu_struct *ssp,
>  		void (*func)(struct rcu_head *head));
>  void cleanup_srcu_struct(struct srcu_struct *ssp);
>  int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp);
> +void __srcu_clone_read_lock(struct srcu_struct *ssp, int idx);
>  void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp);
>  void synchronize_srcu(struct srcu_struct *ssp);
>  unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp);
> --- a/include/linux/srcutiny.h
> +++ b/include/linux/srcutiny.h
> @@ -71,6 +71,16 @@ static inline int __srcu_read_lock(struc
>  	return idx;
>  }
>  
> +static inline void __srcu_clone_read_lock(struct srcu_struct *ssp, int idx)
> +{
> +	int newval;
> +
> +	preempt_disable();  // Needed for PREEMPT_AUTO
> +	newval = READ_ONCE(ssp->srcu_lock_nesting[idx]) + 1;
> +	WRITE_ONCE(ssp->srcu_lock_nesting[idx], newval);
> +	preempt_enable();
> +}
> +
>  static inline void synchronize_srcu_expedited(struct srcu_struct *ssp)
>  {
>  	synchronize_srcu(ssp);
> --- a/kernel/rcu/srcutree.c
> +++ b/kernel/rcu/srcutree.c
> @@ -720,6 +720,11 @@ int __srcu_read_lock(struct srcu_struct
>  }
>  EXPORT_SYMBOL_GPL(__srcu_read_lock);
>  
> +void __srcu_clone_read_lock(struct srcu_struct *ssp, int idx)
> +{
> +	this_cpu_inc(ssp->sda->srcu_lock_count[idx].counter);
> +}
> +
>  /*
>   * Removes the count for the old reader from the appropriate per-CPU
>   * element of the srcu_struct.  Note that this may well be a different
> 
>
Re: [PATCH 08/10] srcu: Add __srcu_clone_read_lock()
Posted by Peter Zijlstra 1 year, 7 months ago
On Tue, Jul 09, 2024 at 10:48:39PM -0700, Boqun Feng wrote:
> On Mon, Jul 08, 2024 at 11:12:49AM +0200, Peter Zijlstra wrote:
> > In order to support carrying an srcu_read_lock() section across fork,
> > where both the parent and child process will do: srcu_read_unlock(),
> > it is needed to account for the extra decrement with an extra
> > increment at fork time.
> > 
> 
> We also need to dup the per-task lock held stack in order to maintain
> consistent data for lockdep, right?

Urgh, not the whole stack, but yeah, we need to stick an entry on there.

Let me see if I can frob that somehow.
Re: [PATCH 08/10] srcu: Add __srcu_clone_read_lock()
Posted by Peter Zijlstra 1 year, 7 months ago
On Wed, Jul 10, 2024 at 12:02:02PM +0200, Peter Zijlstra wrote:
> On Tue, Jul 09, 2024 at 10:48:39PM -0700, Boqun Feng wrote:
> > On Mon, Jul 08, 2024 at 11:12:49AM +0200, Peter Zijlstra wrote:
> > > In order to support carrying an srcu_read_lock() section across fork,
> > > where both the parent and child process will do: srcu_read_unlock(),
> > > it is needed to account for the extra decrement with an extra
> > > increment at fork time.
> > > 
> > 
> > We also need to dup the per-task lock held stack in order to maintain
> > consistent data for lockdep, right?
> 
> Urgh, not the whole stack, but yeah, we need to stick an entry on there.
> 
> Let me see if I can frob that somehow.

Ah, since this all is across userspace, the easiest solution it so also
use __srcu_read_{,un}lock() and ignore lockdep entirely.