public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] loop: respect REQ_NOWAIT for memory allocation
@ 2025-11-16  2:52 Chaitanya Kulkarni
  2025-11-16  2:52 ` [PATCH 2/2] zloop: " Chaitanya Kulkarni
  2025-11-16  3:50 ` [PATCH 1/2] loop: " Damien Le Moal
  0 siblings, 2 replies; 10+ messages in thread
From: Chaitanya Kulkarni @ 2025-11-16  2:52 UTC (permalink / raw)
  To: linux-block; +Cc: axboe, hch, kch, dlemoal, Chaitanya Kulkarni

loop advertises REQ_NOWAIT support via BLK_FEAT_NOWAIT (set by
default for all blk-mq devices), but violates REQ_NOWAIT semantics
by using GFP_NOIO allocations which can block.

BLK_FEAT_NOWAIT is advertised through this call chain:
  loop_add()
   blk_mq_alloc_disk(&lim, ...)
    __blk_mq_alloc_disk()
     blk_mq_alloc_queue()
      lim->features |= BLK_FEAT_NOWAIT <-- Set by default for blk-mq

However, the REQ_NOWAIT I/O path violates this contract. For io_uring
inline submissions, the call chain is:

  1. Userspace (io_uring setup):
   io_uring_setup(entries, &params)
   fd = io_uring_register(IORING_REGISTER_FILES, &zloop_fd, 1)

  2. Userspace (submit I/O with NOWAIT):
   sqe = io_uring_get_sqe(ring)
   io_uring_prep_write(sqe, zloop_fd, buf, len, offset)
   io_uring_submit(ring)  <-- inline submission

  3. io_uring core (inline path):
   io_submit_sqes()
    io_submit_sqe()
     io_queue_sqe()
      issue_flags = IO_URING_F_NONBLOCK <-- Sets inline/nowait mode
      io_issue_sqe()
       io_write()
        if (force_nonblock)  <-- true for inline submission
         kiocb->ki_flags |= IOCB_NOWAIT

  4. VFS layer:
   call_write_iter()
    blkdev_write_iter()
     -> propagates IOCB_NOWAIT to REQ_NOWAIT on bio
     __blkdev_direct_IO_simple() OR
     __blkdev_direct_IO()        OR
     __blkdev_direct_IO_async()

  5. Block layer:
   blk_mq_submit_bio()
    blk_mq_get_new_requests()
     __blk_mq_alloc_requests()
      blk_mq_rq_ctx_init()
       -> propagates REQ_NOWAIT to request->cmd_flags
    __blk_mq_try_issue_directly() OR blk_mq_sched_insert_request()
     blk_mq_run_hw_queue()
      __blk_mq_delay_run_hw_queue()
       __blk_mq_run_hw_queue()
        blk_mq_sched_dispatch_requests()

  6. Loop driver:
   loop_queue_rq()
    lo_rw_aio()
     kmalloc_array(..., GFP_NOIO) <-- BLOCKS (REQ_NOWAIT violation)
      -> Should use GFP_NOWAIT when rq->cmd_flags & REQ_NOWAIT

Fix by using GFP_NOWAIT when REQ_NOWAIT is set, and return -EAGAIN
instead of -ENOMEM when NOWAIT allocation fails, allowing io_uring
to retry the operation with blocking allowed.

Signed-off-by: Chaitanya Kulkarni <ckulkarnilinux@gmail.com>
---
 drivers/block/loop.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 13ce229d450c..bad61f41df34 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -350,6 +350,8 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 	unsigned int offset;
 	int nr_bvec = 0;
 	int ret;
+	bool nowait = rq->cmd_flags & REQ_NOWAIT;
+	gfp_t alloc_flags = nowait ? GFP_NOWAIT : GFP_NOIO;
 
 	rq_for_each_bvec(tmp, rq, rq_iter)
 		nr_bvec++;
@@ -357,9 +359,9 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd,
 	if (rq->bio != rq->biotail) {
 
 		bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
-				     GFP_NOIO);
+				     alloc_flags);
 		if (!bvec)
-			return -EIO;
+			return nowait ? -EAGAIN : -ENOMEM;
 		cmd->bvec = bvec;
 
 		/*
-- 
2.40.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2025-11-19  0:39 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-16  2:52 [PATCH 1/2] loop: respect REQ_NOWAIT for memory allocation Chaitanya Kulkarni
2025-11-16  2:52 ` [PATCH 2/2] zloop: " Chaitanya Kulkarni
2025-11-16  3:44   ` Damien Le Moal
2025-11-16  3:50 ` [PATCH 1/2] loop: " Damien Le Moal
2025-11-16  5:43   ` Chaitanya Kulkarni
2025-11-16  6:26     ` Damien Le Moal
2025-11-18  1:52       ` Chaitanya Kulkarni
2025-11-18  5:21     ` hch
2025-11-18 13:57       ` Jens Axboe
2025-11-19  0:39         ` Chaitanya Kulkarni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox