linux-cxl.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] cxl/region: Fix cxlr_pmem leaks
@ 2024-04-28  3:07 Li Zhijian
  2024-04-30 16:45 ` Jonathan Cameron
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Li Zhijian @ 2024-04-28  3:07 UTC (permalink / raw)
  To: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, linux-cxl
  Cc: linux-kernel, Li Zhijian

Before this error path, cxlr_pmem pointed to a kzalloc() memory, free
it to avoid this memory leaking.

Fixes: f17b558d6663 ("cxl/pmem: Refactor nvdimm device registration, delete the workqueue")
Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
---
 drivers/cxl/core/region.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
index 5c186e0a39b9..812b2948b6c6 100644
--- a/drivers/cxl/core/region.c
+++ b/drivers/cxl/core/region.c
@@ -2719,6 +2719,7 @@ static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
 		if (i == 0) {
 			cxl_nvb = cxl_find_nvdimm_bridge(cxlmd);
 			if (!cxl_nvb) {
+				kfree(cxlr_pmem);
 				cxlr_pmem = ERR_PTR(-ENODEV);
 				goto out;
 			}
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/region: Fix cxlr_pmem leaks
  2024-04-28  3:07 [PATCH] cxl/region: Fix cxlr_pmem leaks Li Zhijian
@ 2024-04-30 16:45 ` Jonathan Cameron
  2024-04-30 19:58   ` Dan Williams
  2024-04-30 22:55 ` fan
  2024-05-01 19:23 ` Markus Elfring
  2 siblings, 1 reply; 5+ messages in thread
From: Jonathan Cameron @ 2024-04-30 16:45 UTC (permalink / raw)
  To: Li Zhijian
  Cc: dave, dave.jiang, alison.schofield, vishal.l.verma, ira.weiny,
	dan.j.williams, linux-cxl, linux-kernel

On Sun, 28 Apr 2024 11:07:48 +0800
Li Zhijian <lizhijian@fujitsu.com> wrote:

> Before this error path, cxlr_pmem pointed to a kzalloc() memory, free
> it to avoid this memory leaking.
> 
> Fixes: f17b558d6663 ("cxl/pmem: Refactor nvdimm device registration, delete the workqueue")
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>

Fix is good, but this looks like nice case for conversion to cleanup.h stuff
perhaps better to just do that?  Would need a small amount of dancing
on the final return to return cxlr->cxlr_pmem + pointer steal
when setting cxlr->cxlr_pmem a few lines up.

Also guard for the rwsem.

Dave, Dan, worth doing or take this minimal fix and spin around later?

If you think this is the way to go.
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

> ---
>  drivers/cxl/core/region.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 5c186e0a39b9..812b2948b6c6 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -2719,6 +2719,7 @@ static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
>  		if (i == 0) {
>  			cxl_nvb = cxl_find_nvdimm_bridge(cxlmd);
>  			if (!cxl_nvb) {
> +				kfree(cxlr_pmem);
>  				cxlr_pmem = ERR_PTR(-ENODEV);
>  				goto out;
>  			}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/region: Fix cxlr_pmem leaks
  2024-04-30 16:45 ` Jonathan Cameron
