* [PATCH] io_uring: Fix release of pinned pages when __io_uaddr_map fails
@ 2024-03-13 21:39 Gabriel Krisman Bertazi
2024-03-13 21:45 ` Jens Axboe
2024-03-13 21:56 ` Jens Axboe
0 siblings, 2 replies; 3+ messages in thread
From: Gabriel Krisman Bertazi @ 2024-03-13 21:39 UTC (permalink / raw)
To: axboe; +Cc: io-uring, Gabriel Krisman Bertazi
Looking at the error path of __io_uaddr_map, if we fail after pinning
the pages for any reasons, ret will be set to -EINVAL and the error
handler won't properly release the pinned pages.
I didn't manage to trigger it without forcing a failure, but it can
happen in real life when memory is heavily fragmented.
Signed-off-by: Gabriel Krisman Bertazi <[email protected]>
---
io_uring/io_uring.c | 22 +++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)
diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c
index 85df615f1196..f26f9ff86300 100644
--- a/io_uring/io_uring.c
+++ b/io_uring/io_uring.c
@@ -2719,7 +2719,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
struct page **page_array;
unsigned int nr_pages;
void *page_addr;
- int ret, i;
+ int ret, i, pinned;
*npages = 0;
@@ -2733,12 +2733,12 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
if (!page_array)
return ERR_PTR(-ENOMEM);
- ret = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
- page_array);
- if (ret != nr_pages) {
-err:
- io_pages_free(&page_array, ret > 0 ? ret : 0);
- return ret < 0 ? ERR_PTR(ret) : ERR_PTR(-EFAULT);
+
+ pinned = pin_user_pages_fast(uaddr, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
+ page_array);
+ if (pinned != nr_pages) {
+ ret = (pinned < 0) ? pinned : -EFAULT;
+ goto free_pages;
}
page_addr = page_address(page_array[0]);
@@ -2752,7 +2752,7 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
* didn't support this feature.
*/
if (PageHighMem(page_array[i]))
- goto err;
+ goto free_pages;
/*
* No support for discontig pages for now, should either be a
@@ -2761,13 +2761,17 @@ static void *__io_uaddr_map(struct page ***pages, unsigned short *npages,
* just fail them with EINVAL.
*/
if (page_address(page_array[i]) != page_addr)
- goto err;
+ goto free_pages;
page_addr += PAGE_SIZE;
}
*pages = page_array;
*npages = nr_pages;
return page_to_virt(page_array[0]);
+
+free_pages:
+ io_pages_free(&page_array, pinned > 0 ? pinned : 0);
+ return ERR_PTR(ret);
}
static void *io_rings_map(struct io_ring_ctx *ctx, unsigned long uaddr,
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] io_uring: Fix release of pinned pages when __io_uaddr_map fails
2024-03-13 21:39 [PATCH] io_uring: Fix release of pinned pages when __io_uaddr_map fails Gabriel Krisman Bertazi
@ 2024-03-13 21:45 ` Jens Axboe
2024-03-13 21:56 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2024-03-13 21:45 UTC (permalink / raw)
To: Gabriel Krisman Bertazi; +Cc: io-uring
On 3/13/24 3:39 PM, Gabriel Krisman Bertazi wrote:
> Looking at the error path of __io_uaddr_map, if we fail after pinning
> the pages for any reasons, ret will be set to -EINVAL and the error
> handler won't properly release the pinned pages.
>
> I didn't manage to trigger it without forcing a failure, but it can
> happen in real life when memory is heavily fragmented.
Good catch, fix looks good to me. I'll add a:
Fixes: 223ef4743164 ("io_uring: don't allow IORING_SETUP_NO_MMAP rings on highmem pages")
to it, however.
--
Jens Axboe
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] io_uring: Fix release of pinned pages when __io_uaddr_map fails
2024-03-13 21:39 [PATCH] io_uring: Fix release of pinned pages when __io_uaddr_map fails Gabriel Krisman Bertazi
2024-03-13 21:45 ` Jens Axboe
@ 2024-03-13 21:56 ` Jens Axboe
1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2024-03-13 21:56 UTC (permalink / raw)
To: Gabriel Krisman Bertazi; +Cc: io-uring
On Wed, 13 Mar 2024 17:39:12 -0400, Gabriel Krisman Bertazi wrote:
> Looking at the error path of __io_uaddr_map, if we fail after pinning
> the pages for any reasons, ret will be set to -EINVAL and the error
> handler won't properly release the pinned pages.
>
> I didn't manage to trigger it without forcing a failure, but it can
> happen in real life when memory is heavily fragmented.
>
> [...]
Applied, thanks!
[1/1] io_uring: Fix release of pinned pages when __io_uaddr_map fails
(no commit info)
Best regards,
--
Jens Axboe
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-03-13 21:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-13 21:39 [PATCH] io_uring: Fix release of pinned pages when __io_uaddr_map fails Gabriel Krisman Bertazi
2024-03-13 21:45 ` Jens Axboe
2024-03-13 21:56 ` Jens Axboe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox