Skip to contentSkip to conversation

loop: fix stale dma_alignment when direct I/O is silently dropped

PATCH In review Tao Cui 2 participants 3 messages Nov 24

Subsystems (2)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 loop: fix stale dma_alignment when direct I/O is silently dropped

From: Tao Cui <[email protected]>

LOOP_SET_BLOCK_SIZE and LOOP_CONFIGURE can silently drop LO_FLAGS_DIRECT_IO when the new block size is smaller than the backing device's DIO alignment or the offset is unaligned. The drop happens in loop_update_dio(), which runs after queue_limits_commit_update(), so the committed limits keep the dma_alignment computed from the pre-drop flag state, i.e. the backing file's alignment. With an f2fs backing file, setting the block size to 512 then leaves every O_DIRECT request requiring 4K-aligned buffers.

loop_set_status() has the same window in a different shape: an offset change can drop the flag but nothing recomputes the queue limits.

Judge direct I/O usability against the block size the limits are being computed for rather than the committed one, and let loop_set_dma_limit() fall back to SECTOR_SIZE - 1 when the flag is about to be dropped, which is the order loop_set_dio() already uses. Recompute the DMA alignment after the flag flip in loop_set_status().

Fixes 6c8dec275ccc35e8f ("loop: set dma_alignment from the backing file for direct I/O")
---
 drivers/block/loop.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
drivers/block/loop.c +23 -8
index 194f51d9dab6..4798c4d47a0e 100644
@@ -179,11 +179,11 @@ static loff_t lo_calculate_size(struct loop_device *lo, struct file *file)
  * of backing device, and the logical block size of loop is bigger than that of
  * the backing device.
  */
-static bool lo_can_use_dio(struct loop_device *lo)
+static bool lo_can_use_dio(struct loop_device *lo, unsigned int bsize)
 {
 	if (!(lo->lo_backing_file->f_mode & FMODE_CAN_ODIRECT))
 		return false;
-	if (queue_logical_block_size(lo->lo_queue) < lo->lo_min_dio_size)
+	if (bsize < lo->lo_min_dio_size)
 		return false;
 	if (lo->lo_offset & (lo->lo_min_dio_size - 1))
 		return false;
@@ -204,7 +204,8 @@ static inline void loop_update_dio(struct loop_device *lo)
 	WARN_ON_ONCE(lo->lo_state == Lo_bound &&
 		     lo->lo_queue->mq_freeze_depth == 0);
 
-	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
+	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) &&
+	    !lo_can_use_dio(lo, queue_logical_block_size(lo->lo_queue)))
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
 }
 
@@ -840,13 +841,17 @@ static unsigned int loop_default_blocksize(struct loop_device *lo)
 	return SECTOR_SIZE;
 }
 
-static void loop_set_dma_limit(struct loop_device *lo, struct queue_limits *lim)
+static void loop_set_dma_limit(struct loop_device *lo, struct queue_limits *lim,
+		unsigned int bsize)
 {
 	/*
 	 * Direct I/O forwards the user pages to the backing file unchanged, so
 	 * track the backing's DMA alignment requirement as the mode is toggled.
+	 * Judge usability against @bsize rather than the committed block size,
+	 * so limits computed for a pending change match the flag state that
+	 * loop_update_dio() settles on right after the commit.
 	 */
-	if (lo->lo_flags & LO_FLAGS_DIRECT_IO)
+	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && lo_can_use_dio(lo, bsize))
 		lim->dma_alignment = max_t(unsigned int, lo->lo_dio_mem_align,
 					   SECTOR_SIZE - 1);
 	else
