The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Tao Cui <cui.tao@linux.dev>
To: Bart Van Assche <bvanassche@acm.org>, axboe@kernel.dk
Cc: hch@lst.de, Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>,
	cuitao@kylinos.cn, linux-block@vger.kernel.org,
	linux-kernel@vger.kernel.org, cui.tao@linux.dev,
	Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Subject: [PATCH v10] loop: defer the queue limits clear to a workqueue
Date: Thu,  3 Dec 2026 16:16:57 +0800	[thread overview]
Message-ID: <20261203081657.2577866-1-cui.tao@linux.dev> (raw)
In-Reply-To: <20261126054223.176824-1-cui.tao@linux.dev>

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: queue a dynamically allocated work item on a
dedicated workqueue, holding references to the backing file and to
the request queue so the work item can detect unbind and rebind by
identity, and skip the update if the device is no longer bound to
that file.  The clear workqueue is allocated once per device, like
lo->workqueue, and lo_free_disk() drains it, so the work item cannot
outlive the device and the pinned queue keeps it addressable while
it runs.  The work item takes the queue limits lock before freezing,
matching the order used by loop_set_dio() and loop_set_block_size(),
and commits only when a mode actually needs clearing.

Each binding reports its first failure to the console; repeats are
completed quietly.  A failure whose work item could not be queued is
not counted as reported, so later failures stay audible.  The
reported bit resets when the backing file is detached.

Tested with PROVE_LOCKING enabled: loop/011, the concurrency sweep
and the mkfs/unbind stress run clean.

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 v9:
- convert to Bart's simpler architecture: instead of tracking
  unsupported-mode verdicts, queue a dynamically allocated work item
  on a dedicated workqueue.  The work item pins the backing file and
  the request queue, skips the update when the device is no longer
  bound to that file, gates on the hardware limits at execution time,
  re-validates the binding after freezing the queue, and commits only
  when a mode actually needs clearing.  This removes the
  clear_limits_seen tracking, the clear_limits_lock mutex, the
  cancel_work_sync() calls in __loop_clr_fd(), lo_free_disk() and
  loop_remove(), and the loop_set_block_size() verdict reset -- none
  of that state exists anymore (Bart review of v9; queueing the work
  item off the request path per Bart's follow-up, with the workqueue
  allocated once per device and drained by lo_free_disk())
- keep the console-once semantics with a single reported bit instead of
  the verdict bitmap: the first failure per binding completes without
  RQF_QUIET, repeats are quiet, and the bit resets on unbind; a failure
  whose work item could not be queued stays audible so an allocation
  failure cannot silence the device forever
