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

[ Upstream commit 814a66741b ]

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 both functions.

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

Signed-off-by: Andreas Gruenbacher <agruenba@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Andreas Gruenbacher 2021-07-21 19:03:47 +02:00 committed by Greg Kroah-Hartman
parent d78de051ff
commit c45c83c171
1 changed files with 3 additions and 2 deletions

View File

@ -1488,7 +1488,7 @@ ssize_t iov_iter_get_pages(struct iov_iter *i,
res = get_user_pages_fast(addr, n, res = get_user_pages_fast(addr, n,
iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0,
pages); pages);
if (unlikely(res < 0)) if (unlikely(res <= 0))
return res; return res;
return (res == n ? len : res * PAGE_SIZE) - *start; return (res == n ? len : res * PAGE_SIZE) - *start;
} }
@ -1612,8 +1612,9 @@ ssize_t iov_iter_get_pages_alloc(struct iov_iter *i,
return -ENOMEM; return -ENOMEM;
res = get_user_pages_fast(addr, n, res = get_user_pages_fast(addr, n,
iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p); iov_iter_rw(i) != WRITE ? FOLL_WRITE : 0, p);
if (unlikely(res < 0)) { if (unlikely(res <= 0)) {
kvfree(p); kvfree(p);
*pages = NULL;
return res; return res;
} }
*pages = p; *pages = p;