2020-02-11 05:59:15

by Yang Shi

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

When kstrndup fails (returns NULL) there is no memory is allocated by
kmalloc, so no need to call kfree().

Signed-off-by: Yang Shi <[email protected]>
---
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, ",");
--
1.8.3.1


2020-02-11 05:59:48

by Yang Shi

[permalink] [raw]
Subject: [PATCH 2/2] mm: vmpressure: use mem_cgroup_is_root API

Use mem_cgroup_is_root() API to check if memcg is root memcg instead of
open coding.

Signed-off-by: Yang Shi <[email protected]>
---
mm/vmpressure.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/vmpressure.c b/mm/vmpressure.c
index 0590f00..d69019f 100644
--- a/mm/vmpressure.c
+++ b/mm/vmpressure.c
@@ -280,7 +280,7 @@ void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
enum vmpressure_levels level;

/* For now, no users for root-level efficiency */
- if (!memcg || memcg == root_mem_cgroup)
+ if (!memcg || mem_cgroup_is_root(memcg))
return;

spin_lock(&vmpr->sr_lock);
--
1.8.3.1

2020-02-12 02:09:05

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH 2/2] mm: vmpressure: use mem_cgroup_is_root API

On Tue, 11 Feb 2020, Yang Shi wrote:

> Use mem_cgroup_is_root() API to check if memcg is root memcg instead of
> open coding.
>
> Signed-off-by: Yang Shi <[email protected]>

Acked-by: David Rientjes <[email protected]>

2020-02-12 02:09:08

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH 1/2] mm: vmpressure: don't need call kfree if kstrndup fails

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 <[email protected]>

Acked-by: David Rientjes <[email protected]>

2020-02-12 08:24:51

by Michal Hocko

[permalink] [raw]
Subject: Re: [PATCH 2/2] mm: vmpressure: use mem_cgroup_is_root API

On Tue 11-02-20 13:24:09, Yang Shi wrote:
> Use mem_cgroup_is_root() API to check if memcg is root memcg instead of
> open coding.

Yes, the direct use outside of memcontrol.c should be really an
exception. The only other similar case is cgwb_bdi_init and there is no
easy way to replace - except for adding a helper which is not worth it.

> Signed-off-by: Yang Shi <[email protected]>

Acked-by: Michal Hocko <[email protected]>

Thanks!

> ---
> mm/vmpressure.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/vmpressure.c b/mm/vmpressure.c
> index 0590f00..d69019f 100644
> --- a/mm/vmpressure.c
> +++ b/mm/vmpressure.c
> @@ -280,7 +280,7 @@ void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
> enum vmpressure_levels level;
>
> /* For now, no users for root-level efficiency */
> - if (!memcg || memcg == root_mem_cgroup)
> + if (!memcg || mem_cgroup_is_root(memcg))
> return;
>
> spin_lock(&vmpr->sr_lock);
> --
> 1.8.3.1
>

--
Michal Hocko
SUSE Labs

2020-02-12 11:21:11

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH 2/2] mm: vmpressure: use mem_cgroup_is_root API

On 11.02.20 06:24, Yang Shi wrote:
> Use mem_cgroup_is_root() API to check if memcg is root memcg instead of
> open coding.
>
> Signed-off-by: Yang Shi <[email protected]>
> ---
> mm/vmpressure.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/vmpressure.c b/mm/vmpressure.c
> index 0590f00..d69019f 100644
> --- a/mm/vmpressure.c
> +++ b/mm/vmpressure.c
> @@ -280,7 +280,7 @@ void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
> enum vmpressure_levels level;
>
> /* For now, no users for root-level efficiency */
> - if (!memcg || memcg == root_mem_cgroup)
> + if (!memcg || mem_cgroup_is_root(memcg))
> return;
>
> spin_lock(&vmpr->sr_lock);
>

Reviewed-by: David Hildenbrand <[email protected]>

--
Thanks,

David / dhildenb

2020-02-12 11:22:12

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH 1/2] mm: vmpressure: don't need call kfree if kstrndup fails

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 <[email protected]>

>
> Signed-off-by: Yang Shi <[email protected]>
> ---
> 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, ",");
>


--
Thanks,

David / dhildenb

2020-02-13 03:15:54

by Yang Shi

[permalink] [raw]
Subject: Re: [PATCH 1/2] mm: vmpressure: don't need call kfree if kstrndup fails



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 <[email protected]>
>
>> Signed-off-by: Yang Shi <[email protected]>
>> ---
>> 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, ",");
>>
>

2020-02-13 03:19:54

by Yang Shi

[permalink] [raw]
Subject: Re: [PATCH 2/2] mm: vmpressure: use mem_cgroup_is_root API



On 2/12/20 12:23 AM, Michal Hocko wrote:
> On Tue 11-02-20 13:24:09, Yang Shi wrote:
>> Use mem_cgroup_is_root() API to check if memcg is root memcg instead of
>> open coding.
> Yes, the direct use outside of memcontrol.c should be really an
> exception. The only other similar case is cgwb_bdi_init and there is no
> easy way to replace - except for adding a helper which is not worth it.

Yes, it seems so. cgwb_bdi_init just deferences root_mem_cgroup to
access its css. It is the only user outside memcontrol.c, so I agree a
helper for it might be overkilling. Once we have more users, it should
be considered.

>
>> Signed-off-by: Yang Shi <[email protected]>
> Acked-by: Michal Hocko <[email protected]>

Thanks.

>
> Thanks!
>
>> ---
>> mm/vmpressure.c | 2 +-
>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/mm/vmpressure.c b/mm/vmpressure.c
>> index 0590f00..d69019f 100644
>> --- a/mm/vmpressure.c
>> +++ b/mm/vmpressure.c
>> @@ -280,7 +280,7 @@ void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree,
>> enum vmpressure_levels level;
>>
>> /* For now, no users for root-level efficiency */
>> - if (!memcg || memcg == root_mem_cgroup)
>> + if (!memcg || mem_cgroup_is_root(memcg))
>> return;
>>
>> spin_lock(&vmpr->sr_lock);
>> --
>> 1.8.3.1
>>

2020-02-13 04:49:17

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 1/2] mm: vmpressure: don't need call kfree if kstrndup fails

On Wed, 12 Feb 2020 19:14:27 -0800 Yang Shi <[email protected]> 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 <[email protected]>
Subject: mm: vmpressure: don't need call kfree if kstrndup fails

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

[[email protected]: reword changelog]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Yang Shi <[email protected]>
Reviewed-by: Andrew Morton <[email protected]>
Reviewed-by: David Hildenbrand <[email protected]>
Acked-by: David Rientjes <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
---

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, ",");
_

2020-02-13 04:51:52

by Yang Shi

[permalink] [raw]
Subject: Re: [PATCH 1/2] mm: vmpressure: don't need call kfree if kstrndup fails



On 2/12/20 8:48 PM, Andrew Morton wrote:
> On Wed, 12 Feb 2020 19:14:27 -0800 Yang Shi <[email protected]> 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 <[email protected]>
> Subject: mm: vmpressure: don't need call kfree if kstrndup fails
>
> When kstrndup fails, no memory was allocated and we can exit directly.
>
> [[email protected]: reword changelog]
> Link: http://lkml.kernel.org/r/[email protected]
> Signed-off-by: Yang Shi <[email protected]>
> Reviewed-by: Andrew Morton <[email protected]>
> Reviewed-by: David Hildenbrand <[email protected]>
> Acked-by: David Rientjes <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> ---
>
> 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, ",");
> _