mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
To: LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH v6] block: avoid long sync_blockdev() under disk->open_mutex during parallel close
Date: Thu, 15 Oct 2026 20:18:04 +0900	[thread overview]
Message-ID: <06cd882d-7f4e-4697-b6b1-ba140f8ad4dd@I-love.SAKURA.ne.jp> (raw)
In-Reply-To: <8d188283-b5c7-4b16-95ee-6d985472c120@I-love.SAKURA.ne.jp>

Commit b849dd84b6cc ("bdev: Reduce time holding bd_mutex in sync in
blkdev_close()") introduced a sync_blockdev() call immediately before
holding disk->open_mutex. But this check was racy and best-effort basis.

If more than one thread concurrently called bdev_release() and saw
atomic_read(&bdev->bd_openers) > 1, the thread which actually hit
the atomic_dec_and_test(&bdev->bd_openers) condition will call
sync_blockdev() from blkdev_flush_mapping() (which could take several
minutes) with disk->open_mutex held.

Kill this race window and make sync_blockdev() always be called before
holding disk->open_mutex, by using a counter that tracks how many threads
are calling sync_blockdev() from bdev_release() without disk->open_mutex
held.

Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
---
 block/bdev.c              | 24 ++++++++++++++++++++++--
 include/linux/blk_types.h |  1 +
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/block/bdev.c b/block/bdev.c
index cd8323083740..cc16f0d7d496 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -1167,10 +1167,30 @@ void bdev_release(struct file *bdev_file)
 	 * of the world and we want to avoid long (could be several minute)
 	 * syncs while holding the mutex.
 	 */
-	if (atomic_read(&bdev->bd_openers) == 1)
+	mutex_lock(&disk->open_mutex);
+	if (atomic_read(&bdev->bd_openers) == 1 + bdev->bd_closers) {
+		struct block_device *whole = bdev_is_partition(bdev) ?
+			bdev_whole(bdev) : NULL;
+
+		bdev->bd_closers++;
+		if (whole)
+			whole->bd_closers++;
+		mutex_unlock(&disk->open_mutex);
 		sync_blockdev(bdev);
+		/*
+		 * Always sync the whole disk when closing a partition despite
+		 * we need to sync the whole disk only when we're also closing
+		 * the whole disk, for we can't serialize concurrent closing of
+		 * partitions using disk->open_mutex.
+		 */
+		if (whole)
+			sync_blockdev(whole);
+		mutex_lock(&disk->open_mutex);
+		bdev->bd_closers--;
+		if (whole)
+			whole->bd_closers--;
+	}
 
-	mutex_lock(&disk->open_mutex);
 	bdev_yield_write_access(bdev_file);
 
 	if (holder)
diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
index 98e21b4cbf32..04d378818c76 100644
--- a/include/linux/blk_types.h
+++ b/include/linux/blk_types.h
@@ -74,6 +74,7 @@ struct block_device {
 #ifdef CONFIG_SECURITY
 	void			*bd_security;
 #endif
+	int			bd_closers;
 	/*
 	 * keep this out-of-line as it's both big and not needed in the fast
 	 * path
-- 
2.56.0


  reply	other threads:[~2026-10-15 11:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 12:02 [PATCH] " Tetsuo Handa
2026-10-14 13:07 ` [PATCH v2] " Tetsuo Handa
2026-10-14 14:12   ` [PATCH v3] " Tetsuo Handa
2026-10-14 16:21     ` [PATCH v4] " Tetsuo Handa
2026-10-15 10:25       ` [PATCH v5] " Tetsuo Handa
2026-10-15 11:18         ` Tetsuo Handa [this message]
2026-10-16 15:40           ` [PATCH v7] " Tetsuo Handa
2026-10-19  2:29             ` kernel test robot

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=06cd882d-7f4e-4697-b6b1-ba140f8ad4dd@I-love.SAKURA.ne.jp \
    --to=penguin-kernel@i-love.sakura.ne.jp \
    --cc=linux-kernel@vger.kernel.org \
    /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®