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;
55 struct compat_msghdr __user *umsg_compat;
56 struct user_msghdr __user *umsg;
63 /* initialised and used only by !msg send variants */
67 /* used only for send zerocopy */
68 struct io_kiocb *notif;
71 int io_shutdown_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
73 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
75 if (unlikely(sqe->off || sqe->addr || sqe->rw_flags ||
76 sqe->buf_index || sqe->splice_fd_in))
79 shutdown->how = READ_ONCE(sqe->len);
83 int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
85 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
89 if (issue_flags & IO_URING_F_NONBLOCK)
92 sock = sock_from_file(req->file);
96 ret = __sys_shutdown_sock(sock, shutdown->how);
97 io_req_set_res(req, ret, 0);
101 static bool io_net_retry(struct socket *sock, int flags)
103 if (!(flags & MSG_WAITALL))
105 return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
108 static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags)
110 struct io_async_msghdr *hdr = req->async_data;
112 if (!req_has_async_data(req) || issue_flags & IO_URING_F_UNLOCKED)
115 /* Let normal cleanup path reap it if we fail adding to the cache */
116 if (io_alloc_cache_put(&req->ctx->netmsg_cache, &hdr->cache)) {
117 req->async_data = NULL;
118 req->flags &= ~REQ_F_ASYNC_DATA;
122 static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req,
123 unsigned int issue_flags)
125 struct io_ring_ctx *ctx = req->ctx;
126 struct io_cache_entry *entry;
127 struct io_async_msghdr *hdr;
129 if (!(issue_flags & IO_URING_F_UNLOCKED) &&
130 (entry = io_alloc_cache_get(&ctx->netmsg_cache)) != NULL) {
131 hdr = container_of(entry, struct io_async_msghdr, cache);
132 hdr->free_iov = NULL;
133 req->flags |= REQ_F_ASYNC_DATA;
134 req->async_data = hdr;
138 if (!io_alloc_async_data(req)) {
139 hdr = req->async_data;
140 hdr->free_iov = NULL;
146 static inline struct io_async_msghdr *io_msg_alloc_async_prep(struct io_kiocb *req)
148 /* ->prep_async is always called from the submission context */
149 return io_msg_alloc_async(req, 0);
152 static int io_setup_async_msg(struct io_kiocb *req,
153 struct io_async_msghdr *kmsg,
154 unsigned int issue_flags)
156 struct io_async_msghdr *async_msg;
158 if (req_has_async_data(req))
160 async_msg = io_msg_alloc_async(req, issue_flags);
162 kfree(kmsg->free_iov);
165 req->flags |= REQ_F_NEED_CLEANUP;
166 memcpy(async_msg, kmsg, sizeof(*kmsg));
167 if (async_msg->msg.msg_name)
168 async_msg->msg.msg_name = &async_msg->addr;
169 /* if were using fast_iov, set it to the new one */
170 if (!kmsg->free_iov) {
171 size_t fast_idx = kmsg->msg.msg_iter.iov - kmsg->fast_iov;
172 async_msg->msg.msg_iter.iov = &async_msg->fast_iov[fast_idx];
178 static int io_sendmsg_copy_hdr(struct io_kiocb *req,
179 struct io_async_msghdr *iomsg)
181 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
183 iomsg->msg.msg_name = &iomsg->addr;
184 iomsg->free_iov = iomsg->fast_iov;
185 return sendmsg_copy_msghdr(&iomsg->msg, sr->umsg, sr->msg_flags,
189 int io_send_prep_async(struct io_kiocb *req)
191 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
192 struct io_async_msghdr *io;
195 if (!zc->addr || req_has_async_data(req))
197 io = io_msg_alloc_async_prep(req);
200 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &io->addr);
204 static int io_setup_async_addr(struct io_kiocb *req,
205 struct sockaddr_storage *addr_storage,
206 unsigned int issue_flags)
208 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
209 struct io_async_msghdr *io;
211 if (!sr->addr || req_has_async_data(req))
213 io = io_msg_alloc_async(req, issue_flags);
216 memcpy(&io->addr, addr_storage, sizeof(io->addr));
220 int io_sendmsg_prep_async(struct io_kiocb *req)
224 if (!io_msg_alloc_async_prep(req))
226 ret = io_sendmsg_copy_hdr(req, req->async_data);
228 req->flags |= REQ_F_NEED_CLEANUP;
232 void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req)
234 struct io_async_msghdr *io = req->async_data;
239 int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
241 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
243 if (req->opcode == IORING_OP_SEND) {
244 if (READ_ONCE(sqe->__pad3[0]))
246 sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
247 sr->addr_len = READ_ONCE(sqe->addr_len);
248 } else if (sqe->addr2 || sqe->file_index) {
252 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
253 sr->len = READ_ONCE(sqe->len);
254 sr->flags = READ_ONCE(sqe->ioprio);
255 if (sr->flags & ~IORING_RECVSEND_POLL_FIRST)
257 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
258 if (sr->msg_flags & MSG_DONTWAIT)
259 req->flags |= REQ_F_NOWAIT;
262 if (req->ctx->compat)
263 sr->msg_flags |= MSG_CMSG_COMPAT;
269 int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
271 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
272 struct io_async_msghdr iomsg, *kmsg;
278 sock = sock_from_file(req->file);
282 if (req_has_async_data(req)) {
283 kmsg = req->async_data;
285 ret = io_sendmsg_copy_hdr(req, &iomsg);
291 if (!(req->flags & REQ_F_POLLED) &&
292 (sr->flags & IORING_RECVSEND_POLL_FIRST))
293 return io_setup_async_msg(req, kmsg, issue_flags);
295 flags = sr->msg_flags;
296 if (issue_flags & IO_URING_F_NONBLOCK)
297 flags |= MSG_DONTWAIT;
298 if (flags & MSG_WAITALL)
299 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
301 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
304 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
305 return io_setup_async_msg(req, kmsg, issue_flags);
306 if (ret > 0 && io_net_retry(sock, flags)) {
308 req->flags |= REQ_F_PARTIAL_IO;
309 return io_setup_async_msg(req, kmsg, issue_flags);
311 if (ret == -ERESTARTSYS)
315 /* fast path, check for non-NULL to avoid function call */
317 kfree(kmsg->free_iov);
318 req->flags &= ~REQ_F_NEED_CLEANUP;
319 io_netmsg_recycle(req, issue_flags);
322 else if (sr->done_io)
324 io_req_set_res(req, ret, 0);
328 int io_send(struct io_kiocb *req, unsigned int issue_flags)
330 struct sockaddr_storage __address;
331 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
340 msg.msg_control = NULL;
341 msg.msg_controllen = 0;
346 if (req_has_async_data(req)) {
347 struct io_async_msghdr *io = req->async_data;
349 msg.msg_name = &io->addr;
351 ret = move_addr_to_kernel(sr->addr, sr->addr_len, &__address);
352 if (unlikely(ret < 0))
354 msg.msg_name = (struct sockaddr *)&__address;
356 msg.msg_namelen = sr->addr_len;
359 if (!(req->flags & REQ_F_POLLED) &&
360 (sr->flags & IORING_RECVSEND_POLL_FIRST))
361 return io_setup_async_addr(req, &__address, issue_flags);
363 sock = sock_from_file(req->file);
367 ret = import_single_range(ITER_SOURCE, sr->buf, sr->len, &iov, &msg.msg_iter);
371 flags = sr->msg_flags;
372 if (issue_flags & IO_URING_F_NONBLOCK)
373 flags |= MSG_DONTWAIT;
374 if (flags & MSG_WAITALL)
375 min_ret = iov_iter_count(&msg.msg_iter);
377 msg.msg_flags = flags;
378 ret = sock_sendmsg(sock, &msg);
380 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
381 return io_setup_async_addr(req, &__address, issue_flags);
383 if (ret > 0 && io_net_retry(sock, flags)) {
387 req->flags |= REQ_F_PARTIAL_IO;
388 return io_setup_async_addr(req, &__address, issue_flags);
390 if (ret == -ERESTARTSYS)
396 else if (sr->done_io)
398 io_req_set_res(req, ret, 0);
402 static bool io_recvmsg_multishot_overflow(struct io_async_msghdr *iomsg)
406 if (iomsg->namelen < 0)
408 if (check_add_overflow((int)sizeof(struct io_uring_recvmsg_out),
409 iomsg->namelen, &hdr))
411 if (check_add_overflow(hdr, (int)iomsg->controllen, &hdr))
417 static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
418 struct io_async_msghdr *iomsg)
420 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
421 struct user_msghdr msg;
424 if (copy_from_user(&msg, sr->umsg, sizeof(*sr->umsg)))
427 ret = __copy_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
431 if (req->flags & REQ_F_BUFFER_SELECT) {
432 if (msg.msg_iovlen == 0) {
433 sr->len = iomsg->fast_iov[0].iov_len = 0;
434 iomsg->fast_iov[0].iov_base = NULL;
435 iomsg->free_iov = NULL;
436 } else if (msg.msg_iovlen > 1) {
439 if (copy_from_user(iomsg->fast_iov, msg.msg_iov, sizeof(*msg.msg_iov)))
441 sr->len = iomsg->fast_iov[0].iov_len;
442 iomsg->free_iov = NULL;
445 if (req->flags & REQ_F_APOLL_MULTISHOT) {
446 iomsg->namelen = msg.msg_namelen;
447 iomsg->controllen = msg.msg_controllen;
448 if (io_recvmsg_multishot_overflow(iomsg))
452 iomsg->free_iov = iomsg->fast_iov;
453 ret = __import_iovec(ITER_DEST, msg.msg_iov, msg.msg_iovlen, UIO_FASTIOV,
454 &iomsg->free_iov, &iomsg->msg.msg_iter,
464 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
465 struct io_async_msghdr *iomsg)
467 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
468 struct compat_msghdr msg;
469 struct compat_iovec __user *uiov;
472 if (copy_from_user(&msg, sr->umsg_compat, sizeof(msg)))
475 ret = __get_compat_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
479 uiov = compat_ptr(msg.msg_iov);
480 if (req->flags & REQ_F_BUFFER_SELECT) {
483 iomsg->free_iov = NULL;
484 if (msg.msg_iovlen == 0) {
486 } else if (msg.msg_iovlen > 1) {
489 if (!access_ok(uiov, sizeof(*uiov)))
491 if (__get_user(clen, &uiov->iov_len))
498 if (req->flags & REQ_F_APOLL_MULTISHOT) {
499 iomsg->namelen = msg.msg_namelen;
500 iomsg->controllen = msg.msg_controllen;
501 if (io_recvmsg_multishot_overflow(iomsg))
505 iomsg->free_iov = iomsg->fast_iov;
506 ret = __import_iovec(ITER_DEST, (struct iovec __user *)uiov, msg.msg_iovlen,
507 UIO_FASTIOV, &iomsg->free_iov,
508 &iomsg->msg.msg_iter, true);
517 static int io_recvmsg_copy_hdr(struct io_kiocb *req,
518 struct io_async_msghdr *iomsg)
520 iomsg->msg.msg_name = &iomsg->addr;
523 if (req->ctx->compat)
524 return __io_compat_recvmsg_copy_hdr(req, iomsg);
527 return __io_recvmsg_copy_hdr(req, iomsg);
530 int io_recvmsg_prep_async(struct io_kiocb *req)
534 if (!io_msg_alloc_async_prep(req))
536 ret = io_recvmsg_copy_hdr(req, req->async_data);
538 req->flags |= REQ_F_NEED_CLEANUP;
542 #define RECVMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT)
544 int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
546 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
548 if (unlikely(sqe->file_index || sqe->addr2))
551 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
552 sr->len = READ_ONCE(sqe->len);
553 sr->flags = READ_ONCE(sqe->ioprio);
554 if (sr->flags & ~(RECVMSG_FLAGS))
556 sr->msg_flags = READ_ONCE(sqe->msg_flags);
557 if (sr->msg_flags & MSG_DONTWAIT)
558 req->flags |= REQ_F_NOWAIT;
559 if (sr->msg_flags & MSG_ERRQUEUE)
560 req->flags |= REQ_F_CLEAR_POLLIN;
561 if (sr->flags & IORING_RECV_MULTISHOT) {
562 if (!(req->flags & REQ_F_BUFFER_SELECT))
564 if (sr->msg_flags & MSG_WAITALL)
566 if (req->opcode == IORING_OP_RECV && sr->len)
568 req->flags |= REQ_F_APOLL_MULTISHOT;
570 * Store the buffer group for this multishot receive separately,
571 * as if we end up doing an io-wq based issue that selects a
572 * buffer, it has to be committed immediately and that will
573 * clear ->buf_list. This means we lose the link to the buffer
574 * list, and the eventual buffer put on completion then cannot
577 sr->buf_group = req->buf_index;
581 if (req->ctx->compat)
582 sr->msg_flags |= MSG_CMSG_COMPAT;
588 static inline void io_recv_prep_retry(struct io_kiocb *req)
590 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
593 sr->len = 0; /* get from the provided buffer */
594 req->buf_index = sr->buf_group;
598 * Finishes io_recv and io_recvmsg.
600 * Returns true if it is actually finished, or false if it should run
601 * again (for multishot).
603 static inline bool io_recv_finish(struct io_kiocb *req, int *ret,
604 unsigned int cflags, bool mshot_finished,
605 unsigned issue_flags)
607 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
608 io_req_set_res(req, *ret, cflags);
613 if (!mshot_finished) {
614 if (io_post_aux_cqe(req->ctx, req->cqe.user_data, *ret,
615 cflags | IORING_CQE_F_MORE, false)) {
616 io_recv_prep_retry(req);
620 * Otherwise stop multishot but use the current result.
621 * Probably will end up going into overflow, but this means
622 * we cannot trust the ordering anymore
626 io_req_set_res(req, *ret, cflags);
628 if (issue_flags & IO_URING_F_MULTISHOT)
629 *ret = IOU_STOP_MULTISHOT;
635 static int io_recvmsg_prep_multishot(struct io_async_msghdr *kmsg,
636 struct io_sr_msg *sr, void __user **buf,
639 unsigned long ubuf = (unsigned long) *buf;
642 hdr = sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
647 if (kmsg->controllen) {
648 unsigned long control = ubuf + hdr - kmsg->controllen;
650 kmsg->msg.msg_control_user = (void __user *) control;
651 kmsg->msg.msg_controllen = kmsg->controllen;
654 sr->buf = *buf; /* stash for later copy */
655 *buf = (void __user *) (ubuf + hdr);
656 kmsg->payloadlen = *len = *len - hdr;
660 struct io_recvmsg_multishot_hdr {
661 struct io_uring_recvmsg_out msg;
662 struct sockaddr_storage addr;
665 static int io_recvmsg_multishot(struct socket *sock, struct io_sr_msg *io,
666 struct io_async_msghdr *kmsg,
667 unsigned int flags, bool *finished)
671 struct io_recvmsg_multishot_hdr hdr;
674 kmsg->msg.msg_name = &hdr.addr;
675 kmsg->msg.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
676 kmsg->msg.msg_namelen = 0;
678 if (sock->file->f_flags & O_NONBLOCK)
679 flags |= MSG_DONTWAIT;
681 err = sock_recvmsg(sock, &kmsg->msg, flags);
682 *finished = err <= 0;
686 hdr.msg = (struct io_uring_recvmsg_out) {
687 .controllen = kmsg->controllen - kmsg->msg.msg_controllen,
688 .flags = kmsg->msg.msg_flags & ~MSG_CMSG_COMPAT
691 hdr.msg.payloadlen = err;
692 if (err > kmsg->payloadlen)
693 err = kmsg->payloadlen;
695 copy_len = sizeof(struct io_uring_recvmsg_out);
696 if (kmsg->msg.msg_namelen > kmsg->namelen)
697 copy_len += kmsg->namelen;
699 copy_len += kmsg->msg.msg_namelen;
702 * "fromlen shall refer to the value before truncation.."
705 hdr.msg.namelen = kmsg->msg.msg_namelen;
707 /* ensure that there is no gap between hdr and sockaddr_storage */
708 BUILD_BUG_ON(offsetof(struct io_recvmsg_multishot_hdr, addr) !=
709 sizeof(struct io_uring_recvmsg_out));
710 if (copy_to_user(io->buf, &hdr, copy_len)) {
715 return sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
716 kmsg->controllen + err;
719 int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
721 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
722 struct io_async_msghdr iomsg, *kmsg;
726 int ret, min_ret = 0;
727 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
728 bool mshot_finished = true;
730 sock = sock_from_file(req->file);
734 if (req_has_async_data(req)) {
735 kmsg = req->async_data;
737 ret = io_recvmsg_copy_hdr(req, &iomsg);
743 if (!(req->flags & REQ_F_POLLED) &&
744 (sr->flags & IORING_RECVSEND_POLL_FIRST))
745 return io_setup_async_msg(req, kmsg, issue_flags);
748 if (io_do_buffer_select(req)) {
750 size_t len = sr->len;
752 buf = io_buffer_select(req, &len, issue_flags);
756 if (req->flags & REQ_F_APOLL_MULTISHOT) {
757 ret = io_recvmsg_prep_multishot(kmsg, sr, &buf, &len);
759 io_kbuf_recycle(req, issue_flags);
764 kmsg->fast_iov[0].iov_base = buf;
765 kmsg->fast_iov[0].iov_len = len;
766 iov_iter_init(&kmsg->msg.msg_iter, ITER_DEST, kmsg->fast_iov, 1,
770 flags = sr->msg_flags;
772 flags |= MSG_DONTWAIT;
773 if (flags & MSG_WAITALL)
774 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
776 kmsg->msg.msg_get_inq = 1;
777 if (req->flags & REQ_F_APOLL_MULTISHOT)
778 ret = io_recvmsg_multishot(sock, sr, kmsg, flags,
781 ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg,
785 if (ret == -EAGAIN && force_nonblock) {
786 ret = io_setup_async_msg(req, kmsg, issue_flags);
787 if (ret == -EAGAIN && (issue_flags & IO_URING_F_MULTISHOT)) {
788 io_kbuf_recycle(req, issue_flags);
789 return IOU_ISSUE_SKIP_COMPLETE;
793 if (ret > 0 && io_net_retry(sock, flags)) {
795 req->flags |= REQ_F_PARTIAL_IO;
796 return io_setup_async_msg(req, kmsg, issue_flags);
798 if (ret == -ERESTARTSYS)
801 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
807 else if (sr->done_io)
810 io_kbuf_recycle(req, issue_flags);
812 cflags = io_put_kbuf(req, issue_flags);
813 if (kmsg->msg.msg_inq)
814 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
816 if (!io_recv_finish(req, &ret, cflags, mshot_finished, issue_flags))
817 goto retry_multishot;
819 if (mshot_finished) {
820 /* fast path, check for non-NULL to avoid function call */
822 kfree(kmsg->free_iov);
823 io_netmsg_recycle(req, issue_flags);
824 req->flags &= ~REQ_F_NEED_CLEANUP;
830 int io_recv(struct io_kiocb *req, unsigned int issue_flags)
832 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
838 int ret, min_ret = 0;
839 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
840 size_t len = sr->len;
842 if (!(req->flags & REQ_F_POLLED) &&
843 (sr->flags & IORING_RECVSEND_POLL_FIRST))
846 sock = sock_from_file(req->file);
851 if (io_do_buffer_select(req)) {
854 buf = io_buffer_select(req, &len, issue_flags);
860 ret = import_single_range(ITER_DEST, sr->buf, len, &iov, &msg.msg_iter);
866 msg.msg_control = NULL;
869 msg.msg_controllen = 0;
873 flags = sr->msg_flags;
875 flags |= MSG_DONTWAIT;
876 if (flags & MSG_WAITALL)
877 min_ret = iov_iter_count(&msg.msg_iter);
879 ret = sock_recvmsg(sock, &msg, flags);
881 if (ret == -EAGAIN && force_nonblock) {
882 if (issue_flags & IO_URING_F_MULTISHOT) {
883 io_kbuf_recycle(req, issue_flags);
884 return IOU_ISSUE_SKIP_COMPLETE;
889 if (ret > 0 && io_net_retry(sock, flags)) {
893 req->flags |= REQ_F_PARTIAL_IO;
896 if (ret == -ERESTARTSYS)
899 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
906 else if (sr->done_io)
909 io_kbuf_recycle(req, issue_flags);
911 cflags = io_put_kbuf(req, issue_flags);
913 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
915 if (!io_recv_finish(req, &ret, cflags, ret <= 0, issue_flags))
916 goto retry_multishot;
921 void io_send_zc_cleanup(struct io_kiocb *req)
923 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
924 struct io_async_msghdr *io;
926 if (req_has_async_data(req)) {
927 io = req->async_data;
928 /* might be ->fast_iov if *msg_copy_hdr failed */
929 if (io->free_iov != io->fast_iov)
933 io_notif_flush(zc->notif);
938 int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
940 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
941 struct io_ring_ctx *ctx = req->ctx;
942 struct io_kiocb *notif;
944 if (unlikely(READ_ONCE(sqe->__pad2[0]) || READ_ONCE(sqe->addr3)))
946 /* we don't support IOSQE_CQE_SKIP_SUCCESS just yet */
947 if (req->flags & REQ_F_CQE_SKIP)
950 zc->flags = READ_ONCE(sqe->ioprio);
951 if (zc->flags & ~(IORING_RECVSEND_POLL_FIRST |
952 IORING_RECVSEND_FIXED_BUF |
953 IORING_SEND_ZC_REPORT_USAGE))
955 notif = zc->notif = io_alloc_notif(ctx);
958 notif->cqe.user_data = req->cqe.user_data;
960 notif->cqe.flags = IORING_CQE_F_NOTIF;
961 req->flags |= REQ_F_NEED_CLEANUP;
962 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
963 unsigned idx = READ_ONCE(sqe->buf_index);
965 if (unlikely(idx >= ctx->nr_user_bufs))
967 idx = array_index_nospec(idx, ctx->nr_user_bufs);
968 req->imu = READ_ONCE(ctx->user_bufs[idx]);
969 io_req_set_rsrc_node(notif, ctx, 0);
971 if (zc->flags & IORING_SEND_ZC_REPORT_USAGE) {
972 io_notif_to_data(notif)->zc_report = true;
975 if (req->opcode == IORING_OP_SEND_ZC) {
976 if (READ_ONCE(sqe->__pad3[0]))
978 zc->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
979 zc->addr_len = READ_ONCE(sqe->addr_len);
981 if (unlikely(sqe->addr2 || sqe->file_index))
983 if (unlikely(zc->flags & IORING_RECVSEND_FIXED_BUF))
987 zc->buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
988 zc->len = READ_ONCE(sqe->len);
989 zc->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
990 if (zc->msg_flags & MSG_DONTWAIT)
991 req->flags |= REQ_F_NOWAIT;
996 if (req->ctx->compat)
997 zc->msg_flags |= MSG_CMSG_COMPAT;
1002 static int io_sg_from_iter_iovec(struct sock *sk, struct sk_buff *skb,
1003 struct iov_iter *from, size_t length)
1005 skb_zcopy_downgrade_managed(skb);
1006 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1009 static int io_sg_from_iter(struct sock *sk, struct sk_buff *skb,
1010 struct iov_iter *from, size_t length)
1012 struct skb_shared_info *shinfo = skb_shinfo(skb);
1013 int frag = shinfo->nr_frags;
1015 struct bvec_iter bi;
1017 unsigned long truesize = 0;
1020 shinfo->flags |= SKBFL_MANAGED_FRAG_REFS;
1021 else if (unlikely(!skb_zcopy_managed(skb)))
1022 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1024 bi.bi_size = min(from->count, length);
1025 bi.bi_bvec_done = from->iov_offset;
1028 while (bi.bi_size && frag < MAX_SKB_FRAGS) {
1029 struct bio_vec v = mp_bvec_iter_bvec(from->bvec, bi);
1032 truesize += PAGE_ALIGN(v.bv_len + v.bv_offset);
1033 __skb_fill_page_desc_noacc(shinfo, frag++, v.bv_page,
1034 v.bv_offset, v.bv_len);
1035 bvec_iter_advance_single(from->bvec, &bi, v.bv_len);
1040 shinfo->nr_frags = frag;
1041 from->bvec += bi.bi_idx;
1042 from->nr_segs -= bi.bi_idx;
1043 from->count -= copied;
1044 from->iov_offset = bi.bi_bvec_done;
1046 skb->data_len += copied;
1048 skb->truesize += truesize;
1050 if (sk && sk->sk_type == SOCK_STREAM) {
1051 sk_wmem_queued_add(sk, truesize);
1052 if (!skb_zcopy_pure(skb))
1053 sk_mem_charge(sk, truesize);
1055 refcount_add(truesize, &skb->sk->sk_wmem_alloc);
1060 int io_send_zc(struct io_kiocb *req, unsigned int issue_flags)
1062 struct sockaddr_storage __address;
1063 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
1066 struct socket *sock;
1068 int ret, min_ret = 0;
1070 sock = sock_from_file(req->file);
1071 if (unlikely(!sock))
1073 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1076 msg.msg_name = NULL;
1077 msg.msg_control = NULL;
1078 msg.msg_controllen = 0;
1079 msg.msg_namelen = 0;
1082 if (req_has_async_data(req)) {
1083 struct io_async_msghdr *io = req->async_data;
1085 msg.msg_name = &io->addr;
1087 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &__address);
1088 if (unlikely(ret < 0))
1090 msg.msg_name = (struct sockaddr *)&__address;
1092 msg.msg_namelen = zc->addr_len;
1095 if (!(req->flags & REQ_F_POLLED) &&
1096 (zc->flags & IORING_RECVSEND_POLL_FIRST))
1097 return io_setup_async_addr(req, &__address, issue_flags);
1099 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
1100 ret = io_import_fixed(ITER_SOURCE, &msg.msg_iter, req->imu,
1101 (u64)(uintptr_t)zc->buf, zc->len);
1104 msg.sg_from_iter = io_sg_from_iter;
1106 ret = import_single_range(ITER_SOURCE, zc->buf, zc->len, &iov,
1110 ret = io_notif_account_mem(zc->notif, zc->len);
1113 msg.sg_from_iter = io_sg_from_iter_iovec;
1116 msg_flags = zc->msg_flags | MSG_ZEROCOPY;
1117 if (issue_flags & IO_URING_F_NONBLOCK)
1118 msg_flags |= MSG_DONTWAIT;
1119 if (msg_flags & MSG_WAITALL)
1120 min_ret = iov_iter_count(&msg.msg_iter);
1122 msg.msg_flags = msg_flags;
1123 msg.msg_ubuf = &io_notif_to_data(zc->notif)->uarg;
1124 ret = sock_sendmsg(sock, &msg);
1126 if (unlikely(ret < min_ret)) {
1127 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1128 return io_setup_async_addr(req, &__address, issue_flags);
1130 if (ret > 0 && io_net_retry(sock, msg.msg_flags)) {
1134 req->flags |= REQ_F_PARTIAL_IO;
1135 return io_setup_async_addr(req, &__address, issue_flags);
1137 if (ret == -ERESTARTSYS)
1144 else if (zc->done_io)
1148 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1149 * flushing notif to io_send_zc_cleanup()
1151 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1152 io_notif_flush(zc->notif);
1153 req->flags &= ~REQ_F_NEED_CLEANUP;
1155 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1159 int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags)
1161 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1162 struct io_async_msghdr iomsg, *kmsg;
1163 struct socket *sock;
1165 int ret, min_ret = 0;
1167 sock = sock_from_file(req->file);
1168 if (unlikely(!sock))
1170 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1173 if (req_has_async_data(req)) {
1174 kmsg = req->async_data;
1176 ret = io_sendmsg_copy_hdr(req, &iomsg);
1182 if (!(req->flags & REQ_F_POLLED) &&
1183 (sr->flags & IORING_RECVSEND_POLL_FIRST))
1184 return io_setup_async_msg(req, kmsg, issue_flags);
1186 flags = sr->msg_flags | MSG_ZEROCOPY;
1187 if (issue_flags & IO_URING_F_NONBLOCK)
1188 flags |= MSG_DONTWAIT;
1189 if (flags & MSG_WAITALL)
1190 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
1192 kmsg->msg.msg_ubuf = &io_notif_to_data(sr->notif)->uarg;
1193 kmsg->msg.sg_from_iter = io_sg_from_iter_iovec;
1194 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
1196 if (unlikely(ret < min_ret)) {
1197 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1198 return io_setup_async_msg(req, kmsg, issue_flags);
1200 if (ret > 0 && io_net_retry(sock, flags)) {
1202 req->flags |= REQ_F_PARTIAL_IO;
1203 return io_setup_async_msg(req, kmsg, issue_flags);
1205 if (ret == -ERESTARTSYS)
1209 /* fast path, check for non-NULL to avoid function call */
1210 if (kmsg->free_iov) {
1211 kfree(kmsg->free_iov);
1212 kmsg->free_iov = NULL;
1215 io_netmsg_recycle(req, issue_flags);
1218 else if (sr->done_io)
1222 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1223 * flushing notif to io_send_zc_cleanup()
1225 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1226 io_notif_flush(sr->notif);
1227 req->flags &= ~REQ_F_NEED_CLEANUP;
1229 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1233 void io_sendrecv_fail(struct io_kiocb *req)
1235 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1237 if (req->flags & REQ_F_PARTIAL_IO)
1238 req->cqe.res = sr->done_io;
1240 if ((req->flags & REQ_F_NEED_CLEANUP) &&
1241 (req->opcode == IORING_OP_SEND_ZC || req->opcode == IORING_OP_SENDMSG_ZC))
1242 req->cqe.flags |= IORING_CQE_F_MORE;
1245 int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1247 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1250 if (sqe->len || sqe->buf_index)
1253 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1254 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
1255 accept->flags = READ_ONCE(sqe->accept_flags);
1256 accept->nofile = rlimit(RLIMIT_NOFILE);
1257 flags = READ_ONCE(sqe->ioprio);
1258 if (flags & ~IORING_ACCEPT_MULTISHOT)
1261 accept->file_slot = READ_ONCE(sqe->file_index);
1262 if (accept->file_slot) {
1263 if (accept->flags & SOCK_CLOEXEC)
1265 if (flags & IORING_ACCEPT_MULTISHOT &&
1266 accept->file_slot != IORING_FILE_INDEX_ALLOC)
1269 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1271 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
1272 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1273 if (flags & IORING_ACCEPT_MULTISHOT)
1274 req->flags |= REQ_F_APOLL_MULTISHOT;
1278 int io_accept(struct io_kiocb *req, unsigned int issue_flags)
1280 struct io_ring_ctx *ctx = req->ctx;
1281 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1282 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1283 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
1284 bool fixed = !!accept->file_slot;
1290 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
1291 if (unlikely(fd < 0))
1294 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
1299 ret = PTR_ERR(file);
1300 if (ret == -EAGAIN && force_nonblock) {
1302 * if it's multishot and polled, we don't need to
1303 * return EAGAIN to arm the poll infra since it
1304 * has already been done
1306 if (issue_flags & IO_URING_F_MULTISHOT)
1307 ret = IOU_ISSUE_SKIP_COMPLETE;
1310 if (ret == -ERESTARTSYS)
1313 } else if (!fixed) {
1314 fd_install(fd, file);
1317 ret = io_fixed_fd_install(req, issue_flags, file,
1321 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
1322 io_req_set_res(req, ret, 0);
1327 io_post_aux_cqe(ctx, req->cqe.user_data, ret, IORING_CQE_F_MORE, false))
1330 io_req_set_res(req, ret, 0);
1331 return (issue_flags & IO_URING_F_MULTISHOT) ? IOU_STOP_MULTISHOT : IOU_OK;
1334 int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1336 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1338 if (sqe->addr || sqe->rw_flags || sqe->buf_index)
1341 sock->domain = READ_ONCE(sqe->fd);
1342 sock->type = READ_ONCE(sqe->off);
1343 sock->protocol = READ_ONCE(sqe->len);
1344 sock->file_slot = READ_ONCE(sqe->file_index);
1345 sock->nofile = rlimit(RLIMIT_NOFILE);
1347 sock->flags = sock->type & ~SOCK_TYPE_MASK;
1348 if (sock->file_slot && (sock->flags & SOCK_CLOEXEC))
1350 if (sock->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1355 int io_socket(struct io_kiocb *req, unsigned int issue_flags)
1357 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1358 bool fixed = !!sock->file_slot;
1363 fd = __get_unused_fd_flags(sock->flags, sock->nofile);
1364 if (unlikely(fd < 0))
1367 file = __sys_socket_file(sock->domain, sock->type, sock->protocol);
1371 ret = PTR_ERR(file);
1372 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1374 if (ret == -ERESTARTSYS)
1377 } else if (!fixed) {
1378 fd_install(fd, file);
1381 ret = io_fixed_fd_install(req, issue_flags, file,
1384 io_req_set_res(req, ret, 0);
1388 int io_connect_prep_async(struct io_kiocb *req)
1390 struct io_async_connect *io = req->async_data;
1391 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1393 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
1396 int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1398 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1400 if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
1403 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1404 conn->addr_len = READ_ONCE(sqe->addr2);
1405 conn->in_progress = false;
1409 int io_connect(struct io_kiocb *req, unsigned int issue_flags)
1411 struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect);
1412 struct io_async_connect __io, *io;
1413 unsigned file_flags;
1415 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1417 if (connect->in_progress) {
1418 struct socket *socket;
1421 socket = sock_from_file(req->file);
1423 ret = sock_error(socket->sk);
1427 if (req_has_async_data(req)) {
1428 io = req->async_data;
1430 ret = move_addr_to_kernel(connect->addr,
1438 file_flags = force_nonblock ? O_NONBLOCK : 0;
1440 ret = __sys_connect_file(req->file, &io->address,
1441 connect->addr_len, file_flags);
1442 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
1443 if (ret == -EINPROGRESS) {
1444 connect->in_progress = true;
1446 if (req_has_async_data(req))
1448 if (io_alloc_async_data(req)) {
1452 memcpy(req->async_data, &__io, sizeof(__io));
1456 if (ret == -ERESTARTSYS)
1461 io_req_set_res(req, ret, 0);
1465 void io_netmsg_cache_free(struct io_cache_entry *entry)
1467 kfree(container_of(entry, struct io_async_msghdr, cache));