Re: [PATCH v2 1/1] cxl/edac: Fix potential memory leak issues
From: Dave Jiang
Date: Fri Jun 13 2025 - 11:58:15 EST
On 6/12/25 6:16 PM, Li Ming 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")
> Signed-off-by: Li Ming <ming.li@xxxxxxxxxxxx>
> Tested-by: Shiju Jose <shiju.jose@xxxxxxxxxx>
> Reviewed-by: Shiju Jose <shiju.jose@xxxxxxxxxx>
> ---
> 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.
> ---
> drivers/cxl/core/edac.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/cxl/core/edac.c b/drivers/cxl/core/edac.c
> index 2cbc664e5d62..628786def464 100644
> --- a/drivers/cxl/core/edac.c
> +++ b/drivers/cxl/core/edac.c
> @@ -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);
>