Skip to contentSkip to conversation

mm/gup.c: Handling ERR within unpin_user_pages()

PATCH In review Souptick Joarder 5 participants 7 messages Sep 13, 2020 – Sep 14, 2020

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

PATCH 1/1 mm/gup.c: Handling ERR within unpin_user_pages()

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

This can be fixed by adding extra check inside unpin_user_pages().

---
 mm/gup.c | 3 +++
 1 file changed, 3 insertions(+)
mm/gup.c +3 -0
index 0b5c308b..2e19bd6 100644
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 {
 	unsigned long index;
 
+	if (WARN_ON_ONCE(npages < 0))
+		return;
+
 	/*
 	 * TODO: this can be optimized for huge pages: if a series of pages is
 	 * physically contiguous and part of the same compound page, then a
Signature · 1 line
1.9.1
#1 lore raw

mm/gup.c void unpin_user_pages(struct page **pages, unsigned long npages) · line 332

Quoted 9 lines · Souptick Joarder

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

mm/gup.c +2 -0
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 {
 	unsigned long index;
 
+	if (WARN_ON_ONCE(npages < 0))
+		return;

But npages is unsigned long. So it can't be less than zero.

#2 lore raw
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · Souptick Joarder

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

mm/gup.c +0 -0
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 {
      unsigned long index;
 
Quoted 4 lines

mm/gup.c void unpin_user_pages(struct page **pages, unsigned long npages) · line 332

Quoted 2 lines
mm/gup.c +2 -0
+     if (WARN_ON_ONCE(npages < 0))
+             return;

But npages is unsigned long. So it can't be less than zero.

Sorry, I missed it.

Then, it means if npages is assigned with -ERRNO by caller, unpin_user_pages() may end up calling a big loop, which is unnecessary.

#3 lore raw
Quoted 15 lines · Souptick Joarder
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · Souptick Joarder

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

mm/gup.c +0 -0
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 {
      unsigned long index;
 
Quoted 4 lines

mm/gup.c void unpin_user_pages(struct page **pages, unsigned long npages) · line 332

Quoted 2 lines
mm/gup.c +2 -0
+     if (WARN_ON_ONCE(npages < 0))
+             return;

But npages is unsigned long. So it can't be less than zero.

Quoted 4 lines

Sorry, I missed it.

Then, it means if npages is assigned with -ERRNO by caller, unpin_user_pages() may end up calling a big loop, which is unnecessary.

How will a caller allocate memory of the right size and still manage to call with the wrong npages? Do you have an example of a broken caller?

Jason

#4 lore raw
Quoted 17 lines · Jason Gunthorpe
Quoted 15 lines · Souptick Joarder
Quoted 13 lines · Matthew Wilcox

mm/gup.c void unpin_user_pages(struct page **pages, unsigned long npages) · line 332

Quoted 9 lines · Souptick Joarder

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

mm/gup.c +2 -0
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 {
      unsigned long index;
 
+     if (WARN_ON_ONCE(npages < 0))
+             return;

But npages is unsigned long. So it can't be less than zero.

Quoted 7 lines
Quoted 4 lines

Sorry, I missed it.

Then, it means if npages is assigned with -ERRNO by caller, unpin_user_pages() may end up calling a big loop, which is unnecessary.

How will a caller allocate memory of the right size and still manage to call with the wrong npages? Do you have an example of a broken caller?

These are two broken callers which might end up calling unpin_user_pages() with -ERRNO.
drivers/rapidio/devices/rio_mport_cdev.c#L952 drivers/misc/mic/scif/scif_rma.c#L1399

They both are in error handling paths, so might not have any serious impact. But theoretically possible.

reviewer
#5 lore raw
Quoted 26 lines · Souptick Joarder
Quoted 17 lines · Jason Gunthorpe
Quoted 15 lines · Souptick Joarder
Quoted 13 lines · Matthew Wilcox

mm/gup.c void unpin_user_pages(struct page **pages, unsigned long npages) · line 332

Quoted 9 lines · Souptick Joarder

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

mm/gup.c +2 -0
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
  {
       unsigned long index;
 
+     if (WARN_ON_ONCE(npages < 0))
+             return;

But npages is unsigned long. So it can't be less than zero.

Quoted 7 lines
Quoted 4 lines

Sorry, I missed it.

Then, it means if npages is assigned with -ERRNO by caller, unpin_user_pages() may end up calling a big loop, which is unnecessary.

How will a caller allocate memory of the right size and still manage to call with the wrong npages? Do you have an example of a broken caller?

Quoted 8 lines

These are two broken callers which might end up calling unpin_user_pages() with -ERRNO.
drivers/rapidio/devices/rio_mport_cdev.c#L952 drivers/misc/mic/scif/scif_rma.c#L1399

They both are in error handling paths, so might not have any serious impact. But theoretically possible.

Eventually, I settled on fixing up the callers so that they match the gup/pup API better. In other words, gup/pup has signed int for both input and return value, and the callers need to handle that perfectly.

So let's fix up the callers.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#6 lore raw
In reply to Souptick Joarder
Quoted 23 lines · Souptick Joarder
Quoted 22 lines · Jason Gunthorpe
Quoted 19 lines · Souptick Joarder
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · Souptick Joarder

It is possible that a buggy caller of unpin_user_pages() (specially in error handling path) may end up calling it with npages < 0 which is unnecessary.

mm/gup.c +0 -0
@@ -328,6 +328,9 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 {
      unsigned long index;
 
Quoted 4 lines

mm/gup.c void unpin_user_pages(struct page **pages, unsigned long npages) · line 332

Quoted 2 lines
mm/gup.c +2 -0
+     if (WARN_ON_ONCE(npages < 0))
+             return;

But npages is unsigned long. So it can't be less than zero.

Sorry, I missed it.

Then, it means if npages is assigned with -ERRNO by caller, unpin_user_pages() may end up calling a big loop, which is unnecessary.

Quoted 6 lines
Quoted 2 lines

How will a caller allocate memory of the right size and still manage to call with the wrong npages? Do you have an example of a broken caller?

These are two broken callers which might end up calling unpin_user_pages() with -ERRNO.
drivers/rapidio/devices/rio_mport_cdev.c#L952

The error here is that nr_pages should not be set to pinned if pinned is < 0.

Why not fix the logic there? Because it is inherently dangerous to set an unsigned from a signed variable like that.

Quoted 1 line

drivers/misc/mic/scif/scif_rma.c#L1399

Again this is a caller who is not properly checking error conditions.

Quoted 3 lines

They both are in error handling paths, so might not have any serious impact. But theoretically possible.

Actually I think they might have serious problems so they both should be fixed.

In the end this patch just can't work because npages can't be < 0 like Matthew said.

Ira