Skip to contentSkip to conversation

fpga: m10bmc-sec: Fix possible memory leak of flash_buf

PATCH Applied Russ Weight 3 participants 3 messages Sep 16, 2022 – Sep 19, 2022

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

PATCH 1/1 fpga: m10bmc-sec: Fix possible memory leak of flash_buf

There is an error check following the allocation of flash_buf that returns without freeing flash_buf. It makes more sense to do the error check before the allocation and the reordering eliminates the memory leak.

Fixes 154afa5c31cd ("fpga: m10bmc-sec: expose max10 flash update count")
---
 drivers/fpga/intel-m10-bmc-sec-update.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/fpga/intel-m10-bmc-sec-update.c +4 -4
index 526c8cdd1474..79d48852825e 100644
@@ -148,10 +148,6 @@ static ssize_t flash_count_show(struct device *dev,
 	stride = regmap_get_reg_stride(sec->m10bmc->regmap);
 	num_bits = FLASH_COUNT_SIZE * 8;
 
-	flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL);
-	if (!flash_buf)
-		return -ENOMEM;
-
 	if (FLASH_COUNT_SIZE % stride) {
 		dev_err(sec->dev,
 			"FLASH_COUNT_SIZE (0x%x) not aligned to stride (0x%x)\n",
@@ -160,6 +156,10 @@ static ssize_t flash_count_show(struct device *dev,
 		return -EINVAL;
 	}
 
+	flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL);
+	if (!flash_buf)
+		return -ENOMEM;
+
 	ret = regmap_bulk_read(sec->m10bmc->regmap, STAGING_FLASH_COUNT,
 			       flash_buf, FLASH_COUNT_SIZE / stride);
 	if (ret) {
Signature · 1 line
2.25.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Russ Weight

There is an error check following the allocation of flash_buf that returns without freeing flash_buf. It makes more sense to do the error check before the allocation and the reordering eliminates the memory leak.

Fixes 154afa5c31cd ("fpga: m10bmc-sec: expose max10 flash update count")
Quoted 31 lines
---
  drivers/fpga/intel-m10-bmc-sec-update.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/fpga/intel-m10-bmc-sec-update.c +4 -4
index 526c8cdd1474..79d48852825e 100644
@@ -148,10 +148,6 @@ static ssize_t flash_count_show(struct device *dev,
  	stride = regmap_get_reg_stride(sec->m10bmc->regmap);
  	num_bits = FLASH_COUNT_SIZE * 8;
 
-	flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL);
-	if (!flash_buf)
-		return -ENOMEM;
-
  	if (FLASH_COUNT_SIZE % stride) {
  		dev_err(sec->dev,
  			"FLASH_COUNT_SIZE (0x%x) not aligned to stride (0x%x)\n",
@@ -160,6 +156,10 @@ static ssize_t flash_count_show(struct device *dev,
  		return -EINVAL;
  	}
 
+	flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL);
+	if (!flash_buf)
+		return -ENOMEM;
+
  	ret = regmap_bulk_read(sec->m10bmc->regmap, STAGING_FLASH_COUNT,
  			       flash_buf, FLASH_COUNT_SIZE / stride);
  	if (ret) {
maintainer Acked-by Applied
#2 lore raw
Quoted 11 lines · Tom Rix
Quoted 8 lines · Russ Weight

There is an error check following the allocation of flash_buf that returns without freeing flash_buf. It makes more sense to do the error check before the allocation and the reordering eliminates the memory leak.

Fixes 154afa5c31cd ("fpga: m10bmc-sec: expose max10 flash update count")

Applied to for-6.0

Quoted 30 lines
Quoted 29 lines
---
  drivers/fpga/intel-m10-bmc-sec-update.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/fpga/intel-m10-bmc-sec-update.c +4 -4
index 526c8cdd1474..79d48852825e 100644
@@ -148,10 +148,6 @@ static ssize_t flash_count_show(struct device *dev,
  	stride = regmap_get_reg_stride(sec->m10bmc->regmap);
  	num_bits = FLASH_COUNT_SIZE * 8;
-	flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL);
-	if (!flash_buf)
-		return -ENOMEM;
-
  	if (FLASH_COUNT_SIZE % stride) {
  		dev_err(sec->dev,
  			"FLASH_COUNT_SIZE (0x%x) not aligned to stride (0x%x)\n",
@@ -160,6 +156,10 @@ static ssize_t flash_count_show(struct device *dev,
  		return -EINVAL;
  	}
+	flash_buf = kmalloc(FLASH_COUNT_SIZE, GFP_KERNEL);
+	if (!flash_buf)
+		return -ENOMEM;
+
  	ret = regmap_bulk_read(sec->m10bmc->regmap, STAGING_FLASH_COUNT,
  			       flash_buf, FLASH_COUNT_SIZE / stride);
  	if (ret) {