af_unix: Remove spin_lock_nested() and convert to lock_cmp_fn.
Subsystems (3)
v2 cover af_unix: Remove spin_lock_nested() and convert to lock_cmp_fn.
This series removes spin_lock_nested() in AF_UNIX and instead defines the locking orders as functions tied to each lock by lockdep_set_lock_cmp_fn().
When the defined function returns a negative value, lockdep considers it will not cause deadlock. (See ->cmp_fn() in check_deadlock() and check_prev_add().)
When we cannot define the total ordering, we return -1 for the allowed order and otherwise 0 as undefined. [0]
[0]: https://lore.kernel.org/netdev/thzkgbuwuo3knevpipu4rzsh5qgmwhklihypdgziiruabvh46f@uwdkpcfxgloo/
Kuniyuki Iwashima (11):
af_unix: Define locking order for unix_table_double_lock().
af_unix: Define locking order for U_LOCK_SECOND in
unix_state_double_lock().
af_unix: Don't retry after unix_state_lock_nested() in
unix_stream_connect().
af_unix: Define locking order for U_LOCK_SECOND in
unix_stream_connect().
af_unix: Don't acquire unix_state_lock() for sock_i_ino().
af_unix: Remove U_LOCK_DIAG.
af_unix: Remove U_LOCK_GC_LISTENER.
af_unix: Define locking order for U_RECVQ_LOCK_EMBRYO in
unix_collect_skb().
af_unix: Set sk_peer_pid/sk_peer_cred locklessly for new socket.
af_unix: Remove put_pid()/put_cred() in copy_peercred().
af_unix: Don't use spin_lock_nested() in copy_peercred().
include/net/af_unix.h | 14 ----- net/unix/af_unix.c | 136 +++++++++++++++++++++++++++--------------- net/unix/diag.c | 47 ++++----------- net/unix/garbage.c | 8 +-- 4 files changed, 101 insertions(+), 104 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 1/11 af_unix: Define locking order for unix_table_double_lock().
When created, AF_UNIX socket is put into net->unx.table.buckets[], and the hash is stored in sk->sk_hash.
* unbound socket : 0 <= sk_hash <= UNIX_HASH_MOD
When bind() is called, the socket could be moved to another bucket.
* pathname socket : 0 <= sk_hash <= UNIX_HASH_MOD
* abstract socket : UNIX_HASH_MOD + 1 <= sk_hash <= UNIX_HASH_MOD * 2 + 1
Then, we call unix_table_double_lock() which locks a single bucket or two.
Let's define the order as unix_table_lock_cmp_fn() instead of using spin_lock_nested().
The locking is always done in ascending order of sk->sk_hash, which is the index of buckets/locks array allocated by kvmalloc_array().
sk_hash_A < sk_hash_B
<=> &locks[sk_hash_A].dep_map < &locks[sk_hash_B].dep_map
So, the relation of two sk->sk_hash can be derived from the addresses of dep_map in the array of locks.
--- net/unix/af_unix.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
net/unix/af_unix.c +11 -1
@@ -126,6 +126,15 @@ static spinlock_t bsd_socket_locks[UNIX_HASH_SIZE / 2];* hash table is protected with spinlock. * each socket state is protected by separate spinlock. */ +#ifdef CONFIG_PROVE_LOCKING +#define cmp_ptr (l, r) (((l) > (r)) - ((l) < (r))) + +static int unix_table_lock_cmp_fn (const struct lockdep_map *a, +const struct lockdep_map *b) +{ +return cmp_ptr (a, b); +} +#endif static unsigned int unix_unbound_hash (struct sock *sk) {
@@ -168,7 +177,7 @@ static void unix_table_double_lock(struct net *net,swap (hash1, hash2);spin_lock (&net->unx.table.locks[hash1]); -spin_lock_nested (&net->unx.table.locks[hash2],SINGLE_DEPTH_NESTING ); +spin_lock (&net->unx.table.locks[hash2]); } static void unix_table_double_unlock(struct net *net,
@@ -3578,6 +3587,7 @@ static int __net_init unix_net_init(struct net *net)for (i =0 ; i <UNIX_HASH_SIZE ; i++) {spin_lock_init (&net->unx.table.locks[i]); +lock_set_cmp_fn (&net->unx.table.locks[i], unix_table_lock_cmp_fn,NULL );INIT_HLIST_HEAD (&net->unx.table.buckets[i]); }
Signature · 1 line
2.30.2
Re: [PATCH v2 net-next 01/11] af_unix: Define locking order for unix_table_double_lock().
Quoted 19 lines · Kuniyuki Iwashima
When created,
AF_UNIXsocket is put into net->unx.table.buckets[], and the hash is stored insk->sk_hash.* unbound socket : 0 <=
sk_hash<=UNIX_HASH_MODWhen
bind()is called, the socket could be moved to another bucket.* pathname socket : 0 <=
sk_hash<=UNIX_HASH_MOD
* abstract socket :UNIX_HASH_MOD+ 1 <=sk_hash<=UNIX_HASH_MOD* 2 + 1Then, we call
unix_table_double_lock()which locks a single bucket or two.Let's define the order as
unix_table_lock_cmp_fn()instead of usingspin_lock_nested().The locking is always done in ascending order of
sk->sk_hash, which is the index of buckets/locks array allocated bykvmalloc_array().
Quoted 7 lines
sk_hash_A<sk_hash_B
<=> &locks[sk_hash_A].dep_map < &locks[sk_hash_B].dep_mapSo, the relation of two
sk->sk_hashcan be derived from the addresses ofdep_mapin the array of locks.Signed-off-by Kuniyuki Iwashima <[email protected]>
Quoted 44 lines
--- net/unix/af_unix.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)net/unix/af_unix.c +11
-1@@ -126,6 +126,15 @@ static spinlock_t bsd_socket_locks[UNIX_HASH_SIZE / 2];* hash table is protected with spinlock. * each socket state is protected by separate spinlock. */ +#ifdef CONFIG_PROVE_LOCKING +#define cmp_ptr (l, r) (((l) > (r)) - ((l) < (r))) + +static int unix_table_lock_cmp_fn (const struct lockdep_map *a, +const struct lockdep_map *b) +{ +return cmp_ptr (a, b); +} +#endif static unsigned int unix_unbound_hash (struct sock *sk) {@@ -168,7 +177,7 @@ static void unix_table_double_lock(struct net *net,swap (hash1, hash2);spin_lock (&net->unx.table.locks[hash1]); -spin_lock_nested (&net->unx.table.locks[hash2],SINGLE_DEPTH_NESTING ); +spin_lock (&net->unx.table.locks[hash2]); } static void unix_table_double_unlock(struct net *net,@@ -3578,6 +3587,7 @@ static int __net_init unix_net_init(struct net *net)for (i =0 ; i <UNIX_HASH_SIZE ; i++) {spin_lock_init (&net->unx.table.locks[i]); +lock_set_cmp_fn (&net->unx.table.locks[i], unix_table_lock_cmp_fn,NULL );INIT_HLIST_HEAD (&net->unx.table.buckets[i]); }Signature · 1 line
2.30.2
PATCH v2 2/11 af_unix: Define locking order for U_LOCK_SECOND in unix_state_double_lock().
In reply to Kuniyuki Iwashimaunix_dgram_connect() and unix_dgram_{send,recv}msg() lock the socket and peer in ascending order of the socket address.
Let's define the order as unix_state_lock_cmp_fn() instead of using unix_state_lock_nested().
--- net/unix/af_unix.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
net/unix/af_unix.c +15 -1
@@ -134,6 +134,18 @@ static int unix_table_lock_cmp_fn(const struct lockdep_map *a, {return cmp_ptr (a, b); } + +static int unix_state_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct unix_sock *a, *b; + + a = container_of(_a, struct unix_sock, lock.dep_map); + b = container_of(_b, struct unix_sock, lock.dep_map); + +/* unix_state_double_lock(): ascending address order. */ +return cmp_ptr (a, b); +}#endif static unsigned int unix_unbound_hash (struct sock *sk)
@@ -987,6 +999,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, u->path.dentry =NULL ; u->path.mnt =NULL ;spin_lock_init (&u->lock); +lock_set_cmp_fn (&u->lock, unix_state_lock_cmp_fn,NULL );mutex_init (&u->iolock);/* single task reading lock */ mutex_init (&u->bindlock);/* single task binding lock */ init_waitqueue_head (&u->peer_wait);
@@ -1335,11 +1348,12 @@ static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)unix_state_lock (sk1);return ; } +if (sk1 > sk2)swap (sk1, sk2);unix_state_lock (sk1); -unix_state_lock_nested (sk2,U_LOCK_SECOND ); +unix_state_lock (sk2); }static void unix_state_double_unlock (struct sock *sk1,struct sock *sk2)
Signature · 1 line
2.30.2
Re: [PATCH v2 net-next 02/11] af_unix: Define locking order for U_LOCK_SECOND in unix_state_double_lock().
Quoted 7 lines · Kuniyuki Iwashima
unix_dgram_connect()and unix_dgram_{send,recv}msg() lock the socket and peer in ascending order of the socket address.Let's define the order as
unix_state_lock_cmp_fn()instead of usingunix_state_lock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>
Quoted 52 lines
--- net/unix/af_unix.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)net/unix/af_unix.c +15
-1@@ -134,6 +134,18 @@ static int unix_table_lock_cmp_fn(const struct lockdep_map *a, {return cmp_ptr (a, b); } + +static int unix_state_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct unix_sock *a, *b; + + a = container_of(_a, struct unix_sock, lock.dep_map); + b = container_of(_b, struct unix_sock, lock.dep_map); + +/* unix_state_double_lock(): ascending address order. */ +return cmp_ptr (a, b); +}#endif static unsigned int unix_unbound_hash (struct sock *sk)@@ -987,6 +999,7 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, u->path.dentry =NULL ; u->path.mnt =NULL ;spin_lock_init (&u->lock); +lock_set_cmp_fn (&u->lock, unix_state_lock_cmp_fn,NULL );mutex_init (&u->iolock);/* single task reading lock */ mutex_init (&u->bindlock);/* single task binding lock */ init_waitqueue_head (&u->peer_wait);@@ -1335,11 +1348,12 @@ static void unix_state_double_lock(struct sock *sk1, struct sock *sk2)unix_state_lock (sk1);return ; } +if (sk1 > sk2)swap (sk1, sk2);unix_state_lock (sk1); -unix_state_lock_nested (sk2,U_LOCK_SECOND ); +unix_state_lock (sk2); }static void unix_state_double_unlock (struct sock *sk1,struct sock *sk2)Signature · 1 line
2.30.2
PATCH v2 3/11 af_unix: Don't retry after unix_state_lock_nested() in unix_stream_connect().
In reply to Kuniyuki IwashimaWhen a SOCK_(STREAM|SEQPACKET) socket connect()s to another one, we need to lock the two sockets to check their states in unix_stream_connect().
We use unix_state_lock() for the server and unix_state_lock_nested() for client with tricky sk->sk_state check to avoid deadlock.
The possible deadlock scenario are the following:
1) Self connect()
2) Simultaneous connect()
The former is simple, attempt to grab the same lock, and the latter is AB-BA deadlock.
After the server's unix_state_lock(), we check the server socket's state, and if it's not TCP_LISTEN, connect() fails with -EINVAL.
Then, we avoid the former deadlock by checking the client's state before unix_state_lock_nested(). If its state is not TCP_LISTEN, we can make sure that the client and the server are not identical based on the state.
Also, the latter deadlock can be avoided in the same way. Due to the server sk->sk_state requirement, AB-BA deadlock could happen only with TCP_LISTEN sockets. So, if the client's state is TCP_LISTEN, we can give up the second lock to avoid the deadlock.
CPU 1 CPU 2 CPU 3
connect(A -> B) connect(B -> A) listen(A)
--- --- ---
unix_state_lock(B)
B->sk_state == TCP_LISTEN
READ_ONCE(A->sk_state) == TCP_CLOSE
^^^^^^^^^
ok, will lock A unix_state_lock(A)
.--------------' WRITE_ONCE(A->sk_state, TCP_LISTEN)
| unix_state_unlock(A)
|
| unix_state_lock(A)
| A->sk_sk_state == TCP_LISTEN
| READ_ONCE(B->sk_state) == TCP_LISTEN
v ^^^^^^^^^^ unix_state_lock_nested(A) Don't lock B !!
Currently, while checking the client's state, we also check if it's TCP_ESTABLISHED, but this is unlikely and can be checked after we know the state is not TCP_CLOSE.
Moreover, if it happens after the second lock, we now jump to the restart label, but it's unlikely that the server is not found during the retry, so the jump is mostly to revist the client state check.
Let's remove the retry logic and check the state against TCP_CLOSE first.
Note that sk->sk_state does not change once it's changed from TCP_CLOSE, so READ_ONCE() is not needed in the second state read in the first check.
--- net/unix/af_unix.c | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-)
net/unix/af_unix.c +8 -26
@@ -1546,7 +1546,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto out; } -/* Latch state of peer */ unix_state_lock (other);/* Apparently VFS overslept socket death. Retry. */
@@ -1576,37 +1575,20 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto restart; } -/* Latch our state. - -It is tricky place. We need to grab our state lock and cannot -drop lock on peer. It is dangerous because deadlock is -possible. Connect to self case and simultaneous -attempt to connect are eliminated by checking socket -state. other is TCP_LISTEN, if sk is TCP_LISTEN we -check this before attempt to grab lock. - -Well, and we have to recheck the state after socket locked. +/* self connect and simultaneous connect are eliminated +* by rejecting TCP_LISTEN socket to avoid deadlock. */ -switch (READ_ONCE (sk->sk_state)) { -case TCP_CLOSE : -/* This is ok... continue with connect */ -break ; -case TCP_ESTABLISHED : -/* Socket is already connected */ - err = -EISCONN ; - goto out_unlock; -default : - err = -EINVAL ; +if (unlikely (READ_ONCE (sk->sk_state) !=TCP_CLOSE )) { + err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ; goto out_unlock; }unix_state_lock_nested (sk,U_LOCK_SECOND ); -if (sk->sk_state !=TCP_CLOSE ) { -unix_state_unlock (sk); -unix_state_unlock (other); -sock_put (other); - goto restart; +if (unlikely (sk->sk_state !=TCP_CLOSE )) { + err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ; +unix_state_lock (sk); + goto out_unlock; } err =security_unix_stream_connect (sk, other, newsk);
Signature · 1 line
2.30.2
Paolo Abeni
maintainerRe: [PATCH v2 net-next 03/11] af_unix: Don't retry after unix_state_lock_nested() in unix_stream_connect().
Quoted 92 lines · Kuniyuki Iwashima
When a
SOCK_(STREAM|SEQPACKET)socket connect()s to another one, we need to lock the two sockets to check their states inunix_stream_connect().We use
unix_state_lock()for the server andunix_state_lock_nested()for client with trickysk->sk_statecheck to avoid deadlock.The possible deadlock scenario are the following:
1) Self
connect()
2) Simultaneousconnect()The former is simple, attempt to grab the same lock, and the latter is AB-BA deadlock.
After the server's
unix_state_lock(), we check the server socket's state, and if it's notTCP_LISTEN,connect()fails with-EINVAL.Then, we avoid the former deadlock by checking the client's state before
unix_state_lock_nested(). If its state is notTCP_LISTEN, we can make sure that the client and the server are not identical based on the state.Also, the latter deadlock can be avoided in the same way. Due to the server
sk->sk_staterequirement, AB-BA deadlock could happen only withTCP_LISTENsockets. So, if the client's state isTCP_LISTEN, we can give up the second lock to avoid the deadlock.CPU 1 CPU 2 CPU 3 connect(A -> B) connect(B -> A) listen(A) --- --- --- unix_state_lock(B) B->sk_state == TCP_LISTEN READ_ONCE(A->sk_state) == TCP_CLOSE ^^^^^^^^^ ok, will lock A unix_state_lock(A) .--------------' WRITE_ONCE(A->sk_state, TCP_LISTEN) | unix_state_unlock(A) | | unix_state_lock(A) | A->sk_sk_state == TCP_LISTEN | READ_ONCE(B->sk_state) == TCP_LISTEN v ^^^^^^^^^^
unix_state_lock_nested(A)Don't lock B !!Currently, while checking the client's state, we also check if it's
TCP_ESTABLISHED, but this is unlikely and can be checked after we know the state is notTCP_CLOSE.Moreover, if it happens after the second lock, we now jump to the restart label, but it's unlikely that the server is not found during the retry, so the jump is mostly to revist the client state check.
Let's remove the retry logic and check the state against
TCP_CLOSEfirst.Note that
sk->sk_statedoes not change once it's changed fromTCP_CLOSE, soREAD_ONCE()is not needed in the second state read in the first check.Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/unix/af_unix.c | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-)net/unix/af_unix.c +2
-13@@ -1546,7 +1546,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto out; } -/* Latch state of peer */ unix_state_lock (other);/* Apparently VFS overslept socket death. Retry. */ @@ -1576,37 +1575,20 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto restart; } -/* Latch our state. - -It is tricky place. We need to grab our state lock and cannot -drop lock on peer. It is dangerous because deadlock is -possible. Connect to self case and simultaneous -attempt to connect are eliminated by checking socket -state. other is TCP_LISTEN, if sk is TCP_LISTEN we -check this before attempt to grab lock. - -Well, and we have to recheck the state after socket locked. +/* self connect and simultaneous connect are eliminated +* by rejecting TCP_LISTEN socket to avoid deadlock. */ -switch (READ_ONCE (sk->sk_state)) { -case TCP_CLOSE :
net/unix/af_unix.c static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, · line 1582
Quoted 10 lines
net/unix/af_unix.c +2
-8-/* This is ok... continue with connect */ -break ; -case TCP_ESTABLISHED : -/* Socket is already connected */ - err = -EISCONN ; - goto out_unlock; -default : - err = -EINVAL ; +if (unlikely (READ_ONCE (sk->sk_state) !=TCP_CLOSE )) { + err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ;
I find the mixed READ_ONCE()/plain read confusing. What about using a single READ_ONCE() caching the return value?
net/unix/af_unix.c static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, · line 1590
Quoted 13 lines
net/unix/af_unix.c +3
-5goto out_unlock; }unix_state_lock_nested (sk,U_LOCK_SECOND ); -if (sk->sk_state !=TCP_CLOSE ) { -unix_state_unlock (sk); -unix_state_unlock (other); -sock_put (other); - goto restart; +if (unlikely (sk->sk_state !=TCP_CLOSE )) { + err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ; +unix_state_lock (sk);
Should likely be:
unix_state_unlock(sk)
?
Thanks!
Paolo
Re: [PATCH v2 net-next 03/11] af_unix: Don't retry after unix_state_lock_nested() in unix_stream_connect().
From: Paolo Abeni <[email protected]>
Date: Fri, 14 Jun 2024 12:49:35 +0200
Quoted 98 lines · Kuniyuki Iwashima
Quoted 97 lines
When a
SOCK_(STREAM|SEQPACKET)socket connect()s to another one, we need to lock the two sockets to check their states inunix_stream_connect().We use
unix_state_lock()for the server andunix_state_lock_nested()for client with trickysk->sk_statecheck to avoid deadlock.The possible deadlock scenario are the following:
1) Self
connect()
2) Simultaneousconnect()The former is simple, attempt to grab the same lock, and the latter is AB-BA deadlock.
After the server's
unix_state_lock(), we check the server socket's state, and if it's notTCP_LISTEN,connect()fails with-EINVAL.Then, we avoid the former deadlock by checking the client's state before
unix_state_lock_nested(). If its state is notTCP_LISTEN, we can make sure that the client and the server are not identical based on the state.Also, the latter deadlock can be avoided in the same way. Due to the server
sk->sk_staterequirement, AB-BA deadlock could happen only withTCP_LISTENsockets. So, if the client's state isTCP_LISTEN, we can give up the second lock to avoid the deadlock.CPU 1 CPU 2 CPU 3 connect(A -> B) connect(B -> A) listen(A) --- --- --- unix_state_lock(B) B->sk_state == TCP_LISTEN READ_ONCE(A->sk_state) == TCP_CLOSE ^^^^^^^^^ ok, will lock A unix_state_lock(A) .--------------' WRITE_ONCE(A->sk_state, TCP_LISTEN) | unix_state_unlock(A) | | unix_state_lock(A) | A->sk_sk_state == TCP_LISTEN | READ_ONCE(B->sk_state) == TCP_LISTEN v ^^^^^^^^^^
unix_state_lock_nested(A)Don't lock B !!Currently, while checking the client's state, we also check if it's
TCP_ESTABLISHED, but this is unlikely and can be checked after we know the state is notTCP_CLOSE.Moreover, if it happens after the second lock, we now jump to the restart label, but it's unlikely that the server is not found during the retry, so the jump is mostly to revist the client state check.
Let's remove the retry logic and check the state against
TCP_CLOSEfirst.Note that
sk->sk_statedoes not change once it's changed fromTCP_CLOSE, soREAD_ONCE()is not needed in the second state read in the first check.Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/unix/af_unix.c | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-)net/unix/af_unix.c +2
-18@@ -1546,7 +1546,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto out; } -/* Latch state of peer */ unix_state_lock (other);/* Apparently VFS overslept socket death. Retry. */ @@ -1576,37 +1575,20 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto restart; } -/* Latch our state. - -It is tricky place. We need to grab our state lock and cannot -drop lock on peer. It is dangerous because deadlock is -possible. Connect to self case and simultaneous -attempt to connect are eliminated by checking socket -state. other is TCP_LISTEN, if sk is TCP_LISTEN we -check this before attempt to grab lock. - -Well, and we have to recheck the state after socket locked. +/* self connect and simultaneous connect are eliminated +* by rejecting TCP_LISTEN socket to avoid deadlock. */ -switch (READ_ONCE (sk->sk_state)) { -case TCP_CLOSE : -/* This is ok... continue with connect */ -break ; -case TCP_ESTABLISHED : -/* Socket is already connected */ - err = -EISCONN ;
Quoted 8 lines
net/unix/af_unix.c static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, · line 1582
Quoted 5 lines
net/unix/af_unix.c +2
-3- goto out_unlock; -default : - err = -EINVAL ; +if (unlikely (READ_ONCE (sk->sk_state) !=TCP_CLOSE )) { + err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ;I find the mixed READ_ONCE()/plain read confusing. What about using a single
READ_ONCE()caching the return value?
Will use cached sk_state.
Quoted 6 lines
Quoted 6 lines
net/unix/af_unix.c +0
-0goto out_unlock; }unix_state_lock_nested (sk,U_LOCK_SECOND );
Quoted 12 lines
net/unix/af_unix.c static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, · line 1590
Quoted 8 lines
net/unix/af_unix.c +3
-5-if (sk->sk_state !=TCP_CLOSE ) { -unix_state_unlock (sk); -unix_state_unlock (other); -sock_put (other); - goto restart; +if (unlikely (sk->sk_state !=TCP_CLOSE )) { + err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ; +unix_state_lock (sk);Should likely be:
unix_state_unlock(sk)
?
Oops, will fix it.
Thanks!
PATCH v2 4/11 af_unix: Define locking order for U_LOCK_SECOND in unix_stream_connect().
In reply to Kuniyuki IwashimaWhile a SOCK_(STREAM|SEQPACKET) socket connect()s to another, we hold two locks of them by unix_state_lock() and unix_state_lock_nested() in unix_stream_connect().
Before unix_state_lock_nested(), the following is guaranteed by checking sk->sk_state:
1. The first socket is TCP_LISTEN
2. The second socket is not the first one
3. Simultaneous connect() must fail
So, the client state can be TCP_CLOSE or TCP_LISTEN or TCP_ESTABLISHED.
Let's define the expected states as unix_state_lock_cmp_fn() instead of using unix_state_lock_nested().
Note that 2. is detected by debug_spin_lock_before() and 3. cannot be expressed as lock_cmp_fn.
--- include/net/af_unix.h | 1 - net/unix/af_unix.c | 26 +++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-)
include/net/af_unix.h +0 -1
@@ -98,7 +98,6 @@ struct unix_sock {#define unix_state_unlock (s) spin_unlock(&unix_sk(s)->lock)enum unix_socket_lock_class {U_LOCK_NORMAL , -U_LOCK_SECOND ,/* for double locking, see unix_state_double_lock(). */ U_LOCK_DIAG ,/* used while dumping icons, see sk_diag_dump_icons(). */ U_LOCK_GC_LISTENER ,/* used for listening socket while determining gc * candidates to close a small race window.
net/unix/af_unix.c +25 -1
@@ -143,6 +143,30 @@ static int unix_state_lock_cmp_fn(const struct lockdep_map *_a, a = container_of(_a, struct unix_sock, lock.dep_map); b = container_of(_b, struct unix_sock, lock.dep_map); +if (a->sk.sk_state ==TCP_LISTEN ) { +/* unix_stream_connect(): Before the 2nd unix_state_lock(), +* +* 1. a is TCP_LISTEN. +* 2. b is not a. +* 3. concurrent connect(b -> a) must fail. +* +* Except for 2. & 3., the b's state can be any possible +* value due to concurrent connect() or listen(). +* +* 2. is detected in debug_spin_lock_before(), and 3. cannot +* be expressed as lock_cmp_fn. +*/ +switch (b->sk.sk_state) { +case TCP_CLOSE : +case TCP_ESTABLISHED : +case TCP_LISTEN : +return -1 ; +default : +/* Invalid case. */ +return 0 ; + } + } +/* unix_state_double_lock(): ascending address order. */ return cmp_ptr (a, b); }
@@ -1583,7 +1607,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, goto out_unlock; } -unix_state_lock_nested (sk,U_LOCK_SECOND ); +unix_state_lock (sk);if (unlikely (sk->sk_state !=TCP_CLOSE )) { err = sk->sk_state ==TCP_ESTABLISHED ? -EISCONN : -EINVAL ;
Signature · 1 line
2.30.2
PATCH v2 5/11 af_unix: Don't acquire unix_state_lock() for sock_i_ino().
In reply to Kuniyuki Iwashimask_diag_dump_peer() and sk_diag_dump() call unix_state_lock() for sock_i_ino() which reads SOCK_INODE(sk->sk_socket)->i_ino, but it's protected by sk->sk_callback_lock.
Let's remove unnecessary unix_state_lock().
--- net/unix/diag.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-)
net/unix/diag.c +10 -20
@@ -47,9 +47,7 @@ static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb) peer =unix_peer_get (sk);if (peer) { -unix_state_lock (peer); ino =sock_i_ino (peer); -unix_state_unlock (peer);sock_put (peer);return nla_put_u32 (nlskb,UNIX_DIAG_PEER , ino);
@@ -180,22 +178,6 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_rreturn -EMSGSIZE ; } -static int sk_diag_dump (struct sock *sk,struct sk_buff *skb,struct unix_diag_req *req, -struct user_namespace *user_ns, -u32 portid,u32 seq,u32 flags) -{ -int sk_ino; - -unix_state_lock (sk); - sk_ino =sock_i_ino (sk); -unix_state_unlock (sk); - -if (!sk_ino) -return 0 ; - -return sk_diag_fill (sk, skb, req, user_ns, portid, seq, flags, sk_ino); -} -static int unix_diag_dump (struct sk_buff *skb,struct netlink_callback *cb) {struct net *net =sock_net (skb->sk);
@@ -213,14 +195,22 @@ static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) num =0 ;spin_lock (&net->unx.table.locks[slot]);sk_for_each (sk, &net->unx.table.buckets[slot]) { +int sk_ino; +if (num < s_num) goto next; +if (!(req->udiag_states & (1 <<READ_ONCE (sk->sk_state)))) goto next; -if (sk_diag_dump (sk, skb, req,sk_user_ns (skb->sk), + + sk_ino =sock_i_ino (sk); +if (!sk_ino) + goto next; + +if (sk_diag_fill (sk, skb, req,sk_user_ns (skb->sk),NETLINK_CB (cb->skb).portid, cb->nlh->nlmsg_seq, -NLM_F_MULTI ) <0 ) { +NLM_F_MULTI , sk_ino) <0 ) {spin_unlock (&net->unx.table.locks[slot]); goto done; }
Signature · 1 line
2.30.2
PATCH v2 6/11 af_unix: Remove U_LOCK_DIAG.
In reply to Kuniyuki Iwashimask_diag_dump_icons() acquires embryo's lock by unix_state_lock_nested() to fetch its peer.
The embryo's ->peer is set to NULL only when its parent listener is close()d. Then, unix_release_sock() is called for each embryo after unlinking skb by skb_dequeue().
In sk_diag_dump_icons(), we hold the parent's recvq lock, so we need not acquire unix_state_lock_nested(), and peer is always non-NULL.
Let's remove unnecessary unix_state_lock_nested() and non-NULL test for peer.
--- include/net/af_unix.h | 1 - net/unix/diag.c | 17 +++-------------- 2 files changed, 3 insertions(+), 15 deletions(-)
include/net/af_unix.h +0 -1
@@ -98,7 +98,6 @@ struct unix_sock {#define unix_state_unlock (s) spin_unlock(&unix_sk(s)->lock)enum unix_socket_lock_class {U_LOCK_NORMAL , -U_LOCK_DIAG ,/* used while dumping icons, see sk_diag_dump_icons(). */ U_LOCK_GC_LISTENER ,/* used for listening socket while determining gc * candidates to close a small race window. */
net/unix/diag.c +3 -14
@@ -73,20 +73,9 @@ static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb) buf =nla_data (attr); i =0 ; - skb_queue_walk(&sk->sk_receive_queue, skb) { -struct sock *req, *peer; - - req = skb->sk; -/* -* The state lock is outer for the same sk's -* queue lock. With the other's queue locked it's -* OK to lock the state. -*/ -unix_state_lock_nested (req,U_LOCK_DIAG ); - peer =unix_sk (req)->peer; - buf[i++] = (peer ?sock_i_ino (peer) :0 ); -unix_state_unlock (req); - } +skb_queue_walk (&sk->sk_receive_queue, skb) + buf[i++] = sock_i_ino(unix_peer(skb->sk)); +spin_unlock (&sk->sk_receive_queue.lock); }
Signature · 1 line
2.30.2
PATCH v2 7/11 af_unix: Remove U_LOCK_GC_LISTENER.
In reply to Kuniyuki IwashimaCommit 1971d13ffa84 ("af_unix: Suppress false-positive lockdep splat for spin_lock() in __unix_gc().") added U_LOCK_GC_LISTENER for the old GC, but it's no longer needed for the new GC.
Let's remove U_LOCK_GC_LISTENER and unix_state_lock_nested() as there's no user.
--- include/net/af_unix.h | 12 ------------ 1 file changed, 12 deletions(-)
include/net/af_unix.h +0 -12
@@ -96,18 +96,6 @@ struct unix_sock {#define unix_state_lock (s) spin_lock(&unix_sk(s)->lock)#define unix_state_unlock (s) spin_unlock(&unix_sk(s)->lock) -enum unix_socket_lock_class { -U_LOCK_NORMAL , -U_LOCK_GC_LISTENER ,/* used for listening socket while determining gc -* candidates to close a small race window. -*/ -}; - -static inline void unix_state_lock_nested (struct sock *sk, -enum unix_socket_lock_class subclass) -{ -spin_lock_nested (&unix_sk (sk)->lock, subclass); -}#define peer_wait peer_wq.wait
Signature · 1 line
2.30.2
PATCH v2 8/11 af_unix: Define locking order for U_RECVQ_LOCK_EMBRYO in unix_collect_skb().
In reply to Kuniyuki IwashimaWhile GC is cleaning up cyclic references by SCM_RIGHTS, unix_collect_skb() collects skb in the socket's recvq.
If the socket is TCP_LISTEN, we need to collect skb in the embryo's queue. Then, both the listener's recvq lock and the embroy's one are held.
The locking is always done in the listener -> embryo order.
Let's define it as unix_recvq_lock_cmp_fn() instead of using spin_lock_nested().
--- net/unix/af_unix.c | 17 +++++++++++++++++ net/unix/garbage.c | 8 +------- 2 files changed, 18 insertions(+), 7 deletions(-)
net/unix/af_unix.c +17 -0
@@ -170,6 +170,21 @@ static int unix_state_lock_cmp_fn(const struct lockdep_map *_a,/* unix_state_double_lock(): ascending address order. */ return cmp_ptr (a, b); } + +static int unix_recvq_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct sock *a, *b; + + a = container_of(_a, struct sock, sk_receive_queue.lock.dep_map); + b = container_of(_b, struct sock, sk_receive_queue.lock.dep_map); + +/* unix_collect_skb(): listener -> embryo order. */ +if (a->sk_state ==TCP_LISTEN &&unix_sk (b)->listener == a) +return -1 ; + +return 0 ; +}#endif static unsigned int unix_unbound_hash (struct sock *sk)
@@ -1017,6 +1032,8 @@ static struct sock *unix_create1(struct net *net, struct socket *sock, int kern, sk->sk_write_space = unix_write_space; sk->sk_max_ack_backlog =READ_ONCE (net->unx.sysctl_max_dgram_qlen); sk->sk_destruct = unix_sock_destructor; +lock_set_cmp_fn (&sk->sk_receive_queue.lock, unix_recvq_lock_cmp_fn,NULL ); + u =unix_sk (sk); u->listener =NULL ; u->vertex =NULL ;
net/unix/garbage.c +1 -7
@@ -337,11 +337,6 @@ static bool unix_vertex_dead(struct unix_vertex *vertex)return true; } -enum unix_recv_queue_lock_class { -U_RECVQ_LOCK_NORMAL , -U_RECVQ_LOCK_EMBRYO , -}; -static void unix_collect_queue (struct unix_sock *u,struct sk_buff_head *hitlist) {skb_queue_splice_init (&u->sk.sk_receive_queue, hitlist);
@@ -375,8 +370,7 @@ static void unix_collect_skb(struct list_head *scc, struct sk_buff_head *hitlist skb_queue_walk(queue, skb) {struct sk_buff_head *embryo_queue = &skb->sk->sk_receive_queue; -/* listener -> embryo order, the inversion never happens. */ -spin_lock_nested (&embryo_queue->lock,U_RECVQ_LOCK_EMBRYO ); +spin_lock (&embryo_queue->lock);unix_collect_queue (unix_sk (skb->sk), hitlist);spin_unlock (&embryo_queue->lock); }
Signature · 1 line
2.30.2
Re: [PATCH v2 net-next 08/11] af_unix: Define locking order for U_RECVQ_LOCK_EMBRYO in unix_collect_skb().
Quoted 32 lines · Kuniyuki Iwashima
While GC is cleaning up cyclic references by
SCM_RIGHTS,unix_collect_skb()collects skb in the socket's recvq.If the socket is
TCP_LISTEN, we need to collect skb in the embryo's queue. Then, both the listener's recvq lock and the embroy's one are held.The locking is always done in the listener -> embryo order.
Let's define it as
unix_recvq_lock_cmp_fn()instead of usingspin_lock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/unix/af_unix.c | 17 +++++++++++++++++ net/unix/garbage.c | 8 +------- 2 files changed, 18 insertions(+), 7 deletions(-)net/unix/af_unix.c +6
-0@@ -170,6 +170,21 @@ static int unix_state_lock_cmp_fn(const struct lockdep_map *_a,/* unix_state_double_lock(): ascending address order. */ return cmp_ptr (a, b); } + +static int unix_recvq_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct sock *a, *b; +
net/unix/af_unix.c static int unix_state_lock_cmp_fn(const struct lockdep_map *_a, · line 188
Quoted 10 lines
net/unix/af_unix.c +9
-0+ a = container_of(_a, struct sock, sk_receive_queue.lock.dep_map); + b = container_of(_b, struct sock, sk_receive_queue.lock.dep_map); + +/* unix_collect_skb(): listener -> embryo order. */ +if (a->sk_state ==TCP_LISTEN &&unix_sk (b)->listener == a) +return -1 ; + + return 0; +}#endif
That's not symmetric.
Re: [PATCH v2 net-next 08/11] af_unix: Define locking order for U_RECVQ_LOCK_EMBRYO in unix_collect_skb().
From: Kent Overstreet <[email protected]>
Date: Tue, 11 Jun 2024 19:17:53 -0400
Quoted 37 lines · Kuniyuki Iwashima
Quoted 36 lines
While GC is cleaning up cyclic references by
SCM_RIGHTS,unix_collect_skb()collects skb in the socket's recvq.If the socket is
TCP_LISTEN, we need to collect skb in the embryo's queue. Then, both the listener's recvq lock and the embroy's one are held.The locking is always done in the listener -> embryo order.
Let's define it as
unix_recvq_lock_cmp_fn()instead of usingspin_lock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/unix/af_unix.c | 17 +++++++++++++++++ net/unix/garbage.c | 8 +------- 2 files changed, 18 insertions(+), 7 deletions(-)net/unix/af_unix.c +10
-0@@ -170,6 +170,21 @@ static int unix_state_lock_cmp_fn(const struct lockdep_map *_a,/* unix_state_double_lock(): ascending address order. */ return cmp_ptr (a, b); } + +static int unix_recvq_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct sock *a, *b; + + a = container_of(_a, struct sock, sk_receive_queue.lock.dep_map); + b = container_of(_b, struct sock, sk_receive_queue.lock.dep_map); + +/* unix_collect_skb(): listener -> embryo order. */
Quoted 8 lines
net/unix/af_unix.c static int unix_state_lock_cmp_fn(const struct lockdep_map *_a, · line 188
Quoted 6 lines
net/unix/af_unix.c +5
-0+if (a->sk_state ==TCP_LISTEN &&unix_sk (b)->listener == a) +return -1 ; + + return 0; +}#endif That's not symmetric.
I think we agreed this is allowed, no ?
https://lore.kernel.org/netdev/thzkgbuwuo3knevpipu4rzsh5qgmwhklihypdgziiruabvh46f@uwdkpcfxgloo/
Paolo Abeni
maintainerRe: [PATCH v2 net-next 08/11] af_unix: Define locking order for U_RECVQ_LOCK_EMBRYO in unix_collect_skb().
Quoted 48 lines · Kuniyuki Iwashima
From: Kent Overstreet <[email protected]>
Date: Tue, 11 Jun 2024 19:17:53 -0400Quoted 37 lines · Kuniyuki Iwashima
Quoted 36 lines
While GC is cleaning up cyclic references by
SCM_RIGHTS,unix_collect_skb()collects skb in the socket's recvq.If the socket is
TCP_LISTEN, we need to collect skb in the embryo's queue. Then, both the listener's recvq lock and the embroy's one are held.The locking is always done in the listener -> embryo order.
Let's define it as
unix_recvq_lock_cmp_fn()instead of usingspin_lock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/unix/af_unix.c | 17 +++++++++++++++++ net/unix/garbage.c | 8 +------- 2 files changed, 18 insertions(+), 7 deletions(-)net/unix/af_unix.c +10
-0@@ -170,6 +170,21 @@ static int unix_state_lock_cmp_fn(const struct lockdep_map *_a,/* unix_state_double_lock(): ascending address order. */ return cmp_ptr (a, b); } + +static int unix_recvq_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct sock *a, *b; + + a = container_of(_a, struct sock, sk_receive_queue.lock.dep_map); + b = container_of(_b, struct sock, sk_receive_queue.lock.dep_map); + +/* unix_collect_skb(): listener -> embryo order. */ Quoted 8 lines
net/unix/af_unix.c static int unix_state_lock_cmp_fn(const struct lockdep_map *_a, · line 188
Quoted 6 lines
net/unix/af_unix.c +5
-0+if (a->sk_state ==TCP_LISTEN &&unix_sk (b)->listener == a) +return -1 ; + + return 0; +}#endif That's not symmetric.
Quoted 3 lines
I think we agreed this is allowed, no ?
https://lore.kernel.org/netdev/thzkgbuwuo3knevpipu4rzsh5qgmwhklihypdgziiruabvh46f@uwdkpcfxgloo/
My understanding of such thread is that you should return 1 for the embryo -> listener order (for consistency). You can keep returning 0 for all the other 'undefined' cases.
Thanks!
Paolo
Re: [PATCH v2 net-next 08/11] af_unix: Define locking order for U_RECVQ_LOCK_EMBRYO in unix_collect_skb().
From: Paolo Abeni <[email protected]>
Date: Fri, 14 Jun 2024 13:04:06 +0200
Quoted 49 lines · Kuniyuki Iwashima
Quoted 48 lines
From: Kent Overstreet <[email protected]>
Date: Tue, 11 Jun 2024 19:17:53 -0400Quoted 45 lines · Kuniyuki Iwashima
Quoted 32 lines
While GC is cleaning up cyclic references by
SCM_RIGHTS,unix_collect_skb()collects skb in the socket's recvq.If the socket is
TCP_LISTEN, we need to collect skb in the embryo's queue. Then, both the listener's recvq lock and the embroy's one are held.The locking is always done in the listener -> embryo order.
Let's define it as
unix_recvq_lock_cmp_fn()instead of usingspin_lock_nested().Signed-off-by Kuniyuki Iwashima <[email protected]>--- net/unix/af_unix.c | 17 +++++++++++++++++ net/unix/garbage.c | 8 +------- 2 files changed, 18 insertions(+), 7 deletions(-)net/unix/af_unix.c +6
-0@@ -170,6 +170,21 @@ static int unix_state_lock_cmp_fn(const struct lockdep_map *_a,/* unix_state_double_lock(): ascending address order. */ return cmp_ptr (a, b); } + +static int unix_recvq_lock_cmp_fn (const struct lockdep_map *_a, +const struct lockdep_map *_b) +{ +const struct sock *a, *b; +net/unix/af_unix.c static int unix_state_lock_cmp_fn(const struct lockdep_map *_a, · line 188
Quoted 10 lines
net/unix/af_unix.c +9
-0+ a = container_of(_a, struct sock, sk_receive_queue.lock.dep_map); + b = container_of(_b, struct sock, sk_receive_queue.lock.dep_map); + +/* unix_collect_skb(): listener -> embryo order. */ +if (a->sk_state ==TCP_LISTEN &&unix_sk (b)->listener == a) +return -1 ; + + return 0; +}#endif That's not symmetric.
Quoted 7 lines
Quoted 3 lines
I think we agreed this is allowed, no ?
https://lore.kernel.org/netdev/thzkgbuwuo3knevpipu4rzsh5qgmwhklihypdgziiruabvh46f@uwdkpcfxgloo/
My understanding of such thread is that you should return 1 for the embryo -> listener order (for consistency). You can keep returning 0 for all the other 'undefined' cases.
Ah, I understood. Will do so in v3.
Thanks!
PATCH v2 9/11 af_unix: Set sk_peer_pid/sk_peer_cred locklessly for new socket.
In reply to Kuniyuki Iwashimainit_peercred() is called in 3 places:
1. socketpair() : both sockets
2. connect() : child socket
3. listen() : listening socket
The first two need not hold sk_peer_lock because no one can touch the socket.
Let's set cred/pid without holding lock for the two cases and rename the old init_peercred() to update_peercred() to properly reflect the use case.
--- net/unix/af_unix.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
net/unix/af_unix.c +8 -3
@@ -735,6 +735,12 @@ static void unix_release_sock(struct sock *sk, int embrion) }static void init_peercred (struct sock *sk) +{ + sk->sk_peer_pid =get_pid (task_tgid (current)); + sk->sk_peer_cred =get_current_cred (); +} + +static void update_peercred (struct sock *sk) {const struct cred *old_cred;struct pid *old_pid;
@@ -742,8 +748,7 @@ static void init_peercred(struct sock *sk)spin_lock (&sk->sk_peer_lock); old_pid = sk->sk_peer_pid; old_cred = sk->sk_peer_cred; - sk->sk_peer_pid =get_pid (task_tgid (current)); - sk->sk_peer_cred =get_current_cred (); +init_peercred (sk);spin_unlock (&sk->sk_peer_lock);put_pid (old_pid);
@@ -795,7 +800,7 @@ static int unix_listen(struct socket *sock, int backlog)WRITE_ONCE (sk->sk_state,TCP_LISTEN );/* set credentials so connect can copy them */ -init_peercred (sk); +update_peercred (sk); err =0 ; out_unlock:
Signature · 1 line
2.30.2
PATCH v2 10/11 af_unix: Remove put_pid()/put_cred() in copy_peercred().
In reply to Kuniyuki IwashimaWhen (AF_UNIX, SOCK_STREAM) socket connect()s to a listening socket, the listener's sk_peer_pid/sk_peer_cred are copied to the client in copy_peercred().
Then, the client's sk_peer_pid and sk_peer_cred are always NULL, so we need not call put_pid() and put_cred() there.
--- net/unix/af_unix.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
net/unix/af_unix.c +1 -8
@@ -757,9 +757,6 @@ static void update_peercred(struct sock *sk)static void copy_peercred (struct sock *sk,struct sock *peersk) { -const struct cred *old_cred; -struct pid *old_pid; -if (sk < peersk) {spin_lock (&sk->sk_peer_lock);spin_lock_nested (&peersk->sk_peer_lock,SINGLE_DEPTH_NESTING );
@@ -767,16 +764,12 @@ static void copy_peercred(struct sock *sk, struct sock *peersk)spin_lock (&peersk->sk_peer_lock);spin_lock_nested (&sk->sk_peer_lock,SINGLE_DEPTH_NESTING ); } - old_pid = sk->sk_peer_pid; - old_cred = sk->sk_peer_cred; + sk->sk_peer_pid =get_pid (peersk->sk_peer_pid); sk->sk_peer_cred =get_cred (peersk->sk_peer_cred);spin_unlock (&sk->sk_peer_lock);spin_unlock (&peersk->sk_peer_lock); - -put_pid (old_pid); -put_cred (old_cred); }static int unix_listen (struct socket *sock,int backlog)
Signature · 1 line
2.30.2
PATCH v2 11/11 af_unix: Don't use spin_lock_nested() in copy_peercred().
In reply to Kuniyuki IwashimaWhen (AF_UNIX, SOCK_STREAM) socket connect()s to a listening socket, the listener's sk_peer_pid/sk_peer_cred are copied to the client in copy_peercred().
Then, two sk_peer_locks are held there; one is client's and another is listener's.
However, the latter is not needed because we hold the listner's unix_state_lock() there and unix_listen() cannot update the cred concurrently.
Let's drop the unnecessary spin_lock() and use the bare spin_lock() for the client to protect concurrent read by getsockopt(SO_PEERCRED).
--- net/unix/af_unix.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-)
net/unix/af_unix.c +3 -10
@@ -757,19 +757,12 @@ static void update_peercred(struct sock *sk)static void copy_peercred (struct sock *sk,struct sock *peersk) { -if (sk < peersk) { -spin_lock (&sk->sk_peer_lock); -spin_lock_nested (&peersk->sk_peer_lock,SINGLE_DEPTH_NESTING ); - }else { -spin_lock (&peersk->sk_peer_lock); -spin_lock_nested (&sk->sk_peer_lock,SINGLE_DEPTH_NESTING ); - } +lockdep_assert_held (&unix_sk (peersk)->lock); - sk->sk_peer_pid =get_pid (peersk->sk_peer_pid); +spin_lock (&sk->sk_peer_lock); + sk->sk_peer_pid =get_pid (peersk->sk_peer_pid); sk->sk_peer_cred =get_cred (peersk->sk_peer_cred); -spin_unlock (&sk->sk_peer_lock); -spin_unlock (&peersk->sk_peer_lock); }static int unix_listen (struct socket *sock,int backlog)
Signature · 1 line
2.30.2