* [PATCH v8] loop: defer the queue limits clear to a workqueue
@ 2026-11-26 5:42 Tao Cui
2026-11-26 10:06 ` Tao Cui
2026-11-26 13:56 ` [PATCH v9] " Tao Cui
0 siblings, 2 replies; 4+ messages in thread
From: Tao Cui @ 2026-11-26 5:42 UTC (permalink / raw)
To: Bart Van Assche, axboe, hch, Tetsuo Handa
Cc: cuitao, linux-block, linux-kernel, cui.tao,
Shin'ichiro Kawasaki
From: Tao Cui <cuitao@kylinos.cn>
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.
Fixes: 5f75e081ab5c ("loop: Disable fallocate() zero and discard if not supported")
Suggested-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
Changes since v7:
- quiet repeated failures at the point of failure instead of at
queue_rq() time: verdicts are recorded per mode for the lifetime of
the backing file and loop_clear_limits() reports whether a verdict
is new, so repeats mark only their own request quiet. Under
concurrent dispatch this keeps exactly the first failure per mode on
the console (sashiko review; eight-thread write-zeroes on a
tmpfs-backed loop device: v7 logged two errors where loop/011
expects one, this logs one)
- schedule the clear work while holding clear_limits_lock and reset
the verdicts before cancel_work_sync() in __loop_clr_fd(): a verdict
that passed the backing-file check now queues the work before the
lock is released, so the cancel cannot miss a schedule from a
request still in flight, which previously could touch a loop_device
already freed (Bart)
- skip the fallocate probe when the mode is already known to be
unsupported, instead of repeating it while the limits commit is in
flight (Bart)
- return early from the work item when no verdict is left to commit,
instead of freezing the queue and committing unchanged limits (Bart)
- drop the verdict reset in loop_assign_backing_file(): since the
LOOP_CHANGE_FD removal (d4fabede1da0) its only caller is
loop_configure(), which requires an unbound device, so it was dead;
one reset site remains, so no helper was added (Bart)
Changes since v6:
- fix the __loop_clr_fd() comment: asynchronous I/O can be in flight
across an unbind, and loop_remove()'s cancel is the backstop for
work scheduled after __loop_clr_fd()'s
- drop the cancel_work_sync() from lo_free_disk(): no reachable path
needs it
- reword the loop_remove() comment accordingly
drivers/block/loop.c | 123 ++++++++++++++++++++++++++++++++++++++-----
1 file changed, 111 insertions(+), 12 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 194f51d9dab6..8c2ef0c327d4 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -67,6 +67,9 @@ struct loop_device {
struct list_head rootcg_cmd_list;
struct list_head idle_worker_list;
struct rb_root worker_tree;
+ struct work_struct clear_limits_work;
+ struct mutex clear_limits_lock;
+ unsigned int clear_limits_seen;
struct timer_list timer;
bool sysfs_inited;
@@ -222,9 +225,31 @@ 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)
+static void loop_clear_limits_workfn(struct work_struct *work)
{
- struct queue_limits lim = queue_limits_start_update(lo->lo_queue);
+ struct loop_device *lo =
+ container_of(work, struct loop_device, clear_limits_work);
+ struct queue_limits lim;
+ unsigned int memflags;
+ int mode;
+
+ /*
+ * Snapshot the recorded modes without clearing them; verdicts stay
+ * for the life of the backing file. The freeze drains in-flight
+ * requests, so everything scheduled before it is visible here.
+ */
+ memflags = blk_mq_freeze_queue(lo->lo_queue);
+ mutex_lock(&lo->clear_limits_lock);
+ mode = lo->clear_limits_seen;
+ mutex_unlock(&lo->clear_limits_lock);
+
+ /* A racing unbind may have reset the verdicts; nothing to commit. */
+ if (!mode) {
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
+ return;
+ }
+
+ lim = queue_limits_start_update(lo->lo_queue);
if (mode & FALLOC_FL_ZERO_RANGE)
lim.max_write_zeroes_sectors = 0;
@@ -234,14 +259,65 @@ static void loop_clear_limits(struct loop_device *lo, int mode)
lim.discard_granularity = 0;
}
- /*
- * 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.
- */
queue_limits_commit_update(lo->lo_queue, &lim);
+ blk_mq_unfreeze_queue(lo->lo_queue, memflags);
+}
+
+/*
+ * Record that the backing file does not support @mode. Returns true
+ * for a first verdict or a dropped stale record, false for a repeat.
+ */
+static bool loop_clear_limits(struct loop_device *lo, struct file *file,
+ int mode)
+{
+ bool first;
+
+ mutex_lock(&lo->clear_limits_lock);
+ if (lo->lo_backing_file != file) {
+ /* Stale record from a racing unbind or rebind; drop it. */
+ mutex_unlock(&lo->clear_limits_lock);
+ return true;
+ }
+ first = (mode & ~lo->clear_limits_seen) != 0;
+ lo->clear_limits_seen |= mode;
+ /* Scheduled under the lock so cancel_work_sync() cannot miss it. */
+ if (first)
+ schedule_work(&lo->clear_limits_work);
+ mutex_unlock(&lo->clear_limits_lock);
+
+ return first;
+}
+
+/*
+ * Record the verdict and return -EOPNOTSUPP; repeats stay quiet so only
+ * the first failure per mode reports.
+ */
+static int lo_unsupported(struct loop_device *lo, struct request *rq,
+ struct file *file, int mode)
+{
+ if (!loop_clear_limits(lo, file, mode))
+ rq->rq_flags |= RQF_QUIET;
+
+ return -EOPNOTSUPP;
+}
+
+/*
+ * Return true when @mode is already recorded as unsupported, so the
+ * fallocate probe can be skipped.
+ */
+static bool loop_mode_unsupported(struct loop_device *lo, struct file *file,
+ int mode)
+{
+ bool known;
+
+ mode &= FALLOC_FL_ZERO_RANGE | FALLOC_FL_PUNCH_HOLE;
+
+ mutex_lock(&lo->clear_limits_lock);
+ known = lo->lo_backing_file == file &&
+ (mode & lo->clear_limits_seen);
+ mutex_unlock(&lo->clear_limits_lock);
+
+ return known;
}
static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
@@ -256,8 +332,16 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
mode |= FALLOC_FL_KEEP_SIZE;
- if (!bdev_max_discard_sectors(lo->lo_device))
- return -EOPNOTSUPP;
+ if (!bdev_max_discard_sectors(lo->lo_device)) {
+ /*
+ * Limits already committed; record the op's bit to keep
+ * the per-mode quiet accounting.
+ */
+ return lo_unsupported(lo, rq, file, mode);
+ }
+
+ if (loop_mode_unsupported(lo, file, mode))
+ return lo_unsupported(lo, rq, file, mode);
scoped_guard(file_write, file)
ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq));
@@ -269,7 +353,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);
+ return lo_unsupported(lo, rq, file, mode);
return ret;
}
@@ -1035,6 +1119,15 @@ static void __loop_clr_fd(struct loop_device *lo)
lo->lo_backing_file = NULL;
spin_unlock_irq(&lo->lo_lock);
+ /*
+ * Requests may still be in flight across this unbind; reset the
+ * verdicts before the cancel so it cannot miss the work item.
+ */
+ mutex_lock(&lo->clear_limits_lock);
+ lo->clear_limits_seen = 0;
+ mutex_unlock(&lo->clear_limits_lock);
+ cancel_work_sync(&lo->clear_limits_work);
+
lo->lo_device = NULL;
lo->lo_offset = 0;
lo->lo_sizelimit = 0;
@@ -1700,6 +1793,7 @@ static void lo_free_disk(struct gendisk *disk)
loop_free_idle_workers(lo, true);
timer_shutdown_sync(&lo->timer);
mutex_destroy(&lo->lo_mutex);
+ mutex_destroy(&lo->clear_limits_lock);
kfree(lo);
}
@@ -2018,6 +2112,8 @@ static int loop_add(int i)
spin_lock_init(&lo->lo_lock);
spin_lock_init(&lo->lo_work_lock);
INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn);
+ INIT_WORK(&lo->clear_limits_work, loop_clear_limits_workfn);
+ mutex_init(&lo->clear_limits_lock);
INIT_LIST_HEAD(&lo->rootcg_cmd_list);
disk->major = LOOP_MAJOR;
disk->first_minor = i << part_shift;
@@ -2056,6 +2152,9 @@ static int loop_add(int i)
static void loop_remove(struct loop_device *lo)
{
+ /* Cancel before the queue becomes unreachable. */
+ cancel_work_sync(&lo->clear_limits_work);
+
/* Make this loop device unreachable from pathname. */
del_gendisk(lo->lo_disk);
blk_mq_free_tag_set(&lo->tag_set);
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v8] loop: defer the queue limits clear to a workqueue 2026-11-26 5:42 [PATCH v8] loop: defer the queue limits clear to a workqueue Tao Cui @ 2026-11-26 10:06 ` Tao Cui 2026-11-26 13:56 ` [PATCH v9] " Tao Cui 1 sibling, 0 replies; 4+ messages in thread From: Tao Cui @ 2026-11-26 10:06 UTC (permalink / raw) To: Bart Van Assche, axboe, hch, Tetsuo Handa Cc: cui.tao, cuitao, linux-block, linux-kernel, Shin'ichiro Kawasaki Hello, Bart, 在 2026/11/26 13:42, Tao Cui 写道: > From: Tao Cui <cuitao@kylinos.cn> > > 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@linux.dev ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v9] loop: defer the queue limits clear to a workqueue 2026-11-26 5:42 [PATCH v8] loop: defer the queue limits clear to a workqueue Tao Cui 2026-11-26 10:06 ` Tao Cui @ 2026-11-26 13:56 ` Tao Cui 2026-11-26 15:32 ` Bart Van Assche 1 sibling, 1 reply; 4+ messages in thread From: Tao Cui @ 2026-11-26 13:56 UTC (permalink / raw) To: Bart Van Assche, axboe, hch, Tetsuo Handa Cc: cuitao, linux-block, linux-kernel, cui.tao, Shin'ichiro Kawasaki From: Tao Cui <cuitao@kylinos.cn> 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. Fixes: 5f75e081ab5c ("loop: Disable fallocate() zero and discard if not supported") Suggested-by: Bart Van Assche <bvanassche@acm.org> Signed-off-by: Tao Cui <cuitao@kylinos.cn> --- Changes since v8: - take limits_lock via queue_limits_start_update() before freezing in the work item, following the convention that all block driver code takes limits_lock before freezing (06a2ff603f1f, loop_set_dio(), loop_set_block_size(), queue_limits_commit_update_frozen()); the v8 order was a regression introduced by the v8 early-return rework (sashiko review) - gate lo_fallocate() on the per-mode hardware limits (max_hw_discard_sectors / max_write_zeroes_sectors) instead of the shared bdev_max_discard_sectors(), which is also driven by the sysfs discard cap: with only the punch-hole verdict committed, a write-zeroes request could latch a false ZERO_RANGE verdict and clear the write-zeroes limits on a backing file that supports them (sashiko review) - reset clear_limits_seen in loop_set_block_size(): its limits recompute re-enables cleared modes, and with verdicts previously kept for the life of the backing file, repeat failures would never reschedule the clear (sashiko review) - drop verdicts recorded against a NULL backing file silently and annotate the lockless lo_backing_file accesses with READ_ONCE and WRITE_ONCE (sashiko review) - verified with a PROVE_LOCKING-enabled kernel running loop/011, the concurrency sweep, interleaved LOOP_SET_BLOCK_SIZE and write-zeroes traffic, and sixty mkfs/unbind stress rounds: no lockdep reports and no hangs, both with this reorder and, as a negative control, with the v8 order drivers/block/loop.c | 142 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 128 insertions(+), 14 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index 194f51d9dab6..460c3a319988 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -67,6 +67,9 @@ struct loop_device { struct list_head rootcg_cmd_list; struct list_head idle_worker_list; struct rb_root worker_tree; + struct work_struct clear_limits_work; + struct mutex clear_limits_lock; + unsigned int clear_limits_seen; struct timer_list timer; bool sysfs_inited; @@ -222,9 +225,35 @@ 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) +static void loop_clear_limits_workfn(struct work_struct *work) { - struct queue_limits lim = queue_limits_start_update(lo->lo_queue); + struct loop_device *lo = + container_of(work, struct loop_device, clear_limits_work); + struct queue_limits lim; + unsigned int memflags; + int mode; + + /* + * Take limits_lock before freezing, like loop_set_dio() and + * loop_set_block_size(). + */ + lim = queue_limits_start_update(lo->lo_queue); + + /* + * The freeze drains in-flight requests, so everything scheduled + * before it is visible in the snapshot below. + */ + memflags = blk_mq_freeze_queue(lo->lo_queue); + mutex_lock(&lo->clear_limits_lock); + mode = lo->clear_limits_seen; + mutex_unlock(&lo->clear_limits_lock); + + /* A racing unbind may have reset the verdicts; nothing to commit. */ + if (!mode) { + queue_limits_commit_update(lo->lo_queue, &lim); + blk_mq_unfreeze_queue(lo->lo_queue, memflags); + return; + } if (mode & FALLOC_FL_ZERO_RANGE) lim.max_write_zeroes_sectors = 0; @@ -234,14 +263,69 @@ static void loop_clear_limits(struct loop_device *lo, int mode) lim.discard_granularity = 0; } - /* - * 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. - */ queue_limits_commit_update(lo->lo_queue, &lim); + blk_mq_unfreeze_queue(lo->lo_queue, memflags); +} + +/* + * Record that the backing file does not support @mode. Returns true + * for a first verdict or a dropped stale record, false for a repeat. + */ +static bool loop_clear_limits(struct loop_device *lo, struct file *file, + int mode) +{ + bool first; + + mutex_lock(&lo->clear_limits_lock); + if (READ_ONCE(lo->lo_backing_file) != file) { + /* Stale record from a racing unbind or rebind; drop it. */ + mutex_unlock(&lo->clear_limits_lock); + return true; + } + first = (mode & ~lo->clear_limits_seen) != 0; + lo->clear_limits_seen |= mode; + /* Scheduled under the lock so cancel_work_sync() cannot miss it. */ + if (first) + schedule_work(&lo->clear_limits_work); + mutex_unlock(&lo->clear_limits_lock); + + return first; +} + +/* + * Record the verdict and return -EOPNOTSUPP; repeats stay quiet so only + * the first failure per mode reports. + */ +static int lo_unsupported(struct loop_device *lo, struct request *rq, + struct file *file, int mode) +{ + /* A NULL file means the backing file is already gone. */ + if (!file) + return -EOPNOTSUPP; + + if (!loop_clear_limits(lo, file, mode)) + rq->rq_flags |= RQF_QUIET; + + return -EOPNOTSUPP; +} + +/* + * Return true when @mode is already recorded as unsupported, so the + * fallocate probe can be skipped. + */ +static bool loop_mode_unsupported(struct loop_device *lo, struct file *file, + int mode) +{ + bool known; + + mode &= FALLOC_FL_ZERO_RANGE | FALLOC_FL_PUNCH_HOLE; + + mutex_lock(&lo->clear_limits_lock); + known = file && READ_ONCE(lo->lo_backing_file) == file && + (mode & lo->clear_limits_seen); + mutex_unlock(&lo->clear_limits_lock); + + return known; } static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, @@ -256,8 +340,19 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos, mode |= FALLOC_FL_KEEP_SIZE; - if (!bdev_max_discard_sectors(lo->lo_device)) - return -EOPNOTSUPP; + /* + * Zeroed hardware limits mean the matching verdict is already + * committed; record the op's bit for the per-mode quiet accounting. + */ + if ((mode & FALLOC_FL_PUNCH_HOLE) && + !bdev_limits(lo->lo_device)->max_hw_discard_sectors) + return lo_unsupported(lo, rq, file, mode); + if ((mode & FALLOC_FL_ZERO_RANGE) && + !bdev_write_zeroes_sectors(lo->lo_device)) + return lo_unsupported(lo, rq, file, mode); + + if (loop_mode_unsupported(lo, file, mode)) + return lo_unsupported(lo, rq, file, mode); scoped_guard(file_write, file) ret = file->f_op->fallocate(file, mode, pos, blk_rq_bytes(rq)); @@ -269,7 +364,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); + return lo_unsupported(lo, rq, file, mode); return ret; } @@ -513,7 +608,7 @@ static int loop_validate_file(struct file *file, struct block_device *bdev) static void loop_assign_backing_file(struct loop_device *lo, struct file *file) { - lo->lo_backing_file = file; + WRITE_ONCE(lo->lo_backing_file, file); lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping); mapping_set_gfp_mask(file->f_mapping, lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS)); @@ -1032,9 +1127,18 @@ static void __loop_clr_fd(struct loop_device *lo) spin_lock_irq(&lo->lo_lock); filp = lo->lo_backing_file; - lo->lo_backing_file = NULL; + WRITE_ONCE(lo->lo_backing_file, NULL); spin_unlock_irq(&lo->lo_lock); + /* + * Requests may still be in flight across this unbind; reset the + * verdicts before the cancel so it cannot miss the work item. + */ + mutex_lock(&lo->clear_limits_lock); + lo->clear_limits_seen = 0; + mutex_unlock(&lo->clear_limits_lock); + cancel_work_sync(&lo->clear_limits_work); + lo->lo_device = NULL; lo->lo_offset = 0; lo->lo_sizelimit = 0; @@ -1415,6 +1519,10 @@ static int loop_set_block_size(struct loop_device *lo, blk_mode_t mode, loop_update_limits(lo, &lim, arg); memflags = blk_mq_freeze_queue(lo->lo_queue); + /* The recompute above may re-enable modes with recorded verdicts. */ + mutex_lock(&lo->clear_limits_lock); + lo->clear_limits_seen = 0; + mutex_unlock(&lo->clear_limits_lock); err = queue_limits_commit_update(lo->lo_queue, &lim); loop_update_dio(lo); blk_mq_unfreeze_queue(lo->lo_queue, memflags); @@ -1700,6 +1808,7 @@ static void lo_free_disk(struct gendisk *disk) loop_free_idle_workers(lo, true); timer_shutdown_sync(&lo->timer); mutex_destroy(&lo->lo_mutex); + mutex_destroy(&lo->clear_limits_lock); kfree(lo); } @@ -2018,6 +2127,8 @@ static int loop_add(int i) spin_lock_init(&lo->lo_lock); spin_lock_init(&lo->lo_work_lock); INIT_WORK(&lo->rootcg_work, loop_rootcg_workfn); + INIT_WORK(&lo->clear_limits_work, loop_clear_limits_workfn); + mutex_init(&lo->clear_limits_lock); INIT_LIST_HEAD(&lo->rootcg_cmd_list); disk->major = LOOP_MAJOR; disk->first_minor = i << part_shift; @@ -2056,6 +2167,9 @@ static int loop_add(int i) static void loop_remove(struct loop_device *lo) { + /* Cancel before the queue becomes unreachable. */ + cancel_work_sync(&lo->clear_limits_work); + /* Make this loop device unreachable from pathname. */ del_gendisk(lo->lo_disk); blk_mq_free_tag_set(&lo->tag_set); -- 2.53.0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v9] loop: defer the queue limits clear to a workqueue 2026-11-26 13:56 ` [PATCH v9] " Tao Cui @ 2026-11-26 15:32 ` Bart Van Assche 0 siblings, 0 replies; 4+ messages in thread From: Bart Van Assche @ 2026-11-26 15:32 UTC (permalink / raw) To: Tao Cui, axboe, hch, Tetsuo Handa Cc: cuitao, linux-block, linux-kernel, Shin'ichiro Kawasaki 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; } ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 15:32 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-26 5:42 [PATCH v8] loop: defer the queue limits clear to a workqueue Tao Cui 2026-11-26 10:06 ` Tao Cui 2026-11-26 13:56 ` [PATCH v9] " Tao Cui 2026-11-26 15:32 ` Bart Van Assche
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox