• Roman Gushchin's avatar
    mm: kmem: optimize get_obj_cgroup_from_current() · 7d0715d0
    Roman Gushchin authored
    Patch series "mm: improve performance of accounted kernel memory
    allocations", v5.
    
    This patchset improves the performance of accounted kernel memory
    allocations by ~30% as measured by a micro-benchmark [1].  The benchmark
    is very straightforward: 1M of 64 bytes-large kmalloc() allocations.
    
    Below are results with the disabled kernel memory accounting, the original state
    and with this patchset applied.
    
    |             | Kmem disabled | Original | Patched |  Delta |
    |-------------+---------------+----------+---------+--------|
    | User cgroup |         29764 |    84548 |   59078 | -30.0% |
    | Root cgroup |         29742 |    48342 |   31501 | -34.8% |
    
    As we can see, the patchset removes the majority of the overhead when
    there is no actual accounting (a task belongs to the root memory cgroup)
    and almost halves the accounting overhead otherwise.
    
    The main idea is to get rid of unnecessary memcg to objcg conversions and
    switch to a scope-based protection of objcgs, which eliminates extra
    operations with objcg reference counters under a rcu read lock.  More
    details are provided in individual commit descriptions.
    
    
    This patch (of 5):
    
    Manually inline memcg_kmem_bypass() and active_memcg() to speed up
    get_obj_cgroup_from_current() by avoiding duplicate in_task() checks and
    active_memcg() readings.
    
    Also add a likely() macro to __get_obj_cgroup_from_memcg():
    obj_cgroup_tryget() should succeed at almost all times except a very
    unlikely race with the memcg deletion path.
    
    Link: https://lkml.kernel.org/r/20231019225346.1822282-1-roman.gushchin@linux.dev
    Link: https://lkml.kernel.org/r/20231019225346.1822282-2-roman.gushchin@linux.devSigned-off-by: default avatarRoman Gushchin (Cruise) <roman.gushchin@linux.dev>
    Tested-by: default avatarNaresh Kamboju <naresh.kamboju@linaro.org>
    Acked-by: default avatarShakeel Butt <shakeelb@google.com>
    Acked-by: default avatarJohannes Weiner <hannes@cmpxchg.org>
    Reviewed-by: default avatarVlastimil Babka <vbabka@suse.cz>
    Cc: David Rientjes <rientjes@google.com>
    Cc: Dennis Zhou <dennis@kernel.org>
    Cc: Johannes Weiner <hannes@cmpxchg.org>
    Cc: Michal Hocko <mhocko@kernel.org>
    Cc: Muchun Song <muchun.song@linux.dev>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    7d0715d0
memcontrol.c 208 KB