Skip to contentSkip to conversation

Add io_uring socket(2) support

PATCH Applied Jens Axboe 3 participants 6 messages Apr 12, 2022 – Apr 13, 2022

Subsystems (5)
lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH 1/2 net: add __sys_socket_file()

In reply to Jens Axboe

This works like __sys_socket(), except instead of allocating and returning a socket fd, it just returns the file associated with the socket. No fd is installed into the process file table.

This is similar to do_accept(), and allows io_uring to use this without instantiating a file descriptor in the process file table.

---
 include/linux/socket.h |  1 +
 net/socket.c           | 52 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 10 deletions(-)
include/linux/socket.h +1 -0
index 6f85f5d957ef..a1882e1e71d2 100644
@@ -434,6 +434,7 @@ extern struct file *do_accept(struct file *file, unsigned file_flags,
 extern int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
 			 int __user *upeer_addrlen, int flags);
 extern int __sys_socket(int family, int type, int protocol);
+extern struct file *__sys_socket_file(int family, int type, int protocol);
 extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
 extern int __sys_connect_file(struct file *file, struct sockaddr_storage *addr,
 			      int addrlen, int file_flags);
net/socket.c +42 -10

77 lines not on this page · raw message

Signature · 1 line
2.35.1

Jens Axboe

author maintainer
#0 lore raw

cover Add io_uring socket(2) support

Hi,

The main motivator here is to allow creating a socket as a direct descriptor, similarly to how we do it for the open/accept support.

Signature · 1 line
Jens Axboe
author maintainer
#3 lore raw

PATCH 2/2 io_uring: add socket(2) support

Supports both regular socket(2) where a normal file descriptor is instantiated when called, or direct descriptors.

---
 fs/io_uring.c                 | 77 +++++++++++++++++++++++++++++++++++
 include/uapi/linux/io_uring.h |  1 +
 2 files changed, 78 insertions(+)
fs/io_uring.c +77 -0
index b83134906a3a..1523a43c4469 100644
@@ -558,6 +558,16 @@ struct io_accept {
 	unsigned long			nofile;
 };
 
+struct io_socket {
+	struct file			*file;
+	int				domain;
+	int				type;
+	int				protocol;
+	int				flags;
+	u32				file_slot;
+	unsigned long			nofile;
+};
+
 struct io_sync {
 	struct file			*file;
 	loff_t				len;
@@ -926,6 +936,7 @@ struct io_kiocb {
 		struct io_hardlink	hardlink;
 		struct io_msg		msg;
 		struct io_xattr		xattr;
+		struct io_socket	sock;
 	};
 
 	u8				opcode;
@@ -1192,6 +1203,9 @@ static const struct io_op_def io_op_defs[] = {
 		.needs_file = 1
 	},
 	[IORING_OP_GETXATTR] = {},
+	[IORING_OP_SOCKET] = {
+		.audit_skip		= 1,
+	},
 };
 
 /* requests with any of those set should undergo io_disarm_next() */
@@ -5968,6 +5982,63 @@ static int io_accept(struct io_kiocb *req, unsigned int issue_flags)
 	return 0;
 }
 
+static int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+	struct io_socket *sock = &req->sock;
+
+	if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL))
+		return -EINVAL;
+	if (sqe->ioprio || sqe->addr || sqe->rw_flags || sqe->buf_index)
+		return -EINVAL;
+
+	sock->domain = READ_ONCE(sqe->fd);
+	sock->type = READ_ONCE(sqe->off);
+	sock->protocol = READ_ONCE(sqe->len);
+	sock->file_slot = READ_ONCE(sqe->file_index);
+	sock->nofile = rlimit(RLIMIT_NOFILE);
+
+	sock->flags = sock->type & ~SOCK_TYPE_MASK;
+	if (sock->file_slot && (sock->flags & SOCK_CLOEXEC))
+		return -EINVAL;
+	if (sock->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
+		return -EINVAL;
+	return 0;
+}
+
+static int io_socket(struct io_kiocb *req, unsigned int issue_flags)
+{
+	struct io_socket *sock = &req->sock;
+	bool fixed = !!sock->file_slot;
+	struct file *file;
+	int ret, fd;
+
+	if (!fixed) {
+		fd = __get_unused_fd_flags(sock->flags, sock->nofile);
+		if (unlikely(fd < 0))
+			return fd;
+	}
+	file = __sys_socket_file(sock->domain, sock->type, sock->protocol);
+	if (IS_ERR(file)) {
+		if (!fixed)
+			put_unused_fd(fd);
+		ret = PTR_ERR(file);
+		if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
+			return -EAGAIN;
+		if (ret == -ERESTARTSYS)
+			ret = -EINTR;
+		req_set_fail(req);
+	} else if (!fixed) {
+		fd_install(fd, file);
+		ret = fd;
+	} else {
+		io_sock_nolock_set(file);
+		ret = io_install_fixed_file(req, file, issue_flags,
+					    sock->file_slot - 1);
+	}
+	__io_req_complete(req, issue_flags, ret, 0);
+	return 0;
+}
+
 static int io_connect_prep_async(struct io_kiocb *req)
 {
 	struct io_async_connect *io = req->async_data;
@@ -6055,6 +6126,7 @@ IO_NETOP_PREP_ASYNC(sendmsg);
 IO_NETOP_PREP_ASYNC(recvmsg);
 IO_NETOP_PREP_ASYNC(connect);
 IO_NETOP_PREP(accept);
+IO_NETOP_PREP(socket);
 IO_NETOP_FN(send);
 IO_NETOP_FN(recv);
 #endif /* CONFIG_NET */
@@ -7269,6 +7341,8 @@ static int io_req_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 		return io_fgetxattr_prep(req, sqe);
 	case IORING_OP_GETXATTR:
 		return io_getxattr_prep(req, sqe);
+	case IORING_OP_SOCKET:
+		return io_socket_prep(req, sqe);
 	}
 
 	printk_once(KERN_WARNING "io_uring: unhandled opcode %d\n",
@@ -7590,6 +7664,9 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)
 	case IORING_OP_GETXATTR:
 		ret = io_getxattr(req, issue_flags);
 		break;
