mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hemanth Selam <hemanth.selam@gmail.com>
To: djbw@kernel.org, vishal.l.verma@intel.com, dave.jiang@intel.com,
	alison.schofield@intel.com
Cc: iweiny@kernel.org, nvdimm@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH] nvdimm: pmem: fix gendisk leak when badblocks init fails
Date: Mon, 24 Aug 2026 11:31:13 +0530	[thread overview]
Message-ID: <20260824060113.2330468-1-hemanth.selam@gmail.com> (raw)

pmem_attach_disk() allocates the gendisk with blk_alloc_disk() and only
hands it over to devres at the very end, after device_add_disk() has
succeeded:

	if (devm_add_action_or_reset(dev, pmem_release_disk, pmem))
		return -ENOMEM;

Every error path in between either has nothing to release yet or jumps
to the out: label, which drops the last reference with put_disk().  The
devm_init_badblocks() failure returns directly instead, so on that path
the gendisk, its queue and its bdev inode are never freed.  The disk has
been allocated before this check since the check was introduced, so the
leak is as old as the check itself.

Forcing the branch and rebinding a namespace 64 times shows one gendisk
leaked per failed probe: disk_release() is never reached and bdev_cache
grows by 60 objects.  With the goto, disk_release() runs on every
attempt and bdev_cache returns to its original size.

Fixes: b95f5f4391fa ("libnvdimm: convert to statically allocated badblocks")
Signed-off-by: Hemanth Selam <hemanth.selam@gmail.com>
---
 drivers/nvdimm/pmem.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/nvdimm/pmem.c b/drivers/nvdimm/pmem.c
index 30a51c365ce8..648fc7d66063 100644
--- a/drivers/nvdimm/pmem.c
+++ b/drivers/nvdimm/pmem.c
@@ -563,8 +563,10 @@ static int pmem_attach_disk(struct device *dev,
 	nvdimm_namespace_disk_name(ndns, disk->disk_name);
 	set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
 			/ 512);
-	if (devm_init_badblocks(dev, &pmem->bb))
-		return -ENOMEM;
+	if (devm_init_badblocks(dev, &pmem->bb)) {
+		rc = -ENOMEM;
+		goto out;
+	}
 	nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_range);
 	disk->bb = &pmem->bb;
 
-- 
2.43.7


             reply	other threads:[~2026-08-24  6:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-24  6:01 Hemanth Selam [this message]
2026-08-27 22:51 ` Alison Schofield

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=20260824060113.2330468-1-hemanth.selam@gmail.com \
    --to=hemanth.selam@gmail.com \
    --cc=alison.schofield@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=djbw@kernel.org \
    --cc=iweiny@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=vishal.l.verma@intel.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®