Skip to content

Commit 3aa4ed8

Browse files
MiaoheLinakpm00
authored andcommitted
mm/hugetlb: make detecting shared pte more reliable
If the pagetables are shared, we shouldn't copy or take references. Since src could have unshared and dst shares with another vma, huge_pte_none() is thus used to determine whether dst_pte is shared. But this check isn't reliable. A shared pte could have pte none in pagetable in fact. The page count of ptep page should be checked here in order to reliably determine whether pte is shared. [lukas.bulwahn@gmail.com: remove unused local variable dst_entry in copy_hugetlb_page_range()] Link: https://lkml.kernel.org/r/20220822082525.26071-1-lukas.bulwahn@gmail.com Link: https://lkml.kernel.org/r/20220816130553.31406-7-linmiaohe@huawei.com Signed-off-by: Miaohe Lin <linmiaohe@huawei.com> Signed-off-by: Lukas Bulwahn <lukas.bulwahn@gmail.com> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com> Cc: Muchun Song <songmuchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 01088a6 commit 3aa4ed8

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

mm/hugetlb.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4750,7 +4750,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
47504750
struct vm_area_struct *dst_vma,
47514751
struct vm_area_struct *src_vma)
47524752
{
4753-
pte_t *src_pte, *dst_pte, entry, dst_entry;
4753+
pte_t *src_pte, *dst_pte, entry;
47544754
struct page *ptepage;
47554755
unsigned long addr;
47564756
bool cow = is_cow_mapping(src_vma->vm_flags);
@@ -4795,15 +4795,13 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
47954795

47964796
/*
47974797
* If the pagetables are shared don't copy or take references.
4798-
* dst_pte == src_pte is the common case of src/dest sharing.
47994798
*
4799+
* dst_pte == src_pte is the common case of src/dest sharing.
48004800
* However, src could have 'unshared' and dst shares with
4801-
* another vma. If dst_pte !none, this implies sharing.
4802-
* Check here before taking page table lock, and once again
4803-
* after taking the lock below.
4801+
* another vma. So page_count of ptep page is checked instead
4802+
* to reliably determine whether pte is shared.
48044803
*/
4805-
dst_entry = huge_ptep_get(dst_pte);
4806-
if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) {
4804+
if (page_count(virt_to_page(dst_pte)) > 1) {
48074805
addr |= last_addr_mask;
48084806
continue;
48094807
}
@@ -4812,13 +4810,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
48124810
src_ptl = huge_pte_lockptr(h, src, src_pte);
48134811
spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
48144812
entry = huge_ptep_get(src_pte);
4815-
dst_entry = huge_ptep_get(dst_pte);
48164813
again:
4817-
if (huge_pte_none(entry) || !huge_pte_none(dst_entry)) {
4814+
if (huge_pte_none(entry)) {
48184815
/*
4819-
* Skip if src entry none. Also, skip in the
4820-
* unlikely case dst entry !none as this implies
4821-
* sharing with another vma.
4816+
* Skip if src entry none.
48224817
*/
48234818
;
48244819
} else if (unlikely(is_hugetlb_entry_hwpoisoned(entry))) {
@@ -4897,7 +4892,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
48974892
restore_reserve_on_error(h, dst_vma, addr,
48984893
new);
48994894
put_page(new);
4900-
/* dst_entry won't change as in child */
4895+
/* huge_ptep of dst_pte won't change as in child */
49014896
goto again;
49024897
}
49034898
hugetlb_install_page(dst_vma, dst_pte, addr, new);

0 commit comments

Comments
 (0)