Skip to content

Commit bebc511

Browse files
Naoya Horiguchigregkh
authored andcommitted
mm/hugetlb: pmd_huge() returns true for non-present hugepage
commit cbef847 upstream. Migrating hugepages and hwpoisoned hugepages are considered as non-present hugepages, and they are referenced via migration entries and hwpoison entries in their page table slots. This behavior causes race condition because pmd_huge() doesn't tell non-huge pages from migrating/hwpoisoned hugepages. follow_page_mask() is one example where the kernel would call follow_page_pte() for such hugepage while this function is supposed to handle only normal pages. To avoid this, this patch makes pmd_huge() return true when pmd_none() is true *and* pmd_present() is false. We don't have to worry about mixing up non-present pmd entry with normal pmd (pointing to leaf level pte entry) because pmd_present() is true in normal pmd. The same race condition could happen in (x86-specific) gup_pmd_range(), where this patch simply adds pmd_present() check instead of pmd_huge(). This is because gup_pmd_range() is fast path. If we have non-present hugepage in this function, we will go into gup_huge_pmd(), then return 0 at flag mask check, and finally fall back to the slow path. Fixes: 290408d ("hugetlb: hugepage migration core") Signed-off-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Hugh Dickins <hughd@google.com> Cc: James Hogan <james.hogan@imgtec.com> Cc: David Rientjes <rientjes@google.com> Cc: Mel Gorman <mel@csn.ul.ie> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@suse.cz> Cc: Rik van Riel <riel@redhat.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Luiz Capitulino <lcapitulino@redhat.com> Cc: Nishanth Aravamudan <nacc@linux.vnet.ibm.com> Cc: Lee Schermerhorn <lee.schermerhorn@hp.com> Cc: Steve Capper <steve.capper@linaro.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent aa07e80 commit bebc511

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎arch/x86/mm/gup.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
172172
*/
173173
if (pmd_none(pmd) || pmd_trans_splitting(pmd))
174174
return 0;
175-
if (unlikely(pmd_large(pmd))) {
175+
if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
176176
/*
177177
* NUMA hinting faults need to be handled in the GUP
178178
* slowpath for accounting purposes and so that they

‎arch/x86/mm/hugetlbpage.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@ follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
6666
return ERR_PTR(-EINVAL);
6767
}
6868

69+
/*
70+
* pmd_huge() returns 1 if @pmd is hugetlb related entry, that is normal
71+
* hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
72+
* Otherwise, returns 0.
73+
*/
6974
int pmd_huge(pmd_t pmd)
7075
{
71-
return !!(pmd_val(pmd) & _PAGE_PSE);
76+
return !pmd_none(pmd) &&
77+
(pmd_val(pmd) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
7278
}
7379

7480
int pud_huge(pud_t pud)

‎mm/hugetlb.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,6 +3659,8 @@ follow_huge_pmd(struct mm_struct *mm, unsigned long address,
36593659
{
36603660
struct page *page;
36613661

3662+
if (!pmd_present(*pmd))
3663+
return NULL;
36623664
page = pte_page(*(pte_t *)pmd);
36633665
if (page)
36643666
page += ((address & ~PMD_MASK) >> PAGE_SHIFT);

0 commit comments

Comments
 (0)