* [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*()
2023-09-26 18:45 [PATCH v1 0/2] Fix memory leak and move to modern scope based rollback Michal Wilczynski
@ 2023-09-26 18:45 ` Michal Wilczynski
2023-09-26 18:52 ` Dave Jiang
` (2 more replies)
2023-09-26 18:45 ` [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback Michal Wilczynski
2023-10-11 14:14 ` [PATCH v1 0/2] Fix memory leak and move to " Wilczynski, Michal
2 siblings, 3 replies; 9+ messages in thread
From: Michal Wilczynski @ 2023-09-26 18:45 UTC (permalink / raw)
To: linux-acpi, linux-kernel, nvdimm
Cc: rafael.j.wysocki, andriy.shevchenko, lenb, dan.j.williams,
vishal.l.verma, ira.weiny, Michal Wilczynski, Andy Shevchenko
devm_*() family of functions purpose is managing memory attached to a
device. So in general it should only be used for allocations that should
last for the whole lifecycle of the device. This is not the case for
acpi_nfit_init_interleave_set(). There are two allocations that are only
used locally in this function. What's more - if the function exits on
error path memory is never freed. It's still attached to dev and would
be freed on device detach, so this leak could be called a 'local leak'.
Fix this by switching from devm_kcalloc() to kcalloc(), and adding
proper rollback.
Fixes: eaf961536e16 ("libnvdimm, nfit: add interleave-set state-tracking infrastructure")
Reported-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Michal Wilczynski <michal.wilczynski@intel.com>
---
drivers/acpi/nfit/core.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c
index f0e6738ae3c9..78f0f855c4de 100644
--- a/drivers/acpi/nfit/core.c
+++ b/drivers/acpi/nfit/core.c
@@ -2262,6 +2262,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
u16 nr = ndr_desc->num_mappings;
struct nfit_set_info2 *info2;
struct nfit_set_info *info;
+ int err = 0;
int i;
nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
@@ -2269,13 +2270,15 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
return -ENOMEM;
import_guid(&nd_set->type_guid, spa->range_guid);
- info = devm_kcalloc(dev, nr, sizeof(*info), GFP_KERNEL);
+ info = kcalloc(nr, sizeof(*info), GFP_KERNEL);
if (!info)
return -ENOMEM;
- info2 = devm_kcalloc(dev, nr, sizeof(*info2), GFP_KERNEL);
- if (!info2)
- 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];
@@ -2289,7 +2292,8 @@ 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__);
- return -ENODEV;
+ err = -ENODEV;
+ goto free_info2;
}
map->region_offset = memdev->region_offset;
@@ -2337,10 +2341,13 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
}
ndr_desc->nd_set = nd_set;
- devm_kfree(dev, info);
- devm_kfree(dev, info2);
- return 0;
+free_info2:
+ kfree(info2);
+free_info:
+ kfree(info);
+
+ return err;
}
static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
--
2.41.0
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*()
2023-09-26 18:45 ` [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Michal Wilczynski
@ 2023-09-26 18:52 ` Dave Jiang
2023-10-02 9:37 ` Andy Shevchenko
2023-10-12 23:40 ` Dan Williams
2 siblings, 0 replies; 9+ messages in thread
From: Dave Jiang @ 2023-09-26 18:52 UTC (permalink / raw)
To: Michal Wilczynski, linux-acpi, linux-kernel, nvdimm
Cc: rafael.j.wysocki, andriy.shevchenko, lenb, dan.j.williams,
vishal.l.verma, ira.weiny, Andy Shevchenko
On 9/26/23 11:45, Michal Wilczynski wrote:
> devm_*() family of functions purpose is managing memory attached to a
> device. So in general it should only be used for allocations that should
> last for the whole lifecycle of the device. This is not the case for
> acpi_nfit_init_interleave_set(). There are two allocations that are only
> used locally in this function. What's more - if the function exits on
> error path memory is never freed. It's still attached to dev and would
> be freed on device detach, so this leak could be called a 'local leak'.
>
> Fix this by switching from devm_kcalloc() to kcalloc(), and adding
> proper rollback.
>
> Fixes: eaf961536e16 ("libnvdimm, nfit: add interleave-set state-tracking infrastructure")
> Reported-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Signed-off-by: Michal Wilczynski <michal.wilczynski@intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> drivers/acpi/nfit/core.c | 23 +++++++++++++++--------
> 1 file changed, 15 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/acpi/nfit/core.c b/drivers/acpi/nfit/core.c
> index f0e6738ae3c9..78f0f855c4de 100644
> --- a/drivers/acpi/nfit/core.c
> +++ b/drivers/acpi/nfit/core.c
> @@ -2262,6 +2262,7 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
> u16 nr = ndr_desc->num_mappings;
> struct nfit_set_info2 *info2;
> struct nfit_set_info *info;
> + int err = 0;
> int i;
>
> nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
> @@ -2269,13 +2270,15 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
> return -ENOMEM;
> import_guid(&nd_set->type_guid, spa->range_guid);
>
> - info = devm_kcalloc(dev, nr, sizeof(*info), GFP_KERNEL);
> + info = kcalloc(nr, sizeof(*info), GFP_KERNEL);
> if (!info)
> return -ENOMEM;
>
> - info2 = devm_kcalloc(dev, nr, sizeof(*info2), GFP_KERNEL);
> - if (!info2)
> - 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];
> @@ -2289,7 +2292,8 @@ 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__);
> - return -ENODEV;
> + err = -ENODEV;
> + goto free_info2;
> }
>
> map->region_offset = memdev->region_offset;
> @@ -2337,10 +2341,13 @@ static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
> }
>
> ndr_desc->nd_set = nd_set;
> - devm_kfree(dev, info);
> - devm_kfree(dev, info2);
>
> - return 0;
> +free_info2:
> + kfree(info2);
> +free_info:
> + kfree(info);
> +
> + return err;
> }
>
> static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*()
2023-09-26 18:45 ` [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Michal Wilczynski
2023-09-26 18:52 ` Dave Jiang
@ 2023-10-02 9:37 ` Andy Shevchenko
2023-10-12 23:40 ` Dan Williams
2 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2023-10-02 9:37 UTC (permalink / raw)
To: Michal Wilczynski
Cc: linux-acpi, linux-kernel, nvdimm, rafael.j.wysocki, lenb,
dan.j.williams, vishal.l.verma, ira.weiny
On Tue, Sep 26, 2023 at 09:45:19PM +0300, Michal Wilczynski wrote:
> devm_*() family of functions purpose is managing memory attached to a
> device. So in general it should only be used for allocations that should
> last for the whole lifecycle of the device. This is not the case for
> acpi_nfit_init_interleave_set(). There are two allocations that are only
> used locally in this function. What's more - if the function exits on
> error path memory is never freed. It's still attached to dev and would
> be freed on device detach, so this leak could be called a 'local leak'.
>
> Fix this by switching from devm_kcalloc() to kcalloc(), and adding
> proper rollback.
LGTM,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*()
2023-09-26 18:45 ` [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Michal Wilczynski
2023-09-26 18:52 ` Dave Jiang
2023-10-02 9:37 ` Andy Shevchenko
@ 2023-10-12 23:40 ` Dan Williams
2 siblings, 0 replies; 9+ messages in thread
From: Dan Williams @ 2023-10-12 23:40 UTC (permalink / raw)
To: Michal Wilczynski, linux-acpi, linux-kernel, nvdimm
Cc: rafael.j.wysocki, andriy.shevchenko, lenb, dan.j.williams,
vishal.l.verma, ira.weiny, Michal Wilczynski, Andy Shevchenko
Michal Wilczynski wrote:
> devm_*() family of functions purpose is managing memory attached to a
> device. So in general it should only be used for allocations that should
> last for the whole lifecycle of the device. This is not the case for
> acpi_nfit_init_interleave_set(). There are two allocations that are only
> used locally in this function. What's more - if the function exits on
> error path memory is never freed. It's still attached to dev and would
> be freed on device detach, so this leak could be called a 'local leak'.
This analysis is incorrect devm cleans up on driver ->probe() failure in
addition to ->remove(), and these error returns result in ->probe()
failures. No leak, i.e. this is not a fix.
The conversion to modern probe is ok if you want to resubmit that one
without this intervening change.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback
2023-09-26 18:45 [PATCH v1 0/2] Fix memory leak and move to modern scope based rollback Michal Wilczynski
2023-09-26 18:45 ` [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Michal Wilczynski
@ 2023-09-26 18:45 ` Michal Wilczynski
2023-09-26 18:50 ` Dave Jiang
2023-10-02 9:38 ` Andy Shevchenko
2023-10-11 14:14 ` [PATCH v1 0/2] Fix memory leak and move to " Wilczynski, Michal
2 siblings, 2 replies; 9+ messages in thread
From: Michal Wilczynski @ 2023-09-26 18:45 UTC (permalink / raw)
To: linux-acpi, linux-kernel, nvdimm
Cc: rafael.j.wysocki, andriy.shevchenko, lenb, dan.j.williams,
vishal.l.verma, ira.weiny, Michal Wilczynski, Dave Jiang,
Andy Shevchenko
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 <dave.jiang@intel.com>
Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Signed-off-by: Michal Wilczynski <michal.wilczynski@intel.com>
---
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
^ permalink raw reply related [flat|nested] 9+ messages in thread* Re: [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback
2023-09-26 18:45 ` [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback Michal Wilczynski
@ 2023-09-26 18:50 ` Dave Jiang
2023-10-02 9:38 ` Andy Shevchenko
1 sibling, 0 replies; 9+ messages in thread
From: Dave Jiang @ 2023-09-26 18:50 UTC (permalink / raw)
To: Michal Wilczynski, linux-acpi, linux-kernel, nvdimm
Cc: rafael.j.wysocki, andriy.shevchenko, lenb, dan.j.williams,
vishal.l.verma, ira.weiny, Andy Shevchenko
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 <dave.jiang@intel.com>
> Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
> Signed-off-by: Michal Wilczynski <michal.wilczynski@intel.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> 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,
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback
2023-09-26 18:45 ` [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback Michal Wilczynski
2023-09-26 18:50 ` Dave Jiang
@ 2023-10-02 9:38 ` Andy Shevchenko
1 sibling, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2023-10-02 9:38 UTC (permalink / raw)
To: Michal Wilczynski
Cc: linux-acpi, linux-kernel, nvdimm, rafael.j.wysocki, lenb,
dan.j.williams, vishal.l.verma, ira.weiny, Dave Jiang
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 <andriy.shevchenko@linux.intel.com>
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1 0/2] Fix memory leak and move to modern scope based rollback
2023-09-26 18:45 [PATCH v1 0/2] Fix memory leak and move to modern scope based rollback Michal Wilczynski
2023-09-26 18:45 ` [PATCH v1 1/2] ACPI: NFIT: Fix memory leak, and local use of devm_*() Michal Wilczynski
2023-09-26 18:45 ` [PATCH v1 2/2] ACPI: NFIT: Use modern scope based rollback Michal Wilczynski
@ 2023-10-11 14:14 ` Wilczynski, Michal
2 siblings, 0 replies; 9+ messages in thread
From: Wilczynski, Michal @ 2023-10-11 14:14 UTC (permalink / raw)
To: linux-acpi, linux-kernel, nvdimm, Williams, Dan J
Cc: rafael.j.wysocki, andriy.shevchenko, lenb, vishal.l.verma,
ira.weiny
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ł
>
^ permalink raw reply [flat|nested] 9+ messages in thread