Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations

Yafang Shao reported an issue related to the accounting of bpf memory:
if a bpf map is charged indirectly for memory consumed from an
interrupt context and allocations are enforced, MEMCG_MAX events are
not raised.

It's not/less of an issue in a generic case because consequent
allocations from a process context will trigger the direct reclaim and
MEMCG_MAX events will be raised. However a bpf map can belong to a
dying/abandoned memory cgroup, so there will be no allocations from a
process context and no MEMCG_MAX events will be triggered.

Link: https://lkml.kernel.org/r/20220702033521.64630-1-roman.gushchin@linux.dev
Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Reported-by: Yafang Shao <laoar.shao@gmail.com>
Acked-by: Shakeel Butt <shakeelb@google.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Muchun Song <songmuchun@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Roman Gushchin and committed by
akpm
d6e103a7 ccac11da

+9
+9
mm/memcontrol.c
··· 2577 2577 bool passed_oom = false; 2578 2578 bool may_swap = true; 2579 2579 bool drained = false; 2580 + bool raised_max_event = false; 2580 2581 unsigned long pflags; 2581 2582 2582 2583 retry: ··· 2617 2616 goto nomem; 2618 2617 2619 2618 memcg_memory_event(mem_over_limit, MEMCG_MAX); 2619 + raised_max_event = true; 2620 2620 2621 2621 psi_memstall_enter(&pflags); 2622 2622 nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages, ··· 2684 2682 if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH))) 2685 2683 return -ENOMEM; 2686 2684 force: 2685 + /* 2686 + * If the allocation has to be enforced, don't forget to raise 2687 + * a MEMCG_MAX event. 2688 + */ 2689 + if (!raised_max_event) 2690 + memcg_memory_event(mem_over_limit, MEMCG_MAX); 2691 + 2687 2692 /* 2688 2693 * The allocation either can't fail or will lead to more memory 2689 2694 * being freed very soon. Allow memory usage go over the limit