mm/gup: Fix memory leak in __gup_benchmark_ioctl
Subsystems (2)
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().
--- mm/gup_benchmark.c | 1 + 1 file changed, 1 insertion(+)
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 ; }
Signature · 1 line
2.17.1
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 viakvfree().Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>--- mm/gup_benchmark.c | 1 + 1 file changed, 1 insertion(+)mm/gup_benchmark.c +0
-0
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
-0return -1 ; }Signature · 1 line
2.17.1
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 viakvfree().Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>--- mm/gup_benchmark.c | 1 + 1 file changed, 1 insertion(+)mm/gup_benchmark.c +0
-0
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
Andrew Morton
maintainerQuoted 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 viakvfree().Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>--- mm/gup_benchmark.c | 1 + 1 file changed, 1 insertion(+)mm/gup_benchmark.c +0
-0mm/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(unsigneunsigned 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(unsignebreak ;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,
_
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 viakvfree().Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>--- mm/gup_benchmark.c | 1 + 1 file changed, 1 insertion(+)mm/gup_benchmark.c +0
-0mm/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
-EINVALrather 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(unsigneunsigned 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(unsignebreak ;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.
PATCH v2 1/1 mm/gup: Fix memory leak in __gup_benchmark_ioctl
In reply to John HubbardIn 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.
---
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
@@ -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
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 viakvfree()by goto done.Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>
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@@ -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
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 viakvfree()by goto done.
Quoted 9 lines
Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>---
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@@ -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,
…
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
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 viakvfree()by goto done.Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")Signed-off-by Navid Emamdoost <[email protected]>---
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@@ -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