Skip to contentSkip to conversation

libnvdimm: call devm_namespace_disable() on error

PATCH In review Hannes Reinecke 2 participants 4 messages Jul 3, 2020 – Jul 13, 2020

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

PATCH 1/1 libnvdimm: call devm_namespace_disable() on error

Once devm_namespace_enable() has been called the error path in the calling function will not call devm_namespace_disable(), leaving the namespace enabled on error.

---
 drivers/dax/pmem/core.c   |  2 +-
 drivers/nvdimm/btt.c      |  5 ++++-
 drivers/nvdimm/claim.c    |  8 +++++++-
 drivers/nvdimm/pfn_devs.c |  1 +
 drivers/nvdimm/pmem.c     | 20 ++++++++++----------
 5 files changed, 23 insertions(+), 13 deletions(-)
drivers/dax/pmem/core.c +1 -1
index 2bedf8414fff..4b26434f0aca 100644
@@ -31,9 +31,9 @@ struct dev_dax *__dax_pmem_probe(struct device *dev, enum dev_dax_subsys subsys)
 	if (rc)
 		return ERR_PTR(rc);
 	rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
+	devm_namespace_disable(dev, ndns);
 	if (rc)
 		return ERR_PTR(rc);
-	devm_namespace_disable(dev, ndns);
 
 	/* reserve the metadata area, device-dax will reserve the data */
 	pfn_sb = nd_pfn->pfn_sb;
drivers/nvdimm/btt.c +4 -1
index 48e9d169b6f9..bd4747f2c99b 100644
@@ -1704,13 +1704,16 @@ int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
 		dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
 				dev_name(&ndns->dev),
 				ARENA_MIN_SIZE + nd_btt->initial_offset);
+		devm_namespace_disable(&nd_btt->dev, ndns);
 		return -ENXIO;
 	}
 	nd_region = to_nd_region(nd_btt->dev.parent);
 	btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
 			nd_region);
-	if (!btt)
+	if (!btt) {
+		devm_namespace_disable(&nd_btt->dev, ndns);
 		return -ENOMEM;
+	}
 	nd_btt->btt = btt;
 
 	return 0;
drivers/nvdimm/claim.c +7 -1
index 45964acba944..15fd1b92d32f 100644
@@ -314,12 +314,18 @@ int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio,
 	}
 
 	ndns->rw_bytes = nsio_rw_bytes;
-	if (devm_init_badblocks(dev, &nsio->bb))
+	if (devm_init_badblocks(dev, &nsio->bb)) {
+		devm_release_mem_region(dev, res->start, size);
 		return -ENOMEM;
+	}
 	nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
 			&nsio->res);
 
 	nsio->addr = devm_memremap(dev, res->start, size, ARCH_MEMREMAP_PMEM);
+	if (IS_ERR(nsio->addr)) {
+		devm_exit_badblocks(dev, &nsio->bb);
+		devm_release_mem_region(dev, res->start, size);
+	}
 
 	return PTR_ERR_OR_ZERO(nsio->addr);
 }
