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 */
66 /* used only for send zerocopy */
67 struct io_kiocb *notif;
70 int io_shutdown_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
72 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
74 if (unlikely(sqe->off || sqe->addr || sqe->rw_flags ||
75 sqe->buf_index || sqe->splice_fd_in))
78 shutdown->how = READ_ONCE(sqe->len);
82 int io_shutdown(struct io_kiocb *req, unsigned int issue_flags)
84 struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
88 if (issue_flags & IO_URING_F_NONBLOCK)
91 sock = sock_from_file(req->file);
95 ret = __sys_shutdown_sock(sock, shutdown->how);
96 io_req_set_res(req, ret, 0);
100 static bool io_net_retry(struct socket *sock, int flags)
102 if (!(flags & MSG_WAITALL))
104 return sock->type == SOCK_STREAM || sock->type == SOCK_SEQPACKET;
107 static void io_netmsg_recycle(struct io_kiocb *req, unsigned int issue_flags)
109 struct io_async_msghdr *hdr = req->async_data;
111 if (!req_has_async_data(req) || issue_flags & IO_URING_F_UNLOCKED)
114 /* Let normal cleanup path reap it if we fail adding to the cache */
115 if (io_alloc_cache_put(&req->ctx->netmsg_cache, &hdr->cache)) {
116 req->async_data = NULL;
117 req->flags &= ~REQ_F_ASYNC_DATA;
121 static struct io_async_msghdr *io_msg_alloc_async(struct io_kiocb *req,
122 unsigned int issue_flags)
124 struct io_ring_ctx *ctx = req->ctx;
125 struct io_cache_entry *entry;
126 struct io_async_msghdr *hdr;
128 if (!(issue_flags & IO_URING_F_UNLOCKED) &&
129 (entry = io_alloc_cache_get(&ctx->netmsg_cache)) != NULL) {
130 hdr = container_of(entry, struct io_async_msghdr, cache);
131 hdr->free_iov = NULL;
132 req->flags |= REQ_F_ASYNC_DATA;
133 req->async_data = hdr;
137 if (!io_alloc_async_data(req)) {
138 hdr = req->async_data;
139 hdr->free_iov = NULL;
145 static inline struct io_async_msghdr *io_msg_alloc_async_prep(struct io_kiocb *req)
147 /* ->prep_async is always called from the submission context */
148 return io_msg_alloc_async(req, 0);
151 static int io_setup_async_msg(struct io_kiocb *req,
152 struct io_async_msghdr *kmsg,
153 unsigned int issue_flags)
155 struct io_async_msghdr *async_msg;
157 if (req_has_async_data(req))
159 async_msg = io_msg_alloc_async(req, issue_flags);
161 kfree(kmsg->free_iov);
164 req->flags |= REQ_F_NEED_CLEANUP;
165 memcpy(async_msg, kmsg, sizeof(*kmsg));
166 if (async_msg->msg.msg_name)
167 async_msg->msg.msg_name = &async_msg->addr;
168 /* if were using fast_iov, set it to the new one */
169 if (!kmsg->free_iov) {
170 size_t fast_idx = kmsg->msg.msg_iter.iov - kmsg->fast_iov;
171 async_msg->msg.msg_iter.iov = &async_msg->fast_iov[fast_idx];
177 static int io_sendmsg_copy_hdr(struct io_kiocb *req,
178 struct io_async_msghdr *iomsg)
180 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
182 iomsg->msg.msg_name = &iomsg->addr;
183 iomsg->free_iov = iomsg->fast_iov;
184 return sendmsg_copy_msghdr(&iomsg->msg, sr->umsg, sr->msg_flags,
188 int io_send_prep_async(struct io_kiocb *req)
190 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
191 struct io_async_msghdr *io;
194 if (!zc->addr || req_has_async_data(req))
196 io = io_msg_alloc_async_prep(req);
199 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &io->addr);
203 static int io_setup_async_addr(struct io_kiocb *req,
204 struct sockaddr_storage *addr_storage,
205 unsigned int issue_flags)
207 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
208 struct io_async_msghdr *io;
210 if (!sr->addr || req_has_async_data(req))
212 io = io_msg_alloc_async(req, issue_flags);
215 memcpy(&io->addr, addr_storage, sizeof(io->addr));
219 int io_sendmsg_prep_async(struct io_kiocb *req)
223 if (!io_msg_alloc_async_prep(req))
225 ret = io_sendmsg_copy_hdr(req, req->async_data);
227 req->flags |= REQ_F_NEED_CLEANUP;
231 void io_sendmsg_recvmsg_cleanup(struct io_kiocb *req)
233 struct io_async_msghdr *io = req->async_data;
238 int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
240 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
242 if (req->opcode == IORING_OP_SEND) {
243 if (READ_ONCE(sqe->__pad3[0]))
245 sr->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
246 sr->addr_len = READ_ONCE(sqe->addr_len);
247 } else if (sqe->addr2 || sqe->file_index) {
251 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
252 sr->len = READ_ONCE(sqe->len);
253 sr->flags = READ_ONCE(sqe->ioprio);
254 if (sr->flags & ~IORING_RECVSEND_POLL_FIRST)
256 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
257 if (sr->msg_flags & MSG_DONTWAIT)
258 req->flags |= REQ_F_NOWAIT;
261 if (req->ctx->compat)
262 sr->msg_flags |= MSG_CMSG_COMPAT;
268 int io_sendmsg(struct io_kiocb *req, unsigned int issue_flags)
270 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
271 struct io_async_msghdr iomsg, *kmsg;
277 sock = sock_from_file(req->file);
281 if (req_has_async_data(req)) {
282 kmsg = req->async_data;
284 ret = io_sendmsg_copy_hdr(req, &iomsg);
290 if (!(req->flags & REQ_F_POLLED) &&
291 (sr->flags & IORING_RECVSEND_POLL_FIRST))
292 return io_setup_async_msg(req, kmsg, issue_flags);
294 flags = sr->msg_flags;
295 if (issue_flags & IO_URING_F_NONBLOCK)
296 flags |= MSG_DONTWAIT;
297 if (flags & MSG_WAITALL)
298 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
300 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
303 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
304 return io_setup_async_msg(req, kmsg, issue_flags);
305 if (ret > 0 && io_net_retry(sock, flags)) {
307 req->flags |= REQ_F_PARTIAL_IO;
308 return io_setup_async_msg(req, kmsg, issue_flags);
310 if (ret == -ERESTARTSYS)
314 /* fast path, check for non-NULL to avoid function call */
316 kfree(kmsg->free_iov);
317 req->flags &= ~REQ_F_NEED_CLEANUP;
318 io_netmsg_recycle(req, issue_flags);
321 else if (sr->done_io)
323 io_req_set_res(req, ret, 0);
327 int io_send(struct io_kiocb *req, unsigned int issue_flags)
329 struct sockaddr_storage __address;
330 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
339 msg.msg_control = NULL;
340 msg.msg_controllen = 0;
345 if (req_has_async_data(req)) {
346 struct io_async_msghdr *io = req->async_data;
348 msg.msg_name = &io->addr;
350 ret = move_addr_to_kernel(sr->addr, sr->addr_len, &__address);
351 if (unlikely(ret < 0))
353 msg.msg_name = (struct sockaddr *)&__address;
355 msg.msg_namelen = sr->addr_len;
358 if (!(req->flags & REQ_F_POLLED) &&
359 (sr->flags & IORING_RECVSEND_POLL_FIRST))
360 return io_setup_async_addr(req, &__address, issue_flags);
362 sock = sock_from_file(req->file);
366 ret = import_single_range(WRITE, sr->buf, sr->len, &iov, &msg.msg_iter);
370 flags = sr->msg_flags;
371 if (issue_flags & IO_URING_F_NONBLOCK)
372 flags |= MSG_DONTWAIT;
373 if (flags & MSG_WAITALL)
374 min_ret = iov_iter_count(&msg.msg_iter);
376 msg.msg_flags = flags;
377 ret = sock_sendmsg(sock, &msg);
379 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
380 return io_setup_async_addr(req, &__address, issue_flags);
382 if (ret > 0 && io_net_retry(sock, flags)) {
386 req->flags |= REQ_F_PARTIAL_IO;
387 return io_setup_async_addr(req, &__address, issue_flags);
389 if (ret == -ERESTARTSYS)
395 else if (sr->done_io)
397 io_req_set_res(req, ret, 0);
401 static bool io_recvmsg_multishot_overflow(struct io_async_msghdr *iomsg)
405 if (iomsg->namelen < 0)
407 if (check_add_overflow((int)sizeof(struct io_uring_recvmsg_out),
408 iomsg->namelen, &hdr))
410 if (check_add_overflow(hdr, (int)iomsg->controllen, &hdr))
416 static int __io_recvmsg_copy_hdr(struct io_kiocb *req,
417 struct io_async_msghdr *iomsg)
419 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
420 struct user_msghdr msg;
423 if (copy_from_user(&msg, sr->umsg, sizeof(*sr->umsg)))
426 ret = __copy_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
430 if (req->flags & REQ_F_BUFFER_SELECT) {
431 if (msg.msg_iovlen == 0) {
432 sr->len = iomsg->fast_iov[0].iov_len = 0;
433 iomsg->fast_iov[0].iov_base = NULL;
434 iomsg->free_iov = NULL;
435 } else if (msg.msg_iovlen > 1) {
438 if (copy_from_user(iomsg->fast_iov, msg.msg_iov, sizeof(*msg.msg_iov)))
440 sr->len = iomsg->fast_iov[0].iov_len;
441 iomsg->free_iov = NULL;
444 if (req->flags & REQ_F_APOLL_MULTISHOT) {
445 iomsg->namelen = msg.msg_namelen;
446 iomsg->controllen = msg.msg_controllen;
447 if (io_recvmsg_multishot_overflow(iomsg))
451 iomsg->free_iov = iomsg->fast_iov;
452 ret = __import_iovec(READ, msg.msg_iov, msg.msg_iovlen, UIO_FASTIOV,
453 &iomsg->free_iov, &iomsg->msg.msg_iter,
463 static int __io_compat_recvmsg_copy_hdr(struct io_kiocb *req,
464 struct io_async_msghdr *iomsg)
466 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
467 struct compat_msghdr msg;
468 struct compat_iovec __user *uiov;
471 if (copy_from_user(&msg, sr->umsg_compat, sizeof(msg)))
474 ret = __get_compat_msghdr(&iomsg->msg, &msg, &iomsg->uaddr);
478 uiov = compat_ptr(msg.msg_iov);
479 if (req->flags & REQ_F_BUFFER_SELECT) {
482 if (msg.msg_iovlen == 0) {
484 } else if (msg.msg_iovlen > 1) {
487 if (!access_ok(uiov, sizeof(*uiov)))
489 if (__get_user(clen, &uiov->iov_len))
496 if (req->flags & REQ_F_APOLL_MULTISHOT) {
497 iomsg->namelen = msg.msg_namelen;
498 iomsg->controllen = msg.msg_controllen;
499 if (io_recvmsg_multishot_overflow(iomsg))
503 iomsg->free_iov = iomsg->fast_iov;
504 ret = __import_iovec(READ, (struct iovec __user *)uiov, msg.msg_iovlen,
505 UIO_FASTIOV, &iomsg->free_iov,
506 &iomsg->msg.msg_iter, true);
515 static int io_recvmsg_copy_hdr(struct io_kiocb *req,
516 struct io_async_msghdr *iomsg)
518 iomsg->msg.msg_name = &iomsg->addr;
521 if (req->ctx->compat)
522 return __io_compat_recvmsg_copy_hdr(req, iomsg);
525 return __io_recvmsg_copy_hdr(req, iomsg);
528 int io_recvmsg_prep_async(struct io_kiocb *req)
532 if (!io_msg_alloc_async_prep(req))
534 ret = io_recvmsg_copy_hdr(req, req->async_data);
536 req->flags |= REQ_F_NEED_CLEANUP;
540 #define RECVMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECV_MULTISHOT)
542 int io_recvmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
544 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
546 if (unlikely(sqe->file_index || sqe->addr2))
549 sr->umsg = u64_to_user_ptr(READ_ONCE(sqe->addr));
550 sr->len = READ_ONCE(sqe->len);
551 sr->flags = READ_ONCE(sqe->ioprio);
552 if (sr->flags & ~(RECVMSG_FLAGS))
554 sr->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
555 if (sr->msg_flags & MSG_DONTWAIT)
556 req->flags |= REQ_F_NOWAIT;
557 if (sr->msg_flags & MSG_ERRQUEUE)
558 req->flags |= REQ_F_CLEAR_POLLIN;
559 if (sr->flags & IORING_RECV_MULTISHOT) {
560 if (!(req->flags & REQ_F_BUFFER_SELECT))
562 if (sr->msg_flags & MSG_WAITALL)
564 if (req->opcode == IORING_OP_RECV && sr->len)
566 req->flags |= REQ_F_APOLL_MULTISHOT;
570 if (req->ctx->compat)
571 sr->msg_flags |= MSG_CMSG_COMPAT;
577 static inline void io_recv_prep_retry(struct io_kiocb *req)
579 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
582 sr->len = 0; /* get from the provided buffer */
586 * Finishes io_recv and io_recvmsg.
588 * Returns true if it is actually finished, or false if it should run
589 * again (for multishot).
591 static inline bool io_recv_finish(struct io_kiocb *req, int *ret,
592 unsigned int cflags, bool mshot_finished,
593 unsigned issue_flags)
595 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
596 io_req_set_res(req, *ret, cflags);
601 if (!mshot_finished) {
602 if (io_post_aux_cqe(req->ctx, req->cqe.user_data, *ret,
603 cflags | IORING_CQE_F_MORE, false)) {
604 io_recv_prep_retry(req);
608 * Otherwise stop multishot but use the current result.
609 * Probably will end up going into overflow, but this means
610 * we cannot trust the ordering anymore
614 io_req_set_res(req, *ret, cflags);
616 if (issue_flags & IO_URING_F_MULTISHOT)
617 *ret = IOU_STOP_MULTISHOT;
623 static int io_recvmsg_prep_multishot(struct io_async_msghdr *kmsg,
624 struct io_sr_msg *sr, void __user **buf,
627 unsigned long ubuf = (unsigned long) *buf;
630 hdr = sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
635 if (kmsg->controllen) {
636 unsigned long control = ubuf + hdr - kmsg->controllen;
638 kmsg->msg.msg_control_user = (void __user *) control;
639 kmsg->msg.msg_controllen = kmsg->controllen;
642 sr->buf = *buf; /* stash for later copy */
643 *buf = (void __user *) (ubuf + hdr);
644 kmsg->payloadlen = *len = *len - hdr;
648 struct io_recvmsg_multishot_hdr {
649 struct io_uring_recvmsg_out msg;
650 struct sockaddr_storage addr;
653 static int io_recvmsg_multishot(struct socket *sock, struct io_sr_msg *io,
654 struct io_async_msghdr *kmsg,
655 unsigned int flags, bool *finished)
659 struct io_recvmsg_multishot_hdr hdr;
662 kmsg->msg.msg_name = &hdr.addr;
663 kmsg->msg.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
664 kmsg->msg.msg_namelen = 0;
666 if (sock->file->f_flags & O_NONBLOCK)
667 flags |= MSG_DONTWAIT;
669 err = sock_recvmsg(sock, &kmsg->msg, flags);
670 *finished = err <= 0;
674 hdr.msg = (struct io_uring_recvmsg_out) {
675 .controllen = kmsg->controllen - kmsg->msg.msg_controllen,
676 .flags = kmsg->msg.msg_flags & ~MSG_CMSG_COMPAT
679 hdr.msg.payloadlen = err;
680 if (err > kmsg->payloadlen)
681 err = kmsg->payloadlen;
683 copy_len = sizeof(struct io_uring_recvmsg_out);
684 if (kmsg->msg.msg_namelen > kmsg->namelen)
685 copy_len += kmsg->namelen;
687 copy_len += kmsg->msg.msg_namelen;
690 * "fromlen shall refer to the value before truncation.."
693 hdr.msg.namelen = kmsg->msg.msg_namelen;
695 /* ensure that there is no gap between hdr and sockaddr_storage */
696 BUILD_BUG_ON(offsetof(struct io_recvmsg_multishot_hdr, addr) !=
697 sizeof(struct io_uring_recvmsg_out));
698 if (copy_to_user(io->buf, &hdr, copy_len)) {
703 return sizeof(struct io_uring_recvmsg_out) + kmsg->namelen +
704 kmsg->controllen + err;
707 int io_recvmsg(struct io_kiocb *req, unsigned int issue_flags)
709 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
710 struct io_async_msghdr iomsg, *kmsg;
714 int ret, min_ret = 0;
715 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
716 bool mshot_finished = true;
718 sock = sock_from_file(req->file);
722 if (req_has_async_data(req)) {
723 kmsg = req->async_data;
725 ret = io_recvmsg_copy_hdr(req, &iomsg);
731 if (!(req->flags & REQ_F_POLLED) &&
732 (sr->flags & IORING_RECVSEND_POLL_FIRST))
733 return io_setup_async_msg(req, kmsg, issue_flags);
736 if (io_do_buffer_select(req)) {
738 size_t len = sr->len;
740 buf = io_buffer_select(req, &len, issue_flags);
744 if (req->flags & REQ_F_APOLL_MULTISHOT) {
745 ret = io_recvmsg_prep_multishot(kmsg, sr, &buf, &len);
747 io_kbuf_recycle(req, issue_flags);
752 kmsg->fast_iov[0].iov_base = buf;
753 kmsg->fast_iov[0].iov_len = len;
754 iov_iter_init(&kmsg->msg.msg_iter, READ, kmsg->fast_iov, 1,
758 flags = sr->msg_flags;
760 flags |= MSG_DONTWAIT;
761 if (flags & MSG_WAITALL)
762 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
764 kmsg->msg.msg_get_inq = 1;
765 if (req->flags & REQ_F_APOLL_MULTISHOT)
766 ret = io_recvmsg_multishot(sock, sr, kmsg, flags,
769 ret = __sys_recvmsg_sock(sock, &kmsg->msg, sr->umsg,
773 if (ret == -EAGAIN && force_nonblock) {
774 ret = io_setup_async_msg(req, kmsg, issue_flags);
775 if (ret == -EAGAIN && (issue_flags & IO_URING_F_MULTISHOT)) {
776 io_kbuf_recycle(req, issue_flags);
777 return IOU_ISSUE_SKIP_COMPLETE;
781 if (ret > 0 && io_net_retry(sock, flags)) {
783 req->flags |= REQ_F_PARTIAL_IO;
784 return io_setup_async_msg(req, kmsg, issue_flags);
786 if (ret == -ERESTARTSYS)
789 } else if ((flags & MSG_WAITALL) && (kmsg->msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
795 else if (sr->done_io)
798 io_kbuf_recycle(req, issue_flags);
800 cflags = io_put_kbuf(req, issue_flags);
801 if (kmsg->msg.msg_inq)
802 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
804 if (!io_recv_finish(req, &ret, cflags, mshot_finished, issue_flags))
805 goto retry_multishot;
807 if (mshot_finished) {
808 io_netmsg_recycle(req, issue_flags);
809 /* fast path, check for non-NULL to avoid function call */
811 kfree(kmsg->free_iov);
812 req->flags &= ~REQ_F_NEED_CLEANUP;
818 int io_recv(struct io_kiocb *req, unsigned int issue_flags)
820 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
826 int ret, min_ret = 0;
827 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
828 size_t len = sr->len;
830 if (!(req->flags & REQ_F_POLLED) &&
831 (sr->flags & IORING_RECVSEND_POLL_FIRST))
834 sock = sock_from_file(req->file);
839 if (io_do_buffer_select(req)) {
842 buf = io_buffer_select(req, &len, issue_flags);
848 ret = import_single_range(READ, sr->buf, len, &iov, &msg.msg_iter);
854 msg.msg_control = NULL;
857 msg.msg_controllen = 0;
861 flags = sr->msg_flags;
863 flags |= MSG_DONTWAIT;
864 if (flags & MSG_WAITALL)
865 min_ret = iov_iter_count(&msg.msg_iter);
867 ret = sock_recvmsg(sock, &msg, flags);
869 if (ret == -EAGAIN && force_nonblock) {
870 if (issue_flags & IO_URING_F_MULTISHOT) {
871 io_kbuf_recycle(req, issue_flags);
872 return IOU_ISSUE_SKIP_COMPLETE;
877 if (ret > 0 && io_net_retry(sock, flags)) {
881 req->flags |= REQ_F_PARTIAL_IO;
884 if (ret == -ERESTARTSYS)
887 } else if ((flags & MSG_WAITALL) && (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC))) {
894 else if (sr->done_io)
897 io_kbuf_recycle(req, issue_flags);
899 cflags = io_put_kbuf(req, issue_flags);
901 cflags |= IORING_CQE_F_SOCK_NONEMPTY;
903 if (!io_recv_finish(req, &ret, cflags, ret <= 0, issue_flags))
904 goto retry_multishot;
909 void io_send_zc_cleanup(struct io_kiocb *req)
911 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
912 struct io_async_msghdr *io;
914 if (req_has_async_data(req)) {
915 io = req->async_data;
916 /* might be ->fast_iov if *msg_copy_hdr failed */
917 if (io->free_iov != io->fast_iov)
921 io_notif_flush(zc->notif);
926 int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
928 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
929 struct io_ring_ctx *ctx = req->ctx;
930 struct io_kiocb *notif;
932 if (unlikely(READ_ONCE(sqe->__pad2[0]) || READ_ONCE(sqe->addr3)))
934 /* we don't support IOSQE_CQE_SKIP_SUCCESS just yet */
935 if (req->flags & REQ_F_CQE_SKIP)
938 zc->flags = READ_ONCE(sqe->ioprio);
939 if (zc->flags & ~(IORING_RECVSEND_POLL_FIRST |
940 IORING_RECVSEND_FIXED_BUF))
942 notif = zc->notif = io_alloc_notif(ctx);
945 notif->cqe.user_data = req->cqe.user_data;
947 notif->cqe.flags = IORING_CQE_F_NOTIF;
948 req->flags |= REQ_F_NEED_CLEANUP;
949 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
950 unsigned idx = READ_ONCE(sqe->buf_index);
952 if (unlikely(idx >= ctx->nr_user_bufs))
954 idx = array_index_nospec(idx, ctx->nr_user_bufs);
955 req->imu = READ_ONCE(ctx->user_bufs[idx]);
956 io_req_set_rsrc_node(notif, ctx, 0);
959 if (req->opcode == IORING_OP_SEND_ZC) {
960 if (READ_ONCE(sqe->__pad3[0]))
962 zc->addr = u64_to_user_ptr(READ_ONCE(sqe->addr2));
963 zc->addr_len = READ_ONCE(sqe->addr_len);
965 if (unlikely(sqe->addr2 || sqe->file_index))
967 if (unlikely(zc->flags & IORING_RECVSEND_FIXED_BUF))
971 zc->buf = u64_to_user_ptr(READ_ONCE(sqe->addr));
972 zc->len = READ_ONCE(sqe->len);
973 zc->msg_flags = READ_ONCE(sqe->msg_flags) | MSG_NOSIGNAL;
974 if (zc->msg_flags & MSG_DONTWAIT)
975 req->flags |= REQ_F_NOWAIT;
980 if (req->ctx->compat)
981 zc->msg_flags |= MSG_CMSG_COMPAT;
986 static int io_sg_from_iter_iovec(struct sock *sk, struct sk_buff *skb,
987 struct iov_iter *from, size_t length)
989 skb_zcopy_downgrade_managed(skb);
990 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
993 static int io_sg_from_iter(struct sock *sk, struct sk_buff *skb,
994 struct iov_iter *from, size_t length)
996 struct skb_shared_info *shinfo = skb_shinfo(skb);
997 int frag = shinfo->nr_frags;
1001 unsigned long truesize = 0;
1004 shinfo->flags |= SKBFL_MANAGED_FRAG_REFS;
1005 else if (unlikely(!skb_zcopy_managed(skb)))
1006 return __zerocopy_sg_from_iter(NULL, sk, skb, from, length);
1008 bi.bi_size = min(from->count, length);
1009 bi.bi_bvec_done = from->iov_offset;
1012 while (bi.bi_size && frag < MAX_SKB_FRAGS) {
1013 struct bio_vec v = mp_bvec_iter_bvec(from->bvec, bi);
1016 truesize += PAGE_ALIGN(v.bv_len + v.bv_offset);
1017 __skb_fill_page_desc_noacc(shinfo, frag++, v.bv_page,
1018 v.bv_offset, v.bv_len);
1019 bvec_iter_advance_single(from->bvec, &bi, v.bv_len);
1024 shinfo->nr_frags = frag;
1025 from->bvec += bi.bi_idx;
1026 from->nr_segs -= bi.bi_idx;
1027 from->count -= copied;
1028 from->iov_offset = bi.bi_bvec_done;
1030 skb->data_len += copied;
1032 skb->truesize += truesize;
1034 if (sk && sk->sk_type == SOCK_STREAM) {
1035 sk_wmem_queued_add(sk, truesize);
1036 if (!skb_zcopy_pure(skb))
1037 sk_mem_charge(sk, truesize);
1039 refcount_add(truesize, &skb->sk->sk_wmem_alloc);
1044 int io_send_zc(struct io_kiocb *req, unsigned int issue_flags)
1046 struct sockaddr_storage __address;
1047 struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
1050 struct socket *sock;
1052 int ret, min_ret = 0;
1054 sock = sock_from_file(req->file);
1055 if (unlikely(!sock))
1057 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1060 msg.msg_name = NULL;
1061 msg.msg_control = NULL;
1062 msg.msg_controllen = 0;
1063 msg.msg_namelen = 0;
1066 if (req_has_async_data(req)) {
1067 struct io_async_msghdr *io = req->async_data;
1069 msg.msg_name = &io->addr;
1071 ret = move_addr_to_kernel(zc->addr, zc->addr_len, &__address);
1072 if (unlikely(ret < 0))
1074 msg.msg_name = (struct sockaddr *)&__address;
1076 msg.msg_namelen = zc->addr_len;
1079 if (!(req->flags & REQ_F_POLLED) &&
1080 (zc->flags & IORING_RECVSEND_POLL_FIRST))
1081 return io_setup_async_addr(req, &__address, issue_flags);
1083 if (zc->flags & IORING_RECVSEND_FIXED_BUF) {
1084 ret = io_import_fixed(WRITE, &msg.msg_iter, req->imu,
1085 (u64)(uintptr_t)zc->buf, zc->len);
1088 msg.sg_from_iter = io_sg_from_iter;
1090 ret = import_single_range(WRITE, zc->buf, zc->len, &iov,
1094 ret = io_notif_account_mem(zc->notif, zc->len);
1097 msg.sg_from_iter = io_sg_from_iter_iovec;
1100 msg_flags = zc->msg_flags | MSG_ZEROCOPY;
1101 if (issue_flags & IO_URING_F_NONBLOCK)
1102 msg_flags |= MSG_DONTWAIT;
1103 if (msg_flags & MSG_WAITALL)
1104 min_ret = iov_iter_count(&msg.msg_iter);
1106 msg.msg_flags = msg_flags;
1107 msg.msg_ubuf = &io_notif_to_data(zc->notif)->uarg;
1108 ret = sock_sendmsg(sock, &msg);
1110 if (unlikely(ret < min_ret)) {
1111 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1112 return io_setup_async_addr(req, &__address, issue_flags);
1114 if (ret > 0 && io_net_retry(sock, msg.msg_flags)) {
1118 req->flags |= REQ_F_PARTIAL_IO;
1119 return io_setup_async_addr(req, &__address, issue_flags);
1121 if (ret == -ERESTARTSYS)
1128 else if (zc->done_io)
1132 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1133 * flushing notif to io_send_zc_cleanup()
1135 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1136 io_notif_flush(zc->notif);
1137 req->flags &= ~REQ_F_NEED_CLEANUP;
1139 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1143 int io_sendmsg_zc(struct io_kiocb *req, unsigned int issue_flags)
1145 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1146 struct io_async_msghdr iomsg, *kmsg;
1147 struct socket *sock;
1149 int ret, min_ret = 0;
1151 sock = sock_from_file(req->file);
1152 if (unlikely(!sock))
1154 if (!test_bit(SOCK_SUPPORT_ZC, &sock->flags))
1157 if (req_has_async_data(req)) {
1158 kmsg = req->async_data;
1160 ret = io_sendmsg_copy_hdr(req, &iomsg);
1166 if (!(req->flags & REQ_F_POLLED) &&
1167 (sr->flags & IORING_RECVSEND_POLL_FIRST))
1168 return io_setup_async_msg(req, kmsg, issue_flags);
1170 flags = sr->msg_flags | MSG_ZEROCOPY;
1171 if (issue_flags & IO_URING_F_NONBLOCK)
1172 flags |= MSG_DONTWAIT;
1173 if (flags & MSG_WAITALL)
1174 min_ret = iov_iter_count(&kmsg->msg.msg_iter);
1176 kmsg->msg.msg_ubuf = &io_notif_to_data(sr->notif)->uarg;
1177 kmsg->msg.sg_from_iter = io_sg_from_iter_iovec;
1178 ret = __sys_sendmsg_sock(sock, &kmsg->msg, flags);
1180 if (unlikely(ret < min_ret)) {
1181 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1182 return io_setup_async_msg(req, kmsg, issue_flags);
1184 if (ret > 0 && io_net_retry(sock, flags)) {
1186 req->flags |= REQ_F_PARTIAL_IO;
1187 return io_setup_async_msg(req, kmsg, issue_flags);
1189 if (ret == -ERESTARTSYS)
1193 /* fast path, check for non-NULL to avoid function call */
1194 if (kmsg->free_iov) {
1195 kfree(kmsg->free_iov);
1196 kmsg->free_iov = NULL;
1199 io_netmsg_recycle(req, issue_flags);
1202 else if (sr->done_io)
1206 * If we're in io-wq we can't rely on tw ordering guarantees, defer
1207 * flushing notif to io_send_zc_cleanup()
1209 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
1210 io_notif_flush(sr->notif);
1211 req->flags &= ~REQ_F_NEED_CLEANUP;
1213 io_req_set_res(req, ret, IORING_CQE_F_MORE);
1217 void io_sendrecv_fail(struct io_kiocb *req)
1219 struct io_sr_msg *sr = io_kiocb_to_cmd(req, struct io_sr_msg);
1221 if (req->flags & REQ_F_PARTIAL_IO)
1222 req->cqe.res = sr->done_io;
1224 if ((req->flags & REQ_F_NEED_CLEANUP) &&
1225 (req->opcode == IORING_OP_SEND_ZC || req->opcode == IORING_OP_SENDMSG_ZC))
1226 req->cqe.flags |= IORING_CQE_F_MORE;
1229 int io_accept_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1231 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1234 if (sqe->len || sqe->buf_index)
1237 accept->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1238 accept->addr_len = u64_to_user_ptr(READ_ONCE(sqe->addr2));
1239 accept->flags = READ_ONCE(sqe->accept_flags);
1240 accept->nofile = rlimit(RLIMIT_NOFILE);
1241 flags = READ_ONCE(sqe->ioprio);
1242 if (flags & ~IORING_ACCEPT_MULTISHOT)
1245 accept->file_slot = READ_ONCE(sqe->file_index);
1246 if (accept->file_slot) {
1247 if (accept->flags & SOCK_CLOEXEC)
1249 if (flags & IORING_ACCEPT_MULTISHOT &&
1250 accept->file_slot != IORING_FILE_INDEX_ALLOC)
1253 if (accept->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1255 if (SOCK_NONBLOCK != O_NONBLOCK && (accept->flags & SOCK_NONBLOCK))
1256 accept->flags = (accept->flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1257 if (flags & IORING_ACCEPT_MULTISHOT)
1258 req->flags |= REQ_F_APOLL_MULTISHOT;
1262 int io_accept(struct io_kiocb *req, unsigned int issue_flags)
1264 struct io_ring_ctx *ctx = req->ctx;
1265 struct io_accept *accept = io_kiocb_to_cmd(req, struct io_accept);
1266 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1267 unsigned int file_flags = force_nonblock ? O_NONBLOCK : 0;
1268 bool fixed = !!accept->file_slot;
1274 fd = __get_unused_fd_flags(accept->flags, accept->nofile);
1275 if (unlikely(fd < 0))
1278 file = do_accept(req->file, file_flags, accept->addr, accept->addr_len,
1283 ret = PTR_ERR(file);
1284 if (ret == -EAGAIN && force_nonblock) {
1286 * if it's multishot and polled, we don't need to
1287 * return EAGAIN to arm the poll infra since it
1288 * has already been done
1290 if (issue_flags & IO_URING_F_MULTISHOT)
1291 ret = IOU_ISSUE_SKIP_COMPLETE;
1294 if (ret == -ERESTARTSYS)
1297 } else if (!fixed) {
1298 fd_install(fd, file);
1301 ret = io_fixed_fd_install(req, issue_flags, file,
1305 if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {
1306 io_req_set_res(req, ret, 0);
1311 io_post_aux_cqe(ctx, req->cqe.user_data, ret, IORING_CQE_F_MORE, false))
1314 io_req_set_res(req, ret, 0);
1315 return (issue_flags & IO_URING_F_MULTISHOT) ? IOU_STOP_MULTISHOT : IOU_OK;
1318 int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1320 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1322 if (sqe->addr || sqe->rw_flags || sqe->buf_index)
1325 sock->domain = READ_ONCE(sqe->fd);
1326 sock->type = READ_ONCE(sqe->off);
1327 sock->protocol = READ_ONCE(sqe->len);
1328 sock->file_slot = READ_ONCE(sqe->file_index);
1329 sock->nofile = rlimit(RLIMIT_NOFILE);
1331 sock->flags = sock->type & ~SOCK_TYPE_MASK;
1332 if (sock->file_slot && (sock->flags & SOCK_CLOEXEC))
1334 if (sock->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1339 int io_socket(struct io_kiocb *req, unsigned int issue_flags)
1341 struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1342 bool fixed = !!sock->file_slot;
1347 fd = __get_unused_fd_flags(sock->flags, sock->nofile);
1348 if (unlikely(fd < 0))
1351 file = __sys_socket_file(sock->domain, sock->type, sock->protocol);
1355 ret = PTR_ERR(file);
1356 if (ret == -EAGAIN && (issue_flags & IO_URING_F_NONBLOCK))
1358 if (ret == -ERESTARTSYS)
1361 } else if (!fixed) {
1362 fd_install(fd, file);
1365 ret = io_fixed_fd_install(req, issue_flags, file,
1368 io_req_set_res(req, ret, 0);
1372 int io_connect_prep_async(struct io_kiocb *req)
1374 struct io_async_connect *io = req->async_data;
1375 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1377 return move_addr_to_kernel(conn->addr, conn->addr_len, &io->address);
1380 int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
1382 struct io_connect *conn = io_kiocb_to_cmd(req, struct io_connect);
1384 if (sqe->len || sqe->buf_index || sqe->rw_flags || sqe->splice_fd_in)
1387 conn->addr = u64_to_user_ptr(READ_ONCE(sqe->addr));
1388 conn->addr_len = READ_ONCE(sqe->addr2);
1389 conn->in_progress = false;
1393 int io_connect(struct io_kiocb *req, unsigned int issue_flags)
1395 struct io_connect *connect = io_kiocb_to_cmd(req, struct io_connect);
1396 struct io_async_connect __io, *io;
1397 unsigned file_flags;
1399 bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
1401 if (connect->in_progress) {
1402 struct socket *socket;
1405 socket = sock_from_file(req->file);
1407 ret = sock_error(socket->sk);
1411 if (req_has_async_data(req)) {
1412 io = req->async_data;
1414 ret = move_addr_to_kernel(connect->addr,
1422 file_flags = force_nonblock ? O_NONBLOCK : 0;
1424 ret = __sys_connect_file(req->file, &io->address,
1425 connect->addr_len, file_flags);
1426 if ((ret == -EAGAIN || ret == -EINPROGRESS) && force_nonblock) {
1427 if (ret == -EINPROGRESS) {
1428 connect->in_progress = true;
1430 if (req_has_async_data(req))
1432 if (io_alloc_async_data(req)) {
1436 memcpy(req->async_data, &__io, sizeof(__io));
1440 if (ret == -ERESTARTSYS)
1445 io_req_set_res(req, ret, 0);
1449 void io_netmsg_cache_free(struct io_cache_entry *entry)
1451 kfree(container_of(entry, struct io_async_msghdr, cache));