Skip to contentSkip to conversation

rcu: mollify sparse with RCU guard

PATCH Applied Johannes Berg 3 participants 3 messages Mar 25, 2024 – Mar 26, 2024 RCU

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rcu: mollify sparse with RCU guard

From: Johannes Berg <[email protected]>

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.

---
v2: add a comment after discussion with Boqun

---
 include/linux/rcupdate.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/linux/rcupdate.h +13 -1
index 0746b1b0b663..6a3c52b3c180 100644
@@ -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 */
Signature · 1 line
2.44.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 20 lines · Johannes Berg

From: Johannes Berg <[email protected]>

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.

Quoted 5 lines

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.

Regards,
Boqun

Quoted 34 lines

---
v2: add a comment after discussion with Boqun

---
 include/linux/rcupdate.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/linux/rcupdate.h +13 -1
index 0746b1b0b663..6a3c52b3c180 100644
@@ -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 */
Signature · 1 line
2.44.0
maintainer Applied
#2 lore raw
Quoted 21 lines · Boqun Feng
Quoted 20 lines · Johannes Berg

From: Johannes Berg <[email protected]>

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.

Quoted 7 lines
Quoted 5 lines

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.

Queued, thank you both!

Thanx, Paul

Quoted 37 lines

Regards,
Boqun

Quoted 34 lines

---
v2: add a comment after discussion with Boqun

---
 include/linux/rcupdate.h | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
include/linux/rcupdate.h +13 -1
index 0746b1b0b663..6a3c52b3c180 100644
@@ -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 */
Signature · 1 line
2.44.0