Skip to contentSkip to conversation

account zcrx area pinned memory

PATCH Applied Pavel Begunkov 2 participants 5 messages Jul 16, 2025

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Pavel Begunkov

author maintainer
#0 lore raw

cover account zcrx area pinned memory

Honour RLIMIT_MEMLOCK while pinning zcrx areas.

Pavel Begunkov (2):
io_uring: export io_[un]account_mem
io_uring/zcrx: account area memory

 io_uring/rsrc.c |  4 ++--
 io_uring/rsrc.h |  2 ++
 io_uring/zcrx.c | 27 +++++++++++++++++++++++++++
 io_uring/zcrx.h |  1 +
 4 files changed, 32 insertions(+), 2 deletions(-)
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 1/2 io_uring: export io_[un]account_mem

Export pinned memory accounting helpers, they'll be used by zcrx shortly.

Fixes cf96310c5f9a0 ("io_uring/zcrx: add io_zcrx_area")
---
 io_uring/rsrc.c | 4 ++--
 io_uring/rsrc.h | 2 ++
 2 files changed, 4 insertions(+), 2 deletions(-)
io_uring/rsrc.c +2 -2
index 043018bc9b9c..f75f5e43fa4a 100644
@@ -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
index 25e7e998dcfd..a3ca6ba66596 100644
@@ -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
author maintainer
#2 lore raw

PATCH 2/2 io_uring/zcrx: account area memory

In reply to Pavel Begunkov

zcrx 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.

Fixes cf96310c5f9a0 ("io_uring/zcrx: add io_zcrx_area")
---
 io_uring/zcrx.c | 27 +++++++++++++++++++++++++++
 io_uring/zcrx.h |  1 +
 2 files changed, 28 insertions(+)
io_uring/zcrx.c +27 -0
index 7d7396ce876c..dabce3ee0e8b 100644
@@ -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,6 +197,13 @@ 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 < 0) {
+		mem->account_pages = 0;
+		return ret;
+	}
+
 	mem->pages = pages;
 	mem->nr_folios = nr_pages;
 	mem->size = area_reg->len;
@@ -357,6 +381,9 @@ static void io_zcrx_free_area(struct io_zcrx_area *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);
io_uring/zcrx.h +1 -0
index 89015b923911..109c4ca36434 100644
@@ -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
author maintainer
#3 lore raw
In reply to Pavel Begunkov
Quoted 1 line · Pavel Begunkov

Honour RLIMIT_MEMLOCK while pinning zcrx areas.

Base on for-next

Quoted 10 lines

Pavel Begunkov (2):
io_uring: export io_[un]account_mem
io_uring/zcrx: account area memory

  io_uring/rsrc.c |  4 ++--
  io_uring/rsrc.h |  2 ++
  io_uring/zcrx.c | 27 +++++++++++++++++++++++++++
  io_uring/zcrx.h |  1 +
  4 files changed, 32 insertions(+), 2 deletions(-)
Signature · 1 line
Pavel Begunkov
maintainer Applied
#4 lore raw
In reply to Pavel Begunkov
Quoted 6 lines · Pavel Begunkov

Honour RLIMIT_MEMLOCK while pinning zcrx areas.

Pavel Begunkov (2):
io_uring: export io_[un]account_mem
io_uring/zcrx: account area memory

Quoted 7 lines

io_uring/rsrc.c | 4 ++--

 io_uring/rsrc.h |  2 ++
 io_uring/zcrx.c | 27 +++++++++++++++++++++++++++
 io_uring/zcrx.h |  1 +
 4 files changed, 32 insertions(+), 2 deletions(-)

[...]

Applied, thanks!

[1/2] io_uring: export io_[un]account_mem

      commit: 11fbada7184f9e19bcdfa2f6b15828a78b8897a6
[2/2] io_uring/zcrx: account area memory
      commit: 262ab205180d2ba3ab6110899a4dbe439c51dfaa

Best regards,

Signature · 1 line
Jens Axboe