mm: memcontrol: raise MEMCG_MAX for charges that fail without reclaiming
Subsystems (5)
PATCH v2 1/1 mm: memcontrol: raise MEMCG_MAX for charges that fail without reclaiming
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.
---
v2:
- v1 raised MEMCG_MAX once, as soon as the charge was known not to fit, which collapsed the existing per-iteration events raised while a charge loops through reclaim and retry. Instead leave that event where it is and route the -ENOMEM return through the same exit path that already covers forced charges, so only the rejected-charge case changes, as suggested by Shakeel.
- Add Fixes tag and CC stable, as suggested by Shakeel.
v1: https://lore.kernel.org/cgroups/[email protected]/
mm/memcontrol.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
mm/memcontrol.c +16 -12
@@ -2656,10 +2656,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 */
@@ -2770,16 +2771,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
@@ -2789,7 +2785,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)
@@ -2848,7 +2852,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,
Signature · 1 line
2.53.0-Meta
Quoted 30 lines · Joe Damato
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 missMEMCG_MAXevents for enforced allocations") addedraised_max_eventto 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 correspondingMEMCG_MAXevent is unexpected and makes debugging and monitoring harder.Raise the event on the way out for rejected charges as well, by routing the
-ENOMEMreturn through the same exit path that already covers forced charges. The existing behavior of raising aMEMCG_MAXevent on every charge/reclaim/retry iteration is left unchanged.Tested with a module that performs accounted
GFP_NOWAITpage 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.
Quoted 4 lines
Fixes d6e103a757fa ("mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations")Suggested-by Shakeel Butt <[email protected]>Signed-off-by Joe Damato <[email protected]>