From: Tao Cui <cui.tao@linux.dev>
To: Bart Van Assche <bvanassche@acm.org>
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>,
Jens Axboe <axboe@kernel.dk>, Christoph Hellwig <hch@lst.de>,
Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>,
cuitao@kylinos.cn, cui.tao@linux.dev
Subject: Re: [PATCH v9] loop: defer the queue limits clear to a workqueue
Date: Mon, 30 Nov 2026 18:20:08 +0800 [thread overview]
Message-ID: <0523850b-2049-41ff-8dfa-ca3a8f055a5f@linux.dev> (raw)
In-Reply-To: <46cefe96-a193-40f9-bc1a-facc73cf6afa@acm.org>
Hello, Bart,
在 2026/11/26 23:32, Bart Van Assche 写道:
> 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?
>
v10 will below switches to your suggestion. It is indeed much simpler: a
dynamically allocated work item carries a reference to the backing
file, so unbind and rebind are detected by comparing against that
pinned file under lo->lo_mutex, and the clear_limits_seen tracking,
the clear_limits_lock mutex and all three cancel_work_sync() calls
are gone.
I first implemented your sketch as-is. That removed all of the shared
state used to track queue-limit updates. The only shared state left
is a single reported bit for one separate requirement: loop/011
expects exactly one console report per binding, which is inherently a
cross-request property. Without that bit, concurrent failures against
the same binding can all report before the first work item updates the
queue limits. The first failure per binding is left without
RQF_QUIET, repeats are marked quiet, and the bit is reset on unbind.
If the loop device is already unbound, no backing file reference is
taken and the work item is simply discarded.
I'm planning to send this as v10 unless you see a problem with the
design. Tested with PROVE_LOCKING enabled: loop/011, the concurrency
sweep and the mkfs/unbind stress run clean.
Thanks,
Tao
> 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;
> }
next prev parent reply other threads:[~2026-11-30 10:20 UTC|newest]
Thread overview: 7+ 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 [this message]
2026-11-30 14:04 ` Bart Van Assche
2026-11-30 14:57 ` Tao Cui
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=0523850b-2049-41ff-8dfa-ca3a8f055a5f@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