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