io_uring treats -EOPNOTSUPP from a nonblocking read or write like -EAGAIN and retries the request from io-wq, both when ->read_iter() or ->write_iter() returns it and when it is passed to ->ki_complete(), because some filesystems and block devices return -EOPNOTSUPP for IOCB_NOWAIT requests that they can serve when allowed to block. That retry cannot succeed for a request that uses a dma-buf registered buffer. dma-buf I/O has no buffered fallback, so wherever the I/O path gives up with -EOPNOTSUPP, it gives up again from blocking context, even where the first -EOPNOTSUPP was caused by IOCB_NOWAIT. For example, register a dma-buf against a raw NVMe namespace opened with O_DIRECT, then clear O_DIRECT with F_SETFL. blkdev_read_iter() refuses the buffered read of a dma-buf iterator with -EOPNOTSUPP. A write fails in blkdev_write_iter() because IOCB_NOWAIT is set without IOCB_DIRECT, and then fails again in blkdev_buffered_write() because of the dma-buf iterator. With the ext4 dma-buf support under review, a direct read that reaches a hole inside the file fails the same way, and so does a write to a file with data journalling, which ext4 turns into a buffered write. The retry costs a second issue. For a read that covers mapped blocks before the hole, those blocks are read from the device a second time. With RWF_NOWAIT, the -EOPNOTSUPP is turned into -EAGAIN and returned to userspace, which asks the application to retry something that cannot succeed. Leave -EOPNOTSUPP alone for REQ_F_DMABUF requests. The flag is still set after io_detach_dmabuf_map(), so io_complete_rw() can check it. The one IOCB_NOWAIT-specific -EOPNOTSUPP that a blocking retry can fix comes from submit_bio_noacct() for a device without NOWAIT support. dma-buf I/O does not reach it today: only NVMe implements ->init_dma_buf_io_ctx, and NVMe namespaces and multipath devices support NOWAIT. This needs another look if stacking drivers gain dma-buf support. Tested on v7.4-rc2 with QEMU NVMe and udmabuf buffers, using the raw device case above: with RWF_NOWAIT, reads and writes now fail with -EOPNOTSUPP instead of -EAGAIN, and the write is no longer queued to io-wq a second time. A buffered write from user memory to the same device, the case the -EOPNOTSUPP retry exists for, is still retried from io-wq and succeeds. Also tested with the ext4 dma-buf series, on QEMU NVMe and on a KIOXIA BG5: a hole read with RWF_NOWAIT gets -EOPNOTSUPP, and a read of [mapped | hole] issues one device read instead of two. Fixes: 62e4bf5b25d4 ("io_uring/rsrc: add dmabuf backed registered buffers") Signed-off-by: Yun Wang --- io_uring/rw.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/io_uring/rw.c b/io_uring/rw.c index 7917d1536c47..0ab7cf3d503a 100644 --- a/io_uring/rw.c +++ b/io_uring/rw.c @@ -545,7 +545,9 @@ static void __io_complete_rw_common(struct io_kiocb *req, long res) { if (res == req->cqe.res) return; - if ((res == -EOPNOTSUPP || res == -EAGAIN) && io_rw_should_reissue(req)) { + if ((res == -EAGAIN || + (res == -EOPNOTSUPP && !(req->flags & REQ_F_DMABUF))) && + io_rw_should_reissue(req)) { req->flags |= REQ_F_REISSUE | REQ_F_BL_NO_RECYCLE; } else { req_set_fail(req); @@ -944,7 +946,8 @@ static int __io_read(struct io_kiocb *req, struct io_br_sel *sel, * issue, even though they should be returning -EAGAIN. To be safe, * retry from blocking context for either. */ - if (ret == -EOPNOTSUPP && force_nonblock) + if (ret == -EOPNOTSUPP && force_nonblock && + !(req->flags & REQ_F_DMABUF)) ret = -EAGAIN; if (ret == -EAGAIN) { @@ -1172,7 +1175,8 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags) * Raw bdev writes will return -EOPNOTSUPP for IOCB_NOWAIT. Just * retry them without IOCB_NOWAIT. */ - if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT)) + if (ret2 == -EOPNOTSUPP && (kiocb->ki_flags & IOCB_NOWAIT) && + !(req->flags & REQ_F_DMABUF)) ret2 = -EAGAIN; /* no retry on NONBLOCK nor RWF_NOWAIT */ if (ret2 == -EAGAIN && (req->flags & REQ_F_NOWAIT)) -- 2.56.1