loop: fix queue freeze vs limits lock order
authorChristoph Hellwig <hch@lst.de>
Fri, 10 Jan 2025 05:47:19 +0000 (06:47 +0100)
committerJens Axboe <axboe@kernel.dk>
Fri, 10 Jan 2025 14:29:24 +0000 (07:29 -0700)
Match the locking order used by the core block code by only freezing
the queue after taking the limits lock using the
queue_limits_commit_update_frozen helper and document the callers that
do not freeze the queue at all.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: Nilay Shroff <nilay@linux.ibm.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20250110054726.1499538-12-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/loop.c

index 560d6d5879d6f2344b2c5c44220114ba9832bb80..15e486baa223a5f0a40758007d53fb30b4a65c13 100644 (file)
@@ -311,6 +311,13 @@ static void loop_clear_limits(struct loop_device *lo, int mode)
                lim.discard_granularity = 0;
        }
 
+       /*
+        * XXX: this updates the queue limits without freezing the queue, which
+        * is against the locking protocol and dangerous.  But we can't just
+        * freeze the queue as we're inside the ->queue_rq method here.  So this
+        * should move out into a workqueue unless we get the file operations to
+        * advertise if they support specific fallocate operations.
+        */
        queue_limits_commit_update(lo->lo_queue, &lim);
 }
 
@@ -1091,6 +1098,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
 
        lim = queue_limits_start_update(lo->lo_queue);
        loop_update_limits(lo, &lim, config->block_size);
+       /* No need to freeze the queue as the device isn't bound yet. */
        error = queue_limits_commit_update(lo->lo_queue, &lim);
        if (error)
                goto out_unlock;
@@ -1151,7 +1159,12 @@ static void __loop_clr_fd(struct loop_device *lo)
        lo->lo_sizelimit = 0;
        memset(lo->lo_file_name, 0, LO_NAME_SIZE);
 
-       /* reset the block size to the default */
+       /*
+        * Reset the block size to the default.
+        *
+        * No queue freezing needed because this is called from the final
+        * ->release call only, so there can't be any outstanding I/O.
+        */
        lim = queue_limits_start_update(lo->lo_queue);
        lim.logical_block_size = SECTOR_SIZE;
        lim.physical_block_size = SECTOR_SIZE;
@@ -1471,9 +1484,10 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)
        sync_blockdev(lo->lo_device);
        invalidate_bdev(lo->lo_device);
 
-       blk_mq_freeze_queue(lo->lo_queue);
        lim = queue_limits_start_update(lo->lo_queue);
        loop_update_limits(lo, &lim, arg);
+
+       blk_mq_freeze_queue(lo->lo_queue);
        err = queue_limits_commit_update(lo->lo_queue, &lim);
        loop_update_dio(lo);
        blk_mq_unfreeze_queue(lo->lo_queue);