[PATCH v2] lib/string_helpers: fix not adding strarray to device's resource list.

Puyou Lu posted 1 patch 4 years ago
lib/string_helpers.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH v2] lib/string_helpers: fix not adding strarray to device's resource list.
Posted by Puyou Lu 4 years ago
Add allocated strarray to device's resource list. This is a must to
automatically release strarray when the device disappears.

Fixes: acdb89b6c87a ("lib/string_helpers: Introduce managed variant of kasprintf_strarray()")
Signed-off-by: Puyou Lu <puyou.lu@gmail.com>

---

Change since v1:
also set 'n' of strarray https://lore.kernel.org/lkml/20220506022845.26750-1-puyou.lu@gmail.com/

---
 lib/string_helpers.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 4f877e9551d5..5ed3beb066e6 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -757,6 +757,9 @@ char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
 		return ERR_PTR(-ENOMEM);
 	}
 
+	ptr->n = n;
+	devres_add(dev, ptr);
+
 	return ptr->array;
 }
 EXPORT_SYMBOL_GPL(devm_kasprintf_strarray);
-- 
2.17.1
Re: [PATCH v2] lib/string_helpers: fix not adding strarray to device's resource list.
Posted by Andrew Morton 4 years ago
On Fri,  6 May 2022 15:36:22 +0800 Puyou Lu <puyou.lu@gmail.com> wrote:

> Add allocated strarray to device's resource list. This is a must to
> automatically release strarray when the device disappears.

So at present we have a memory leak?

Is this likely to be serious enough to justify backporting the fix into
-stable kernels?

> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -757,6 +757,9 @@ char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
>  		return ERR_PTR(-ENOMEM);
>  	}
>  
> +	ptr->n = n;
> +	devres_add(dev, ptr);
> +
>  	return ptr->array;
>  }
>  EXPORT_SYMBOL_GPL(devm_kasprintf_strarray);
Re: [PATCH v2] lib/string_helpers: fix not adding strarray to device's resource list.
Posted by Andy Shevchenko 4 years ago
On Sat, May 07, 2022 at 11:45:16AM -0700, Andrew Morton wrote:
> On Fri,  6 May 2022 15:36:22 +0800 Puyou Lu <puyou.lu@gmail.com> wrote:
> 
> > Add allocated strarray to device's resource list. This is a must to
> > automatically release strarray when the device disappears.
> 
> So at present we have a memory leak?

Yes.

> Is this likely to be serious enough to justify backporting the fix into
> -stable kernels?

I think so, however there are only few drivers are using it, hence it's
serious, but rare.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] lib/string_helpers: fix not adding strarray to device's resource list.
Posted by Andy Shevchenko 4 years ago
On Fri, May 6, 2022 at 9:36 AM Puyou Lu <puyou.lu@gmail.com> wrote:
>
> Add allocated strarray to device's resource list. This is a must to
> automatically release strarray when the device disappears.

LGTM,
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Thanks!

> Fixes: acdb89b6c87a ("lib/string_helpers: Introduce managed variant of kasprintf_strarray()")
> Signed-off-by: Puyou Lu <puyou.lu@gmail.com>
>
> ---
>
> Change since v1:
> also set 'n' of strarray https://lore.kernel.org/lkml/20220506022845.26750-1-puyou.lu@gmail.com/
>
> ---
>  lib/string_helpers.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> index 4f877e9551d5..5ed3beb066e6 100644
> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -757,6 +757,9 @@ char **devm_kasprintf_strarray(struct device *dev, const char *prefix, size_t n)
>                 return ERR_PTR(-ENOMEM);
>         }
>
> +       ptr->n = n;
> +       devres_add(dev, ptr);
> +
>         return ptr->array;
>  }
>  EXPORT_SYMBOL_GPL(devm_kasprintf_strarray);
> --
> 2.17.1
>


-- 
With Best Regards,
Andy Shevchenko