Skip to contentSkip to conversation

lightnvm: prevent memory leak in nvm_bb_chunk_sense

PATCH In review Navid Emamdoost 3 participants 3 messages Sep 30, 2019

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

PATCH 1/1 lightnvm: prevent memory leak in nvm_bb_chunk_sense

In nvm_bb_chunk_sense alloc_page allocates memory which is released at the end of the function. But if nvm_submit_io_sync_raw fails the error check skips the release and leaks the allocated page. To fix this issue I moved the __free_page call before error check.

Fixes aff3fb18f957 ("lightnvm: move bad block and chunk state logic to core")
---
 drivers/lightnvm/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/lightnvm/core.c +3 -2
index 7543e395a2c6..5fdae518f6c9 100644
@@ -849,11 +849,12 @@ static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
 	rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
 
 	ret = nvm_submit_io_sync_raw(dev, &rqd);
-	if (ret)
-		return ret;
 
 	__free_page(page);
 
+	if (ret)
+		return ret;
+
 	return rqd.error;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 1 line

To fix this issue I moved the __free_page call before error check.

Would the wording “Move the __free_page() call before the error check.” be more succinct for the change description?

Can the following code variant be applied at the end of this function?

return ret ? ret : rqd.error;

Regards,
Markus

Reviewed-by on v1 1/1
#2 lore raw
In reply to Navid Emamdoost
Quoted 33 lines · Navid Emamdoost

In nvm_bb_chunk_sense alloc_page allocates memory which is released at the end of the function. But if nvm_submit_io_sync_raw fails the error check skips the release and leaks the allocated page. To fix this issue I moved the __free_page call before error check.

Fixes aff3fb18f957 ("lightnvm: move bad block and chunk state logic to core")

---
drivers/lightnvm/core.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
drivers/lightnvm/core.c +3 -3
index 7543e395a2c6..5fdae518f6c9 100644
@@ -849,11 +849,12 @@ static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
 	rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
 
 	ret = nvm_submit_io_sync_raw(dev, &rqd);
-	if (ret)
-		return ret;
 
 	__free_page(page);
 
+	if (ret)
+		return ret;
+
 	return rqd.error;
 }
 
--
Quoted 1 line

2.17.1

You’re right, there is a leak here. Fix looks good to me.