Skip to content

Commit 9b19df2

Browse files
punitagrawaltorvalds
authored andcommitted
mm/hugetlb.c: make huge_pte_offset() consistent and document behaviour
When walking the page tables to resolve an address that points to !p*d_present() entry, huge_pte_offset() returns inconsistent values depending on the level of page table (PUD or PMD). It returns NULL in the case of a PUD entry while in the case of a PMD entry, it returns a pointer to the page table entry. A similar inconsitency exists when handling swap entries - returns NULL for a PUD entry while a pointer to the pte_t is retured for the PMD entry. Update huge_pte_offset() to make the behaviour consistent - return a pointer to the pte_t for hugepage or swap entries. Only return NULL in instances where we have a p*d_none() entry and the size parameter doesn't match the hugepage size at this level of the page table. Document the behaviour to clarify the expected behaviour of this function. This is to set clear semantics for architecture specific implementations of huge_pte_offset(). Discussions on the arm64 implementation of huge_pte_offset() (http://www.spinics.net/lists/linux-mm/msg133699.html) showed that there is benefit from returning a pte_t* in the case of p*d_none(). The fault handling code in hugetlb_fault() can handle p*d_none() entries and saves an extra round trip to huge_pte_alloc(). Other callers of huge_pte_offset() should be ok as well. [[email protected]: v2] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Punit Agrawal <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Mike Kravetz <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Acked-by: Michal Hocko <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: Steve Capper <[email protected]> Cc: Will Deacon <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 09180ca commit 9b19df2

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

mm/hugetlb.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,6 +4600,15 @@ pte_t *huge_pte_alloc(struct mm_struct *mm,
46004600
return pte;
46014601
}
46024602

4603+
/*
4604+
* huge_pte_offset() - Walk the page table to resolve the hugepage
4605+
* entry at address @addr
4606+
*
4607+
* Return: Pointer to page table or swap entry (PUD or PMD) for
4608+
* address @addr, or NULL if a p*d_none() entry is encountered and the
4609+
* size @sz doesn't match the hugepage size at this level of the page
4610+
* table.
4611+
*/
46034612
pte_t *huge_pte_offset(struct mm_struct *mm,
46044613
unsigned long addr, unsigned long sz)
46054614
{
@@ -4614,13 +4623,22 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
46144623
p4d = p4d_offset(pgd, addr);
46154624
if (!p4d_present(*p4d))
46164625
return NULL;
4626+
46174627
pud = pud_offset(p4d, addr);
4618-
if (!pud_present(*pud))
4628+
if (sz != PUD_SIZE && pud_none(*pud))
46194629
return NULL;
4620-
if (pud_huge(*pud))
4630+
/* hugepage or swap? */
4631+
if (pud_huge(*pud) || !pud_present(*pud))
46214632
return (pte_t *)pud;
4633+
46224634
pmd = pmd_offset(pud, addr);
4623-
return (pte_t *) pmd;
4635+
if (sz != PMD_SIZE && pmd_none(*pmd))
4636+
return NULL;
4637+
/* hugepage or swap? */
4638+
if (pmd_huge(*pmd) || !pmd_present(*pmd))
4639+
return (pte_t *)pmd;
4640+
4641+
return NULL;
46244642
}
46254643

46264644
#endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */

0 commit comments

Comments
 (0)