Skip to content

Commit 820d070

Browse files
committed
io_uring: don't allow discontig pages for IORING_SETUP_NO_MMAP
io_sqes_map() is used rather than io_mem_alloc(), if the application passes in memory for mapping rather than have the kernel allocate it and then mmap(2) the ranges. This then calls __io_uaddr_map() to perform the page mapping and pinning, which checks if we end up with the same pages, if more than one page is mapped. But this check is incorrect and only checks if the first and last pages are the same, where it really should be checking if the mapped pages are contigous. This allows mapping a single normal page, or a huge page range. Down the line we can add support for remapping pages to be virtually contigous, which is really all that io_uring cares about. Cc: [email protected] Fixes: 03d89a2 ("io_uring: support for user allocated memory for rings/sqes") Reported-by: Jann Horn <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent d6fef34 commit 820d070

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

io_uring/io_uring.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,6 +2697,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
26972697
{
26982698
struct page **page_array;
26992699
unsigned int nr_pages;
2700+
void *page_addr;
27002701
int ret, i;
27012702

27022703
*npages = 0;
@@ -2718,27 +2719,29 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
27182719
io_pages_free(&page_array, ret > 0 ? ret : 0);
27192720
return ret < 0 ? ERR_PTR(ret) : ERR_PTR(-EFAULT);
27202721
}
2721-
/*
2722-
* Should be a single page. If the ring is small enough that we can
2723-
* use a normal page, that is fine. If we need multiple pages, then
2724-
* userspace should use a huge page. That's the only way to guarantee
2725-
* that we get contigious memory, outside of just being lucky or
2726-
* (currently) having low memory fragmentation.
2727-
*/
2728-
if (page_array[0] != page_array[ret - 1])
2729-
goto err;
27302722

2731-
/*
2732-
* Can't support mapping user allocated ring memory on 32-bit archs
2733-
* where it could potentially reside in highmem. Just fail those with
2734-
* -EINVAL, just like we did on kernels that didn't support this
2735-
* feature.
2736-
*/
2723+
page_addr = page_address(page_array[0]);
27372724
for (i = 0; i < nr_pages; i++) {
2738-
if (PageHighMem(page_array[i])) {
2739-
ret = -EINVAL;
2725+
ret = -EINVAL;
2726+
2727+
/*
2728+
* Can't support mapping user allocated ring memory on 32-bit
2729+
* archs where it could potentially reside in highmem. Just
2730+
* fail those with -EINVAL, just like we did on kernels that
2731+
* didn't support this feature.
2732+
*/
2733+
if (PageHighMem(page_array[i]))
27402734
goto err;
2741-
}
2735+
2736+
/*
2737+
* No support for discontig pages for now, should either be a
2738+
* single normal page, or a huge page. Later on we can add
2739+
* support for remapping discontig pages, for now we will
2740+
* just fail them with EINVAL.
2741+
*/
2742+
if (page_address(page_array[i]) != page_addr)
2743+
goto err;
2744+
page_addr += PAGE_SIZE;
27422745
}
27432746

27442747
*pages = page_array;

0 commit comments

Comments
 (0)