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: Kuniyuki Iwashima <kuniyu@amazon.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>
Subject: [PATCH v1 net-next 06/15] af_unix: Set error only when needed in unix_dgram_sendmsg().
Date: Fri, 6 Dec 2024 14:25:58 +0900	[thread overview]
Message-ID: <20241206052607.1197-7-kuniyu@amazon.com> (raw)
In-Reply-To: <20241206052607.1197-1-kuniyu@amazon.com>

We will introduce skb drop reason for AF_UNIX, then we need to
set an errno and a drop reason for each path.

Let's set an error only when it's needed in unix_dgram_sendmsg().

Then, we need not (re)set 0 to err.

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

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 7e6241ba7604..e439829efc56 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -1978,9 +1978,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 
 	wait_for_unix_gc(scm.fp);
 
-	err = -EOPNOTSUPP;
-	if (msg->msg_flags&MSG_OOB)
+	if (msg->msg_flags & MSG_OOB) {
+		err = -EOPNOTSUPP;
 		goto out;
+	}
 
 	if (msg->msg_namelen) {
 		err = unix_validate_addr(sunaddr, msg->msg_namelen);
@@ -1995,10 +1996,11 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 			goto out;
 	} else {
 		sunaddr = NULL;
-		err = -ENOTCONN;
 		other = unix_peer_get(sk);
-		if (!other)
+		if (!other) {
+			err = -ENOTCONN;
 			goto out;
+		}
 	}
 
 	if ((test_bit(SOCK_PASSCRED, &sock->flags) ||
@@ -2009,9 +2011,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 			goto out;
 	}
 
-	err = -EMSGSIZE;
-	if (len > READ_ONCE(sk->sk_sndbuf) - 32)
+	if (len > READ_ONCE(sk->sk_sndbuf) - 32) {
+		err = -EMSGSIZE;
 		goto out;
+	}
 
 	if (len > SKB_MAX_ALLOC) {
 		data_len = min_t(size_t,
@@ -2043,9 +2046,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 
 restart:
 	if (!other) {
-		err = -ECONNRESET;
-		if (sunaddr == NULL)
+		if (!sunaddr) {
+			err = -ECONNRESET;
 			goto out_free;
+		}
 
 		other = unix_find_other(sock_net(sk), sunaddr, msg->msg_namelen,
 					sk->sk_type);
@@ -2065,9 +2069,11 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 	sk_locked = 0;
 	unix_state_lock(other);
 restart_locked:
-	err = -EPERM;
-	if (!unix_may_send(sk, other))
+
+	if (!unix_may_send(sk, other)) {
+		err = -EPERM;
 		goto out_unlock;
+	}
 
 	if (unlikely(sock_flag(other, SOCK_DEAD))) {
 		/*
@@ -2080,7 +2086,6 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 		if (!sk_locked)
 			unix_state_lock(sk);
 
-		err = 0;
 		if (sk->sk_type == SOCK_SEQPACKET) {
 			/* We are here only when racing with unix_release_sock()
 			 * is clearing @other. Never change state to TCP_CLOSE
@@ -2108,9 +2113,10 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
 		goto restart;
 	}
 
-	err = -EPIPE;
-	if (other->sk_shutdown & RCV_SHUTDOWN)
+	if (other->sk_shutdown & RCV_SHUTDOWN) {
+		err = -EPIPE;
 		goto out_unlock;
+	}
 
 	if (sk->sk_type != SOCK_SEQPACKET) {
 		err = security_unix_may_send(sk->sk_socket, other->sk_socket);
-- 
2.39.5 (Apple Git-154)


  parent reply	other threads:[~2024-12-06  5:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-06  5:25 [PATCH v1 net-next 00/15] af_unix: Prepare for skb drop reason Kuniyuki Iwashima
2024-12-06  5:25 ` [PATCH v1 net-next 01/15] af_unix: Set error only when needed in unix_stream_connect() Kuniyuki Iwashima
2024-12-06  5:25 ` [PATCH v1 net-next 02/15] af_unix: Clean up error paths " Kuniyuki Iwashima
2024-12-06  5:25 ` [PATCH v1 net-next 03/15] af_unix: Set error only when needed in unix_stream_sendmsg() Kuniyuki Iwashima
2024-12-06  5:25 ` [PATCH v1 net-next 04/15] af_unix: Remove redundant SEND_SHUTDOWN check " Kuniyuki Iwashima
2024-12-10 11:48   ` Paolo Abeni
2024-12-13  8:18     ` Kuniyuki Iwashima
2024-12-06  5:25 ` [PATCH v1 net-next 05/15] af_unix: Clean up error paths " Kuniyuki Iwashima
2024-12-06  5:25 ` Kuniyuki Iwashima [this message]
2024-12-06  5:25 ` [PATCH v1 net-next 07/15] af_unix: Call unix_autobind() only when msg_namelen is specified in unix_dgram_sendmsg() Kuniyuki Iwashima
2024-12-10 11:11   ` Paolo Abeni
2024-12-10 11:33     ` Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 08/15] af_unix: Move !sunaddr case " Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 09/15] af_unix: Use msg->{msg_name,msg_namelen} " Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 10/15] af_unix: Split restart label " Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 11/15] af_unix: Defer sock_put() to clean up path " Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 12/15] af_unix: Clean up SOCK_DEAD error paths " Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 13/15] af_unix: Clean up error path " Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 14/15] af_unix: Remove sk_locked logic " Kuniyuki Iwashima
2024-12-10 11:42   ` Paolo Abeni
2024-12-13  8:26     ` Kuniyuki Iwashima
2024-12-06  5:26 ` [PATCH v1 net-next 15/15] af_unix: Remove unix_our_peer() Kuniyuki Iwashima

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=20241206052607.1197-7-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --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).