Skip to contentSkip to conversation

bugfix and optimize for drivers/nvdimm

PATCH Superseded Zhen Lei 3 participants 12 messages Aug 20, 2020 – Aug 27, 2020

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

v3 cover bugfix and optimize for drivers/nvdimm

v2 --> v3:
1. Fix spelling error of patch 1 subject: memmory --> memory
2. Add "Reviewed-by: Oliver O'Halloran <[email protected]>" into patch 1
3. Rewrite patch descriptions of Patch 1, 3, 4
4. Add 3 new trivial patches 5-7, I just found that yesterday.
5. Unify all "subsystem" names to "libnvdimm:"

v1 --> v2:
1. Add Fixes for Patch 1-2
2. Slightly change the subject and description of Patch 1
3. Add a new trivial Patch 4, I just found that yesterday.

v1:
I found a memleak when I learned the drivers/nvdimm code today. And I also added a sanity check for priv->bus_desc.provider_name, because strdup() maybe failed. Patch 3 is a trivial source code optimization.

Zhen Lei (7):
libnvdimm: fix memory leaks in of_pmem.c
libnvdimm: add sanity check for provider_name in
of_pmem_region_probe()
libnvdimm: simplify walk_to_nvdimm_bus()
libnvdimm: reduce an unnecessary if branch in nd_region_create()
libnvdimm: reduce an unnecessary if branch in nd_region_activate()
libnvdimm: make sure EXPORT_SYMBOL_GPL(nvdimm_flush) close to its
function
libnvdimm: slightly simplify available_slots_show()

 drivers/nvdimm/bus.c         |  7 +++----
 drivers/nvdimm/dimm_devs.c   |  5 ++---
 drivers/nvdimm/of_pmem.c     |  7 +++++++
 drivers/nvdimm/region_devs.c | 13 ++++---------
 4 files changed, 16 insertions(+), 16 deletions(-)
Signature · 5 lines
1.8.3

_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
author
#1 lore raw

PATCH v3 1/7 libnvdimm: fix memory leaks in of_pmem.c

Currently, in the last error path of of_pmem_region_probe() and in of_pmem_region_remove(), free the memory allocated by kstrdup() is missing. Add kfree(priv->bus_desc.provider_name) to fix it.

Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")
---
 drivers/nvdimm/of_pmem.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvdimm/of_pmem.c +2 -0
index 10dbdcdfb9ce913..1292ffca7b2ecc0 100644
@@ -36,6 +36,7 @@ static int of_pmem_region_probe(struct platform_device *pdev)
 
 	priv->bus = bus = nvdimm_bus_register(&pdev->dev, &priv->bus_desc);
 	if (!bus) {
+		kfree(priv->bus_desc.provider_name);
 		kfree(priv);
 		return -ENODEV;
 	}
@@ -83,6 +84,7 @@ static int of_pmem_region_remove(struct platform_device *pdev)
 	struct of_pmem_private *priv = platform_get_drvdata(pdev);
 
 	nvdimm_bus_unregister(priv->bus);
+	kfree(priv->bus_desc.provider_name);
 	kfree(priv);
 
 	return 0;
Signature · 1 line
1.8.3
author
#2 lore raw

PATCH v3 2/7 libnvdimm: add sanity check for provider_name in of_pmem_region_probe()

In reply to Zhen Lei

kstrdup() may return NULL because of no memory, check it.

Fixes 49bddc73d15c ("libnvdimm/of_pmem: Provide a unique name for bus provider")
---
 drivers/nvdimm/of_pmem.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/nvdimm/of_pmem.c +5 -0
index 1292ffca7b2ecc0..13c4c274ca6ea88 100644
@@ -31,6 +31,11 @@ static int of_pmem_region_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	priv->bus_desc.provider_name = kstrdup(pdev->name, GFP_KERNEL);
+	if (!priv->bus_desc.provider_name) {
+		kfree(priv);
+		return -ENOMEM;
+	}
+
 	priv->bus_desc.module = THIS_MODULE;
 	priv->bus_desc.of_node = np;
 
