Skip to contentSkip to conversation

mm/gup: Fix memory leak in __gup_benchmark_ioctl

PATCH Superseded Navid Emamdoost 6 participants 10 messages Dec 11, 2019 – Dec 16, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/gup: Fix memory leak in __gup_benchmark_ioctl

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree().

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 1 +
 1 file changed, 1 insertion(+)
mm/gup_benchmark.c +1 -0
index 7dd602d7f8db..b160638f647e 100644
@@ -63,6 +63,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
+			kvfree(pages);
 			return -1;
 		}
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree().

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 1 +
 1 file changed, 1 insertion(+)
mm/gup_benchmark.c +0 -0
index 7dd602d7f8db..b160638f647e 100644

mm/gup_benchmark.c static int __gup_benchmark_ioctl(unsigned int cmd, · line 66

Quoted 4 lines
mm/gup_benchmark.c +1 -0
@@ -63,6 +63,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
+			kvfree(pages);

I wonder if adding a ret value and a goto where the free is done would be better. But may be overkill at this time. So...

Quoted 6 lines
mm/gup_benchmark.c +0 -0
 			return -1;
 		}
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Navid Emamdoost
Quoted 15 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree().

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 1 +
 1 file changed, 1 insertion(+)
mm/gup_benchmark.c +0 -0
index 7dd602d7f8db..b160638f647e 100644

mm/gup_benchmark.c static int __gup_benchmark_ioctl(unsigned int cmd, · line 69

Quoted 7 lines
mm/gup_benchmark.c +1 -0
@@ -63,6 +63,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
+			kvfree(pages);
 			return -1;
 		}
 

Hi,

The patch is correct, but I would like to second Ira's request for a ret value, and a "goto done" to use a single place to kvfree, if you don't mind.

Either way, you can add:

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
maintainer
#3 lore raw
Quoted 26 lines · On Fri, 13 Dec 2019 13:40:15 -0800 John Hubbard <[email protected]> wrote:
Quoted 15 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree().

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 1 +
 1 file changed, 1 insertion(+)
mm/gup_benchmark.c +0 -0
index 7dd602d7f8db..b160638f647e 100644

mm/gup_benchmark.c static int __gup_benchmark_ioctl(unsigned int cmd, · line 69

Quoted 7 lines
mm/gup_benchmark.c +1 -0
@@ -63,6 +63,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
+			kvfree(pages);
 			return -1;
 		}
 

Hi,

Quoted 3 lines

The patch is correct, but I would like to second Ira's request for a ret value, and a "goto done" to use a single place to kvfree, if you don't mind.

Fair enough.

And let's make it return -EINVAL rather than -1, which appears to be -EPERM.

