Re: [PATCH] loop: fix stale dma_alignment when direct I/O is silently dropped

From: Bart Van Assche

Date: Tue Nov 24 2026 - 11:38:09 EST


On 11/24/26 2:56 AM, Tao Cui wrote:
> 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?

> 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.

> 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.

> 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.