public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] rcu: mollify sparse with RCU guard
@ 2024-03-25 17:39 Johannes Berg
  2024-03-25 17:41 ` Boqun Feng
  0 siblings, 1 reply; 3+ messages in thread
From: Johannes Berg @ 2024-03-25 17:39 UTC (permalink / raw)
  To: rcu
  Cc: linux-kernel, Paul E. McKenney, Frederic Weisbecker,
	Josh Triplett, Peter Zijlstra, Boqun Feng, Johannes Berg

From: Johannes Berg <johannes.berg@intel.com>

When using "guard(rcu)();" sparse will complain, because even
though it now understands the cleanup attribute, it doesn't
evaluate the calls from it at function exit, and thus doesn't
count the context correctly.

Given that there's a conditional in the resulting code:

  static inline void class_rcu_destructor(class_rcu_t *_T)
  {
      if (_T->lock) {
          rcu_read_unlock();
      }
  }

it seems that even trying to teach sparse to evalulate the
cleanup attribute function it'd still be difficult to really
make it understand the full context here.

Suppress the sparse warning by just releasing the context in
the acquisition part of the function, after all we know it's
safe with the guard, that's the whole point of it.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
v2: add a comment after discussion with Boqun

---
 include/linux/rcupdate.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 0746b1b0b663..6a3c52b3c180 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -1059,6 +1059,18 @@ rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
 extern int rcu_expedited;
 extern int rcu_normal;
 
-DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock())
+DEFINE_LOCK_GUARD_0(rcu,
+		    do {
+			rcu_read_lock();
+			/*
+			 * sparse doesn't call the cleanup function,
+			 * so just release immediately and don't track
+			 * the context. We don't need to anyway, since
+			 * the whole point of the guard is to not need
+			 * the explicit unlock.
+			 */
+			__release(RCU);
+		    } while(0),
+		    rcu_read_unlock())
 
 #endif /* __LINUX_RCUPDATE_H */
-- 
2.44.0


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

* Re: [PATCH v2] rcu: mollify sparse with RCU guard
  2024-03-25 17:39 [PATCH v2] rcu: mollify sparse with RCU guard Johannes Berg
@ 2024-03-25 17:41 ` Boqun Feng
  2024-03-26 17:21   ` Paul E. McKenney
  0 siblings, 1 reply; 3+ messages in thread
From: Boqun Feng @ 2024-03-25 17:41 UTC (permalink / raw)
  To: Johannes Berg
  Cc: rcu, linux-kernel, Paul E. McKenney, Frederic Weisbecker,
	Josh Triplett, Peter Zijlstra, Johannes Berg

On Mon, Mar 25, 2024 at 06:39:08PM +0100, Johannes Berg wrote:
> From: Johannes Berg <johannes.berg@intel.com>
> 
> When using "guard(rcu)();" sparse will complain, because even
> though it now understands the cleanup attribute, it doesn't
> evaluate the calls from it at function exit, and thus doesn't
> count the context correctly.
> 
> Given that there's a conditional in the resulting code:
> 
>   static inline void class_rcu_destructor(class_rcu_t *_T)
>   {
>       if (_T->lock) {
>           rcu_read_unlock();
>       }
>   }
> 
> it seems that even trying to teach sparse to evalulate the
> cleanup attribute function it'd still be difficult to really
> make it understand the full context here.
> 
> Suppress the sparse warning by just releasing the context in
> the acquisition part of the function, after all we know it's
> safe with the guard, that's the whole point of it.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>

Reviewed-by: Boqun Feng <boqun.feng@gmail.com>

Regards,
Boqun

> ---
> v2: add a comment after discussion with Boqun
> 
> ---
>  include/linux/rcupdate.h | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> index 0746b1b0b663..6a3c52b3c180 100644
> --- a/include/linux/rcupdate.h
> +++ b/include/linux/rcupdate.h
> @@ -1059,6 +1059,18 @@ rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
>  extern int rcu_expedited;
>  extern int rcu_normal;
>  
> -DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock())
> +DEFINE_LOCK_GUARD_0(rcu,
> +		    do {
> +			rcu_read_lock();
> +			/*
> +			 * sparse doesn't call the cleanup function,
> +			 * so just release immediately and don't track
> +			 * the context. We don't need to anyway, since
> +			 * the whole point of the guard is to not need
> +			 * the explicit unlock.
> +			 */
> +			__release(RCU);
> +		    } while(0),
> +		    rcu_read_unlock())
>  
>  #endif /* __LINUX_RCUPDATE_H */
> -- 
> 2.44.0
> 

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

* Re: [PATCH v2] rcu: mollify sparse with RCU guard
  2024-03-25 17:41 ` Boqun Feng
