2022-05-25 06:25:13 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
|
#include <linux/file.h>
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
|
#include <linux/net.h>
|
|
|
|
|
#include <linux/compat.h>
|
|
|
|
|
#include <net/compat.h>
|
|
|
|
|
#include <linux/io_uring.h>
|
|
|
|
|
|
|
|
|
|
#include <uapi/linux/io_uring.h>
|
|
|
|
|
|
|
|
|
|
#include "io_uring.h"
|
2022-06-13 07:07:23 -06:00
|
|
|
#include "kbuf.h"
|
2022-07-07 14:30:09 -06:00
|
|
|
#include "alloc_cache.h"
|
2022-05-25 06:25:13 -06:00
|
|
|
#include "net.h"
|
2022-07-12 21:52:43 +01:00
|
|
|
#include "notif.h"
|
2022-07-12 21:52:46 +01:00
|
|
|
#include "rsrc.h"
|
2025-02-14 16:09:41 -08:00
|
|
|
#include "zcrx.h"
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET)
|
|
|
|
|
struct io_shutdown {
|
|
|
|
|
struct file *file;
|
|
|
|
|
int how;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct io_accept {
|
|
|
|
|
struct file *file;
|
|
|
|
|
struct sockaddr __user *addr;
|
|
|
|
|
int __user *addr_len;
|
|
|
|
|
int flags;
|
2024-05-07 14:06:15 -06:00
|
|
|
int iou_flags;
|
2022-05-25 06:25:13 -06:00
|
|
|
u32 file_slot;
|
|
|
|
|
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_connect {
|
|
|
|
|
struct file *file;
|
|
|
|
|
struct sockaddr __user *addr;
|
|
|
|
|
int addr_len;
|
2022-10-04 20:29:48 -06:00
|
|
|
bool in_progress;
|
2023-03-20 11:13:49 -06:00
|
|
|
bool seen_econnaborted;
|
2022-05-25 06:25:13 -06:00
|
|
|
};
|
|
|
|
|
|
2024-06-14 12:30:46 -04:00
|
|
|
struct io_bind {
|
|
|
|
|
struct file *file;
|
|
|
|
|
int addr_len;
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-14 12:30:47 -04:00
|
|
|
struct io_listen {
|
|
|
|
|
struct file *file;
|
|
|
|
|
int backlog;
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
struct io_sr_msg {
|
|
|
|
|
struct file *file;
|
|
|
|
|
union {
|
|
|
|
|
struct compat_msghdr __user *umsg_compat;
|
|
|
|
|
struct user_msghdr __user *umsg;
|
|
|
|
|
void __user *buf;
|
|
|
|
|
};
|
2024-03-05 13:10:04 -07:00
|
|
|
int len;
|
2022-09-08 13:20:33 +01:00
|
|
|
unsigned done_io;
|
2022-07-25 10:52:06 +01:00
|
|
|
unsigned msg_flags;
|
2024-01-29 12:00:58 -07:00
|
|
|
unsigned nr_multishot_loops;
|
2022-09-08 13:20:33 +01:00
|
|
|
u16 flags;
|
2022-09-21 12:17:51 +01:00
|
|
|
/* initialised and used only by !msg send variants */
|
2023-01-22 10:02:55 -07:00
|
|
|
u16 buf_group;
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
bool retry;
|
2023-06-12 13:51:36 -06:00
|
|
|
void __user *msg_control;
|
2022-09-21 12:17:51 +01:00
|
|
|
/* used only for send zerocopy */
|
2022-09-01 11:54:04 +01:00
|
|
|
struct io_kiocb *notif;
|
2022-07-12 21:52:43 +01:00
|
|
|
};
|
|
|
|
|
|
2024-01-29 12:00:58 -07:00
|
|
|
/*
|
|
|
|
|
* Number of times we'll try and do receives if there's more data. If we
|
|
|
|
|
* exceed this limit, then add us to the back of the queue and retry from
|
|
|
|
|
* there. This helps fairness between flooding clients.
|
|
|
|
|
*/
|
|
|
|
|
#define MULTISHOT_MAX_RETRY 32
|
|
|
|
|
|
2025-02-14 16:09:41 -08:00
|
|
|
struct io_recvzc {
|
|
|
|
|
struct file *file;
|
|
|
|
|
unsigned msg_flags;
|
|
|
|
|
u16 flags;
|
2025-02-23 20:13:18 -08:00
|
|
|
u32 len;
|
2025-02-14 16:09:41 -08:00
|
|
|
struct io_zcrx_ifq *ifq;
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_shutdown_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
if (unlikely(sqe->off || sqe->addr || sqe->rw_flags ||
|
|
|
|
|
sqe->buf_index || sqe->splice_fd_in))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
shutdown->how = READ_ONCE(sqe->len);
|
2023-01-27 05:52:25 -08:00
|
|
|
req->flags |= REQ_F_FORCE_ASYNC;
|
2022-05-25 06:25:13 -06:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
|
2022-05-25 06:25:13 -06:00
|
|
|
struct socket *sock;
|
|
|
|
|
int ret;
|
|
|
|
|
|
2023-01-27 05:52:25 -08:00
|
|
|
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
|
|
|
|
ret = __sys_shutdown_sock(sock, shutdown->how);
|
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
|
return IOU_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool io_net_retry(struct socket *sock, int flags)
|
|
|
|
|
{
|
|
|
|
|
if (!(flags & MSG_WAITALL))
|
|
|
|
|
return false;
|
|
|
|
|
return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-16 15:33:53 -06:00
|
|
|
static void io_netmsg_iovec_free(struct io_async_msghdr *kmsg)
|
|
|
|
|
{
|
2025-03-07 16:00:35 +00:00
|
|
|
if (kmsg->vec.iovec)
|
|
|
|
|
io_vec_free(&kmsg->vec);
|
2024-03-16 15:33:53 -06:00
|
|
|
}
|
|
|
|
|
|
2022-07-07 14:30:09 -06:00
|
|
|
static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_async_msghdr *hdr = req->async_data;
|
|
|
|
|
|
2024-03-16 15:33:53 -06:00
|
|
|
/* can't recycle, ensure we free the iovec if we have one */
|
|
|
|
|
if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) {
|
|
|
|
|
io_netmsg_iovec_free(hdr);
|
2022-07-07 14:30:09 -06:00
|
|
|
return;
|
2024-03-16 15:33:53 -06:00
|
|
|
}
|
2022-07-07 14:30:09 -06:00
|
|
|
|
|
|
|
|
/* Let normal cleanup path reap it if we fail adding to the cache */
|
2025-03-07 16:00:35 +00:00
|
|
|
io_alloc_cache_vec_kasan(&hdr->vec);
|
2025-03-07 16:00:37 +00:00
|
|
|
if (hdr->vec.nr > IO_VEC_CACHE_SOFT_CAP)
|
|
|
|
|
io_vec_free(&hdr->vec);
|
|
|
|
|
|
2024-03-20 15:19:44 -06:00
|
|
|
if (io_alloc_cache_put(&req->ctx->netmsg_cache, hdr)) {
|
2022-07-07 14:30:09 -06:00
|
|
|
req->async_data = NULL;
|
2025-03-20 12:25:12 -06:00
|
|
|
req->flags &= ~(REQ_F_ASYNC_DATA|REQ_F_NEED_CLEANUP);
|
2022-07-07 14:30:09 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 10:07:37 -06:00
|
|
|
static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req)
|
2022-07-07 14:30:09 -06:00
|
|
|
{
|
|
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
2022-09-26 14:35:09 +01:00
|
|
|
struct io_async_msghdr *hdr;
|
2022-07-07 14:30:09 -06:00
|
|
|
|
2025-01-22 20:00:57 -07:00
|
|
|
hdr = io_uring_alloc_async_data(&ctx->netmsg_cache, req);
|
2024-12-16 15:46:12 -05:00
|
|
|
if (!hdr)
|
|
|
|
|
return NULL;
|
2022-07-07 14:30:09 -06:00
|
|
|
|
2024-12-16 15:46:12 -05:00
|
|
|
/* If the async data was cached, we might have an iov cached inside. */
|
2025-03-07 16:00:35 +00:00
|
|
|
if (hdr->vec.iovec)
|
2024-12-16 15:46:12 -05:00
|
|
|
req->flags |= REQ_F_NEED_CLEANUP;
|
|
|
|
|
return hdr;
|
2022-07-07 14:30:09 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-25 12:52:39 -07:00
|
|
|
static inline void io_mshot_prep_retry(struct io_kiocb *req,
|
|
|
|
|
struct io_async_msghdr *kmsg)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
|
|
|
|
|
req->flags &= ~REQ_F_BL_EMPTY;
|
|
|
|
|
sr->done_io = 0;
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
sr->retry = false;
|
2024-02-25 12:52:39 -07:00
|
|
|
sr->len = 0; /* get from the provided buffer */
|
|
|
|
|
req->buf_index = sr->buf_group;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 11:41:21 +00:00
|
|
|
static int io_net_import_vec(struct io_kiocb *req, struct io_async_msghdr *iomsg,
|
|
|
|
|
const struct iovec __user *uiov, unsigned uvec_seg,
|
|
|
|
|
int ddir)
|
2024-02-19 14:16:47 -07:00
|
|
|
{
|
2024-03-16 15:33:53 -06:00
|
|
|
struct iovec *iov;
|
|
|
|
|
int ret, nr_segs;
|
|
|
|
|
|
2025-03-07 16:00:35 +00:00
|
|
|
if (iomsg->vec.iovec) {
|
|
|
|
|
nr_segs = iomsg->vec.nr;
|
|
|
|
|
iov = iomsg->vec.iovec;
|
2024-03-16 15:33:53 -06:00
|
|
|
} else {
|
|
|
|
|
nr_segs = 1;
|
2025-02-26 11:41:21 +00:00
|
|
|
iov = &iomsg->fast_iov;
|
2024-03-16 15:33:53 -06:00
|
|
|
}
|
2024-02-19 14:16:47 -07:00
|
|
|
|
2025-02-26 11:41:21 +00:00
|
|
|
ret = __import_iovec(ddir, uiov, uvec_seg, nr_segs, &iov,
|
|
|
|
|
&iomsg->msg.msg_iter, io_is_compat(req->ctx));
|
|
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
return ret;
|
2025-03-28 23:10:55 +00:00
|
|
|
|
|
|
|
|
if (iov) {
|
|
|
|
|
req->flags |= REQ_F_NEED_CLEANUP;
|
|
|
|
|
io_vec_reset_iovec(&iomsg->vec, iov, iomsg->msg.msg_iter.nr_segs);
|
|
|
|
|
}
|
2025-02-26 11:41:21 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 11:09:20 -07:00
|
|
|
static int io_compat_msg_copy_hdr(struct io_kiocb *req,
|
|
|
|
|
struct io_async_msghdr *iomsg,
|
2025-02-26 11:41:18 +00:00
|
|
|
struct compat_msghdr *msg, int ddir,
|
|
|
|
|
struct sockaddr __user **save_addr)
|
2024-02-19 14:16:47 -07:00
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
struct compat_iovec __user *uiov;
|
2025-02-26 11:41:21 +00:00
|
|
|
int ret;
|
2024-03-16 15:33:53 -06:00
|
|
|
|
2024-02-27 11:09:20 -07:00
|
|
|
if (copy_from_user(msg, sr->umsg_compat, sizeof(*msg)))
|
2024-02-19 14:16:47 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
2025-02-26 11:41:18 +00:00
|
|
|
ret = __get_compat_msghdr(&iomsg->msg, msg, save_addr);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
2024-02-27 11:09:20 -07:00
|
|
|
uiov = compat_ptr(msg->msg_iov);
|
2024-02-19 14:16:47 -07:00
|
|
|
if (req->flags & REQ_F_BUFFER_SELECT) {
|
2024-02-27 11:09:20 -07:00
|
|
|
if (msg->msg_iovlen == 0) {
|
2025-02-26 11:41:19 +00:00
|
|
|
sr->len = 0;
|
2024-02-27 11:09:20 -07:00
|
|
|
} else if (msg->msg_iovlen > 1) {
|
2024-02-19 14:16:47 -07:00
|
|
|
return -EINVAL;
|
|
|
|
|
} else {
|
2025-02-26 11:41:16 +00:00
|
|
|
struct compat_iovec tmp_iov;
|
|
|
|
|
|
|
|
|
|
if (copy_from_user(&tmp_iov, uiov, sizeof(tmp_iov)))
|
2024-02-19 14:16:47 -07:00
|
|
|
return -EFAULT;
|
2025-02-26 11:41:16 +00:00
|
|
|
sr->len = tmp_iov.iov_len;
|
2024-02-19 14:16:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-07 16:00:34 +00:00
|
|
|
return 0;
|
2024-02-19 14:16:47 -07:00
|
|
|
}
|
|
|
|
|
|
2025-02-26 11:41:17 +00:00
|
|
|
static int io_copy_msghdr_from_user(struct user_msghdr *msg,
|
|
|
|
|
struct user_msghdr __user *umsg)
|
2024-02-19 14:16:47 -07:00
|
|
|
{
|
2024-10-22 15:43:15 +01:00
|
|
|
if (!user_access_begin(umsg, sizeof(*umsg)))
|
2024-02-19 14:16:47 -07:00
|
|
|
return -EFAULT;
|
2024-10-22 15:43:15 +01:00
|
|
|
unsafe_get_user(msg->msg_name, &umsg->msg_name, ua_end);
|
|
|
|
|
unsafe_get_user(msg->msg_namelen, &umsg->msg_namelen, ua_end);
|
|
|
|
|
unsafe_get_user(msg->msg_iov, &umsg->msg_iov, ua_end);
|
|
|
|
|
unsafe_get_user(msg->msg_iovlen, &umsg->msg_iovlen, ua_end);
|
|
|
|
|
unsafe_get_user(msg->msg_control, &umsg->msg_control, ua_end);
|
|
|
|
|
unsafe_get_user(msg->msg_controllen, &umsg->msg_controllen, ua_end);
|
2025-02-26 11:41:17 +00:00
|
|
|
user_access_end();
|
|
|
|
|
return 0;
|
|
|
|
|
ua_end:
|
|
|
|
|
user_access_end();
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 11:09:20 -07:00
|
|
|
static int io_msg_copy_hdr(struct io_kiocb *req, struct io_async_msghdr *iomsg,
|
2025-02-26 11:41:18 +00:00
|
|
|
struct user_msghdr *msg, int ddir,
|
|
|
|
|
struct sockaddr __user **save_addr)
|
2024-02-19 14:16:47 -07:00
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-10-22 15:43:15 +01:00
|
|
|
struct user_msghdr __user *umsg = sr->umsg;
|
2025-02-26 11:41:21 +00:00
|
|
|
int ret;
|
2024-03-16 15:33:53 -06:00
|
|
|
|
2025-03-07 16:00:33 +00:00
|
|
|
iomsg->msg.msg_name = &iomsg->addr;
|
|
|
|
|
iomsg->msg.msg_iter.nr_segs = 0;
|
|
|
|
|
|
|
|
|
|
if (io_is_compat(req->ctx)) {
|
|
|
|
|
struct compat_msghdr cmsg;
|
|
|
|
|
|
|
|
|
|
ret = io_compat_msg_copy_hdr(req, iomsg, &cmsg, ddir, save_addr);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
memset(msg, 0, sizeof(*msg));
|
|
|
|
|
msg->msg_namelen = cmsg.msg_namelen;
|
|
|
|
|
msg->msg_controllen = cmsg.msg_controllen;
|
|
|
|
|
msg->msg_iov = compat_ptr(cmsg.msg_iov);
|
|
|
|
|
msg->msg_iovlen = cmsg.msg_iovlen;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 11:41:17 +00:00
|
|
|
ret = io_copy_msghdr_from_user(msg, umsg);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
2024-02-19 14:16:47 -07:00
|
|
|
|
2024-02-26 16:43:01 -07:00
|
|
|
msg->msg_flags = 0;
|
|
|
|
|
|
2025-02-26 11:41:18 +00:00
|
|
|
ret = __copy_msghdr(&iomsg->msg, msg, save_addr);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
2024-02-19 14:16:47 -07:00
|
|
|
if (req->flags & REQ_F_BUFFER_SELECT) {
|
2024-02-27 11:09:20 -07:00
|
|
|
if (msg->msg_iovlen == 0) {
|
2025-02-26 11:41:19 +00:00
|
|
|
sr->len = 0;
|
2024-02-27 11:09:20 -07:00
|
|
|
} else if (msg->msg_iovlen > 1) {
|
2025-02-26 11:41:17 +00:00
|
|
|
return -EINVAL;
|
2024-02-19 14:16:47 -07:00
|
|
|
} else {
|
2025-01-28 20:56:13 +00:00
|
|
|
struct iovec __user *uiov = msg->msg_iov;
|
2025-02-26 11:41:19 +00:00
|
|
|
struct iovec tmp_iov;
|
2025-01-28 20:56:13 +00:00
|
|
|
|
2025-02-26 11:41:19 +00:00
|
|
|
if (copy_from_user(&tmp_iov, uiov, sizeof(tmp_iov)))
|
2025-02-26 11:41:17 +00:00
|
|
|
return -EFAULT;
|
2025-02-26 11:41:19 +00:00
|
|
|
sr->len = tmp_iov.iov_len;
|
2024-02-19 14:16:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-03-07 16:00:34 +00:00
|
|
|
return 0;
|
2024-02-19 14:16:47 -07:00
|
|
|
}
|
|
|
|
|
|
2024-03-18 08:09:47 -06:00
|
|
|
void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req)
|
|
|
|
|
{
|
|
|
|
|
struct io_async_msghdr *io = req->async_data;
|
|
|
|
|
|
2024-03-16 15:33:53 -06:00
|
|
|
io_netmsg_iovec_free(io);
|
2024-03-18 08:09:47 -06:00
|
|
|
}
|
|
|
|
|
|
2024-10-22 15:43:13 +01:00
|
|
|
static int io_send_setup(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
2022-08-24 13:07:43 +01:00
|
|
|
{
|
2024-03-05 09:34:21 -07:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 08:09:47 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2024-10-22 15:43:13 +01:00
|
|
|
void __user *addr;
|
|
|
|
|
u16 addr_len;
|
2022-08-24 13:07:43 +01:00
|
|
|
int ret;
|
|
|
|
|
|
2024-10-22 15:43:14 +01:00
|
|
|
sr->buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
|
|
|
|
|
|
2024-10-22 15:43:13 +01:00
|
|
|
if (READ_ONCE(sqe->__pad3[0]))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
2024-03-18 08:09:47 -06:00
|
|
|
kmsg->msg.msg_name = NULL;
|
|
|
|
|
kmsg->msg.msg_namelen = 0;
|
|
|
|
|
kmsg->msg.msg_control = NULL;
|
|
|
|
|
kmsg->msg.msg_controllen = 0;
|
|
|
|
|
kmsg->msg.msg_ubuf = NULL;
|
|
|
|
|
|
2024-10-22 15:43:13 +01:00
|
|
|
addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
|
|
|
|
|
addr_len = READ_ONCE(sqe->addr_len);
|
|
|
|
|
if (addr) {
|
|
|
|
|
ret = move_addr_to_kernel(addr, addr_len, &kmsg->addr);
|
2024-03-18 08:09:47 -06:00
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
return ret;
|
|
|
|
|
kmsg->msg.msg_name = &kmsg->addr;
|
2024-10-22 15:43:13 +01:00
|
|
|
kmsg->msg.msg_namelen = addr_len;
|
2024-03-18 08:09:47 -06:00
|
|
|
}
|
io_uring/net: add provided buffer support for IORING_OP_SEND
It's pretty trivial to wire up provided buffer support for the send
side, just like how it's done the receive side. This enables setting up
a buffer ring that an application can use to push pending sends to,
and then have a send pick a buffer from that ring.
One of the challenges with async IO and networking sends is that you
can get into reordering conditions if you have more than one inflight
at the same time. Consider the following scenario where everything is
fine:
1) App queues sendA for socket1
2) App queues sendB for socket1
3) App does io_uring_submit()
4) sendA is issued, completes successfully, posts CQE
5) sendB is issued, completes successfully, posts CQE
All is fine. Requests are always issued in-order, and both complete
inline as most sends do.
However, if we're flooding socket1 with sends, the following could
also result from the same sequence:
1) App queues sendA for socket1
2) App queues sendB for socket1
3) App does io_uring_submit()
4) sendA is issued, socket1 is full, poll is armed for retry
5) Space frees up in socket1, this triggers sendA retry via task_work
6) sendB is issued, completes successfully, posts CQE
7) sendA is retried, completes successfully, posts CQE
Now we've sent sendB before sendA, which can make things unhappy. If
both sendA and sendB had been using provided buffers, then it would look
as follows instead:
1) App queues dataA for sendA, queues sendA for socket1
2) App queues dataB for sendB queues sendB for socket1
3) App does io_uring_submit()
4) sendA is issued, socket1 is full, poll is armed for retry
5) Space frees up in socket1, this triggers sendA retry via task_work
6) sendB is issued, picks first buffer (dataA), completes successfully,
posts CQE (which says "I sent dataA")
7) sendA is retried, picks first buffer (dataB), completes successfully,
posts CQE (which says "I sent dataB")
Now we've sent the data in order, and everybody is happy.
It's worth noting that this also opens the door for supporting multishot
sends, as provided buffers would be a prerequisite for that. Those can
trigger either when new buffers are added to the outgoing ring, or (if
stalled due to lack of space) when space frees up in the socket.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-02-19 10:46:44 -07:00
|
|
|
if (!io_do_buffer_select(req)) {
|
|
|
|
|
ret = import_ubuf(ITER_SOURCE, sr->buf, sr->len,
|
|
|
|
|
&kmsg->msg.msg_iter);
|
|
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2024-03-18 08:09:47 -06:00
|
|
|
return 0;
|
2022-08-24 13:07:43 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-22 15:43:14 +01:00
|
|
|
static int io_sendmsg_setup(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
2022-05-25 06:25:13 -06:00
|
|
|
{
|
2024-10-22 15:43:14 +01:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-10-22 15:43:12 +01:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2025-03-28 23:10:54 +00:00
|
|
|
struct user_msghdr msg;
|
|
|
|
|
int ret;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2024-10-22 15:43:14 +01:00
|
|
|
sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
|
2025-03-28 23:10:54 +00:00
|
|
|
ret = io_msg_copy_hdr(req, kmsg, &msg, ITER_SOURCE, NULL);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
/* save msg_control as sys_sendmsg() overwrites it */
|
|
|
|
|
sr->msg_control = kmsg->msg.msg_control_user;
|
2024-10-22 15:43:14 +01:00
|
|
|
|
2025-03-28 23:10:54 +00:00
|
|
|
if (req->flags & REQ_F_BUFFER_SELECT)
|
|
|
|
|
return 0;
|
|
|
|
|
return io_net_import_vec(req, kmsg, msg.msg_iov, msg.msg_iovlen, ITER_SOURCE);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2025-03-07 16:00:36 +00:00
|
|
|
static int io_sendmsg_zc_setup(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
|
|
|
|
struct user_msghdr msg;
|
2025-03-08 18:21:15 +00:00
|
|
|
int ret;
|
2025-03-07 16:00:36 +00:00
|
|
|
|
|
|
|
|
if (!(sr->flags & IORING_RECVSEND_FIXED_BUF))
|
|
|
|
|
return io_sendmsg_setup(req, sqe);
|
|
|
|
|
|
|
|
|
|
sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
|
|
|
|
|
|
|
|
|
|
ret = io_msg_copy_hdr(req, kmsg, &msg, ITER_SOURCE, NULL);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
sr->msg_control = kmsg->msg.msg_control_user;
|
|
|
|
|
kmsg->msg.msg_iter.nr_segs = msg.msg_iovlen;
|
2025-03-08 18:21:15 +00:00
|
|
|
|
|
|
|
|
return io_prep_reg_iovec(req, &kmsg->vec, msg.msg_iov, msg.msg_iovlen);
|
2025-03-07 16:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-05 13:10:04 -07:00
|
|
|
#define SENDMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_BUNDLE)
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2024-03-07 12:43:22 -07:00
|
|
|
sr->done_io = 0;
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
sr->retry = false;
|
2024-03-07 12:43:22 -07:00
|
|
|
|
2024-10-22 15:43:13 +01:00
|
|
|
if (req->opcode != IORING_OP_SEND) {
|
|
|
|
|
if (sqe->addr2 || sqe->file_index)
|
2022-09-21 12:17:51 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
sr->len = READ_ONCE(sqe->len);
|
|
|
|
|
sr->flags = READ_ONCE(sqe->ioprio);
|
2024-03-05 13:10:04 -07:00
|
|
|
if (sr->flags & ~SENDMSG_FLAGS)
|
2022-05-25 06:25:13 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
|
|
|
|
|
if (sr->msg_flags & MSG_DONTWAIT)
|
|
|
|
|
req->flags |= REQ_F_NOWAIT;
|
2024-03-05 13:10:04 -07:00
|
|
|
if (sr->flags & IORING_RECVSEND_BUNDLE) {
|
|
|
|
|
if (req->opcode == IORING_OP_SENDMSG)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (!(req->flags & REQ_F_BUFFER_SELECT))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
sr->msg_flags |= MSG_WAITALL;
|
|
|
|
|
sr->buf_group = req->buf_index;
|
|
|
|
|
req->buf_list = NULL;
|
2025-03-27 09:57:27 +00:00
|
|
|
req->flags |= REQ_F_MULTISHOT;
|
2024-03-05 13:10:04 -07:00
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2025-02-24 12:42:24 +00:00
|
|
|
if (io_is_compat(req->ctx))
|
2022-05-25 06:25:13 -06:00
|
|
|
sr->msg_flags |= MSG_CMSG_COMPAT;
|
2025-02-24 12:42:24 +00:00
|
|
|
|
2024-10-22 15:43:12 +01:00
|
|
|
if (unlikely(!io_msg_alloc_async(req)))
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
if (req->opcode != IORING_OP_SENDMSG)
|
2024-10-22 15:43:13 +01:00
|
|
|
return io_send_setup(req, sqe);
|
2024-10-22 15:43:14 +01:00
|
|
|
return io_sendmsg_setup(req, sqe);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-06 07:57:57 -07:00
|
|
|
static void io_req_msg_cleanup(struct io_kiocb *req,
|
|
|
|
|
unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
io_netmsg_recycle(req, issue_flags);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 13:10:04 -07:00
|
|
|
/*
|
|
|
|
|
* For bundle completions, we need to figure out how many segments we consumed.
|
|
|
|
|
* A bundle could be using a single ITER_UBUF if that's all we mapped, or it
|
|
|
|
|
* could be using an ITER_IOVEC. If the latter, then if we consumed all of
|
|
|
|
|
* the segments, then it's a trivial questiont o answer. If we have residual
|
|
|
|
|
* data in the iter, then loop the segments to figure out how much we
|
|
|
|
|
* transferred.
|
|
|
|
|
*/
|
|
|
|
|
static int io_bundle_nbufs(struct io_async_msghdr *kmsg, int ret)
|
|
|
|
|
{
|
|
|
|
|
struct iovec *iov;
|
|
|
|
|
int nbufs;
|
|
|
|
|
|
|
|
|
|
/* no data is always zero segments, and a ubuf is always 1 segment */
|
|
|
|
|
if (ret <= 0)
|
|
|
|
|
return 0;
|
|
|
|
|
if (iter_is_ubuf(&kmsg->msg.msg_iter))
|
|
|
|
|
return 1;
|
|
|
|
|
|
2025-03-07 16:00:35 +00:00
|
|
|
iov = kmsg->vec.iovec;
|
2024-03-05 13:10:04 -07:00
|
|
|
if (!iov)
|
|
|
|
|
iov = &kmsg->fast_iov;
|
|
|
|
|
|
|
|
|
|
/* if all data was transferred, it's basic pointer math */
|
|
|
|
|
if (!iov_iter_count(&kmsg->msg.msg_iter))
|
|
|
|
|
return iter_iov(&kmsg->msg.msg_iter) - iov;
|
|
|
|
|
|
|
|
|
|
/* short transfer, count segments */
|
|
|
|
|
nbufs = 0;
|
|
|
|
|
do {
|
|
|
|
|
int this_len = min_t(int, iov[nbufs].iov_len, ret);
|
|
|
|
|
|
|
|
|
|
nbufs++;
|
|
|
|
|
ret -= this_len;
|
|
|
|
|
} while (ret);
|
|
|
|
|
|
|
|
|
|
return nbufs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool io_send_finish(struct io_kiocb *req, int *ret,
|
|
|
|
|
struct io_async_msghdr *kmsg,
|
|
|
|
|
unsigned issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
bool bundle_finished = *ret <= 0;
|
|
|
|
|
unsigned int cflags;
|
|
|
|
|
|
|
|
|
|
if (!(sr->flags & IORING_RECVSEND_BUNDLE)) {
|
2024-08-27 08:26:07 -06:00
|
|
|
cflags = io_put_kbuf(req, *ret, issue_flags);
|
2024-03-05 13:10:04 -07:00
|
|
|
goto finish;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 08:26:07 -06:00
|
|
|
cflags = io_put_kbufs(req, *ret, io_bundle_nbufs(kmsg, *ret), issue_flags);
|
2024-03-05 13:10:04 -07:00
|
|
|
|
|
|
|
|
if (bundle_finished || req->flags & REQ_F_BL_EMPTY)
|
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Fill CQE for this receive and see if we should keep trying to
|
|
|
|
|
* receive from this socket.
|
|
|
|
|
*/
|
|
|
|
|
if (io_req_post_cqe(req, *ret, cflags | IORING_CQE_F_MORE)) {
|
|
|
|
|
io_mshot_prep_retry(req, kmsg);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Otherwise stop bundle and use the current result. */
|
|
|
|
|
finish:
|
|
|
|
|
io_req_set_res(req, *ret, cflags);
|
|
|
|
|
*ret = IOU_OK;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 08:09:47 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2022-05-25 06:25:13 -06:00
|
|
|
struct socket *sock;
|
|
|
|
|
unsigned flags;
|
|
|
|
|
int min_ret = 0;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(sr->flags & IORING_RECVSEND_POLL_FIRST))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
flags = sr->msg_flags;
|
|
|
|
|
if (issue_flags & IO_URING_F_NONBLOCK)
|
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
|
if (flags & MSG_WAITALL)
|
|
|
|
|
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
|
|
|
|
|
|
2024-04-12 12:39:54 -06:00
|
|
|
kmsg->msg.msg_control_user = sr->msg_control;
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
|
|
|
|
|
|
|
|
|
|
if (ret < min_ret) {
|
|
|
|
|
if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret > 0 && io_net_retry(sock, flags)) {
|
2023-06-19 09:35:34 -06:00
|
|
|
kmsg->msg.msg_controllen = 0;
|
|
|
|
|
kmsg->msg.msg_control = NULL;
|
2022-05-25 06:25:13 -06:00
|
|
|
sr->done_io += ret;
|
2024-03-07 12:53:24 -07:00
|
|
|
req->flags |= REQ_F_BL_NO_RECYCLE;
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
2022-09-08 13:20:29 +01:00
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
2022-05-25 06:25:13 -06:00
|
|
|
req_set_fail(req);
|
|
|
|
|
}
|
2024-03-18 13:52:42 -06:00
|
|
|
io_req_msg_cleanup(req, issue_flags);
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret >= 0)
|
|
|
|
|
ret += sr->done_io;
|
|
|
|
|
else if (sr->done_io)
|
|
|
|
|
ret = sr->done_io;
|
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
|
return IOU_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 20:56:14 +00:00
|
|
|
static int io_send_select_buffer(struct io_kiocb *req, unsigned int issue_flags,
|
|
|
|
|
struct io_async_msghdr *kmsg)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
struct buf_sel_arg arg = {
|
|
|
|
|
.iovs = &kmsg->fast_iov,
|
|
|
|
|
.max_len = min_not_zero(sr->len, INT_MAX),
|
|
|
|
|
.nr_iovs = 1,
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-07 16:00:35 +00:00
|
|
|
if (kmsg->vec.iovec) {
|
|
|
|
|
arg.nr_iovs = kmsg->vec.nr;
|
|
|
|
|
arg.iovs = kmsg->vec.iovec;
|
2025-01-28 20:56:14 +00:00
|
|
|
arg.mode = KBUF_MODE_FREE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!(sr->flags & IORING_RECVSEND_BUNDLE))
|
|
|
|
|
arg.nr_iovs = 1;
|
|
|
|
|
else
|
|
|
|
|
arg.mode |= KBUF_MODE_EXPAND;
|
|
|
|
|
|
|
|
|
|
ret = io_buffers_select(req, &arg, issue_flags);
|
|
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
return ret;
|
|
|
|
|
|
2025-03-07 16:00:35 +00:00
|
|
|
if (arg.iovs != &kmsg->fast_iov && arg.iovs != kmsg->vec.iovec) {
|
|
|
|
|
kmsg->vec.nr = ret;
|
|
|
|
|
kmsg->vec.iovec = arg.iovs;
|
2025-01-28 20:56:14 +00:00
|
|
|
req->flags |= REQ_F_NEED_CLEANUP;
|
|
|
|
|
}
|
|
|
|
|
sr->len = arg.out_len;
|
|
|
|
|
|
|
|
|
|
if (ret == 1) {
|
|
|
|
|
sr->buf = arg.iovs[0].iov_base;
|
|
|
|
|
ret = import_ubuf(ITER_SOURCE, sr->buf, sr->len,
|
|
|
|
|
&kmsg->msg.msg_iter);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
} else {
|
|
|
|
|
iov_iter_init(&kmsg->msg.msg_iter, ITER_SOURCE,
|
|
|
|
|
arg.iovs, ret, arg.out_len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 09:34:21 -07:00
|
|
|
int io_send(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 08:09:47 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2024-03-05 09:34:21 -07:00
|
|
|
struct socket *sock;
|
|
|
|
|
unsigned flags;
|
|
|
|
|
int min_ret = 0;
|
|
|
|
|
int ret;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
2024-03-18 08:09:47 -06:00
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(sr->flags & IORING_RECVSEND_POLL_FIRST))
|
|
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
flags = sr->msg_flags;
|
|
|
|
|
if (issue_flags & IO_URING_F_NONBLOCK)
|
|
|
|
|
flags |= MSG_DONTWAIT;
|
2024-03-05 13:10:04 -07:00
|
|
|
|
|
|
|
|
retry_bundle:
|
|
|
|
|
if (io_do_buffer_select(req)) {
|
2025-01-28 20:56:14 +00:00
|
|
|
ret = io_send_select_buffer(req, issue_flags, kmsg);
|
|
|
|
|
if (ret)
|
2024-03-05 13:10:04 -07:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If MSG_WAITALL is set, or this is a bundle send, then we need
|
|
|
|
|
* the full amount. If just bundle is set, if we do a short send
|
|
|
|
|
* then we complete the bundle sequence rather than continue on.
|
|
|
|
|
*/
|
|
|
|
|
if (flags & MSG_WAITALL || sr->flags & IORING_RECVSEND_BUNDLE)
|
2024-03-05 09:34:21 -07:00
|
|
|
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2023-05-22 13:11:10 +01:00
|
|
|
flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
|
2024-03-05 09:34:21 -07:00
|
|
|
kmsg->msg.msg_flags = flags;
|
|
|
|
|
ret = sock_sendmsg(sock, &kmsg->msg);
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret < min_ret) {
|
|
|
|
|
if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-09-21 12:17:51 +01:00
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret > 0 && io_net_retry(sock, flags)) {
|
|
|
|
|
sr->len -= ret;
|
|
|
|
|
sr->buf += ret;
|
|
|
|
|
sr->done_io += ret;
|
2024-03-07 12:53:24 -07:00
|
|
|
req->flags |= REQ_F_BL_NO_RECYCLE;
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
2022-09-08 13:20:29 +01:00
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
2022-05-25 06:25:13 -06:00
|
|
|
req_set_fail(req);
|
|
|
|
|
}
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
ret += sr->done_io;
|
|
|
|
|
else if (sr->done_io)
|
|
|
|
|
ret = sr->done_io;
|
2024-03-05 13:10:04 -07:00
|
|
|
|
|
|
|
|
if (!io_send_finish(req, &ret, kmsg, issue_flags))
|
|
|
|
|
goto retry_bundle;
|
|
|
|
|
|
2024-03-18 13:52:42 -06:00
|
|
|
io_req_msg_cleanup(req, issue_flags);
|
2024-03-05 13:10:04 -07:00
|
|
|
return ret;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2024-02-27 11:09:20 -07:00
|
|
|
static int io_recvmsg_mshot_prep(struct io_kiocb *req,
|
|
|
|
|
struct io_async_msghdr *iomsg,
|
2024-03-01 19:43:48 +05:00
|
|
|
int namelen, size_t controllen)
|
2022-07-14 04:02:58 -07:00
|
|
|
{
|
2024-02-27 11:09:20 -07:00
|
|
|
if ((req->flags & (REQ_F_APOLL_MULTISHOT|REQ_F_BUFFER_SELECT)) ==
|
|
|
|
|
(REQ_F_APOLL_MULTISHOT|REQ_F_BUFFER_SELECT)) {
|
|
|
|
|
int hdr;
|
|
|
|
|
|
|
|
|
|
if (unlikely(namelen < 0))
|
|
|
|
|
return -EOVERFLOW;
|
2024-03-01 18:29:39 +03:00
|
|
|
if (check_add_overflow(sizeof(struct io_uring_recvmsg_out),
|
2024-02-27 11:09:20 -07:00
|
|
|
namelen, &hdr))
|
|
|
|
|
return -EOVERFLOW;
|
2024-03-01 18:29:39 +03:00
|
|
|
if (check_add_overflow(hdr, controllen, &hdr))
|
2024-02-27 11:09:20 -07:00
|
|
|
return -EOVERFLOW;
|
|
|
|
|
|
|
|
|
|
iomsg->namelen = namelen;
|
|
|
|
|
iomsg->controllen = controllen;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-07-14 04:02:58 -07:00
|
|
|
|
2024-02-27 11:09:20 -07:00
|
|
|
return 0;
|
2022-07-14 04:02:58 -07:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
static int io_recvmsg_copy_hdr(struct io_kiocb *req,
|
|
|
|
|
struct io_async_msghdr *iomsg)
|
|
|
|
|
{
|
2022-07-14 04:02:56 -07:00
|
|
|
struct user_msghdr msg;
|
2022-05-25 06:25:13 -06:00
|
|
|
int ret;
|
|
|
|
|
|
2025-03-07 16:00:33 +00:00
|
|
|
ret = io_msg_copy_hdr(req, iomsg, &msg, ITER_DEST, &iomsg->uaddr);
|
2024-02-27 11:09:20 -07:00
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
2025-03-07 16:00:34 +00:00
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_BUFFER_SELECT)) {
|
|
|
|
|
ret = io_net_import_vec(req, iomsg, msg.msg_iov, msg.msg_iovlen,
|
|
|
|
|
ITER_DEST);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2024-02-27 11:09:20 -07:00
|
|
|
return io_recvmsg_mshot_prep(req, iomsg, msg.msg_namelen,
|
|
|
|
|
msg.msg_controllen);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-18 07:36:03 -06:00
|
|
|
static int io_recvmsg_prep_setup(struct io_kiocb *req)
|
2022-05-25 06:25:13 -06:00
|
|
|
{
|
2024-03-15 16:36:23 -06:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 07:36:03 -06:00
|
|
|
struct io_async_msghdr *kmsg;
|
2022-05-25 06:25:13 -06:00
|
|
|
int ret;
|
|
|
|
|
|
2024-03-18 10:07:37 -06:00
|
|
|
kmsg = io_msg_alloc_async(req);
|
2024-03-18 07:36:03 -06:00
|
|
|
if (unlikely(!kmsg))
|
2022-09-08 13:20:30 +01:00
|
|
|
return -ENOMEM;
|
2024-03-18 07:36:03 -06:00
|
|
|
|
|
|
|
|
if (req->opcode == IORING_OP_RECV) {
|
|
|
|
|
kmsg->msg.msg_name = NULL;
|
|
|
|
|
kmsg->msg.msg_namelen = 0;
|
2025-01-02 16:32:51 -07:00
|
|
|
kmsg->msg.msg_inq = 0;
|
2024-03-18 07:36:03 -06:00
|
|
|
kmsg->msg.msg_control = NULL;
|
|
|
|
|
kmsg->msg.msg_get_inq = 1;
|
|
|
|
|
kmsg->msg.msg_controllen = 0;
|
|
|
|
|
kmsg->msg.msg_iocb = NULL;
|
|
|
|
|
kmsg->msg.msg_ubuf = NULL;
|
|
|
|
|
|
|
|
|
|
if (!io_do_buffer_select(req)) {
|
|
|
|
|
ret = import_ubuf(ITER_DEST, sr->buf, sr->len,
|
|
|
|
|
&kmsg->msg.msg_iter);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 11:41:15 +00:00
|
|
|
return io_recvmsg_copy_hdr(req, kmsg);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-05 16:22:04 -07:00
|
|
|
#define RECVMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT | \
|
|
|
|
|
IORING_RECVSEND_BUNDLE)
|
2022-06-30 02:12:29 -07:00
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2024-03-07 12:43:22 -07:00
|
|
|
sr->done_io = 0;
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
sr->retry = false;
|
2024-03-07 12:43:22 -07:00
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
if (unlikely(sqe->file_index || sqe->addr2))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
|
|
|
|
|
sr->len = READ_ONCE(sqe->len);
|
|
|
|
|
sr->flags = READ_ONCE(sqe->ioprio);
|
2024-03-05 16:22:04 -07:00
|
|
|
if (sr->flags & ~RECVMSG_FLAGS)
|
2022-05-25 06:25:13 -06:00
|
|
|
return -EINVAL;
|
2023-02-24 16:01:24 +01:00
|
|
|
sr->msg_flags = READ_ONCE(sqe->msg_flags);
|
2022-05-25 06:25:13 -06:00
|
|
|
if (sr->msg_flags & MSG_DONTWAIT)
|
|
|
|
|
req->flags |= REQ_F_NOWAIT;
|
|
|
|
|
if (sr->msg_flags & MSG_ERRQUEUE)
|
|
|
|
|
req->flags |= REQ_F_CLEAR_POLLIN;
|
2024-03-05 16:22:04 -07:00
|
|
|
if (req->flags & REQ_F_BUFFER_SELECT) {
|
2023-01-22 10:02:55 -07:00
|
|
|
/*
|
|
|
|
|
* Store the buffer group for this multishot receive separately,
|
|
|
|
|
* as if we end up doing an io-wq based issue that selects a
|
|
|
|
|
* buffer, it has to be committed immediately and that will
|
|
|
|
|
* clear ->buf_list. This means we lose the link to the buffer
|
|
|
|
|
* list, and the eventual buffer put on completion then cannot
|
|
|
|
|
* restore it.
|
|
|
|
|
*/
|
|
|
|
|
sr->buf_group = req->buf_index;
|
2024-03-05 16:22:04 -07:00
|
|
|
req->buf_list = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (sr->flags & IORING_RECV_MULTISHOT) {
|
|
|
|
|
if (!(req->flags & REQ_F_BUFFER_SELECT))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (sr->msg_flags & MSG_WAITALL)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (req->opcode == IORING_OP_RECV && sr->len)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
req->flags |= REQ_F_APOLL_MULTISHOT;
|
|
|
|
|
}
|
|
|
|
|
if (sr->flags & IORING_RECVSEND_BUNDLE) {
|
|
|
|
|
if (req->opcode == IORING_OP_RECVMSG)
|
|
|
|
|
return -EINVAL;
|
2022-06-30 02:12:29 -07:00
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2025-02-24 12:42:24 +00:00
|
|
|
if (io_is_compat(req->ctx))
|
2022-05-25 06:25:13 -06:00
|
|
|
sr->msg_flags |= MSG_CMSG_COMPAT;
|
2025-02-24 12:42:24 +00:00
|
|
|
|
2024-01-29 12:00:58 -07:00
|
|
|
sr->nr_multishot_loops = 0;
|
2024-03-18 07:36:03 -06:00
|
|
|
return io_recvmsg_prep_setup(req);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
/* bits to clear in old and inherit in new cflags on bundle retry */
|
|
|
|
|
#define CQE_F_MASK (IORING_CQE_F_SOCK_NONEMPTY|IORING_CQE_F_MORE)
|
|
|
|
|
|
2022-06-30 02:12:29 -07:00
|
|
|
/*
|
2022-07-14 04:02:58 -07:00
|
|
|
* Finishes io_recv and io_recvmsg.
|
2022-06-30 02:12:29 -07:00
|
|
|
*
|
|
|
|
|
* Returns true if it is actually finished, or false if it should run
|
|
|
|
|
* again (for multishot).
|
|
|
|
|
*/
|
2022-07-14 04:02:58 -07:00
|
|
|
static inline bool io_recv_finish(struct io_kiocb *req, int *ret,
|
2024-03-05 15:39:16 -07:00
|
|
|
struct io_async_msghdr *kmsg,
|
|
|
|
|
bool mshot_finished, unsigned issue_flags)
|
2022-06-30 02:12:29 -07:00
|
|
|
{
|
2024-03-05 16:22:04 -07:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-07-02 09:16:46 -06:00
|
|
|
unsigned int cflags = 0;
|
2024-03-05 16:22:04 -07:00
|
|
|
|
2024-03-05 15:39:16 -07:00
|
|
|
if (kmsg->msg.msg_inq > 0)
|
2023-05-17 12:20:44 -06:00
|
|
|
cflags |= IORING_CQE_F_SOCK_NONEMPTY;
|
|
|
|
|
|
2024-07-02 09:16:46 -06:00
|
|
|
if (sr->flags & IORING_RECVSEND_BUNDLE) {
|
2024-08-27 08:26:07 -06:00
|
|
|
cflags |= io_put_kbufs(req, *ret, io_bundle_nbufs(kmsg, *ret),
|
2024-07-02 09:16:46 -06:00
|
|
|
issue_flags);
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
if (sr->retry)
|
|
|
|
|
cflags = req->cqe.flags | (cflags & CQE_F_MASK);
|
2024-07-02 09:16:46 -06:00
|
|
|
/* bundle with no more immediate buffers, we're done */
|
|
|
|
|
if (req->flags & REQ_F_BL_EMPTY)
|
|
|
|
|
goto finish;
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
/* if more is available, retry and append to this one */
|
|
|
|
|
if (!sr->retry && kmsg->msg.msg_inq > 0 && *ret > 0) {
|
|
|
|
|
req->cqe.flags = cflags & ~CQE_F_MASK;
|
|
|
|
|
sr->len = kmsg->msg.msg_inq;
|
|
|
|
|
sr->done_io += *ret;
|
|
|
|
|
sr->retry = true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-07-02 09:16:46 -06:00
|
|
|
} else {
|
2024-08-27 08:26:07 -06:00
|
|
|
cflags |= io_put_kbuf(req, *ret, issue_flags);
|
2024-07-02 09:16:46 -06:00
|
|
|
}
|
2024-03-05 16:22:04 -07:00
|
|
|
|
2024-01-29 11:54:18 -07:00
|
|
|
/*
|
|
|
|
|
* Fill CQE for this receive and see if we should keep trying to
|
|
|
|
|
* receive from this socket.
|
|
|
|
|
*/
|
2024-03-08 13:55:58 +00:00
|
|
|
if ((req->flags & REQ_F_APOLL_MULTISHOT) && !mshot_finished &&
|
2024-03-18 22:00:31 +00:00
|
|
|
io_req_post_cqe(req, *ret, cflags | IORING_CQE_F_MORE)) {
|
2025-03-08 17:19:32 +00:00
|
|
|
*ret = IOU_RETRY;
|
2024-02-25 12:52:39 -07:00
|
|
|
io_mshot_prep_retry(req, kmsg);
|
2024-01-29 11:54:18 -07:00
|
|
|
/* Known not-empty or unknown state, retry */
|
2024-03-05 15:39:16 -07:00
|
|
|
if (cflags & IORING_CQE_F_SOCK_NONEMPTY || kmsg->msg.msg_inq < 0) {
|
2024-01-29 12:00:58 -07:00
|
|
|
if (sr->nr_multishot_loops++ < MULTISHOT_MAX_RETRY)
|
|
|
|
|
return false;
|
|
|
|
|
/* mshot retries exceeded, force a requeue */
|
|
|
|
|
sr->nr_multishot_loops = 0;
|
2025-03-08 17:19:32 +00:00
|
|
|
if (issue_flags & IO_URING_F_MULTISHOT)
|
|
|
|
|
*ret = IOU_REQUEUE;
|
2024-01-29 12:00:58 -07:00
|
|
|
}
|
2024-01-29 11:54:18 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
2024-03-08 13:55:58 +00:00
|
|
|
|
|
|
|
|
/* Finish the request / stop multishot. */
|
2024-03-05 16:22:04 -07:00
|
|
|
finish:
|
2022-06-30 02:12:29 -07:00
|
|
|
io_req_set_res(req, *ret, cflags);
|
2025-03-08 17:19:33 +00:00
|
|
|
*ret = IOU_COMPLETE;
|
2024-03-18 13:52:42 -06:00
|
|
|
io_req_msg_cleanup(req, issue_flags);
|
2022-06-30 02:12:29 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 04:02:58 -07:00
|
|
|
static int io_recvmsg_prep_multishot(struct io_async_msghdr *kmsg,
|
|
|
|
|
struct io_sr_msg *sr, void __user **buf,
|
|
|
|
|
size_t *len)
|
|
|
|
|
{
|
|
|
|
|
unsigned long ubuf = (unsigned long) *buf;
|
|
|
|
|
unsigned long hdr;
|
|
|
|
|
|
|
|
|
|
hdr = sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
|
|
|
|
|
kmsg->controllen;
|
|
|
|
|
if (*len < hdr)
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
|
|
if (kmsg->controllen) {
|
|
|
|
|
unsigned long control = ubuf + hdr - kmsg->controllen;
|
|
|
|
|
|
2022-08-05 04:54:50 -07:00
|
|
|
kmsg->msg.msg_control_user = (void __user *) control;
|
2022-07-14 04:02:58 -07:00
|
|
|
kmsg->msg.msg_controllen = kmsg->controllen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sr->buf = *buf; /* stash for later copy */
|
2022-08-05 04:54:50 -07:00
|
|
|
*buf = (void __user *) (ubuf + hdr);
|
2022-07-14 04:02:58 -07:00
|
|
|
kmsg->payloadlen = *len = *len - hdr;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct io_recvmsg_multishot_hdr {
|
|
|
|
|
struct io_uring_recvmsg_out msg;
|
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int io_recvmsg_multishot(struct socket *sock, struct io_sr_msg *io,
|
|
|
|
|
struct io_async_msghdr *kmsg,
|
|
|
|
|
unsigned int flags, bool *finished)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
int copy_len;
|
|
|
|
|
struct io_recvmsg_multishot_hdr hdr;
|
|
|
|
|
|
|
|
|
|
if (kmsg->namelen)
|
|
|
|
|
kmsg->msg.msg_name = &hdr.addr;
|
|
|
|
|
kmsg->msg.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
|
|
|
|
|
kmsg->msg.msg_namelen = 0;
|
|
|
|
|
|
|
|
|
|
if (sock->file->f_flags & O_NONBLOCK)
|
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
|
|
|
|
|
|
err = sock_recvmsg(sock, &kmsg->msg, flags);
|
|
|
|
|
*finished = err <= 0;
|
|
|
|
|
if (err < 0)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
hdr.msg = (struct io_uring_recvmsg_out) {
|
|
|
|
|
.controllen = kmsg->controllen - kmsg->msg.msg_controllen,
|
|
|
|
|
.flags = kmsg->msg.msg_flags & ~MSG_CMSG_COMPAT
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
hdr.msg.payloadlen = err;
|
|
|
|
|
if (err > kmsg->payloadlen)
|
|
|
|
|
err = kmsg->payloadlen;
|
|
|
|
|
|
|
|
|
|
copy_len = sizeof(struct io_uring_recvmsg_out);
|
|
|
|
|
if (kmsg->msg.msg_namelen > kmsg->namelen)
|
|
|
|
|
copy_len += kmsg->namelen;
|
|
|
|
|
else
|
|
|
|
|
copy_len += kmsg->msg.msg_namelen;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* "fromlen shall refer to the value before truncation.."
|
|
|
|
|
* 1003.1g
|
|
|
|
|
*/
|
|
|
|
|
hdr.msg.namelen = kmsg->msg.msg_namelen;
|
|
|
|
|
|
|
|
|
|
/* ensure that there is no gap between hdr and sockaddr_storage */
|
|
|
|
|
BUILD_BUG_ON(offsetof(struct io_recvmsg_multishot_hdr, addr) !=
|
|
|
|
|
sizeof(struct io_uring_recvmsg_out));
|
|
|
|
|
if (copy_to_user(io->buf, &hdr, copy_len)) {
|
|
|
|
|
*finished = true;
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
|
|
|
|
|
kmsg->controllen + err;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 07:36:03 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2022-05-25 06:25:13 -06:00
|
|
|
struct socket *sock;
|
|
|
|
|
unsigned flags;
|
|
|
|
|
int ret, min_ret = 0;
|
|
|
|
|
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
|
2022-07-14 04:02:58 -07:00
|
|
|
bool mshot_finished = true;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(sr->flags & IORING_RECVSEND_POLL_FIRST))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2024-02-25 12:59:05 -07:00
|
|
|
flags = sr->msg_flags;
|
|
|
|
|
if (force_nonblock)
|
|
|
|
|
flags |= MSG_DONTWAIT;
|
2022-12-07 03:53:31 +00:00
|
|
|
|
2022-07-14 04:02:58 -07:00
|
|
|
retry_multishot:
|
2022-05-25 06:25:13 -06:00
|
|
|
if (io_do_buffer_select(req)) {
|
|
|
|
|
void __user *buf;
|
2022-07-14 04:02:58 -07:00
|
|
|
size_t len = sr->len;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2022-07-14 04:02:58 -07:00
|
|
|
buf = io_buffer_select(req, &len, issue_flags);
|
2022-05-25 06:25:13 -06:00
|
|
|
if (!buf)
|
|
|
|
|
return -ENOBUFS;
|
2022-07-14 04:02:58 -07:00
|
|
|
|
|
|
|
|
if (req->flags & REQ_F_APOLL_MULTISHOT) {
|
|
|
|
|
ret = io_recvmsg_prep_multishot(kmsg, sr, &buf, &len);
|
|
|
|
|
if (ret) {
|
|
|
|
|
io_kbuf_recycle(req, issue_flags);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 11:07:31 -08:00
|
|
|
iov_iter_ubuf(&kmsg->msg.msg_iter, ITER_DEST, buf, len);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
kmsg->msg.msg_get_inq = 1;
|
2023-06-27 12:05:01 -06:00
|
|
|
kmsg->msg.msg_inq = -1;
|
2023-06-19 09:41:05 -06:00
|
|
|
if (req->flags & REQ_F_APOLL_MULTISHOT) {
|
2022-07-14 04:02:58 -07:00
|
|
|
ret = io_recvmsg_multishot(sock, sr, kmsg, flags,
|
|
|
|
|
&mshot_finished);
|
2023-06-19 09:41:05 -06:00
|
|
|
} else {
|
|
|
|
|
/* disable partial retry for recvmsg with cmsg attached */
|
|
|
|
|
if (flags & MSG_WAITALL && !kmsg->msg.msg_controllen)
|
|
|
|
|
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
|
|
|
|
|
|
2022-07-14 04:02:58 -07:00
|
|
|
ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg,
|
|
|
|
|
kmsg->uaddr, flags);
|
2023-06-19 09:41:05 -06:00
|
|
|
}
|
2022-07-14 04:02:58 -07:00
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret < min_ret) {
|
2022-07-14 04:02:58 -07:00
|
|
|
if (ret == -EAGAIN && force_nonblock) {
|
2025-03-08 17:19:32 +00:00
|
|
|
if (issue_flags & IO_URING_F_MULTISHOT)
|
2022-07-14 04:02:58 -07:00
|
|
|
io_kbuf_recycle(req, issue_flags);
|
2025-03-08 17:19:32 +00:00
|
|
|
|
|
|
|
|
return IOU_RETRY;
|
2022-07-14 04:02:58 -07:00
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret > 0 && io_net_retry(sock, flags)) {
|
|
|
|
|
sr->done_io += ret;
|
2024-03-07 12:53:24 -07:00
|
|
|
req->flags |= REQ_F_BL_NO_RECYCLE;
|
2025-03-08 17:19:32 +00:00
|
|
|
return IOU_RETRY;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
2022-09-08 13:20:29 +01:00
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
2022-05-25 06:25:13 -06:00
|
|
|
req_set_fail(req);
|
|
|
|
|
} else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
|
|
|
|
|
req_set_fail(req);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-30 02:12:23 -07:00
|
|
|
if (ret > 0)
|
2022-05-25 06:25:13 -06:00
|
|
|
ret += sr->done_io;
|
|
|
|
|
else if (sr->done_io)
|
|
|
|
|
ret = sr->done_io;
|
2022-06-30 02:12:23 -07:00
|
|
|
else
|
|
|
|
|
io_kbuf_recycle(req, issue_flags);
|
|
|
|
|
|
2024-03-05 15:39:16 -07:00
|
|
|
if (!io_recv_finish(req, &ret, kmsg, mshot_finished, issue_flags))
|
2022-07-14 04:02:58 -07:00
|
|
|
goto retry_multishot;
|
|
|
|
|
|
|
|
|
|
return ret;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-05 16:22:04 -07:00
|
|
|
static int io_recv_buf_select(struct io_kiocb *req, struct io_async_msghdr *kmsg,
|
|
|
|
|
size_t *len, unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If the ring isn't locked, then don't use the peek interface
|
|
|
|
|
* to grab multiple buffers as we will lock/unlock between
|
|
|
|
|
* this selection and posting the buffers.
|
|
|
|
|
*/
|
|
|
|
|
if (!(issue_flags & IO_URING_F_UNLOCKED) &&
|
|
|
|
|
sr->flags & IORING_RECVSEND_BUNDLE) {
|
|
|
|
|
struct buf_sel_arg arg = {
|
|
|
|
|
.iovs = &kmsg->fast_iov,
|
|
|
|
|
.nr_iovs = 1,
|
|
|
|
|
.mode = KBUF_MODE_EXPAND,
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-07 16:00:35 +00:00
|
|
|
if (kmsg->vec.iovec) {
|
|
|
|
|
arg.nr_iovs = kmsg->vec.nr;
|
|
|
|
|
arg.iovs = kmsg->vec.iovec;
|
2024-03-05 16:22:04 -07:00
|
|
|
arg.mode |= KBUF_MODE_FREE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (kmsg->msg.msg_inq > 0)
|
|
|
|
|
arg.max_len = min_not_zero(sr->len, kmsg->msg.msg_inq);
|
|
|
|
|
|
|
|
|
|
ret = io_buffers_peek(req, &arg);
|
|
|
|
|
if (unlikely(ret < 0))
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
/* special case 1 vec, can be a fast path */
|
|
|
|
|
if (ret == 1) {
|
|
|
|
|
sr->buf = arg.iovs[0].iov_base;
|
|
|
|
|
sr->len = arg.iovs[0].iov_len;
|
|
|
|
|
goto map_ubuf;
|
|
|
|
|
}
|
|
|
|
|
iov_iter_init(&kmsg->msg.msg_iter, ITER_DEST, arg.iovs, ret,
|
|
|
|
|
arg.out_len);
|
2025-03-07 16:00:35 +00:00
|
|
|
if (arg.iovs != &kmsg->fast_iov && arg.iovs != kmsg->vec.iovec) {
|
|
|
|
|
kmsg->vec.nr = ret;
|
|
|
|
|
kmsg->vec.iovec = arg.iovs;
|
2024-08-07 15:06:45 -06:00
|
|
|
req->flags |= REQ_F_NEED_CLEANUP;
|
2024-03-05 16:22:04 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
void __user *buf;
|
|
|
|
|
|
|
|
|
|
*len = sr->len;
|
|
|
|
|
buf = io_buffer_select(req, len, issue_flags);
|
|
|
|
|
if (!buf)
|
|
|
|
|
return -ENOBUFS;
|
|
|
|
|
sr->buf = buf;
|
|
|
|
|
sr->len = *len;
|
|
|
|
|
map_ubuf:
|
|
|
|
|
ret = import_ubuf(ITER_DEST, sr->buf, sr->len,
|
|
|
|
|
&kmsg->msg.msg_iter);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_recv(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 07:36:03 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2022-05-25 06:25:13 -06:00
|
|
|
struct socket *sock;
|
|
|
|
|
unsigned flags;
|
|
|
|
|
int ret, min_ret = 0;
|
|
|
|
|
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
|
2022-06-30 02:12:29 -07:00
|
|
|
size_t len = sr->len;
|
2024-09-26 07:08:10 -06:00
|
|
|
bool mshot_finished;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(sr->flags & IORING_RECVSEND_POLL_FIRST))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
2024-02-25 12:59:05 -07:00
|
|
|
flags = sr->msg_flags;
|
|
|
|
|
if (force_nonblock)
|
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
|
|
2022-06-30 02:12:29 -07:00
|
|
|
retry_multishot:
|
2022-05-25 06:25:13 -06:00
|
|
|
if (io_do_buffer_select(req)) {
|
2024-03-05 16:22:04 -07:00
|
|
|
ret = io_recv_buf_select(req, kmsg, &len, issue_flags);
|
2024-07-02 09:37:30 -06:00
|
|
|
if (unlikely(ret)) {
|
|
|
|
|
kmsg->msg.msg_inq = -1;
|
2024-03-05 15:39:16 -07:00
|
|
|
goto out_free;
|
2024-07-02 09:37:30 -06:00
|
|
|
}
|
2024-03-05 16:22:04 -07:00
|
|
|
sr->buf = NULL;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2024-07-02 09:37:30 -06:00
|
|
|
kmsg->msg.msg_flags = 0;
|
|
|
|
|
kmsg->msg.msg_inq = -1;
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
if (flags & MSG_WAITALL)
|
2024-03-05 15:39:16 -07:00
|
|
|
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
2024-03-05 15:39:16 -07:00
|
|
|
ret = sock_recvmsg(sock, &kmsg->msg, flags);
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret < min_ret) {
|
2022-06-30 02:12:29 -07:00
|
|
|
if (ret == -EAGAIN && force_nonblock) {
|
2025-03-08 17:19:32 +00:00
|
|
|
if (issue_flags & IO_URING_F_MULTISHOT)
|
2022-06-30 02:12:29 -07:00
|
|
|
io_kbuf_recycle(req, issue_flags);
|
|
|
|
|
|
2025-03-08 17:19:32 +00:00
|
|
|
return IOU_RETRY;
|
2022-06-30 02:12:29 -07:00
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret > 0 && io_net_retry(sock, flags)) {
|
|
|
|
|
sr->len -= ret;
|
|
|
|
|
sr->buf += ret;
|
|
|
|
|
sr->done_io += ret;
|
2024-03-07 12:53:24 -07:00
|
|
|
req->flags |= REQ_F_BL_NO_RECYCLE;
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
2022-09-08 13:20:29 +01:00
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
2022-05-25 06:25:13 -06:00
|
|
|
req_set_fail(req);
|
2024-03-05 15:39:16 -07:00
|
|
|
} else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
|
2022-05-25 06:25:13 -06:00
|
|
|
out_free:
|
|
|
|
|
req_set_fail(req);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 07:08:10 -06:00
|
|
|
mshot_finished = ret <= 0;
|
2022-06-30 02:12:23 -07:00
|
|
|
if (ret > 0)
|
2022-05-25 06:25:13 -06:00
|
|
|
ret += sr->done_io;
|
|
|
|
|
else if (sr->done_io)
|
|
|
|
|
ret = sr->done_io;
|
2022-06-30 02:12:23 -07:00
|
|
|
else
|
|
|
|
|
io_kbuf_recycle(req, issue_flags);
|
|
|
|
|
|
2024-09-26 07:08:10 -06:00
|
|
|
if (!io_recv_finish(req, &ret, kmsg, mshot_finished, issue_flags))
|
2022-06-30 02:12:29 -07:00
|
|
|
goto retry_multishot;
|
|
|
|
|
|
|
|
|
|
return ret;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
2025-02-14 16:09:41 -08:00
|
|
|
int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
|
|
|
|
struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc);
|
|
|
|
|
unsigned ifq_idx;
|
|
|
|
|
|
|
|
|
|
if (unlikely(sqe->file_index || sqe->addr2 || sqe->addr ||
|
2025-02-23 20:13:18 -08:00
|
|
|
sqe->addr3))
|
2025-02-14 16:09:41 -08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
ifq_idx = READ_ONCE(sqe->zcrx_ifq_idx);
|
|
|
|
|
if (ifq_idx != 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
zc->ifq = req->ctx->ifq;
|
|
|
|
|
if (!zc->ifq)
|
|
|
|
|
return -EINVAL;
|
2025-02-23 20:13:18 -08:00
|
|
|
zc->len = READ_ONCE(sqe->len);
|
2025-02-14 16:09:41 -08:00
|
|
|
zc->flags = READ_ONCE(sqe->ioprio);
|
|
|
|
|
zc->msg_flags = READ_ONCE(sqe->msg_flags);
|
|
|
|
|
if (zc->msg_flags)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (zc->flags & ~(IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
/* multishot required */
|
|
|
|
|
if (!(zc->flags & IORING_RECV_MULTISHOT))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
/* All data completions are posted as aux CQEs. */
|
|
|
|
|
req->flags |= REQ_F_APOLL_MULTISHOT;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_recvzc(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc);
|
|
|
|
|
struct socket *sock;
|
2025-02-23 20:13:18 -08:00
|
|
|
unsigned int len;
|
2025-02-14 16:09:41 -08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(zc->flags & IORING_RECVSEND_POLL_FIRST))
|
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
2025-02-23 20:13:18 -08:00
|
|
|
len = zc->len;
|
2025-02-14 16:09:41 -08:00
|
|
|
ret = io_zcrx_recv(req, zc->ifq, sock, zc->msg_flags | MSG_DONTWAIT,
|
2025-02-23 20:13:18 -08:00
|
|
|
issue_flags, &zc->len);
|
|
|
|
|
if (len && zc->len == 0) {
|
|
|
|
|
io_req_set_res(req, 0, 0);
|
|
|
|
|
|
2025-03-08 17:19:33 +00:00
|
|
|
return IOU_COMPLETE;
|
2025-02-23 20:13:18 -08:00
|
|
|
}
|
2025-02-14 16:09:41 -08:00
|
|
|
if (unlikely(ret <= 0) && ret != -EAGAIN) {
|
|
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
2025-02-14 16:09:43 -08:00
|
|
|
if (ret == IOU_REQUEUE)
|
|
|
|
|
return IOU_REQUEUE;
|
2025-02-14 16:09:41 -08:00
|
|
|
|
|
|
|
|
req_set_fail(req);
|
|
|
|
|
io_req_set_res(req, ret, 0);
|
2025-03-08 17:19:33 +00:00
|
|
|
return IOU_COMPLETE;
|
2025-02-14 16:09:41 -08:00
|
|
|
}
|
2025-03-08 17:19:32 +00:00
|
|
|
return IOU_RETRY;
|
2025-02-14 16:09:41 -08:00
|
|
|
}
|
|
|
|
|
|
2022-09-21 12:17:52 +01:00
|
|
|
void io_send_zc_cleanup(struct io_kiocb *req)
|
2022-09-01 11:54:04 +01:00
|
|
|
{
|
2022-09-08 13:20:34 +01:00
|
|
|
struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-16 15:33:53 -06:00
|
|
|
struct io_async_msghdr *io = req->async_data;
|
2022-09-01 11:54:04 +01:00
|
|
|
|
2024-03-16 15:33:53 -06:00
|
|
|
if (req_has_async_data(req))
|
|
|
|
|
io_netmsg_iovec_free(io);
|
2022-09-23 16:23:34 +01:00
|
|
|
if (zc->notif) {
|
|
|
|
|
io_notif_flush(zc->notif);
|
|
|
|
|
zc->notif = NULL;
|
|
|
|
|
}
|
2022-09-01 11:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
2022-11-04 10:59:45 +00:00
|
|
|
#define IO_ZC_FLAGS_COMMON (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_FIXED_BUF)
|
|
|
|
|
#define IO_ZC_FLAGS_VALID (IO_ZC_FLAGS_COMMON | IORING_SEND_ZC_REPORT_USAGE)
|
|
|
|
|
|
2022-09-21 12:17:52 +01:00
|
|
|
int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
2022-07-12 21:52:43 +01:00
|
|
|
{
|
2022-09-08 13:20:34 +01:00
|
|
|
struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2022-07-12 21:52:46 +01:00
|
|
|
struct io_ring_ctx *ctx = req->ctx;
|
2022-09-01 11:54:04 +01:00
|
|
|
struct io_kiocb *notif;
|
2025-03-27 15:02:20 +00:00
|
|
|
int ret;
|
2022-07-12 21:52:43 +01:00
|
|
|
|
2024-03-07 12:43:22 -07:00
|
|
|
zc->done_io = 0;
|
io_uring/net: improve recv bundles
Current recv bundles are only supported for multishot receives, and
additionally they also always post at least 2 CQEs if more data is
available than what a buffer will hold. This happens because the initial
bundle recv will do a single buffer, and then do the rest of what is in
the socket as a followup receive. As shown in a test program, if 1k
buffers are available and 32k is available to receive in the socket,
you'd get the following completions:
bundle=1, mshot=0
cqe res 1024
cqe res 1024
[...]
cqe res 1024
bundle=1, mshot=1
cqe res 1024
cqe res 31744
where bundle=1 && mshot=0 will post 32 1k completions, and bundle=1 &&
mshot=1 will post a 1k completion and then a 31k completion.
To support bundle recv without multishot, it's possible to simply retry
the recv immediately and post a single completion, rather than split it
into two completions. With the below patch, the same test looks as
follows:
bundle=1, mshot=0
cqe res 32768
bundle=1, mshot=1
cqe res 32768
where mshot=0 works fine for bundles, and both of them post just a
single 32k completion rather than split it into separate completions.
Posting fewer completions is always a nice win, and not needing
multishot for proper bundle efficiency is nice for cases that can't
necessarily use multishot.
Reported-by: Norman Maurer <norman_maurer@apple.com>
Link: https://lore.kernel.org/r/184f9f92-a682-4205-a15d-89e18f664502@kernel.dk
Fixes: 2f9c9515bdfd ("io_uring/net: support bundles for recv")
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2025-02-08 10:50:34 -07:00
|
|
|
zc->retry = false;
|
2024-04-30 16:42:30 +01:00
|
|
|
req->flags |= REQ_F_POLL_NO_LAZY;
|
2024-03-07 12:43:22 -07:00
|
|
|
|
2022-09-21 12:17:54 +01:00
|
|
|
if (unlikely(READ_ONCE(sqe->__pad2[0]) || READ_ONCE(sqe->addr3)))
|
2022-09-01 11:54:04 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
/* we don't support IOSQE_CQE_SKIP_SUCCESS just yet */
|
|
|
|
|
if (req->flags & REQ_F_CQE_SKIP)
|
2022-07-12 21:52:43 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
2022-09-16 23:22:57 +01:00
|
|
|
notif = zc->notif = io_alloc_notif(ctx);
|
|
|
|
|
if (!notif)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
notif->cqe.user_data = req->cqe.user_data;
|
|
|
|
|
notif->cqe.res = 0;
|
|
|
|
|
notif->cqe.flags = IORING_CQE_F_NOTIF;
|
|
|
|
|
req->flags |= REQ_F_NEED_CLEANUP;
|
2022-11-04 10:59:45 +00:00
|
|
|
|
|
|
|
|
zc->flags = READ_ONCE(sqe->ioprio);
|
|
|
|
|
if (unlikely(zc->flags & ~IO_ZC_FLAGS_COMMON)) {
|
|
|
|
|
if (zc->flags & ~IO_ZC_FLAGS_VALID)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (zc->flags & IORING_SEND_ZC_REPORT_USAGE) {
|
2024-04-08 00:54:56 +01:00
|
|
|
struct io_notif_data *nd = io_notif_to_data(notif);
|
|
|
|
|
|
|
|
|
|
nd->zc_report = true;
|
|
|
|
|
nd->zc_used = false;
|
|
|
|
|
nd->zc_copied = false;
|
2022-11-04 10:59:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 15:43:13 +01:00
|
|
|
if (req->opcode != IORING_OP_SEND_ZC) {
|
2022-09-21 12:17:54 +01:00
|
|
|
if (unlikely(sqe->addr2 || sqe->file_index))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-12 21:52:43 +01:00
|
|
|
zc->len = READ_ONCE(sqe->len);
|
2024-04-08 00:54:57 +01:00
|
|
|
zc->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL | MSG_ZEROCOPY;
|
2025-02-24 13:31:08 -08:00
|
|
|
req->buf_index = READ_ONCE(sqe->buf_index);
|
2022-07-12 21:52:43 +01:00
|
|
|
if (zc->msg_flags & MSG_DONTWAIT)
|
|
|
|
|
req->flags |= REQ_F_NOWAIT;
|
2022-07-12 21:52:45 +01:00
|
|
|
|
2025-02-24 12:42:24 +00:00
|
|
|
if (io_is_compat(req->ctx))
|
2022-07-12 21:52:43 +01:00
|
|
|
zc->msg_flags |= MSG_CMSG_COMPAT;
|
2025-02-24 12:42:24 +00:00
|
|
|
|
2024-10-22 15:43:12 +01:00
|
|
|
if (unlikely(!io_msg_alloc_async(req)))
|
|
|
|
|
return -ENOMEM;
|
2025-03-25 08:39:42 -06:00
|
|
|
if (req->opcode == IORING_OP_SEND_ZC) {
|
|
|
|
|
req->flags |= REQ_F_IMPORT_BUFFER;
|
2024-10-22 15:43:13 +01:00
|
|
|
return io_send_setup(req, sqe);
|
2025-03-25 08:39:42 -06:00
|
|
|
}
|
2025-03-27 15:02:20 +00:00
|
|
|
ret = io_sendmsg_zc_setup(req, sqe);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
if (!(zc->flags & IORING_RECVSEND_FIXED_BUF)) {
|
|
|
|
|
struct io_async_msghdr *iomsg = req->async_data;
|
|
|
|
|
|
|
|
|
|
return io_notif_account_mem(zc->notif, iomsg->msg.msg_iter.count);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2022-07-12 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-27 13:59:44 +01:00
|
|
|
static int io_sg_from_iter_iovec(struct sk_buff *skb,
|
2022-09-08 13:20:32 +01:00
|
|
|
struct iov_iter *from, size_t length)
|
|
|
|
|
{
|
|
|
|
|
skb_zcopy_downgrade_managed(skb);
|
2024-06-27 13:59:44 +01:00
|
|
|
return zerocopy_fill_skb_from_iter(skb, from, length);
|
2022-09-08 13:20:32 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-27 13:59:44 +01:00
|
|
|
static int io_sg_from_iter(struct sk_buff *skb,
|
2022-07-12 21:52:50 +01:00
|
|
|
struct iov_iter *from, size_t length)
|
|
|
|
|
{
|
|
|
|
|
struct skb_shared_info *shinfo = skb_shinfo(skb);
|
|
|
|
|
int frag = shinfo->nr_frags;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
struct bvec_iter bi;
|
|
|
|
|
ssize_t copied = 0;
|
|
|
|
|
unsigned long truesize = 0;
|
|
|
|
|
|
2022-09-08 13:20:32 +01:00
|
|
|
if (!frag)
|
2022-07-12 21:52:50 +01:00
|
|
|
shinfo->flags |= SKBFL_MANAGED_FRAG_REFS;
|
2022-09-08 13:20:32 +01:00
|
|
|
else if (unlikely(!skb_zcopy_managed(skb)))
|
2024-06-27 13:59:44 +01:00
|
|
|
return zerocopy_fill_skb_from_iter(skb, from, length);
|
2022-07-12 21:52:50 +01:00
|
|
|
|
|
|
|
|
bi.bi_size = min(from->count, length);
|
|
|
|
|
bi.bi_bvec_done = from->iov_offset;
|
|
|
|
|
bi.bi_idx = 0;
|
|
|
|
|
|
|
|
|
|
while (bi.bi_size && frag < MAX_SKB_FRAGS) {
|
|
|
|
|
struct bio_vec v = mp_bvec_iter_bvec(from->bvec, bi);
|
|
|
|
|
|
|
|
|
|
copied += v.bv_len;
|
|
|
|
|
truesize += PAGE_ALIGN(v.bv_len + v.bv_offset);
|
|
|
|
|
__skb_fill_page_desc_noacc(shinfo, frag++, v.bv_page,
|
|
|
|
|
v.bv_offset, v.bv_len);
|
|
|
|
|
bvec_iter_advance_single(from->bvec, &bi, v.bv_len);
|
|
|
|
|
}
|
|
|
|
|
if (bi.bi_size)
|
|
|
|
|
ret = -EMSGSIZE;
|
|
|
|
|
|
|
|
|
|
shinfo->nr_frags = frag;
|
|
|
|
|
from->bvec += bi.bi_idx;
|
|
|
|
|
from->nr_segs -= bi.bi_idx;
|
2022-08-26 17:15:47 +01:00
|
|
|
from->count -= copied;
|
2022-07-12 21:52:50 +01:00
|
|
|
from->iov_offset = bi.bi_bvec_done;
|
|
|
|
|
|
|
|
|
|
skb->data_len += copied;
|
|
|
|
|
skb->len += copied;
|
|
|
|
|
skb->truesize += truesize;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 07:39:31 -06:00
|
|
|
static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)
|
2024-03-05 09:34:21 -07:00
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-10-16 07:39:31 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2024-03-05 09:34:21 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
if (sr->flags & IORING_RECVSEND_FIXED_BUF) {
|
2025-02-24 13:31:10 -08:00
|
|
|
sr->notif->buf_index = req->buf_index;
|
|
|
|
|
ret = io_import_reg_buf(sr->notif, &kmsg->msg.msg_iter,
|
|
|
|
|
(u64)(uintptr_t)sr->buf, sr->len,
|
|
|
|
|
ITER_SOURCE, issue_flags);
|
2024-03-05 09:34:21 -07:00
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
kmsg->msg.sg_from_iter = io_sg_from_iter;
|
|
|
|
|
} else {
|
|
|
|
|
ret = import_ubuf(ITER_SOURCE, sr->buf, sr->len, &kmsg->msg.msg_iter);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
ret = io_notif_account_mem(sr->notif, sr->len);
|
|
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
kmsg->msg.sg_from_iter = io_sg_from_iter_iovec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 12:17:52 +01:00
|
|
|
int io_send_zc(struct io_kiocb *req, unsigned int issue_flags)
|
2022-07-12 21:52:43 +01:00
|
|
|
{
|
2022-09-08 13:20:34 +01:00
|
|
|
struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 08:09:47 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2022-07-12 21:52:43 +01:00
|
|
|
struct socket *sock;
|
2022-09-28 00:51:49 +01:00
|
|
|
unsigned msg_flags;
|
2022-07-12 21:52:43 +01:00
|
|
|
int ret, min_ret = 0;
|
|
|
|
|
|
|
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
2022-10-21 11:16:40 +01:00
|
|
|
if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
|
|
|
|
|
return -EOPNOTSUPP;
|
2022-07-12 21:52:43 +01:00
|
|
|
|
2024-03-18 08:09:47 -06:00
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(zc->flags & IORING_RECVSEND_POLL_FIRST))
|
|
|
|
|
return -EAGAIN;
|
2022-09-08 14:01:10 +01:00
|
|
|
|
2025-03-25 08:39:42 -06:00
|
|
|
if (req->flags & REQ_F_IMPORT_BUFFER) {
|
|
|
|
|
req->flags &= ~REQ_F_IMPORT_BUFFER;
|
2024-10-16 07:39:31 -06:00
|
|
|
ret = io_send_zc_import(req, issue_flags);
|
2022-07-12 21:52:46 +01:00
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2022-07-12 21:52:43 +01:00
|
|
|
|
2024-04-08 00:54:57 +01:00
|
|
|
msg_flags = zc->msg_flags;
|
2022-07-12 21:52:43 +01:00
|
|
|
if (issue_flags & IO_URING_F_NONBLOCK)
|
|
|
|
|
msg_flags |= MSG_DONTWAIT;
|
|
|
|
|
if (msg_flags & MSG_WAITALL)
|
2024-03-05 09:34:21 -07:00
|
|
|
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
|
2023-05-22 13:11:10 +01:00
|
|
|
msg_flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
|
2022-07-12 21:52:43 +01:00
|
|
|
|
2024-03-05 09:34:21 -07:00
|
|
|
kmsg->msg.msg_flags = msg_flags;
|
|
|
|
|
kmsg->msg.msg_ubuf = &io_notif_to_data(zc->notif)->uarg;
|
|
|
|
|
ret = sock_sendmsg(sock, &kmsg->msg);
|
2022-07-12 21:52:43 +01:00
|
|
|
|
|
|
|
|
if (unlikely(ret < min_ret)) {
|
|
|
|
|
if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-08-24 13:07:43 +01:00
|
|
|
|
2024-03-05 09:34:21 -07:00
|
|
|
if (ret > 0 && io_net_retry(sock, kmsg->msg.msg_flags)) {
|
2022-08-04 15:15:30 +01:00
|
|
|
zc->len -= ret;
|
|
|
|
|
zc->buf += ret;
|
|
|
|
|
zc->done_io += ret;
|
2024-03-07 12:53:24 -07:00
|
|
|
req->flags |= REQ_F_BL_NO_RECYCLE;
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-08-04 15:15:30 +01:00
|
|
|
}
|
|
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
2022-08-24 13:07:39 +01:00
|
|
|
req_set_fail(req);
|
2022-07-12 21:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-04 15:15:30 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
|
ret += zc->done_io;
|
|
|
|
|
else if (zc->done_io)
|
|
|
|
|
ret = zc->done_io;
|
2022-09-01 11:54:04 +01:00
|
|
|
|
2022-09-29 22:23:19 +01:00
|
|
|
/*
|
|
|
|
|
* If we're in io-wq we can't rely on tw ordering guarantees, defer
|
|
|
|
|
* flushing notif to io_send_zc_cleanup()
|
|
|
|
|
*/
|
|
|
|
|
if (!(issue_flags & IO_URING_F_UNLOCKED)) {
|
|
|
|
|
io_notif_flush(zc->notif);
|
2025-03-22 11:47:27 +00:00
|
|
|
zc->notif = NULL;
|
2024-03-18 13:52:42 -06:00
|
|
|
io_req_msg_cleanup(req, 0);
|
2022-09-29 22:23:19 +01:00
|
|
|
}
|
2022-09-28 00:51:49 +01:00
|
|
|
io_req_set_res(req, ret, IORING_CQE_F_MORE);
|
2022-07-12 21:52:43 +01:00
|
|
|
return IOU_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 12:17:54 +01:00
|
|
|
int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
2024-03-18 08:09:47 -06:00
|
|
|
struct io_async_msghdr *kmsg = req->async_data;
|
2022-09-21 12:17:54 +01:00
|
|
|
struct socket *sock;
|
2022-09-28 00:51:49 +01:00
|
|
|
unsigned flags;
|
2022-09-21 12:17:54 +01:00
|
|
|
int ret, min_ret = 0;
|
|
|
|
|
|
2025-03-07 16:00:36 +00:00
|
|
|
kmsg->msg.sg_from_iter = io_sg_from_iter_iovec;
|
|
|
|
|
|
|
|
|
|
if (req->flags & REQ_F_IMPORT_BUFFER) {
|
|
|
|
|
unsigned uvec_segs = kmsg->msg.msg_iter.nr_segs;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = io_import_reg_vec(ITER_SOURCE, &kmsg->msg.msg_iter, req,
|
2025-03-08 18:21:16 +00:00
|
|
|
&kmsg->vec, uvec_segs, issue_flags);
|
2025-03-07 16:00:36 +00:00
|
|
|
if (unlikely(ret))
|
|
|
|
|
return ret;
|
|
|
|
|
kmsg->msg.sg_from_iter = io_sg_from_iter;
|
|
|
|
|
req->flags &= ~REQ_F_IMPORT_BUFFER;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 12:17:54 +01:00
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
2022-10-21 11:16:41 +01:00
|
|
|
if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
|
|
|
|
|
return -EOPNOTSUPP;
|
2022-09-21 12:17:54 +01:00
|
|
|
|
|
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
(sr->flags & IORING_RECVSEND_POLL_FIRST))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-09-21 12:17:54 +01:00
|
|
|
|
2024-04-08 00:54:57 +01:00
|
|
|
flags = sr->msg_flags;
|
2022-09-21 12:17:54 +01:00
|
|
|
if (issue_flags & IO_URING_F_NONBLOCK)
|
|
|
|
|
flags |= MSG_DONTWAIT;
|
|
|
|
|
if (flags & MSG_WAITALL)
|
|
|
|
|
min_ret = iov_iter_count(&kmsg->msg.msg_iter);
|
|
|
|
|
|
2024-04-12 12:39:54 -06:00
|
|
|
kmsg->msg.msg_control_user = sr->msg_control;
|
2022-09-21 12:17:54 +01:00
|
|
|
kmsg->msg.msg_ubuf = &io_notif_to_data(sr->notif)->uarg;
|
|
|
|
|
ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
|
|
|
|
|
|
|
|
|
|
if (unlikely(ret < min_ret)) {
|
|
|
|
|
if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-09-21 12:17:54 +01:00
|
|
|
|
|
|
|
|
if (ret > 0 && io_net_retry(sock, flags)) {
|
|
|
|
|
sr->done_io += ret;
|
2024-03-07 12:53:24 -07:00
|
|
|
req->flags |= REQ_F_BL_NO_RECYCLE;
|
io_uring/net: always setup an io_async_msghdr
Rather than use an on-stack one and then need to allocate and copy if
async execution is required, always grab one upfront. This should be
very cheap, and potentially even have cache hotness benefits for
back-to-back send/recv requests.
For any recv type of request, this is probably a good choice in general,
as it's expected that no data is available initially. For send this is
not necessarily the case, as space in the socket buffer is expected to
be available. However, getting a cached io_async_msghdr is very cheap,
and as it should be cache hot, probably the difference here is neglible,
if any.
A nice side benefit is that io_setup_async_msg can get killed
completely, which has some nasty iovec manipulation code.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2024-03-16 17:26:09 -06:00
|
|
|
return -EAGAIN;
|
2022-09-21 12:17:54 +01:00
|
|
|
}
|
|
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
|
|
|
|
req_set_fail(req);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
|
ret += sr->done_io;
|
|
|
|
|
else if (sr->done_io)
|
|
|
|
|
ret = sr->done_io;
|
|
|
|
|
|
2022-09-29 22:23:19 +01:00
|
|
|
/*
|
|
|
|
|
* If we're in io-wq we can't rely on tw ordering guarantees, defer
|
|
|
|
|
* flushing notif to io_send_zc_cleanup()
|
|
|
|
|
*/
|
|
|
|
|
if (!(issue_flags & IO_URING_F_UNLOCKED)) {
|
|
|
|
|
io_notif_flush(sr->notif);
|
2025-03-22 11:47:27 +00:00
|
|
|
sr->notif = NULL;
|
2024-03-18 13:52:42 -06:00
|
|
|
io_req_msg_cleanup(req, 0);
|
2022-09-29 22:23:19 +01:00
|
|
|
}
|
2022-09-28 00:51:49 +01:00
|
|
|
io_req_set_res(req, ret, IORING_CQE_F_MORE);
|
2022-09-21 12:17:54 +01:00
|
|
|
return IOU_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 12:17:48 +01:00
|
|
|
void io_sendrecv_fail(struct io_kiocb *req)
|
|
|
|
|
{
|
|
|
|
|
struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
|
|
|
|
|
|
2024-03-07 12:43:22 -07:00
|
|
|
if (sr->done_io)
|
2022-09-28 00:51:49 +01:00
|
|
|
req->cqe.res = sr->done_io;
|
|
|
|
|
|
2022-09-21 12:17:53 +01:00
|
|
|
if ((req->flags & REQ_F_NEED_CLEANUP) &&
|
2022-09-28 00:51:49 +01:00
|
|
|
(req->opcode == IORING_OP_SEND_ZC || req->opcode == IORING_OP_SENDMSG_ZC))
|
|
|
|
|
req->cqe.flags |= IORING_CQE_F_MORE;
|
2022-09-21 12:17:49 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-08 08:17:50 -06:00
|
|
|
#define ACCEPT_FLAGS (IORING_ACCEPT_MULTISHOT | IORING_ACCEPT_DONTWAIT | \
|
|
|
|
|
IORING_ACCEPT_POLL_FIRST)
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
if (sqe->len || sqe->buf_index)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
|
|
|
|
|
accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
|
|
|
|
|
accept->flags = READ_ONCE(sqe->accept_flags);
|
|
|
|
|
accept->nofile = rlimit(RLIMIT_NOFILE);
|
2024-05-07 14:06:15 -06:00
|
|
|
accept->iou_flags = READ_ONCE(sqe->ioprio);
|
2024-05-08 08:17:50 -06:00
|
|
|
if (accept->iou_flags & ~ACCEPT_FLAGS)
|
2022-05-25 06:25:13 -06:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
accept->file_slot = READ_ONCE(sqe->file_index);
|
|
|
|
|
if (accept->file_slot) {
|
|
|
|
|
if (accept->flags & SOCK_CLOEXEC)
|
|
|
|
|
return -EINVAL;
|
2024-05-07 14:06:15 -06:00
|
|
|
if (accept->iou_flags & IORING_ACCEPT_MULTISHOT &&
|
2022-05-25 06:25:13 -06:00
|
|
|
accept->file_slot != IORING_FILE_INDEX_ALLOC)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
|
|
|
|
|
accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
|
2024-05-07 14:06:15 -06:00
|
|
|
if (accept->iou_flags & IORING_ACCEPT_MULTISHOT)
|
2022-05-25 06:25:13 -06:00
|
|
|
req->flags |= REQ_F_APOLL_MULTISHOT;
|
2024-05-07 14:06:15 -06:00
|
|
|
if (accept->iou_flags & IORING_ACCEPT_DONTWAIT)
|
|
|
|
|
req->flags |= REQ_F_NOWAIT;
|
2022-05-25 06:25:13 -06:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_accept(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
|
2022-05-25 06:25:13 -06:00
|
|
|
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
|
|
|
|
|
bool fixed = !!accept->file_slot;
|
2024-05-09 09:31:05 -06:00
|
|
|
struct proto_accept_arg arg = {
|
|
|
|
|
.flags = force_nonblock ? O_NONBLOCK : 0,
|
|
|
|
|
};
|
2022-05-25 06:25:13 -06:00
|
|
|
struct file *file;
|
2024-05-09 09:41:10 -06:00
|
|
|
unsigned cflags;
|
2022-05-25 06:25:13 -06:00
|
|
|
int ret, fd;
|
|
|
|
|
|
2024-05-08 08:17:50 -06:00
|
|
|
if (!(req->flags & REQ_F_POLLED) &&
|
|
|
|
|
accept->iou_flags & IORING_ACCEPT_POLL_FIRST)
|
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
retry:
|
|
|
|
|
if (!fixed) {
|
|
|
|
|
fd = __get_unused_fd_flags(accept->flags, accept->nofile);
|
|
|
|
|
if (unlikely(fd < 0))
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
2024-05-09 09:41:10 -06:00
|
|
|
arg.err = 0;
|
|
|
|
|
arg.is_empty = -1;
|
2024-05-09 09:31:05 -06:00
|
|
|
file = do_accept(req->file, &arg, accept->addr, accept->addr_len,
|
2022-05-25 06:25:13 -06:00
|
|
|
accept->flags);
|
|
|
|
|
if (IS_ERR(file)) {
|
|
|
|
|
if (!fixed)
|
|
|
|
|
put_unused_fd(fd);
|
|
|
|
|
ret = PTR_ERR(file);
|
2024-05-07 14:06:15 -06:00
|
|
|
if (ret == -EAGAIN && force_nonblock &&
|
2025-03-08 17:19:32 +00:00
|
|
|
!(accept->iou_flags & IORING_ACCEPT_DONTWAIT))
|
|
|
|
|
return IOU_RETRY;
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
|
|
|
|
} else if (!fixed) {
|
|
|
|
|
fd_install(fd, file);
|
|
|
|
|
ret = fd;
|
|
|
|
|
} else {
|
|
|
|
|
ret = io_fixed_fd_install(req, issue_flags, file,
|
|
|
|
|
accept->file_slot);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 09:41:10 -06:00
|
|
|
cflags = 0;
|
|
|
|
|
if (!arg.is_empty)
|
|
|
|
|
cflags |= IORING_CQE_F_SOCK_NONEMPTY;
|
|
|
|
|
|
2025-02-23 17:22:30 +00:00
|
|
|
if (ret >= 0 && (req->flags & REQ_F_APOLL_MULTISHOT) &&
|
|
|
|
|
io_req_post_cqe(req, ret, cflags | IORING_CQE_F_MORE)) {
|
2024-05-09 09:41:10 -06:00
|
|
|
if (cflags & IORING_CQE_F_SOCK_NONEMPTY || arg.is_empty == -1)
|
|
|
|
|
goto retry;
|
2025-03-08 17:19:32 +00:00
|
|
|
return IOU_RETRY;
|
2024-05-09 09:41:10 -06:00
|
|
|
}
|
2022-06-30 02:12:28 -07:00
|
|
|
|
2024-05-09 09:41:10 -06:00
|
|
|
io_req_set_res(req, ret, cflags);
|
2025-02-23 17:22:30 +00:00
|
|
|
if (ret < 0)
|
|
|
|
|
req_set_fail(req);
|
2025-03-08 17:19:33 +00:00
|
|
|
return IOU_COMPLETE;
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
if (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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_socket(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
|
2022-05-25 06:25:13 -06:00
|
|
|
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 {
|
|
|
|
|
ret = io_fixed_fd_install(req, issue_flags, file,
|
|
|
|
|
sock->file_slot);
|
|
|
|
|
}
|
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
|
return IOU_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
|
2024-03-18 20:37:22 -06:00
|
|
|
struct io_async_msghdr *io;
|
2022-05-25 06:25:13 -06:00
|
|
|
|
|
|
|
|
if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
|
|
|
|
|
conn->addr_len = READ_ONCE(sqe->addr2);
|
2023-03-20 11:13:49 -06:00
|
|
|
conn->in_progress = conn->seen_econnaborted = false;
|
2024-03-18 20:37:22 -06:00
|
|
|
|
|
|
|
|
io = io_msg_alloc_async(req);
|
|
|
|
|
if (unlikely(!io))
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
return move_addr_to_kernel(conn->addr, conn->addr_len, &io->addr);
|
2022-05-25 06:25:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_connect(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
2022-08-11 09:11:15 +02:00
|
|
|
struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect);
|
2024-03-18 20:37:22 -06:00
|
|
|
struct io_async_msghdr *io = req->async_data;
|
2022-05-25 06:25:13 -06:00
|
|
|
unsigned file_flags;
|
|
|
|
|
int ret;
|
|
|
|
|
bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
|
|
|
|
|
|
2025-01-30 08:40:29 -07:00
|
|
|
if (unlikely(req->flags & REQ_F_FAIL)) {
|
|
|
|
|
ret = -ECONNRESET;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 06:25:13 -06:00
|
|
|
file_flags = force_nonblock ? O_NONBLOCK : 0;
|
|
|
|
|
|
2024-03-18 20:37:22 -06:00
|
|
|
ret = __sys_connect_file(req->file, &io->addr, connect->addr_len,
|
|
|
|
|
file_flags);
|
2023-03-20 11:13:49 -06:00
|
|
|
if ((ret == -EAGAIN || ret == -EINPROGRESS || ret == -ECONNABORTED)
|
|
|
|
|
&& force_nonblock) {
|
2022-10-04 20:29:48 -06:00
|
|
|
if (ret == -EINPROGRESS) {
|
|
|
|
|
connect->in_progress = true;
|
io_uring/net: ensure socket is marked connected on connect retry
io_uring does non-blocking connection attempts, which can yield some
unexpected results if a connect request is re-attempted by an an
application. This is equivalent to the following sync syscall sequence:
sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
connect(sock, &addr, sizeof(addr);
ret == -1 and errno == EINPROGRESS expected here. Now poll for POLLOUT
on sock, and when that returns, we expect the socket to be connected.
But if we follow that procedure with:
connect(sock, &addr, sizeof(addr));
you'd expect ret == -1 and errno == EISCONN here, but you actually get
ret == 0. If we attempt the connection one more time, then we get EISCON
as expected.
io_uring used to do this, but turns out that bluetooth fails with EBADFD
if you attempt to re-connect. Also looks like EISCONN _could_ occur with
this sequence.
Retain the ->in_progress logic, but work-around a potential EISCONN or
EBADFD error and only in those cases look at the sock_error(). This
should work in general and avoid the odd sequence of a repeated connect
request returning success when the socket is already connected.
This is all a side effect of the socket state being in a CONNECTING
state when we get EINPROGRESS, and only a re-connect or other related
operation will turn that into CONNECTED.
Cc: stable@vger.kernel.org
Fixes: 3fb1bd688172 ("io_uring/net: handle -EINPROGRESS correct for IORING_OP_CONNECT")
Link: https://github.com/axboe/liburing/issues/980
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-11-03 10:35:40 -06:00
|
|
|
} else if (ret == -ECONNABORTED) {
|
2023-03-20 11:13:49 -06:00
|
|
|
if (connect->seen_econnaborted)
|
2022-10-04 20:29:48 -06:00
|
|
|
goto out;
|
2023-03-20 11:13:49 -06:00
|
|
|
connect->seen_econnaborted = true;
|
|
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
return -EAGAIN;
|
|
|
|
|
}
|
io_uring/net: ensure socket is marked connected on connect retry
io_uring does non-blocking connection attempts, which can yield some
unexpected results if a connect request is re-attempted by an an
application. This is equivalent to the following sync syscall sequence:
sock = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
connect(sock, &addr, sizeof(addr);
ret == -1 and errno == EINPROGRESS expected here. Now poll for POLLOUT
on sock, and when that returns, we expect the socket to be connected.
But if we follow that procedure with:
connect(sock, &addr, sizeof(addr));
you'd expect ret == -1 and errno == EISCONN here, but you actually get
ret == 0. If we attempt the connection one more time, then we get EISCON
as expected.
io_uring used to do this, but turns out that bluetooth fails with EBADFD
if you attempt to re-connect. Also looks like EISCONN _could_ occur with
this sequence.
Retain the ->in_progress logic, but work-around a potential EISCONN or
EBADFD error and only in those cases look at the sock_error(). This
should work in general and avoid the odd sequence of a repeated connect
request returning success when the socket is already connected.
This is all a side effect of the socket state being in a CONNECTING
state when we get EINPROGRESS, and only a re-connect or other related
operation will turn that into CONNECTED.
Cc: stable@vger.kernel.org
Fixes: 3fb1bd688172 ("io_uring/net: handle -EINPROGRESS correct for IORING_OP_CONNECT")
Link: https://github.com/axboe/liburing/issues/980
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2023-11-03 10:35:40 -06:00
|
|
|
if (connect->in_progress) {
|
|
|
|
|
/*
|
|
|
|
|
* At least bluetooth will return -EBADFD on a re-connect
|
|
|
|
|
* attempt, and it's (supposedly) also valid to get -EISCONN
|
|
|
|
|
* which means the previous result is good. For both of these,
|
|
|
|
|
* grab the sock_error() and use that for the completion.
|
|
|
|
|
*/
|
|
|
|
|
if (ret == -EBADFD || ret == -EISCONN)
|
|
|
|
|
ret = sock_error(sock_from_file(req->file)->sk);
|
|
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
if (ret == -ERESTARTSYS)
|
|
|
|
|
ret = -EINTR;
|
|
|
|
|
out:
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
req_set_fail(req);
|
2024-03-18 20:37:22 -06:00
|
|
|
io_req_msg_cleanup(req, issue_flags);
|
2022-05-25 06:25:13 -06:00
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
|
return IOU_OK;
|
|
|
|
|
}
|
2022-07-07 14:30:09 -06:00
|
|
|
|
2024-06-14 12:30:46 -04:00
|
|
|
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;
|
2024-07-13 19:05:02 +09:00
|
|
|
struct socket *sock;
|
2024-06-14 12:30:46 -04:00
|
|
|
int ret;
|
|
|
|
|
|
2024-07-13 19:05:02 +09:00
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
|
|
|
|
ret = __sys_bind_socket(sock, &io->addr, bind->addr_len);
|
2024-06-14 12:30:46 -04:00
|
|
|
if (ret < 0)
|
|
|
|
|
req_set_fail(req);
|
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 12:30:47 -04:00
|
|
|
int io_listen_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
|
|
|
|
|
{
|
|
|
|
|
struct io_listen *listen = io_kiocb_to_cmd(req, struct io_listen);
|
|
|
|
|
|
|
|
|
|
if (sqe->addr || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in || sqe->addr2)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
listen->backlog = READ_ONCE(sqe->len);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int io_listen(struct io_kiocb *req, unsigned int issue_flags)
|
|
|
|
|
{
|
|
|
|
|
struct io_listen *listen = io_kiocb_to_cmd(req, struct io_listen);
|
2024-07-13 19:05:02 +09:00
|
|
|
struct socket *sock;
|
2024-06-14 12:30:47 -04:00
|
|
|
int ret;
|
|
|
|
|
|
2024-07-13 19:05:02 +09:00
|
|
|
sock = sock_from_file(req->file);
|
|
|
|
|
if (unlikely(!sock))
|
|
|
|
|
return -ENOTSOCK;
|
|
|
|
|
|
|
|
|
|
ret = __sys_listen_socket(sock, listen->backlog);
|
2024-06-14 12:30:47 -04:00
|
|
|
if (ret < 0)
|
|
|
|
|
req_set_fail(req);
|
|
|
|
|
io_req_set_res(req, ret, 0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 15:19:44 -06:00
|
|
|
void io_netmsg_cache_free(const void *entry)
|
2022-07-07 14:30:09 -06:00
|
|
|
{
|
2024-03-20 15:19:44 -06:00
|
|
|
struct io_async_msghdr *kmsg = (struct io_async_msghdr *) entry;
|
2024-03-16 15:33:53 -06:00
|
|
|
|
2025-03-07 16:00:35 +00:00
|
|
|
io_vec_free(&kmsg->vec);
|
2024-03-16 15:33:53 -06:00
|
|
|
kfree(kmsg);
|
2022-07-07 14:30:09 -06:00
|
|
|
}
|
2022-05-25 06:25:13 -06:00
|
|
|
#endif
|