mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shuai Liu <847807867@qq.com>
To: linux-mtd@lists.infradead.org
Cc: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com,
	syzbot+c66e7044aa73c620d862@syzkaller.appspotmail.com,
	linux-kernel@vger.kernel.org, Shuai Liu <847807867@qq.com>
Subject: [PATCH] mtd: blktrans: keep reclaim out of the ->release paths
Date: Tue, 17 Nov 2026 13:28:37 +0800	[thread overview]
Message-ID: <tencent_B9AEB406CBA2FC785312AE8158D20489FB06@qq.com> (raw)
In-Reply-To: <6afb28e0.d140b701.164b01.0001.GAE@google.com>

blktrans_release() invokes tr->release() while holding dev->lock, and
translators can walk down into the flash driver from there:
mtdblock_release() flushes its write cache via write_cached_data() ->
erase_write() -> mtd_erase(), and concat_erase() allocates a struct
erase_info with GFP_KERNEL. del_mtd_blktrans_dev() calls ->release()
under the same lock on the device removal path.

Reclaim from those allocations can recurse back into the same block
queue and deadlock. lockdep reports the full cycle when a write to the
concat device is flushed at last close:

    init/1 is trying to acquire lock:
      fs_reclaim, at: __kmalloc_cache_noprof
    but task is already holding lock:
      &mtdblk->cache_mutex, at: mtdblock_release

    Chain exists of:
      fs_reclaim --> &new->lock --> &mtdblk->cache_mutex

    the existing dependency chain (in reverse order) is:
      fs_reclaim -> q_usage_counter(io) -> elevator_lock
        -> set->srcu -> new->lock -> mtdblk->cache_mutex

    Call Trace:
       concat_erase
       mtd_erase
       erase_write
       write_cached_data
       mtdblock_release
       blktrans_release
       bdev_release

The io path is already protected: mtd_queue_rq() disables reclaim
across dispatch with memalloc_noio_save(), because translators may
allocate while processing requests under dev->lock and the srcu read
side. ->release() runs under the same locks, so give it the same
treatment in blktrans_release() and del_mtd_blktrans_dev().

Verified with mtdram + block2mtd + concat + mtdblock on QEMU
(CONFIG_PROVE_LOCKING): writing through /dev/mtdblockN and closing
the fd produces the warning above on the unpatched kernel and stays
silent with this patch, with the readback still succeeding.

Reported-by: syzbot+c66e7044aa73c620d862@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c66e7044aa73c620d862
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 drivers/mtd/mtd_blkdevs.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index 42f250344f0cd..be4ead01f5210 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -233,6 +233,7 @@ static int blktrans_open(struct gendisk *disk, blk_mode_t mode)
 static void blktrans_release(struct gendisk *disk)
 {
 	struct mtd_blktrans_dev *dev = disk->private_data;
+	unsigned int noio_flags;
 
 	mutex_lock(&dev->lock);
 
@@ -242,8 +243,12 @@ static void blktrans_release(struct gendisk *disk)
 	module_put(dev->tr->owner);
 
 	if (dev->mtd) {
-		if (dev->tr->release)
+		if (dev->tr->release) {
+			/* Reclaim must not recurse into I/O while releasing the device. */
+			noio_flags = memalloc_noio_save();
 			dev->tr->release(dev);
+			memalloc_noio_restore(noio_flags);
+		}
 		__put_mtd_device(dev->mtd);
 	}
 unlock:
@@ -410,6 +415,7 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
 {
 	unsigned long flags;
 	unsigned int memflags;
+	unsigned int noio_flags;
 
 	lockdep_assert_held(&mtd_table_mutex);
 
@@ -435,8 +441,11 @@ int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
 		then put mtd device, and don't touch it again */
 	mutex_lock(&old->lock);
 	if (old->open) {
-		if (old->tr->release)
+		if (old->tr->release) {
+			noio_flags = memalloc_noio_save();
 			old->tr->release(old);
+			memalloc_noio_restore(noio_flags);
+		}
 		__put_mtd_device(old->mtd);
 	}
 

base-commit: 84abb955b1015477aa11a96e2f3ac1b938b77f48
-- 
2.43.0


      reply	other threads:[~2026-11-17  5:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 16:01 [syzbot] [mtd?] possible deadlock in blktrans_release syzbot
2026-11-17  5:28 ` Shuai Liu [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=tencent_B9AEB406CBA2FC785312AE8158D20489FB06@qq.com \
    --to=847807867@qq.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=syzbot+c66e7044aa73c620d862@syzkaller.appspotmail.com \
    --cc=vigneshr@ti.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®