diff mbox series

[1/2] mm: vmpressure: don't need call kfree if kstrndup fails

Message ID 1581398649-125989-1-git-send-email-yang.shi@linux.alibaba.com (mailing list archive)
State New, archived
Headers show
Series [1/2] mm: vmpressure: don't need call kfree if kstrndup fails | expand

Commit Message

Yang Shi Feb. 11, 2020, 5:24 a.m. UTC
When kstrndup fails (returns NULL) there is no memory is allocated by
kmalloc, so no need to call kfree().

Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
---
 mm/vmpressure.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

David Rientjes Feb. 12, 2020, 2:07 a.m. UTC | #1
On Tue, 11 Feb 2020, Yang Shi wrote:

> When kstrndup fails (returns NULL) there is no memory is allocated by
> kmalloc, so no need to call kfree().
> 
> Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>

Acked-by: David Rientjes <rientjes@google.com>
David Hildenbrand Feb. 12, 2020, 11:21 a.m. UTC | #2
On 11.02.20 06:24, Yang Shi wrote:
> When kstrndup fails (returns NULL) there is no memory is allocated by
> kmalloc, so no need to call kfree().

"When kstrndup fails, no memory was allocated and we can exit directly."

Reviewed-by: David Hildenbrand <david@redhat.com>

> 
> Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
> ---
>  mm/vmpressure.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/mm/vmpressure.c b/mm/vmpressure.c
> index 4bac22f..0590f00 100644
> --- a/mm/vmpressure.c
> +++ b/mm/vmpressure.c
> @@ -371,10 +371,8 @@ int vmpressure_register_event(struct mem_cgroup *memcg,
>  	int ret = 0;
>  
>  	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
> -	if (!spec) {
> -		ret = -ENOMEM;
> -		goto out;
> -	}
> +	if (!spec)
> +		return -ENOMEM;
>  
>  	/* Find required level */
>  	token = strsep(&spec, ",");
>
Yang Shi Feb. 13, 2020, 3:14 a.m. UTC | #3
On 2/12/20 3:21 AM, David Hildenbrand wrote:
> On 11.02.20 06:24, Yang Shi wrote:
>> When kstrndup fails (returns NULL) there is no memory is allocated by
>> kmalloc, so no need to call kfree().
> "When kstrndup fails, no memory was allocated and we can exit directly."

Thanks for correcting the commit log.

Andrew, do you prefer I send an updated version or you would just update 
the patch in -mm tree?

>
> Reviewed-by: David Hildenbrand <david@redhat.com>
>
>> Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
>> ---
>>   mm/vmpressure.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/mm/vmpressure.c b/mm/vmpressure.c
>> index 4bac22f..0590f00 100644
>> --- a/mm/vmpressure.c
>> +++ b/mm/vmpressure.c
>> @@ -371,10 +371,8 @@ int vmpressure_register_event(struct mem_cgroup *memcg,
>>   	int ret = 0;
>>   
>>   	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
>> -	if (!spec) {
>> -		ret = -ENOMEM;
>> -		goto out;
>> -	}
>> +	if (!spec)
>> +		return -ENOMEM;
>>   
>>   	/* Find required level */
>>   	token = strsep(&spec, ",");
>>
>
Andrew Morton Feb. 13, 2020, 4:48 a.m. UTC | #4
On Wed, 12 Feb 2020 19:14:27 -0800 Yang Shi <yang.shi@linux.alibaba.com> wrote:

> On 2/12/20 3:21 AM, David Hildenbrand wrote:
> > On 11.02.20 06:24, Yang Shi wrote:
> >> When kstrndup fails (returns NULL) there is no memory is allocated by
> >> kmalloc, so no need to call kfree().
> > "When kstrndup fails, no memory was allocated and we can exit directly."
> 
> Thanks for correcting the commit log.
> 
> Andrew, do you prefer I send an updated version or you would just update 
> the patch in -mm tree?

I have already done this.

From: Yang Shi <yang.shi@linux.alibaba.com>
Subject: mm: vmpressure: don't need call kfree if kstrndup fails

When kstrndup fails, no memory was allocated and we can exit directly.

[david@redhat.com: reword changelog]
Link: http://lkml.kernel.org/r/1581398649-125989-1-git-send-email-yang.shi@linux.alibaba.com
Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: David Hildenbrand <david@redhat.com>
Acked-by: David Rientjes <rientjes@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 mm/vmpressure.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

--- a/mm/vmpressure.c~mm-vmpressure-dont-need-call-kfree-if-kstrndup-fails
+++ a/mm/vmpressure.c
@@ -371,10 +371,8 @@ int vmpressure_register_event(struct mem
 	int ret = 0;
 
 	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
-	if (!spec) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!spec)
+		return -ENOMEM;
 
 	/* Find required level */
 	token = strsep(&spec, ",");
Yang Shi Feb. 13, 2020, 4:51 a.m. UTC | #5
On 2/12/20 8:48 PM, Andrew Morton wrote:
> On Wed, 12 Feb 2020 19:14:27 -0800 Yang Shi <yang.shi@linux.alibaba.com> wrote:
>
>> On 2/12/20 3:21 AM, David Hildenbrand wrote:
>>> On 11.02.20 06:24, Yang Shi wrote:
>>>> When kstrndup fails (returns NULL) there is no memory is allocated by
>>>> kmalloc, so no need to call kfree().
>>> "When kstrndup fails, no memory was allocated and we can exit directly."
>> Thanks for correcting the commit log.
>>
>> Andrew, do you prefer I send an updated version or you would just update
>> the patch in -mm tree?
> I have already done this.

Thanks!

>
> From: Yang Shi <yang.shi@linux.alibaba.com>
> Subject: mm: vmpressure: don't need call kfree if kstrndup fails
>
> When kstrndup fails, no memory was allocated and we can exit directly.
>
> [david@redhat.com: reword changelog]
> Link: http://lkml.kernel.org/r/1581398649-125989-1-git-send-email-yang.shi@linux.alibaba.com
> Signed-off-by: Yang Shi <yang.shi@linux.alibaba.com>
> Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
> Reviewed-by: David Hildenbrand <david@redhat.com>
> Acked-by: David Rientjes <rientjes@google.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> ---
>
>   mm/vmpressure.c |    6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
>
> --- a/mm/vmpressure.c~mm-vmpressure-dont-need-call-kfree-if-kstrndup-fails
> +++ a/mm/vmpressure.c
> @@ -371,10 +371,8 @@ int vmpressure_register_event(struct mem
>   	int ret = 0;
>   
>   	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
> -	if (!spec) {
> -		ret = -ENOMEM;
> -		goto out;
> -	}
> +	if (!spec)
> +		return -ENOMEM;
>   
>   	/* Find required level */
>   	token = strsep(&spec, ",");
> _
diff mbox series

Patch

diff --git a/mm/vmpressure.c b/mm/vmpressure.c
index 4bac22f..0590f00 100644
--- a/mm/vmpressure.c
+++ b/mm/vmpressure.c
@@ -371,10 +371,8 @@  int vmpressure_register_event(struct mem_cgroup *memcg,
 	int ret = 0;
 
 	spec_orig = spec = kstrndup(args, MAX_VMPRESSURE_ARGS_LEN, GFP_KERNEL);
-	if (!spec) {
-		ret = -ENOMEM;
-		goto out;
-	}
+	if (!spec)
+		return -ENOMEM;
 
 	/* Find required level */
 	token = strsep(&spec, ",");