drivers/nvdimm/pfn_devs.c +1 -0
index 34db557dbad1..9faa92662643 100644
@@ -408,6 +408,7 @@ static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
 				nsoff += chunk;
 			}
 			if (rc) {
+				devm_namespace_disable(&nd_pfn->dev, ndns);
 				dev_err(&nd_pfn->dev,
 					"error clearing %x badblocks at %llx\n",
 					num_bad, first_bad);
drivers/nvdimm/pmem.c +10 -10
index d25e66fd942d..4f667fe6ef72 100644
@@ -401,8 +401,10 @@ static int pmem_attach_disk(struct device *dev,
 	if (is_nd_pfn(dev)) {
 		nd_pfn = to_nd_pfn(dev);
 		rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
-		if (rc)
+		if (rc) {
+			devm_namespace_disable(dev, ndns);
 			return rc;
+		}
 	}
 
 	/* we're attaching a block device, disable raw namespace access */
@@ -549,17 +551,15 @@ static int nd_pmem_probe(struct device *dev)
 	ret = nd_pfn_probe(dev, ndns);
 	if (ret == 0)
 		return -ENXIO;
-	else if (ret == -EOPNOTSUPP)
-		return ret;
-
-	ret = nd_dax_probe(dev, ndns);
-	if (ret == 0)
-		return -ENXIO;
-	else if (ret == -EOPNOTSUPP)
-		return ret;
-
+	else if (ret != EOPNOTSUPP) {
+		ret = nd_dax_probe(dev, ndns);
+		if (ret == 0)
+			return -ENXIO;
+	}
 	/* probe complete, attach handles namespace enabling */
 	devm_namespace_disable(dev, ndns);
+	if (ret == -EOPNOTSUPP)
+		return ret;
 
 	return pmem_attach_disk(dev, ndns);
 }
Signature · 4 lines
2.16.4
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
#1 lore raw

Hi Hannes,

Quoted 79 lines · Hannes Reinecke

Once devm_namespace_enable() has been called the error path in the calling function will not call devm_namespace_disable(), leaving the namespace enabled on error.

---
 drivers/dax/pmem/core.c   |  2 +-
 drivers/nvdimm/btt.c      |  5 ++++-
 drivers/nvdimm/claim.c    |  8 +++++++-
 drivers/nvdimm/pfn_devs.c |  1 +
 drivers/nvdimm/pmem.c     | 20 ++++++++++----------
 5 files changed, 23 insertions(+), 13 deletions(-)
drivers/dax/pmem/core.c +1 -1
index 2bedf8414fff..4b26434f0aca 100644
@@ -31,9 +31,9 @@ struct dev_dax *__dax_pmem_probe(struct device *dev, enum dev_dax_subsys subsys)
 	if (rc)
 		return ERR_PTR(rc);
 	rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
+	devm_namespace_disable(dev, ndns);
 	if (rc)
 		return ERR_PTR(rc);
-	devm_namespace_disable(dev, ndns);
 
 	/* reserve the metadata area, device-dax will reserve the data */
 	pfn_sb = nd_pfn->pfn_sb;
drivers/nvdimm/btt.c +4 -1
index 48e9d169b6f9..bd4747f2c99b 100644
@@ -1704,13 +1704,16 @@ int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
 		dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
 				dev_name(&ndns->dev),
 				ARENA_MIN_SIZE + nd_btt->initial_offset);
+		devm_namespace_disable(&nd_btt->dev, ndns);
 		return -ENXIO;
 	}
 	nd_region = to_nd_region(nd_btt->dev.parent);
 	btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
 			nd_region);
-	if (!btt)
+	if (!btt) {
+		devm_namespace_disable(&nd_btt->dev, ndns);
 		return -ENOMEM;
+	}
 	nd_btt->btt = btt;
 
 	return 0;
drivers/nvdimm/claim.c +7 -1
index 45964acba944..15fd1b92d32f 100644
@@ -314,12 +314,18 @@ int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio,
 	}
 
 	ndns->rw_bytes = nsio_rw_bytes;
-	if (devm_init_badblocks(dev, &nsio->bb))
+	if (devm_init_badblocks(dev, &nsio->bb)) {
+		devm_release_mem_region(dev, res->start, size);
 		return -ENOMEM;
+	}
 	nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
 			&nsio->res);
 
 	nsio->addr = devm_memremap(dev, res->start, size, ARCH_MEMREMAP_PMEM);
+	if (IS_ERR(nsio->addr)) {
+		devm_exit_badblocks(dev, &nsio->bb);
+		devm_release_mem_region(dev, res->start, size);
+	}
 
 	return PTR_ERR_OR_ZERO(nsio->addr);
 }
drivers/nvdimm/pfn_devs.c +0 -0
index 34db557dbad1..9faa92662643 100644

drivers/nvdimm/pfn_devs.c static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn) · line 411

