kcm: Fix two locking issues
Subsystems (3)
cover kcm: Fix two locking issues
One issue is lockdep warnings when sock_owned_by_user returns true in strparser. Fix is to add and call sock_owned_by_user_nocheck since the check for owned by user is not an error condition in this case.
The other issue is a potential deadlock between TX and RX paths
KCM socket lock and the psock socket lock are acquired in both the RX and TX path, however they take the locks in opposite order which can lead to deadlock. The fix is to add try_sock_lock to see if psock socket lock can get acquired in the TX path with KCM lock held. If not, then KCM socket is released and the psock socket lock and KCM socket lock are acquired in the same order as the RX path.
Tested:
Ran KCM traffic without incident.
Tom Herbert (4):
sock: Add sock_owned_by_user_nocheck
strparser: Call sock_owned_by_user_nocheck
sock_lock: Add try_sock_lock
kcm: Address deadlock between TX and RX paths
include/net/kcm.h | 1 + include/net/sock.h | 12 +++++++++ net/core/sock.c | 20 +++++++++++++++ net/kcm/kcmsock.c | 64 ++++++++++++++++++++++++++++++++++------------- net/strparser/strparser.c | 2 +- 5 files changed, 81 insertions(+), 18 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/4 sock: Add sock_owned_by_user_nocheck
This allows checking socket lock ownership with producing lockdep warnings.
--- include/net/sock.h | 5 +++++ 1 file changed, 5 insertions(+)
include/net/sock.h +5 -0
@@ -1515,6 +1515,11 @@ static inline bool sock_owned_by_user(const struct sock *sk)return sk->sk_lock.owned; } +static inline bool sock_owned_by_user_nocheck (const struct sock *sk) +{ +return sk->sk_lock.owned; +} +/* no reclassification while locks are held */ static inline bool sock_allow_reclassification (const struct sock *csk) {
Signature · 1 line
2.11.0
PATCH 2/4 strparser: Call sock_owned_by_user_nocheck
In reply to Tom Herbertstrparser wants to check socket ownership without producing any warnings. As indicated by the comment in the code, it is permissible for owned_by_user to return true.
--- net/strparser/strparser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/strparser/strparser.c +1 -1
@@ -401,7 +401,7 @@ void strp_data_ready(struct strparser *strp)* allows a thread in BH context to safely check if the process * lock is held. In this case, if the lock is held, queue work. */ -if (sock_owned_by_user (strp->sk)) { +if (sock_owned_by_user_nocheck (strp->sk)) {queue_work (strp_wq, &strp->work);return ; }
Signature · 1 line
2.11.0
PATCH 3/4 sock_lock: Add try_sock_lock
In reply to Tom Herberttry_sock lock is an opportunistic attempt to acquire a socket lock without blocking or sleeping. If the socket lock is acquired then true is returned, else false is returned.
--- include/net/sock.h | 7 +++++++ net/core/sock.c | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/net/sock.h +7 -0
@@ -1462,6 +1462,13 @@ static inline void lock_sock(struct sock *sk)lock_sock_nested (sk,0 ); } +bool try_lock_sock_nested (struct sock *sk,int subclass); + +static inline bool try_lock_sock (struct sock *sk) +{ +return try_lock_sock_nested (sk,0 ); +} +void release_sock (struct sock *sk);/* BH context may only use the following locking interface. */
net/core/sock.c +20 -0
@@ -2782,6 +2782,26 @@ void lock_sock_nested(struct sock *sk, int subclass) }EXPORT_SYMBOL (lock_sock_nested); +bool try_lock_sock_nested (struct sock *sk,int subclass) +{ +spin_lock_bh (&sk->sk_lock.slock); +if (sk->sk_lock.owned) { +spin_unlock_bh (&sk->sk_lock.slock); +return false; + } + + sk->sk_lock.owned =1 ; +spin_unlock (&sk->sk_lock.slock); + +/* The sk_lock has mutex_lock() semantics here: */ + +mutex_acquire (&sk->sk_lock.dep_map, subclass,0 , _RET_IP_); +local_bh_enable (); + +return true; +} +EXPORT_SYMBOL (try_lock_sock_nested); +void release_sock (struct sock *sk) {spin_lock_bh (&sk->sk_lock.slock);
Signature · 1 line
2.11.0
PATCH 4/4 kcm: Address deadlock between TX and RX paths
In reply to Tom HerbertBoth the transmit and receive paths of KCM need to take both the KCM socket lock and the psock socket lock, however they take the locks in opposite order which can lead to deadlock.
This patch changes the transmit path (kcm_write_msgs to be specific) so the locks are taken in the proper order. try_sock_lock is first used to get the lower socket lock, if that is successful then sending data can proceed with dropping KCM lock. If try_sock_lock fails then the KCM lock is released and lock_sock is done on the lower socket followed by the lock_sock on the KCM sock.
A doing_write_msgs flag has been added to kcm structure to prevent multiple threads doing write_msgs when the KCM lock is dropped. kernel_sendpage_locked is now called to do the send data with lock already held.
--- include/net/kcm.h | 1 + net/kcm/kcmsock.c | 64 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 17 deletions(-)
include/net/kcm.h +1 -0
@@ -78,6 +78,7 @@ struct kcm_sock {/* Don't use bit fields here, these are set under different locks */ bool tx_wait;bool tx_wait_more; +bool doing_write_msgs;/* Receive */ struct kcm_psock *rx_psock;
net/kcm/kcmsock.c +47 -17
110 lines not on this page · raw message
Signature · 1 line
2.11.0