• Lorenzo Stoakes's avatar
    mm: abstract the vma_merge()/split_vma() pattern for mprotect() et al. · 94d7d923
    Lorenzo Stoakes authored
    mprotect() and other functions which change VMA parameters over a range
    each employ a pattern of:-
    
    1. Attempt to merge the range with adjacent VMAs.
    2. If this fails, and the range spans a subset of the VMA, split it
       accordingly.
    
    This is open-coded and duplicated in each case. Also in each case most of
    the parameters passed to vma_merge() remain the same.
    
    Create a new function, vma_modify(), which abstracts this operation,
    accepting only those parameters which can be changed.
    
    To avoid the mess of invoking each function call with unnecessary
    parameters, create inline wrapper functions for each of the modify
    operations, parameterised only by what is required to perform the action.
    
    We can also significantly simplify the logic - by returning the VMA if we
    split (or merged VMA if we do not) we no longer need specific handling for
    merge/split cases in any of the call sites.
    
    Note that the userfaultfd_release() case works even though it does not
    split VMAs - since start is set to vma->vm_start and end is set to
    vma->vm_end, the split logic does not trigger.
    
    In addition, since we calculate pgoff to be equal to vma->vm_pgoff + (start
    - vma->vm_start) >> PAGE_SHIFT, and start - vma->vm_start will be 0 in this
    instance, this invocation will remain unchanged.
    
    We eliminate a VM_WARN_ON() in mprotect_fixup() as this simply asserts that
    vma_merge() correctly ensures that flags remain the same, something that is
    already checked in is_mergeable_vma() and elsewhere, and in any case is not
    specific to mprotect().
    
    Link: https://lkml.kernel.org/r/0dfa9368f37199a423674bf0ee312e8ea0619044.1697043508.git.lstoakes@gmail.comSigned-off-by: default avatarLorenzo Stoakes <lstoakes@gmail.com>
    Reviewed-by: default avatarVlastimil Babka <vbabka@suse.cz>
    Cc: Alexander Viro <viro@zeniv.linux.org.uk>
    Cc: Christian Brauner <brauner@kernel.org>
    Cc: Liam R. Howlett <Liam.Howlett@oracle.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    94d7d923
userfaultfd.c 58.2 KB