13 Aug, 2020

1 commit


08 Aug, 2020

11 commits

  • Instead of having two sets of kmem_caches: one for system-wide and
    non-accounted allocations and the second one shared by all accounted
    allocations, we can use just one.

    The idea is simple: space for obj_cgroup metadata can be allocated on
    demand and filled only for accounted allocations.

    It allows to remove a bunch of code which is required to handle kmem_cache
    clones for accounted allocations. There is no more need to create them,
    accumulate statistics, propagate attributes, etc. It's a quite
    significant simplification.

    Also, because the total number of slab_caches is reduced almost twice (not
    all kmem_caches have a memcg clone), some additional memory savings are
    expected. On my devvm it additionally saves about 3.5% of slab memory.

    [guro@fb.com: fix build on MIPS]
    Link: http://lkml.kernel.org/r/20200717214810.3733082-1-guro@fb.com

    Suggested-by: Johannes Weiner
    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Michal Hocko
    Cc: Tejun Heo
    Cc: Naresh Kamboju
    Link: http://lkml.kernel.org/r/20200623174037.3951353-18-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • memcg_accumulate_slabinfo() is never called with a non-root kmem_cache as
    a first argument, so the is_root_cache(s) check is redundant and can be
    removed without any functional change.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-17-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Currently there are two lists of kmem_caches:
    1) slab_caches, which contains all kmem_caches,
    2) slab_root_caches, which contains only root kmem_caches.

    And there is some preprocessor magic to have a single list if
    CONFIG_MEMCG_KMEM isn't enabled.

    It was required earlier because the number of non-root kmem_caches was
    proportional to the number of memory cgroups and could reach really big
    values. Now, when it cannot exceed the number of root kmem_caches, there
    is really no reason to maintain two lists.

    We never iterate over the slab_root_caches list on any hot paths, so it's
    perfectly fine to iterate over slab_caches and filter out non-root
    kmem_caches.

    It allows to remove a lot of config-dependent code and two pointers from
    the kmem_cache structure.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-16-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • The memcg_kmem_get_cache() function became really trivial, so let's just
    inline it into the single call point: memcg_slab_pre_alloc_hook().

    It will make the code less bulky and can also help the compiler to
    generate a better code.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-15-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Because the number of non-root kmem_caches doesn't depend on the number of
    memory cgroups anymore and is generally not very big, there is no more
    need for a dedicated workqueue.

    Also, as there is no more need to pass any arguments to the
    memcg_create_kmem_cache() except the root kmem_cache, it's possible to
    just embed the work structure into the kmem_cache and avoid the dynamic
    allocation of the work structure.

    This will also simplify the synchronization: for each root kmem_cache
    there is only one work. So there will be no more concurrent attempts to
    create a non-root kmem_cache for a root kmem_cache: the second and all
    following attempts to queue the work will fail.

    On the kmem_cache destruction path there is no more need to call the
    expensive flush_workqueue() and wait for all pending works to be finished.
    Instead, cancel_work_sync() can be used to cancel/wait for only one work.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-14-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • This is fairly big but mostly red patch, which makes all accounted slab
    allocations use a single set of kmem_caches instead of creating a separate
    set for each memory cgroup.

    Because the number of non-root kmem_caches is now capped by the number of
    root kmem_caches, there is no need to shrink or destroy them prematurely.
    They can be perfectly destroyed together with their root counterparts.
    This allows to dramatically simplify the management of non-root
    kmem_caches and delete a ton of code.

    This patch performs the following changes:
    1) introduces memcg_params.memcg_cache pointer to represent the
    kmem_cache which will be used for all non-root allocations
    2) reuses the existing memcg kmem_cache creation mechanism
    to create memcg kmem_cache on the first allocation attempt
    3) memcg kmem_caches are named -memcg,
    e.g. dentry-memcg
    4) simplifies memcg_kmem_get_cache() to just return memcg kmem_cache
    or schedule it's creation and return the root cache
    5) removes almost all non-root kmem_cache management code
    (separate refcounter, reparenting, shrinking, etc)
    6) makes slab debugfs to display root_mem_cgroup css id and never
    show :dead and :deact flags in the memcg_slabinfo attribute.

    Following patches in the series will simplify the kmem_cache creation.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-13-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Deprecate memory.kmem.slabinfo.

    An empty file will be presented if corresponding config options are
    enabled.

    The interface is implementation dependent, isn't present in cgroup v2, and
    is generally useful only for core mm debugging purposes. In other words,
    it doesn't provide any value for the absolute majority of users.

    A drgn-based replacement can be found in
    tools/cgroup/memcg_slabinfo.py. It does support cgroup v1 and v2,
    mimics memory.kmem.slabinfo output and also allows to get any
    additional information without a need to recompile the kernel.

    If a drgn-based solution is too slow for a task, a bpf-based tracing tool
    can be used, which can easily keep track of all slab allocations belonging
    to a memory cgroup.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Acked-by: Johannes Weiner
    Cc: Christoph Lameter
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-11-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • In order to prepare for per-object slab memory accounting, convert
    NR_SLAB_RECLAIMABLE and NR_SLAB_UNRECLAIMABLE vmstat items to bytes.

    To make it obvious, rename them to NR_SLAB_RECLAIMABLE_B and
    NR_SLAB_UNRECLAIMABLE_B (similar to NR_KERNEL_STACK_KB).

    Internally global and per-node counters are stored in pages, however memcg
    and lruvec counters are stored in bytes. This scheme may look weird, but
    only for now. As soon as slab pages will be shared between multiple
    cgroups, global and node counters will reflect the total number of slab
    pages. However memcg and lruvec counters will be used for per-memcg slab
    memory tracking, which will take separate kernel objects in the account.
    Keeping global and node counters in pages helps to avoid additional
    overhead.

    The size of slab memory shouldn't exceed 4Gb on 32-bit machines, so it
    will fit into atomic_long_t we use for vmstats.

    Signed-off-by: Roman Gushchin
    Signed-off-by: Andrew Morton
    Reviewed-by: Shakeel Butt
    Acked-by: Johannes Weiner
    Acked-by: Vlastimil Babka
    Cc: Christoph Lameter
    Cc: Michal Hocko
    Cc: Tejun Heo
    Link: http://lkml.kernel.org/r/20200623174037.3951353-4-guro@fb.com
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • kmalloc cannot allocate memory from HIGHMEM. Allocating large amounts of
    memory currently bypasses the check and will simply leak the memory when
    page_address() returns NULL. To fix this, factor the GFP_SLAB_BUG_MASK
    check out of slab & slub, and call it from kmalloc_order() as well. In
    order to make the code clear, the warning message is put in one place.

    Signed-off-by: Long Li
    Signed-off-by: Andrew Morton
    Reviewed-by: Matthew Wilcox (Oracle)
    Reviewed-by: Pekka Enberg
    Acked-by: David Rientjes
    Cc: Christoph Lameter
    Cc: Joonsoo Kim
    Link: http://lkml.kernel.org/r/20200704035027.GA62481@lilong
    Signed-off-by: Linus Torvalds

    Long Li
     
  • Other mm routines such as kfree() and kzfree() silently do the right thing
    if passed a NULL pointer, so ksize() should do the same.

    Signed-off-by: William Kucharski
    Signed-off-by: Andrew Morton
    Reviewed-by: Matthew Wilcox (Oracle)
    Reviewed-by: David Hildenbrand
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Link: http://lkml.kernel.org/r/20200616225409.4670-1-william.kucharski@oracle.com
    Signed-off-by: Linus Torvalds

    William Kucharski
     
  • As said by Linus:

    A symmetric naming is only helpful if it implies symmetries in use.
    Otherwise it's actively misleading.

    In "kzalloc()", the z is meaningful and an important part of what the
    caller wants.

    In "kzfree()", the z is actively detrimental, because maybe in the
    future we really _might_ want to use that "memfill(0xdeadbeef)" or
    something. The "zero" part of the interface isn't even _relevant_.

    The main reason that kzfree() exists is to clear sensitive information
    that should not be leaked to other future users of the same memory
    objects.

    Rename kzfree() to kfree_sensitive() to follow the example of the recently
    added kvfree_sensitive() and make the intention of the API more explicit.
    In addition, memzero_explicit() is used to clear the memory to make sure
    that it won't get optimized away by the compiler.

    The renaming is done by using the command sequence:

    git grep -w --name-only kzfree |\
    xargs sed -i 's/kzfree/kfree_sensitive/'

    followed by some editing of the kfree_sensitive() kerneldoc and adding
    a kzfree backward compatibility macro in slab.h.

    [akpm@linux-foundation.org: fs/crypto/inline_crypt.c needs linux/slab.h]
    [akpm@linux-foundation.org: fix fs/crypto/inline_crypt.c some more]

    Suggested-by: Joe Perches
    Signed-off-by: Waiman Long
    Signed-off-by: Andrew Morton
    Acked-by: David Howells
    Acked-by: Michal Hocko
    Acked-by: Johannes Weiner
    Cc: Jarkko Sakkinen
    Cc: James Morris
    Cc: "Serge E. Hallyn"
    Cc: Joe Perches
    Cc: Matthew Wilcox
    Cc: David Rientjes
    Cc: Dan Carpenter
    Cc: "Jason A . Donenfeld"
    Link: http://lkml.kernel.org/r/20200616154311.12314-3-longman@redhat.com
    Signed-off-by: Linus Torvalds

    Waiman Long
     

