Re: [PATCH] mm: rework remote memcg charging API to support nesting

From: Shakeel Butt
Date: Fri Aug 21 2020 - 18:41:57 EST


On Fri, Aug 21, 2020 at 2:21 PM Roman Gushchin <guro@xxxxxx> wrote:
>
> Currently the remote memcg charging API consists of two functions:
> memalloc_use_memcg() and memalloc_unuse_memcg(), which set and clear
> the memcg value, which overwrites the memcg of the current task.
>
> memalloc_use_memcg(target_memcg);
> <...>
> memalloc_unuse_memcg();
>
> It works perfectly for allocations performed from a normal context,
> however an attempt to call it from an interrupt context or just nest
> two remote charging blocks will lead to an incorrect accounting.
> On exit from the inner block the active memcg will be cleared
> instead of being restored.
>
> memalloc_use_memcg(target_memcg);
>
> memalloc_use_memcg(target_memcg_2);
> <...>
> memalloc_unuse_memcg();
>
> Error: allocation here are charged to the memcg of the current
> process instead of target_memcg.
>
> memalloc_unuse_memcg();
>
> This patch extends the remote charging API by switching to a single
> function: struct mem_cgroup *set_active_memcg(struct mem_cgroup *memcg),
> which sets the new value and returns the old one. So a remote charging
> block will look like:
>
> old_memcg = set_active_memcg(target_memcg);
> <...>
> set_active_memcg(old_memcg);
>
> This patch is heavily based on the patch by Johannes Weiner,
> which can be found here: https://lkml.org/lkml/2020/5/28/806 .
>
> Signed-off-by: Roman Gushchin <guro@xxxxxx>
> Cc: Johannes Weiner <hannes@xxxxxxxxxxx>
> Cc: Shakeel Butt <shakeelb@xxxxxxxxxx>
> Cc: Dan Schatzberg <dschatzberg@xxxxxx>

Reviewed-by: Shakeel Butt <shakeelb@xxxxxxxxxx>