From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: Tao Cui <cui.tao@linux.dev>,
io-uring@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] io_uring: charge imported iovec arrays to memcg
Date: Sun, 6 Dec 2026 10:04:49 +0000 [thread overview]
Message-ID: <20261206100449.2060856-3-hang.zhengyang1010@gmail.com> (raw)
In-Reply-To: <20261206100449.2060856-1-hang.zhengyang1010@gmail.com>
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
prev parent reply other threads:[~2026-12-06 10:04 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 10:04 [PATCH 0/2] io_uring: charge request async data and " 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 [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=20261206100449.2060856-3-hang.zhengyang1010@gmail.com \
--to=hang.zhengyang1010@gmail.com \
--cc=axboe@kernel.dk \
--cc=cui.tao@linux.dev \
--cc=io-uring@vger.kernel.org \
--cc=linux-kernel@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
all inboxes | Powered by JetHome®