• Thomas Gleixner's avatar
    mm/vmalloc: prevent stale TLBs in fully utilized blocks · fc1e0d98
    Thomas Gleixner authored
    Patch series "mm/vmalloc: Assorted fixes and improvements", v2.
    
    this series addresses the following issues:
    
      1) Prevent the stale TLB problem related to fully utilized vmap blocks
    
      2) Avoid the double per CPU list walk in _vm_unmap_aliases()
    
      3) Avoid flushing dirty space over and over
    
      4) Add a lockless quickcheck in vb_alloc() and add missing
         READ/WRITE_ONCE() annotations
    
      5) Prevent overeager purging of usable vmap_blocks if
         not under memory/address space pressure.
    
    
    This patch (of 6):
    
    _vm_unmap_aliases() is used to ensure that no unflushed TLB entries for a
    page are left in the system. This is required due to the lazy TLB flush
    mechanism in vmalloc.
    
    This is tried to achieve by walking the per CPU free lists, but those do
    not contain fully utilized vmap blocks because they are removed from the
    free list once the blocks free space became zero.
    
    When the block is not fully unmapped then it is not on the purge list
    either.
    
    So neither the per CPU list iteration nor the purge list walk find the
    block and if the page was mapped via such a block and the TLB has not yet
    been flushed, the guarantee of _vm_unmap_aliases() that there are no stale
    TLBs after returning is broken:
    
    x = vb_alloc() // Removes vmap_block from free list because vb->free became 0
    vb_free(x)     // Unmaps page and marks in dirty_min/max range
    	       // Block has still mappings and is not put on purge list
    
    // Page is reused
    vm_unmap_aliases() // Can't find vmap block with the dirty space -> FAIL
    
    So instead of walking the per CPU free lists, walk the per CPU xarrays
    which hold pointers to _all_ active blocks in the system including those
    removed from the free lists.
    
    Link: https://lkml.kernel.org/r/20230525122342.109672430@linutronix.de
    Link: https://lkml.kernel.org/r/20230525124504.573987880@linutronix.de
    Fixes: db64fe02 ("mm: rewrite vmap layer")
    Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
    Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
    Reviewed-by: default avatarLorenzo Stoakes <lstoakes@gmail.com>
    Reviewed-by: default avatarUladzislau Rezki (Sony) <urezki@gmail.com>
    Reviewed-by: default avatarBaoquan He <bhe@redhat.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    fc1e0d98
vmalloc.c 114 KB