From: Bart Van Assche <bvanassche@acm.org>
To: Tao Cui <cui.tao@linux.dev>,
axboe@kernel.dk, hch@lst.de,
Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: cuitao@kylinos.cn, linux-block@vger.kernel.org,
linux-kernel@vger.kernel.org,
Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Subject: Re: [PATCH v9] loop: defer the queue limits clear to a workqueue
Date: Thu, 26 Nov 2026 07:32:12 -0800 [thread overview]
Message-ID: <46cefe96-a193-40f9-bc1a-facc73cf6afa@acm.org> (raw)
In-Reply-To: <20261126135628.495497-1-cui.tao@linux.dev>
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;
}
prev parent reply other threads:[~2026-11-26 15:32 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 5:42 [PATCH v8] " 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 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=46cefe96-a193-40f9-bc1a-facc73cf6afa@acm.org \
--to=bvanassche@acm.org \
--cc=axboe@kernel.dk \
--cc=cui.tao@linux.dev \
--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
all inboxes | Powered by JetHome®