Skip to contentSkip to conversation

nvdimm/pmem: Fix disk resource cleanup on initialization failure

PATCH In review [email protected] 2 participants 3 messages Nov 4 – Nov 10

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvdimm/pmem: Fix disk resource cleanup on initialization failure

From: Mohammad Shahid <[email protected]>

pmem_attach_disk() allocates a disk using blk_alloc_disk() before initializing badblocks.

If devm_init_badblocks() fails, the function return directly without releasing the previously allocated disk.

Use the existing cleanup path to release the disk and preserve the original error code returned by devm_init_badblocks().

---
 drivers/nvdimm/pmem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/nvdimm/pmem.c +3 -2
index 5fb86595e8bd..76e80eccc8e7 100644
@@ -563,8 +563,9 @@ 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;
+	rc = devm_init_badblocks(dev, &pmem->bb);
+	if (rc)
+		goto out;
 	nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_range);
 	disk->bb = &pmem->bb;
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 10 lines · On Wed, Nov 04, 2026 at 09:48:25PM +0530, [email protected] wrote:

From: Mohammad Shahid <[email protected]>

pmem_attach_disk() allocates a disk using blk_alloc_disk() before initializing badblocks.

If devm_init_badblocks() fails, the function return directly without releasing the previously allocated disk.

Use the existing cleanup path to release the disk and preserve the original error code returned by devm_init_badblocks().

Hi Mohammad,

Your patch looks right but this leak is fixed in v7.4-rc1 by:
855a46681e0b ("nvdimm/pmem: Release gendisk on probe failure")

Basing and building your patches on the latest tree, usually the most recent mainline -rc before sending helps avoid this. Sometimes a patch is also just on the mailing list, so check the list too.

-- Alison

Quoted 25 lines
---
 drivers/nvdimm/pmem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/nvdimm/pmem.c +3 -2
index 5fb86595e8bd..76e80eccc8e7 100644
@@ -563,8 +563,9 @@ 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;
+	rc = devm_init_badblocks(dev, &pmem->bb);
+	if (rc)
+		goto out;
 	nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_range);
 	disk->bb = &pmem->bb;
 
Signature · 1 line
2.43.0
#2 lore raw

Thanks Alison .

Lesson learned !!!

Quoted 51 lines · Alison Schofield
Quoted 10 lines · On Wed, Nov 04, 2026 at 09:48:25PM +0530, [email protected] wrote:

From: Mohammad Shahid <[email protected]>

pmem_attach_disk() allocates a disk using blk_alloc_disk() before initializing badblocks.

If devm_init_badblocks() fails, the function return directly without releasing the previously allocated disk.

Use the existing cleanup path to release the disk and preserve the original error code returned by devm_init_badblocks().

Hi Mohammad,

Your patch looks right but this leak is fixed in v7.4-rc1 by:
855a46681e0b ("nvdimm/pmem: Release gendisk on probe failure")

Basing and building your patches on the latest tree, usually the most recent mainline -rc before sending helps avoid this. Sometimes a patch is also just on the mailing list, so check the list too.

-- Alison

Quoted 25 lines
---
 drivers/nvdimm/pmem.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/nvdimm/pmem.c +3 -2
index 5fb86595e8bd..76e80eccc8e7 100644
@@ -563,8 +563,9 @@ 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;
+	rc = devm_init_badblocks(dev, &pmem->bb);
+	if (rc)
+		goto out;
 	nvdimm_badblocks_populate(nd_region, &pmem->bb, &bb_range);
 	disk->bb = &pmem->bb;
 
Signature · 1 line
2.43.0