Skip to contentSkip to conversation

tipc: use lock_sock() in tipc_sk_reinit()

PATCH In review Cong Wang 3 participants 3 messages Dec 10, 2018 – Dec 14, 2018

Subsystems (3)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 tipc: use lock_sock() in tipc_sk_reinit()

lock_sock() must be used in process context to be race-free with other lock_sock() callers, for example, tipc_release(). Otherwise using the spinlock directly can't serialize a parallel tipc_release().

As it is blocking, we have to hold the sock refcnt before rhashtable_walk_stop() and release it after rhashtable_walk_start().

Fixes 07f6c4bc048a ("tipc: convert tipc reference table to use generic rhashtable")
Cc Ying Xue <[email protected]>
Cc Jon Maloy <[email protected]>
---
 net/tipc/socket.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/tipc/socket.c +6 -2
index b57b1be7252b..e1396fb87779 100644
@@ -2724,11 +2724,15 @@ void tipc_sk_reinit(struct net *net)
 		rhashtable_walk_start(&iter);
 
 		while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
-			spin_lock_bh(&tsk->sk.sk_lock.slock);
+			sock_hold(&tsk->sk);
+			rhashtable_walk_stop(&iter);
+			lock_sock(&tsk->sk);
 			msg = &tsk->phdr;
 			msg_set_prevnode(msg, tipc_own_addr(net));
 			msg_set_orignode(msg, tipc_own_addr(net));
-			spin_unlock_bh(&tsk->sk.sk_lock.slock);
+			release_sock(&tsk->sk);
+			rhashtable_walk_start(&iter);
+			sock_put(&tsk->sk);
 		}
 
 		rhashtable_walk_stop(&iter);
Signature · 1 line
2.19.2
#1 lore raw
Quoted 7 lines · Cong Wang

lock_sock() must be used in process context to be race-free with other lock_sock() callers, for example, tipc_release(). Otherwise using the spinlock directly can't serialize a parallel tipc_release().

As it is blocking, we have to hold the sock refcnt before rhashtable_walk_stop() and release it after rhashtable_walk_start().

Thanks for the quick fix!

Quoted 34 lines
Fixes 07f6c4bc048a ("tipc: convert tipc reference table to use generic rhashtable")
Cc Ying Xue <[email protected]>
Cc Jon Maloy <[email protected]>
---
 net/tipc/socket.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/tipc/socket.c +6 -3
index b57b1be7252b..e1396fb87779 100644
@@ -2724,11 +2724,15 @@ void tipc_sk_reinit(struct net *net)
                rhashtable_walk_start(&iter);
 
                while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
-                       spin_lock_bh(&tsk->sk.sk_lock.slock);
+                       sock_hold(&tsk->sk);
+                       rhashtable_walk_stop(&iter);
+                       lock_sock(&tsk->sk);
                        msg = &tsk->phdr;
                        msg_set_prevnode(msg, tipc_own_addr(net));
                        msg_set_orignode(msg, tipc_own_addr(net));
-                       spin_unlock_bh(&tsk->sk.sk_lock.slock);
+                       release_sock(&tsk->sk);
+                       rhashtable_walk_start(&iter);
+                       sock_put(&tsk->sk);
                }
 
                rhashtable_walk_stop(&iter);
--

2.19.2

maintainer
#2 lore raw
In reply to Cong Wang

From: Cong Wang <[email protected]>
Date: Mon, 10 Dec 2018 11:49:55 -0800

Quoted 4 lines

lock_sock() must be used in process context to be race-free with other lock_sock() callers, for example, tipc_release(). Otherwise using the spinlock directly can't serialize a parallel tipc_release().

Quoted 8 lines

As it is blocking, we have to hold the sock refcnt before rhashtable_walk_stop() and release it after rhashtable_walk_start().

Fixes 07f6c4bc048a ("tipc: convert tipc reference table to use generic rhashtable")
Cc Ying Xue <[email protected]>
Cc Jon Maloy <[email protected]>

Applied and queued up for -stable.