Re: [PATCH] io_uring: do not charge the SQ/CQ rings to RLIMIT_MEMLOCK
flat view
From: Jens Axboe <axboe@kernel.dk>
Date: 2026-10-06 14:59:34
Also in:
io-uring, lkml, stable
Subsystem:
io_uring, the rest · Maintainers:
Jens Axboe, Linus Torvalds
On 10/6/26 6:57 AM, hengyul@cs.unc.edu wrote:
From: Hengyu Liang <redacted>
Commit 8078486e1d53 ("io_uring: use region api for SQ") and commit
81a4058e0cd0 ("io_uring: use region api for CQ") made io_uring_setup()
allocate the rings with io_create_region().
However, io_create_region() charges the memory to RLIMIT_MEMLOCK, and
the rings had been exempt from that limit since commit 26bfa89e25f4
("io_uring: place ring SQ/CQ arrays under memcg memory limits"). As of
now, a user without CAP_IPC_LOCK gets ENOMEM from io_uring_setup() when
their rings exceed the limit, which is 8 MiB by default. PostgreSQL
developers have already hit this in their io_uring tests [1].
The issue can be reproduced with a simple liburing program, run as an
unprivileged user:
#include <liburing.h>
#include <stdio.h>
int main(void)
{
static struct io_uring ring[64];
int i;
for (i = 0; i < 64; i++)
if (io_uring_queue_init(4096, &ring[i], 0) < 0)
break;
printf("%d rings\n", i);
return 0;
}
Before those commits (v6.13), it prints "64 rings". After those commits
(v6.14), it prints "21 rings".
This patch makes io_create_region() take the user to charge, and passes
no user for the SQ/CQ rings.Agree that this is a bug, stricter accounting may break use cases. However, I think we can solve this simpler, and actually kill more code. How about something like the below instead? Only apply accounting to user backed memory, which is how it used to work too. Would be great if you could take a look and also run your test case against it.
diff --git a/io_uring/memmap.c b/io_uring/memmap.c
index 48c0eb012412..da2328b52b38 100644
--- a/io_uring/memmap.c
+++ b/io_uring/memmap.c@@ -16,10 +16,8 @@ #include "zcrx.h" static bool io_mem_alloc_compound(struct page **pages, int nr_pages, - size_t size, gfp_t gfp, - struct user_struct *user) + size_t size, gfp_t gfp) { - unsigned long nr_compound, extra; struct page *page; int i, order;
@@ -29,22 +27,9 @@ static bool io_mem_alloc_compound(struct page **pages, int nr_pages, else if (order) gfp |= __GFP_COMP; - /* - * get_order() rounds a non power of two size up, so the allocation - * can hold more pages than the region exposes. Account those too, - * and leave the compound allocation alone if they do not fit. - */ - nr_compound = 1UL << order; - extra = nr_compound - nr_pages; - if (extra && user && __io_account_mem(user, extra)) - return false; - page = alloc_pages(gfp, order); - if (!page) { - if (extra && user) - __io_unaccount_mem(user, extra); + if (!page) return false; - } for (i = 0; i < nr_pages; i++) pages[i] = page + i;
@@ -120,15 +105,8 @@ void io_free_region(struct user_struct *user, struct io_mapped_region *mr) } if ((mr->flags & IO_REGION_F_VMAP) && mr->ptr) vunmap(mr->ptr); - if (mr->nr_pages && user) { - unsigned long nr_accounted = mr->nr_pages; - - /* a compound region was accounted for the whole allocation */ - if (mr->flags & IO_REGION_F_SINGLE_REF) - nr_accounted = 1UL << get_order(io_region_size(mr)); - - __io_unaccount_mem(user, nr_accounted); - } + if ((mr->flags & IO_REGION_F_USER_PROVIDED) && user) + __io_unaccount_mem(user, mr->nr_pages); memset(mr, 0, sizeof(*mr)); }
@@ -154,11 +132,12 @@ static int io_region_init_ptr(struct io_mapped_region *mr) } static int io_region_pin_pages(struct io_mapped_region *mr, - struct io_uring_region_desc *reg) + struct io_uring_region_desc *reg, + struct user_struct *user) { size_t size = io_region_size(mr); struct page **pages; - int nr_pages; + int nr_pages, ret; pages = io_pin_pages(reg->user_addr, size, &nr_pages); if (IS_ERR(pages))
@@ -166,6 +145,16 @@ static int io_region_pin_pages(struct io_mapped_region *mr, if (WARN_ON_ONCE(nr_pages != mr->nr_pages)) return -EFAULT; + /* pinned user memory is what RLIMIT_MEMLOCK is for */ + if (user) { + ret = __io_account_mem(user, nr_pages); + if (ret) { + unpin_user_pages(pages, nr_pages); + kvfree(pages); + return ret; + } + } + mr->pages = pages; mr->flags |= IO_REGION_F_USER_PROVIDED; return 0;
@@ -173,8 +162,7 @@ static int io_region_pin_pages(struct io_mapped_region *mr, static int io_region_allocate_pages(struct io_mapped_region *mr, struct io_uring_region_desc *reg, - unsigned long mmap_offset, - struct user_struct *user) + unsigned long mmap_offset) { gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN; size_t size = io_region_size(mr);
@@ -185,7 +173,7 @@ static int io_region_allocate_pages(struct io_mapped_region *mr, if (!pages) return -ENOMEM; - if (io_mem_alloc_compound(pages, mr->nr_pages, size, gfp, user)) { + if (io_mem_alloc_compound(pages, mr->nr_pages, size, gfp)) { mr->flags |= IO_REGION_F_SINGLE_REF; goto done; }
@@ -230,17 +218,16 @@ int io_create_region(struct io_ring_ctx *ctx, struct io_mapped_region *mr, return -EOVERFLOW; nr_pages = reg->size >> PAGE_SHIFT; - if (ctx->user) { - ret = __io_account_mem(ctx->user, nr_pages); - if (ret) - return ret; - } mr->nr_pages = nr_pages; + /* + * Only pinned user memory counts against RLIMIT_MEMLOCK, kernel + * allocated regions are memcg accounted through GFP_KERNEL_ACCOUNT. + */ if (reg->flags & IORING_MEM_REGION_TYPE_USER) - ret = io_region_pin_pages(mr, reg); + ret = io_region_pin_pages(mr, reg, ctx->user); else - ret = io_region_allocate_pages(mr, reg, mmap_offset, ctx->user); + ret = io_region_allocate_pages(mr, reg, mmap_offset); if (ret) goto out_free;
--
Jens Axboe