netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <kuniyu@amazon.com>
Cc: <davem@davemloft.net>, <edumazet@google.com>,
	<kent.overstreet@linux.dev>, <kuba@kernel.org>,
	<kuni1840@gmail.com>, <netdev@vger.kernel.org>,
	<pabeni@redhat.com>
Subject: [PATCH v1 net-next 2/2] af_unix: Don't hold client's sk_peer_lock in copy_peercred().
Date: Sun, 28 Jan 2024 13:04:07 -0800	[thread overview]
Message-ID: <20240128210407.94429-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20240128103732.18185-3-kuniyu@amazon.com>

From: Kuniyuki Iwashima <kuniyu@amazon.com>
Date: Sun, 28 Jan 2024 02:37:32 -0800
> 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, we need not hold the client's lock.
> 
> The only place where the client's sk_peer_pid/sk_peer_cred are set
> is copy_peercred(), and once they are set, they never change.
> 
> Let's not hold client's sk_peer_lock in copy_peercred().
> 
> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
> ---
>  net/unix/af_unix.c | 20 ++------------------
>  1 file changed, 2 insertions(+), 18 deletions(-)
> 
> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index f07374d31f7c..c5c292393be8 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -706,26 +706,10 @@ 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);
> -	} else {
> -		spin_lock(&peersk->sk_peer_lock);
> -		spin_lock_nested(&sk->sk_peer_lock, SINGLE_DEPTH_NESTING);

Somehow I forgot about that the client's sk_peer_pid could be
fetched at the same time by SO_PEERCRED/SO_PEERPIDFD, so the
lock is needed.


> -	}
> -	old_pid = sk->sk_peer_pid;
> -	old_cred = sk->sk_peer_cred;

but old_pid/old_cred are always NULL, so this can be removed.

Also, the locking order can be fixed as

  peer (TCP_LISTEN) -> sk (TCP_ESTABLISHED)

and we can write a lock_cmp_fn for sk_peer_lock.

I'll do so in v2, so

pw-bot: cr


> -	sk->sk_peer_pid  = get_pid(peersk->sk_peer_pid);
> +	spin_lock(&peersk->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);
> -
> -	put_pid(old_pid);
> -	put_cred(old_cred);
>  }
>  
>  static int unix_listen(struct socket *sock, int backlog)
> -- 
> 2.30.2
> 

      reply	other threads:[~2024-01-28 21:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-28 10:37 [PATCH v1 net-next 0/2] af_unix: Clean up unnecessary spin_lock(&sk->sk_peer_lock) Kuniyuki Iwashima
2024-01-28 10:37 ` [PATCH v1 net-next 1/2] af_unix: Set sk_peer_pid/sk_peer_cred locklessly for new socket Kuniyuki Iwashima
2024-01-28 10:37 ` [PATCH v1 net-next 2/2] af_unix: Don't hold client's sk_peer_lock in copy_peercred() Kuniyuki Iwashima
2024-01-28 21:04   ` 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=20240128210407.94429-1-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).