Skip to contentSkip to conversation

mm: vmpressure: don't need call kfree if kstrndup fails

PATCH Applied Yang Shi 5 participants 11 messages Feb 11, 2020 – Feb 13, 2020

Subsystems (2)
lore
Share & tools
lore

Yang Shi

author
#0 lore raw

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().

---
 mm/vmpressure.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/vmpressure.c +2 -4
index 4bac22f..0590f00 100644
@@ -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, ",");
Signature · 1 line
1.8.3.1
author
#1 lore raw

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.

---
 mm/vmpressure.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmpressure.c +1 -1
index 0590f00..d69019f 100644
@@ -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);
Signature · 1 line
1.8.3.1
Acked-by on v1 2/2
#3 lore raw

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

In reply to Yang Shi
Quoted 2 lines · Yang Shi

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.

Quoted 1 line

Thanks!

Quoted 20 lines
---
 mm/vmpressure.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmpressure.c +1 -1
index 0590f00..d69019f 100644
@@ -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);
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#4 lore raw

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

Quoted 6 lines · Michal Hocko
Quoted 2 lines · Yang Shi

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.

Quoted 3 lines
Quoted 2 lines

Thanks.

Quoted 23 lines

Thanks!

Quoted 20 lines
---
  mm/vmpressure.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmpressure.c +1 -1
index 0590f00..d69019f 100644
@@ -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);
Signature · 1 line
1.8.3.1
Reviewed-by on v1 2/2
#5 lore raw

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

In reply to Yang Shi
Quoted 14 lines · Yang Shi

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

---
 mm/vmpressure.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/vmpressure.c +0 -0
index 0590f00..d69019f 100644

mm/vmpressure.c void vmpressure(gfp_t gfp, struct mem_cgroup *memcg, bool tree, · line 286

Quoted 8 lines
mm/vmpressure.c +1 -1
@@ -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);
Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by on v1 1/2
#7 lore raw
In reply to Yang Shi
Quoted 2 lines · Yang Shi

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."

Quoted 24 lines
---
 mm/vmpressure.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/vmpressure.c +2 -4
index 4bac22f..0590f00 100644
@@ -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, ",");
Signature · 3 lines
Thanks,

David / dhildenb
author
#8 lore raw
Quoted 4 lines · David Hildenbrand
Quoted 2 lines · Yang Shi

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?

Quoted 27 lines
Quoted 23 lines
---
  mm/vmpressure.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
mm/vmpressure.c +2 -4
index 4bac22f..0590f00 100644
@@ -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, ",");
maintainer Reviewed-by Reviewed-by David Hildenbrand Acked-by David Rientjes
#9 lore raw
Quoted 10 lines · On Wed, 12 Feb 2020 19:14:27 -0800 Yang Shi <[email protected]> wrote:
Quoted 4 lines · David Hildenbrand
Quoted 2 lines · Yang Shi

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.

---

 mm/vmpressure.c |    6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
mm/vmpressure.c renamed from mm/vmpressure.c~mm-vmpressure-dont-need-call-kfree-if-kstrndup-fails +2 -4
@@ -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, ",");

_

author
#10 lore raw
Quoted 4 lines · Andrew Morton
Quoted 2 lines · On Wed, 12 Feb 2020 19:14:27 -0800 Yang Shi <[email protected]> wrote:
Quoted 1 line · David Hildenbrand

On 11.02.20 06:24, Yang Shi wrote:

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

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!

Quoted 34 lines

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.

---

  mm/vmpressure.c |    6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
mm/vmpressure.c renamed from mm/vmpressure.c~mm-vmpressure-dont-need-call-kfree-if-kstrndup-fails +2 -4
@@ -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, ",");

_