diff mbox series

[v2,3/4] io_uring: Introduce IORING_OP_BIND

Message ID 20240614163047.31581-3-krisman@suse.de (mailing list archive)
State Handled Elsewhere
Delegated to: Netdev Maintainers
Headers show
Series [v2,1/4] net: Split a __sys_bind helper for io_uring | expand

Checks

Context Check Description
netdev/series_format warning Series does not have a cover letter
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1011 this patch: 1011
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 1 maintainers not CCed: asml.silence@gmail.com
netdev/build_clang success Errors and warnings before: 851 this patch: 851
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1018 this patch: 1018
netdev/checkpatch warning WARNING: line length of 87 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-06-16--18-00 (tests: 659)

Commit Message

Gabriel Krisman Bertazi June 14, 2024, 4:30 p.m. UTC
IORING_OP_BIND provides the semantic of bind(2) via io_uring.  While
this is an essentially synchronous system call, the main point is to
enable a network path to execute fully with io_uring registered and
descriptorless files.

Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>

---
changes since v1:
- drop explocit error handling for move_addr_to_kernel (jens)
- Remove empty line ahead of return;
---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/net.c                | 36 +++++++++++++++++++++++++++++++++++
 io_uring/net.h                |  3 +++
 io_uring/opdef.c              | 13 +++++++++++++
 4 files changed, 53 insertions(+)

Comments

