Commit b03732a9 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe
Browse files

loop: fix queue freeze vs limits lock order



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: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarMing Lei <ming.lei@redhat.com>
Reviewed-by: default avatarDamien Le Moal <dlemoal@kernel.org>
Reviewed-by: default avatarMartin K. Petersen <martin.petersen@oracle.com>
Reviewed-by: default avatarNilay Shroff <nilay@linux.ibm.com>
Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Link: https://lore.kernel.org/r/20250110054726.1499538-12-hch@lst.de


Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent b38c8be2
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -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);