@@ -874,7 +879,7 @@ static void loop_update_limits(struct loop_device *lo, struct queue_limits *lim,
 	lim->logical_block_size = bsize;
 	lim->physical_block_size = bsize;
 	lim->io_min = bsize;
-	loop_set_dma_limit(lo, lim);
+	loop_set_dma_limit(lo, lim, bsize);
 	lim->features &= ~(BLK_FEAT_WRITE_CACHE | BLK_FEAT_ROTATIONAL);
 	if (file->f_op->fsync && !(lo->lo_flags & LO_FLAGS_READ_ONLY))
 		lim->features |= BLK_FEAT_WRITE_CACHE;
@@ -1134,6 +1139,7 @@ loop_set_status(struct loop_device *lo, blk_mode_t mode,
 		struct block_device *bdev, const struct loop_info64 *info)
 {
 	struct block_device *claim_bdev = bdev_whole(bdev);
+	struct queue_limits lim;
 	int err;
 	bool partscan = false;
 	bool size_changed = false;
@@ -1189,6 +1195,15 @@ loop_set_status(struct loop_device *lo, blk_mode_t mode,
 	/* update the direct I/O flag if lo_offset changed */
 	loop_update_dio(lo);
 
+	/*
+	 * The offset change may have silently dropped LO_FLAGS_DIRECT_IO;
+	 * refresh the DMA alignment so the queue limits match the new flag
+	 * state.
+	 */
+	lim = queue_limits_start_update(lo->lo_queue);
+	loop_set_dma_limit(lo, &lim, queue_logical_block_size(lo->lo_queue));
+	queue_limits_commit_update(lo->lo_queue, &lim);
+
 	blk_mq_unfreeze_queue(lo->lo_queue, memflags);
 	if (partscan)
 		clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
@@ -1361,7 +1376,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 		return 0;
 
 	if (use_dio) {
-		if (!lo_can_use_dio(lo))
+		if (!lo_can_use_dio(lo, queue_logical_block_size(lo->lo_queue)))
 			return -EINVAL;
 		/* flush dirty pages before starting to use direct I/O */
 		vfs_fsync(lo->lo_backing_file, 0);
@@ -1373,7 +1388,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
 	else
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
-	loop_set_dma_limit(lo, &lim);
+	loop_set_dma_limit(lo, &lim, queue_logical_block_size(lo->lo_queue));
 	queue_limits_commit_update(lo->lo_queue, &lim);
 	blk_mq_unfreeze_queue(lo->lo_queue, memflags);
 	return 0;
Signature · 1 line
2.53.0
author
#1 lore raw

Hello,

Quoted 11 lines · Tao Cui

From: Tao Cui <[email protected]>

LOOP_SET_BLOCK_SIZE and LOOP_CONFIGURE can silently drop LO_FLAGS_DIRECT_IO when the new block size is smaller than the backing device's DIO alignment or the offset is unaligned. The drop happens in loop_update_dio(), which runs after queue_limits_commit_update(), so the committed limits keep the dma_alignment computed from the pre-drop flag state, i.e. the backing file's alignment. With an f2fs backing file, setting the block size to 512 then leaves every O_DIRECT request requiring 4K-aligned buffers.

Quoted 9 lines

loop_set_status() has the same window in a different shape: an offset change can drop the flag but nothing recomputes the queue limits.

Judge direct I/O usability against the block size the limits are being computed for rather than the committed one, and let loop_set_dma_limit() fall back to SECTOR_SIZE - 1 when the flag is about to be dropped, which is the order loop_set_dio() already uses. Recompute the DMA alignment after the flag flip in loop_set_status().

I noticed the sashiko report [1]. The lock order in loop_set_status() is indeed different from 06a2ff603f1f, but I think the reported deadlock cannot be reached through this code. The queue is frozen and fully drained before queue_limits_start_update() takes q->limits_lock, and nothing between start_update() and commit_update() blocks, so the new code never waits for anything while holding the lock.

I am more worried about the suggested reorder: with start_update() before blk_mq_freeze_queue(), loop_set_status() would hold limits_lock while draining, and an in-flight write-zeroes request on an unsupported backing file would block in loop_clear_limits() on that lock while holding a queue usage counter, so the freeze would never complete. The snapshot taken before the drain could also overwrite a concurrent loop_clear_limits() commit.

The cycle in the report does exist, but it predates this patch: it needs a thread that keeps limits_lock across a drain, for example queue_attr_store() via queue_limits_commit_update_frozen(), racing an unsupported fallocate in flight. The "loop: defer the queue limits clear to a workqueue" series removes it at the root.

So I would like to keep the patch as it stands, but happy to discuss further.

Thanks,
Tao.

[1] https://sashiko.dev/#/patchset/[email protected]

Quoted 106 lines
Fixes 6c8dec275ccc35e8f ("loop: set dma_alignment from the backing file for direct I/O")
---
 drivers/block/loop.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
drivers/block/loop.c +23 -8
index 194f51d9dab6..4798c4d47a0e 100644
@@ -179,11 +179,11 @@ static loff_t lo_calculate_size(struct loop_device *lo, struct file *file)
  * of backing device, and the logical block size of loop is bigger than that of
  * the backing device.
  */
-static bool lo_can_use_dio(struct loop_device *lo)
+static bool lo_can_use_dio(struct loop_device *lo, unsigned int bsize)
 {
 	if (!(lo->lo_backing_file->f_mode & FMODE_CAN_ODIRECT))
 		return false;
-	if (queue_logical_block_size(lo->lo_queue) < lo->lo_min_dio_size)
+	if (bsize < lo->lo_min_dio_size)
 		return false;
 	if (lo->lo_offset & (lo->lo_min_dio_size - 1))
 		return false;
@@ -204,7 +204,8 @@ static inline void loop_update_dio(struct loop_device *lo)
 	WARN_ON_ONCE(lo->lo_state == Lo_bound &&
 		     lo->lo_queue->mq_freeze_depth == 0);
 
-	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && !lo_can_use_dio(lo))
+	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) &&
+	    !lo_can_use_dio(lo, queue_logical_block_size(lo->lo_queue)))
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
 }
 
@@ -840,13 +841,17 @@ static unsigned int loop_default_blocksize(struct loop_device *lo)
 	return SECTOR_SIZE;
 }
 
