Skip to contentSkip to conversation

iov_iter: Fix iov_iter_get_pages{,_alloc} page fault return value

PATCH New Andreas Gruenbacher 1 participant 1 message Jul 21, 2021

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

PATCH 1/1 iov_iter: Fix iov_iter_get_pages{,_alloc} page fault return value

Both iov_iter_get_pages and iov_iter_get_pages_alloc return the number of bytes of the iovec they could get the pages for. When they cannot get any pages, they're supposed to return 0, but when the start of the iovec isn't page aligned, the calculation goes wrong and they return a negative value. Fix that in both functions.

In addition, change iov_iter_get_pages_alloc to return ZERO_SIZE_PTR in that case to prevent resource leaks.

It seems that the cifs and nfs filesystems don't handle the zero case very well. Steve, Trond, Anna, could you please have a look?

---
 lib/iov_iter.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
lib/iov_iter.c +3 -2
index e23123ae3a13..20dc3d800573 100644
@@ -1484,7 +1484,7 @@ ssize_t iov_iter_get_pages(struct iov_iter *i,
 		res = get_user_pages_fast(addr, n,
 				iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0,
 				pages);
-		if (unlikely(res < 0))
+		if (unlikely(res <= 0))
 			return res;
 		return (res == n ? len : res * PAGE_SIZE) - *start;
 	}
@@ -1608,8 +1608,9 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
 			return -ENOMEM;
 		res = get_user_pages_fast(addr, n,
 				iov_iter_rw(i) != WRITE ?  FOLL_WRITE : 0, p);
-		if (unlikely(res < 0)) {
+		if (unlikely(res <= 0)) {
 			kvfree(p);
+			*pages = ZERO_SIZE_PTR;
 			return res;
 		}
 		*pages = p;
Signature · 1 line
2.26.3