@ 2024-03-26 17:21   ` Paul E. McKenney
  0 siblings, 0 replies; 3+ messages in thread
From: Paul E. McKenney @ 2024-03-26 17:21 UTC (permalink / raw)
  To: Boqun Feng
  Cc: Johannes Berg, rcu, linux-kernel, Frederic Weisbecker,
	Josh Triplett, Peter Zijlstra, Johannes Berg

On Mon, Mar 25, 2024 at 10:41:36AM -0700, Boqun Feng wrote:
> On Mon, Mar 25, 2024 at 06:39:08PM +0100, Johannes Berg wrote:
> > From: Johannes Berg <johannes.berg@intel.com>
> > 
> > When using "guard(rcu)();" sparse will complain, because even
> > though it now understands the cleanup attribute, it doesn't
> > evaluate the calls from it at function exit, and thus doesn't
> > count the context correctly.
> > 
> > Given that there's a conditional in the resulting code:
> > 
> >   static inline void class_rcu_destructor(class_rcu_t *_T)
> >   {
> >       if (_T->lock) {
> >           rcu_read_unlock();
> >       }
> >   }
> > 
> > it seems that even trying to teach sparse to evalulate the
> > cleanup attribute function it'd still be difficult to really
> > make it understand the full context here.
> > 
> > Suppress the sparse warning by just releasing the context in
> > the acquisition part of the function, after all we know it's
> > safe with the guard, that's the whole point of it.
> > 
> > Signed-off-by: Johannes Berg <johannes.berg@intel.com>
> 
> Reviewed-by: Boqun Feng <boqun.feng@gmail.com>

Queued, thank you both!

							Thanx, Paul

> Regards,
> Boqun
> 
> > ---
> > v2: add a comment after discussion with Boqun
> > 
> > ---
> >  include/linux/rcupdate.h | 14 +++++++++++++-
> >  1 file changed, 13 insertions(+), 1 deletion(-)
> > 
> > diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> > index 0746b1b0b663..6a3c52b3c180 100644
> > --- a/include/linux/rcupdate.h
> > +++ b/include/linux/rcupdate.h
> > @@ -1059,6 +1059,18 @@ rcu_head_after_call_rcu(struct rcu_head *rhp, rcu_callback_t f)
> >  extern int rcu_expedited;
> >  extern int rcu_normal;
> >  
> > -DEFINE_LOCK_GUARD_0(rcu, rcu_read_lock(), rcu_read_unlock())
> > +DEFINE_LOCK_GUARD_0(rcu,
> > +		    do {
> > +			rcu_read_lock();
> > +			/*
> > +			 * sparse doesn't call the cleanup function,
> > +			 * so just release immediately and don't track
> > +			 * the context. We don't need to anyway, since
> > +			 * the whole point of the guard is to not need
> > +			 * the explicit unlock.
> > +			 */
> > +			__release(RCU);
> > +		    } while(0),
> > +		    rcu_read_unlock())
> >  
> >  #endif /* __LINUX_RCUPDATE_H */
> > -- 
> > 2.44.0
> > 

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

end of thread, other threads:[~2024-03-26 17:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-25 17:39 [PATCH v2] rcu: mollify sparse with RCU guard Johannes Berg
2024-03-25 17:41 ` Boqun Feng
2024-03-26 17:21   ` Paul E. McKenney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox