account zcrx area pinned memory
Subsystems (2)
Pavel Begunkov
maintainerv2 cover account zcrx area pinned memory
Honour RLIMIT_MEMLOCK while pinning zcrx areas.
v2: Fix null ifq on partial destruction
Fix page leaks on io_zcrx_free_area() failure.
Pavel Begunkov (3):
io_uring: export io_[un]account_mem io_uring/zcrx: account area memory io_uring/zcrx: fix leaking pages on sg init fail
io_uring/rsrc.c | 4 ++-- io_uring/rsrc.h | 2 ++ io_uring/zcrx.c | 36 +++++++++++++++++++++++++++++++----- io_uring/zcrx.h | 1 + 4 files changed, 36 insertions(+), 7 deletions(-)
Signature · 1 line
2.49.0
Pavel Begunkov
maintainerPATCH v2 1/3 io_uring: export io_[un]account_mem
Export pinned memory accounting helpers, they'll be used by zcrx shortly.
--- io_uring/rsrc.c | 4 ++-- io_uring/rsrc.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-)
io_uring/rsrc.c +2 -2
@@ -55,7 +55,7 @@ int __io_account_mem(struct user_struct *user, unsigned long nr_pages)return 0 ; } -static void io_unaccount_mem (struct io_ring_ctx *ctx,unsigned long nr_pages) +void io_unaccount_mem (struct io_ring_ctx *ctx,unsigned long nr_pages) {if (ctx->user)__io_unaccount_mem (ctx->user, nr_pages);
@@ -64,7 +64,7 @@ static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)atomic64_sub (nr_pages, &ctx->mm_account->pinned_vm); } -static int io_account_mem (struct io_ring_ctx *ctx,unsigned long nr_pages) +int io_account_mem (struct io_ring_ctx *ctx,unsigned long nr_pages) {int ret;
io_uring/rsrc.h +2 -0
@@ -120,6 +120,8 @@ int io_files_update(struct io_kiocb *req, unsigned int issue_flags);int io_files_update_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe);int __io_account_mem (struct user_struct *user,unsigned long nr_pages); +int io_account_mem (struct io_ring_ctx *ctx,unsigned long nr_pages); +void io_unaccount_mem (struct io_ring_ctx *ctx,unsigned long nr_pages);static inline void __io_unaccount_mem (struct user_struct *user,unsigned long nr_pages)
Signature · 1 line
2.49.0
Pavel Begunkov
maintainerPATCH v2 2/3 io_uring/zcrx: account area memory
In reply to Pavel Begunkovzcrx areas can be quite large and need to be accounted and checked against RLIMIT_MEMLOCK. In practise it shouldn't be a big issue as the inteface already requires cap_net_admin.
--- io_uring/zcrx.c | 32 ++++++++++++++++++++++++++++---- io_uring/zcrx.h | 1 + 2 files changed, 29 insertions(+), 4 deletions(-)
io_uring/zcrx.c +28 -4
@@ -158,6 +158,23 @@ static int io_zcrx_map_area_dmabuf(struct io_zcrx_ifq *ifq, struct io_zcrx_area area->mem.dmabuf_offset); } +static unsigned long io_count_account_pages (struct page **pages,unsigned nr_pages) +{ +struct folio *last_folio =NULL ; +unsigned long res =0 ; +int i; + +for (i =0 ; i < nr_pages; i++) { +struct folio *folio =page_folio (pages[i]); + +if (folio == last_folio) +continue ; + last_folio = folio; + res +=1UL <<folio_order (folio); + } +return res; +} +static int io_import_umem (struct io_zcrx_ifq *ifq,struct io_zcrx_mem *mem,struct io_uring_zcrx_area_reg *area_reg)
@@ -180,10 +197,15 @@ static int io_import_umem(struct io_zcrx_ifq *ifq,if (ret)return ret; + mem->account_pages =io_count_account_pages (pages, nr_pages); + ret =io_account_mem (ifq->ctx, mem->account_pages); +if (ret) + mem->account_pages =0 ; + mem->pages = pages; mem->nr_folios = nr_pages; mem->size = area_reg->len; -return 0 ; +return ret; }static void io_release_area_mem (struct io_zcrx_mem *mem)
@@ -353,10 +375,12 @@ static void io_free_rbuf_ring(struct io_zcrx_ifq *ifq)static void io_zcrx_free_area (struct io_zcrx_area *area) { -if (area->ifq) -io_zcrx_unmap_area (area->ifq, area); +io_zcrx_unmap_area (area->ifq, area);io_release_area_mem (&area->mem); +if (area->mem.account_pages) +io_unaccount_mem (area->ifq->ctx, area->mem.account_pages); +kvfree (area->freelist);kvfree (area->nia.niovs);kvfree (area->user_refs);
@@ -384,6 +408,7 @@ static int io_zcrx_create_area(struct io_zcrx_ifq *ifq, area =kzalloc (sizeof (*area),GFP_KERNEL );if (!area) goto err; + area->ifq = ifq; ret =io_import_area (ifq, &area->mem, area_reg);if (ret)
@@ -418,7 +443,6 @@ static int io_zcrx_create_area(struct io_zcrx_ifq *ifq, } area->free_count = nr_iovs; - area->ifq = ifq;/* we're only supporting one area per ifq for now */ area->area_id =0 ; area_reg->rq_area_token = (u64 )area->area_id <<IORING_ZCRX_AREA_SHIFT ;
io_uring/zcrx.h +1 -0
@@ -15,6 +15,7 @@ struct io_zcrx_mem {struct page **pages;unsigned long nr_folios;struct sg_table page_sg_table; +unsigned long account_pages;struct dma_buf_attachment *attach;struct dma_buf *dmabuf;
Signature · 1 line
2.49.0
Pavel Begunkov
maintainerPATCH v2 3/3 io_uring/zcrx: fix leaking pages on sg init fail
In reply to Pavel BegunkovIf sg_alloc_table_from_pages() fails, io_import_umem() returns without cleaning up pinned pages first. Fix it.
--- io_uring/zcrx.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
io_uring/zcrx.c +3 -1
@@ -194,8 +194,10 @@ static int io_import_umem(struct io_zcrx_ifq *ifq, ret =sg_alloc_table_from_pages (&mem->page_sg_table, pages, nr_pages,0 , nr_pages <<PAGE_SHIFT ,GFP_KERNEL_ACCOUNT ); -if (ret) +if (ret) { +unpin_user_pages (pages, nr_pages);return ret; + } mem->account_pages =io_count_account_pages (pages, nr_pages); ret =io_account_mem (ifq->ctx, mem->account_pages);
Signature · 1 line
2.49.0
Pavel Begunkov
maintainerQuoted 1 line · Pavel Begunkov
Honour
RLIMIT_MEMLOCKwhile pinning zcrx areas.
It appeared cleaner resending the whole thing. Let me know if a fixup patch is preferable.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerQuoted 5 lines · Pavel Begunkov
Quoted 1 line · Pavel Begunkov
Honour
RLIMIT_MEMLOCKwhile pinning zcrx areas.It appeared cleaner resending the whole thing. Let me know if a fixup patch is preferable.
Fixup would be better in this case, as there's already other patches on top at this point.
Signature · 1 line
Jens Axboe