mm/gup_benchmark.c renamed from mm/gup_benchmark.c~mm-gup-fix-memory-leak-in-__gup_benchmark_ioctl-fix +5 -2
@@ -26,6 +26,7 @@ static int __gup_benchmark_ioctl(unsigne
 	unsigned long i, nr_pages, addr, next;
 	int nr;
 	struct page **pages;
+	int ret = 0;
 
 	if (gup->size > ULONG_MAX)
 		return -EINVAL;
@@ -64,7 +65,8 @@ static int __gup_benchmark_ioctl(unsigne
 			break;
 		default:
 			kvfree(pages);
-			return -1;
+			ret = -EINVAL;
+			goto out;
 		}
 
 		if (nr <= 0)
@@ -86,7 +88,8 @@ static int __gup_benchmark_ioctl(unsigne
 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
 	kvfree(pages);
-	return 0;
+out:
+	return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,

_

#4 lore raw
Quoted 29 lines · Andrew Morton
Quoted 26 lines · On Fri, 13 Dec 2019 13:40:15 -0800 John Hubbard <[email protected]> wrote:
Quoted 15 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree().

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 1 +
 1 file changed, 1 insertion(+)
mm/gup_benchmark.c +0 -0
index 7dd602d7f8db..b160638f647e 100644

mm/gup_benchmark.c static int __gup_benchmark_ioctl(unsigned int cmd, · line 69

Quoted 7 lines
mm/gup_benchmark.c +1 -0
@@ -63,6 +63,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
                                        NULL);
                    break;
            default:
+                   kvfree(pages);
                    return -1;
            }
 

Hi,

Quoted 8 lines
Quoted 3 lines

The patch is correct, but I would like to second Ira's request for a ret value, and a "goto done" to use a single place to kvfree, if you don't mind.

Fair enough.

And let's make it return -EINVAL rather than -1, which appears to be -EPERM.

Sure! patch v2 has been sent.

Quoted 33 lines
mm/gup_benchmark.c renamed from mm/gup_benchmark.c~mm-gup-fix-memory-leak-in-__gup_benchmark_ioctl-fix +5 -2
@@ -26,6 +26,7 @@ static int __gup_benchmark_ioctl(unsigne
        unsigned long i, nr_pages, addr, next;
        int nr;
        struct page **pages;
+       int ret = 0;
 
        if (gup->size > ULONG_MAX)
                return -EINVAL;
@@ -64,7 +65,8 @@ static int __gup_benchmark_ioctl(unsigne
                        break;
                default:
                        kvfree(pages);
-                       return -1;
+                       ret = -EINVAL;
+                       goto out;
                }
 
                if (nr <= 0)
@@ -86,7 +88,8 @@ static int __gup_benchmark_ioctl(unsigne
        gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
        kvfree(pages);
-       return 0;
+out:
+       return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,

_

Signature · 1 line
Navid.
#5 lore raw

PATCH v2 1/1 mm/gup: Fix memory leak in __gup_benchmark_ioctl

In reply to John Hubbard

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree() by goto done.

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")

---
Changes in v2:
-- added goto and ret value instead of return -1.

---
 mm/gup_benchmark.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/gup_benchmark.c +5 -4
index b160638f647e..b773b2568544 100644
@@ -24,7 +24,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 {
 	ktime_t start_time, end_time;
 	unsigned long i, nr_pages, addr, next;
-	int nr;
+	int nr, ret = 0;
 	struct page **pages;
 
 	if (gup->size > ULONG_MAX)
@@ -63,8 +63,8 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
-			kvfree(pages);
-			return -1;
+			ret = -EINVAL;
+			goto done;
 		}
 
 		if (nr <= 0)
@@ -85,8 +85,9 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 	end_time = ktime_get();
 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
+done:
 	kvfree(pages);
-	return 0;
+	return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#6 lore raw
Quoted 6 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree() by goto done.

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
Quoted 46 lines

---
Changes in v2:
-- added goto and ret value instead of return -1.

---
 mm/gup_benchmark.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/gup_benchmark.c +5 -4
index b160638f647e..b773b2568544 100644
@@ -24,7 +24,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 {
 	ktime_t start_time, end_time;
 	unsigned long i, nr_pages, addr, next;
-	int nr;
+	int nr, ret = 0;
 	struct page **pages;
 
 	if (gup->size > ULONG_MAX)
@@ -63,8 +63,8 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
-			kvfree(pages);
-			return -1;
+			ret = -EINVAL;
+			goto done;
 		}
 
 		if (nr <= 0)
@@ -85,8 +85,9 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 	end_time = ktime_get();
 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
+done:
 	kvfree(pages);
-	return 0;
+	return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#7 lore raw
In reply to Navid Emamdoost
Quoted 4 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree() by goto done.

Quoted 9 lines
Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")

---
Changes in v2:
-- added goto and ret value instead of return -1.

---
 mm/gup_benchmark.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 36 lines
mm/gup_benchmark.c +5 -4
index b160638f647e..b773b2568544 100644
@@ -24,7 +24,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 {
 	ktime_t start_time, end_time;
 	unsigned long i, nr_pages, addr, next;
-	int nr;
+	int nr, ret = 0;
 	struct page **pages;
 
 	if (gup->size > ULONG_MAX)
@@ -63,8 +63,8 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
-			kvfree(pages);
-			return -1;
+			ret = -EINVAL;
+			goto done;
 		}
 
 		if (nr <= 0)
@@ -85,8 +85,9 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 	end_time = ktime_get();
 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
+done:
 	kvfree(pages);
-	return 0;
+	return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,
#8 lore raw
In reply to Navid Emamdoost

…

Quoted 1 line

+++ b/mm/gup_benchmark.c

…

mm/gup_benchmark.c static int __gup_benchmark_ioctl(unsigned int cmd, · line 93

Quoted 11 lines
mm/gup_benchmark.c +2 -1
@@ -85,8 +85,9 @@  static int __gup_benchmark_ioctl(unsigned int cmd,
 	end_time = ktime_get();
 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
+done:
 	kvfree(pages);
-	return 0;
+	return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,

Can the addition of a label like “free_pages” be more appropriate here?

Regards,
Markus

Reviewed-by on v2 1/1
#9 lore raw
In reply to Navid Emamdoost
Quoted 39 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() the allocated pages should be released before returning in case of an invalid cmd. Release pages via kvfree() by goto done.

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")

---
Changes in v2:
-- added goto and ret value instead of return -1.

---
 mm/gup_benchmark.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
mm/gup_benchmark.c +3 -3
index b160638f647e..b773b2568544 100644
@@ -24,7 +24,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 {
 	ktime_t start_time, end_time;
 	unsigned long i, nr_pages, addr, next;
-	int nr;
+	int nr, ret = 0;
 	struct page **pages;
 
 	if (gup->size > ULONG_MAX)
@@ -63,8 +63,8 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
-			kvfree(pages);
-			return -1;
+			ret = -EINVAL;
+			goto done;
 		}
 
 		if (nr <= 0)

mm/gup_benchmark.c static int __gup_benchmark_ioctl(unsigned int cmd, · line 93

Quoted 10 lines
mm/gup_benchmark.c +2 -1
@@ -85,8 +85,9 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 	end_time = ktime_get();
 	gup->put_delta_usec = ktime_us_delta(end_time, start_time);
 
+done:
 	kvfree(pages);
-	return 0;
+	return ret;
 }
 
 static long gup_benchmark_ioctl(struct file *filep, unsigned int cmd,

You should really CC people that give review feedback in previous iterations (IOW me :) ) and properly tag versions/mention code change since the last version.

(v1 was already <[email protected]>, this here would be v3)

Also, this patch here is not a standalone patch, it is an addon on top of <[email protected]>

Enough rambling :) This squashed into <[email protected]>:

I agree that the label could have a better name (e.g., out_free)

Signature · 3 lines
Thanks,

David / dhildenb