25 Jul, 2020

1 commit

  • If the kmem_cache refcount is greater than one, we should not mark the
    root kmem_cache as dying. If we mark the root kmem_cache dying
    incorrectly, the non-root kmem_cache can never be destroyed. It
    resulted in memory leak when memcg was destroyed. We can use the
    following steps to reproduce.

    1) Use kmem_cache_create() to create a new kmem_cache named A.
    2) Coincidentally, the kmem_cache A is an alias for kmem_cache B,
    so the refcount of B is just increased.
    3) Use kmem_cache_destroy() to destroy the kmem_cache A, just
    decrease the B's refcount but mark the B as dying.
    4) Create a new memory cgroup and alloc memory from the kmem_cache
    B. It leads to create a non-root kmem_cache for allocating memory.
    5) When destroy the memory cgroup created in the step 4), the
    non-root kmem_cache can never be destroyed.

    If we repeat steps 4) and 5), this will cause a lot of memory leak. So
    only when refcount reach zero, we mark the root kmem_cache as dying.

    Fixes: 92ee383f6daa ("mm: fix race between kmem_cache destroy, create and deactivate")
    Signed-off-by: Muchun Song
    Signed-off-by: Andrew Morton
    Reviewed-by: Shakeel Butt
    Acked-by: Roman Gushchin
    Cc: Vlastimil Babka
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Shakeel Butt
    Cc:
    Link: http://lkml.kernel.org/r/20200716165103.83462-1-songmuchun@bytedance.com
    Signed-off-by: Linus Torvalds

    Muchun Song
     

26 Jun, 2020

1 commit

  • The kzfree() function is normally used to clear some sensitive
    information, like encryption keys, in the buffer before freeing it back to
    the pool. Memset() is currently used for buffer clearing. However
    unlikely, there is still a non-zero probability that the compiler may
    choose to optimize away the memory clearing especially if LTO is being
    used in the future.

    To make sure that this optimization will never happen,
    memzero_explicit(), which is introduced in v3.18, is now used in
    kzfree() to future-proof it.

    Link: http://lkml.kernel.org/r/20200616154311.12314-2-longman@redhat.com
    Fixes: 3ef0e5ba4673 ("slab: introduce kzfree()")
    Signed-off-by: Waiman Long
    Acked-by: Michal Hocko
    Cc: David Howells
    Cc: Jarkko Sakkinen
    Cc: James Morris
    Cc: "Serge E. Hallyn"
    Cc: Joe Perches
    Cc: Matthew Wilcox
    Cc: David Rientjes
    Cc: Johannes Weiner
    Cc: Dan Carpenter
    Cc: "Jason A . Donenfeld"
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Waiman Long
     

03 Jun, 2020

1 commit

  • We have seen a "usercopy: Kernel memory overwrite attempt detected to
    SLUB object 'dma-kmalloc-1 k' (offset 0, size 11)!" error on s390x, as
    IUCV uses kmalloc() with __GFP_DMA because of memory address
    restrictions. The issue has been discussed [2] and it has been noted
    that if all the kmalloc caches are marked as usercopy, there's little
    reason not to mark dma-kmalloc caches too. The 'dma' part merely means
    that __GFP_DMA is used to restrict memory address range.

    As Jann Horn put it [3]:
    "I think dma-kmalloc slabs should be handled the same way as normal
    kmalloc slabs. When a dma-kmalloc allocation is freshly created, it is
    just normal kernel memory - even if it might later be used for DMA -,
    and it should be perfectly fine to copy_from_user() into such
    allocations at that point, and to copy_to_user() out of them at the
    end. If you look at the places where such allocations are created, you
    can see things like kmemdup(), memcpy() and so on - all normal
    operations that shouldn't conceptually be different from usercopy in
    any relevant way."

    Thus this patch marks the dma-kmalloc-* caches as usercopy.

    [1] https://bugzilla.suse.com/show_bug.cgi?id=1156053
    [2] https://lore.kernel.org/kernel-hardening/bfca96db-bbd0-d958-7732-76e36c667c68@suse.cz/
    [3] https://lore.kernel.org/kernel-hardening/CAG48ez1a4waGk9kB0WLaSbs4muSoK0AYAVk8=XYaKj4_+6e6Hg@mail.gmail.com/

    Signed-off-by: Vlastimil Babka
    Signed-off-by: Andrew Morton
    Acked-by: Christian Borntraeger
    Acked-by: Jiri Slaby
    Cc: Jann Horn
    Cc: Christoph Hellwig
    Cc: Christopher Lameter
    Cc: Julian Wiedmann
    Cc: Ursula Braun
    Cc: Alexander Viro
    Cc: David Windsor
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Andy Lutomirski
    Cc: "David S. Miller"
    Cc: Laura Abbott
    Cc: Mark Rutland
    Cc: "Martin K. Petersen"
    Cc: Paolo Bonzini
    Cc: Christoffer Dall
    Cc: Dave Kleikamp
    Cc: Jan Kara
    Cc: Luis de Bethencourt
    Cc: Marc Zyngier
    Cc: Rik van Riel
    Cc: Matthew Garrett
    Cc: Michal Kubecek
    Link: http://lkml.kernel.org/r/7d810f6d-8085-ea2f-7805-47ba3842dc50@suse.cz
    Signed-off-by: Linus Torvalds

    Vlastimil Babka
     

11 Apr, 2020

1 commit


08 Apr, 2020

1 commit

  • Now that "struct proc_ops" exist we can start putting there stuff which
    could not fly with VFS "struct file_operations"...

    Most of fs/proc/inode.c file is dedicated to make open/read/.../close
    reliable in the event of disappearing /proc entries which usually happens
    if module is getting removed. Files like /proc/cpuinfo which never
    disappear simply do not need such protection.

    Save 2 atomic ops, 1 allocation, 1 free per open/read/close sequence for such
    "permanent" files.

    Enable "permanent" flag for

    /proc/cpuinfo
    /proc/kmsg
    /proc/modules
    /proc/slabinfo
    /proc/stat
    /proc/sysvipc/*
    /proc/swaps

    More will come once I figure out foolproof way to prevent out module
    authors from marking their stuff "permanent" for performance reasons
    when it is not.

    This should help with scalability: benchmark is "read /proc/cpuinfo R times
    by N threads scattered over the system".

    N R t, s (before) t, s (after)
    -----------------------------------------------------
    64 4096 1.582458 1.530502 -3.2%
    256 4096 6.371926 6.125168 -3.9%
    1024 4096 25.64888 24.47528 -4.6%

    Benchmark source:

    #include
    #include
    #include
    #include

    #include
    #include
    #include
    #include

    const int NR_CPUS = sysconf(_SC_NPROCESSORS_ONLN);
    int N;
    const char *filename;
    int R;

    int xxx = 0;

    int glue(int n)
    {
    cpu_set_t m;
    CPU_ZERO(&m);
    CPU_SET(n, &m);
    return sched_setaffinity(0, sizeof(cpu_set_t), &m);
    }

    void f(int n)
    {
    glue(n % NR_CPUS);

    while (*(volatile int *)&xxx == 0) {
    }

    for (int i = 0; i < R; i++) {
    int fd = open(filename, O_RDONLY);
    char buf[4096];
    ssize_t rv = read(fd, buf, sizeof(buf));
    asm volatile ("" :: "g" (rv));
    close(fd);
    }
    }

    int main(int argc, char *argv[])
    {
    if (argc < 4) {
    std::cerr << "usage: " << argv[0] << ' ' << "N /proc/filename R
    ";
    return 1;
    }

    N = atoi(argv[1]);
    filename = argv[2];
    R = atoi(argv[3]);

    for (int i = 0; i < NR_CPUS; i++) {
    if (glue(i) == 0)
    break;
    }

    std::vector T;
    T.reserve(N);
    for (int i = 0; i < N; i++) {
    T.emplace_back(f, i);
    }

    auto t0 = std::chrono::system_clock::now();
    {
    *(volatile int *)&xxx = 1;
    for (auto& t: T) {
    t.join();
    }
    }
    auto t1 = std::chrono::system_clock::now();
    std::chrono::duration dt = t1 - t0;
    std::cout << dt.count() << '
    ';

    return 0;
    }

    P.S.:
    Explicit randomization marker is added because adding non-function pointer
    will silently disable structure layout randomization.

    [akpm@linux-foundation.org: coding style fixes]
    Reported-by: kbuild test robot
    Reported-by: Dan Carpenter
    Signed-off-by: Alexey Dobriyan
    Signed-off-by: Andrew Morton
    Cc: Al Viro
    Cc: Joe Perches
    Link: http://lkml.kernel.org/r/20200222201539.GA22576@avx2
    Signed-off-by: Linus Torvalds

    Alexey Dobriyan
     

03 Apr, 2020

1 commit

  • When I manually set default n to MEMCG_KMEM in init/Kconfig, bellow error
    occurs,

    mm/slab_common.c: In function 'memcg_slab_start':
    mm/slab_common.c:1530:30: error: 'struct mem_cgroup' has no member named
    'kmem_caches'
    return seq_list_start(&memcg->kmem_caches, *pos);
    ^
    mm/slab_common.c: In function 'memcg_slab_next':
    mm/slab_common.c:1537:32: error: 'struct mem_cgroup' has no member named
    'kmem_caches'
    return seq_list_next(p, &memcg->kmem_caches, pos);
    ^
    mm/slab_common.c: In function 'memcg_slab_show':
    mm/slab_common.c:1551:16: error: 'struct mem_cgroup' has no member named
    'kmem_caches'
    if (p == memcg->kmem_caches.next)
    ^
    CC arch/x86/xen/smp.o
    mm/slab_common.c: In function 'memcg_slab_start':
    mm/slab_common.c:1531:1: warning: control reaches end of non-void function
    [-Wreturn-type]
    }
    ^
    mm/slab_common.c: In function 'memcg_slab_next':
    mm/slab_common.c:1538:1: warning: control reaches end of non-void function
    [-Wreturn-type]
    }
    ^

    That's because kmem_caches is defined only when CONFIG_MEMCG_KMEM is set,
    while memcg_slab_start() will use it no matter CONFIG_MEMCG_KMEM is defined
    or not.

    By the way, the reason I mannuly undefined CONFIG_MEMCG_KMEM is to verify
    whether my some other code change is still stable when CONFIG_MEMCG_KMEM is
    not set. Unfortunately, the existing code has been already unstable since
    v4.11.

    Fixes: bc2791f857e1 ("slab: link memcg kmem_caches on their associated memory cgroup")
    Signed-off-by: Yafang Shao
    Signed-off-by: Andrew Morton
    Acked-by: Andrew Morton
    Cc: Tejun Heo
    Cc: Vladimir Davydov
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Link: http://lkml.kernel.org/r/1580970260-2045-1-git-send-email-laoar.shao@gmail.com
    Signed-off-by: Linus Torvalds

    Yafang Shao
     

04 Feb, 2020

2 commits

  • The most notable change is DEFINE_SHOW_ATTRIBUTE macro split in
    seq_file.h.

    Conversion rule is:

    llseek => proc_lseek
    unlocked_ioctl => proc_ioctl

    xxx => proc_xxx

    delete ".owner = THIS_MODULE" line

    [akpm@linux-foundation.org: fix drivers/isdn/capi/kcapi_proc.c]
    [sfr@canb.auug.org.au: fix kernel/sched/psi.c]
    Link: http://lkml.kernel.org/r/20200122180545.36222f50@canb.auug.org.au
    Link: http://lkml.kernel.org/r/20191225172546.GB13378@avx2
    Signed-off-by: Alexey Dobriyan
    Signed-off-by: Stephen Rothwell
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Alexey Dobriyan
     
  • Since 5.5-rc1 the last user of this function is gone, so remove the
    functionality.

    See commit
    2ad9d7747c10 ("netfilter: conntrack: free extension area immediately")
    for details.

    Link: http://lkml.kernel.org/r/20191212223442.22141-1-fw@strlen.de
    Signed-off-by: Florian Westphal
    Acked-by: Andrew Morton
    Acked-by: David Rientjes
    Reviewed-by: David Hildenbrand
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: Joonsoo Kim
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Florian Westphal
     

14 Jan, 2020

1 commit

  • When booting with amd_iommu=off, the following WARNING message
    appears:

    AMD-Vi: AMD IOMMU disabled on kernel command-line
    ------------[ cut here ]------------
    WARNING: CPU: 0 PID: 0 at kernel/workqueue.c:2772 flush_workqueue+0x42e/0x450
    Modules linked in:
    CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.5.0-rc3-amd-iommu #6
    Hardware name: Lenovo ThinkSystem SR655-2S/7D2WRCZ000, BIOS D8E101L-1.00 12/05/2019
    RIP: 0010:flush_workqueue+0x42e/0x450
    Code: ff 0f 0b e9 7a fd ff ff 4d 89 ef e9 33 fe ff ff 0f 0b e9 7f fd ff ff 0f 0b e9 bc fd ff ff 0f 0b e9 a8 fd ff ff e8 52 2c fe ff 0b 31 d2 48 c7 c6 e0 88 c5 95 48 c7 c7 d8 ad f0 95 e8 19 f5 04
    Call Trace:
    kmem_cache_destroy+0x69/0x260
    iommu_go_to_state+0x40c/0x5ab
    amd_iommu_prepare+0x16/0x2a
    irq_remapping_prepare+0x36/0x5f
    enable_IR_x2apic+0x21/0x172
    default_setup_apic_routing+0x12/0x6f
    apic_intr_mode_init+0x1a1/0x1f1
    x86_late_time_init+0x17/0x1c
    start_kernel+0x480/0x53f
    secondary_startup_64+0xb6/0xc0
    ---[ end trace 30894107c3749449 ]---
    x2apic: IRQ remapping doesn't support X2APIC mode
    x2apic disabled

    The warning is caused by the calling of 'kmem_cache_destroy()'
    in free_iommu_resources(). Here is the call path:

    free_iommu_resources
    kmem_cache_destroy
    flush_memcg_workqueue
    flush_workqueue

    The root cause is that the IOMMU subsystem runs before the workqueue
    subsystem, which the variable 'wq_online' is still 'false'. This leads
    to the statement 'if (WARN_ON(!wq_online))' in flush_workqueue() is
    'true'.

    Since the variable 'memcg_kmem_cache_wq' is not allocated during the
    time, it is unnecessary to call flush_memcg_workqueue(). This prevents
    the WARNING message triggered by flush_workqueue().

    Link: http://lkml.kernel.org/r/20200103085503.1665-1-ahuang12@lenovo.com
    Fixes: 92ee383f6daab ("mm: fix race between kmem_cache destroy, create and deactivate")
    Signed-off-by: Adrian Huang
    Reported-by: Xiaochun Lee
    Reviewed-by: Shakeel Butt
    Cc: Joerg Roedel
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Michal Hocko
    Cc: Johannes Weiner
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Adrian Huang
     

05 Dec, 2019

1 commit

  • Christian reported a warning like the following obtained during running
    some KVM-related tests on s390:

    WARNING: CPU: 8 PID: 208 at lib/percpu-refcount.c:108 percpu_ref_exit+0x50/0x58
    Modules linked in: kvm(-) xt_CHECKSUM xt_MASQUERADE bonding xt_tcpudp ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 ipt_REJECT nf_reject_ipv4 xt_conntrack ip6table_na>
    CPU: 8 PID: 208 Comm: kworker/8:1 Not tainted 5.2.0+ #66
    Hardware name: IBM 2964 NC9 712 (LPAR)
    Workqueue: events sysfs_slab_remove_workfn
    Krnl PSW : 0704e00180000000 0000001529746850 (percpu_ref_exit+0x50/0x58)
    R:0 T:1 IO:1 EX:1 Key:0 M:1 W:0 P:0 AS:3 CC:2 PM:0 RI:0 EA:3
    Krnl GPRS: 00000000ffff8808 0000001529746740 000003f4e30e8e18 0036008100000000
    0000001f00000000 0035008100000000 0000001fb3573ab8 0000000000000000
    0000001fbdb6de00 0000000000000000 0000001529f01328 0000001fb3573b00
    0000001fbb27e000 0000001fbdb69300 000003e009263d00 000003e009263cd0
    Krnl Code: 0000001529746842: f0a0000407fe srp 4(11,%r0),2046,0
    0000001529746848: 47000700 bc 0,1792
    #000000152974684c: a7f40001 brc 15,152974684e
    >0000001529746850: a7f4fff2 brc 15,1529746834
    0000001529746854: 0707 bcr 0,%r7
    0000001529746856: 0707 bcr 0,%r7
    0000001529746858: eb8ff0580024 stmg %r8,%r15,88(%r15)
    000000152974685e: a738ffff lhi %r3,-1
    Call Trace:
    ([] 0x3e009263d00)
    [] slab_kmem_cache_release+0x3a/0x70
    [] kobject_put+0xaa/0xe8
    [] process_one_work+0x1e8/0x428
    [] worker_thread+0x48/0x460
    [] kthread+0x126/0x160
    [] ret_from_fork+0x28/0x30
    [] kernel_thread_starter+0x0/0x10
    Last Breaking-Event-Address:
    [] percpu_ref_exit+0x4c/0x58
    ---[ end trace b035e7da5788eb09 ]---

    The problem occurs because kmem_cache_destroy() is called immediately
    after deleting of a memcg, so it races with the memcg kmem_cache
    deactivation.

    flush_memcg_workqueue() at the beginning of kmem_cache_destroy() is
    supposed to guarantee that all deactivation processes are finished, but
    failed to do so. It waits for an rcu grace period, after which all
    children kmem_caches should be deactivated. During the deactivation
    percpu_ref_kill() is called for non root kmem_cache refcounters, but it
    requires yet another rcu grace period to finish the transition to the
    atomic (dead) state.

    So in a rare case when not all children kmem_caches are destroyed at the
    moment when the root kmem_cache is about to be gone, we need to wait
    another rcu grace period before destroying the root kmem_cache.

    This issue can be triggered only with dynamically created kmem_caches
    which are used with memcg accounting. In this case per-memcg child
    kmem_caches are created. They are deactivated from the cgroup removing
    path. If the destruction of the root kmem_cache is racing with the
    removal of the cgroup (both are quite complicated multi-stage
    processes), the described issue can occur. The only known way to
    trigger it in the real life, is to unload some kernel module which
    creates a dedicated kmem_cache, used from different memory cgroups with
    GFP_ACCOUNT flag. If the unloading happens immediately after calling
    rmdir on the corresponding cgroup, there is some chance to trigger the
    issue.

    Link: http://lkml.kernel.org/r/20191129025011.3076017-1-guro@fb.com
    Fixes: f0a3a24b532d ("mm: memcg/slab: rework non-root kmem_cache lifecycle management")
    Signed-off-by: Roman Gushchin
    Reported-by: Christian Borntraeger
    Tested-by: Christian Borntraeger
    Reviewed-by: Shakeel Butt
    Acked-by: Michal Hocko
    Cc: Johannes Weiner
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     

01 Dec, 2019

3 commits

  • The type of local variable *type* of new_kmalloc_cache() should be enum
    kmalloc_cache_type instead of int, so correct it.

    Link: http://lkml.kernel.org/r/1569241648-26908-4-git-send-email-lpf.vector@gmail.com
    Signed-off-by: Pengfei Li
    Acked-by: Vlastimil Babka
    Acked-by: Roman Gushchin
    Acked-by: David Rientjes
    Cc: Christoph Lameter
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Pengfei Li
     
  • The size of kmalloc can be obtained from kmalloc_info[], so remove
    kmalloc_size() that will not be used anymore.

    Link: http://lkml.kernel.org/r/1569241648-26908-3-git-send-email-lpf.vector@gmail.com
    Signed-off-by: Pengfei Li
    Acked-by: Vlastimil Babka
    Acked-by: Roman Gushchin
    Acked-by: David Rientjes
    Cc: Christoph Lameter
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Pengfei Li
     
  • Patch series "mm, slab: Make kmalloc_info[] contain all types of names", v6.

    There are three types of kmalloc, KMALLOC_NORMAL, KMALLOC_RECLAIM
    and KMALLOC_DMA.

    The name of KMALLOC_NORMAL is contained in kmalloc_info[].name,
    but the names of KMALLOC_RECLAIM and KMALLOC_DMA are dynamically
    generated by kmalloc_cache_name().

    Patch1 predefines the names of all types of kmalloc to save
    the time spent dynamically generating names.

    These changes make sense, and the time spent by new_kmalloc_cache()
    has been reduced by approximately 36.3%.

    Time spent by new_kmalloc_cache()
    (CPU cycles)
    5.3-rc7 66264
    5.3-rc7+patch 42188

    This patch (of 3):

    There are three types of kmalloc, KMALLOC_NORMAL, KMALLOC_RECLAIM and
    KMALLOC_DMA.

    The name of KMALLOC_NORMAL is contained in kmalloc_info[].name, but the
    names of KMALLOC_RECLAIM and KMALLOC_DMA are dynamically generated by
    kmalloc_cache_name().

    This patch predefines the names of all types of kmalloc to save the time
    spent dynamically generating names.

    Besides, remove the kmalloc_cache_name() that is no longer used.

    Link: http://lkml.kernel.org/r/1569241648-26908-2-git-send-email-lpf.vector@gmail.com
    Signed-off-by: Pengfei Li
    Acked-by: Vlastimil Babka
    Acked-by: Roman Gushchin
    Acked-by: David Rientjes
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: Joonsoo Kim
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Pengfei Li
     

19 Oct, 2019

1 commit

  • Karsten reported the following panic in __free_slab() happening on a s390x
    machine:

    Unable to handle kernel pointer dereference in virtual kernel address space
    Failing address: 0000000000000000 TEID: 0000000000000483
    Fault in home space mode while using kernel ASCE.
    AS:00000000017d4007 R3:000000007fbd0007 S:000000007fbff000 P:000000000000003d
    Oops: 0004 ilc:3 Ý#1¨ PREEMPT SMP
    Modules linked in: tcp_diag inet_diag xt_tcpudp ip6t_rpfilter ip6t_REJECT nf_reject_ipv6 ipt_REJECT nf_reject_ipv4 xt_conntrack ip6table_nat ip6table_mangle ip6table_raw ip6table_security iptable_at nf_nat
    CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.3.0-05872-g6133e3e4bada-dirty #14
    Hardware name: IBM 2964 NC9 702 (z/VM 6.4.0)
    Krnl PSW : 0704d00180000000 00000000003cadb6 (__free_slab+0x686/0x6b0)
    R:0 T:1 IO:1 EX:1 Key:0 M:1 W:0 P:0 AS:3 CC:1 PM:0 RI:0 EA:3
    Krnl GPRS: 00000000f3a32928 0000000000000000 000000007fbf5d00 000000000117c4b8
    0000000000000000 000000009e3291c1 0000000000000000 0000000000000000
    0000000000000003 0000000000000008 000000002b478b00 000003d080a97600
    0000000000000003 0000000000000008 000000002b478b00 000003d080a97600
    000000000117ba00 000003e000057db0 00000000003cabcc 000003e000057c78
    Krnl Code: 00000000003cada6: e310a1400004 lg %r1,320(%r10)
    00000000003cadac: c0e50046c286 brasl %r14,ca32b8
    #00000000003cadb2: a7f4fe36 brc 15,3caa1e
    >00000000003cadb6: e32060800024 stg %r2,128(%r6)
    00000000003cadbc: a7f4fd9e brc 15,3ca8f8
    00000000003cadc0: c0e50046790c brasl %r14,c99fd8
    00000000003cadc6: a7f4fe2c brc 15,3caa
    00000000003cadc6: a7f4fe2c brc 15,3caa1e
    00000000003cadca: ecb1ffff00d9 aghik %r11,%r1,-1
    Call Trace:
    ( __free_slab+0x49c/0x6b0)
    rcu_core+0x5a6/0x7e0
    __do_softirq+0xf2/0x5c0
    irq_exit+0x104/0x130
    do_IRQ+0x9a/0xf0
    ext_int_handler+0x130/0x134
    enabled_wait+0x58/0x128
    ( enabled_wait+0x44/0x128)
    arch_cpu_idle+0x40/0x58
    default_idle_call+0x3c/0x68
    do_idle+0xec/0x1c0
    cpu_startup_entry+0x36/0x40
    arch_call_rest_init+0x5c/0x88
    0x0
    INFO: lockdep is turned off.
    Last Breaking-Event-Address:
    __free_slab+0x1c4/0x6b0
    Kernel panic - not syncing: Fatal exception in interrupt

    The kernel panics on an attempt to dereference the NULL memcg pointer.
    When shutdown_cache() is called from the kmem_cache_destroy() context, a
    memcg kmem_cache might have empty slab pages in a partial list, which are
    still charged to the memory cgroup.

    These pages are released by free_partial() at the beginning of
    shutdown_cache(): either directly or by scheduling a RCU-delayed work
    (if the kmem_cache has the SLAB_TYPESAFE_BY_RCU flag). The latter case
    is when the reported panic can happen: memcg_unlink_cache() is called
    immediately after shrinking partial lists, without waiting for scheduled
    RCU works. It sets the kmem_cache->memcg_params.memcg pointer to NULL,
    and the following attempt to dereference it by __free_slab() from the
    RCU work context causes the panic.

    To fix the issue, let's postpone the release of the memcg pointer to
    destroy_memcg_params(). It's called from a separate work context by
    slab_caches_to_rcu_destroy_workfn(), which contains a full RCU barrier.
    This guarantees that all scheduled page release RCU works will complete
    before the memcg pointer will be zeroed.

    Big thanks for Karsten for the perfect report containing all necessary
    information, his help with the analysis of the problem and testing of the
    fix.

    Link: http://lkml.kernel.org/r/20191010160549.1584316-1-guro@fb.com
    Fixes: fb2f2b0adb98 ("mm: memcg/slab: reparent memcg kmem_caches on cgroup removal")
    Signed-off-by: Roman Gushchin
    Reported-by: Karsten Graul
    Tested-by: Karsten Graul
    Acked-by: Vlastimil Babka
    Reviewed-by: Shakeel Butt
    Cc: Karsten Graul
    Cc: Vladimir Davydov
    Cc: David Rientjes
    Cc:
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     

08 Oct, 2019

2 commits

  • In most configurations, kmalloc() happens to return naturally aligned
    (i.e. aligned to the block size itself) blocks for power of two sizes.

    That means some kmalloc() users might unknowingly rely on that
    alignment, until stuff breaks when the kernel is built with e.g.
    CONFIG_SLUB_DEBUG or CONFIG_SLOB, and blocks stop being aligned. Then
    developers have to devise workaround such as own kmem caches with
    specified alignment [1], which is not always practical, as recently
    evidenced in [2].

    The topic has been discussed at LSF/MM 2019 [3]. Adding a
    'kmalloc_aligned()' variant would not help with code unknowingly relying
    on the implicit alignment. For slab implementations it would either
    require creating more kmalloc caches, or allocate a larger size and only
    give back part of it. That would be wasteful, especially with a generic
    alignment parameter (in contrast with a fixed alignment to size).

    Ideally we should provide to mm users what they need without difficult
    workarounds or own reimplementations, so let's make the kmalloc()
    alignment to size explicitly guaranteed for power-of-two sizes under all
    configurations. What this means for the three available allocators?

    * SLAB object layout happens to be mostly unchanged by the patch. The
    implicitly provided alignment could be compromised with
    CONFIG_DEBUG_SLAB due to redzoning, however SLAB disables redzoning for
    caches with alignment larger than unsigned long long. Practically on at
    least x86 this includes kmalloc caches as they use cache line alignment,
    which is larger than that. Still, this patch ensures alignment on all
    arches and cache sizes.

    * SLUB layout is also unchanged unless redzoning is enabled through
    CONFIG_SLUB_DEBUG and boot parameter for the particular kmalloc cache.
    With this patch, explicit alignment is guaranteed with redzoning as
    well. This will result in more memory being wasted, but that should be
    acceptable in a debugging scenario.

    * SLOB has no implicit alignment so this patch adds it explicitly for
    kmalloc(). The potential downside is increased fragmentation. While
    pathological allocation scenarios are certainly possible, in my testing,
    after booting a x86_64 kernel+userspace with virtme, around 16MB memory
    was consumed by slab pages both before and after the patch, with
    difference in the noise.

    [1] https://lore.kernel.org/linux-btrfs/c3157c8e8e0e7588312b40c853f65c02fe6c957a.1566399731.git.christophe.leroy@c-s.fr/
    [2] https://lore.kernel.org/linux-fsdevel/20190225040904.5557-1-ming.lei@redhat.com/
    [3] https://lwn.net/Articles/787740/

    [akpm@linux-foundation.org: documentation fixlet, per Matthew]
    Link: http://lkml.kernel.org/r/20190826111627.7505-3-vbabka@suse.cz
    Signed-off-by: Vlastimil Babka
    Reviewed-by: Matthew Wilcox (Oracle)
    Acked-by: Michal Hocko
    Acked-by: Kirill A. Shutemov
    Acked-by: Christoph Hellwig
    Cc: David Sterba
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Ming Lei
    Cc: Dave Chinner
    Cc: "Darrick J . Wong"
    Cc: Christoph Hellwig
    Cc: James Bottomley
    Cc: Vlastimil Babka
    Cc: Joonsoo Kim
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Vlastimil Babka
     
  • Patch series "guarantee natural alignment for kmalloc()", v2.

    This patch (of 2):

    SLOB currently doesn't account its pages at all, so in /proc/meminfo the
    Slab field shows zero. Modifying a counter on page allocation and
    freeing should be acceptable even for the small system scenarios SLOB is
    intended for. Since reclaimable caches are not separated in SLOB,
    account everything as unreclaimable.

    SLUB currently doesn't account kmalloc() and kmalloc_node() allocations
    larger than order-1 page, that are passed directly to the page
    allocator. As they also don't appear in /proc/slabinfo, it might look
    like a memory leak. For consistency, account them as well. (SLAB
    doesn't actually use page allocator directly, so no change there).

    Ideally SLOB and SLUB would be handled in separate patches, but due to
    the shared kmalloc_order() function and different kfree()
    implementations, it's easier to patch both at once to prevent
    inconsistencies.

    Link: http://lkml.kernel.org/r/20190826111627.7505-2-vbabka@suse.cz
    Signed-off-by: Vlastimil Babka
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Ming Lei
    Cc: Dave Chinner
    Cc: Matthew Wilcox
    Cc: "Darrick J . Wong"
    Cc: Christoph Hellwig
    Cc: James Bottomley
    Cc: Vlastimil Babka
    Cc: Joonsoo Kim
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Vlastimil Babka
     

25 Sep, 2019

1 commit

  • Currently, a value of '1" is written to /sys/kernel/slab//shrink
    file to shrink the slab by flushing out all the per-cpu slabs and free
    slabs in partial lists. This can be useful to squeeze out a bit more
    memory under extreme condition as well as making the active object counts
    in /proc/slabinfo more accurate.

    This usually applies only to the root caches, as the SLUB_MEMCG_SYSFS_ON
    option is usually not enabled and "slub_memcg_sysfs=1" not set. Even if
    memcg sysfs is turned on, it is too cumbersome and impractical to manage
    all those per-memcg sysfs files in a real production system.

    So there is no practical way to shrink memcg caches. Fix this by enabling
    a proper write to the shrink sysfs file of the root cache to scan all the
    available memcg caches and shrink them as well. For a non-root memcg
    cache (when SLUB_MEMCG_SYSFS_ON or slub_memcg_sysfs is on), only that
    cache will be shrunk when written.

    On a 2-socket 64-core 256-thread arm64 system with 64k page after
    a parallel kernel build, the the amount of memory occupied by slabs
    before shrinking slabs were:

    # grep task_struct /proc/slabinfo
    task_struct 53137 53192 4288 61 4 : tunables 0 0
    0 : slabdata 872 872 0
    # grep "^S[lRU]" /proc/meminfo
    Slab: 3936832 kB
    SReclaimable: 399104 kB
    SUnreclaim: 3537728 kB

    After shrinking slabs (by echoing "1" to all shrink files):

    # grep "^S[lRU]" /proc/meminfo
    Slab: 1356288 kB
    SReclaimable: 263296 kB
    SUnreclaim: 1092992 kB
    # grep task_struct /proc/slabinfo
    task_struct 2764 6832 4288 61 4 : tunables 0 0
    0 : slabdata 112 112 0

    Link: http://lkml.kernel.org/r/20190723151445.7385-1-longman@redhat.com
    Signed-off-by: Waiman Long
    Acked-by: Roman Gushchin
    Acked-by: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Michal Hocko
    Cc: Johannes Weiner
    Cc: Shakeel Butt
    Cc: Vladimir Davydov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Waiman Long
     

17 Jul, 2019

1 commit

  • Clang gets rather confused about two variables in the same special
    section when one of them is not initialized, leading to an assembler
    warning later:

    /tmp/slab_common-18f869.s: Assembler messages:
    /tmp/slab_common-18f869.s:7526: Warning: ignoring changed section attributes for .data..ro_after_init

    Adding an initialization to kmalloc_caches is rather silly here
    but does avoid the issue.

    Link: https://bugs.llvm.org/show_bug.cgi?id=42570
    Link: http://lkml.kernel.org/r/20190712090455.266021-1-arnd@arndb.de
    Signed-off-by: Arnd Bergmann
    Acked-by: David Rientjes
    Reviewed-by: Andrew Morton
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: Joonsoo Kim
    Cc: Stephen Rothwell
    Cc: Roman Gushchin
    Cc: Shakeel Butt
    Cc: Vladimir Davydov
    Cc: Andrey Konovalov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Arnd Bergmann
     

13 Jul, 2019

10 commits

  • There are concerns about memory leaks from extensive use of memory cgroups
    as each memory cgroup creates its own set of kmem caches. There is a
    possiblity that the memcg kmem caches may remain even after the memory
    cgroups have been offlined. Therefore, it will be useful to show the
    status of each of memcg kmem caches.

    This patch introduces a new /memcg_slabinfo file which is
    somewhat similar to /proc/slabinfo in format, but lists only information
    about kmem caches that have child memcg kmem caches. Information
    available in /proc/slabinfo are not repeated in memcg_slabinfo.

    A portion of a sample output of the file was:

    #
    rpc_inode_cache root 13 51 1 1
    rpc_inode_cache 48 0 0 0 0
    fat_inode_cache root 1 45 1 1
    fat_inode_cache 41 2 45 1 1
    xfs_inode root 770 816 24 24
    xfs_inode 92 22 34 1 1
    xfs_inode 88:dead 1 34 1 1
    xfs_inode 89:dead 23 34 1 1
    xfs_inode 85 4 34 1 1
    xfs_inode 84 9 34 1 1

    The css id of the memcg is also listed. If a memcg is not online,
    the tag ":dead" will be attached as shown above.

    [longman@redhat.com: memcg: add ":deact" tag for reparented kmem caches in memcg_slabinfo]
    Link: http://lkml.kernel.org/r/20190621173005.31514-1-longman@redhat.com
    [longman@redhat.com: set the flag in the common code as suggested by Roman]
    Link: http://lkml.kernel.org/r/20190627184324.5875-1-longman@redhat.com
    Link: http://lkml.kernel.org/r/20190619171621.26209-1-longman@redhat.com
    Signed-off-by: Waiman Long
    Suggested-by: Shakeel Butt
    Reviewed-by: Shakeel Butt
    Acked-by: Roman Gushchin
    Acked-by: David Rientjes
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: Joonsoo Kim
    Cc: Michal Hocko
    Cc: Johannes Weiner
    Cc: Vladimir Davydov
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Waiman Long
     
  • Let's reparent non-root kmem_caches on memcg offlining. This allows us to
    release the memory cgroup without waiting for the last outstanding kernel
    object (e.g. dentry used by another application).

    Since the parent cgroup is already charged, everything we need to do is to
    splice the list of kmem_caches to the parent's kmem_caches list, swap the
    memcg pointer, drop the css refcounter for each kmem_cache and adjust the
    parent's css refcounter.

    Please, note that kmem_cache->memcg_params.memcg isn't a stable pointer
    anymore. It's safe to read it under rcu_read_lock(), cgroup_mutex held,
    or any other way that protects the memory cgroup from being released.

    We can race with the slab allocation and deallocation paths. It's not a
    big problem: parent's charge and slab global stats are always correct, and
    we don't care anymore about the child usage and global stats. The child
    cgroup is already offline, so we don't use or show it anywhere.

    Local slab stats (NR_SLAB_RECLAIMABLE and NR_SLAB_UNRECLAIMABLE) aren't
    used anywhere except count_shadow_nodes(). But even there it won't break
    anything: after reparenting "nodes" will be 0 on child level (because
    we're already reparenting shrinker lists), and on parent level page stats
    always were 0, and this patch won't change anything.

    [guro@fb.com: properly handle kmem_caches reparented to root_mem_cgroup]
    Link: http://lkml.kernel.org/r/20190620213427.1691847-1-guro@fb.com
    Link: http://lkml.kernel.org/r/20190611231813.3148843-11-guro@fb.com
    Signed-off-by: Roman Gushchin
    Acked-by: Vladimir Davydov
    Reviewed-by: Shakeel Butt
    Acked-by: David Rientjes
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Waiman Long
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Cc: Andrei Vagin
    Cc: Qian Cai
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Currently each charged slab page holds a reference to the cgroup to which
    it's charged. Kmem_caches are held by the memcg and are released all
    together with the memory cgroup. It means that none of kmem_caches are
    released unless at least one reference to the memcg exists, which is very
    far from optimal.

    Let's rework it in a way that allows releasing individual kmem_caches as
    soon as the cgroup is offline, the kmem_cache is empty and there are no
    pending allocations.

    To make it possible, let's introduce a new percpu refcounter for non-root
    kmem caches. The counter is initialized to the percpu mode, and is
    switched to the atomic mode during kmem_cache deactivation. The counter
    is bumped for every charged page and also for every running allocation.
    So the kmem_cache can't be released unless all allocations complete.

    To shutdown non-active empty kmem_caches, let's reuse the work queue,
    previously used for the kmem_cache deactivation. Once the reference
    counter reaches 0, let's schedule an asynchronous kmem_cache release.

    * I used the following simple approach to test the performance
    (stolen from another patchset by T. Harding):

    time find / -name fname-no-exist
    echo 2 > /proc/sys/vm/drop_caches
    repeat 10 times

    Results:

    orig patched

    real 0m1.455s real 0m1.355s
    user 0m0.206s user 0m0.219s
    sys 0m0.855s sys 0m0.807s

    real 0m1.487s real 0m1.699s
    user 0m0.221s user 0m0.256s
    sys 0m0.806s sys 0m0.948s

    real 0m1.515s real 0m1.505s
    user 0m0.183s user 0m0.215s
    sys 0m0.876s sys 0m0.858s

    real 0m1.291s real 0m1.380s
    user 0m0.193s user 0m0.198s
    sys 0m0.843s sys 0m0.786s

    real 0m1.364s real 0m1.374s
    user 0m0.180s user 0m0.182s
    sys 0m0.868s sys 0m0.806s

    real 0m1.352s real 0m1.312s
    user 0m0.201s user 0m0.212s
    sys 0m0.820s sys 0m0.761s

    real 0m1.302s real 0m1.349s
    user 0m0.205s user 0m0.203s
    sys 0m0.803s sys 0m0.792s

    real 0m1.334s real 0m1.301s
    user 0m0.194s user 0m0.201s
    sys 0m0.806s sys 0m0.779s

    real 0m1.426s real 0m1.434s
    user 0m0.216s user 0m0.181s
    sys 0m0.824s sys 0m0.864s

    real 0m1.350s real 0m1.295s
    user 0m0.200s user 0m0.190s
    sys 0m0.842s sys 0m0.811s

    So it looks like the difference is not noticeable in this test.

    [cai@lca.pw: fix an use-after-free in kmemcg_workfn()]
    Link: http://lkml.kernel.org/r/1560977573-10715-1-git-send-email-cai@lca.pw
    Link: http://lkml.kernel.org/r/20190611231813.3148843-9-guro@fb.com
    Signed-off-by: Roman Gushchin
    Signed-off-by: Qian Cai
    Acked-by: Vladimir Davydov
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Shakeel Butt
    Cc: Waiman Long
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Cc: Andrei Vagin
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Currently the memcg_params.dying flag and the corresponding workqueue used
    for the asynchronous deactivation of kmem_caches is synchronized using the
    slab_mutex.

    It makes impossible to check this flag from the irq context, which will be
    required in order to implement asynchronous release of kmem_caches.

    So let's switch over to the irq-save flavor of the spinlock-based
    synchronization.

    Link: http://lkml.kernel.org/r/20190611231813.3148843-8-guro@fb.com
    Signed-off-by: Roman Gushchin
    Acked-by: Vladimir Davydov
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Waiman Long
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Cc: Andrei Vagin
    Cc: Qian Cai
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • There is no point in checking the root_cache->memcg_params.dying flag on
    kmem_cache creation path. New allocations shouldn't be performed using a
    dead root kmem_cache, so no new memcg kmem_cache creation can be scheduled
    after the flag is set. And if it was scheduled before,
    flush_memcg_workqueue() will wait for it anyway.

    So let's drop this check to simplify the code.

    Link: http://lkml.kernel.org/r/20190611231813.3148843-7-guro@fb.com
    Signed-off-by: Roman Gushchin
    Acked-by: Vladimir Davydov
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Waiman Long
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Cc: Andrei Vagin
    Cc: Qian Cai
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Currently SLUB uses a work scheduled after an RCU grace period to
    deactivate a non-root kmem_cache. This mechanism can be reused for
    kmem_caches release, but requires generalization for SLAB case.

    Introduce kmemcg_cache_deactivate() function, which calls
    allocator-specific __kmem_cache_deactivate() and schedules execution of
    __kmem_cache_deactivate_after_rcu() with all necessary locks in a worker
    context after an rcu grace period.

    Here is the new calling scheme:
    kmemcg_cache_deactivate()
    __kmemcg_cache_deactivate() SLAB/SLUB-specific
    kmemcg_rcufn() rcu
    kmemcg_workfn() work
    __kmemcg_cache_deactivate_after_rcu() SLAB/SLUB-specific

    instead of:
    __kmemcg_cache_deactivate() SLAB/SLUB-specific
    slab_deactivate_memcg_cache_rcu_sched() SLUB-only
    kmemcg_rcufn() rcu
    kmemcg_workfn() work
    kmemcg_cache_deact_after_rcu() SLUB-only

    For consistency, all allocator-specific functions start with "__".

    Link: http://lkml.kernel.org/r/20190611231813.3148843-4-guro@fb.com
    Signed-off-by: Roman Gushchin
    Acked-by: Vladimir Davydov
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Waiman Long
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Cc: Andrei Vagin
    Cc: Qian Cai
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • The delayed work/rcu deactivation infrastructure of non-root kmem_caches
    can be also used for asynchronous release of these objects. Let's get rid
    of the word "deactivation" in corresponding names to make the code look
    better after generalization.

    It's easier to make the renaming first, so that the generalized code will
    look consistent from scratch.

    Let's rename struct memcg_cache_params fields:
    deact_fn -> work_fn
    deact_rcu_head -> rcu_head
    deact_work -> work

    And RCU/delayed work callbacks in slab common code:
    kmemcg_deactivate_rcufn -> kmemcg_rcufn
    kmemcg_deactivate_workfn -> kmemcg_workfn

    This patch contains no functional changes, only renamings.

    Link: http://lkml.kernel.org/r/20190611231813.3148843-3-guro@fb.com
    Signed-off-by: Roman Gushchin
    Acked-by: Vladimir Davydov
    Reviewed-by: Shakeel Butt
    Cc: Christoph Lameter
    Cc: Johannes Weiner
    Cc: Michal Hocko
    Cc: Waiman Long
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Pekka Enberg
    Cc: Andrei Vagin
    Cc: Qian Cai
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • Patch series "mm: reparent slab memory on cgroup removal", v7.

    # Why do we need this?

    We've noticed that the number of dying cgroups is steadily growing on most
    of our hosts in production. The following investigation revealed an issue
    in the userspace memory reclaim code [1], accounting of kernel stacks [2],
    and also the main reason: slab objects.

    The underlying problem is quite simple: any page charged to a cgroup holds
    a reference to it, so the cgroup can't be reclaimed unless all charged
    pages are gone. If a slab object is actively used by other cgroups, it
    won't be reclaimed, and will prevent the origin cgroup from being
    reclaimed.

    Slab objects, and first of all vfs cache, is shared between cgroups, which
    are using the same underlying fs, and what's even more important, it's
    shared between multiple generations of the same workload. So if something
    is running periodically every time in a new cgroup (like how systemd
    works), we do accumulate multiple dying cgroups.

    Strictly speaking pagecache isn't different here, but there is a key
    difference: we disable protection and apply some extra pressure on LRUs of
    dying cgroups, and these LRUs contain all charged pages. My experiments
    show that with the disabled kernel memory accounting the number of dying
    cgroups stabilizes at a relatively small number (~100, depends on memory
    pressure and cgroup creation rate), and with kernel memory accounting it
    grows pretty steadily up to several thousands.

    Memory cgroups are quite complex and big objects (mostly due to percpu
    stats), so it leads to noticeable memory losses. Memory occupied by dying
    cgroups is measured in hundreds of megabytes. I've even seen a host with
    more than 100Gb of memory wasted for dying cgroups. It leads to a
    degradation of performance with the uptime, and generally limits the usage
    of cgroups.

    My previous attempt [3] to fix the problem by applying extra pressure on
    slab shrinker lists caused a regressions with xfs and ext4, and has been
    reverted [4]. The following attempts to find the right balance [5, 6]
    were not successful.

    So instead of trying to find a maybe non-existing balance, let's do
    reparent accounted slab caches to the parent cgroup on cgroup removal.

    # Implementation approach

    There is however a significant problem with reparenting of slab memory:
    there is no list of charged pages. Some of them are in shrinker lists,
    but not all. Introducing of a new list is really not an option.

    But fortunately there is a way forward: every slab page has a stable
    pointer to the corresponding kmem_cache. So the idea is to reparent
    kmem_caches instead of slab pages.

    It's actually simpler and cheaper, but requires some underlying changes:
    1) Make kmem_caches to hold a single reference to the memory cgroup,
    instead of a separate reference per every slab page.
    2) Stop setting page->mem_cgroup pointer for memcg slab pages and use
    page->kmem_cache->memcg indirection instead. It's used only on
    slab page release, so performance overhead shouldn't be a big issue.
    3) Introduce a refcounter for non-root slab caches. It's required to
    be able to destroy kmem_caches when they become empty and release
    the associated memory cgroup.

    There is a bonus: currently we release all memcg kmem_caches all together
    with the memory cgroup itself. This patchset allows individual
    kmem_caches to be released as soon as they become inactive and free.

    Some additional implementation details are provided in corresponding
    commit messages.

    # Results

    Below is the average number of dying cgroups on two groups of our
    production hosts. They do run some sort of web frontend workload, the
    memory pressure is moderate. As we can see, with the kernel memory
    reparenting the number stabilizes in 60s range; however with the original
    version it grows almost linearly and doesn't show any signs of plateauing.
    The difference in slab and percpu usage between patched and unpatched
    versions also grows linearly. In 7 days it exceeded 200Mb.

    day 0 1 2 3 4 5 6 7
    original 56 362 628 752 1070 1250 1490 1560
    patched 23 46 51 55 60 57 67 69
    mem diff(Mb) 22 74 123 152 164 182 214 241

    # Links

    [1]: commit 68600f623d69 ("mm: don't miss the last page because of round-off error")
    [2]: commit 9b6f7e163cd0 ("mm: rework memcg kernel stack accounting")
    [3]: commit 172b06c32b94 ("mm: slowly shrink slabs with a relatively small number of objects")
    [4]: commit a9a238e83fbb ("Revert "mm: slowly shrink slabs with a relatively small number of objects")
    [5]: https://lkml.org/lkml/2019/1/28/1865
    [6]: https://marc.info/?l=linux-mm&m=155064763626437&w=2

    This patch (of 10):

    Initialize kmem_cache->memcg_params.memcg pointer in memcg_link_cache()
    rather than in init_memcg_params().

    Once kmem_cache will hold a reference to the memory cgroup, it will
    simplify the refcounting.

    For non-root kmem_caches memcg_link_cache() is always called before the
    kmem_cache becomes visible to a user, so it's safe.

    Link: http://lkml.kernel.org/r/20190611231813.3148843-2-guro@fb.com
    Signed-off-by: Roman Gushchin
    Reviewed-by: Shakeel Butt
    Acked-by: Vladimir Davydov
    Acked-by: Johannes Weiner
    Cc: Waiman Long
    Cc: Michal Hocko
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Andrei Vagin
    Cc: Qian Cai
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Roman Gushchin
     
  • ksize() has been unconditionally unpoisoning the whole shadow memory
    region associated with an allocation. This can lead to various undetected
    bugs, for example, double-kzfree().

    Specifically, kzfree() uses ksize() to determine the actual allocation
    size, and subsequently zeroes the memory. Since ksize() used to just
    unpoison the whole shadow memory region, no invalid free was detected.

    This patch addresses this as follows:

    1. Add a check in ksize(), and only then unpoison the memory region.

    2. Preserve kasan_unpoison_slab() semantics by explicitly unpoisoning
    the shadow memory region using the size obtained from __ksize().

    Tested:
    1. With SLAB allocator: a) normal boot without warnings; b) verified the
    added double-kzfree() is detected.
    2. With SLUB allocator: a) normal boot without warnings; b) verified the
    added double-kzfree() is detected.

    [elver@google.com: s/BUG_ON/WARN_ON_ONCE/, per Kees]
    Link: http://lkml.kernel.org/r/20190627094445.216365-6-elver@google.com
    Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=199359
    Link: http://lkml.kernel.org/r/20190626142014.141844-6-elver@google.com
    Signed-off-by: Marco Elver
    Acked-by: Kees Cook
    Reviewed-by: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Andrey Konovalov
    Cc: Christoph Lameter
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Mark Rutland
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Marco Elver
     
  • This refactors common code of ksize() between the various allocators into
    slab_common.c: __ksize() is the allocator-specific implementation without
    instrumentation, whereas ksize() includes the required KASAN logic.

    Link: http://lkml.kernel.org/r/20190626142014.141844-5-elver@google.com
    Signed-off-by: Marco Elver
    Acked-by: Christoph Lameter
    Reviewed-by: Andrey Ryabinin
    Cc: Dmitry Vyukov
    Cc: Alexander Potapenko
    Cc: Andrey Konovalov
    Cc: Pekka Enberg
    Cc: David Rientjes
    Cc: Joonsoo Kim
    Cc: Mark Rutland
    Cc: Kees Cook
    Signed-off-by: Andrew Morton
    Signed-off-by: Linus Torvalds

    Marco Elver