Skip to contentSkip to conversation

mm/gup: Fix memory leaks in __gup_benchmark_ioctl

PATCH In review Navid Emamdoost 2 participants 2 messages Nov 22, 2019 – Nov 25, 2019 Memory management

lore
Share & tools
lore
#0 lore raw

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

In the implementation of __gup_benchmark_ioctl() memory is leaked if the passed cmd is invalid. Release pages before returning -1.

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/gup_benchmark.c +7 -1
index 7dd602d7f8db..33ede5727523 100644
@@ -23,7 +23,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 		struct gup_benchmark *gup)
 {
 	ktime_t start_time, end_time;
-	unsigned long i, nr_pages, addr, next;
+	unsigned long i, j, nr_pages, addr, next;
 	int nr;
 	struct page **pages;
 
@@ -63,6 +63,12 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
+			for (j = 0; j < i; j++) {
+				if (!pages[j])
+					break;
+				put_page(pages[j]);
+			}
+			kvfree(pages);
 			return -1;
 		}
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · Navid Emamdoost

In the implementation of __gup_benchmark_ioctl() memory is leaked if the passed cmd is invalid. Release pages before returning -1.

Fixes 714a3a1ebafe ("mm/gup_benchmark.c: add additional pinning methods")
---
 mm/gup_benchmark.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/gup_benchmark.c +1 -1
index 7dd602d7f8db..33ede5727523 100644
@@ -23,7 +23,7 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 		struct gup_benchmark *gup)
 {
 	ktime_t start_time, end_time;
-	unsigned long i, nr_pages, addr, next;
+	unsigned long i, j, nr_pages, addr, next;
 	int nr;
 	struct page **pages;
 

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

Quoted 8 lines
mm/gup_benchmark.c +5 -0
@@ -63,6 +63,12 @@ static int __gup_benchmark_ioctl(unsigned int cmd,
 					    NULL);
 			break;
 		default:
+			for (j = 0; j < i; j++) {
+				if (!pages[j])
+					break;
+				put_page(pages[j]);
+			}

We didn't pin any pages, why should we release them?

IMHO, all that's needed is the ...

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

Quoted 1 line
mm/gup_benchmark.c +1 -0
+			kvfree(pages);

.. here

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

Quoted 3 lines
mm/gup_benchmark.c +0 -0
 			return -1;
 		}
 

Not sure how often CONFIG_GUP_BENCHMARK is really enabled. Maybe we should

Cc [email protected] # v4.20+

With only the kvfree(pages)

Signature · 3 lines
Thanks,

David / dhildenb