@@ -57,7 +57,7 @@ struct io_sr_msg {
5757 struct user_msghdr __user * umsg ;
5858 void __user * buf ;
5959 };
60- unsigned len ;
60+ int len ;
6161 unsigned done_io ;
6262 unsigned msg_flags ;
6363 unsigned nr_multishot_loops ;
@@ -389,6 +389,8 @@ static int io_sendmsg_prep_setup(struct io_kiocb *req, int is_msg)
389389 return ret ;
390390}
391391
392+ #define SENDMSG_FLAGS (IORING_RECVSEND_POLL_FIRST | IORING_RECVSEND_BUNDLE)
393+
392394int io_sendmsg_prep (struct io_kiocb * req , const struct io_uring_sqe * sqe )
393395{
394396 struct io_sr_msg * sr = io_kiocb_to_cmd (req , struct io_sr_msg );
@@ -407,11 +409,20 @@ int io_sendmsg_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
407409 sr -> umsg = u64_to_user_ptr (READ_ONCE (sqe -> addr ));
408410 sr -> len = READ_ONCE (sqe -> len );
409411 sr -> flags = READ_ONCE (sqe -> ioprio );
410- if (sr -> flags & ~IORING_RECVSEND_POLL_FIRST )
412+ if (sr -> flags & ~SENDMSG_FLAGS )
411413 return - EINVAL ;
412414 sr -> msg_flags = READ_ONCE (sqe -> msg_flags ) | MSG_NOSIGNAL ;
413415 if (sr -> msg_flags & MSG_DONTWAIT )
414416 req -> flags |= REQ_F_NOWAIT ;
417+ if (sr -> flags & IORING_RECVSEND_BUNDLE ) {
418+ if (req -> opcode == IORING_OP_SENDMSG )
419+ return - EINVAL ;
420+ if (!(req -> flags & REQ_F_BUFFER_SELECT ))
421+ return - EINVAL ;
422+ sr -> msg_flags |= MSG_WAITALL ;
423+ sr -> buf_group = req -> buf_index ;
424+ req -> buf_list = NULL ;
425+ }
415426
416427#ifdef CONFIG_COMPAT
417428 if (req -> ctx -> compat )
@@ -427,6 +438,79 @@ static void io_req_msg_cleanup(struct io_kiocb *req,
427438 io_netmsg_recycle (req , issue_flags );
428439}
429440
441+ /*
442+ * For bundle completions, we need to figure out how many segments we consumed.
443+ * A bundle could be using a single ITER_UBUF if that's all we mapped, or it
444+ * could be using an ITER_IOVEC. If the latter, then if we consumed all of
445+ * the segments, then it's a trivial questiont o answer. If we have residual
446+ * data in the iter, then loop the segments to figure out how much we
447+ * transferred.
448+ */
449+ static int io_bundle_nbufs (struct io_async_msghdr * kmsg , int ret )
450+ {
451+ struct iovec * iov ;
452+ int nbufs ;
453+
454+ /* no data is always zero segments, and a ubuf is always 1 segment */
455+ if (ret <= 0 )
456+ return 0 ;
457+ if (iter_is_ubuf (& kmsg -> msg .msg_iter ))
458+ return 1 ;
459+
460+ iov = kmsg -> free_iov ;
461+ if (!iov )
462+ iov = & kmsg -> fast_iov ;
463+
464+ /* if all data was transferred, it's basic pointer math */
465+ if (!iov_iter_count (& kmsg -> msg .msg_iter ))
466+ return iter_iov (& kmsg -> msg .msg_iter ) - iov ;
467+
468+ /* short transfer, count segments */
469+ nbufs = 0 ;
470+ do {
471+ int this_len = min_t (int , iov [nbufs ].iov_len , ret );
472+
473+ nbufs ++ ;
474+ ret -= this_len ;
475+ } while (ret );
476+
477+ return nbufs ;
478+ }
479+
480+ static inline bool io_send_finish (struct io_kiocb * req , int * ret ,
481+ struct io_async_msghdr * kmsg ,
482+ unsigned issue_flags )
483+ {
484+ struct io_sr_msg * sr = io_kiocb_to_cmd (req , struct io_sr_msg );
485+ bool bundle_finished = * ret <= 0 ;
486+ unsigned int cflags ;
487+
488+ if (!(sr -> flags & IORING_RECVSEND_BUNDLE )) {
489+ cflags = io_put_kbuf (req , issue_flags );
490+ goto finish ;
491+ }
492+
493+ cflags = io_put_kbufs (req , io_bundle_nbufs (kmsg , * ret ), issue_flags );
494+
495+ if (bundle_finished || req -> flags & REQ_F_BL_EMPTY )
496+ goto finish ;
497+
498+ /*
499+ * Fill CQE for this receive and see if we should keep trying to
500+ * receive from this socket.
501+ */
502+ if (io_req_post_cqe (req , * ret , cflags | IORING_CQE_F_MORE )) {
503+ io_mshot_prep_retry (req , kmsg );
504+ return false;
505+ }
506+
507+ /* Otherwise stop bundle and use the current result. */
508+ finish :
509+ io_req_set_res (req , * ret , cflags );
510+ * ret = IOU_OK ;
511+ return true;
512+ }
513+
430514int io_sendmsg (struct io_kiocb * req , unsigned int issue_flags )
431515{
432516 struct io_sr_msg * sr = io_kiocb_to_cmd (req , struct io_sr_msg );
@@ -482,7 +566,6 @@ int io_send(struct io_kiocb *req, unsigned int issue_flags)
482566 struct io_sr_msg * sr = io_kiocb_to_cmd (req , struct io_sr_msg );
483567 struct io_async_msghdr * kmsg = req -> async_data ;
484568 struct socket * sock ;
485- unsigned int cflags ;
486569 unsigned flags ;
487570 int min_ret = 0 ;
488571 int ret ;
@@ -495,21 +578,47 @@ int io_send(struct io_kiocb *req, unsigned int issue_flags)
495578 (sr -> flags & IORING_RECVSEND_POLL_FIRST ))
496579 return - EAGAIN ;
497580
581+ flags = sr -> msg_flags ;
582+ if (issue_flags & IO_URING_F_NONBLOCK )
583+ flags |= MSG_DONTWAIT ;
584+
585+ retry_bundle :
498586 if (io_do_buffer_select (req )) {
499- size_t len = sr -> len ;
500- void __user * buf ;
587+ struct buf_sel_arg arg = {
588+ .iovs = & kmsg -> fast_iov ,
589+ .max_len = min_not_zero (sr -> len , INT_MAX ),
590+ .nr_iovs = 1 ,
591+ .mode = KBUF_MODE_EXPAND ,
592+ };
593+
594+ if (kmsg -> free_iov ) {
595+ arg .nr_iovs = kmsg -> free_iov_nr ;
596+ arg .iovs = kmsg -> free_iov ;
597+ arg .mode |= KBUF_MODE_FREE ;
598+ }
501599
502- buf = io_buffer_select (req , & len , issue_flags );
503- if (unlikely (!buf ))
504- return - ENOBUFS ;
505- sr -> buf = buf ;
506- sr -> len = len ;
600+ if (!(sr -> flags & IORING_RECVSEND_BUNDLE ))
601+ arg .nr_iovs = 1 ;
602+
603+ ret = io_buffers_select (req , & arg , issue_flags );
604+ if (unlikely (ret < 0 ))
605+ return ret ;
606+
607+ sr -> len = arg .out_len ;
608+ iov_iter_init (& kmsg -> msg .msg_iter , ITER_SOURCE , arg .iovs , ret ,
609+ arg .out_len );
610+ if (arg .iovs != & kmsg -> fast_iov && arg .iovs != kmsg -> free_iov ) {
611+ kmsg -> free_iov_nr = ret ;
612+ kmsg -> free_iov = arg .iovs ;
613+ }
507614 }
508615
509- flags = sr -> msg_flags ;
510- if (issue_flags & IO_URING_F_NONBLOCK )
511- flags |= MSG_DONTWAIT ;
512- if (flags & MSG_WAITALL )
616+ /*
617+ * If MSG_WAITALL is set, or this is a bundle send, then we need
618+ * the full amount. If just bundle is set, if we do a short send
619+ * then we complete the bundle sequence rather than continue on.
620+ */
621+ if (flags & MSG_WAITALL || sr -> flags & IORING_RECVSEND_BUNDLE )
513622 min_ret = iov_iter_count (& kmsg -> msg .msg_iter );
514623
515624 flags &= ~MSG_INTERNAL_SENDMSG_FLAGS ;
@@ -534,10 +643,12 @@ int io_send(struct io_kiocb *req, unsigned int issue_flags)
534643 ret += sr -> done_io ;
535644 else if (sr -> done_io )
536645 ret = sr -> done_io ;
646+
647+ if (!io_send_finish (req , & ret , kmsg , issue_flags ))
648+ goto retry_bundle ;
649+
537650 io_req_msg_cleanup (req , issue_flags );
538- cflags = io_put_kbuf (req , issue_flags );
539- io_req_set_res (req , ret , cflags );
540- return IOU_OK ;
651+ return ret ;
541652}
542653
543654static int io_recvmsg_mshot_prep (struct io_kiocb * req ,
0 commit comments