Skip to content

Commit 922a2c7

Browse files
committed
io_uring/rsrc: cleanup io_pin_pages()
This function is overly convoluted with a goto error path, and checks under the mmap_read_lock() that don't need to be at all. Rearrange it a bit so the checks and errors fall out naturally, rather than needing to jump around for it. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 93b8cc6 commit 922a2c7

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

io_uring/rsrc.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,39 +1037,36 @@ struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
10371037
{
10381038
unsigned long start, end, nr_pages;
10391039
struct page **pages = NULL;
1040-
int pret, ret = -ENOMEM;
1040+
int ret;
10411041

10421042
end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
10431043
start = ubuf >> PAGE_SHIFT;
10441044
nr_pages = end - start;
1045+
WARN_ON(!nr_pages);
10451046

10461047
pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
10471048
if (!pages)
1048-
goto done;
1049+
return ERR_PTR(-ENOMEM);
10491050

1050-
ret = 0;
10511051
mmap_read_lock(current->mm);
1052-
pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1053-
pages);
1054-
if (pret == nr_pages)
1052+
ret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM, pages);
1053+
mmap_read_unlock(current->mm);
1054+
1055+
/* success, mapped all pages */
1056+
if (ret == nr_pages) {
10551057
*npages = nr_pages;
1056-
else
1057-
ret = pret < 0 ? pret : -EFAULT;
1058+
return pages;
1059+
}
10581060

1059-
mmap_read_unlock(current->mm);
1060-
if (ret) {
1061+
/* partial map, or didn't map anything */
1062+
if (ret >= 0) {
10611063
/* if we did partial map, release any pages we did get */
1062-
if (pret > 0)
1063-
unpin_user_pages(pages, pret);
1064-
goto done;
1065-
}
1066-
ret = 0;
1067-
done:
1068-
if (ret < 0) {
1069-
kvfree(pages);
1070-
pages = ERR_PTR(ret);
1064+
if (ret)
1065+
unpin_user_pages(pages, ret);
1066+
ret = -EFAULT;
10711067
}
1072-
return pages;
1068+
kvfree(pages);
1069+
return ERR_PTR(ret);
10731070
}
10741071

10751072
static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,

0 commit comments

Comments
 (0)