mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v7] loop: defer the queue limits clear to a workqueue
@ 2026-11-24  3:12 Tao Cui
  2026-11-24  8:10 ` Tao Cui
  0 siblings, 1 reply; 3+ messages in thread
From: Tao Cui @ 2026-11-24  3:12 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
pending 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.  Requests that arrive while the clear is in flight fail
quietly, so the console report stays at the first failure.  The work
item is cancelled before the backing file changes and before the
device is freed.

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>
---
 drivers/block/loop.c | 91 +++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 82 insertions(+), 9 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 194f51d9dab6..d15ccb339f03 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -67,6 +67,10 @@ 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_mode;
+	bool			clear_limits_pending;
 	struct timer_list       timer;
 	bool			sysfs_inited;
 
@@ -222,9 +226,25 @@ 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 loop_device *lo =
+		container_of(work, struct loop_device, clear_limits_work);
 	struct queue_limits lim = queue_limits_start_update(lo->lo_queue);
+	unsigned int memflags;
+	int mode;
+
+	/*
+	 * Requests that arrive while this clear is in flight are failed
+	 * quietly (see loop_queue_rq); once the limits have been
+	 * committed, further ones are rejected early at submission.
+	 */
+	memflags = blk_mq_freeze_queue(lo->lo_queue);
+	mutex_lock(&lo->clear_limits_lock);
+	mode = lo->clear_limits_mode;
+	lo->clear_limits_mode = 0;
+	WRITE_ONCE(lo->clear_limits_pending, false);
+	mutex_unlock(&lo->clear_limits_lock);
 
 	if (mode & FALLOC_FL_ZERO_RANGE)
 		lim.max_write_zeroes_sectors = 0;
@@ -234,14 +254,27 @@ 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);
+}
+
+static void loop_clear_limits(struct loop_device *lo, struct file *file,
+			      int mode)
+{
+	mutex_lock(&lo->clear_limits_lock);
+	if (lo->lo_backing_file != file) {
+		/*
+		 * The recording request was racing with an unbind or a
+		 * rebinding; its verdict is about a backing file that is
+		 * no longer current.  Drop it.
+		 */
+		mutex_unlock(&lo->clear_limits_lock);
+		return;
+	}
+	lo->clear_limits_mode |= mode;
+	WRITE_ONCE(lo->clear_limits_pending, true);
+	mutex_unlock(&lo->clear_limits_lock);
+	schedule_work(&lo->clear_limits_work);
 }
 
 static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
@@ -269,7 +302,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;
 }
@@ -514,6 +547,15 @@ 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;
+	/*
+	 * Discard any modes recorded against the old backing file.  The
+	 * paths that can race with a pending work item cancel it before
+	 * they get here.
+	 */
+	mutex_lock(&lo->clear_limits_lock);
+	lo->clear_limits_mode = 0;
+	WRITE_ONCE(lo->clear_limits_pending, false);
+	mutex_unlock(&lo->clear_limits_lock);
 	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));
@@ -1035,6 +1077,21 @@ static void __loop_clr_fd(struct loop_device *lo)
 	lo->lo_backing_file = NULL;
 	spin_unlock_irq(&lo->lo_lock);
 
+	/*
+	 * Discard and write-zeroes requests are submitted synchronously,
+	 * but a fatal signal can kill the submitting task between the
+	 * per-bio completions of a multi-bio range, leaving requests in
+	 * flight across this unbind.  Cancel any clear they have
+	 * scheduled and reset the modes; loop_clear_limits() also drops
+	 * records whose backing file is no longer current, so stragglers
+	 * arriving after a later loop_configure() cannot affect it.
+	 */
+	cancel_work_sync(&lo->clear_limits_work);
+	mutex_lock(&lo->clear_limits_lock);
+	lo->clear_limits_mode = 0;
+	WRITE_ONCE(lo->clear_limits_pending, false);
+	mutex_unlock(&lo->clear_limits_lock);
+
 	lo->lo_device = NULL;
 	lo->lo_offset = 0;
 	lo->lo_sizelimit = 0;
@@ -1700,6 +1757,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);
 }
 
@@ -1802,9 +1860,19 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
 
 	switch (req_op(rq)) {
 	case REQ_OP_FLUSH:
+		cmd->use_aio = false;
+		break;
 	case REQ_OP_DISCARD:
 	case REQ_OP_WRITE_ZEROES:
 		cmd->use_aio = false;
+		/*
+		 * A pending limits clear means this operation is already
+		 * known to be unsupported and the commit will make later
+		 * attempts fail early at submission.  Fail quietly so only
+		 * the first failure reports to the console.
+		 */
+		if (READ_ONCE(lo->clear_limits_pending))
+			rq->rq_flags |= RQF_QUIET;
 		break;
 	default:
 		cmd->use_aio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
@@ -2018,6 +2086,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 +2126,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	[flat|nested] 3+ messages in thread

* Re: [PATCH v7] loop: defer the queue limits clear to a workqueue
  2026-11-24  3:12 [PATCH v7] loop: defer the queue limits clear to a workqueue Tao Cui
@ 2026-11-24  8:10 ` Tao Cui
  2026-11-24 16:56   ` Bart Van Assche
  0 siblings, 1 reply; 3+ messages in thread
From: Tao Cui @ 2026-11-24  8:10 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/24 11:12, 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
> pending 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.  Requests that arrive while the clear is in flight fail
> quietly, so the console report stays at the first failure.  The work
> item is cancelled before the backing file changes and before the
> device is freed.
> 
Sashiko's automated review pointed out a hole in the quiet marking in v7 [1], and after looking into it, I believe the report is correct.

Marking requests quiet at queue_rq() time misses two cases. Requests submitted in a batch can all pass loop_queue_rq() before the first failure records the verdict. Also, bios that are already inside submit_bio_noacct() when the queue freezes have already passed their limits check; they resume from bio_queue_enter() after loop_clear_limits_workfn() clears the pending flag and still report the same failure. Both cases can produce repeated console messages.

I can reproduce this with eight threads issuing concurrent write-zeroes to a tmpfs-backed loop device: v7 logs two errors where loop/011 expects exactly one.

For v8, my plan is to stop deciding whether to quiet a request at queue_rq() time and instead quiet repeated failures at the point of failure. The verdicts remain recorded for the lifetime of the backing file and are cleared only on rebind or unbind. loop_clear_limits() reports whether a verdict is new, and repeated failures mark only their own request quiet. That guarantees the first failure for each mode is reported, regardless of how dispatch and queue freezing interleave.

With this change, the same eight-thread test logs exactly one error.

Does this approach look reasonable to you? If so, I'll send the already tested patch as v8.

Thanks,
Tao

[1] https://sashiko.dev/#/patchset/20261124031238.555291-1-cui.tao@linux.dev

> 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>
> ---
>  drivers/block/loop.c | 91 +++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 82 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index 194f51d9dab6..d15ccb339f03 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -67,6 +67,10 @@ 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_mode;
> +	bool			clear_limits_pending;
>  	struct timer_list       timer;
>  	bool			sysfs_inited;
>  
> @@ -222,9 +226,25 @@ 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 loop_device *lo =
> +		container_of(work, struct loop_device, clear_limits_work);
>  	struct queue_limits lim = queue_limits_start_update(lo->lo_queue);
> +	unsigned int memflags;
> +	int mode;
> +
> +	/*
> +	 * Requests that arrive while this clear is in flight are failed
> +	 * quietly (see loop_queue_rq); once the limits have been
> +	 * committed, further ones are rejected early at submission.
> +	 */
> +	memflags = blk_mq_freeze_queue(lo->lo_queue);
> +	mutex_lock(&lo->clear_limits_lock);
> +	mode = lo->clear_limits_mode;
> +	lo->clear_limits_mode = 0;
> +	WRITE_ONCE(lo->clear_limits_pending, false);
> +	mutex_unlock(&lo->clear_limits_lock);
>  
>  	if (mode & FALLOC_FL_ZERO_RANGE)
>  		lim.max_write_zeroes_sectors = 0;
> @@ -234,14 +254,27 @@ 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);
> +}
> +
> +static void loop_clear_limits(struct loop_device *lo, struct file *file,
> +			      int mode)
> +{
> +	mutex_lock(&lo->clear_limits_lock);
> +	if (lo->lo_backing_file != file) {
> +		/*
> +		 * The recording request was racing with an unbind or a
> +		 * rebinding; its verdict is about a backing file that is
> +		 * no longer current.  Drop it.
> +		 */
> +		mutex_unlock(&lo->clear_limits_lock);
> +		return;
> +	}
> +	lo->clear_limits_mode |= mode;
> +	WRITE_ONCE(lo->clear_limits_pending, true);
> +	mutex_unlock(&lo->clear_limits_lock);
> +	schedule_work(&lo->clear_limits_work);
>  }
>  
>  static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
> @@ -269,7 +302,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;
>  }
> @@ -514,6 +547,15 @@ 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;
> +	/*
> +	 * Discard any modes recorded against the old backing file.  The
> +	 * paths that can race with a pending work item cancel it before
> +	 * they get here.
> +	 */
> +	mutex_lock(&lo->clear_limits_lock);
> +	lo->clear_limits_mode = 0;
> +	WRITE_ONCE(lo->clear_limits_pending, false);
> +	mutex_unlock(&lo->clear_limits_lock);
>  	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));
> @@ -1035,6 +1077,21 @@ static void __loop_clr_fd(struct loop_device *lo)
>  	lo->lo_backing_file = NULL;
>  	spin_unlock_irq(&lo->lo_lock);
>  
> +	/*
> +	 * Discard and write-zeroes requests are submitted synchronously,
> +	 * but a fatal signal can kill the submitting task between the
> +	 * per-bio completions of a multi-bio range, leaving requests in
> +	 * flight across this unbind.  Cancel any clear they have
> +	 * scheduled and reset the modes; loop_clear_limits() also drops
> +	 * records whose backing file is no longer current, so stragglers
> +	 * arriving after a later loop_configure() cannot affect it.
> +	 */
> +	cancel_work_sync(&lo->clear_limits_work);
> +	mutex_lock(&lo->clear_limits_lock);
> +	lo->clear_limits_mode = 0;
> +	WRITE_ONCE(lo->clear_limits_pending, false);
> +	mutex_unlock(&lo->clear_limits_lock);
> +
>  	lo->lo_device = NULL;
>  	lo->lo_offset = 0;
>  	lo->lo_sizelimit = 0;
> @@ -1700,6 +1757,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);
>  }
>  
> @@ -1802,9 +1860,19 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
>  
>  	switch (req_op(rq)) {
>  	case REQ_OP_FLUSH:
> +		cmd->use_aio = false;
> +		break;
>  	case REQ_OP_DISCARD:
>  	case REQ_OP_WRITE_ZEROES:
>  		cmd->use_aio = false;
> +		/*
> +		 * A pending limits clear means this operation is already
> +		 * known to be unsupported and the commit will make later
> +		 * attempts fail early at submission.  Fail quietly so only
> +		 * the first failure reports to the console.
> +		 */
> +		if (READ_ONCE(lo->clear_limits_pending))
> +			rq->rq_flags |= RQF_QUIET;
>  		break;
>  	default:
>  		cmd->use_aio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
> @@ -2018,6 +2086,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 +2126,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);


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v7] loop: defer the queue limits clear to a workqueue
  2026-11-24  8:10 ` Tao Cui
