* [PATCH 0/2] io_uring: charge request async data and iovec arrays to memcg
@ 2026-12-06 10:04 Hang Zhengyang
2026-12-06 10:04 ` [PATCH 1/2] io_uring: charge request async data " Hang Zhengyang
2026-12-06 10:04 ` [PATCH 2/2] io_uring: charge imported iovec arrays " Hang Zhengyang
0 siblings, 2 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-06 10:04 UTC (permalink / raw)
To: Jens Axboe; +Cc: Tao Cui, io-uring, linux-kernel
Requests that wait, such as readv requests on an empty pipe, hold
allocations that aren't charged to memcg, so a cgroup can hold several
times its memory.max in unreclaimable slab. Patch 1 charges the async
data, and patch 2 the iovec arrays that vectored requests import.
The poll entries and the data of a single futex wait stay uncharged.
They are allocated with GFP_ATOMIC and GFP_NOWAIT. With __GFP_ACCOUNT,
the first is charged past memory.max, and the second fails there without
reclaiming, even if the cgroup holds reclaimable page cache.
Tested on for-7.5/io_uring in a KVM guest without KASAN or lockdep, as
an unprivileged task in a cgroup with memory.max at 64MiB. Each run
queues requests that wait: reads and readv on an empty pipe, or recvmsg
on an empty socketpair. The table shows the growth of the cgroup's
slab_unreclaimable out of the growth of the system's SUnreclaim, in MiB:
unpatched both patches
50000 reads 12.6 of 28.8 25.2 of 29.4
2000 readv, 1024 iovecs each 0.5 of 32.4 32.2 of 32.2
2000 recvmsg, 1024 iovecs each 0.0 of 32.7 32.7 of 32.8
40000 readv, 1024 iovecs each 10.1 of 648 OOM-killed in
the cgroup
The full liburing suite shows no new failures, on this kernel and on one
with KASAN and lockdep. The charge was not measured for compat iovecs,
writev, sendmsg and vectored sends.
Tao Cui's io_uring patches [1][2] charge other allocations and don't
touch these.
Found, fixed and tested by Claude Code, I handled the review process.
[1] https://lore.kernel.org/io-uring/20261106131829.3228926-7-cui.tao@linux.dev/
[2] https://lore.kernel.org/io-uring/20261106131829.3228926-8-cui.tao@linux.dev/
Hang Zhengyang (2):
io_uring: charge request async data to memcg
io_uring: charge imported iovec arrays to memcg
io_uring/io_uring.h | 4 ++--
io_uring/net.c | 4 ++++
io_uring/rsrc.h | 12 ++++++++++++
io_uring/rw.c | 4 ++++
4 files changed, 22 insertions(+), 2 deletions(-)
base-commit: 416f1d31a8acea8b45b5da50e78147b83ccf63a8
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] io_uring: charge request async data to memcg
2026-12-06 10:04 [PATCH 0/2] io_uring: charge request async data and iovec arrays to memcg Hang Zhengyang
@ 2026-12-06 10:04 ` Hang Zhengyang
2026-12-06 10:04 ` [PATCH 2/2] io_uring: charge imported iovec arrays " Hang Zhengyang
1 sibling, 0 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-06 10:04 UTC (permalink / raw)
To: Jens Axboe; +Cc: Tao Cui, io-uring, linux-kernel
The io_kiocb of a request is charged to memcg, but its async data is
usually allocated with plain GFP_KERNEL. A request that waits, such as a
read on an empty pipe, holds its async data until it completes. 50000
such reads took 29MiB of unreclaimable slab, of which 13MiB was charged.
Use GFP_KERNEL_ACCOUNT, as commit 6e0d71c288fd ("io_uring/futex: use
GFP_KERNEL_ACCOUNT for futex data allocation") did for futexv. Both
async data allocations could already sleep, so at memory.max the charge
reclaims like any other.
Found, fixed and tested by Claude Code, I handled the review process.
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
io_uring/io_uring.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index ef30ddf4cbf7..d5a9534a4bb0 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -343,7 +343,7 @@ static inline bool
io_uring_alloc_cached_async_data(struct io_alloc_cache *cache,
struct io_kiocb *req)
{
- if (!io_cache_alloc(cache, GFP_KERNEL, &req->async_data))
+ if (!io_cache_alloc(cache, GFP_KERNEL_ACCOUNT, &req->async_data))
return false;
io_req_set_flags(req, REQ_F_ASYNC_DATA);
@@ -355,7 +355,7 @@ static inline void *io_uring_alloc_async_data(struct io_kiocb *req)
const struct io_issue_def *def = &io_issue_defs[req->opcode];
WARN_ON_ONCE(!def->async_size);
- req->async_data = kmalloc(def->async_size, GFP_KERNEL);
+ req->async_data = kmalloc(def->async_size, GFP_KERNEL_ACCOUNT);
if (req->async_data)
io_req_set_flags(req, REQ_F_ASYNC_DATA);
return req->async_data;
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] io_uring: charge imported iovec arrays to memcg
2026-12-06 10:04 [PATCH 0/2] io_uring: charge request async data and iovec arrays to memcg Hang Zhengyang
2026-12-06 10:04 ` [PATCH 1/2] io_uring: charge request async data " Hang Zhengyang
@ 2026-12-06 10:04 ` Hang Zhengyang
1 sibling, 0 replies; 3+ messages in thread
From: Hang Zhengyang @ 2026-12-06 10:04 UTC (permalink / raw)
To: Jens Axboe; +Cc: Tao Cui, io-uring, linux-kernel
Vectored reads and writes, sendmsg, recvmsg and sends with
IORING_SEND_VECTORIZED import their iovecs with __import_iovec(). If the
array in the async data is too small, it allocates a new one with plain
GFP_KERNEL, and the request keeps it in the async data while it waits.
Without this series, 40000 readv requests of 1024 iovecs each, waiting
on an empty pipe, took 648MiB of unreclaimable slab with about 10MiB
charged, in a cgroup with memory.max at 64MiB.
Before the import, grow the array in the async data with
io_vec_realloc(), which charges it, so that __import_iovec() uses it
instead of allocating one. io_prep_reg_iovec() already does this for
registered buffers.
Found, fixed and tested by Claude Code, I handled the review process.
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
io_uring/net.c | 4 ++++
io_uring/rsrc.h | 12 ++++++++++++
io_uring/rw.c | 4 ++++
3 files changed, 20 insertions(+)
diff --git a/io_uring/net.c b/io_uring/net.c
index 994f6aadbda1..3dcc1de0539f 100644
--- a/io_uring/net.c
+++ b/io_uring/net.c
@@ -217,7 +217,11 @@ static int io_net_import_vec(struct io_kiocb *req, struct io_async_msghdr *iomsg
struct iovec *iov;
int ret, nr_segs;
+ ret = io_vec_reserve(&iomsg->vec, uvec_seg);
+ if (unlikely(ret))
+ return ret;
if (iomsg->vec.iovec) {
+ io_req_set_flags(req, REQ_F_NEED_CLEANUP);
nr_segs = iomsg->vec.nr;
iov = iomsg->vec.iovec;
} else {
diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h
index edbfe8174f6c..67d1acc8a6fc 100644
--- a/io_uring/rsrc.h
+++ b/io_uring/rsrc.h
@@ -169,6 +169,18 @@ static inline void io_vec_reset_iovec(struct iou_vec *iv,
iv->nr = nr;
}
+/*
+ * Make room for @nr_segs iovecs in @iv, so that __import_iovec() uses it
+ * instead of allocating an array that isn't charged to memcg and that we
+ * would then keep cached.
+ */
+static inline int io_vec_reserve(struct iou_vec *iv, unsigned int nr_segs)
+{
+ if (nr_segs <= 1 || nr_segs <= iv->nr || nr_segs > UIO_MAXIOV)
+ return 0;
+ return io_vec_realloc(iv, nr_segs);
+}
+
static inline void io_alloc_cache_vec_kasan(struct iou_vec *iv)
{
if (IS_ENABLED(CONFIG_KASAN))
diff --git a/io_uring/rw.c b/io_uring/rw.c
index 3ecb842147a4..df30bcb2cac8 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -76,7 +76,11 @@ static int io_import_vec(int ddir, struct io_kiocb *req,
int ret, nr_segs;
struct iovec *iov;
+ ret = io_vec_reserve(&io->vec, uvec_segs);
+ if (unlikely(ret))
+ return ret;
if (io->vec.iovec) {
+ io_req_set_flags(req, REQ_F_NEED_CLEANUP);
nr_segs = io->vec.nr;
iov = io->vec.iovec;
} else {
--
2.53.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-06 10:04 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-06 10:04 [PATCH 0/2] io_uring: charge request async data and iovec arrays to memcg Hang Zhengyang
2026-12-06 10:04 ` [PATCH 1/2] io_uring: charge request async data " Hang Zhengyang
2026-12-06 10:04 ` [PATCH 2/2] io_uring: charge imported iovec arrays " Hang Zhengyang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®