Skip to contentSkip to conversation

net: leave O_NONBLOCK to the sendmsg and recvmsg syscalls

PATCH Superseded Jens Axboe 2 participants 3 messages Nov 6

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author
#0 lore raw

PATCH 1/1 net: leave O_NONBLOCK to the sendmsg and recvmsg syscalls

____sys_sendmsg() and ____sys_recvmsg() turn O_NONBLOCK on the socket into MSG_DONTWAIT. That is right for the system calls, but io_uring uses __sys_sendmsg_sock() and __sys_recvmsg_sock() for a blocking attempt from io-wq as well, after the non-blocking attempts with MSG_DONTWAIT have failed. On an O_NONBLOCK socket that attempt can't block either and the request completes with -EAGAIN, unlike IORING_OP_SEND and RECV, which call sock_sendmsg() and sock_recvmsg() directly and don't look at O_NONBLOCK at all.

Move the O_NONBLOCK handling to ___sys_sendmsg() and ___sys_recvmsg(), which the sendmsg, sendmmsg, recvmsg and recvmmsg system calls go through. The io_uring helpers then pass the flags the caller asked for.

---

n renamed from net/socket.c +6 -5
index 2e3b5737c481..08658a9b8065 100644
@@ -2697,8 +2697,6 @@ static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
 	flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
 	msg_sys->msg_flags = flags;
 
-	if (sock->file->f_flags & O_NONBLOCK)
-		msg_sys->msg_flags |= MSG_DONTWAIT;
 	/*
 	 * If this is sendmmsg() and current destination address is same as
 	 * previously succeeded address, omit asking LSM's decision.
@@ -2762,6 +2760,9 @@ static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
 
 	msg_sys->msg_name = &address;
 
+	if (sock->file->f_flags & O_NONBLOCK)
+		flags |= MSG_DONTWAIT;
+
 	err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
 	if (err < 0)
 		return err;
@@ -2926,9 +2927,6 @@ static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
 	/* We assume all kernel code knows the size of sockaddr_storage */
 	msg_sys->msg_namelen = 0;
 
-	if (sock->file->f_flags & O_NONBLOCK)
-		flags |= MSG_DONTWAIT;
-
 	if (unlikely(nosec))
 		err = sock_recvmsg_nosec(sock, msg_sys, flags);
 	else
@@ -2970,6 +2968,9 @@ static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
 	struct sockaddr __user *uaddr;
 	ssize_t err;
 
+	if (sock->file->f_flags & O_NONBLOCK)
+		flags |= MSG_DONTWAIT;
+
 	err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
 	if (err < 0)
 		return err;
Signature · 1 line
Jens Axboe
maintainer
#1 lore raw
Quoted 9 lines · Jens Axboe

____sys_sendmsg() and ____sys_recvmsg() turn O_NONBLOCK on the socket into MSG_DONTWAIT. That is right for the system calls, but io_uring uses __sys_sendmsg_sock() and __sys_recvmsg_sock() for a blocking attempt from io-wq as well, after the non-blocking attempts with MSG_DONTWAIT have failed. On an O_NONBLOCK socket that attempt can't block either and the request completes with -EAGAIN, unlike IORING_OP_SEND and RECV, which call sock_sendmsg() and sock_recvmsg() directly and don't look at O_NONBLOCK at all.

Makes sense. The socket.c side looks fine to me: all syscall paths (sendmsg, sendmmsg, recvmsg, recvmmsg, compat) go through ___sys_sendmsg() or ___sys_recvmsg(), and the copy_msghdr helpers only care about MSG_CMSG_COMPAT, so setting MSG_DONTWAIT earlier is harmless.

Fixes: tags? And please tag the subject [PATCH net] if this is meant as a fix.

Fixes 0fa03c624d8f ("io_uring: add support for sendmsg()")
Fixes aa1fa28fc73e ("io_uring: add support for recvmsg()")
Quoted 6 lines

Move the O_NONBLOCK handling to ___sys_sendmsg() and ___sys_recvmsg(), which the sendmsg, sendmmsg, recvmsg and recvmmsg system calls go through. The io_uring helpers then pass the flags the caller asked for.

The name lost its accents (Lukáš Lalinský). A Closes: or Link: to the report would be nice too.

n

Quoted 8 lines

---

n renamed from net/socket.c +0 -0
index 2e3b5737c481..08658a9b8065 100644

This header got truncated, so the patch does not apply as posted.

n static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys, · line 2699

Quoted 4 lines
n renamed from net/socket.c +0 -0
@@ -2697,8 +2697,6 @@ static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
        flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
        msg_sys->msg_flags = flags;
 

pw-bot: cr

author
#2 lore raw
Quoted 11 lines · Eric Dumazet

Le sam. 7 nov. 2026 ? 00:19, Jens Axboe <[email protected]> a ?crit :

Quoted 9 lines

____sys_sendmsg() and ____sys_recvmsg() turn O_NONBLOCK on the socket into MSG_DONTWAIT. That is right for the system calls, but io_uring uses __sys_sendmsg_sock() and __sys_recvmsg_sock() for a blocking attempt from io-wq as well, after the non-blocking attempts with MSG_DONTWAIT have failed. On an O_NONBLOCK socket that attempt can't block either and the request completes with -EAGAIN, unlike IORING_OP_SEND and RECV, which call sock_sendmsg() and sock_recvmsg() directly and don't look at O_NONBLOCK at all.

Quoted 4 lines

Makes sense. The socket.c side looks fine to me: all syscall paths (sendmsg, sendmmsg, recvmsg, recvmmsg, compat) go through ___sys_sendmsg() or ___sys_recvmsg(), and the copy_msghdr helpers only care about MSG_CMSG_COMPAT, so setting MSG_DONTWAIT earlier is harmless.

Thanks for checking!

Quoted 5 lines

Fixes: tags? And please tag the subject [PATCH net] if this is meant as a fix.

Fixes 0fa03c624d8f ("io_uring: add support for sendmsg()")
Fixes aa1fa28fc73e ("io_uring: add support for recvmsg()")

Good point, I'll add the fixes tags and the subject.

Quoted 8 lines
Quoted 5 lines

Move the O_NONBLOCK handling to ___sys_sendmsg() and ___sys_recvmsg(), which the sendmsg, sendmmsg, recvmsg and recvmmsg system calls go through. The io_uring helpers then pass the flags the caller asked for.

The name lost its accents (Luk?? Lalinsk?). A Closes: or Link: to the report would be nice too.

Man, not a lot went right in this posting... But yes, it lost it. I'll add a link too.

Quoted 6 lines

n

Quoted 4 lines
n renamed from net/socket.c +0 -0
index 2e3b5737c481..08658a9b8065 100644

This header got truncated, so the patch does not apply as posted.

I'll send a v2 :)

Signature · 1 line
Jens Axboe