Re: [PATCH] cxl/region: Fix cxlr_pmem leaks

From: Jonathan Cameron
Date: Tue Apr 30 2024 - 12:45:56 EST


On Sun, 28 Apr 2024 11:07:48 +0800
Li Zhijian <lizhijian@xxxxxxxxxxx> 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@xxxxxxxxxxx>

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@xxxxxxxxxx>

> ---
> 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;
> }