From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796551499; b=wVWCsWX7u/8pd/faTaNu/6ALW6yIRA8vhomi3Lk+WtbULQdeJN1rxX0DZpn6mzSQpbnZ Zhk4Ow323MT887rNBr1EdZZ/grBtQQWioTlBFOpwsbbv46TQeUCONQPzHKqGUOYTAq0pH Rym2575cGtfjsouJVuFNxH7EprVGGUOKGDjjsQ9hzBNlko89010Nb1m5YqeSVo0rEpg8i i3Tu7jujMOIzYDcS7yTB0uMm9Ku8TNyDEeMYxlekSQrKEK3iL19L7NZ8VcJ1fFG2PuzZk 2dZLZnTHsbZEebCg2PJMUrhf7XLpK+dTERxLMjv35ZvA5Sao31wjgCSBZeOVOR36iWg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796551499; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=cgrB+hG4azoIPu0U1urETw0cS9IrEtzCSIXQqMkMskk=; b=QSJ4L6FAbAh/F3CAoW6f9V82M7Ap78x5IMT6bIMwWfwSOY+ZGK3ImLSoDXdmuVaOPJlN E1wSJcLmIiYfkn5uF+0NEIk2jHD5tdGNeOCkq7WPLPH665Z1JW5WNV0M/DZA8aaA/ZB0U tbdfz7VEargz/fHtfxQmIBkWKTDdMNNtssJPB2SpxoX4swKd3tYfEPdzqPEpbhqdoPgo5 Ov2ZSGo2ge3LL03rj9GPDouOvCN1Oj2fAtf70/ViFiBQK7gTvc1HzcLnYAk25PsfC6auq B77A6RFERPyCPF47POJUTS5cZo2hYQFhf4DhziKhv65iBQOUY8gZpvtJSIJU6q1aq1w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="gz/Tmcbc"; arc=none smtp.remote-ip=74.125.82.51 Received: from mail-dl1-f51.google.com (mail-dl1-f51.google.com [74.125.82.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB362606A68A for ; Sun, 6 Dec 2026 10:04:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB362606A68A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=gz/Tmcbc Received: by mail-dl1-f51.google.com with SMTP id a92af1059eb24-170135e43c7so6066236c88.0 for ; Sun, 06 Dec 2026 02:04:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796551498; x=1797156298; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cgrB+hG4azoIPu0U1urETw0cS9IrEtzCSIXQqMkMskk=; b=gz/Tmcbcqmlw6Dhy/zpSefM0LSuKpwQhNDeds8Mj9S3DVsHdF2hpPE50IaPLkFDUZ9 77k3XO2utIfWP2KxUBp+PSm49Mxih/Is2emJ/sq8EpO8IvZEjHHUSeF58wHfr0yPh/k4 Mj9pA6qYNRAWXjNhgOE8EiUJuaFduTFxoE5z7qBudzCEx/aCcHUUl0KPZ3AKh9mogCS9 WLY7x7xjfVQqo/KSeiKTSPubFAdykMH2F76S0jHqmoYNvQTUU4D1mHRaVD3oMK4/vx0S YSHz/lAi1FYFg2UozwsjuIwkQC8stIyNm9B+NKvMzQlmDobgGqR4OSXlE2QcIZbdBbDC hJFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796551498; x=1797156298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=cgrB+hG4azoIPu0U1urETw0cS9IrEtzCSIXQqMkMskk=; b=1zkqF87TYTDxcoNUrJfUFf6AUcCJinN8VJFzQN4JqP6Fur5FOHUQHq7efgpHltVm7v wZipPiDQscLpSKJv9jnuWjIR9a2YKMIJjW5WZu8YIH30xOYLVOl48X+b18fx8J0kFVoi as0HYOv21RO3pPklYWZfuEcSFIIgIc5RsRR8CMeDPGTfluTlceVm3xfJl1wTPgQQv0QQ JCC3UIkwC33P1Mqwd5YWQAryLoiAB3NC5xzQxDkuUuOr5iEqY1000dIyH6Y9B2/2F5nG sEuY0icw2nYzAb30wvUJhOpNJDi/DX7tutgr7h2Th6PBZN3RnDDdccigw5UoBecqJo/M C6wQ== X-Forwarded-Encrypted: i=1; AL+hErEgXFILLGyq1VEEcndfMpt3K+GZ3/lZ9+SQACXKa/MQqv4yGiHQL6n93A5558BtLVHwKEGHTwLi7DXx1Uo=@vger.kernel.org X-Gm-Message-State: APuU4Z9s9+Xm4FRuBQP96t1RL7WM7FPcMTW4bN0U5zKq5uEuW2LJmJTJ A9DogQ6ob8nfc96D6zLfQyBTKBxxSt7I80qM9FmQULJuB1roeQoWSlVD X-Gm-Gg: AZFdTZczOBv9lFdK7uYZ/LfkanlisWyT+RB015YfzgblWwUAG2BmEmeldD7PRnWbauK gDlz7/5fwNoY0k2SNAP7jn5et13ig0pEy3zE76kmgkIMSkuAkPF7hvx7aIcIaVZFBqoTj6QTLX6 hmorQ0rW4KRutG8YCJtq2m8S2vdfcqhowdoJ8/hIM/6062JNt3wKmEhn8L4TPGpCO5N4Dy1us8R H5X+Rzkxl51yY/dkMHz+72WhZirzfYyTHyaZYHvP45vNkhXYyvIae8eVE1QCeEJ8yBROSYxyuqC bXjv/n2oWLT1zCibcOIxSkHEQhO18V7IY3QftONFEFfWyHQXjaHzJqXTUeW6b526qCiqRfLbfd3 10NLNj3Gd+pOp3hkSK+1r2Wjba0AXKa6ltjbMd/iBqlwuZobC4IYIQKGzSKXvnCz8cOtYuWjJgE wGqlivb3MwFkINmroyUbT0NhvI2q6TP6zaxYfjtWwrlUwfjFDmi6BIUp83K5EVOC4QxUL+DMgMh EIp5BXjnWmacDXavGLh2XAFnq9t0xjTDPggRfZM1iJjxwzMli5qSpIkRG2w2Ih1Az9PBrUmgsyn ag6V+Cgn4OfIvpFknyUeWxGrrmj2nhnT3TktKQ1hJijkpz5pxApS7hyjuN0U/FTpug== X-Received: by 2002:a05:7022:b1d1:20b0:1b5:8a69:cc65 with SMTP id a92af1059eb24-1b5d705c400mr6961621c88.4.1796551497690; Sun, 06 Dec 2026 02:04:57 -0800 (PST) Received: from 6610b1a2ef46 ([203.116.140.24]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d64706d3sm24295789c88.13.2026.12.06.02.04.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 02:04:57 -0800 (PST) From: Hang Zhengyang To: Jens Axboe Cc: Tao Cui , 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 Message-ID: <20261206100449.2060856-3-hang.zhengyang1010@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261206100449.2060856-1-hang.zhengyang1010@gmail.com> References: <20261206100449.2060856-1-hang.zhengyang1010@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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