From: Jens Axboe <axboe@kernel.dk>
To: hengyul@cs.unc.edu, Pavel Begunkov <asml.silence@gmail.com>,
io-uring@vger.kernel.org
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] io_uring: do not charge the SQ/CQ rings to RLIMIT_MEMLOCK
Date: Tue, 6 Oct 2026 08:59:22 -0600 [thread overview]
Message-ID: <01d75013-9eae-4ec6-93b5-cc1b2b83b791@kernel.dk> (raw)
In-Reply-To: <20261006125732.3425762-1-hengyul@cs.unc.edu>
On 10/6/26 6:57 AM, hengyul@cs.unc.edu wrote:
> From: Hengyu Liang <hengyul@cs.unc.edu>
>
> 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
prev parent reply other threads:[~2026-10-06 14:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-06 12:57 [PATCH] io_uring: do not charge the SQ/CQ rings to RLIMIT_MEMLOCK hengyul
2026-10-06 14:59 ` Jens Axboe [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=01d75013-9eae-4ec6-93b5-cc1b2b83b791@kernel.dk \
--to=axboe@kernel.dk \
--cc=asml.silence@gmail.com \
--cc=hengyul@cs.unc.edu \
--cc=io-uring@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox