Skip to contentSkip to conversation

ovpn: fix incorrect use of rcu_access_pointer()

PATCH Superseded Qingfang Deng 3 participants 7 messages Jul 22 – Jul 24

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ovpn: fix incorrect use of rcu_access_pointer()

rcu_access_pointer() should only be used to test the value of a pointer, not to dereference it. Use rcu_dereference() instead. No extra rcu_read_lock() is needed here, as the spin_lock_bh() above marks an RCU critical section.

Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")
---
 drivers/net/ovpn/peer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ovpn/peer.c +1 -1
index a09d61296425..8de737579112 100644
@@ -1164,7 +1164,7 @@ static void ovpn_peer_release_p2p(struct ovpn_priv *ovpn, struct sock *sk,
 	}
 
 	if (sk) {
-		ovpn_sock = rcu_access_pointer(peer->sock);
+		ovpn_sock = rcu_dereference(peer->sock);
 		if (!ovpn_sock || ovpn_sock->sk != sk) {
 			spin_unlock_bh(&ovpn->lock);
 			ovpn_peer_put(peer);
Signature · 8 lines
2.43.0



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#1 lore raw

Hi!

Quoted 16 lines · Qingfang Deng

rcu_access_pointer() should only be used to test the value of a pointer, not to dereference it. Use rcu_dereference() instead. No extra rcu_read_lock() is needed here, as the spin_lock_bh() above marks an RCU critical section.

Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")
---
  drivers/net/ovpn/peer.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ovpn/peer.c +0 -0
index a09d61296425..8de737579112 100644

drivers/net/ovpn/peer.c static void ovpn_peer_release_p2p(struct ovpn_priv *ovpn, struct sock *sk, · line 1167

Quoted 5 lines
drivers/net/ovpn/peer.c +1 -1
@@ -1164,7 +1164,7 @@ static void ovpn_peer_release_p2p(struct ovpn_priv *ovpn, struct sock *sk,
  	}
 
  	if (sk) {
-		ovpn_sock = rcu_access_pointer(peer->sock);
+		ovpn_sock = rcu_dereference(peer->sock);

Sashiko says:

Using rcu_dereference() instead of rcu_dereference_bh() under spin_lock_bh() triggers a lockdep warning.

Signature · 8 lines
Antonio Quartulli



_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#2 lore raw

Hi,

Quoted 27 lines · Antonio Quartulli

Hi!

Quoted 16 lines · Qingfang Deng

rcu_access_pointer() should only be used to test the value of a pointer, not to dereference it. Use rcu_dereference() instead. No extra rcu_read_lock() is needed here, as the spin_lock_bh() above marks an RCU critical section.

Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")

---
  drivers/net/ovpn/peer.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ovpn/peer.c +0 -0
index a09d61296425..8de737579112 100644

drivers/net/ovpn/peer.c static void ovpn_peer_release_p2p(struct · line 1167

Quoted 5 lines
drivers/net/ovpn/peer.c +1 -1
@@ -1164,7 +1164,7 @@ static void ovpn_peer_release_p2p(struct 
 ovpn_priv *ovpn, struct sock *sk,
       }
         if (sk) {
-        ovpn_sock = rcu_access_pointer(peer->sock);
+        ovpn_sock = rcu_dereference(peer->sock);

Sashiko says:

Quoted 2 lines

Using rcu_dereference() instead of rcu_dereference_bh() under spin_lock_bh() triggers a lockdep warning.

I think rcu_dereference() has more relax checks than rcu_dereference_bh() and Sashiko is wrong, i.e. using rcu_dereference() in a BH-disabled context is okay, but using rcu_dereference_bh() in a process context with only rcu_read_lock() is _not_ okay. Can you verify that?

Best regards,

Qingfang

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

reviewer
#3 lore raw
Quoted 1 line · Qingfang Deng

Hi,

Hi Qingfang,

Quoted 28 lines · Antonio Quartulli
Quoted 27 lines

Hi!

Quoted 16 lines · Qingfang Deng

rcu_access_pointer() should only be used to test the value of a pointer, not to dereference it. Use rcu_dereference() instead. No extra rcu_read_lock() is needed here, as the spin_lock_bh() above marks an RCU critical section.

Fixes f6226ae7a0cd ("ovpn: introduce the ovpn_socket object")

---
  drivers/net/ovpn/peer.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ovpn/peer.c +0 -0
index a09d61296425..8de737579112 100644

drivers/net/ovpn/peer.c static void ovpn_peer_release_p2p(struct · line 1167

Quoted 5 lines
drivers/net/ovpn/peer.c +1 -1
@@ -1164,7 +1164,7 @@ static void ovpn_peer_release_p2p(struct 
 ovpn_priv *ovpn, struct sock *sk,
       }
         if (sk) {
-        ovpn_sock = rcu_access_pointer(peer->sock);
+        ovpn_sock = rcu_dereference(peer->sock);

Sashiko says:

Quoted 9 lines
Quoted 3 lines

Using rcu_dereference() instead of rcu_dereference_bh() under spin_lock_bh() triggers a lockdep warning.

I think rcu_dereference() has more relax checks than rcu_dereference_bh() and Sashiko is wrong, i.e. using rcu_dereference() in a BH-disabled context is okay, but using rcu_dereference_bh() in a process context with only rcu_read_lock() is _not_ okay. Can you verify that?

rcu_dereference relies on rcu_read_lock_held to check that the caller context is valid. However, ovpn_peer_release_p2p does not hold a normal rcu_read_lock section, it relies on the BH-disabled section established by spin_lock_bh instead.

rcu_read_lock_held does not consider BH-disabled context, so it will return false here. In contrast, rcu_read_lock_bh_held explicitly recognizes BH-disabled/softirq context.

So I think Sashiko is right: rcu_dereference will trigger its RCU lockdep warning with CONFIG_PROVE_RCU enabled. This should use rcu_dereference_bh instead.

Kind regards.

Signature · 8 lines
Ralf Lici
Mandelbit Srl


_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel
#4 lore raw

Hi Ralf,

Quoted 5 lines · Ralf Lici

rcu_dereference relies on rcu_read_lock_held to check that the caller context is valid. However, ovpn_peer_release_p2p does not hold a normal rcu_read_lock section, it relies on the BH-disabled section established by spin_lock_bh instead.

Quoted 7 lines

rcu_read_lock_held does not consider BH-disabled context, so it will return false here. In contrast, rcu_read_lock_bh_held explicitly recognizes BH-disabled/softirq context.

So I think Sashiko is right: rcu_dereference will trigger its RCU lockdep warning with CONFIG_PROVE_RCU enabled. This should use rcu_dereference_bh instead.

I read the code, and found that you are correct. I'll submit a new patch with your suggested change. Thank you.

Best regards,

Qingfang

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#5 lore raw

hi Qingfang,

When resending, please split the subject as "ovpn net" so that our patchwork can pick it up correctly.

Thanks a lot!

Il 24 Luglio 2026 03:20:51 CEST, Qingfang Deng <[email protected]> ha scritto:

Quoted 16 lines

Hi Ralf,

Quoted 5 lines · Ralf Lici

rcu_dereference relies on rcu_read_lock_held to check that the caller context is valid. However, ovpn_peer_release_p2p does not hold a normal rcu_read_lock section, it relies on the BH-disabled section established by spin_lock_bh instead.

Quoted 7 lines

rcu_read_lock_held does not consider BH-disabled context, so it will return false here. In contrast, rcu_read_lock_bh_held explicitly recognizes BH-disabled/softirq context.

So I think Sashiko is right: rcu_dereference will trigger its RCU lockdep warning with CONFIG_PROVE_RCU enabled. This should use rcu_dereference_bh instead.

Quoted 6 lines

I read the code, and found that you are correct. I'll submit a new patch with your suggested change. Thank you.

Best regards,

Qingfang

Antonio Quartulli
Edited on mobile, please excuse typos and brevity.

_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel