* [PATCH] loop: fix stale dma_alignment when direct I/O is silently dropped
@ 2026-11-24 9:33 Tao Cui
0 siblings, 0 replies; only message in thread
From: Tao Cui @ 2026-11-24 9:33 UTC (permalink / raw)
To: Jens Axboe, Bart Van Assche
Cc: Keith Busch, Jan Kara, Ming Lei, Damien Le Moal, Hannes Reinecke,
Zhao Gongyi, stable, linux-block, linux-kernel, cuitao, cui.tao
From: Tao Cui <cuitao@kylinos.cn>
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")
Cc: stable@vger.kernel.org
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
drivers/block/loop.c | 31 +++++++++++++++++++++++--------
1 file changed, 23 insertions(+), 8 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 194f51d9dab6..4798c4d47a0e 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -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.53.0
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-24 9:34 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 9:33 [PATCH] loop: fix stale dma_alignment when direct I/O is silently dropped Tao Cui
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox