Skip to contentSkip to conversation

drm/amdgpu: fix memory leak in amdgpu_fru_get_product_info()

PATCH In review Yang Wang 3 participants 5 messages Sep 22, 2023

Subsystems (2)
lore
Share & tools
lore

Yang Wang

author
#0 lore raw

PATCH 1/1 drm/amdgpu: fix memory leak in amdgpu_fru_get_product_info()

fix a memory leak that occurs when csum is 0,
the origin function will return directly and forgets to free 'pia' resource.

Fixes 0dbf2c562625 ("drm/amdgpu: Interpret IPMI data for product information (v2)")
CC Luben Tuikov <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +6 -5
index 401651f28ba2..50b6eb447726 100644
@@ -111,7 +111,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 {
 	unsigned char buf[8], *pia;
 	u32 addr, fru_addr;
-	int size, len;
+	int size, len, ret = 0;
 	u8 csum;
 
 	if (!is_fru_eeprom_supported(adev, &fru_addr))
@@ -171,16 +171,17 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	/* Read the whole PIA. */
 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
 	if (len != size) {
-		kfree(pia);
 		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
-		return len < 0 ? len : -EIO;
+		ret = len < 0 ? len : -EIO;
+		goto Out;
 	}
 
 	for (csum = 0; size > 0; size--)
 		csum += pia[size - 1];
 	if (csum) {
 		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
-		return -EIO;
+		ret = -EIO;
+		goto Out;
 	}
 
 	/* Now extract useful information from the PIA.
@@ -220,7 +221,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	adev->serial[sizeof(adev->serial) - 1] = '\0';
 Out:
 	kfree(pia);
-	return 0;
+	return ret;
 }
 
 /**
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Yang Wang

fix a memory leak that occurs when csum is 0,
the origin function will return directly and forgets to free 'pia' resource.

Fixes 0dbf2c562625 ("drm/amdgpu: Interpret IPMI data for product information (v2)")
CC Luben Tuikov <[email protected]>
Quoted 50 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +6 -6
index 401651f28ba2..50b6eb447726 100644
@@ -111,7 +111,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 {
        unsigned char buf[8], *pia;
        u32 addr, fru_addr;
-       int size, len;
+       int size, len, ret = 0;
        u8 csum;
 
        if (!is_fru_eeprom_supported(adev, &fru_addr))
@@ -171,16 +171,17 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
        /* Read the whole PIA. */
        len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
        if (len != size) {
-               kfree(pia);
                DRM_ERROR("Couldn't read the Product Info Area: %d", len);
-               return len < 0 ? len : -EIO;
+               ret = len < 0 ? len : -EIO;
+               goto Out;
        }
 
        for (csum = 0; size > 0; size--)
                csum += pia[size - 1];
        if (csum) {
                DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
-               return -EIO;
+               ret = -EIO;
+               goto Out;
        }
 
        /* Now extract useful information from the PIA.
@@ -220,7 +221,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
        adev->serial[sizeof(adev->serial) - 1] = '\0';
 Out:
        kfree(pia);
-       return 0;
+       return ret;
 }
 
 /**
--

2.34.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Yang Wang
Quoted 7 lines · Yang Wang

fix a memory leak that occurs when csum is 0,
the origin function will return directly and forgets to free 'pia' resource.

Fixes 0dbf2c562625 ("drm/amdgpu: Interpret IPMI data for product information (v2)")
CC Luben Tuikov <[email protected]>

Ah, yes, we should free "pia". Good catch!

Regards,
Luben

Quoted 49 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +5 -4
index 401651f28ba2..50b6eb447726 100644
@@ -111,7 +111,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 {
 	unsigned char buf[8], *pia;
 	u32 addr, fru_addr;
-	int size, len;
+	int size, len, ret = 0;
 	u8 csum;
 
 	if (!is_fru_eeprom_supported(adev, &fru_addr))
@@ -171,16 +171,17 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	/* Read the whole PIA. */
 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
 	if (len != size) {
-		kfree(pia);
 		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
-		return len < 0 ? len : -EIO;
+		ret = len < 0 ? len : -EIO;
+		goto Out;
 	}
 
 
 
 	for (csum = 0; size > 0; size--)
 		csum += pia[size - 1];
 	if (csum) {
 		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
-		return -EIO;
+		ret = -EIO;
+		goto Out;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +1 -1
 	/* Now extract useful information from the PIA.
@@ -220,7 +221,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	adev->serial[sizeof(adev->serial) - 1] = '\0';
 Out:
 	kfree(pia);
-	return 0;
+	return ret;
 }
 
 /**
Signature · 2 lines
Regards,
Luben
#3 lore raw
Quoted 4 lines · Luben Tuikov
Quoted 3 lines · Yang Wang

fix a memory leak that occurs when csum is 0,
the origin function will return directly and forgets to free 'pia' resource.

Quoted 8 lines
Quoted 4 lines
Fixes 0dbf2c562625 ("drm/amdgpu: Interpret IPMI data for product information (v2)")
CC Luben Tuikov <[email protected]>

Ah, yes, we should free "pia". Good catch!

Retracted--see my follow-up email of making this a one-liner change, instead of adding a whole bunch of changes which are unnecessary to fix this memory leak.

Regards,
Luben

Quoted 54 lines

Regards,
Luben

Quoted 49 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +5 -4
index 401651f28ba2..50b6eb447726 100644
@@ -111,7 +111,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 {
 	unsigned char buf[8], *pia;
 	u32 addr, fru_addr;
-	int size, len;
+	int size, len, ret = 0;
 	u8 csum;
 
 	if (!is_fru_eeprom_supported(adev, &fru_addr))
@@ -171,16 +171,17 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	/* Read the whole PIA. */
 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
 	if (len != size) {
-		kfree(pia);
 		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
-		return len < 0 ? len : -EIO;
+		ret = len < 0 ? len : -EIO;
+		goto Out;
 	}
 
 
 
 	for (csum = 0; size > 0; size--)
 		csum += pia[size - 1];
 	if (csum) {
 		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
-		return -EIO;
+		ret = -EIO;
+		goto Out;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +1 -1
 	/* Now extract useful information from the PIA.
@@ -220,7 +221,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	adev->serial[sizeof(adev->serial) - 1] = '\0';
 Out:
 	kfree(pia);
-	return 0;
+	return ret;
 }
 
 /**
Signature · 2 lines
Regards,
Luben
#4 lore raw
In reply to Yang Wang
Quoted 31 lines · Yang Wang

fix a memory leak that occurs when csum is 0,
the origin function will return directly and forgets to free 'pia' resource.

Fixes 0dbf2c562625 ("drm/amdgpu: Interpret IPMI data for product information (v2)")
CC Luben Tuikov <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +1 -3
index 401651f28ba2..50b6eb447726 100644
@@ -111,7 +111,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 {
 	unsigned char buf[8], *pia;
 	u32 addr, fru_addr;
-	int size, len;
+	int size, len, ret = 0;
 	u8 csum;
 
 	if (!is_fru_eeprom_supported(adev, &fru_addr))
@@ -171,16 +171,17 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	/* Read the whole PIA. */
 	len = amdgpu_eeprom_read(adev->pm.fru_eeprom_i2c_bus, addr, pia, size);
 	if (len != size) {
-		kfree(pia);
 		DRM_ERROR("Couldn't read the Product Info Area: %d", len);
-		return len < 0 ? len : -EIO;

drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c int amdgpu_fru_get_product_info(struct amdgpu_device *adev) · line 183

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +3 -1
+		ret = len < 0 ? len : -EIO;
+		goto Out;
 	}
 
 	for (csum = 0; size > 0; size--)
 		csum += pia[size - 1];
 	if (csum) {
 		DRM_ERROR("Bad Product Info Area checksum: 0x%02x", csum);
-		return -EIO;
+		ret = -EIO;

Actually the memory leak is ONLY here.

I wonder if you could've simply added a

		kfree(pia);

drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c int amdgpu_fru_get_product_info(struct amdgpu_device *adev) · line 185

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +1 -0
+		goto Out;
 	}

before the goto Out; which would result in a one-line change.

Yeah, please do that instead.

So, don't add "ret" and what not. Just add the one liner "kfree(pia);" before the "goto Out;" and make it a SINGLE-LINE change to fix this memory leak.

You don't need so many (formulaic) changes of adding "ret" and what not. Just a one-liner change, please.

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c +1 -1
 	/* Now extract useful information from the PIA.
@@ -220,7 +221,7 @@ int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
 	adev->serial[sizeof(adev->serial) - 1] = '\0';
 Out:
 	kfree(pia);
-	return 0;
+	return ret;
 }
 
 /**
Signature · 2 lines
Regards,
Luben