Re: [PATCH v5 1/2] mm: memcg: remote memcg charging for kmem allocations

From: Shakeel Butt
Date: Tue Jun 05 2018 - 13:54:30 EST


On Mon, Apr 16, 2018 at 1:51 PM, Shakeel Butt <shakeelb@xxxxxxxxxx> wrote:
> Introduce the memcg variant for kmalloc[_node] and
> kmem_cache_alloc[_node]. For kmem_cache_alloc, the kernel switches the
> root kmem cache with the memcg specific kmem cache for __GFP_ACCOUNT
> allocations to charge those allocations to the memcg. However, the memcg
> to charge is extracted from the current task_struct. This patch
> introduces the variant of kmem cache allocation functions where the memcg
> can be provided explicitly by the caller instead of deducing the memcg
> from the current task.
>
> The kmalloc allocations are underlying served using the kmem caches unless
> the size of the allocation request is larger than KMALLOC_MAX_CACHE_SIZE,
> in which case, the kmem caches are bypassed and the request is routed
> directly to page allocator. So, for __GFP_ACCOUNT kmalloc allocations,
> the memcg of current task is charged. This patch introduces memcg variant
> of kmalloc functions to allow callers to provide memcg for charging.
>
> These functions are useful for use-cases where the allocations should be
> charged to the memcg different from the memcg of the caller. One such
> concrete use-case is the allocations for fsnotify event objects where the
> objects should be charged to the listener instead of the producer.
>
> One requirement to call these functions is that the caller must have the
> reference to the memcg. Using kmalloc_memcg and kmem_cache_alloc_memcg
> implicitly assumes that the caller is requesting a __GFP_ACCOUNT
> allocation.
>
> Signed-off-by: Shakeel Butt <shakeelb@xxxxxxxxxx>

I will send the v6 of this patchset after this merge window. In v6, I
will make memalloc_memcg_[save|restore] scope API similar to NOFS,
NOIO and NORECLAIM APIs.

> ---
> Changelog since v4:
> - Removed branch from hot path of memory charging.
>
> Changelog since v3:
> - Added node variant of directed kmem allocation functions.
>
> Changelog since v2:
> - Merge the kmalloc_memcg patch into this patch.
> - Instead of plumbing memcg throughout, use field in task_struct to pass
> the target_memcg.
>
> Changelog since v1:
> - Fixed build for SLOB
>