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 /* used only for send zerocopy */
69 struct io_kiocb *notif;
72 static inline bool io_check_multishot(struct io_kiocb *req,
73 unsigned int issue_flags)
76 * When ->locked_cq is set we only allow to post CQEs from the original
77 * task context. Usual request completions will be handled in other
78 * generic paths but multipoll may decide to post extra cqes.
80 return !(issue_flags & IO_URING_F_IOWQ) ||
81 !(issue_flags & IO_URING_F_MULTISHOT) ||
82 !req->ctx->task_complete;
85 int io_shutdown_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
87 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
89 if (unlikely(sqe->off || sqe->addr || sqe->rw_flags ||
90 sqe->buf_index || sqe->splice_fd_in))
93 shutdown->how = READ_ONCE(sqe->len);
94 req->flags |= REQ_F_FORCE_ASYNC;
98 int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
100 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
104 WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
106 sock = sock_from_file(req->file);
110 ret = __sys_shutdown_sock(sock, shutdown->how);
111 io_req_set_res(req, ret, 0);
115 static bool io_net_retry(struct socket *sock, int flags)
117 if (!(flags & MSG_WAITALL))
119 return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
122 static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags)
124 struct io_async_msghdr *hdr = req->async_data;
126 if (!req_has_async_data(req) || issue_flags & IO_URING_F_UNLOCKED)
129 /* Let normal cleanup path reap it if we fail adding to the cache */
130 if (io_alloc_cache_put(&req->ctx->netmsg_cache, &hdr->cache)) {
131 req->async_data = NULL;
132 req->flags &= ~REQ_F_ASYNC_DATA;
136 static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req,
137 unsigned int issue_flags)
139 struct io_ring_ctx *ctx = req->ctx;
140 struct io_cache_entry *entry;
141 struct io_async_msghdr *hdr;
143 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
144 entry = io_alloc_cache_get(&ctx->netmsg_cache);
146 hdr = container_of(entry, struct io_async_msghdr, cache);
147 hdr->free_iov = NULL;
148 req->flags |= REQ_F_ASYNC_DATA;
149 req->async_data = hdr;
154 if (!io_alloc_async_data(req)) {
155 hdr = req->async_data;
156 hdr->free_iov = NULL;
162 static inline struct io_async_msghdr *io_msg_alloc_async_prep(struct io_kiocb *req)
164 /* ->prep_async is always called from the submission context */
165 return io_msg_alloc_async(req, 0);
168 static int io_setup_async_msg(struct io_kiocb *req,
169 struct io_async_msghdr *kmsg,
170 unsigned int issue_flags)
172 struct io_async_msghdr *async_msg;
174 if (req_has_async_data(req))
176 async_msg = io_msg_alloc_async(req, issue_flags);
178 kfree(kmsg->free_iov);
181 req->flags |= REQ_F_NEED_CLEANUP;
182 memcpy(async_msg, kmsg, sizeof(*kmsg));
183 if (async_msg->msg.msg_name)
184 async_msg->msg.msg_name = &async_msg->addr;
185 /* if were using fast_iov, set it to the new one */
186 if (iter_is_iovec(&kmsg->msg.msg_iter) && !kmsg->free_iov) {
187 size_t fast_idx = kmsg->msg.msg_iter.iov - kmsg->fast_iov;
188 async_msg->msg.msg_iter.iov = &async_msg->fast_iov[fast_idx];
194 static int io_sendmsg_copy_hdr(struct io_kiocb *req,
195 struct io_async_msghdr *iomsg)
197 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
199 iomsg->msg.msg_name = &iomsg->addr;
200 iomsg->free_iov = iomsg->fast_iov;
201 return sendmsg_copy_msghdr(&iomsg->msg, sr->umsg, sr->msg_flags,
205 int io_send_prep_async(struct io_kiocb *req)
207 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
208 struct io_async_msghdr *io;
211 if (!zc->addr || req_has_async_data(req))
213 io = io_msg_alloc_async_prep(req);
216 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &io->addr);
220 static int io_setup_async_addr(struct io_kiocb *req,
221 struct sockaddr_storage *addr_storage,
222 unsigned int issue_flags)
224 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
225 struct io_async_msghdr *io;
227 if (!sr->addr || req_has_async_data(req))
229 io = io_msg_alloc_async(req, issue_flags);
232 memcpy(&io->addr, addr_storage, sizeof(io->addr));
236 int io_sendmsg_prep_async(struct io_kiocb *req)
240 if (!io_msg_alloc_async_prep(req))
242 ret = io_sendmsg_copy_hdr(req, req->async_data);
244 req->flags |= REQ_F_NEED_CLEANUP;
248 void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req)
250 struct io_async_msghdr *io = req->async_data;
255 int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
257 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
259 if (req->opcode == IORING_OP_SEND) {
260 if (READ_ONCE(sqe->__pad3[0]))
262 sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
263 sr->addr_len = READ_ONCE(sqe->addr_len);
264 } else if (sqe->addr2 || sqe->file_index) {
268 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
269 sr->len = READ_ONCE(sqe->len);
270 sr->flags = READ_ONCE(sqe->ioprio);
271 if (sr->flags & ~IORING_RECVSEND_POLL_FIRST)
273 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
274 if (sr->msg_flags & MSG_DONTWAIT)
275 req->flags |= REQ_F_NOWAIT;
278 if (req->ctx->compat)
279 sr->msg_flags |= MSG_CMSG_COMPAT;
285 int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
287 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
288 struct io_async_msghdr iomsg, *kmsg;
294 sock = sock_from_file(req->file);
298 if (req_has_async_data(req)) {
299 kmsg = req->async_data;
301 ret = io_sendmsg_copy_hdr(req, &iomsg);
307 if (!(req->flags & REQ_F_POLLED) &&
308 (sr->flags & IORING_RECVSEND_POLL_FIRST))
309 return io_setup_async_msg(req, kmsg, issue_flags);
311 flags = sr->msg_flags;
312 if (issue_flags & IO_URING_F_NONBLOCK)
313 flags |= MSG_DONTWAIT;
314 if (flags & MSG_WAITALL)
315 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
317 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
320 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
321 return io_setup_async_msg(req, kmsg, issue_flags);
322 if (ret > 0 && io_net_retry(sock, flags)) {
324 req->flags |= REQ_F_PARTIAL_IO;
325 return io_setup_async_msg(req, kmsg, issue_flags);
327 if (ret == -ERESTARTSYS)
331 /* fast path, check for non-NULL to avoid function call */
333 kfree(kmsg->free_iov);
334 req->flags &= ~REQ_F_NEED_CLEANUP;
335 io_netmsg_recycle(req, issue_flags);
338 else if (sr->done_io)
340 io_req_set_res(req, ret, 0);
344 int io_send(struct io_kiocb *req, unsigned int issue_flags)
346 struct sockaddr_storage __address;
347 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
355 msg.msg_control = NULL;
356 msg.msg_controllen = 0;
361 if (req_has_async_data(req)) {
362 struct io_async_msghdr *io = req->async_data;
364 msg.msg_name = &io->addr;
366 ret = move_addr_to_kernel(sr->addr, sr->addr_len, &__address);
367 if (unlikely(ret < 0))
369 msg.msg_name = (struct sockaddr *)&__address;
371 msg.msg_namelen = sr->addr_len;
374 if (!(req->flags & REQ_F_POLLED) &&
375 (sr->flags & IORING_RECVSEND_POLL_FIRST))
376 return io_setup_async_addr(req, &__address, issue_flags);
378 sock = sock_from_file(req->file);
382 ret = import_ubuf(ITER_SOURCE, sr->buf, sr->len, &msg.msg_iter);
386 flags = sr->msg_flags;
387 if (issue_flags & IO_URING_F_NONBLOCK)
388 flags |= MSG_DONTWAIT;
389 if (flags & MSG_WAITALL)
390 min_ret = iov_iter_count(&msg.msg_iter);
392 msg.msg_flags = flags;
393 ret = sock_sendmsg(sock, &msg);
395 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
396 return io_setup_async_addr(req, &__address, issue_flags);
398 if (ret > 0 && io_net_retry(sock, flags)) {
402 req->flags |= REQ_F_PARTIAL_IO;
403 return io_setup_async_addr(req, &__address, issue_flags);
405 if (ret == -ERESTARTSYS)
411 else if (sr->done_io)
413 io_req_set_res(req, ret, 0);
417 static bool io_recvmsg_multishot_overflow(struct io_async_msghdr *iomsg)
421 if (iomsg->namelen < 0)
423 if (check_add_overflow((int)sizeof(struct io_uring_recvmsg_out),
424 iomsg->namelen, &hdr))
426 if (check_add_overflow(hdr, (int)iomsg->controllen, &hdr))
432 static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
433 struct io_async_msghdr *iomsg)
435 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
436 struct user_msghdr msg;
439 if (copy_from_user(&msg, sr->umsg, sizeof(*sr->umsg)))
442 ret = __copy_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
446 if (req->flags & REQ_F_BUFFER_SELECT) {
447 if (msg.msg_iovlen == 0) {
448 sr->len = iomsg->fast_iov[0].iov_len = 0;
449 iomsg->fast_iov[0].iov_base = NULL;
450 iomsg->free_iov = NULL;
451 } else if (msg.msg_iovlen > 1) {
454 if (copy_from_user(iomsg->fast_iov, msg.msg_iov, sizeof(*msg.msg_iov)))
456 sr->len = iomsg->fast_iov[0].iov_len;
457 iomsg->free_iov = NULL;
460 if (req->flags & REQ_F_APOLL_MULTISHOT) {
461 iomsg->namelen = msg.msg_namelen;
462 iomsg->controllen = msg.msg_controllen;
463 if (io_recvmsg_multishot_overflow(iomsg))
467 iomsg->free_iov = iomsg->fast_iov;
468 ret = __import_iovec(ITER_DEST, msg.msg_iov, msg.msg_iovlen, UIO_FASTIOV,
469 &iomsg->free_iov, &iomsg->msg.msg_iter,
479 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
480 struct io_async_msghdr *iomsg)
482 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
483 struct compat_msghdr msg;
484 struct compat_iovec __user *uiov;
487 if (copy_from_user(&msg, sr->umsg_compat, sizeof(msg)))
490 ret = __get_compat_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
494 uiov = compat_ptr(msg.msg_iov);
495 if (req->flags & REQ_F_BUFFER_SELECT) {
498 iomsg->free_iov = NULL;
499 if (msg.msg_iovlen == 0) {
501 } else if (msg.msg_iovlen > 1) {
504 if (!access_ok(uiov, sizeof(*uiov)))
506 if (__get_user(clen, &uiov->iov_len))
513 if (req->flags & REQ_F_APOLL_MULTISHOT) {
514 iomsg->namelen = msg.msg_namelen;
515 iomsg->controllen = msg.msg_controllen;
516 if (io_recvmsg_multishot_overflow(iomsg))
520 iomsg->free_iov = iomsg->fast_iov;
521 ret = __import_iovec(ITER_DEST, (struct iovec __user *)uiov, msg.msg_iovlen,
522 UIO_FASTIOV, &iomsg->free_iov,
523 &iomsg->msg.msg_iter, true);
532 static int io_recvmsg_copy_hdr(struct io_kiocb *req,
533 struct io_async_msghdr *iomsg)
535 iomsg->msg.msg_name = &iomsg->addr;
538 if (req->ctx->compat)
539 return __io_compat_recvmsg_copy_hdr(req, iomsg);
542 return __io_recvmsg_copy_hdr(req, iomsg);
545 int io_recvmsg_prep_async(struct io_kiocb *req)
549 if (!io_msg_alloc_async_prep(req))
551 ret = io_recvmsg_copy_hdr(req, req->async_data);
553 req->flags |= REQ_F_NEED_CLEANUP;
557 #define RECVMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT)
559 int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
561 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
563 if (unlikely(sqe->file_index || sqe->addr2))
566 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
567 sr->len = READ_ONCE(sqe->len);
568 sr->flags = READ_ONCE(sqe->ioprio);
569 if (sr->flags & ~(RECVMSG_FLAGS))
571 sr->msg_flags = READ_ONCE(sqe->msg_flags);
572 if (sr->msg_flags & MSG_DONTWAIT)
573 req->flags |= REQ_F_NOWAIT;
574 if (sr->msg_flags & MSG_ERRQUEUE)
575 req->flags |= REQ_F_CLEAR_POLLIN;
576 if (sr->flags & IORING_RECV_MULTISHOT) {
577 if (!(req->flags & REQ_F_BUFFER_SELECT))
579 if (sr->msg_flags & MSG_WAITALL)
581 if (req->opcode == IORING_OP_RECV && sr->len)
583 req->flags |= REQ_F_APOLL_MULTISHOT;
585 * Store the buffer group for this multishot receive separately,
586 * as if we end up doing an io-wq based issue that selects a
587 * buffer, it has to be committed immediately and that will
588 * clear ->buf_list. This means we lose the link to the buffer
589 * list, and the eventual buffer put on completion then cannot
592 sr->buf_group = req->buf_index;
596 if (req->ctx->compat)
597 sr->msg_flags |= MSG_CMSG_COMPAT;
603 static inline void io_recv_prep_retry(struct io_kiocb *req)
605 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
608 sr->len = 0; /* get from the provided buffer */
609 req->buf_index = sr->buf_group;
613 * Finishes io_recv and io_recvmsg.
615 * Returns true if it is actually finished, or false if it should run
616 * again (for multishot).
618 static inline bool io_recv_finish(struct io_kiocb *req, int *ret,
619 unsigned int cflags, bool mshot_finished,
620 unsigned issue_flags)
622 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
623 io_req_set_res(req, *ret, cflags);
628 if (!mshot_finished) {
629 if (io_aux_cqe(req->ctx, issue_flags & IO_URING_F_COMPLETE_DEFER,
630 req->cqe.user_data, *ret, cflags | IORING_CQE_F_MORE, true)) {
631 io_recv_prep_retry(req);
634 /* Otherwise stop multishot but use the current result. */
637 io_req_set_res(req, *ret, cflags);
639 if (issue_flags & IO_URING_F_MULTISHOT)
640 *ret = IOU_STOP_MULTISHOT;
646 static int io_recvmsg_prep_multishot(struct io_async_msghdr *kmsg,
647 struct io_sr_msg *sr, void __user **buf,
650 unsigned long ubuf = (unsigned long) *buf;
653 hdr = sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
658 if (kmsg->controllen) {
659 unsigned long control = ubuf + hdr - kmsg->controllen;
661 kmsg->msg.msg_control_user = (void __user *) control;
662 kmsg->msg.msg_controllen = kmsg->controllen;
665 sr->buf = *buf; /* stash for later copy */
666 *buf = (void __user *) (ubuf + hdr);
667 kmsg->payloadlen = *len = *len - hdr;
671 struct io_recvmsg_multishot_hdr {
672 struct io_uring_recvmsg_out msg;
673 struct sockaddr_storage addr;
676 static int io_recvmsg_multishot(struct socket *sock, struct io_sr_msg *io,
677 struct io_async_msghdr *kmsg,
678 unsigned int flags, bool *finished)
682 struct io_recvmsg_multishot_hdr hdr;
685 kmsg->msg.msg_name = &hdr.addr;
686 kmsg->msg.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
687 kmsg->msg.msg_namelen = 0;
689 if (sock->file->f_flags & O_NONBLOCK)
690 flags |= MSG_DONTWAIT;
692 err = sock_recvmsg(sock, &kmsg->msg, flags);
693 *finished = err <= 0;
697 hdr.msg = (struct io_uring_recvmsg_out) {
698 .controllen = kmsg->controllen - kmsg->msg.msg_controllen,
699 .flags = kmsg->msg.msg_flags & ~MSG_CMSG_COMPAT
702 hdr.msg.payloadlen = err;
703 if (err > kmsg->payloadlen)
704 err = kmsg->payloadlen;
706 copy_len = sizeof(struct io_uring_recvmsg_out);
707 if (kmsg->msg.msg_namelen > kmsg->namelen)
708 copy_len += kmsg->namelen;
710 copy_len += kmsg->msg.msg_namelen;
713 * "fromlen shall refer to the value before truncation.."
716 hdr.msg.namelen = kmsg->msg.msg_namelen;
718 /* ensure that there is no gap between hdr and sockaddr_storage */
719 BUILD_BUG_ON(offsetof(struct io_recvmsg_multishot_hdr, addr) !=
720 sizeof(struct io_uring_recvmsg_out));
721 if (copy_to_user(io->buf, &hdr, copy_len)) {
726 return sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
727 kmsg->controllen + err;
730 int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
732 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
733 struct io_async_msghdr iomsg, *kmsg;
737 int ret, min_ret = 0;
738 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
739 bool mshot_finished = true;
741 sock = sock_from_file(req->file);
745 if (req_has_async_data(req)) {
746 kmsg = req->async_data;
748 ret = io_recvmsg_copy_hdr(req, &iomsg);
754 if (!(req->flags & REQ_F_POLLED) &&
755 (sr->flags & IORING_RECVSEND_POLL_FIRST))
756 return io_setup_async_msg(req, kmsg, issue_flags);
758 if (!io_check_multishot(req, issue_flags))
759 return io_setup_async_msg(req, kmsg, issue_flags);
762 if (io_do_buffer_select(req)) {
764 size_t len = sr->len;
766 buf = io_buffer_select(req, &len, issue_flags);
770 if (req->flags & REQ_F_APOLL_MULTISHOT) {
771 ret = io_recvmsg_prep_multishot(kmsg, sr, &buf, &len);
773 io_kbuf_recycle(req, issue_flags);
778 iov_iter_ubuf(&kmsg->msg.msg_iter, ITER_DEST, buf, len);
781 flags = sr->msg_flags;
783 flags |= MSG_DONTWAIT;
784 if (flags & MSG_WAITALL)
785 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
787 kmsg->msg.msg_get_inq = 1;
788 if (req->flags & REQ_F_APOLL_MULTISHOT)
789 ret = io_recvmsg_multishot(sock, sr, kmsg, flags,
792 ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg,
796 if (ret == -EAGAIN && force_nonblock) {
797 ret = io_setup_async_msg(req, kmsg, issue_flags);
798 if (ret == -EAGAIN && (issue_flags & IO_URING_F_MULTISHOT)) {
799 io_kbuf_recycle(req, issue_flags);
800 return IOU_ISSUE_SKIP_COMPLETE;
804 if (ret > 0 && io_net_retry(sock, flags)) {
806 req->flags |= REQ_F_PARTIAL_IO;
807 return io_setup_async_msg(req, kmsg, issue_flags);
809 if (ret == -ERESTARTSYS)
812 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
818 else if (sr->done_io)
821 io_kbuf_recycle(req, issue_flags);
823 cflags = io_put_kbuf(req, issue_flags);
824 if (kmsg->msg.msg_inq)
825 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
827 if (!io_recv_finish(req, &ret, cflags, mshot_finished, issue_flags))
828 goto retry_multishot;
830 if (mshot_finished) {
831 /* fast path, check for non-NULL to avoid function call */
833 kfree(kmsg->free_iov);
834 io_netmsg_recycle(req, issue_flags);
835 req->flags &= ~REQ_F_NEED_CLEANUP;
841 int io_recv(struct io_kiocb *req, unsigned int issue_flags)
843 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
848 int ret, min_ret = 0;
849 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
850 size_t len = sr->len;
852 if (!(req->flags & REQ_F_POLLED) &&
853 (sr->flags & IORING_RECVSEND_POLL_FIRST))
856 if (!io_check_multishot(req, issue_flags))
859 sock = sock_from_file(req->file);
864 if (io_do_buffer_select(req)) {
867 buf = io_buffer_select(req, &len, issue_flags);
873 ret = import_ubuf(ITER_DEST, sr->buf, len, &msg.msg_iter);
879 msg.msg_control = NULL;
882 msg.msg_controllen = 0;
886 flags = sr->msg_flags;
888 flags |= MSG_DONTWAIT;
889 if (flags & MSG_WAITALL)
890 min_ret = iov_iter_count(&msg.msg_iter);
892 ret = sock_recvmsg(sock, &msg, flags);
894 if (ret == -EAGAIN && force_nonblock) {
895 if (issue_flags & IO_URING_F_MULTISHOT) {
896 io_kbuf_recycle(req, issue_flags);
897 return IOU_ISSUE_SKIP_COMPLETE;
902 if (ret > 0 && io_net_retry(sock, flags)) {
906 req->flags |= REQ_F_PARTIAL_IO;
909 if (ret == -ERESTARTSYS)
912 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
919 else if (sr->done_io)
922 io_kbuf_recycle(req, issue_flags);
924 cflags = io_put_kbuf(req, issue_flags);
926 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
928 if (!io_recv_finish(req, &ret, cflags, ret <= 0, issue_flags))
929 goto retry_multishot;
934 void io_send_zc_cleanup(struct io_kiocb *req)
936 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
937 struct io_async_msghdr *io;
939 if (req_has_async_data(req)) {
940 io = req->async_data;
941 /* might be ->fast_iov if *msg_copy_hdr failed */
942 if (io->free_iov != io->fast_iov)
946 io_notif_flush(zc->notif);
951 #define IO_ZC_FLAGS_COMMON (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_FIXED_BUF)
952 #define IO_ZC_FLAGS_VALID (IO_ZC_FLAGS_COMMON | IORING_SEND_ZC_REPORT_USAGE)
954 int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
956 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
957 struct io_ring_ctx *ctx = req->ctx;
958 struct io_kiocb *notif;
960 if (unlikely(READ_ONCE(sqe->__pad2[0]) || READ_ONCE(sqe->addr3)))
962 /* we don't support IOSQE_CQE_SKIP_SUCCESS just yet */
963 if (req->flags & REQ_F_CQE_SKIP)
966 notif = zc->notif = io_alloc_notif(ctx);
969 notif->cqe.user_data = req->cqe.user_data;
971 notif->cqe.flags = IORING_CQE_F_NOTIF;
972 req->flags |= REQ_F_NEED_CLEANUP;
974 zc->flags = READ_ONCE(sqe->ioprio);
975 if (unlikely(zc->flags & ~IO_ZC_FLAGS_COMMON)) {
976 if (zc->flags & ~IO_ZC_FLAGS_VALID)
978 if (zc->flags & IORING_SEND_ZC_REPORT_USAGE) {
979 io_notif_set_extended(notif);
980 io_notif_to_data(notif)->zc_report = true;
984 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
985 unsigned idx = READ_ONCE(sqe->buf_index);
987 if (unlikely(idx >= ctx->nr_user_bufs))
989 idx = array_index_nospec(idx, ctx->nr_user_bufs);
990 req->imu = READ_ONCE(ctx->user_bufs[idx]);
991 io_req_set_rsrc_node(notif, ctx, 0);
994 if (req->opcode == IORING_OP_SEND_ZC) {
995 if (READ_ONCE(sqe->__pad3[0]))
997 zc->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
998 zc->addr_len = READ_ONCE(sqe->addr_len);
1000 if (unlikely(sqe->addr2 || sqe->file_index))
1002 if (unlikely(zc->flags & IORING_RECVSEND_FIXED_BUF))
1006 zc->buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
1007 zc->len = READ_ONCE(sqe->len);
1008 zc->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
1009 if (zc->msg_flags & MSG_DONTWAIT)
1010 req->flags |= REQ_F_NOWAIT;
1014 #ifdef CONFIG_COMPAT
1015 if (req->ctx->compat)
1016 zc->msg_flags |= MSG_CMSG_COMPAT;
1021 static int io_sg_from_iter_iovec(struct sock *sk, struct sk_buff *skb,
1022 struct iov_iter *from, size_t length)
1024 skb_zcopy_downgrade_managed(skb);
1025 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1028 static int io_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1029 struct iov_iter *from, size_t length)
1031 struct skb_shared_info *shinfo = skb_shinfo(skb);
1032 int frag = shinfo->nr_frags;
1034 struct bvec_iter bi;
1036 unsigned long truesize = 0;
1039 shinfo->flags |= SKBFL_MANAGED_FRAG_REFS;
1040 else if (unlikely(!skb_zcopy_managed(skb)))
1041 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1043 bi.bi_size = min(from->count, length);
1044 bi.bi_bvec_done = from->iov_offset;
1047 while (bi.bi_size && frag < MAX_SKB_FRAGS) {
1048 struct bio_vec v = mp_bvec_iter_bvec(from->bvec, bi);
1051 truesize += PAGE_ALIGN(v.bv_len + v.bv_offset);
1052 __skb_fill_page_desc_noacc(shinfo, frag++, v.bv_page,
1053 v.bv_offset, v.bv_len);
1054 bvec_iter_advance_single(from->bvec, &bi, v.bv_len);
1059 shinfo->nr_frags = frag;
1060 from->bvec += bi.bi_idx;
1061 from->nr_segs -= bi.bi_idx;
1062 from->count -= copied;
1063 from->iov_offset = bi.bi_bvec_done;
1065 skb->data_len += copied;
1067 skb->truesize += truesize;
1069 if (sk && sk->sk_type == SOCK_STREAM) {
1070 sk_wmem_queued_add(sk, truesize);
1071 if (!skb_zcopy_pure(skb))
1072 sk_mem_charge(sk, truesize);
1074 refcount_add(truesize, &skb->sk->sk_wmem_alloc);
1079 int io_send_zc(struct io_kiocb *req, unsigned int issue_flags)
1081 struct sockaddr_storage __address;
1082 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
1084 struct socket *sock;
1086 int ret, min_ret = 0;
1088 sock = sock_from_file(req->file);
1089 if (unlikely(!sock))
1091 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1094 msg.msg_name = NULL;
1095 msg.msg_control = NULL;
1096 msg.msg_controllen = 0;
1097 msg.msg_namelen = 0;
1100 if (req_has_async_data(req)) {
1101 struct io_async_msghdr *io = req->async_data;
1103 msg.msg_name = &io->addr;
1105 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &__address);
1106 if (unlikely(ret < 0))
1108 msg.msg_name = (struct sockaddr *)&__address;
1110 msg.msg_namelen = zc->addr_len;
1113 if (!(req->flags & REQ_F_POLLED) &&
1114 (zc->flags & IORING_RECVSEND_POLL_FIRST))
1115 return io_setup_async_addr(req, &__address, issue_flags);
1117 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
1118 ret = io_import_fixed(ITER_SOURCE, &msg.msg_iter, req->imu,
1119 (u64)(uintptr_t)zc->buf, zc->len);
1122 msg.sg_from_iter = io_sg_from_iter;
1124 io_notif_set_extended(zc->notif);
1125 ret = import_ubuf(ITER_SOURCE, zc->buf, zc->len, &msg.msg_iter);
1128 ret = io_notif_account_mem(zc->notif, zc->len);
1131 msg.sg_from_iter = io_sg_from_iter_iovec;
1134 msg_flags = zc->msg_flags | MSG_ZEROCOPY;
1135 if (issue_flags & IO_URING_F_NONBLOCK)
1136 msg_flags |= MSG_DONTWAIT;
1137 if (msg_flags & MSG_WAITALL)
1138 min_ret = iov_iter_count(&msg.msg_iter);
1140 msg.msg_flags = msg_flags;
1141 msg.msg_ubuf = &io_notif_to_data(zc->notif)->uarg;
1142 ret = sock_sendmsg(sock, &msg);
1144 if (unlikely(ret < min_ret)) {
1145 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1146 return io_setup_async_addr(req, &__address, issue_flags);
1148 if (ret > 0 && io_net_retry(sock, msg.msg_flags)) {
1152 req->flags |= REQ_F_PARTIAL_IO;
1153 return io_setup_async_addr(req, &__address, issue_flags);
1155 if (ret == -ERESTARTSYS)
1162 else if (zc->done_io)
1166 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1167 * flushing notif to io_send_zc_cleanup()
1169 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1170 io_notif_flush(zc->notif);
1171 req->flags &= ~REQ_F_NEED_CLEANUP;
1173 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1177 int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags)
1179 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1180 struct io_async_msghdr iomsg, *kmsg;
1181 struct socket *sock;
1183 int ret, min_ret = 0;
1185 io_notif_set_extended(sr->notif);
1187 sock = sock_from_file(req->file);
1188 if (unlikely(!sock))
1190 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1193 if (req_has_async_data(req)) {
1194 kmsg = req->async_data;
1196 ret = io_sendmsg_copy_hdr(req, &iomsg);
1202 if (!(req->flags & REQ_F_POLLED) &&
1203 (sr->flags & IORING_RECVSEND_POLL_FIRST))
1204 return io_setup_async_msg(req, kmsg, issue_flags);
1206 flags = sr->msg_flags | MSG_ZEROCOPY;
1207 if (issue_flags & IO_URING_F_NONBLOCK)
1208 flags |= MSG_DONTWAIT;
1209 if (flags & MSG_WAITALL)
1210 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
1212 kmsg->msg.msg_ubuf = &io_notif_to_data(sr->notif)->uarg;
1213 kmsg->msg.sg_from_iter = io_sg_from_iter_iovec;
1214 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
1216 if (unlikely(ret < min_ret)) {
1217 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1218 return io_setup_async_msg(req, kmsg, issue_flags);
1220 if (ret > 0 && io_net_retry(sock, flags)) {
1222 req->flags |= REQ_F_PARTIAL_IO;
1223 return io_setup_async_msg(req, kmsg, issue_flags);
1225 if (ret == -ERESTARTSYS)
1229 /* fast path, check for non-NULL to avoid function call */
1230 if (kmsg->free_iov) {
1231 kfree(kmsg->free_iov);
1232 kmsg->free_iov = NULL;
1235 io_netmsg_recycle(req, issue_flags);
1238 else if (sr->done_io)
1242 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1243 * flushing notif to io_send_zc_cleanup()
1245 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1246 io_notif_flush(sr->notif);
1247 req->flags &= ~REQ_F_NEED_CLEANUP;
1249 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1253 void io_sendrecv_fail(struct io_kiocb *req)
1255 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1257 if (req->flags & REQ_F_PARTIAL_IO)
1258 req->cqe.res = sr->done_io;
1260 if ((req->flags & REQ_F_NEED_CLEANUP) &&
1261 (req->opcode == IORING_OP_SEND_ZC || req->opcode == IORING_OP_SENDMSG_ZC))
1262 req->cqe.flags |= IORING_CQE_F_MORE;
1265 int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1267 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1270 if (sqe->len || sqe->buf_index)
1273 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1274 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
1275 accept->flags = READ_ONCE(sqe->accept_flags);
1276 accept->nofile = rlimit(RLIMIT_NOFILE);
1277 flags = READ_ONCE(sqe->ioprio);
1278 if (flags & ~IORING_ACCEPT_MULTISHOT)
1281 accept->file_slot = READ_ONCE(sqe->file_index);
1282 if (accept->file_slot) {
1283 if (accept->flags & SOCK_CLOEXEC)
1285 if (flags & IORING_ACCEPT_MULTISHOT &&
1286 accept->file_slot != IORING_FILE_INDEX_ALLOC)
1289 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1291 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
1292 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1293 if (flags & IORING_ACCEPT_MULTISHOT)
1294 req->flags |= REQ_F_APOLL_MULTISHOT;
1298 int io_accept(struct io_kiocb *req, unsigned int issue_flags)
1300 struct io_ring_ctx *ctx = req->ctx;
1301 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1302 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1303 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
1304 bool fixed = !!accept->file_slot;
1308 if (!io_check_multishot(req, issue_flags))
1312 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
1313 if (unlikely(fd < 0))
1316 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
1321 ret = PTR_ERR(file);
1322 if (ret == -EAGAIN && force_nonblock) {
1324 * if it's multishot and polled, we don't need to
1325 * return EAGAIN to arm the poll infra since it
1326 * has already been done
1328 if (issue_flags & IO_URING_F_MULTISHOT)
1329 ret = IOU_ISSUE_SKIP_COMPLETE;
1332 if (ret == -ERESTARTSYS)
1335 } else if (!fixed) {
1336 fd_install(fd, file);
1339 ret = io_fixed_fd_install(req, issue_flags, file,
1343 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
1344 io_req_set_res(req, ret, 0);
1350 if (io_aux_cqe(ctx, issue_flags & IO_URING_F_COMPLETE_DEFER,
1351 req->cqe.user_data, ret, IORING_CQE_F_MORE, true))
1357 int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1359 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1361 if (sqe->addr || sqe->rw_flags || sqe->buf_index)
1364 sock->domain = READ_ONCE(sqe->fd);
1365 sock->type = READ_ONCE(sqe->off);
1366 sock->protocol = READ_ONCE(sqe->len);
1367 sock->file_slot = READ_ONCE(sqe->file_index);
1368 sock->nofile = rlimit(RLIMIT_NOFILE);
1370 sock->flags = sock->type & ~SOCK_TYPE_MASK;
1371 if (sock->file_slot && (sock->flags & SOCK_CLOEXEC))
1373 if (sock->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1378 int io_socket(struct io_kiocb *req, unsigned int issue_flags)
1380 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1381 bool fixed = !!sock->file_slot;
1386 fd = __get_unused_fd_flags(sock->flags, sock->nofile);
1387 if (unlikely(fd < 0))
1390 file = __sys_socket_file(sock->domain, sock->type, sock->protocol);
1394 ret = PTR_ERR(file);
1395 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1397 if (ret == -ERESTARTSYS)
1400 } else if (!fixed) {
1401 fd_install(fd, file);
1404 ret = io_fixed_fd_install(req, issue_flags, file,
1407 io_req_set_res(req, ret, 0);
1411 int io_connect_prep_async(struct io_kiocb *req)
1413 struct io_async_connect *io = req->async_data;
1414 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1416 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
1419 int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1421 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1423 if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
1426 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1427 conn->addr_len = READ_ONCE(sqe->addr2);
1428 conn->in_progress = conn->seen_econnaborted = false;
1432 int io_connect(struct io_kiocb *req, unsigned int issue_flags)
1434 struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect);
1435 struct io_async_connect __io, *io;
1436 unsigned file_flags;
1438 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1440 if (connect->in_progress) {
1441 struct socket *socket;
1444 socket = sock_from_file(req->file);
1446 ret = sock_error(socket->sk);
1450 if (req_has_async_data(req)) {
1451 io = req->async_data;
1453 ret = move_addr_to_kernel(connect->addr,
1461 file_flags = force_nonblock ? O_NONBLOCK : 0;
1463 ret = __sys_connect_file(req->file, &io->address,
1464 connect->addr_len, file_flags);
1465 if ((ret == -EAGAIN || ret == -EINPROGRESS || ret == -ECONNABORTED)
1466 && force_nonblock) {
1467 if (ret == -EINPROGRESS) {
1468 connect->in_progress = true;
1471 if (ret == -ECONNABORTED) {
1472 if (connect->seen_econnaborted)
1474 connect->seen_econnaborted = true;
1476 if (req_has_async_data(req))
1478 if (io_alloc_async_data(req)) {
1482 memcpy(req->async_data, &__io, sizeof(__io));
1485 if (ret == -ERESTARTSYS)
1490 io_req_set_res(req, ret, 0);
1494 void io_netmsg_cache_free(struct io_cache_entry *entry)
1496 kfree(container_of(entry, struct io_async_msghdr, cache));