From: Koba Ko io_import_umem() has two problems: 1. When io_account_mem() fails, the function returns an error but leaves live pinned pages and sg_table in the mem struct without cleaning them up. The caller happens to handle this today via io_zcrx_free_area() -> io_release_area_mem(), but the contract is fragile. 2. io_release_area_mem() doesn't NULL out mem->pages after kvfree(), making it unsafe to call twice. Since io_zcrx_free_area() always calls it during teardown, any earlier cleanup call would cause a double-free. Fix both: populate mem fields before io_account_mem() so io_release_area_mem() can do a proper cleanup on failure, and add mem->pages = NULL in io_release_area_mem() to make it idempotent. Fixes: 262ab205180d2 ("io_uring/zcrx: account area memory") Signed-off-by: Koba Ko --- io_uring/zcrx.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/io_uring/zcrx.c b/io_uring/zcrx.c index 62d693287457f..c9ed1139c7bcd 100644 --- a/io_uring/zcrx.c +++ b/io_uring/zcrx.c @@ -188,6 +188,8 @@ static unsigned long io_count_account_pages(struct page **pages, unsigned nr_pag return res; } +static void io_release_area_mem(struct io_zcrx_mem *mem); + static int io_import_umem(struct io_zcrx_ifq *ifq, struct io_zcrx_mem *mem, struct io_uring_zcrx_area_reg *area_reg) @@ -213,16 +215,20 @@ static int io_import_umem(struct io_zcrx_ifq *ifq, return ret; } - mem->account_pages = io_count_account_pages(pages, nr_pages); - ret = io_account_mem(ifq->user, ifq->mm_account, mem->account_pages); - if (ret < 0) - mem->account_pages = 0; - mem->sgt = &mem->page_sg_table; mem->pages = pages; mem->nr_folios = nr_pages; mem->size = area_reg->len; - return ret; + + mem->account_pages = io_count_account_pages(pages, nr_pages); + ret = io_account_mem(ifq->user, ifq->mm_account, mem->account_pages); + if (ret < 0) { + mem->account_pages = 0; + io_release_area_mem(mem); + return ret; + } + + return 0; } static void io_release_area_mem(struct io_zcrx_mem *mem) @@ -236,6 +242,7 @@ static void io_release_area_mem(struct io_zcrx_mem *mem) sg_free_table(mem->sgt); mem->sgt = NULL; kvfree(mem->pages); + mem->pages = NULL; } } -- 2.43.0