Skip to contentSkip to conversation

cxl/edac: Fix potential memory leak issues

PATCH Applied Li Ming 4 participants 5 messages Jun 13, 2025 CXL

lore
Share & tools
lore

Li Ming

author reviewer
#0 lore raw

PATCH v2 1/1 cxl/edac: Fix potential memory leak issues

In cxl_store_rec_gen_media() and cxl_store_rec_dram(), use kmemdup() to duplicate a cxl gen_media/dram event to store the event in a xarray by xa_store(). The cxl gen_media/dram event allocated by kmemdup() should be freed in the case that the xa_store() fails.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")

---
v2:
* Use kfree() instead of __free(kfree). (Jonathan)

base-commit: 87b42c114cdda76c8ad3002f2096699ad5146cb3 cxl/next

---
 drivers/cxl/core/edac.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/cxl/core/edac.c +6 -2
index 2cbc664e5d62..628786def464 100644
@@ -1099,8 +1099,10 @@ int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt)
 	old_rec = xa_store(&array_rec->rec_gen_media,
 			   le64_to_cpu(rec->media_hdr.phys_addr), rec,
 			   GFP_KERNEL);
-	if (xa_is_err(old_rec))
+	if (xa_is_err(old_rec)) {
+		kfree(rec);
 		return xa_err(old_rec);
+	}
 
 	kfree(old_rec);
 
@@ -1127,8 +1129,10 @@ int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt)
 	old_rec = xa_store(&array_rec->rec_dram,
 			   le64_to_cpu(rec->media_hdr.phys_addr), rec,
 			   GFP_KERNEL);
-	if (xa_is_err(old_rec))
+	if (xa_is_err(old_rec)) {
+		kfree(rec);
 		return xa_err(old_rec);
+	}
 
 	kfree(old_rec);
 
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · On Fri, 13 Jun 2025 09:16:48 +0800 Li Ming <[email protected]> wrote:

In cxl_store_rec_gen_media() and cxl_store_rec_dram(), use kmemdup() to duplicate a cxl gen_media/dram event to store the event in a xarray by xa_store(). The cxl gen_media/dram event allocated by kmemdup() should be freed in the case that the xa_store() fails.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")

LGTM

Reviewed-by on v2 1/1
#2 lore raw
In reply to Li Ming
Quoted 9 lines · Li Ming

In cxl_store_rec_gen_media() and cxl_store_rec_dram(), use kmemdup() to duplicate a cxl gen_media/dram event to store the event in a xarray by xa_store(). The cxl gen_media/dram event allocated by kmemdup() should be freed in the case that the xa_store() fails.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")

[snip]

Reviewed-by on v2 1/1
#3 lore raw
In reply to Li Ming
Quoted 9 lines · Li Ming

In cxl_store_rec_gen_media() and cxl_store_rec_dram(), use kmemdup() to duplicate a cxl gen_media/dram event to store the event in a xarray by xa_store(). The cxl gen_media/dram event allocated by kmemdup() should be freed in the case that the xa_store() fails.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")

[snip]

maintainer Applied
#4 lore raw
In reply to Li Ming
Quoted 9 lines · Li Ming

In cxl_store_rec_gen_media() and cxl_store_rec_dram(), use kmemdup() to duplicate a cxl gen_media/dram event to store the event in a xarray by xa_store(). The cxl gen_media/dram event allocated by kmemdup() should be freed in the case that the xa_store() fails.

Fixes 0b5ccb0de1e2 ("cxl/edac: Support for finding memory operation attributes from the current boot")
Quoted 5 lines

---
v2:
* Use kfree() instead of __free(kfree). (Jonathan)

base-commit: 87b42c114cdda76c8ad3002f2096699ad5146cb3 cxl/next

Applied to cxl/fixes

In the future, probably best to base against Linus's latest rc rather than cxl/next. Thanks.

Quoted 32 lines
---
 drivers/cxl/core/edac.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/cxl/core/edac.c +6 -2
index 2cbc664e5d62..628786def464 100644
@@ -1099,8 +1099,10 @@ int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt)
 	old_rec = xa_store(&array_rec->rec_gen_media,
 			   le64_to_cpu(rec->media_hdr.phys_addr), rec,
 			   GFP_KERNEL);
-	if (xa_is_err(old_rec))
+	if (xa_is_err(old_rec)) {
+		kfree(rec);
 		return xa_err(old_rec);
+	}
 
 	kfree(old_rec);
 
@@ -1127,8 +1129,10 @@ int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt)
 	old_rec = xa_store(&array_rec->rec_dram,
 			   le64_to_cpu(rec->media_hdr.phys_addr), rec,
 			   GFP_KERNEL);
-	if (xa_is_err(old_rec))
+	if (xa_is_err(old_rec)) {
+		kfree(rec);
 		return xa_err(old_rec);
+	}
 
 	kfree(old_rec);