netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: Kent Overstreet <kent.overstreet@linux.dev>,
	Kuniyuki Iwashima <kuniyu@amazon.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>
Subject: [PATCH v1 net-next 11/11] af_unix: Don't use spin_lock_nested() in copy_peercred().
Date: Mon, 10 Jun 2024 15:35:01 -0700	[thread overview]
Message-ID: <20240610223501.73191-12-kuniyu@amazon.com> (raw)
In-Reply-To: <20240610223501.73191-1-kuniyu@amazon.com>

When (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).

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 net/unix/af_unix.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 7d9c4c9b6f85..0d706d8f56a0 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -755,19 +755,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)
-- 
2.30.2


      parent reply	other threads:[~2024-06-10 22:39 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-10 22:34 [PATCH v1 net-next 00/11] af_unix: Remove spin_lock_nested() and convert to lock_cmp_fn Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 01/11] af_unix: Define locking order for unix_table_double_lock() Kuniyuki Iwashima
2024-06-10 22:43   ` Kent Overstreet
2024-06-10 23:38     ` Kuniyuki Iwashima
2024-06-10 23:50       ` Kent Overstreet
2024-06-10 23:58         ` Kuniyuki Iwashima
2024-06-11  0:30           ` Kent Overstreet
2024-06-11  0:47             ` Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 02/11] af_unix: Define locking order for U_LOCK_SECOND in unix_state_double_lock() Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 03/11] af_unix: Don't retry after unix_state_lock_nested() in unix_stream_connect() Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 04/11] af_unix: Define locking order for U_LOCK_SECOND " Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 05/11] af_unix: Don't acquire unix_state_lock() for sock_i_ino() Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 06/11] af_unix: Remove U_LOCK_DIAG Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 07/11] af_unix: Remove U_LOCK_GC_LISTENER Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 08/11] af_unix: Remove U_RECVQ_LOCK_EMBRYO Kuniyuki Iwashima
2024-06-10 22:34 ` [PATCH v1 net-next 09/11] af_unix: Set sk_peer_pid/sk_peer_cred locklessly for new socket Kuniyuki Iwashima
2024-06-10 22:35 ` [PATCH v1 net-next 10/11] af_unix: Remove put_pid()/put_cred() in copy_peercred() Kuniyuki Iwashima
2024-06-10 22:35 ` Kuniyuki Iwashima [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240610223501.73191-12-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kent.overstreet@linux.dev \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).