Kuniyuki Iwashima June 14, 2024, 10:46 p.m. UTC | #1
From: Gabriel Krisman Bertazi <krisman@suse.de>
Date: Fri, 14 Jun 2024 12:30:46 -0400
> IORING_OP_BIND provides the semantic of bind(2) via io_uring.  While
> this is an essentially synchronous system call, the main point is to
> enable a network path to execute fully with io_uring registered and
> descriptorless files.
> 
> Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
> 
> ---
> changes since v1:
> - drop explocit error handling for move_addr_to_kernel (jens)
> - Remove empty line ahead of return;
> ---
>  include/uapi/linux/io_uring.h |  1 +
>  io_uring/net.c                | 36 +++++++++++++++++++++++++++++++++++
>  io_uring/net.h                |  3 +++
>  io_uring/opdef.c              | 13 +++++++++++++
>  4 files changed, 53 insertions(+)
> 
> diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
> index 994bf7af0efe..4ef153d95c87 100644
> --- a/include/uapi/linux/io_uring.h
> +++ b/include/uapi/linux/io_uring.h
> @@ -257,6 +257,7 @@ enum io_uring_op {
>  	IORING_OP_FUTEX_WAITV,
>  	IORING_OP_FIXED_FD_INSTALL,
>  	IORING_OP_FTRUNCATE,
> +	IORING_OP_BIND,
>  
>  	/* this goes last, obviously */
>  	IORING_OP_LAST,
> diff --git a/io_uring/net.c b/io_uring/net.c
> index 0a48596429d9..8cbc29aff15c 100644
> --- a/io_uring/net.c
> +++ b/io_uring/net.c
> @@ -51,6 +51,11 @@ struct io_connect {
>  	bool				seen_econnaborted;
>  };
>  
> +struct io_bind {
> +	struct file			*file;
> +	int				addr_len;
> +};
> +
>  struct io_sr_msg {
>  	struct file			*file;
>  	union {
> @@ -1715,6 +1720,37 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags)
>  	return IOU_OK;
>  }
>  
> +int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
> +{
> +	struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
> +	struct sockaddr __user *uaddr;
> +	struct io_async_msghdr *io;
> +
> +	if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
> +		return -EINVAL;
> +
> +	uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
> +	bind->addr_len =  READ_ONCE(sqe->addr2);
                        ^^
nit: double space


> +
> +	io = io_msg_alloc_async(req);
> +	if (unlikely(!io))
> +		return -ENOMEM;
> +	return move_addr_to_kernel(uaddr, bind->addr_len, &io->addr);
> +}
> +
> +int io_bind(struct io_kiocb *req, unsigned int issue_flags)
> +{
> +	struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
> +	struct io_async_msghdr *io = req->async_data;
> +	int ret;
> +
> +	ret = __sys_bind_socket(sock_from_file(req->file),  &io->addr, bind->addr_len);
                                                          ^^
ditto


> +	if (ret < 0)
> +		req_set_fail(req);
> +	io_req_set_res(req, ret, 0);
> +	return 0;
> +}
> +
>  void io_netmsg_cache_free(const void *entry)
>  {
>  	struct io_async_msghdr *kmsg = (struct io_async_msghdr *) entry;
> diff --git a/io_uring/net.h b/io_uring/net.h
> index 0eb1c1920fc9..49f9a7bc1113 100644
> --- a/io_uring/net.h
> +++ b/io_uring/net.h
> @@ -49,6 +49,9 @@ int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags);
>  int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
>  void io_send_zc_cleanup(struct io_kiocb *req);
>  
> +int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
> +int io_bind(struct io_kiocb *req, unsigned int issue_flags);
> +
>  void io_netmsg_cache_free(const void *entry);
>  #else
>  static inline void io_netmsg_cache_free(const void *entry)
> diff --git a/io_uring/opdef.c b/io_uring/opdef.c
> index 2de5cca9504e..19ee9445f024 100644
> --- a/io_uring/opdef.c
> +++ b/io_uring/opdef.c
> @@ -495,6 +495,16 @@ const struct io_issue_def io_issue_defs[] = {
>  		.prep			= io_ftruncate_prep,
>  		.issue			= io_ftruncate,
>  	},
> +	[IORING_OP_BIND] = {
> +#if defined(CONFIG_NET)
> +		.needs_file		= 1,
> +		.prep			= io_bind_prep,
> +		.issue			= io_bind,
> +		.async_size		= sizeof(struct io_async_msghdr),
> +#else
> +		.prep			= io_eopnotsupp_prep,
> +#endif
> +	},
>  };
>  
>  const struct io_cold_def io_cold_defs[] = {
> @@ -711,6 +721,9 @@ const struct io_cold_def io_cold_defs[] = {
>  	[IORING_OP_FTRUNCATE] = {
>  		.name			= "FTRUNCATE",
>  	},
> +	[IORING_OP_BIND] = {
> +		.name			= "BIND",
> +	},
>  };
>  
>  const char *io_uring_get_opcode(u8 opcode)
> -- 
> 2.45.2
Jens Axboe June 15, 2024, 12:27 a.m. UTC | #2
On 6/14/24 4:46 PM, Kuniyuki Iwashima wrote:
> From: Gabriel Krisman Bertazi <krisman@suse.de>
> Date: Fri, 14 Jun 2024 12:30:46 -0400
>> IORING_OP_BIND provides the semantic of bind(2) via io_uring.  While
>> this is an essentially synchronous system call, the main point is to
>> enable a network path to execute fully with io_uring registered and
>> descriptorless files.
>>
>> Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
>>
>> ---
>> changes since v1:
>> - drop explocit error handling for move_addr_to_kernel (jens)
>> - Remove empty line ahead of return;
>> ---
>>  include/uapi/linux/io_uring.h |  1 +
>>  io_uring/net.c                | 36 +++++++++++++++++++++++++++++++++++
>>  io_uring/net.h                |  3 +++
>>  io_uring/opdef.c              | 13 +++++++++++++
>>  4 files changed, 53 insertions(+)
>>
>> diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
>> index 994bf7af0efe..4ef153d95c87 100644
>> --- a/include/uapi/linux/io_uring.h
>> +++ b/include/uapi/linux/io_uring.h
>> @@ -257,6 +257,7 @@ enum io_uring_op {
>>  	IORING_OP_FUTEX_WAITV,
>>  	IORING_OP_FIXED_FD_INSTALL,
>>  	IORING_OP_FTRUNCATE,
>> +	IORING_OP_BIND,
>>  
>>  	/* this goes last, obviously */
>>  	IORING_OP_LAST,
>> diff --git a/io_uring/net.c b/io_uring/net.c
>> index 0a48596429d9..8cbc29aff15c 100644
>> --- a/io_uring/net.c
>> +++ b/io_uring/net.c
>> @@ -51,6 +51,11 @@ struct io_connect {
>>  	bool				seen_econnaborted;
>>  };
>>  
>> +struct io_bind {
>> +	struct file			*file;
>> +	int				addr_len;
>> +};
>> +
>>  struct io_sr_msg {
>>  	struct file			*file;
>>  	union {
>> @@ -1715,6 +1720,37 @@ int io_connect(struct io_kiocb *req, unsigned int issue_flags)
>>  	return IOU_OK;
>>  }
>>  
>> +int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
>> +{
>> +	struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
>> +	struct sockaddr __user *uaddr;
>> +	struct io_async_msghdr *io;
>> +
>> +	if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
>> +		return -EINVAL;
>> +
>> +	uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
>> +	bind->addr_len =  READ_ONCE(sqe->addr2);
>                         ^^
> nit: double space

Thanks for spotting those, I can just remove those two while applying.
Mostly just a note to Grabriel, no need to re-post for that.
diff mbox series

Patch

diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
index 994bf7af0efe..4ef153d95c87 100644
--- a/include/uapi/linux/io_uring.h
+++ b/include/uapi/linux/io_uring.h
@@ -257,6 +257,7 @@  enum io_uring_op {
 	IORING_OP_FUTEX_WAITV,
 	IORING_OP_FIXED_FD_INSTALL,
 	IORING_OP_FTRUNCATE,
+	IORING_OP_BIND,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
diff --git a/io_uring/net.c b/io_uring/net.c
index 0a48596429d9..8cbc29aff15c 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -51,6 +51,11 @@  struct io_connect {
 	bool				seen_econnaborted;
 };
 
+struct io_bind {
+	struct file			*file;
+	int				addr_len;
+};
+
 struct io_sr_msg {
 	struct file			*file;
 	union {
@@ -1715,6 +1720,37 @@  int io_connect(struct io_kiocb *req, unsigned int issue_flags)
 	return IOU_OK;
 }
 
+int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+	struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
+	struct sockaddr __user *uaddr;
+	struct io_async_msghdr *io;
+
+	if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
+		return -EINVAL;
+
+	uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
+	bind->addr_len =  READ_ONCE(sqe->addr2);
+
+	io = io_msg_alloc_async(req);
+	if (unlikely(!io))
+		return -ENOMEM;
+	return move_addr_to_kernel(uaddr, bind->addr_len, &io->addr);
+}
+
+int io_bind(struct io_kiocb *req, unsigned int issue_flags)
+{
+	struct io_bind *bind = io_kiocb_to_cmd(req, struct io_bind);
+	struct io_async_msghdr *io = req->async_data;
+	int ret;
+
+	ret = __sys_bind_socket(sock_from_file(req->file),  &io->addr, bind->addr_len);
+	if (ret < 0)
+		req_set_fail(req);
+	io_req_set_res(req, ret, 0);
+	return 0;
+}
+
 void io_netmsg_cache_free(const void *entry)
 {
 	struct io_async_msghdr *kmsg = (struct io_async_msghdr *) entry;
diff --git a/io_uring/net.h b/io_uring/net.h
index 0eb1c1920fc9..49f9a7bc1113 100644
--- a/io_uring/net.h
+++ b/io_uring/net.h
@@ -49,6 +49,9 @@  int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags);
 int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
 void io_send_zc_cleanup(struct io_kiocb *req);
 
+int io_bind_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_bind(struct io_kiocb *req, unsigned int issue_flags);
+
 void io_netmsg_cache_free(const void *entry);
 #else
 static inline void io_netmsg_cache_free(const void *entry)
diff --git a/io_uring/opdef.c b/io_uring/opdef.c
index 2de5cca9504e..19ee9445f024 100644
--- a/io_uring/opdef.c
+++ b/io_uring/opdef.c
@@ -495,6 +495,16 @@  const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_ftruncate_prep,
 		.issue			= io_ftruncate,
 	},
+	[IORING_OP_BIND] = {
+#if defined(CONFIG_NET)
+		.needs_file		= 1,
+		.prep			= io_bind_prep,
+		.issue			= io_bind,
+		.async_size		= sizeof(struct io_async_msghdr),
+#else
+		.prep			= io_eopnotsupp_prep,
+#endif
+	},
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -711,6 +721,9 @@  const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_FTRUNCATE] = {
 		.name			= "FTRUNCATE",
 	},
+	[IORING_OP_BIND] = {
+		.name			= "BIND",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)