[PATCH 8/8] cxl/region: Unregister the pmem region bridge on setup failure

Guixin Liu <[email protected]>
Newsgroups org.kernel.vger.linux-cxl
Message-ID <[email protected]>
When the nvdimm bridge has no driver bound, devm_cxl_add_pmem_region()
bails out with -ENXIO through the err_bridge label. That label only drops
the reference on @cxl_nvb, so the cxl_pmem_region device that device_add()
just published stays in sysfs forever: there is no device_del() and no
put_device(), and no devm action was registered to do either later.

Beyond the leak, cxlr->cxlr_pmem is left pointing at the stale device, so
a later probe of the same region allocates a second one and fails in
device_add() with -EEXIST on the duplicate "pmem_region%d" name.

Call cxlr_pmem_unregister() explicitly in that branch. It runs under the
bridge's device lock held by the scoped_guard(), which is what its
device_lock_assert() expects, and it performs the same teardown the devm
action would have done, including clearing cxlr->cxlr_pmem, so err_bridge
only has the @cxl_nvb reference left to drop.

Fixes: f17b558d6663 ("cxl/pmem: Refactor nvdimm device registration, delete the workqueue")
Signed-off-by: Guixin Liu <[email protected]>
---
 drivers/cxl/core/region_pmem.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/cxl/core/region_pmem.c b/drivers/cxl/core/region_pmem.c
index 23d97e3d78b6..7ab1373a95e0 100644
--- a/drivers/cxl/core/region_pmem.c
+++ b/drivers/cxl/core/region_pmem.c
@@ -168,12 +168,14 @@ int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
 		dev_name(dev));
 
 	scoped_guard(device, &cxl_nvb->dev) {
-		if (cxl_nvb->dev.driver)
+		if (cxl_nvb->dev.driver) {
 			rc = devm_add_action_or_reset(&cxl_nvb->dev,
 						      cxlr_pmem_unregister,
 						      cxlr_pmem);
-		else
+		} else {
 			rc = -ENXIO;
+			cxlr_pmem_unregister(cxlr_pmem);
+		}
 	}
 
 	if (rc)
-- 
2.43.7
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.