Skip to content

Commit

Permalink
mm: memcontrol: do not miss MEMCG_MAX events for enforced allocations
Browse files Browse the repository at this point in the history
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/[email protected]
Signed-off-by: Roman Gushchin <[email protected]>
Reported-by: Yafang Shao <[email protected]>
Acked-by: Shakeel Butt <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Muchun Song <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
rgushchin authored and akpm00 committed Jul 30, 2022
1 parent ccac11d commit d6e103a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mm/memcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
bool passed_oom = false;
bool may_swap = true;
bool drained = false;
bool raised_max_event = false;
unsigned long pflags;

retry:
Expand Down Expand Up @@ -2616,6 +2617,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
goto nomem;

memcg_memory_event(mem_over_limit, MEMCG_MAX);
raised_max_event = true;

psi_memstall_enter(&pflags);
nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
Expand Down Expand Up @@ -2682,6 +2684,13 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
if (!(gfp_mask & (__GFP_NOFAIL | __GFP_HIGH)))
return -ENOMEM;
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);

/*
* The allocation either can't fail or will lead to more memory
* being freed very soon. Allow memory usage go over the limit
Expand Down

0 comments on commit d6e103a

Please sign in to comment.