[PATCH] x86/mm: Make pud_present() check _PAGE_PROTNONE and _PAGE_PSE as well

From: Anshuman Khandual
Date: Wed Mar 18 2020 - 01:03:13 EST


pud_present() should also check _PAGE_PROTNONE and _PAGE_PSE bits like in
case pmd_present(). This makes a PUD entry test positive for pud_present()
after getting invalidated with pud_mknotpresent(), hence standardizing the
semantics with PMD helpers.

Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxxxx>
Cc: "H. Peter Anvin" <hpa@xxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: x86@xxxxxxxxxx
Cc: linux-mm@xxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
---
Even though pud_mknotpresent() is not used any where currently, there is
a discrepancy between PMD and PUD.

WARN_ON(!pud_present(pud_mknotpresent(pud_mkhuge(pud)))) -> Fail
WARN_ON(!pmd_present(pmd_mknotpresent(pmd_mkhuge(pmd)))) -> Pass

Though pud_mknotpresent() currently clears _PAGE_PROTNONE, pud_present()
does not check it. This change fixes both inconsistencies.

This has been build and boot tested on x86.

arch/x86/include/asm/pgtable.h | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h
index 7e118660bbd9..8adf1d00b506 100644
--- a/arch/x86/include/asm/pgtable.h
+++ b/arch/x86/include/asm/pgtable.h
@@ -857,7 +857,13 @@ static inline int pud_none(pud_t pud)

static inline int pud_present(pud_t pud)
{
- return pud_flags(pud) & _PAGE_PRESENT;
+ /*
+ * Checking for _PAGE_PSE is needed too because
+ * split_huge_page will temporarily clear the present bit (but
+ * the _PAGE_PSE flag will remain set at all times while the
+ * _PAGE_PRESENT bit is clear).
+ */
+ return pud_flags(pud) & (_PAGE_PRESENT | _PAGE_PROTNONE | _PAGE_PSE);
}

static inline unsigned long pud_page_vaddr(pud_t pud)
--
2.17.1