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: put VMA check and prep logic into helper function

Rather than lumping everything together in do_mremap(), add a new helper
function, check_prep_vma(), to do the work relating to each VMA.

This further lays groundwork for subsequent patches which will allow for
batched VMA mremap().

Additionally, if we set vrm->new_addr == vrm->addr when prepping the VMA,
this avoids us needing to do so in the expand VMA mlocked case.

No functional change intended.

Link: https://lkml.kernel.org/r/15efa3c57935f7f8894094b94c1803c2f322c511.1752770784.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Al Viro <viro@zeniv.linux.org.uk>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Peter Xu <peterx@redhat.com>
Cc: Rik van Riel <riel@surriel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
f256a7a4 3215eace

+28 -30
+28 -30
mm/mremap.c
··· 1634 1634 static unsigned long expand_vma(struct vma_remap_struct *vrm) 1635 1635 { 1636 1636 unsigned long err; 1637 - unsigned long addr = vrm->addr; 1638 1637 1639 1638 err = remap_is_valid(vrm); 1640 1639 if (err) ··· 1648 1649 if (err) 1649 1650 return err; 1650 1651 1651 - /* 1652 - * We want to populate the newly expanded portion of the VMA to 1653 - * satisfy the expectation that mlock()'ing a VMA maintains all 1654 - * of its pages in memory. 1655 - */ 1656 - if (vrm->mlocked) 1657 - vrm->new_addr = addr; 1658 - 1659 1652 /* OK we're done! */ 1660 - return addr; 1653 + return vrm->addr; 1661 1654 } 1662 1655 1663 1656 /* ··· 1705 1714 return -EINVAL; 1706 1715 } 1707 1716 1717 + static int check_prep_vma(struct vma_remap_struct *vrm) 1718 + { 1719 + struct vm_area_struct *vma = vrm->vma; 1720 + 1721 + if (!vma) 1722 + return -EFAULT; 1723 + 1724 + /* If mseal()'d, mremap() is prohibited. */ 1725 + if (!can_modify_vma(vma)) 1726 + return -EPERM; 1727 + 1728 + /* Align to hugetlb page size, if required. */ 1729 + if (is_vm_hugetlb_page(vma) && !align_hugetlb(vrm)) 1730 + return -EINVAL; 1731 + 1732 + vrm_set_delta(vrm); 1733 + vrm->remap_type = vrm_remap_type(vrm); 1734 + /* For convenience, we set new_addr even if VMA won't move. */ 1735 + if (!vrm_implies_new_addr(vrm)) 1736 + vrm->new_addr = vrm->addr; 1737 + 1738 + return 0; 1739 + } 1740 + 1708 1741 static unsigned long do_mremap(struct vma_remap_struct *vrm) 1709 1742 { 1710 1743 struct mm_struct *mm = current->mm; 1711 - struct vm_area_struct *vma; 1712 1744 unsigned long res; 1713 1745 1714 1746 vrm->old_len = PAGE_ALIGN(vrm->old_len); ··· 1745 1731 return -EINTR; 1746 1732 vrm->mmap_locked = true; 1747 1733 1748 - vma = vrm->vma = vma_lookup(mm, vrm->addr); 1749 - if (!vma) { 1750 - res = -EFAULT; 1734 + vrm->vma = vma_lookup(current->mm, vrm->addr); 1735 + res = check_prep_vma(vrm); 1736 + if (res) 1751 1737 goto out; 1752 - } 1753 - 1754 - /* If mseal()'d, mremap() is prohibited. */ 1755 - if (!can_modify_vma(vma)) { 1756 - res = -EPERM; 1757 - goto out; 1758 - } 1759 - 1760 - /* Align to hugetlb page size, if required. */ 1761 - if (is_vm_hugetlb_page(vma) && !align_hugetlb(vrm)) { 1762 - res = -EINVAL; 1763 - goto out; 1764 - } 1765 - 1766 - vrm_set_delta(vrm); 1767 - vrm->remap_type = vrm_remap_type(vrm); 1768 1738 1769 1739 /* Actually execute mremap. */ 1770 1740 res = vrm_implies_new_addr(vrm) ? mremap_to(vrm) : mremap_at(vrm);