Signature · 5 lines
1.8.3

_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
author
#3 lore raw

PATCH v3 3/7 libnvdimm: simplify walk_to_nvdimm_bus()

In reply to Zhen Lei

Return as soon as nvdimm_bus device has been found, make us no need to check "dev" or "!dev" in subsequent code.

No functional change.

---
 drivers/nvdimm/bus.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/nvdimm/bus.c +3 -4
index 955265656b96c73..1d89114cb6ab93e 100644
@@ -316,10 +316,9 @@ struct nvdimm_bus *walk_to_nvdimm_bus(struct device *nd_dev)
 
 	for (dev = nd_dev; dev; dev = dev->parent)
 		if (is_nvdimm_bus(dev))
-			break;
-	dev_WARN_ONCE(nd_dev, !dev, "invalid dev, not on nd bus\n");
-	if (dev)
-		return to_nvdimm_bus(dev);
+			return to_nvdimm_bus(dev);
+
+	dev_WARN_ONCE(nd_dev, 1, "invalid dev, not on nd bus\n");
 	return NULL;
 }
 
Signature · 5 lines
1.8.3

_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
author
#4 lore raw

PATCH v3 4/7 libnvdimm: reduce an unnecessary if branch in nd_region_create()

In reply to Zhen Lei

if (ndr_desc->flush)
1) nd_region->flush = ndr_desc->flush;
else
2) nd_region->flush = NULL;

When entering the "else" branch, the value of ndr_desc->flush is NULL. After replaced "NULL" with "ndr_desc->flush" at 2), we will find that it becomes the same to 1).

So the above code snippet can be reduced to one statement:
nd_region->flush = ndr_desc->flush;

No functional change.

---
 drivers/nvdimm/region_devs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/nvdimm/region_devs.c +1 -4
index ef23119db574663..7cf9c7d857909ce 100644
@@ -1131,10 +1131,7 @@ static struct nd_region *nd_region_create(struct nvdimm_bus *nvdimm_bus,
 	nd_region->ndr_size = resource_size(ndr_desc->res);
 	nd_region->ndr_start = ndr_desc->res->start;
 	nd_region->align = default_align(nd_region);
-	if (ndr_desc->flush)
-		nd_region->flush = ndr_desc->flush;
-	else
-		nd_region->flush = NULL;
+	nd_region->flush = ndr_desc->flush;
 
 	nd_device_register(dev);
 
Signature · 5 lines
1.8.3

_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
author
#5 lore raw

PATCH v3 5/7 libnvdimm: reduce an unnecessary if branch in nd_region_activate()

In reply to Zhen Lei

According to the original code logic:

if (!nvdimm->num_flush) {
	flush_data_size += sizeof(void *);
	//nvdimm->num_flush is zero now, add 1) have no side effects
} else {
	flush_data_size += sizeof(void *);

1) flush_data_size += nvdimm->num_flush * sizeof(void *);
}

Obviously, the above code snippet can be reduced to one statement:
flush_data_size += (nvdimm->num_flush + 1) * sizeof(void *);

No functional change.

---
 drivers/nvdimm/region_devs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/nvdimm/region_devs.c +1 -4
index 7cf9c7d857909ce..49be115c9189eff 100644
@@ -77,11 +77,8 @@ int nd_region_activate(struct nd_region *nd_region)
 		}
 
 		/* at least one null hint slot per-dimm for the "no-hint" case */
-		flush_data_size += sizeof(void *);
+		flush_data_size += (nvdimm->num_flush + 1) * sizeof(void *);
 		num_flush = min_not_zero(num_flush, nvdimm->num_flush);
-		if (!nvdimm->num_flush)
-			continue;
-		flush_data_size += nvdimm->num_flush * sizeof(void *);
 	}
 	nvdimm_bus_unlock(&nd_region->dev);
 
Signature · 5 lines
1.8.3

_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
#6 lore raw