Quoted 4 lines
drivers/nvdimm/pfn_devs.c +1 -0
@@ -408,6 +408,7 @@ static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
 				nsoff += chunk;
 			}
 			if (rc) {
+				devm_namespace_disable(&nd_pfn->dev, ndns);

The disable here seems to be wrong.

This function called from the pmem_attach_disk path, where its expected the namespace is enabled after the setup_pfn. Also in case of an error, we do another disable in pmem_attach_disk.
Thanks,
Santosh

Quoted 49 lines
drivers/nvdimm/pfn_devs.c +0 -0
 				dev_err(&nd_pfn->dev,
 					"error clearing %x badblocks at %llx\n",
 					num_bad, first_bad);
drivers/nvdimm/pmem.c +10 -10
index d25e66fd942d..4f667fe6ef72 100644
@@ -401,8 +401,10 @@ static int pmem_attach_disk(struct device *dev,
 	if (is_nd_pfn(dev)) {
 		nd_pfn = to_nd_pfn(dev);
 		rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
-		if (rc)
+		if (rc) {
+			devm_namespace_disable(dev, ndns);
 			return rc;
+		}
 	}
 
 	/* we're attaching a block device, disable raw namespace access */
@@ -549,17 +551,15 @@ static int nd_pmem_probe(struct device *dev)
 	ret = nd_pfn_probe(dev, ndns);
 	if (ret == 0)
 		return -ENXIO;
-	else if (ret == -EOPNOTSUPP)
-		return ret;
-
-	ret = nd_dax_probe(dev, ndns);
-	if (ret == 0)
-		return -ENXIO;
-	else if (ret == -EOPNOTSUPP)
-		return ret;
-
+	else if (ret != EOPNOTSUPP) {
+		ret = nd_dax_probe(dev, ndns);
+		if (ret == 0)
+			return -ENXIO;
+	}
 	/* probe complete, attach handles namespace enabling */
 	devm_namespace_disable(dev, ndns);
+	if (ret == -EOPNOTSUPP)
+		return ret;
 
 	return pmem_attach_disk(dev, ndns);
 }
Signature · 4 lines
2.16.4
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]

#2 lore raw
Quoted 90 lines · Santosh Sivaraj

Hi Hannes,

Quoted 79 lines · Hannes Reinecke

Once devm_namespace_enable() has been called the error path in the calling function will not call devm_namespace_disable(), leaving the namespace enabled on error.

---
  drivers/dax/pmem/core.c   |  2 +-
  drivers/nvdimm/btt.c      |  5 ++++-
  drivers/nvdimm/claim.c    |  8 +++++++-
  drivers/nvdimm/pfn_devs.c |  1 +
  drivers/nvdimm/pmem.c     | 20 ++++++++++----------
  5 files changed, 23 insertions(+), 13 deletions(-)
drivers/dax/pmem/core.c +1 -1
index 2bedf8414fff..4b26434f0aca 100644
@@ -31,9 +31,9 @@ struct dev_dax *__dax_pmem_probe(struct device *dev, enum dev_dax_subsys subsys)
  	if (rc)
  		return ERR_PTR(rc);
  	rc = nvdimm_setup_pfn(nd_pfn, &pgmap);
+	devm_namespace_disable(dev, ndns);
  	if (rc)
  		return ERR_PTR(rc);
-	devm_namespace_disable(dev, ndns);
 
  	/* reserve the metadata area, device-dax will reserve the data */
  	pfn_sb = nd_pfn->pfn_sb;
drivers/nvdimm/btt.c +4 -1
index 48e9d169b6f9..bd4747f2c99b 100644
@@ -1704,13 +1704,16 @@ int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns)
  		dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n",
  				dev_name(&ndns->dev),
  				ARENA_MIN_SIZE + nd_btt->initial_offset);
+		devm_namespace_disable(&nd_btt->dev, ndns);
  		return -ENXIO;
  	}
  	nd_region = to_nd_region(nd_btt->dev.parent);
  	btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid,
  			nd_region);
-	if (!btt)
+	if (!btt) {
+		devm_namespace_disable(&nd_btt->dev, ndns);
  		return -ENOMEM;
+	}
  	nd_btt->btt = btt;
 
  	return 0;
drivers/nvdimm/claim.c +7 -1
index 45964acba944..15fd1b92d32f 100644
@@ -314,12 +314,18 @@ int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio,
  	}
 
  	ndns->rw_bytes = nsio_rw_bytes;