@ 2026-11-24 16:56   ` Bart Van Assche
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Van Assche @ 2026-11-24 16:56 UTC (permalink / raw)
  To: Tao Cui, axboe, hch, Tetsuo Handa
  Cc: cuitao, linux-block, linux-kernel, Shin'ichiro Kawasaki

On 11/24/26 12:10 AM, Tao Cui wrote:
 > For v8, my plan is to stop deciding whether to quiet a request at
 > queue_rq() time and instead quiet repeated failures at the point of
 > failure. The verdicts remain recorded for the lifetime of the backing
 > file and are cleared only on rebind or unbind. loop_clear_limits()
 > reports whether a verdict is new, and repeated failures mark only
 > their own request quiet. That guarantees the first failure for each
 > mode is reported, regardless of how dispatch and queue freezing
 > interleave.
 >
 > [ ... ]
 >
 > Does this approach look reasonable to you? If so, I'll send the
 > already tested patch as v8.

That approach sounds reasonable to me. If the verdicts are kept for the
lifetime of the backing file, I think the following simplifications are
possible:
- The clear_limits_pending member variable is no longer needed. The
   work function can read the mask with the unsupported modes without
   resetting it.
- lo_fallocate() can return -EOPNOTSUPP without calling
   file->f_op->fallocate() if the mode is already known to be
   unsupported.

Tracking the verdicts per mode also fixes the following issue in v7:
clear_limits_pending is not per mode, so a failed discard suppresses
the error message for the first write-zeroes failure.

A few more comments about v7 that also apply to v8:
- loop_clear_limits() calls schedule_work() after clear_limits_lock has
   been unlocked. Can a request that passed the lo->lo_backing_file
   check queue the work item after cancel_work_sync() has been called
   by __loop_clr_fd() or loop_remove()? Since lo_free_disk() no longer
   cancels the work item, I think that would result in a use-after-free.
   How about calling schedule_work() while holding clear_limits_lock and
   resetting the state before calling cancel_work_sync() in
   __loop_clr_fd()?
- The code that resets clear_limits_mode occurs three times. Please
   introduce a helper function. Is the reset in
   loop_assign_backing_file() needed since __loop_clr_fd() already
   resets the state and since loop_clear_limits() ignores failures for
   a backing file that is no longer current?
- The work function freezes the queue and commits the queue limits even
   if no mode has been recorded. Please return early in that case.
- The changelog for v7 is missing. Please include a "Changes since v7"
   and a "Changes since v6" section in v8.

Thanks,

Bart.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 16:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24  3:12 [PATCH v7] loop: defer the queue limits clear to a workqueue Tao Cui
2026-11-24  8:10 ` Tao Cui
2026-11-24 16:56   ` 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

all inboxes | Powered by JetHome®