Re: [PATCH v3 5/7] libnvdimm: reduce an unnecessary if branch in nd_region_activate()

I will drop this patch, because I have a doubt:
Suppose the nd_region->ndr_mappings is 4, and for each nd_region->mapping[],
the value of num_flush is "0, 0, 4, 0", so the flush_data_size is "1 + 1 + 5 + 1", * sizeof(void *). But in ndrd_get_flush_wpq() or ndrd_set_flush_wpq(), the expression is "ndrd->flush_wpq[dimm * num + (hint & mask)]", I don't think the memory "ndrd" allocated is enough. Please refer call chain: nd_region_activate() --> nvdimm_map_flush() --> ndrd_set_flush_wpq()

	for (i = 0; i < nd_region->ndr_mappings; i++) {
                struct nd_mapping *nd_mapping = &nd_region->mapping[i];
                struct nvdimm *nvdimm = nd_mapping->nvdimm;

                /* at least one null hint slot per-dimm for the "no-hint" case */
                flush_data_size += sizeof(void *);
                num_flush = min_not_zero(num_flush, nvdimm->num_flush);
                if (!nvdimm->num_flush)
                        continue;
                flush_data_size += nvdimm->num_flush * sizeof(void *);
        }

        ndrd = devm_kzalloc(dev, sizeof(*ndrd) + flush_data_size, GFP_KERNEL);
Quoted 37 lines · Zhen Lei

According to the original code logic:

if (!nvdimm->num_flush) {
	flush_data_size += sizeof(void *);
	//nvdimm->num_flush is zero now, add 1) have no side effects
} else {
	flush_data_size += sizeof(void *);

1) flush_data_size += nvdimm->num_flush * sizeof(void *);
}

Obviously, the above code snippet can be reduced to one statement:
flush_data_size += (nvdimm->num_flush + 1) * sizeof(void *);

No functional change.

---
 drivers/nvdimm/region_devs.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/nvdimm/region_devs.c +1 -4
index 7cf9c7d857909ce..49be115c9189eff 100644
@@ -77,11 +77,8 @@ int nd_region_activate(struct nd_region *nd_region)
 		}
 
 		/* at least one null hint slot per-dimm for the "no-hint" case */
-		flush_data_size += sizeof(void *);
+		flush_data_size += (nvdimm->num_flush + 1) * sizeof(void *);
 		num_flush = min_not_zero(num_flush, nvdimm->num_flush);
-		if (!nvdimm->num_flush)
-			continue;
-		flush_data_size += nvdimm->num_flush * sizeof(void *);
 	}
 	nvdimm_bus_unlock(&nd_region->dev);
 
author
#7 lore raw

PATCH v3 6/7 libnvdimm: make sure EXPORT_SYMBOL_GPL(nvdimm_flush) close to its function

In reply to Zhen Lei

Move EXPORT_SYMBOL_GPL(nvdimm_flush) close to nvdimm_flush(), currently it's near to generic_nvdimm_flush().

---
 drivers/nvdimm/region_devs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvdimm/region_devs.c +2 -1
index 49be115c9189eff..909bf03edb132cf 100644
@@ -1183,6 +1183,8 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
 
 	return rc;
 }
