Skip to contentSkip to conversation

scsi: ufs: fix potential memory leak

PATCH Superseded Tiezhu Yang 2 participants 3 messages Jun 7, 2016 SCSI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: ufs: fix potential memory leak

There exists potential memory leak in ufshcd_parse_clock_info(), this patch fixes it.

---
 drivers/scsi/ufs/ufshcd-pltfrm.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/scsi/ufs/ufshcd-pltfrm.c +7 -3
index db53f38..8b057f4 100644
@@ -100,19 +100,19 @@ static int ufshcd_parse_clock_info(struct ufs_hba *hba)
 	if (ret && (ret != -EINVAL)) {
 		dev_err(dev, "%s: error reading array %d\n",
 				"freq-table-hz", ret);
-		return ret;
+		goto out_free;
 	}
 
 	for (i = 0; i < sz; i += 2) {
 		ret = of_property_read_string_index(np,
 				"clock-names", i/2, (const char **)&name);
 		if (ret)
-			goto out;
+			goto out_free;
 
 		clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
 		if (!clki) {
 			ret = -ENOMEM;
-			goto out;
+			goto out_free;
 		}
 
 		clki->min_freq = clkfreq[i];
@@ -122,6 +122,10 @@ static int ufshcd_parse_clock_info(struct ufs_hba *hba)
 				clki->min_freq, clki->max_freq, clki->name);
 		list_add_tail(&clki->list, &hba->clk_list_head);
 	}
+
+out_free:
+	devm_kfree(dev, clkfreq);
+	clkfreq = NULL;
 out:
 	return ret;
 }
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 2 lines · Tiezhu Yang

There exists potential memory leak in ufshcd_parse_clock_info(), this patch fixes it.

What makes you think there's a leak here? These are all devm_ allocations, so they're all freed when the device is. If an error is returned, the device is released and the memory freed.

You can argue that on successful initialization, there's no need to keep the clkfreq array but this patch isn't the way you'd change that.

James

Quoted 48 lines
---
 drivers/scsi/ufs/ufshcd-pltfrm.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/scsi/ufs/ufshcd-pltfrm.c +3 -3
index db53f38..8b057f4 100644
@@ -100,19 +100,19 @@ static int ufshcd_parse_clock_info(struct
 ufs_hba *hba)
 	if (ret && (ret != -EINVAL)) {
 		dev_err(dev, "%s: error reading array %d\n",
 				"freq-table-hz", ret);
-		return ret;
+		goto out_free;
 	}
 
 	for (i = 0; i < sz; i += 2) {
 		ret = of_property_read_string_index(np,
 				"clock-names", i/2, (const char
 **)&name);
 		if (ret)
-			goto out;
+			goto out_free;
 
 		clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
 		if (!clki) {
 			ret = -ENOMEM;
-			goto out;
+			goto out_free;
 		}
drivers/scsi/ufs/ufshcd-pltfrm.c +4 -1
 		clki->min_freq = clkfreq[i];
@@ -122,6 +122,10 @@ static int ufshcd_parse_clock_info(struct
 ufs_hba *hba)
 				clki->min_freq, clki->max_freq, clki
->name);
 		list_add_tail(&clki->list, &hba->clk_list_head);
 	}
+
+out_free:
+	devm_kfree(dev, clkfreq);
+	clkfreq = NULL;
 out:
 	return ret;
 }
#2 lore raw
Quoted 4 lines · James Bottomley
Quoted 2 lines · Tiezhu Yang

 There exists potential memory leak in ufshcd_parse_clock_info(), this patch fixes it.

Quoted 9 lines

What makes you think there's a leak here?  These are all devm_ allocations, so they're all freed when the device is.  If an error is returned, the device is released and the memory freed.

You can argue that on successful initialization, there's no need to keep the clkfreq array but this patch isn't the way you'd change that.

James

OK, thanks. I will send a v2 patch.