2023-09-26 21:00:58

by Wilczynski, Michal

[permalink] [raw]
Subject: [PATCH v1 0/2] Fix memory leak and move to modern scope based rollback

In acpi_nfit_init_interleave_set() there is a memory leak + improper use
of devm_*() family of functions for local memory allocations. This patch
series provides two commits - one is meant as a bug fix, and could
potentially be backported, and the other one improves old style rollback
with scope based, similar to C++ RAII [1].

Link: https://lwn.net/Articles/934679/ [1]

Michal Wilczynski (2):
ACPI: NFIT: Fix memory leak, and local use of devm_*()
ACPI: NFIT: Use modern scope based rollback

drivers/acpi/nfit/core.c | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)

--
2.41.0


2023-09-27 03:11:44

by Wilczynski, Michal

[permalink] [raw]
Subject: [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback

Change rollback in acpi_nfit_init_interleave_set() to use modern scope
based attribute __free(). This is similar to C++ RAII and is a preferred
way for handling local memory allocations.

Suggested-by: Dave Jiang <[email protected]>
Suggested-by: Andy Shevchenko <[email protected]>
Signed-off-by: Michal Wilczynski <[email protected]>
---
drivers/acpi/nfit/core.c | 32 ++++++++++----------------------
1 file changed, 10 insertions(+), 22 deletions(-)

diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c
index 78f0f855c4de..079bd663495f 100644
--- a/drivers/acpi/nfit/core.c
+++ b/drivers/acpi/nfit/core.c
@@ -2257,29 +2257,23 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
struct nd_region_desc *ndr_desc,
struct acpi_nfit_system_address *spa)
{
+ u16 nr = ndr_desc->num_mappings;
+ struct nfit_set_info2 *info2 __free(kfree) =
+ kcalloc(nr, sizeof(*info2), GFP_KERNEL);
+ struct nfit_set_info *info __free(kfree) =
+ kcalloc(nr, sizeof(*info), GFP_KERNEL);
struct device *dev = acpi_desc->dev;
struct nd_interleave_set *nd_set;
- u16 nr = ndr_desc->num_mappings;
- struct nfit_set_info2 *info2;
- struct nfit_set_info *info;
- int err = 0;
int i;

+ if (!info || !info2)
+ return -ENOMEM;
+
nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
if (!nd_set)
return -ENOMEM;
import_guid(&nd_set->type_guid, spa->range_guid);

- info = kcalloc(nr, sizeof(*info), GFP_KERNEL);
- if (!info)
- return -ENOMEM;
-
- info2 = kcalloc(nr, sizeof(*info2), GFP_KERNEL);
- if (!info2) {
- err = -ENOMEM;
- goto free_info;
- }
-
for (i = 0; i < nr; i++) {
struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
struct nvdimm *nvdimm = mapping->nvdimm;
@@ -2292,8 +2286,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,

if (!memdev || !nfit_mem->dcr) {
dev_err(dev, "%s: failed to find DCR\n", __func__);
- err = -ENODEV;
- goto free_info2;
+ return -ENODEV;
}

map->region_offset = memdev->region_offset;
@@ -2342,12 +2335,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,

ndr_desc->nd_set = nd_set;

-free_info2:
- kfree(info2);
-free_info:
- kfree(info);
-
- return err;
+ return 0;
}

static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
--
2.41.0

2023-09-29 06:37:53

by Dave Jiang

[permalink] [raw]
Subject: Re: [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback



On 9/26/23 11:45, Michal Wilczynski wrote:
> Change rollback in acpi_nfit_init_interleave_set() to use modern scope
> based attribute __free(). This is similar to C++ RAII and is a preferred
> way for handling local memory allocations.
>
> Suggested-by: Dave Jiang <[email protected]>
> Suggested-by: Andy Shevchenko <[email protected]>
> Signed-off-by: Michal Wilczynski <[email protected]>

Reviewed-by: Dave Jiang <[email protected]>
> ---
> drivers/acpi/nfit/core.c | 32 ++++++++++----------------------
> 1 file changed, 10 insertions(+), 22 deletions(-)
>
> diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c
> index 78f0f855c4de..079bd663495f 100644
> --- a/drivers/acpi/nfit/core.c
> +++ b/drivers/acpi/nfit/core.c
> @@ -2257,29 +2257,23 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
> struct nd_region_desc *ndr_desc,
> struct acpi_nfit_system_address *spa)
> {
> + u16 nr = ndr_desc->num_mappings;
> + struct nfit_set_info2 *info2 __free(kfree) =
> + kcalloc(nr, sizeof(*info2), GFP_KERNEL);
> + struct nfit_set_info *info __free(kfree) =
> + kcalloc(nr, sizeof(*info), GFP_KERNEL);
> struct device *dev = acpi_desc->dev;
> struct nd_interleave_set *nd_set;
> - u16 nr = ndr_desc->num_mappings;
> - struct nfit_set_info2 *info2;
> - struct nfit_set_info *info;
> - int err = 0;
> int i;
>
> + if (!info || !info2)
> + return -ENOMEM;
> +
> nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
> if (!nd_set)
> return -ENOMEM;
> import_guid(&nd_set->type_guid, spa->range_guid);
>
> - info = kcalloc(nr, sizeof(*info), GFP_KERNEL);
> - if (!info)
> - return -ENOMEM;
> -
> - info2 = kcalloc(nr, sizeof(*info2), GFP_KERNEL);
> - if (!info2) {
> - err = -ENOMEM;
> - goto free_info;
> - }
> -
> for (i = 0; i < nr; i++) {
> struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
> struct nvdimm *nvdimm = mapping->nvdimm;
> @@ -2292,8 +2286,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
>
> if (!memdev || !nfit_mem->dcr) {
> dev_err(dev, "%s: failed to find DCR\n", __func__);
> - err = -ENODEV;
> - goto free_info2;
> + return -ENODEV;
> }
>
> map->region_offset = memdev->region_offset;
> @@ -2342,12 +2335,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
>
> ndr_desc->nd_set = nd_set;
>
> -free_info2:
> - kfree(info2);
> -free_info:
> - kfree(info);
> -
> - return err;
> + return 0;
> }
>
> static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,

2023-10-02 21:28:16

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback

On Tue, Sep 26, 2023 at 09:45:20PM +0300, Michal Wilczynski wrote:
> Change rollback in acpi_nfit_init_interleave_set() to use modern scope
> based attribute __free(). This is similar to C++ RAII and is a preferred
> way for handling local memory allocations.

LGTM,
Reviewed-by: Andy Shevchenko <[email protected]>

--
With Best Regards,
Andy Shevchenko


2023-10-11 14:27:22

by Wilczynski, Michal

[permalink] [raw]
Subject: Re: [PATCH v1 0/2] Fix memory leak and move to modern scope based rollback



On 9/26/2023 8:45 PM, Michal Wilczynski wrote:
> In acpi_nfit_init_interleave_set() there is a memory leak + improper use
> of devm_*() family of functions for local memory allocations. This patch
> series provides two commits - one is meant as a bug fix, and could
> potentially be backported, and the other one improves old style rollback
> with scope based, similar to C++ RAII [1].
>
> Link: https://lwn.net/Articles/934679/ [1]
>
> Michal Wilczynski (2):
> ACPI: NFIT: Fix memory leak, and local use of devm_*()
> ACPI: NFIT: Use modern scope based rollback
>
> drivers/acpi/nfit/core.c | 21 ++++++++-------------
> 1 file changed, 8 insertions(+), 13 deletions(-)

Hi Dan,
Do you think this patchset is fine ? Would you like me to
change anything ?

Regards,
MichaƂ

>