From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A896E43DA2C; Tue, 16 Jun 2026 15:35:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781624131; cv=none; b=hng1hGqTriyLawQO70BdC86ZO5g8P5hXVkfUN0SnvgHdXEDQDmeytdL+b3WKYQ3YzhYl7vg7j6Dj7PAT8gzjzdERnfaDAsiPryZ6iXTBcBXFriJWeRRIZuhyHY5gAU6oa3315ju9xwfzZEVBF9Iaz9H27sVdS1cbBft2fLFDBAo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781624131; c=relaxed/simple; bh=9Gd019rsNybPUSo2k1RMqvqsc0fxkvZ/yfiFT/fS4EM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ftLpViJeOT0obQ4CjynTXu7uzXRliuOIV+Rb6g6Zhe5UWUaaKkOzZi0O2wy9Qs17UrErZbkP7XJyiFAX9aP3CXYEVL/5VucP00zBXV+HU2TK8Zb/qBxkWG2lb4Q3JdAwjIZWR4hIpWmfO3sgJQtPzVqtA2xrxpQeRiBHGYec0wk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=CQ2ncvlg; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="CQ2ncvlg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACD8A1F000E9; Tue, 16 Jun 2026 15:35:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1781624130; bh=NT38UhiihVPZgos/IJ43lzbnLFefT2ryXFWJDQbwd7I=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CQ2ncvlgnG27jYv340ebBWNlDR07oR32ZUEF/JwBAykH9NcpIBtsbQE5oKrcghb2k wPCQUVVaS9CGkLY8kd9tJeiLyWtQjPyKsDxfb9pS1iO2lmBhZTQEfDEEnXLIfjLZqe btQFFiiFuaZIGEZ+u1OVFzy/mvSkx10iCSz2WH+Y= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Shakeel Butt , Harry Yoo , Michal Hocko , Johannes Weiner , Muchun Song , Roman Gushchin , Andrew Morton Subject: [PATCH 7.0 280/378] memcg: use round-robin victim selection in refill_stock Date: Tue, 16 Jun 2026 20:28:31 +0530 Message-ID: <20260616145124.848793756@linuxfoundation.org> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260616145109.744539446@linuxfoundation.org> References: <20260616145109.744539446@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.0-stable review patch. If anyone has any objections, please let me know. ------------------ From: Shakeel Butt commit c0cafe24d3f6534294c4b2bc2d47734ff7cbd313 upstream. Harry Yoo reported that get_random_u32_below() is not safe to call in the nmi context and memcg charge draining can happen in nmi context. More specifically get_random_u32_below() is neither reentrant- nor NMI-safe: it acquires a per-cpu local_lock via local_lock_irqsave() on the batched_entropy_u32 state. An NMI that lands on a CPU mid-update of the ChaCha batch state and recurses into the random subsystem would corrupt that state. The memcg_stock local_trylock prevents re-entry on the percpu stock itself, but cannot protect an unrelated subsystem's per-cpu lock. Replace the random pick with a per-cpu round-robin counter stored in memcg_stock_pcp and serialized by the same local_trylock that already guards cached[] and nr_pages[]. No atomics, no random calls, no extra locks needed. Link: https://lore.kernel.org/20260521223751.3794625-1-shakeel.butt@linux.dev Fixes: f735eebe55f8f ("memcg: multi-memcg percpu charge cache") Signed-off-by: Shakeel Butt Reported-by: Harry Yoo Closes: https://lore.kernel.org/4e20f643-6983-4b6e-b12d-c6c4eb20ae0c@kernel.org/ Acked-by: Harry Yoo (Oracle) Acked-by: Michal Hocko Cc: Johannes Weiner Cc: Muchun Song Cc: Roman Gushchin Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/memcontrol.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1799,6 +1799,7 @@ struct memcg_stock_pcp { struct work_struct work; unsigned long flags; + uint8_t drain_idx; }; static DEFINE_PER_CPU_ALIGNED(struct memcg_stock_pcp, memcg_stock) = { @@ -1982,7 +1983,9 @@ static void refill_stock(struct mem_cgro if (!success) { i = empty_slot; if (i == -1) { - i = get_random_u32_below(NR_MEMCG_STOCK); + i = stock->drain_idx++; + if (stock->drain_idx == NR_MEMCG_STOCK) + stock->drain_idx = 0; drain_stock(stock, i); } css_get(&memcg->css);