Linux CXL
 help / color / mirror / Atom feed
From: Dan Williams <dan.j.williams@intel.com>
To: Chenyuan Mi <michenyuan@huawei.com>, <dan.j.williams@intel.com>
Cc: <Jonathan.Cameron@huawei.com>, <dave.jiang@intel.com>,
	<rrichter@amd.com>, <alison.schofield@intel.com>,
	<linux-kernel@vger.kernel.org>, <linux-cxl@vger.kernel.org>
Subject: RE: [PATCH] cxl: Fix memory leak bug in alloc_mock_res()
Date: Mon, 17 Jul 2023 16:49:52 -0700	[thread overview]
Message-ID: <64b5d3a033e5c_45a6294f1@dwillia2-xfh.jf.intel.com.notmuch> (raw)
In-Reply-To: <20230717041609.1162445-1-michenyuan@huawei.com>

Chenyuan Mi wrote:
> When gen_pool_alloc_algo() fails, the error handling path
> forgets to free 'res'. It would cause a memory leak bug.
> 
> Fix it by add kfree() in error handling path.

Going forward I want to set the policy that any error path resource leaks be
fixed by converting to using __free() and associated helpers.

So in this case it would be:

-- >8 --
diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
index 0e78d8e19895..ea04995fe42c 100644
--- a/tools/testing/cxl/test/cxl.c
+++ b/tools/testing/cxl/test/cxl.c
@@ -396,7 +396,8 @@ static void depopulate_all_mock_resources(void)
 
 static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
 {
-	struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
+	struct cxl_mock_res *res __free(kfree) =
+		kzalloc(sizeof(*res), GFP_KERNEL);
 	struct genpool_data_align data = {
 		.align = align,
 	};
@@ -416,7 +417,7 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
 	list_add(&res->list, &mock_res);
 	mutex_unlock(&mock_res_lock);
 
-	return res;
+	return_ptr(res);
 }
 
 static int populate_cedt(void)

  parent reply	other threads:[~2023-07-17 23:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20230717041609.1162445-1-michenyuan@huawei.com>
2023-07-17 10:50 ` [PATCH] cxl: Fix memory leak bug in alloc_mock_res() Jonathan Cameron
2023-07-17 23:49 ` Dan Williams [this message]
2023-07-18  2:53   ` Dan Williams

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=64b5d3a033e5c_45a6294f1@dwillia2-xfh.jf.intel.com.notmuch \
    --to=dan.j.williams@intel.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=alison.schofield@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michenyuan@huawei.com \
    --cc=rrichter@amd.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox