2022-01-17 07:07:52

by Christophe JAILLET

[permalink] [raw]
Subject: [PATCH v2] lib/string_helpers: Use the given gfp flag when allocating memory

kstrdup_quotable_cmdline() and kstrdup_quotable_file() are given a gfp flag
that is passed and used for memory allocation in kstrdup_quotable() just a
few lines below.

It looks reasonable to use this gfp value for the buffer allocated and
freed in these functions as well.

Fixes: 0ee931c4e31a ("mm: treewide: remove GFP_TEMPORARY allocation flag")
Signed-off-by: Christophe JAILLET <[email protected]>
---
According to what I've found in 5.16, all callers use GFP_KERNEL, so this
patch should be a no-op.
But who knows how it will be used in the future. Better safe than sorry.

v2: Add the change for kstrdup_quotable_file()
---
lib/string_helpers.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 90f9f1b7afec..4433e486b725 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -624,7 +624,7 @@ char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
char *buffer, *quoted;
int i, res;

- buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ buffer = kmalloc(PAGE_SIZE, gfp);
if (!buffer)
return NULL;

@@ -660,7 +660,7 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
return kstrdup("<unknown>", gfp);

/* We add 11 spaces for ' (deleted)' to be appended */
- temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
+ temp = kmalloc(PATH_MAX + 11, gfp);
if (!temp)
return kstrdup("<no_memory>", gfp);

--
2.32.0


2022-01-24 19:22:33

by Andy Shevchenko

[permalink] [raw]
Subject: Re: [PATCH v2] lib/string_helpers: Use the given gfp flag when allocating memory

On Sun, Jan 16, 2022 at 01:53:37PM +0100, Christophe JAILLET wrote:
> kstrdup_quotable_cmdline() and kstrdup_quotable_file() are given a gfp flag
> that is passed and used for memory allocation in kstrdup_quotable() just a
> few lines below.
>
> It looks reasonable to use this gfp value for the buffer allocated and
> freed in these functions as well.

Acked-by: Andy Shevchenko <[email protected]>

> Fixes: 0ee931c4e31a ("mm: treewide: remove GFP_TEMPORARY allocation flag")
> Signed-off-by: Christophe JAILLET <[email protected]>
> ---
> According to what I've found in 5.16, all callers use GFP_KERNEL, so this
> patch should be a no-op.
> But who knows how it will be used in the future. Better safe than sorry.
>
> v2: Add the change for kstrdup_quotable_file()
> ---
> lib/string_helpers.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> index 90f9f1b7afec..4433e486b725 100644
> --- a/lib/string_helpers.c
> +++ b/lib/string_helpers.c
> @@ -624,7 +624,7 @@ char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
> char *buffer, *quoted;
> int i, res;
>
> - buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
> + buffer = kmalloc(PAGE_SIZE, gfp);
> if (!buffer)
> return NULL;
>
> @@ -660,7 +660,7 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
> return kstrdup("<unknown>", gfp);
>
> /* We add 11 spaces for ' (deleted)' to be appended */
> - temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
> + temp = kmalloc(PATH_MAX + 11, gfp);
> if (!temp)
> return kstrdup("<no_memory>", gfp);
>
> --
> 2.32.0
>

--
With Best Regards,
Andy Shevchenko


2022-01-24 21:34:51

by Christophe JAILLET

[permalink] [raw]
Subject: Re: [PATCH v2] lib/string_helpers: Use the given gfp flag when allocating memory

Le 24/01/2022 à 15:26, Andy Shevchenko a écrit :
> On Sun, Jan 16, 2022 at 01:53:37PM +0100, Christophe JAILLET wrote:
>> kstrdup_quotable_cmdline() and kstrdup_quotable_file() are given a gfp flag
>> that is passed and used for memory allocation in kstrdup_quotable() just a
>> few lines below.
>>
>> It looks reasonable to use this gfp value for the buffer allocated and
>> freed in these functions as well.
>
> Acked-by: Andy Shevchenko <[email protected]>

Hi,
just in case, another patch has been proposed in the thread related to
the v1 of this patch.
See [1].

I let you see which approach is the more relevant.

CJ

[1]: https://lore.kernel.org/linux-kernel/YeU8PhtvvXIWtTk%[email protected]/

>
>> Fixes: 0ee931c4e31a ("mm: treewide: remove GFP_TEMPORARY allocation flag")
>> Signed-off-by: Christophe JAILLET <[email protected]>
>> ---
>> According to what I've found in 5.16, all callers use GFP_KERNEL, so this
>> patch should be a no-op.
>> But who knows how it will be used in the future. Better safe than sorry.
>>
>> v2: Add the change for kstrdup_quotable_file()
>> ---
>> lib/string_helpers.c | 4 ++--
>> 1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/lib/string_helpers.c b/lib/string_helpers.c
>> index 90f9f1b7afec..4433e486b725 100644
>> --- a/lib/string_helpers.c
>> +++ b/lib/string_helpers.c
>> @@ -624,7 +624,7 @@ char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp)
>> char *buffer, *quoted;
>> int i, res;
>>
>> - buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
>> + buffer = kmalloc(PAGE_SIZE, gfp);
>> if (!buffer)
>> return NULL;
>>
>> @@ -660,7 +660,7 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp)
>> return kstrdup("<unknown>", gfp);
>>
>> /* We add 11 spaces for ' (deleted)' to be appended */
>> - temp = kmalloc(PATH_MAX + 11, GFP_KERNEL);
>> + temp = kmalloc(PATH_MAX + 11, gfp);
>> if (!temp)
>> return kstrdup("<no_memory>", gfp);
>>
>> --
>> 2.32.0
>>
>