Re: [PATCH v8] loop: defer the queue limits clear to a workqueue

From: Tao Cui

Date: Thu Nov 26 2026 - 05:06:37 EST


Hello, Bart,

在 2026/11/26 13:42, Tao Cui 写道:
> From: Tao Cui <cuitao@xxxxxxxxxx>
>
> loop_clear_limits() calls queue_limits_commit_update() directly from
> the loop workqueue that processes the request. That does a
> non-atomic struct assignment to q->limits without freezing the queue,
> which races with lockless readers of q->limits on other CPUs, such
> as bio splitting reading max_hw_sectors and the discard path reading
> max_hw_discard_sectors, letting them observe torn values. The
> trigger is a discard or write-zeroes request on a loop device whose
> backing file does not support the corresponding fallocate operation.
>
> The code already has an XXX comment saying this should move to a
> workqueue. Do that: schedule a work item on the system workqueue,
> where it is safe to freeze the queue around the limits update. The
> unsupported modes live under a new mutex, lo->clear_limits_lock,
> guarded by a backing-file check that drops records from a racing
> unbind or rebind. The work item is scheduled while holding that
> lock, so an unbind resets the verdicts under the lock before calling
> cancel_work_sync() and cannot miss a schedule from a request that is
> still in flight. Requests that arrive while the clear is in flight
> fail quietly, so the console report stays at the first failure.
> loop_remove() also cancels the work item before the queue becomes
> unreachable.
>

Sashiko reported three issues against v8 [1]; all are real and fixed in
v9 below, so v8 can be skipped.

> Does freezing lo->lo_queue before queue_limits_start_update() invert the
> block layer lock ordering between queue freeze and q->limits_lock?

Yes, the work item froze first. v9 takes limits_lock before freezing,
like loop_set_dio() and loop_set_block_size().

The splat and deadlock part I could not reproduce: since f1be1788a32e
("block: model freeze & enter queue as lock for supporting lockdep")
the freeze path annotates io_lockdep_map as a trylock, so the reversed
order does not become a recorded dependency, and requests never take
limits_lock so a freezing caller's drain always completes. Interleaved
LOOP_SET_BLOCK_SIZE, LOOP_SET_DIRECT_IO, write-zeroes and rebind churn
under PROVE_LOCKING stay quiet on both v8 and v9. FWIW the same
applies to loop_set_status() in my dma-alignment patch: I'll reorder it
in v2 for consistency rather than wait for a report.

> If LOOP_SET_BLOCK_SIZE is called on a bound loop device after an
> unsupported mode has been recorded in lo->clear_limits_seen, can the
> cleared queue limits end up permanently re-enabled?

Yes. v9 resets clear_limits_seen inside the recompute's freeze so the
next failure reschedules the clear.

> Can calling lo_unsupported() when !bdev_max_discard_sectors(lo->lo_device)
> falsely disable FALLOC_FL_ZERO_RANGE or latch FALLOC_FL_PUNCH_HOLE when
> discard was only disabled via sysfs?

Yes. v9 gates on the per-mode hardware limits instead of the shared
bdev_max_discard_sectors().

Tested with PROVE_LOCKING enabled: loop/011, the concurrency sweep, the
interleave and the mkfs/unbind stress run clean, v8 as a negative
control included.

Thanks,
Tao.

[1] https://sashiko.dev/#/patchset/20261126054223.176824-1-cui.tao@xxxxxxxxx