From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794063155; b=C3Mv8t9V+ZCmYAQqcaU/+ZJt97jYqmq96Im+yfROklV5wcSDrld+rGww4RLElUlwCyvf bPv+EMfawSiUJJbYtzZ86DaLZ6Sl3KTg1h6D5eECHvC73QEBhrdvaCBX6CA8P8IA88esx Ail+gHJM/9KAi83TmroCxKhqNl2u4OGDvy2Gq/e/Ru0gq9lWR8MiJr3j4Qm6plWZsPo4d SAbjjt7KmXYIE0BTS1884oXmfL4B9QriuvwOnNdKk5nS+Za/5HtxLb1Qb8P/X/EUZlOuf F+BLB0/dRtQBJChDKw+80+QShaCTSnqHZExP0dxjEVaoBEjkAqKahd9Ggzx2J0t53dQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794063155; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Jw6fNHaaxYdbtydtu/vM6i3UzTktKX17y1A8k4Gi1NE=; b=Go2k+Tahlt6xuC7k9VFhgHbygaYAKdnpAschRd67QvZQCM3nNoQD3djkWp8ekrBapU4q F1uaBzBj/dlDUjFHHSWFvThC3qN4i0GpQfrTl9A2yZYQ7vwq7rxVkzovfDTsCI0aEh9SK yjjvlgtd7InACUrJFKHwnynB/AZpWDnpdGP2ZdwHiQ41FtJ5KoT2OILV+aIQlVkCJ2diL A55kxUe8JUz9rufNadUQpEGVcuHo1MhbHyzcPBWq45WT1p/7duI1l6GEW8SrXGZDk8Hsa 4koCujrhGPC1mlelFKzOwQeeMmTnHjYBVmiwCvFwotpRFMP7W0NMr6o1sEh9C1KxwBg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=VRkAnNKP; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA4EA606A331; Sat, 7 Nov 2026 14:52:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=VRkAnNKP Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9ECBE1F0089F; Sat, 7 Nov 2026 14:52:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794063154; bh=Jw6fNHaaxYdbtydtu/vM6i3UzTktKX17y1A8k4Gi1NE=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VRkAnNKPPFPnDlW850BoF5pp8t37DrYg2WY6CwqRknca5J3m+vnUhfkc+XOZfHQDr 5Y9DwjfQRSeXkIDe3VLq5qPJz+y1YS/VD6E3o/FxGcaofeXGTHGnGjaA3hPODgooZD azojWmL0gtjpSBQidQbIFnF2RtOgELOq/O8vrUJI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Joe Damato , Andrew Morton , Shakeel Butt , Johannes Weiner , Michal Hocko , Muchun Song , Roman Gushchin , Sasha Levin Subject: [PATCH 6.18 379/869] mm: memcontrol: raise MEMCG_MAX for charges that fail without reclaiming Date: Sat, 7 Nov 2026 08:51:01 -0500 Message-ID: <20261107134905.981367734@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134852.069646174@linuxfoundation.org> References: <20261107134852.069646174@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Joe Damato [ Upstream commit 374f65b1481992eb6b9c6bffd53f3afa5c2e94cf ] Charges that exceed memory.max and return through the nomem label can raise no event and simply return -ENOMEM. A non-blocking charge can hit the limit, get rejected, but is not visible in memory.events. This was noticed in a production setting where bpf_mem_alloc() attempted to refill its per-cpu freelists, which triggered a non-blocking charge while at the limit. Commit d6e103a757fa ("mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations") added raised_max_event to cover charges that are force charged without ever reaching reclaim, but charges that are rejected outright were left out. Getting an allocation failure without the corresponding MEMCG_MAX event is unexpected and makes debugging and monitoring harder. Raise the event on the way out for rejected charges as well, by routing the -ENOMEM return through the same exit path that already covers forced charges. The existing behavior of raising a MEMCG_MAX event on every charge/reclaim/retry iteration is left unchanged. Tested with a module that performs accounted GFP_NOWAIT page allocations from a task in a cgroup at its memory.max, and measures the resulting memory.events:max delta. Without this patch the rejected charges raise no event at all; with it the delta matches the number of rejected charges exactly. A GFP_KERNEL|__GFP_NORETRY control, which reaches reclaim, raises the same two events per failed charge before and after, confirming the existing charge/reclaim/retry accounting is unchanged. Link: https://lore.kernel.org/20260831174836.3102406-1-joe@dama.to Fixes: d6e103a757fa ("mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations") Signed-off-by: Joe Damato Signed-off-by: Andrew Morton Suggested-by: Shakeel Butt Acked-by: Shakeel Butt Cc: Johannes Weiner Cc: Michal Hocko Cc: Muchun Song Cc: Roman Gushchin Cc: Signed-off-by: Sasha Levin --- mm/memcontrol.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 82ad23d689c7f..2aa741316b49f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2311,10 +2311,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, bool raised_max_event = false; unsigned long pflags; bool allow_spinning = gfpflags_allow_spinning(gfp_mask); + int ret = 0; retry: if (consume_stock(memcg, nr_pages)) - return 0; + return ret; if (!allow_spinning) /* Avoid the refill and flush of the older stock */ @@ -2424,16 +2425,11 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, * put the burden of reclaim on regular allocation requests * and let these go through as privileged allocations. */ - if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) - return -ENOMEM; + if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) { + ret = -ENOMEM; + goto out; + } force: - /* - * If the allocation has to be enforced, don't forget to raise - * a MEMCG_MAX event. - */ - if (!raised_max_event) - __memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning); - /* * The allocation either can't fail or will lead to more memory * being freed very soon. Allow memory usage go over the limit @@ -2443,7 +2439,15 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, if (do_memsw_account()) page_counter_charge(&memcg->memsw, nr_pages); - return 0; +out: + /* + * Don't forget to raise a MEMCG_MAX event for forced or rejected + * requests. + */ + if (!raised_max_event) + __memcg_memory_event(mem_over_limit, MEMCG_MAX, allow_spinning); + + return ret; done_restock: if (batch > nr_pages) @@ -2502,7 +2506,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, !(current->flags & PF_MEMALLOC) && gfpflags_allow_blocking(gfp_mask)) __mem_cgroup_handle_over_high(gfp_mask); - return 0; + return ret; } static inline int try_charge(struct mem_cgroup *memcg, gfp_t gfp_mask, -- 2.53.0