+	case IORING_OP_SOCKET:
+		ret = io_socket(req, issue_flags);
+		break;
 	default:
 		ret = -EINVAL;
 		break;
include/uapi/linux/io_uring.h +1 -0
index 696a05aa9618..9e28014b1e10 100644
@@ -151,6 +151,7 @@ enum {
 	IORING_OP_SETXATTR,
 	IORING_OP_FGETXATTR,
 	IORING_OP_GETXATTR,
+	IORING_OP_SOCKET,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
Signature · 1 line
2.35.1
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] net: add __sys_socket_file()

In reply to Jens Axboe

From: Jens Axboe <[email protected]>
Date: Tue, 12 Apr 2022 14:22:39 -0600

Quoted 8 lines

This works like __sys_socket(), except instead of allocating and returning a socket fd, it just returns the file associated with the socket. No fd is installed into the process file table.

This is similar to do_accept(), and allows io_uring to use this without instantiating a file descriptor in the process file table.

#4 lore raw

Re: [PATCH 2/2] io_uring: add socket(2) support

In reply to Jens Axboe
I love your patch! Yet something to improve:

Hi Jens,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20220412]
[cannot apply to horms-ipvs/master net/master net-next/master linus/master v5.18-rc2 v5.18-rc1 v5.17 v5.18-rc2] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Jens-Axboe/Add-io_uring-socket-2-support/20220413-050345
base:    d0c745e7b2d6ce8bcc768b32361ab8ef520821ee
config: arm64-buildonly-randconfig-r001-20220413 (https://download.01.org/0day-ci/archive/20220413/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project fe2478d44e4f7f191c43fef629ac7a23d0251e72)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm64 cross compiling tool for clang build
# apt-get install binutils-aarch64-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/8f4e27a291c192076d65f71fcaf34d1b30b892f7
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Jens-Axboe/Add-io_uring-socket-2-support/20220413-050345 git checkout 8f4e27a291c192076d65f71fcaf34d1b30b892f7
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

>> fs/io_uring.c:5962:3: error: implicit declaration of function 'io_sock_nolock_set' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
                   io_sock_nolock_set(file);
                   ^

1 error generated.

vim +/io_sock_nolock_set +5962 fs/io_uring.c

5935
5936 static int io_socket(struct io_kiocb *req, unsigned int issue_flags)
5937 {
5938 struct io_socket *sock = &req->sock;
5939 bool fixed = !!sock->file_slot;
5940 struct file *file;
5941 int ret, fd;
5942
5943 if (!fixed) {
5944 fd = __get_unused_fd_flags(sock->flags, sock->nofile);
5945 if (unlikely(fd < 0))
5946 return fd;
5947 }
5948 file = __sys_socket_file(sock->domain, sock->type, sock->protocol);
5949 if (IS_ERR(file)) {
5950 if (!fixed)
5951 put_unused_fd(fd);
5952 ret = PTR_ERR(file);
5953 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
5954 return -EAGAIN;
5955 if (ret == -ERESTARTSYS)
5956 ret = -EINTR;
5957 req_set_fail(req);
5958 } else if (!fixed) {
5959 fd_install(fd, file);
5960 ret = fd;
5961 } else {

> 5962			io_sock_nolock_set(file);

5963 ret = io_install_fixed_file(req, file, issue_flags,
5964 sock->file_slot - 1);
5965 }
5966 __io_req_complete(req, issue_flags, ret, 0);
5967 return 0;
5968 }
5969

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
author maintainer Applied
#5 lore raw
In reply to Jens Axboe
Quoted 3 lines · Jens Axboe

The main motivator here is to allow creating a socket as a direct descriptor, similarly to how we do it for the open/accept support.

Applied, thanks!

[1/2] net: add __sys_socket_file()
(no commit info)
[2/2] io_uring: add socket(2) support
(no commit info)

Best regards,

Signature · 1 line
Jens Axboe