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

From: Bart Van Assche

Date: Thu Nov 26 2026 - 10:32:24 EST


On 11/26/26 5:56 AM, Tao Cui wrote:
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
work item takes the queue limits lock before freezing, matching the
order used by loop_set_dio() and loop_set_block_size(). 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.

This patch is more complicated and harder to verify than it should. Has
it been considered to allocate memory dynamically instead of adding new
members to struct loop_device, as is done in the untested patch below?

Thanks,

Bart.


diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index d122cb008811..bd9c453a9b8c 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -222,26 +222,76 @@ static void loop_set_size(struct loop_device *lo, loff_t size)
kobject_uevent(&disk_to_dev(lo->lo_disk)->kobj, KOBJ_CHANGE);
}

-static void loop_clear_limits(struct loop_device *lo, int mode)
+/*
+ * Information passed from lo_fallocate() to loop_clear_limits_workfn().
+ * @lo_backing_file holds a reference to prevent that the backing file is
+ * freed and its address is reused by another backing file before
+ * loop_clear_limits_workfn() has finished.
+ */
+struct loop_clear_limits_work {
+ struct work_struct clear_limits_work;
+ struct loop_device *lo;
+ struct file *lo_backing_file;
+ int mode;
+};
+
+static void loop_clear_limits_workfn(struct work_struct *work)
{
- struct queue_limits lim = queue_limits_start_update(lo->lo_queue);
+ struct loop_clear_limits_work *w = container_of(work,
+ struct loop_clear_limits_work, clear_limits_work);
+ struct loop_device *lo = w->lo;
+ struct queue_limits lim;
+ unsigned int memflags;
+ bool changed = false;

- if (mode & FALLOC_FL_ZERO_RANGE)
- lim.max_write_zeroes_sectors = 0;
+ mutex_lock(&lo->lo_mutex);
+ /* Skip the update if the loop device has been unbound or rebound. */
+ if (lo->lo_state != Lo_bound || lo->lo_backing_file != w->lo_backing_file)
+ goto unlock;

- if (mode & FALLOC_FL_PUNCH_HOLE) {
+ lim = queue_limits_start_update(lo->lo_queue);
+ if ((w->mode & FALLOC_FL_ZERO_RANGE) && lim.max_write_zeroes_sectors) {
+ lim.max_write_zeroes_sectors = 0;
+ changed = true;
+ }
+ if ((w->mode & FALLOC_FL_PUNCH_HOLE) && lim.max_hw_discard_sectors) {
lim.max_hw_discard_sectors = 0;
lim.discard_granularity = 0;
+ changed = true;
}
-
- /*
- * 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.
- */
+ if (!changed) {
+ queue_limits_cancel_update(lo->lo_queue);
+ goto unlock;
+ }
+ memflags = blk_mq_freeze_queue(lo->lo_queue);
queue_limits_commit_update(lo->lo_queue, &lim);
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
+
+unlock:
+ mutex_unlock(&lo->lo_mutex);
+ fput(w->lo_backing_file);
+ kfree(w);
+}
+
+/*
+ * The queue limits can't be modified from inside lo_fallocate() since that
+ * would require freezing the queue while a request is being processed. Hence
+ * defer the queue limits update to a work item. The work item is queued on
+ * lo->workqueue such that lo_free_disk() waits for it to finish.
+ */
+static void loop_clear_limits(struct loop_device *lo, struct file *file,
+ int mode)
+{
+ struct loop_clear_limits_work *w;
+
+ w = kmalloc_obj(*w, GFP_NOIO);
+ if (!w)
+ return;
+ INIT_WORK(&w->clear_limits_work, loop_clear_limits_workfn);
+ w->lo = lo;
+ w->lo_backing_file = get_file(file);
+ w->mode = mode;
+ queue_work(lo->workqueue, &w->clear_limits_work);
}

static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
@@ -268,7 +318,7 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
* supported and clear them here if that turns out not to be true.
*/
if (unlikely(ret == -EOPNOTSUPP))
- loop_clear_limits(lo, mode);
+ loop_clear_limits(lo, file, mode);

return ret;
}