Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/mremap: catch invalid multi VMA moves earlier

Previously, any attempt to solely move a VMA would require that the
span specified reside within the span of that single VMA, with no gaps
before or afterwards.

After commit d23cb648e365 ("mm/mremap: permit mremap() move of multiple
VMAs"), the multi VMA move permitted a gap to exist only after VMAs.
This was done to provide maximum flexibility.

However, We have consequently permitted this behaviour for the move of
a single VMA including those not eligible for multi VMA move.

The change introduced here means that we no longer permit non-eligible
VMAs from being moved in this way.

This is consistent, as it means all eligible VMA moves are treated the
same, and all non-eligible moves are treated as they were before.

This change does not break previous behaviour, which equally would have
disallowed such a move (only in all cases).

[lorenzo.stoakes@oracle.com: do not incorrectly reference invalid VMA in VM_WARN_ON_ONCE()]
Link: https://lkml.kernel.org/r/b6dbda20-667e-4053-abae-8ed4fa84bb6c@lucifer.local
Link: https://lkml.kernel.org/r/2b5aad5681573be85b5b8fac61399af6fb6b68b6.1754218667.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: David Hildenbrand <david@redhat.com>
Cc: Jann Horn <jannh@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
d5f416c7 7c91e0b9

+13 -8
+13 -8
mm/mremap.c
··· 1820 1820 unsigned long start = vrm->addr; 1821 1821 unsigned long end = vrm->addr + vrm->old_len; 1822 1822 unsigned long new_addr = vrm->new_addr; 1823 - bool allowed = true, seen_vma = false; 1824 1823 unsigned long target_addr = new_addr; 1825 1824 unsigned long res = -EFAULT; 1826 1825 unsigned long last_end; 1826 + bool seen_vma = false; 1827 + 1827 1828 VMA_ITERATOR(vmi, current->mm, start); 1828 1829 1829 1830 /* ··· 1837 1836 unsigned long addr = max(vma->vm_start, start); 1838 1837 unsigned long len = min(end, vma->vm_end) - addr; 1839 1838 unsigned long offset, res_vma; 1840 - 1841 - if (!allowed) 1842 - return -EFAULT; 1839 + bool multi_allowed; 1843 1840 1844 1841 /* No gap permitted at the start of the range. */ 1845 1842 if (!seen_vma && start < vma->vm_start) ··· 1866 1867 vrm->new_addr = target_addr + offset; 1867 1868 vrm->old_len = vrm->new_len = len; 1868 1869 1869 - allowed = vma_multi_allowed(vma); 1870 - if (seen_vma && !allowed) 1871 - return -EFAULT; 1870 + multi_allowed = vma_multi_allowed(vma); 1871 + if (!multi_allowed) { 1872 + /* This is not the first VMA, abort immediately. */ 1873 + if (seen_vma) 1874 + return -EFAULT; 1875 + /* This is the first, but there are more, abort. */ 1876 + if (vma->vm_end < end) 1877 + return -EFAULT; 1878 + } 1872 1879 1873 1880 res_vma = check_prep_vma(vrm); 1874 1881 if (!res_vma) ··· 1883 1878 return res_vma; 1884 1879 1885 1880 if (!seen_vma) { 1886 - VM_WARN_ON_ONCE(allowed && res_vma != new_addr); 1881 + VM_WARN_ON_ONCE(multi_allowed && res_vma != new_addr); 1887 1882 res = res_vma; 1888 1883 } 1889 1884