All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: Li Ming <ming.li@zohomail.com>
Cc: <dave@stgolabs.net>, <dave.jiang@intel.com>,
	<alison.schofield@intel.com>, <vishal.l.verma@intel.com>,
	<ira.weiny@intel.com>, <dan.j.williams@intel.com>,
	<shiju.jose@huawei.com>, <linux-cxl@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/1] cxl/edac: Fix potential memory leak issues
Date: Wed, 11 Jun 2025 14:16:37 +0100	[thread overview]
Message-ID: <20250611141637.0000546b@huawei.com> (raw)
In-Reply-To: <20250611033542.96184-1-ming.li@zohomail.com>

On Wed, 11 Jun 2025 11:35:42 +0800
Li Ming <ming.li@zohomail.com> 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@zohomail.com>
Good fine but I'm not sure this is the best fix.

> ---
> base-commit: 87b42c114cdda76c8ad3002f2096699ad5146cb3 cxl/fixes
> ---
>  drivers/cxl/core/edac.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/cxl/core/edac.c b/drivers/cxl/core/edac.c
> index 2cbc664e5d62..b4c5c23a45d4 100644
> --- a/drivers/cxl/core/edac.c
> +++ b/drivers/cxl/core/edac.c
> @@ -1086,13 +1086,13 @@ static void cxl_del_overflow_old_recs(struct xarray *rec_xarray)
>  int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt)
>  {
>  	struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array;
> -	struct cxl_event_gen_media *rec;
>  	void *old_rec;
>  
>  	if (!IS_ENABLED(CONFIG_CXL_EDAC_MEM_REPAIR) || !array_rec)
>  		return 0;
>  
> -	rec = kmemdup(&evt->gen_media, sizeof(*rec), GFP_KERNEL);
> +	struct cxl_event_gen_media *rec __free(kfree) =
> +		kmemdup(&evt->gen_media, sizeof(*rec), GFP_KERNEL);
>  	if (!rec)
>  		return -ENOMEM;
>  
> @@ -1106,6 +1106,7 @@ int cxl_store_rec_gen_media(struct cxl_memdev *cxlmd, union cxl_event *evt)
>  
>  	cxl_del_expired_gmedia_recs(&array_rec->rec_gen_media, rec);
>  	cxl_del_overflow_old_recs(&array_rec->rec_gen_media);
> +	retain_and_null_ptr(rec);
>  
>  	return 0;
>  }
> @@ -1114,13 +1115,13 @@ EXPORT_SYMBOL_NS_GPL(cxl_store_rec_gen_media, "CXL");
>  int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt)
>  {
>  	struct cxl_mem_err_rec *array_rec = cxlmd->err_rec_array;
> -	struct cxl_event_dram *rec;
>  	void *old_rec;
>  
>  	if (!IS_ENABLED(CONFIG_CXL_EDAC_MEM_REPAIR) || !array_rec)
>  		return 0;
>  
> -	rec = kmemdup(&evt->dram, sizeof(*rec), GFP_KERNEL);
> +	struct cxl_event_dram *rec __free(kfree) =
> +		kmemdup(&evt->dram, sizeof(*rec), GFP_KERNEL);
>  	if (!rec)
>  		return -ENOMEM;
>  
> @@ -1134,6 +1135,7 @@ int cxl_store_rec_dram(struct cxl_memdev *cxlmd, union cxl_event *evt)
>  
>  	cxl_del_expired_dram_recs(&array_rec->rec_dram, rec);
>  	cxl_del_overflow_old_recs(&array_rec->rec_dram);
> +	retain_and_null_ptr(rec);

I'd move this up to immediately after we hand it over to the xa successfully.

Actually I'm not sure this is a good use of __free given we have a single
error path to handle it in and have to manually release it in a different
call to where it is passed to the xa_store() function.  Maybe better
to just kfree in the error path.


>  
>  	return 0;
>  }


  parent reply	other threads:[~2025-06-11 13:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-11  3:35 [PATCH 1/1] cxl/edac: Fix potential memory leak issues Li Ming
2025-06-11 10:44 ` Shiju Jose
2025-06-11 13:16 ` Jonathan Cameron [this message]
2025-06-12  1:05   ` Li Ming

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250611141637.0000546b@huawei.com \
    --to=jonathan.cameron@huawei.com \
    --cc=alison.schofield@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=ira.weiny@intel.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.li@zohomail.com \
    --cc=shiju.jose@huawei.com \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.