1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/file.h>
5 #include <linux/slab.h>
7 #include <linux/compat.h>
8 #include <net/compat.h>
9 #include <linux/io_uring.h>
11 #include <uapi/linux/io_uring.h>
15 #include "alloc_cache.h"
20 #if defined(CONFIG_NET)
28 struct sockaddr __user *addr;
47 struct sockaddr __user *addr;
50 bool seen_econnaborted;
56 struct compat_msghdr __user *umsg_compat;
57 struct user_msghdr __user *umsg;
64 /* initialised and used only by !msg send variants */
68 void __user *msg_control;
69 /* used only for send zerocopy */
70 struct io_kiocb *notif;
73 static inline bool io_check_multishot(struct io_kiocb *req,
74 unsigned int issue_flags)
77 * When ->locked_cq is set we only allow to post CQEs from the original
78 * task context. Usual request completions will be handled in other
79 * generic paths but multipoll may decide to post extra cqes.
81 return !(issue_flags & IO_URING_F_IOWQ) ||
82 !(issue_flags & IO_URING_F_MULTISHOT) ||
83 !req->ctx->task_complete;
86 int io_shutdown_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
88 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
90 if (unlikely(sqe->off || sqe->addr || sqe->rw_flags ||
91 sqe->buf_index || sqe->splice_fd_in))
94 shutdown->how = READ_ONCE(sqe->len);
95 req->flags |= REQ_F_FORCE_ASYNC;
99 int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
101 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
105 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
107 sock = sock_from_file(req->file);
111 ret = __sys_shutdown_sock(sock, shutdown->how);
112 io_req_set_res(req, ret, 0);
116 static bool io_net_retry(struct socket *sock, int flags)
118 if (!(flags & MSG_WAITALL))
120 return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
123 static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags)
125 struct io_async_msghdr *hdr = req->async_data;
127 if (!req_has_async_data(req) || issue_flags & IO_URING_F_UNLOCKED)
130 /* Let normal cleanup path reap it if we fail adding to the cache */
131 if (io_alloc_cache_put(&req->ctx->netmsg_cache, &hdr->cache)) {
132 req->async_data = NULL;
133 req->flags &= ~REQ_F_ASYNC_DATA;
137 static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req,
138 unsigned int issue_flags)
140 struct io_ring_ctx *ctx = req->ctx;
141 struct io_cache_entry *entry;
142 struct io_async_msghdr *hdr;
144 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
145 entry = io_alloc_cache_get(&ctx->netmsg_cache);
147 hdr = container_of(entry, struct io_async_msghdr, cache);
148 hdr->free_iov = NULL;
149 req->flags |= REQ_F_ASYNC_DATA;
150 req->async_data = hdr;
155 if (!io_alloc_async_data(req)) {
156 hdr = req->async_data;
157 hdr->free_iov = NULL;
163 static inline struct io_async_msghdr *io_msg_alloc_async_prep(struct io_kiocb *req)
165 /* ->prep_async is always called from the submission context */
166 return io_msg_alloc_async(req, 0);
169 static int io_setup_async_msg(struct io_kiocb *req,
170 struct io_async_msghdr *kmsg,
171 unsigned int issue_flags)
173 struct io_async_msghdr *async_msg;
175 if (req_has_async_data(req))
177 async_msg = io_msg_alloc_async(req, issue_flags);
179 kfree(kmsg->free_iov);
182 req->flags |= REQ_F_NEED_CLEANUP;
183 memcpy(async_msg, kmsg, sizeof(*kmsg));
184 if (async_msg->msg.msg_name)
185 async_msg->msg.msg_name = &async_msg->addr;
186 /* if were using fast_iov, set it to the new one */
187 if (iter_is_iovec(&kmsg->msg.msg_iter) && !kmsg->free_iov) {
188 size_t fast_idx = iter_iov(&kmsg->msg.msg_iter) - kmsg->fast_iov;
189 async_msg->msg.msg_iter.__iov = &async_msg->fast_iov[fast_idx];
195 static int io_sendmsg_copy_hdr(struct io_kiocb *req,
196 struct io_async_msghdr *iomsg)
198 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
201 iomsg->msg.msg_name = &iomsg->addr;
202 iomsg->free_iov = iomsg->fast_iov;
203 ret = sendmsg_copy_msghdr(&iomsg->msg, sr->umsg, sr->msg_flags,
205 /* save msg_control as sys_sendmsg() overwrites it */
206 sr->msg_control = iomsg->msg.msg_control_user;
210 int io_send_prep_async(struct io_kiocb *req)
212 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
213 struct io_async_msghdr *io;
216 if (!zc->addr || req_has_async_data(req))
218 io = io_msg_alloc_async_prep(req);
221 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &io->addr);
225 static int io_setup_async_addr(struct io_kiocb *req,
226 struct sockaddr_storage *addr_storage,
227 unsigned int issue_flags)
229 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
230 struct io_async_msghdr *io;
232 if (!sr->addr || req_has_async_data(req))
234 io = io_msg_alloc_async(req, issue_flags);
237 memcpy(&io->addr, addr_storage, sizeof(io->addr));
241 int io_sendmsg_prep_async(struct io_kiocb *req)
245 if (!io_msg_alloc_async_prep(req))
247 ret = io_sendmsg_copy_hdr(req, req->async_data);
249 req->flags |= REQ_F_NEED_CLEANUP;
253 void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req)
255 struct io_async_msghdr *io = req->async_data;
260 int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
262 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
264 if (req->opcode == IORING_OP_SEND) {
265 if (READ_ONCE(sqe->__pad3[0]))
267 sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
268 sr->addr_len = READ_ONCE(sqe->addr_len);
269 } else if (sqe->addr2 || sqe->file_index) {
273 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
274 sr->len = READ_ONCE(sqe->len);
275 sr->flags = READ_ONCE(sqe->ioprio);
276 if (sr->flags & ~IORING_RECVSEND_POLL_FIRST)
278 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
279 if (sr->msg_flags & MSG_DONTWAIT)
280 req->flags |= REQ_F_NOWAIT;
283 if (req->ctx->compat)
284 sr->msg_flags |= MSG_CMSG_COMPAT;
290 int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
292 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
293 struct io_async_msghdr iomsg, *kmsg;
299 sock = sock_from_file(req->file);
303 if (req_has_async_data(req)) {
304 kmsg = req->async_data;
305 kmsg->msg.msg_control_user = sr->msg_control;
307 ret = io_sendmsg_copy_hdr(req, &iomsg);
313 if (!(req->flags & REQ_F_POLLED) &&
314 (sr->flags & IORING_RECVSEND_POLL_FIRST))
315 return io_setup_async_msg(req, kmsg, issue_flags);
317 flags = sr->msg_flags;
318 if (issue_flags & IO_URING_F_NONBLOCK)
319 flags |= MSG_DONTWAIT;
320 if (flags & MSG_WAITALL)
321 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
323 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
326 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
327 return io_setup_async_msg(req, kmsg, issue_flags);
328 if (ret > 0 && io_net_retry(sock, flags)) {
329 kmsg->msg.msg_controllen = 0;
330 kmsg->msg.msg_control = NULL;
332 req->flags |= REQ_F_PARTIAL_IO;
333 return io_setup_async_msg(req, kmsg, issue_flags);
335 if (ret == -ERESTARTSYS)
339 /* fast path, check for non-NULL to avoid function call */
341 kfree(kmsg->free_iov);
342 req->flags &= ~REQ_F_NEED_CLEANUP;
343 io_netmsg_recycle(req, issue_flags);
346 else if (sr->done_io)
348 io_req_set_res(req, ret, 0);
352 int io_send(struct io_kiocb *req, unsigned int issue_flags)
354 struct sockaddr_storage __address;
355 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
363 msg.msg_control = NULL;
364 msg.msg_controllen = 0;
369 if (req_has_async_data(req)) {
370 struct io_async_msghdr *io = req->async_data;
372 msg.msg_name = &io->addr;
374 ret = move_addr_to_kernel(sr->addr, sr->addr_len, &__address);
375 if (unlikely(ret < 0))
377 msg.msg_name = (struct sockaddr *)&__address;
379 msg.msg_namelen = sr->addr_len;
382 if (!(req->flags & REQ_F_POLLED) &&
383 (sr->flags & IORING_RECVSEND_POLL_FIRST))
384 return io_setup_async_addr(req, &__address, issue_flags);
386 sock = sock_from_file(req->file);
390 ret = import_ubuf(ITER_SOURCE, sr->buf, sr->len, &msg.msg_iter);
394 flags = sr->msg_flags;
395 if (issue_flags & IO_URING_F_NONBLOCK)
396 flags |= MSG_DONTWAIT;
397 if (flags & MSG_WAITALL)
398 min_ret = iov_iter_count(&msg.msg_iter);
400 msg.msg_flags = flags;
401 ret = sock_sendmsg(sock, &msg);
403 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
404 return io_setup_async_addr(req, &__address, issue_flags);
406 if (ret > 0 && io_net_retry(sock, flags)) {
410 req->flags |= REQ_F_PARTIAL_IO;
411 return io_setup_async_addr(req, &__address, issue_flags);
413 if (ret == -ERESTARTSYS)
419 else if (sr->done_io)
421 io_req_set_res(req, ret, 0);
425 static bool io_recvmsg_multishot_overflow(struct io_async_msghdr *iomsg)
429 if (iomsg->namelen < 0)
431 if (check_add_overflow((int)sizeof(struct io_uring_recvmsg_out),
432 iomsg->namelen, &hdr))
434 if (check_add_overflow(hdr, (int)iomsg->controllen, &hdr))
440 static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
441 struct io_async_msghdr *iomsg)
443 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
444 struct user_msghdr msg;
447 if (copy_from_user(&msg, sr->umsg, sizeof(*sr->umsg)))
450 ret = __copy_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
454 if (req->flags & REQ_F_BUFFER_SELECT) {
455 if (msg.msg_iovlen == 0) {
456 sr->len = iomsg->fast_iov[0].iov_len = 0;
457 iomsg->fast_iov[0].iov_base = NULL;
458 iomsg->free_iov = NULL;
459 } else if (msg.msg_iovlen > 1) {
462 if (copy_from_user(iomsg->fast_iov, msg.msg_iov, sizeof(*msg.msg_iov)))
464 sr->len = iomsg->fast_iov[0].iov_len;
465 iomsg->free_iov = NULL;
468 if (req->flags & REQ_F_APOLL_MULTISHOT) {
469 iomsg->namelen = msg.msg_namelen;
470 iomsg->controllen = msg.msg_controllen;
471 if (io_recvmsg_multishot_overflow(iomsg))
475 iomsg->free_iov = iomsg->fast_iov;
476 ret = __import_iovec(ITER_DEST, msg.msg_iov, msg.msg_iovlen, UIO_FASTIOV,
477 &iomsg->free_iov, &iomsg->msg.msg_iter,
487 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
488 struct io_async_msghdr *iomsg)
490 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
491 struct compat_msghdr msg;
492 struct compat_iovec __user *uiov;
495 if (copy_from_user(&msg, sr->umsg_compat, sizeof(msg)))
498 ret = __get_compat_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
502 uiov = compat_ptr(msg.msg_iov);
503 if (req->flags & REQ_F_BUFFER_SELECT) {
506 iomsg->free_iov = NULL;
507 if (msg.msg_iovlen == 0) {
509 } else if (msg.msg_iovlen > 1) {
512 if (!access_ok(uiov, sizeof(*uiov)))
514 if (__get_user(clen, &uiov->iov_len))
521 if (req->flags & REQ_F_APOLL_MULTISHOT) {
522 iomsg->namelen = msg.msg_namelen;
523 iomsg->controllen = msg.msg_controllen;
524 if (io_recvmsg_multishot_overflow(iomsg))
528 iomsg->free_iov = iomsg->fast_iov;
529 ret = __import_iovec(ITER_DEST, (struct iovec __user *)uiov, msg.msg_iovlen,
530 UIO_FASTIOV, &iomsg->free_iov,
531 &iomsg->msg.msg_iter, true);
540 static int io_recvmsg_copy_hdr(struct io_kiocb *req,
541 struct io_async_msghdr *iomsg)
543 iomsg->msg.msg_name = &iomsg->addr;
546 if (req->ctx->compat)
547 return __io_compat_recvmsg_copy_hdr(req, iomsg);
550 return __io_recvmsg_copy_hdr(req, iomsg);
553 int io_recvmsg_prep_async(struct io_kiocb *req)
557 if (!io_msg_alloc_async_prep(req))
559 ret = io_recvmsg_copy_hdr(req, req->async_data);
561 req->flags |= REQ_F_NEED_CLEANUP;
565 #define RECVMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT)
567 int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
569 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
571 if (unlikely(sqe->file_index || sqe->addr2))
574 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
575 sr->len = READ_ONCE(sqe->len);
576 sr->flags = READ_ONCE(sqe->ioprio);
577 if (sr->flags & ~(RECVMSG_FLAGS))
579 sr->msg_flags = READ_ONCE(sqe->msg_flags);
580 if (sr->msg_flags & MSG_DONTWAIT)
581 req->flags |= REQ_F_NOWAIT;
582 if (sr->msg_flags & MSG_ERRQUEUE)
583 req->flags |= REQ_F_CLEAR_POLLIN;
584 if (sr->flags & IORING_RECV_MULTISHOT) {
585 if (!(req->flags & REQ_F_BUFFER_SELECT))
587 if (sr->msg_flags & MSG_WAITALL)
589 if (req->opcode == IORING_OP_RECV && sr->len)
591 req->flags |= REQ_F_APOLL_MULTISHOT;
593 * Store the buffer group for this multishot receive separately,
594 * as if we end up doing an io-wq based issue that selects a
595 * buffer, it has to be committed immediately and that will
596 * clear ->buf_list. This means we lose the link to the buffer
597 * list, and the eventual buffer put on completion then cannot
600 sr->buf_group = req->buf_index;
604 if (req->ctx->compat)
605 sr->msg_flags |= MSG_CMSG_COMPAT;
611 static inline void io_recv_prep_retry(struct io_kiocb *req)
613 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
616 sr->len = 0; /* get from the provided buffer */
617 req->buf_index = sr->buf_group;
621 * Finishes io_recv and io_recvmsg.
623 * Returns true if it is actually finished, or false if it should run
624 * again (for multishot).
626 static inline bool io_recv_finish(struct io_kiocb *req, int *ret,
627 struct msghdr *msg, bool mshot_finished,
628 unsigned issue_flags)
632 cflags = io_put_kbuf(req, issue_flags);
633 if (msg->msg_inq && msg->msg_inq != -1U)
634 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
636 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
637 io_req_set_res(req, *ret, cflags);
642 if (!mshot_finished) {
643 if (io_aux_cqe(req, issue_flags & IO_URING_F_COMPLETE_DEFER,
644 *ret, cflags | IORING_CQE_F_MORE, true)) {
645 io_recv_prep_retry(req);
646 /* Known not-empty or unknown state, retry */
647 if (cflags & IORING_CQE_F_SOCK_NONEMPTY ||
650 if (issue_flags & IO_URING_F_MULTISHOT)
651 *ret = IOU_ISSUE_SKIP_COMPLETE;
656 /* Otherwise stop multishot but use the current result. */
659 io_req_set_res(req, *ret, cflags);
661 if (issue_flags & IO_URING_F_MULTISHOT)
662 *ret = IOU_STOP_MULTISHOT;
668 static int io_recvmsg_prep_multishot(struct io_async_msghdr *kmsg,
669 struct io_sr_msg *sr, void __user **buf,
672 unsigned long ubuf = (unsigned long) *buf;
675 hdr = sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
680 if (kmsg->controllen) {
681 unsigned long control = ubuf + hdr - kmsg->controllen;
683 kmsg->msg.msg_control_user = (void __user *) control;
684 kmsg->msg.msg_controllen = kmsg->controllen;
687 sr->buf = *buf; /* stash for later copy */
688 *buf = (void __user *) (ubuf + hdr);
689 kmsg->payloadlen = *len = *len - hdr;
693 struct io_recvmsg_multishot_hdr {
694 struct io_uring_recvmsg_out msg;
695 struct sockaddr_storage addr;
698 static int io_recvmsg_multishot(struct socket *sock, struct io_sr_msg *io,
699 struct io_async_msghdr *kmsg,
700 unsigned int flags, bool *finished)
704 struct io_recvmsg_multishot_hdr hdr;
707 kmsg->msg.msg_name = &hdr.addr;
708 kmsg->msg.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
709 kmsg->msg.msg_namelen = 0;
711 if (sock->file->f_flags & O_NONBLOCK)
712 flags |= MSG_DONTWAIT;
714 err = sock_recvmsg(sock, &kmsg->msg, flags);
715 *finished = err <= 0;
719 hdr.msg = (struct io_uring_recvmsg_out) {
720 .controllen = kmsg->controllen - kmsg->msg.msg_controllen,
721 .flags = kmsg->msg.msg_flags & ~MSG_CMSG_COMPAT
724 hdr.msg.payloadlen = err;
725 if (err > kmsg->payloadlen)
726 err = kmsg->payloadlen;
728 copy_len = sizeof(struct io_uring_recvmsg_out);
729 if (kmsg->msg.msg_namelen > kmsg->namelen)
730 copy_len += kmsg->namelen;
732 copy_len += kmsg->msg.msg_namelen;
735 * "fromlen shall refer to the value before truncation.."
738 hdr.msg.namelen = kmsg->msg.msg_namelen;
740 /* ensure that there is no gap between hdr and sockaddr_storage */
741 BUILD_BUG_ON(offsetof(struct io_recvmsg_multishot_hdr, addr) !=
742 sizeof(struct io_uring_recvmsg_out));
743 if (copy_to_user(io->buf, &hdr, copy_len)) {
748 return sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
749 kmsg->controllen + err;
752 int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
754 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
755 struct io_async_msghdr iomsg, *kmsg;
758 int ret, min_ret = 0;
759 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
760 bool mshot_finished = true;
762 sock = sock_from_file(req->file);
766 if (req_has_async_data(req)) {
767 kmsg = req->async_data;
769 ret = io_recvmsg_copy_hdr(req, &iomsg);
775 if (!(req->flags & REQ_F_POLLED) &&
776 (sr->flags & IORING_RECVSEND_POLL_FIRST))
777 return io_setup_async_msg(req, kmsg, issue_flags);
779 if (!io_check_multishot(req, issue_flags))
780 return io_setup_async_msg(req, kmsg, issue_flags);
783 if (io_do_buffer_select(req)) {
785 size_t len = sr->len;
787 buf = io_buffer_select(req, &len, issue_flags);
791 if (req->flags & REQ_F_APOLL_MULTISHOT) {
792 ret = io_recvmsg_prep_multishot(kmsg, sr, &buf, &len);
794 io_kbuf_recycle(req, issue_flags);
799 iov_iter_ubuf(&kmsg->msg.msg_iter, ITER_DEST, buf, len);
802 flags = sr->msg_flags;
804 flags |= MSG_DONTWAIT;
806 kmsg->msg.msg_get_inq = 1;
807 kmsg->msg.msg_inq = -1U;
808 if (req->flags & REQ_F_APOLL_MULTISHOT) {
809 ret = io_recvmsg_multishot(sock, sr, kmsg, flags,
812 /* disable partial retry for recvmsg with cmsg attached */
813 if (flags & MSG_WAITALL && !kmsg->msg.msg_controllen)
814 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
816 ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg,
821 if (ret == -EAGAIN && force_nonblock) {
822 ret = io_setup_async_msg(req, kmsg, issue_flags);
823 if (ret == -EAGAIN && (issue_flags & IO_URING_F_MULTISHOT)) {
824 io_kbuf_recycle(req, issue_flags);
825 return IOU_ISSUE_SKIP_COMPLETE;
829 if (ret > 0 && io_net_retry(sock, flags)) {
831 req->flags |= REQ_F_PARTIAL_IO;
832 return io_setup_async_msg(req, kmsg, issue_flags);
834 if (ret == -ERESTARTSYS)
837 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
843 else if (sr->done_io)
846 io_kbuf_recycle(req, issue_flags);
848 if (!io_recv_finish(req, &ret, &kmsg->msg, mshot_finished, issue_flags))
849 goto retry_multishot;
851 if (mshot_finished) {
852 /* fast path, check for non-NULL to avoid function call */
854 kfree(kmsg->free_iov);
855 io_netmsg_recycle(req, issue_flags);
856 req->flags &= ~REQ_F_NEED_CLEANUP;
862 int io_recv(struct io_kiocb *req, unsigned int issue_flags)
864 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
868 int ret, min_ret = 0;
869 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
870 size_t len = sr->len;
872 if (!(req->flags & REQ_F_POLLED) &&
873 (sr->flags & IORING_RECVSEND_POLL_FIRST))
876 if (!io_check_multishot(req, issue_flags))
879 sock = sock_from_file(req->file);
885 msg.msg_control = NULL;
887 msg.msg_controllen = 0;
892 if (io_do_buffer_select(req)) {
895 buf = io_buffer_select(req, &len, issue_flags);
901 ret = import_ubuf(ITER_DEST, sr->buf, len, &msg.msg_iter);
908 flags = sr->msg_flags;
910 flags |= MSG_DONTWAIT;
911 if (flags & MSG_WAITALL)
912 min_ret = iov_iter_count(&msg.msg_iter);
914 ret = sock_recvmsg(sock, &msg, flags);
916 if (ret == -EAGAIN && force_nonblock) {
917 if (issue_flags & IO_URING_F_MULTISHOT) {
918 io_kbuf_recycle(req, issue_flags);
919 return IOU_ISSUE_SKIP_COMPLETE;
924 if (ret > 0 && io_net_retry(sock, flags)) {
928 req->flags |= REQ_F_PARTIAL_IO;
931 if (ret == -ERESTARTSYS)
934 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
941 else if (sr->done_io)
944 io_kbuf_recycle(req, issue_flags);
946 if (!io_recv_finish(req, &ret, &msg, ret <= 0, issue_flags))
947 goto retry_multishot;
952 void io_send_zc_cleanup(struct io_kiocb *req)
954 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
955 struct io_async_msghdr *io;
957 if (req_has_async_data(req)) {
958 io = req->async_data;
959 /* might be ->fast_iov if *msg_copy_hdr failed */
960 if (io->free_iov != io->fast_iov)
964 io_notif_flush(zc->notif);
969 #define IO_ZC_FLAGS_COMMON (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_FIXED_BUF)
970 #define IO_ZC_FLAGS_VALID (IO_ZC_FLAGS_COMMON | IORING_SEND_ZC_REPORT_USAGE)
972 int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
974 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
975 struct io_ring_ctx *ctx = req->ctx;
976 struct io_kiocb *notif;
978 if (unlikely(READ_ONCE(sqe->__pad2[0]) || READ_ONCE(sqe->addr3)))
980 /* we don't support IOSQE_CQE_SKIP_SUCCESS just yet */
981 if (req->flags & REQ_F_CQE_SKIP)
984 notif = zc->notif = io_alloc_notif(ctx);
987 notif->cqe.user_data = req->cqe.user_data;
989 notif->cqe.flags = IORING_CQE_F_NOTIF;
990 req->flags |= REQ_F_NEED_CLEANUP;
992 zc->flags = READ_ONCE(sqe->ioprio);
993 if (unlikely(zc->flags & ~IO_ZC_FLAGS_COMMON)) {
994 if (zc->flags & ~IO_ZC_FLAGS_VALID)
996 if (zc->flags & IORING_SEND_ZC_REPORT_USAGE) {
997 io_notif_set_extended(notif);
998 io_notif_to_data(notif)->zc_report = true;
1002 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
1003 unsigned idx = READ_ONCE(sqe->buf_index);
1005 if (unlikely(idx >= ctx->nr_user_bufs))
1007 idx = array_index_nospec(idx, ctx->nr_user_bufs);
1008 req->imu = READ_ONCE(ctx->user_bufs[idx]);
1009 io_req_set_rsrc_node(notif, ctx, 0);
1012 if (req->opcode == IORING_OP_SEND_ZC) {
1013 if (READ_ONCE(sqe->__pad3[0]))
1015 zc->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
1016 zc->addr_len = READ_ONCE(sqe->addr_len);
1018 if (unlikely(sqe->addr2 || sqe->file_index))
1020 if (unlikely(zc->flags & IORING_RECVSEND_FIXED_BUF))
1024 zc->buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1025 zc->len = READ_ONCE(sqe->len);
1026 zc->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
1027 if (zc->msg_flags & MSG_DONTWAIT)
1028 req->flags |= REQ_F_NOWAIT;
1032 #ifdef CONFIG_COMPAT
1033 if (req->ctx->compat)
1034 zc->msg_flags |= MSG_CMSG_COMPAT;
1039 static int io_sg_from_iter_iovec(struct sock *sk, struct sk_buff *skb,
1040 struct iov_iter *from, size_t length)
1042 skb_zcopy_downgrade_managed(skb);
1043 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1046 static int io_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1047 struct iov_iter *from, size_t length)
1049 struct skb_shared_info *shinfo = skb_shinfo(skb);
1050 int frag = shinfo->nr_frags;
1052 struct bvec_iter bi;
1054 unsigned long truesize = 0;
1057 shinfo->flags |= SKBFL_MANAGED_FRAG_REFS;
1058 else if (unlikely(!skb_zcopy_managed(skb)))
1059 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1061 bi.bi_size = min(from->count, length);
1062 bi.bi_bvec_done = from->iov_offset;
1065 while (bi.bi_size && frag < MAX_SKB_FRAGS) {
1066 struct bio_vec v = mp_bvec_iter_bvec(from->bvec, bi);
1069 truesize += PAGE_ALIGN(v.bv_len + v.bv_offset);
1070 __skb_fill_page_desc_noacc(shinfo, frag++, v.bv_page,
1071 v.bv_offset, v.bv_len);
1072 bvec_iter_advance_single(from->bvec, &bi, v.bv_len);
1077 shinfo->nr_frags = frag;
1078 from->bvec += bi.bi_idx;
1079 from->nr_segs -= bi.bi_idx;
1080 from->count -= copied;
1081 from->iov_offset = bi.bi_bvec_done;
1083 skb->data_len += copied;
1085 skb->truesize += truesize;
1087 if (sk && sk->sk_type == SOCK_STREAM) {
1088 sk_wmem_queued_add(sk, truesize);
1089 if (!skb_zcopy_pure(skb))
1090 sk_mem_charge(sk, truesize);
1092 refcount_add(truesize, &skb->sk->sk_wmem_alloc);
1097 int io_send_zc(struct io_kiocb *req, unsigned int issue_flags)
1099 struct sockaddr_storage __address;
1100 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
1102 struct socket *sock;
1104 int ret, min_ret = 0;
1106 sock = sock_from_file(req->file);
1107 if (unlikely(!sock))
1109 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1112 msg.msg_name = NULL;
1113 msg.msg_control = NULL;
1114 msg.msg_controllen = 0;
1115 msg.msg_namelen = 0;
1118 if (req_has_async_data(req)) {
1119 struct io_async_msghdr *io = req->async_data;
1121 msg.msg_name = &io->addr;
1123 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &__address);
1124 if (unlikely(ret < 0))
1126 msg.msg_name = (struct sockaddr *)&__address;
1128 msg.msg_namelen = zc->addr_len;
1131 if (!(req->flags & REQ_F_POLLED) &&
1132 (zc->flags & IORING_RECVSEND_POLL_FIRST))
1133 return io_setup_async_addr(req, &__address, issue_flags);
1135 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
1136 ret = io_import_fixed(ITER_SOURCE, &msg.msg_iter, req->imu,
1137 (u64)(uintptr_t)zc->buf, zc->len);
1140 msg.sg_from_iter = io_sg_from_iter;
1142 io_notif_set_extended(zc->notif);
1143 ret = import_ubuf(ITER_SOURCE, zc->buf, zc->len, &msg.msg_iter);
1146 ret = io_notif_account_mem(zc->notif, zc->len);
1149 msg.sg_from_iter = io_sg_from_iter_iovec;
1152 msg_flags = zc->msg_flags | MSG_ZEROCOPY;
1153 if (issue_flags & IO_URING_F_NONBLOCK)
1154 msg_flags |= MSG_DONTWAIT;
1155 if (msg_flags & MSG_WAITALL)
1156 min_ret = iov_iter_count(&msg.msg_iter);
1158 msg.msg_flags = msg_flags;
1159 msg.msg_ubuf = &io_notif_to_data(zc->notif)->uarg;
1160 ret = sock_sendmsg(sock, &msg);
1162 if (unlikely(ret < min_ret)) {
1163 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1164 return io_setup_async_addr(req, &__address, issue_flags);
1166 if (ret > 0 && io_net_retry(sock, msg.msg_flags)) {
1170 req->flags |= REQ_F_PARTIAL_IO;
1171 return io_setup_async_addr(req, &__address, issue_flags);
1173 if (ret == -ERESTARTSYS)
1180 else if (zc->done_io)
1184 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1185 * flushing notif to io_send_zc_cleanup()
1187 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1188 io_notif_flush(zc->notif);
1189 req->flags &= ~REQ_F_NEED_CLEANUP;
1191 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1195 int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags)
1197 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1198 struct io_async_msghdr iomsg, *kmsg;
1199 struct socket *sock;
1201 int ret, min_ret = 0;
1203 io_notif_set_extended(sr->notif);
1205 sock = sock_from_file(req->file);
1206 if (unlikely(!sock))
1208 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1211 if (req_has_async_data(req)) {
1212 kmsg = req->async_data;
1214 ret = io_sendmsg_copy_hdr(req, &iomsg);
1220 if (!(req->flags & REQ_F_POLLED) &&
1221 (sr->flags & IORING_RECVSEND_POLL_FIRST))
1222 return io_setup_async_msg(req, kmsg, issue_flags);
1224 flags = sr->msg_flags | MSG_ZEROCOPY;
1225 if (issue_flags & IO_URING_F_NONBLOCK)
1226 flags |= MSG_DONTWAIT;
1227 if (flags & MSG_WAITALL)
1228 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
1230 kmsg->msg.msg_ubuf = &io_notif_to_data(sr->notif)->uarg;
1231 kmsg->msg.sg_from_iter = io_sg_from_iter_iovec;
1232 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
1234 if (unlikely(ret < min_ret)) {
1235 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1236 return io_setup_async_msg(req, kmsg, issue_flags);
1238 if (ret > 0 && io_net_retry(sock, flags)) {
1240 req->flags |= REQ_F_PARTIAL_IO;
1241 return io_setup_async_msg(req, kmsg, issue_flags);
1243 if (ret == -ERESTARTSYS)
1247 /* fast path, check for non-NULL to avoid function call */
1248 if (kmsg->free_iov) {
1249 kfree(kmsg->free_iov);
1250 kmsg->free_iov = NULL;
1253 io_netmsg_recycle(req, issue_flags);
1256 else if (sr->done_io)
1260 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1261 * flushing notif to io_send_zc_cleanup()
1263 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1264 io_notif_flush(sr->notif);
1265 req->flags &= ~REQ_F_NEED_CLEANUP;
1267 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1271 void io_sendrecv_fail(struct io_kiocb *req)
1273 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1275 if (req->flags & REQ_F_PARTIAL_IO)
1276 req->cqe.res = sr->done_io;
1278 if ((req->flags & REQ_F_NEED_CLEANUP) &&
1279 (req->opcode == IORING_OP_SEND_ZC || req->opcode == IORING_OP_SENDMSG_ZC))
1280 req->cqe.flags |= IORING_CQE_F_MORE;
1283 int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1285 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1288 if (sqe->len || sqe->buf_index)
1291 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1292 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
1293 accept->flags = READ_ONCE(sqe->accept_flags);
1294 accept->nofile = rlimit(RLIMIT_NOFILE);
1295 flags = READ_ONCE(sqe->ioprio);
1296 if (flags & ~IORING_ACCEPT_MULTISHOT)
1299 accept->file_slot = READ_ONCE(sqe->file_index);
1300 if (accept->file_slot) {
1301 if (accept->flags & SOCK_CLOEXEC)
1303 if (flags & IORING_ACCEPT_MULTISHOT &&
1304 accept->file_slot != IORING_FILE_INDEX_ALLOC)
1307 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1309 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
1310 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1311 if (flags & IORING_ACCEPT_MULTISHOT)
1312 req->flags |= REQ_F_APOLL_MULTISHOT;
1316 int io_accept(struct io_kiocb *req, unsigned int issue_flags)
1318 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1319 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1320 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
1321 bool fixed = !!accept->file_slot;
1325 if (!io_check_multishot(req, issue_flags))
1329 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
1330 if (unlikely(fd < 0))
1333 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
1338 ret = PTR_ERR(file);
1339 if (ret == -EAGAIN && force_nonblock) {
1341 * if it's multishot and polled, we don't need to
1342 * return EAGAIN to arm the poll infra since it
1343 * has already been done
1345 if (issue_flags & IO_URING_F_MULTISHOT)
1346 ret = IOU_ISSUE_SKIP_COMPLETE;
1349 if (ret == -ERESTARTSYS)
1352 } else if (!fixed) {
1353 fd_install(fd, file);
1356 ret = io_fixed_fd_install(req, issue_flags, file,
1360 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
1361 io_req_set_res(req, ret, 0);
1367 if (io_aux_cqe(req, issue_flags & IO_URING_F_COMPLETE_DEFER, ret,
1368 IORING_CQE_F_MORE, true))
1374 int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1376 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1378 if (sqe->addr || sqe->rw_flags || sqe->buf_index)
1381 sock->domain = READ_ONCE(sqe->fd);
1382 sock->type = READ_ONCE(sqe->off);
1383 sock->protocol = READ_ONCE(sqe->len);
1384 sock->file_slot = READ_ONCE(sqe->file_index);
1385 sock->nofile = rlimit(RLIMIT_NOFILE);
1387 sock->flags = sock->type & ~SOCK_TYPE_MASK;
1388 if (sock->file_slot && (sock->flags & SOCK_CLOEXEC))
1390 if (sock->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1395 int io_socket(struct io_kiocb *req, unsigned int issue_flags)
1397 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1398 bool fixed = !!sock->file_slot;
1403 fd = __get_unused_fd_flags(sock->flags, sock->nofile);
1404 if (unlikely(fd < 0))
1407 file = __sys_socket_file(sock->domain, sock->type, sock->protocol);
1411 ret = PTR_ERR(file);
1412 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1414 if (ret == -ERESTARTSYS)
1417 } else if (!fixed) {
1418 fd_install(fd, file);
1421 ret = io_fixed_fd_install(req, issue_flags, file,
1424 io_req_set_res(req, ret, 0);
1428 int io_connect_prep_async(struct io_kiocb *req)
1430 struct io_async_connect *io = req->async_data;
1431 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1433 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
1436 int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1438 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1440 if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
1443 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1444 conn->addr_len = READ_ONCE(sqe->addr2);
1445 conn->in_progress = conn->seen_econnaborted = false;
1449 int io_connect(struct io_kiocb *req, unsigned int issue_flags)
1451 struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect);
1452 struct io_async_connect __io, *io;
1453 unsigned file_flags;
1455 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1457 if (connect->in_progress) {
1458 struct socket *socket;
1461 socket = sock_from_file(req->file);
1463 ret = sock_error(socket->sk);
1467 if (req_has_async_data(req)) {
1468 io = req->async_data;
1470 ret = move_addr_to_kernel(connect->addr,
1478 file_flags = force_nonblock ? O_NONBLOCK : 0;
1480 ret = __sys_connect_file(req->file, &io->address,
1481 connect->addr_len, file_flags);
1482 if ((ret == -EAGAIN || ret == -EINPROGRESS || ret == -ECONNABORTED)
1483 && force_nonblock) {
1484 if (ret == -EINPROGRESS) {
1485 connect->in_progress = true;
1488 if (ret == -ECONNABORTED) {
1489 if (connect->seen_econnaborted)
1491 connect->seen_econnaborted = true;
1493 if (req_has_async_data(req))
1495 if (io_alloc_async_data(req)) {
1499 memcpy(req->async_data, &__io, sizeof(__io));
1502 if (ret == -ERESTARTSYS)
1507 io_req_set_res(req, ret, 0);
1511 void io_netmsg_cache_free(struct io_cache_entry *entry)
1513 kfree(container_of(entry, struct io_async_msghdr, cache));