-	if (devm_init_badblocks(dev, &nsio->bb))
+	if (devm_init_badblocks(dev, &nsio->bb)) {
+		devm_release_mem_region(dev, res->start, size);
  		return -ENOMEM;
+	}
  	nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
  			&nsio->res);
 
  	nsio->addr = devm_memremap(dev, res->start, size, ARCH_MEMREMAP_PMEM);
+	if (IS_ERR(nsio->addr)) {
+		devm_exit_badblocks(dev, &nsio->bb);
+		devm_release_mem_region(dev, res->start, size);
+	}
 
  	return PTR_ERR_OR_ZERO(nsio->addr);
  }
drivers/nvdimm/pfn_devs.c +0 -0
index 34db557dbad1..9faa92662643 100644

drivers/nvdimm/pfn_devs.c static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn) · line 411

Quoted 4 lines
drivers/nvdimm/pfn_devs.c +1 -0
@@ -408,6 +408,7 @@ static int nd_pfn_clear_memmap_errors(struct nd_pfn *nd_pfn)
  				nsoff += chunk;
  			}
  			if (rc) {
+				devm_namespace_disable(&nd_pfn->dev, ndns);

The disable here seems to be wrong.

Quoted 3 lines

This function called from the pmem_attach_disk path, where its expected the namespace is enabled after the setup_pfn. Also in case of an error, we do another disable in pmem_attach_disk.

Oh. Correct. Will be fixing it up.

Cheers,

Hannes

Signature · 7 lines
Dr. Hannes Reinecke            Teamlead Storage & Networking
[email protected]                               +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
#3 lore raw
In reply to Hannes Reinecke

Hi! Hannes,

Quoted 13 lines · Hannes Reinecke

Once devm_namespace_enable() has been called the error path in the calling function will not call devm_namespace_disable(), leaving the namespace enabled on error.

---
 drivers/dax/pmem/core.c   |  2 +-
 drivers/nvdimm/btt.c      |  5 ++++-
 drivers/nvdimm/claim.c    |  8 +++++++-
 drivers/nvdimm/pfn_devs.c |  1 +
 drivers/nvdimm/pmem.c     | 20 ++++++++++----------
 5 files changed, 23 insertions(+), 13 deletions(-)

[snip]

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

Quoted 13 lines
drivers/nvdimm/pmem.c +3 -1
index d25e66fd942d..4f667fe6ef72 100644
@@ -401,8 +401,10 @@ static int pmem_attach_disk(struct device *dev,
 	if (is_nd_pfn(dev)) {
 		nd_pfn = to_nd_pfn(dev);
 		rc = nvdimm_setup_pfn(nd_pfn, &pmem->pgmap);
-		if (rc)
+		if (rc) {
+			devm_namespace_disable(dev, ndns);
 			return rc;
+		}

As mentioned in the previous mail, when rc != 0, disable is called again here.

Quoted 7 lines
drivers/nvdimm/pmem.c +0 -0
 	}
 
 	/* we're attaching a block device, disable raw namespace access */
@@ -549,17 +551,15 @@ static int nd_pmem_probe(struct device *dev)
 	ret = nd_pfn_probe(dev, ndns);
 	if (ret == 0)
 		return -ENXIO;

drivers/nvdimm/pmem.c static int nd_pmem_probe(struct device *dev) · line 554

Quoted 10 lines
drivers/nvdimm/pmem.c +1 -9
-	else if (ret == -EOPNOTSUPP)
-		return ret;
-
-	ret = nd_dax_probe(dev, ndns);
-	if (ret == 0)
-		return -ENXIO;
-	else if (ret == -EOPNOTSUPP)
-		return ret;
-
+	else if (ret != EOPNOTSUPP) {

^ -EOPNOTSUPP

Thanks,
Santosh

Quoted 16 lines
drivers/nvdimm/pmem.c +6 -0
+		ret = nd_dax_probe(dev, ndns);
+		if (ret == 0)
+			return -ENXIO;
+	}
 	/* probe complete, attach handles namespace enabling */
 	devm_namespace_disable(dev, ndns);
+	if (ret == -EOPNOTSUPP)
+		return ret;
 
 	return pmem_attach_disk(dev, ndns);
 }
Signature · 4 lines
2.16.4
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]

_______________________________________________
Linux-nvdimm mailing list -- [email protected] To unsubscribe send an email to [email protected]