From patchwork Tue Jun 4 16:52:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kuniyuki Iwashima X-Patchwork-Id: 13685686 X-Patchwork-Delegate: kuba@kernel.org Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49EAF13D607 for ; Tue, 4 Jun 2024 16:57:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=99.78.197.218 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717520267; cv=none; b=AQXEBMjzRgE3x75hRJ6ifNoAtTDhQ0PfmlKeRrjwn2fz5vFvJFcaodAo9D4HntJly38zykSFoS3DqJR+plVrh9uwdO6YQTFPmC9TYN7O2bnm0soNGNbd0Op84hB9W6ea50vPw09AX9TYVsfDMTKMRg/MFHxxtXvS+Hg7ua9Tdxc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717520267; c=relaxed/simple; bh=59cxU2FH4yVvL6kkRmWuGqIYjdze5hbc68cWkSWZJZQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lZc0nwkp8+vtXROJZ/DCBwxWdBprNB+BeTplTrnJ4UunXwrVb4FLVu/Z1KAtPB+ACAPvenkwIjWIR0z1cawcMV1M1mV0ucoQ8oii6SFB/c0cAbULpcMgWAFyV/O41eKm1v/m6JU3sKCV9iRPmkdt93CHa295QLH1QDSII07a8p8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.co.jp; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=d9VAOq/D; arc=none smtp.client-ip=99.78.197.218 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.jp Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="d9VAOq/D" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1717520266; x=1749056266; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6wn+5w9ZqPDM0TqtBYI5jRPLudaXJUs5FL9ViyClaa0=; b=d9VAOq/DDgZap+Z6/2lGRruP5pU91BMi6EzmnsOOYBR43bACadXI2S0H IzwXx/DFT/CNr/cIWY40zjSdpWfEmBiCJs8xKqSlcZzreWHMHK6nZJiFw IXIEUyTBNvnf701mjCJ9qf+pkQ7FUY9upCRaNBf6GyxALWkXAZXU87Bjc U=; X-IronPort-AV: E=Sophos;i="6.08,214,1712620800"; d="scan'208";a="300895808" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Jun 2024 16:57:45 +0000 Received: from EX19MTAUWC002.ant.amazon.com [10.0.21.151:20798] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.57.174:2525] with esmtp (Farcaster) id 104eb820-f873-4ac7-9987-e4f3260629b9; Tue, 4 Jun 2024 16:57:45 +0000 (UTC) X-Farcaster-Flow-ID: 104eb820-f873-4ac7-9987-e4f3260629b9 Received: from EX19D004ANA001.ant.amazon.com (10.37.240.138) by EX19MTAUWC002.ant.amazon.com (10.250.64.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.28; Tue, 4 Jun 2024 16:57:43 +0000 Received: from 88665a182662.ant.amazon.com (10.187.170.50) by EX19D004ANA001.ant.amazon.com (10.37.240.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 4 Jun 2024 16:57:40 +0000 From: Kuniyuki Iwashima To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni CC: Kuniyuki Iwashima , Kuniyuki Iwashima , Subject: [PATCH v2 net 12/15] af_unix: Use unix_recvq_full_lockless() in unix_stream_connect(). Date: Tue, 4 Jun 2024 09:52:38 -0700 Message-ID: <20240604165241.44758-13-kuniyu@amazon.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240604165241.44758-1-kuniyu@amazon.com> References: <20240604165241.44758-1-kuniyu@amazon.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: EX19D035UWB004.ant.amazon.com (10.13.138.104) To EX19D004ANA001.ant.amazon.com (10.37.240.138) X-Patchwork-Delegate: kuba@kernel.org Once sk->sk_state is changed to TCP_LISTEN, it never changes. unix_accept() takes advantage of this characteristics; it does not hold the listener's unix_state_lock() and only acquires recvq lock to pop one skb. It means unix_state_lock() does not prevent the queue length from changing in unix_stream_connect(). Thus, we need to use unix_recvq_full_lockless() to avoid data-race. Now we remove unix_recvq_full() as no one uses it. Note that we can remove READ_ONCE() for sk->sk_max_ack_backlog in unix_recvq_full_lockless() because of the following reasons: (1) For SOCK_DGRAM, it is a written-once field in unix_create1() (2) For SOCK_STREAM and SOCK_SEQPACKET, it is changed under the listener's unix_state_lock() in unix_listen(), and we hold the lock in unix_stream_connect() Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima --- net/unix/af_unix.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 623ee39657e2..eb3ba3448ed3 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -221,15 +221,9 @@ static inline int unix_may_send(struct sock *sk, struct sock *osk) return unix_peer(osk) == NULL || unix_our_peer(sk, osk); } -static inline int unix_recvq_full(const struct sock *sk) -{ - return skb_queue_len(&sk->sk_receive_queue) > sk->sk_max_ack_backlog; -} - static inline int unix_recvq_full_lockless(const struct sock *sk) { - return skb_queue_len_lockless(&sk->sk_receive_queue) > - READ_ONCE(sk->sk_max_ack_backlog); + return skb_queue_len_lockless(&sk->sk_receive_queue) > sk->sk_max_ack_backlog; } struct sock *unix_peer_get(struct sock *s) @@ -1545,7 +1539,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr, if (other->sk_shutdown & RCV_SHUTDOWN) goto out_unlock; - if (unix_recvq_full(other)) { + if (unix_recvq_full_lockless(other)) { err = -EAGAIN; if (!timeo) goto out_unlock;