+EXPORT_SYMBOL_GPL(nvdimm_flush);
+
 /**
  * nvdimm_flush - flush any posted write queues between the cpu and pmem media
  * @nd_region: blk or interleaved pmem region
@@ -1214,7 +1216,6 @@ int generic_nvdimm_flush(struct nd_region *nd_region)
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(nvdimm_flush);
 
 /**
  * nvdimm_has_flush - determine write flushing requirements
Signature · 5 lines
1.8.3

_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
Reviewed-by on v3 6/7
#8 lore raw

Re: [PATCH v3 6/7] libnvdimm: make sure EXPORT_SYMBOL_GPL(nvdimm_flush) close to its function

Quoted 22 lines

Move EXPORT_SYMBOL_GPL(nvdimm_flush) close to nvdimm_flush(), currently it's near to generic_nvdimm_flush().

---
 drivers/nvdimm/region_devs.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvdimm/region_devs.c +2 -0
index 49be115c9189eff..909bf03edb132cf 100644
@@ -1183,6 +1183,8 @@ int nvdimm_flush(struct nd_region *nd_region, struct bio *bio)
 
        return rc;
 }
+EXPORT_SYMBOL_GPL(nvdimm_flush);
+
 /**
  * nvdimm_flush - flush any posted write queues between the cpu and pmem media
  * @nd_region: blk or interleaved pmem region

drivers/nvdimm/region_devs.c int generic_nvdimm_flush(struct nd_region *nd_region)

Quoted 8 lines
drivers/nvdimm/region_devs.c +0 -2
@@ -1214,7 +1216,6 @@ int generic_nvdimm_flush(struct nd_region *nd_region)
 
        return 0;
 }
-EXPORT_SYMBOL_GPL(nvdimm_flush);
 
 /**
  * nvdimm_has_flush - determine write flushing requirements
--
Quoted 5 lines

1.8.3

_______________________________________________
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]

author
#9 lore raw

PATCH v3 7/7 libnvdimm: slightly simplify available_slots_show()

In reply to Zhen Lei

The type of "nfree" is u32, so "nfree - 1" can only be overflowed when "nfree" is zero. Replace "if (nfree - 1 > nfree)" with "if (nfree == 0)" seems more clear. And remove the assignment "nfree = 0", no need for it.

---
 drivers/nvdimm/dimm_devs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/nvdimm/dimm_devs.c +2 -3
index 61374def515552f..bf7d0cdc147cb39 100644
@@ -347,10 +347,9 @@ static ssize_t available_slots_show(struct device *dev,
 
 	nvdimm_bus_lock(dev);
 	nfree = nd_label_nfree(ndd);
-	if (nfree - 1 > nfree) {
+	if (nfree == 0)
 		dev_WARN_ONCE(dev, 1, "we ate our last label?\n");
-		nfree = 0;
-	} else
+	else
 		nfree--;
 	rc = sprintf(buf, "%d\n", nfree);
 	nvdimm_bus_unlock(dev);
Signature · 1 line
1.8.3
#11 lore raw
In reply to Zhen Lei

Hi all:
Any comment? I want to merge patches 1 and 2 into one, then send
other patches separately.

Quoted 29 lines · Zhen Lei

v2 --> v3:
1. Fix spelling error of patch 1 subject: memmory --> memory
2. Add "Reviewed-by: Oliver O'Halloran <[email protected]>" into patch 1
3. Rewrite patch descriptions of Patch 1, 3, 4
4. Add 3 new trivial patches 5-7, I just found that yesterday.
5. Unify all "subsystem" names to "libnvdimm:"

v1 --> v2:
1. Add Fixes for Patch 1-2
2. Slightly change the subject and description of Patch 1
3. Add a new trivial Patch 4, I just found that yesterday.

v1:
I found a memleak when I learned the drivers/nvdimm code today. And I also added a sanity check for priv->bus_desc.provider_name, because strdup() maybe failed. Patch 3 is a trivial source code optimization.

Zhen Lei (7):
libnvdimm: fix memory leaks in of_pmem.c
libnvdimm: add sanity check for provider_name in
of_pmem_region_probe()
libnvdimm: simplify walk_to_nvdimm_bus()
libnvdimm: reduce an unnecessary if branch in nd_region_create()
libnvdimm: reduce an unnecessary if branch in nd_region_activate()
libnvdimm: make sure EXPORT_SYMBOL_GPL(nvdimm_flush) close to its
function
libnvdimm: slightly simplify available_slots_show()

Quoted 6 lines
 drivers/nvdimm/bus.c         |  7 +++----
 drivers/nvdimm/dimm_devs.c   |  5 ++---
 drivers/nvdimm/of_pmem.c     |  7 +++++++
 drivers/nvdimm/region_devs.c | 13 ++++---------
 4 files changed, 16 insertions(+), 16 deletions(-)

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