- drop a work item whose backing file pointer is NULL instead of
  recording a verdict against a detached file

 drivers/block/loop.c | 129 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 116 insertions(+), 13 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 194f51d9dab6..c59eb15fff61 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -67,6 +67,8 @@ struct loop_device {
 	struct list_head        rootcg_cmd_list;
 	struct list_head        idle_worker_list;
 	struct rb_root          worker_tree;
+	struct workqueue_struct *clear_wq;
+	unsigned long		clear_limits_reported;
 	struct timer_list       timer;
 	bool			sysfs_inited;
 
@@ -222,26 +224,107 @@ 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; @q keeps the request queue
+ * alive if the disk is removed while the work item is pending.
+ */
+struct loop_clear_limits_work {
+	struct work_struct	clear_limits_work;
+	struct loop_device	*lo;
+	struct request_queue	*q;
+	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;
+	/* Fast skip without holding lo_mutex across the freeze below. */
+	if (data_race(READ_ONCE(lo->lo_state)) != Lo_bound ||
+	    data_race(READ_ONCE(lo->lo_backing_file)) != w->lo_backing_file)
+		goto out;
 
-	if (mode & FALLOC_FL_PUNCH_HOLE) {
+	lim = queue_limits_start_update(w->q);
+	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;
 	}
+	if (!changed) {
+		queue_limits_cancel_update(w->q);
+		goto out;
+	}
+	memflags = blk_mq_freeze_queue(w->q);
 
 	/*
-	 * 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.
+	 * Re-validate after the freeze: the device may have been unbound
+	 * or rebound while the drain was running.  The pinned reference
+	 * keeps the comparison stable.
 	 */
-	queue_limits_commit_update(lo->lo_queue, &lim);
+	if (data_race(READ_ONCE(lo->lo_state)) != Lo_bound ||
+	    data_race(READ_ONCE(lo->lo_backing_file)) != w->lo_backing_file) {
+		queue_limits_cancel_update(w->q);
+		blk_mq_unfreeze_queue(w->q, memflags);
+		goto out;
+	}
+
+	queue_limits_commit_update(w->q, &lim);
+	blk_mq_unfreeze_queue(w->q, memflags);
+
+out:
+	blk_put_queue(w->q);
+	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 pins
+ * the backing file and the request queue so that both stay alive if the
+ * device is unbound or removed while the work item is pending, and
+ * lo_free_disk() drains the clear workqueue before freeing the
+ * loop_device.  Returns true if a work item was queued.
+ */
+static bool loop_clear_limits(struct loop_device *lo, struct file *file,
+			      int mode)
+{
+	struct loop_clear_limits_work *w;
+
+	if (!file)
+		return false;
+	w = kmalloc_obj(*w, GFP_NOIO);
+	if (!w)
+		return false;
+	if (!blk_get_queue(lo->lo_queue)) {
+		kfree(w);
+		return false;
+	}
+	w->lo = lo;
+	w->q = lo->lo_queue;
+	w->lo_backing_file = get_file(file);
+	w->mode = mode;
+	INIT_WORK(&w->clear_limits_work, loop_clear_limits_workfn);
+	if (!queue_work(lo->clear_wq, &w->clear_limits_work)) {
+		blk_put_queue(w->q);
+		fput(w->lo_backing_file);
+		kfree(w);
+		return false;
+	}
+	return true;
 }
 
 static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
@@ -268,8 +351,15 @@ static int lo_fallocate(struct loop_device *lo, struct request *rq, loff_t pos,
 	 * We initially configure the limits in a hope that fallocate is
 	 * supported and clear them here if that turns out not to be true.
 	 */
-	if (unlikely(ret == -EOPNOTSUPP))
-		loop_clear_limits(lo, mode);
+	if (unlikely(ret == -EOPNOTSUPP)) {
+		/*
+		 * Silence repeats only once a work item is actually queued,
+		 * so that allocation failure keeps later failures audible.
+		 */
+		if (loop_clear_limits(lo, file, mode) &&
+		    test_and_set_bit(0, &lo->clear_limits_reported))
+			rq->rq_flags |= RQF_QUIET;
+	}
 
 	return ret;
 }
@@ -950,6 +1040,16 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
 	    !file->f_op->write_iter)
 		lo->lo_flags |= LO_FLAGS_READ_ONLY;
 
+	if (!lo->clear_wq) {
+		lo->clear_wq = alloc_workqueue("loopc%d",
+						WQ_UNBOUND | WQ_MEM_RECLAIM,
+						0, lo->lo_number);
+		if (!lo->clear_wq) {
+			error = -ENOMEM;
+			goto out_unlock;
+		}
+	}
+
 	if (!lo->workqueue) {
 		lo->workqueue = alloc_workqueue("loop%d",
 						WQ_UNBOUND | WQ_FREEZABLE,
@@ -1034,6 +1134,7 @@ static void __loop_clr_fd(struct loop_device *lo)
 	filp = lo->lo_backing_file;
 	lo->lo_backing_file = NULL;
 	spin_unlock_irq(&lo->lo_lock);
+	clear_bit(0, &lo->clear_limits_reported);
 
 	lo->lo_device = NULL;
 	lo->lo_offset = 0;
@@ -1695,6 +1796,8 @@ static void lo_free_disk(struct gendisk *disk)
 {
 	struct loop_device *lo = disk->private_data;
 
+	if (lo->clear_wq)
+		destroy_workqueue(lo->clear_wq);
 	if (lo->workqueue)
 		destroy_workqueue(lo->workqueue);
 	loop_free_idle_workers(lo, true);
-- 
2.53.0


      parent reply	other threads:[~2026-12-03  8:17 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-11-30 10:20     ` Tao Cui
2026-11-30 14:04       ` Bart Van Assche
2026-11-30 14:57         ` Tao Cui
2026-11-30 16:34           ` Bart Van Assche
2026-12-01 13:14             ` Tao Cui
2026-12-02 15:13               ` Bart Van Assche
2026-12-03  8:08                 ` Tao Cui
2026-12-03  8:16 ` Tao Cui [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203081657.2577866-1-cui.tao@linux.dev \
    --to=cui.tao@linux.dev \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=cuitao@kylinos.cn \
    --cc=hch@lst.de \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    --cc=shinichiro.kawasaki@wdc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox