• Roman Gushchin's avatar
    mm: fork: fix kernel_stack memcg stats for various stack implementations · 8380ce47
    Roman Gushchin authored
    Depending on CONFIG_VMAP_STACK and the THREAD_SIZE / PAGE_SIZE ratio the
    space for task stacks can be allocated using __vmalloc_node_range(),
    alloc_pages_node() and kmem_cache_alloc_node().
    
    In the first and the second cases page->mem_cgroup pointer is set, but
    in the third it's not: memcg membership of a slab page should be
    determined using the memcg_from_slab_page() function, which looks at
    page->slab_cache->memcg_params.memcg .  In this case, using
    mod_memcg_page_state() (as in account_kernel_stack()) is incorrect:
    page->mem_cgroup pointer is NULL even for pages charged to a non-root
    memory cgroup.
    
    It can lead to kernel_stack per-memcg counters permanently showing 0 on
    some architectures (depending on the configuration).
    
    In order to fix it, let's introduce a mod_memcg_obj_state() helper,
    which takes a pointer to a kernel object as a first argument, uses
    mem_cgroup_from_obj() to get a RCU-protected memcg pointer and calls
    mod_memcg_state().  It allows to handle all possible configurations
    (CONFIG_VMAP_STACK and various THREAD_SIZE/PAGE_SIZE values) without
    spilling any memcg/kmem specifics into fork.c .
    
    Note: This is a special version of the patch created for stable
    backports.  It contains code from the following two patches:
      - mm: memcg/slab: introduce mem_cgroup_from_obj()
      - mm: fork: fix kernel_stack memcg stats for various stack implementations
    
    [guro@fb.com: introduce mem_cgroup_from_obj()]
      Link: http://lkml.kernel.org/r/20200324004221.GA36662@carbon.dhcp.thefacebook.com
    Fixes: 4d96ba35 ("mm: memcg/slab: stop setting page->mem_cgroup pointer for slab pages")
    Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Reviewed-by: default avatarShakeel Butt <shakeelb@google.com>
    Acked-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
    Cc: Michal Hocko <mhocko@kernel.org>
    Cc: Bharata B Rao <bharata@linux.ibm.com>
    Cc: Shakeel Butt <shakeelb@google.com>
    Cc: <stable@vger.kernel.org>
    Link: http://lkml.kernel.org/r/20200303233550.251375-1-guro@fb.comSigned-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    8380ce47
memcontrol.h 35.2 KB