@ 2024-04-30 19:58   ` Dan Williams
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Williams @ 2024-04-30 19:58 UTC (permalink / raw)
  To: Jonathan Cameron, Li Zhijian
  Cc: dave, dave.jiang, alison.schofield, vishal.l.verma, ira.weiny,
	dan.j.williams, linux-cxl, linux-kernel

Jonathan Cameron wrote:
> On Sun, 28 Apr 2024 11:07:48 +0800
> Li Zhijian <lizhijian@fujitsu.com> wrote:
> 
> > Before this error path, cxlr_pmem pointed to a kzalloc() memory, free
> > it to avoid this memory leaking.
> > 
> > Fixes: f17b558d6663 ("cxl/pmem: Refactor nvdimm device registration, delete the workqueue")
> > Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
> 
> Fix is good, but this looks like nice case for conversion to cleanup.h stuff
> perhaps better to just do that?  Would need a small amount of dancing
> on the final return to return cxlr->cxlr_pmem + pointer steal
> when setting cxlr->cxlr_pmem a few lines up.
> 
> Also guard for the rwsem.
> 
> Dave, Dan, worth doing or take this minimal fix and spin around later?
> 
> If you think this is the way to go.
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

I had spun up the conversion patch as a replacement in response to this.
However, after looking at that I think merging this fix with a follow-on
cleanup is they way to go because the cleanup touches locking, memory
allocation, and changes the calling convention.

Reviewed-by: Dan Williams <dan.j.williams@intel.com>

I'll send the follow-on cleanup shortly.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/region: Fix cxlr_pmem leaks
  2024-04-28  3:07 [PATCH] cxl/region: Fix cxlr_pmem leaks Li Zhijian
  2024-04-30 16:45 ` Jonathan Cameron
@ 2024-04-30 22:55 ` fan
  2024-05-01 19:23 ` Markus Elfring
  2 siblings, 0 replies; 5+ messages in thread
From: fan @ 2024-04-30 22:55 UTC (permalink / raw)
  To: Li Zhijian
  Cc: dave, jonathan.cameron, dave.jiang, alison.schofield,
	vishal.l.verma, ira.weiny, dan.j.williams, linux-cxl,
	linux-kernel

On Sun, Apr 28, 2024 at 11:07:48AM +0800, Li Zhijian wrote:
> Before this error path, cxlr_pmem pointed to a kzalloc() memory, free
> it to avoid this memory leaking.
> 
> Fixes: f17b558d6663 ("cxl/pmem: Refactor nvdimm device registration, delete the workqueue")
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
> ---

Reviewed-by: Fan Ni <fan.ni@samsung.com>

>  drivers/cxl/core/region.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/cxl/core/region.c b/drivers/cxl/core/region.c
> index 5c186e0a39b9..812b2948b6c6 100644
> --- a/drivers/cxl/core/region.c
> +++ b/drivers/cxl/core/region.c
> @@ -2719,6 +2719,7 @@ static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr)
>  		if (i == 0) {
>  			cxl_nvb = cxl_find_nvdimm_bridge(cxlmd);
>  			if (!cxl_nvb) {
> +				kfree(cxlr_pmem);
>  				cxlr_pmem = ERR_PTR(-ENODEV);
>  				goto out;
>  			}
> -- 
> 2.29.2
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cxl/region: Fix cxlr_pmem leaks
  2024-04-28  3:07 [PATCH] cxl/region: Fix cxlr_pmem leaks Li Zhijian
  2024-04-30 16:45 ` Jonathan Cameron
  2024-04-30 22:55 ` fan
@ 2024-05-01 19:23 ` Markus Elfring
  2 siblings, 0 replies; 5+ messages in thread
From: Markus Elfring @ 2024-05-01 19:23 UTC (permalink / raw)
  To: Li Zhijian, linux-cxl, kernel-janitors, Alison Schofield,
	Dan Williams, Dave Jiang, Davidlohr Bueso, Ira Weiny,
	Jonathan Cameron, Vishal Verma
  Cc: LKML

> Before this error path, cxlr_pmem pointed to a kzalloc() memory, free
> it to avoid this memory leaking.

Can the following wording be a bit nicer?

   The local variable “cxlr_pmem” referred to dynamically allocated memory.
   Free it in one error case before it is reset to an error pointer.


Would the summary phrase “Fix a memory leak in cxl_pmem_region_alloc()”
be more appropriate here?

Regards,
Markus

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-05-01 19:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-28  3:07 [PATCH] cxl/region: Fix cxlr_pmem leaks Li Zhijian
2024-04-30 16:45 ` Jonathan Cameron
2024-04-30 19:58   ` Dan Williams
2024-04-30 22:55 ` fan
2024-05-01 19:23 ` Markus Elfring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).