Skip to contentSkip to conversation

nvdimm/pmem: Release gendisk on probe failure

PATCH Superseded Hemanth Selam 2 participants 3 messages Aug 31

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

PATCH v2 1/1 nvdimm/pmem: Release gendisk on probe failure

pmem namespace probe allocates a gendisk early, fills it in, and only hands it to devres once device_add_disk() has succeeded. Until that handover the probe itself owns the disk, and the error paths in between release it through a common label.

Initializing the namespace's badblocks state sits between those two points but returns directly on failure, so the gendisk, its queue and its bdev inode are left allocated with nothing to free them. The devres release action has not been registered at that point, so unbinding or destroying the namespace does not reclaim them either, and they stay allocated for the rest of the boot. Nothing is user visible, as the disk was never added, so the only effect is the memory.

devm_init_badblocks() fails only if a one page GFP_KERNEL allocation fails, which needs memory exhaustion, so the path is hard to reach in practice.

Release the gendisk through the existing cleanup path on this failure.

Fixes 0caeef63e6d2 ("libnvdimm: Add a poison list and export badblocks")
Assisted-by Cursor:claude-opus-5

---
Changes in v2, all from Alison's review of v1:
- Retitled, and the changelog rewritten as background, problem, impact and resolution rather than a walk through the error labels.
- Fixes: re-derived. b95f5f4391fa only replaced one failing call with another; the early return that leaks the disk was added a week earlier by 0caeef63e6d2, when the disk was already allocated by alloc_disk_node() and the only put_disk() was in the detach path.
- Says what the leak's fate is: the devres release action is registered only after device_add_disk(), so nothing reclaims the disk on unbind.
- The object counts are gone, see below for why.

How this was found: reviewing pmem's probe error paths with an AI assistant, hence the Assisted-by tag. The finding was then confirmed against the code and measured as follows.

How it was tested: this is a unit test of an error path that needs memory exhaustion to reach, not a user visible scenario. The badblocks init in pmem_attach_disk() was forced to fail with a throwaway kernel change, a namespace was bound 32 times in a guest with memmap=256M!1G, and disk_release() calls were counted with ftrace's function profiler:

without the patch with the patch
disk_release() calls 0 of 32 32 of 32
bdev_cache active 48 -> 72 (+24) 48 -> 48 (+0)

v1 quoted the bdev_cache delta, which is what drew the question about 64 attempts and 60 objects: slab accounting does not track this one for one, as the +24 above shows for 32 probes. disk_release() does, so that is the number reported here and the slab figure is only context.

 drivers/nvdimm/pmem.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/nvdimm/pmem.c +4 -2
index 30a51c365ce8..648fc7d66063 100644
@@ -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;
 
Signature · 1 line
2.43.7
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] NULL pointer dereference in sysfs attributes (write_cache) when alloc_dax() fails with -EOPNOTSUPP --

commit b72cbfedec624805721d1461cccfb6dbba985b5b
Author: Hemanth Selam <[email protected]>

nvdimm/pmem: Release gendisk on probe failure

This commit fixes a memory leak in the pmem namespace probe by ensuring the gendisk is properly released through the common cleanup path if devm_init_badblocks() fails.

Quoted 6 lines
drivers/nvdimm/pmem.c +0 -0
index 30a51c365ce8b..648fc7d660639 100644
@@ -563,8 +563,10 @@ static int pmem_attach_disk(struct device *dev,
 	nvdimm_namespace_disk_name(ndns, disk->disk_name);

drivers/nvdimm/pmem.c static int pmem_attach_disk(struct device *dev, · line 571

Quoted 10 lines
drivers/nvdimm/pmem.c +4 -2
 	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;

[Severity: High]
This isn't a bug introduced by this patch, but I wanted to ask about the error handling for alloc_dax() just below this code.

In pmem_attach_disk(), if alloc_dax() fails with -EOPNOTSUPP, the code gracefully falls back and leaves pmem->dax_dev as NULL:

	dax_dev = alloc_dax(pmem, &pmem_dax_ops);
	if (IS_ERR(dax_dev)) {
		rc = PTR_ERR(dax_dev);
		if (rc != -EOPNOTSUPP)
			goto out;
	} else {
		...
	}
	rc = device_add_disk(dev, disk, pmem_attribute_groups);

However, the write_cache sysfs attribute remains unconditionally visible as long as CONFIG_ARCH_HAS_PMEM_API is enabled.

If a user reads this attribute, write_cache_show() will pass the NULL pmem->dax_dev to dax_write_cache_enabled():

static ssize_t write_cache_show(struct device *dev,
struct device_attribute *attr, char *buf)

{
	struct pmem_device *pmem = dev_to_disk(dev)->private_data;

	return sprintf(buf, "%d\n", !!dax_write_cache_enabled(pmem->dax_dev));
}

Similarly, write_cache_store() passes it to dax_write_cache():

static ssize_t write_cache_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t len)

{
	...
	dax_write_cache(pmem->dax_dev, write_cache);
	return len;
}

Since dax_write_cache_enabled() (in drivers/dax/super.c) and dax_write_cache() blindly dereference the pointer, won't this cause a NULL pointer dereference leading to a kernel panic if the attribute is accessed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
Quoted 5 lines · On Mon, Aug 31, 2026 at 04:45:08AM +0000, [email protected] wrote:

This isn't a bug introduced by this patch, but I wanted to ask about the error handling for alloc_dax() just below this code.

In pmem_attach_disk(), if alloc_dax() fails with -EOPNOTSUPP, the code gracefully falls back and leaves pmem->dax_dev as NULL:

[...]

Quoted 2 lines

However, the write_cache sysfs attribute remains unconditionally visible as long as CONFIG_ARCH_HAS_PMEM_API is enabled.

I do not think the two can meet. alloc_dax() only returns -EOPNOTSUPP for

	if (ops && cpu_dcache_is_aliasing())
		return ERR_PTR(-EOPNOTSUPP);

so pmem->dax_dev is left NULL only on architectures whose data caches are virtually aliased: csky, m68k, parisc and sh unconditionally, arm and mips depending on the CPU.

write_cache is visible only when CONFIG_ARCH_HAS_PMEM_API is selected, and that is arm64, powerpc, riscv and x86_64 (X86_64 only), none of which have aliasing data caches. So on every architecture where alloc_dax() can hand back -EOPNOTSUPP, dax_visible() returns 0 for write_cache and there is nothing to read.

Happy to be shown otherwise if there is a configuration I am missing, in which case the fix belongs in dax_visible() rather than in this patch.

Hemanth