7878#include <linux/fs_struct.h>
7979#include <linux/splice.h>
8080#include <linux/task_work.h>
81+ #include <linux/pagemap.h>
8182
8283#define CREATE_TRACE_POINTS
8384#include <trace/events/io_uring.h>
@@ -503,6 +504,8 @@ struct io_async_rw {
503504 struct iovec * iov ;
504505 ssize_t nr_segs ;
505506 ssize_t size ;
507+ struct wait_page_queue wpq ;
508+ struct callback_head task_work ;
506509};
507510
508511struct io_async_ctx {
@@ -2750,6 +2753,126 @@ static int io_read_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe,
27502753 return 0 ;
27512754}
27522755
2756+ static void __io_async_buf_error (struct io_kiocb * req , int error )
2757+ {
2758+ struct io_ring_ctx * ctx = req -> ctx ;
2759+
2760+ spin_lock_irq (& ctx -> completion_lock );
2761+ io_cqring_fill_event (req , error );
2762+ io_commit_cqring (ctx );
2763+ spin_unlock_irq (& ctx -> completion_lock );
2764+
2765+ io_cqring_ev_posted (ctx );
2766+ req_set_fail_links (req );
2767+ io_double_put_req (req );
2768+ }
2769+
2770+ static void io_async_buf_cancel (struct callback_head * cb )
2771+ {
2772+ struct io_async_rw * rw ;
2773+ struct io_kiocb * req ;
2774+
2775+ rw = container_of (cb , struct io_async_rw , task_work );
2776+ req = rw -> wpq .wait .private ;
2777+ __io_async_buf_error (req , - ECANCELED );
2778+ }
2779+
2780+ static void io_async_buf_retry (struct callback_head * cb )
2781+ {
2782+ struct io_async_rw * rw ;
2783+ struct io_ring_ctx * ctx ;
2784+ struct io_kiocb * req ;
2785+
2786+ rw = container_of (cb , struct io_async_rw , task_work );
2787+ req = rw -> wpq .wait .private ;
2788+ ctx = req -> ctx ;
2789+
2790+ __set_current_state (TASK_RUNNING );
2791+ if (!io_sq_thread_acquire_mm (ctx , req )) {
2792+ mutex_lock (& ctx -> uring_lock );
2793+ __io_queue_sqe (req , NULL );
2794+ mutex_unlock (& ctx -> uring_lock );
2795+ } else {
2796+ __io_async_buf_error (req , - EFAULT );
2797+ }
2798+ }
2799+
2800+ static int io_async_buf_func (struct wait_queue_entry * wait , unsigned mode ,
2801+ int sync , void * arg )
2802+ {
2803+ struct wait_page_queue * wpq ;
2804+ struct io_kiocb * req = wait -> private ;
2805+ struct io_async_rw * rw = & req -> io -> rw ;
2806+ struct wait_page_key * key = arg ;
2807+ struct task_struct * tsk ;
2808+ int ret ;
2809+
2810+ wpq = container_of (wait , struct wait_page_queue , wait );
2811+
2812+ ret = wake_page_match (wpq , key );
2813+ if (ret != 1 )
2814+ return ret ;
2815+
2816+ list_del_init (& wait -> entry );
2817+
2818+ init_task_work (& rw -> task_work , io_async_buf_retry );
2819+ /* submit ref gets dropped, acquire a new one */
2820+ refcount_inc (& req -> refs );
2821+ tsk = req -> task ;
2822+ ret = task_work_add (tsk , & rw -> task_work , true);
2823+ if (unlikely (ret )) {
2824+ /* queue just for cancelation */
2825+ init_task_work (& rw -> task_work , io_async_buf_cancel );
2826+ tsk = io_wq_get_task (req -> ctx -> io_wq );
2827+ task_work_add (tsk , & rw -> task_work , true);
2828+ }
2829+ wake_up_process (tsk );
2830+ return 1 ;
2831+ }
2832+
2833+ static bool io_rw_should_retry (struct io_kiocb * req )
2834+ {
2835+ struct kiocb * kiocb = & req -> rw .kiocb ;
2836+ int ret ;
2837+
2838+ /* never retry for NOWAIT, we just complete with -EAGAIN */
2839+ if (req -> flags & REQ_F_NOWAIT )
2840+ return false;
2841+
2842+ /* already tried, or we're doing O_DIRECT */
2843+ if (kiocb -> ki_flags & (IOCB_DIRECT | IOCB_WAITQ ))
2844+ return false;
2845+ /*
2846+ * just use poll if we can, and don't attempt if the fs doesn't
2847+ * support callback based unlocks
2848+ */
2849+ if (file_can_poll (req -> file ) || !(req -> file -> f_mode & FMODE_BUF_RASYNC ))
2850+ return false;
2851+
2852+ /*
2853+ * If request type doesn't require req->io to defer in general,
2854+ * we need to allocate it here
2855+ */
2856+ if (!req -> io && __io_alloc_async_ctx (req ))
2857+ return false;
2858+
2859+ ret = kiocb_wait_page_queue_init (kiocb , & req -> io -> rw .wpq ,
2860+ io_async_buf_func , req );
2861+ if (!ret ) {
2862+ io_get_req_task (req );
2863+ return true;
2864+ }
2865+
2866+ return false;
2867+ }
2868+
2869+ static int io_iter_do_read (struct io_kiocb * req , struct iov_iter * iter )
2870+ {
2871+ if (req -> file -> f_op -> read_iter )
2872+ return call_read_iter (req -> file , & req -> rw .kiocb , iter );
2873+ return loop_rw_iter (READ , req -> file , & req -> rw .kiocb , iter );
2874+ }
2875+
27532876static int io_read (struct io_kiocb * req , bool force_nonblock )
27542877{
27552878 struct iovec inline_vecs [UIO_FASTIOV ], * iovec = inline_vecs ;
@@ -2784,10 +2907,7 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)
27842907 unsigned long nr_segs = iter .nr_segs ;
27852908 ssize_t ret2 = 0 ;
27862909
2787- if (req -> file -> f_op -> read_iter )
2788- ret2 = call_read_iter (req -> file , kiocb , & iter );
2789- else
2790- ret2 = loop_rw_iter (READ , req -> file , kiocb , & iter );
2910+ ret2 = io_iter_do_read (req , & iter );
27912911
27922912 /* Catch -EAGAIN return for forced non-blocking submission */
27932913 if (!force_nonblock || (ret2 != - EAGAIN && ret2 != - EIO )) {
@@ -2804,6 +2924,17 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)
28042924 if (!(req -> flags & REQ_F_NOWAIT ) &&
28052925 !file_can_poll (req -> file ))
28062926 req -> flags |= REQ_F_MUST_PUNT ;
2927+ /* if we can retry, do so with the callbacks armed */
2928+ if (io_rw_should_retry (req )) {
2929+ ret2 = io_iter_do_read (req , & iter );
2930+ if (ret2 == - EIOCBQUEUED ) {
2931+ goto out_free ;
2932+ } else if (ret2 != - EAGAIN ) {
2933+ kiocb_done (kiocb , ret2 );
2934+ goto out_free ;
2935+ }
2936+ }
2937+ kiocb -> ki_flags &= ~IOCB_WAITQ ;
28072938 return - EAGAIN ;
28082939 }
28092940 }
0 commit comments