From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.10 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796285832; b=diu4//jOEvEkpADzuci1BS/p2VDReE5Y1n866yv+07XalvfKsxgJDrI2vTWasDgNU9Xv Q4fCxTFdWhWmRlxaMIVUr2Z2K8rsMnxCwKY2gHep1kcLHmni2BQwWmUjHksUWGjMpm3fm VzZUCWStH4c6tZvMqLxYejhp8nDlrx0BzCsLROM//92OX1iG9Ko2gDlyloIXmys50FnSG VLA0uPatm7cVh6eZ7SOA8NWT8BbSKcBCk6WJJ67w1JUMNztFYij1pSFJcQHgAQ2HTPlpa S//zxGLVgzNh20yrn0lD88qaIlGjNL6GNlDDrdu6ad5cJTGkTKMR9vCVhJBhqIFCzoQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796285832; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=4GYgl/2Bmp8+/Sdzv4LPtaRo1boTbwBOlbUDSwi6EqI=; b=rbp00OOhiyckjsxu2ycmhdIiZ9dWdpo8X/xsaEZGvROFCy+Min3HBFlqyvLx9RWjmpKt erjwaQLK3yuxN6s8XTQUQV1sXXvxZJ/xcFf0X5pVKZ+Rl4FeCZ30FsDQFL+4RtViC3zLU jKFDsZI8SHmdhCWZGoPNU+Se2YUxqVjklStrbxQHdspzNfqlnqzqtCqlspzXbQYS85Fwk 0HGlRtssAk50iutwV9ZiLFo1GhaMuk3bQRr2w2NTZVlBH5VxdqW4IBBeaO+xKClzkJOFN 1mdQzGVIGtqgMbc3Ig5o1TJdoiKKKCH3o7V/D1FrehSvfqKfc86Yr7O1+TXBekrrThA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=kVpgrGmD; arc=none smtp.remote-ip=95.215.58.10 Received: from mta1.migadu.com (out-10.mta1.migadu.com [95.215.58.10]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85B4C606A330 for ; Thu, 3 Dec 2026 08:17:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 85B4C606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=kVpgrGmD X-Envelope-To: linux-block@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=GOsHEvD6De1LS+foj8yKzmTmB60QV4J0HYjISLeqmqw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796285829; v=1; x=1796890629; b=kVpgrGmDvgTYAOO+s6k2AKI9VrLihUm4FT2zn0lPf6Rd70tGYHMrWFjqOJPvS3Efff9vu/z0 3ofSM1pumj45xfXnNV+IrgUgNhgMhvySnKIKz7dOg1EY4jePRMZt289qodMprF9lZcZWFP5haUR ULXglNfAqiIx+swV6hJEdhLU= X-Envelope-To: linux-block@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id afe2f8703e112344; Thu, 03 Dec 2026 08:17:09 +0000 X-Mizu-Trace-ID: afe2f8703e112344 X-Migadu-Flow: FLOW_OUT From: Tao Cui To: Bart Van Assche , axboe@kernel.dk Cc: hch@lst.de, Tetsuo Handa , cuitao@kylinos.cn, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, cui.tao@linux.dev, Shin'ichiro Kawasaki Subject: [PATCH v10] loop: defer the queue limits clear to a workqueue Date: Thu, 3 Dec 2026 16:16:57 +0800 Message-ID: <20261203081657.2577866-1-cui.tao@linux.dev> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261126054223.176824-1-cui.tao@linux.dev> References: <20261126054223.176824-1-cui.tao@linux.dev> X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Tao Cui 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 Signed-off-by: Tao Cui --- 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