-static void loop_set_dma_limit(struct loop_device *lo, struct queue_limits *lim)
+static void loop_set_dma_limit(struct loop_device *lo, struct queue_limits *lim,
+		unsigned int bsize)
 {
 	/*
 	 * Direct I/O forwards the user pages to the backing file unchanged, so
 	 * track the backing's DMA alignment requirement as the mode is toggled.
+	 * Judge usability against @bsize rather than the committed block size,
+	 * so limits computed for a pending change match the flag state that
+	 * loop_update_dio() settles on right after the commit.
 	 */
-	if (lo->lo_flags & LO_FLAGS_DIRECT_IO)
+	if ((lo->lo_flags & LO_FLAGS_DIRECT_IO) && lo_can_use_dio(lo, bsize))
 		lim->dma_alignment = max_t(unsigned int, lo->lo_dio_mem_align,
 					   SECTOR_SIZE - 1);
 	else
@@ -874,7 +879,7 @@ static void loop_update_limits(struct loop_device *lo, struct queue_limits *lim,
 	lim->logical_block_size = bsize;
 	lim->physical_block_size = bsize;
 	lim->io_min = bsize;
-	loop_set_dma_limit(lo, lim);
+	loop_set_dma_limit(lo, lim, bsize);
 	lim->features &= ~(BLK_FEAT_WRITE_CACHE | BLK_FEAT_ROTATIONAL);
 	if (file->f_op->fsync && !(lo->lo_flags & LO_FLAGS_READ_ONLY))
 		lim->features |= BLK_FEAT_WRITE_CACHE;
@@ -1134,6 +1139,7 @@ loop_set_status(struct loop_device *lo, blk_mode_t mode,
 		struct block_device *bdev, const struct loop_info64 *info)
 {
 	struct block_device *claim_bdev = bdev_whole(bdev);
+	struct queue_limits lim;
 	int err;
 	bool partscan = false;
 	bool size_changed = false;
@@ -1189,6 +1195,15 @@ loop_set_status(struct loop_device *lo, blk_mode_t mode,
 	/* update the direct I/O flag if lo_offset changed */
 	loop_update_dio(lo);
 
+	/*
+	 * The offset change may have silently dropped LO_FLAGS_DIRECT_IO;
+	 * refresh the DMA alignment so the queue limits match the new flag
+	 * state.
+	 */
+	lim = queue_limits_start_update(lo->lo_queue);
+	loop_set_dma_limit(lo, &lim, queue_logical_block_size(lo->lo_queue));
+	queue_limits_commit_update(lo->lo_queue, &lim);
+
 	blk_mq_unfreeze_queue(lo->lo_queue, memflags);
 	if (partscan)
 		clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
@@ -1361,7 +1376,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 		return 0;
 
 	if (use_dio) {
-		if (!lo_can_use_dio(lo))
+		if (!lo_can_use_dio(lo, queue_logical_block_size(lo->lo_queue)))
 			return -EINVAL;
 		/* flush dirty pages before starting to use direct I/O */
 		vfs_fsync(lo->lo_backing_file, 0);
@@ -1373,7 +1388,7 @@ static int loop_set_dio(struct loop_device *lo, unsigned long arg)
 		lo->lo_flags |= LO_FLAGS_DIRECT_IO;
 	else
 		lo->lo_flags &= ~LO_FLAGS_DIRECT_IO;
-	loop_set_dma_limit(lo, &lim);
+	loop_set_dma_limit(lo, &lim, queue_logical_block_size(lo->lo_queue));
 	queue_limits_commit_update(lo->lo_queue, &lim);
 	blk_mq_unfreeze_queue(lo->lo_queue, memflags);
 	return 0;
#2 lore raw
Quoted 6 lines · Tao Cui

I noticed the sashiko report [1]. The lock order in loop_set_status() is indeed different from 06a2ff603f1f, but I think the reported deadlock cannot be reached through this code. The queue is frozen and fully drained before queue_limits_start_update() takes q->limits_lock, and nothing between start_update() and commit_update() blocks, so the new code never waits for anything while holding the lock.

Whether or not a deadlock can be triggered, I expect lockdep to complain about this patch. blk_mq_freeze_queue() acquires q->io_lockdep_map and that map is only released by blk_mq_unfreeze_queue(). Hence, calling queue_limits_start_update() between these two calls records the dependency q_usage_counter(io) -> limits_lock. The opposite order is already used by loop_set_dio(), loop_set_block_size() and queue_limits_commit_update_frozen(). This is the same complaint that has been fixed by commit 06a2ff603f1f ("loop: Fix recently introduced lock inversion"). A lockdep complaint is a problem by itself since it disables lockdep and hence hides later reports. Has this patch been tested with lockdep enabled?

Quoted 6 lines

I am more worried about the suggested reorder: with start_update() before blk_mq_freeze_queue(), loop_set_status() would hold limits_lock while draining, and an in-flight write-zeroes request on an unsupported backing file would block in loop_clear_limits() on that lock while holding a queue usage counter, so the freeze would never complete.

That scenario is not specific to loop_set_status(). loop_set_dio(), loop_set_block_size() and the sysfs store methods already hold limits_lock while freezing the queue. Making loop_set_status() follow the same convention does not introduce a new class of problems. The proper fix is the loop_clear_limits() rework, so please make this patch depend on "loop: defer the queue limits clear to a workqueue" instead of deviating from the locking convention.

Quoted 2 lines

The snapshot taken before the drain could also overwrite a concurrent loop_clear_limits() commit.

I don't think that can happen. limits_lock is held from queue_limits_start_update() until queue_limits_commit_update(), so loop_clear_limits() cannot commit anything in between.

Quoted 2 lines

So I would like to keep the patch as it stands, but happy to discuss further.

Please call queue_limits_start_update() before blk_mq_freeze_queue() in loop_set_status() and call loop_set_dma_limit() and queue_limits_commit_update() after loop_update_dio().

A few more comments about the patch itself:
- lo_can_use_dio() is now evaluated twice, once before the limits are committed and once in loop_update_dio(), and both results must agree. That seems fragile to me. Please consider documenting this requirement above loop_set_dma_limit().
- With this patch every LOOP_SET_STATUS call updates the queue limits. Can that be restricted to the case where LO_FLAGS_DIRECT_IO has been cleared?
- Please mention in the patch description that the stale limit is too strict rather than too permissive, in other words, that the effect is that O_DIRECT I/O with 512-byte aligned buffers is rejected.
- How has this patch been tested? A blktests test would be welcome.
- Please use 12 characters for the commit ID in the Fixes: tag.

Thanks,

Bart.