[PATCH] arm64/mm: Change THP helpers to comply with generic MM semantics

From: Anshuman Khandual
Date: Tue Mar 31 2020 - 06:38:53 EST


Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
---
arch/arm64/include/asm/pgtable.h | 74 ++++++++++++++++++++++++++++----
arch/arm64/mm/hugetlbpage.c | 2 +-
2 files changed, 67 insertions(+), 9 deletions(-)

diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index 44883038dbe6..86c22a4fa427 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -348,15 +348,72 @@ static inline int pmd_protnone(pmd_t pmd)
}
#endif

+#define pmd_table(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
+ PMD_TYPE_TABLE)
+#define pmd_sect(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
+ PMD_TYPE_SECT)
+static pmd_t pmd_mksplitting(pmd_t pmd)
+{
+ unsigned long mask = PMD_TYPE_MASK;
+ unsigned long val = pmd_val(pmd);
+
+ val = (val & ~mask) | PMD_TABLE_BIT;
+
+ return __pmd(val);
+}
+
+static bool pmd_splitting(pmd_t pmd)
+{
+ unsigned long mask = PMD_TYPE_MASK;
+ unsigned long val = pmd_val(pmd);
+
+ if ((val & mask) == PMD_TABLE_BIT)
+ return true;
+
+ return false;
+}
+
+static bool pmd_mapped(pmd_t pmd)
+{
+ return pmd_sect(pmd);
+}
+
+static inline int pmd_present(pmd_t pmd)
+{
+ pte_t pte = pmd_pte(pmd);
+
+ if (pte_present(pte))
+ return 1;
+
+ if (pmd_splitting(pmd))
+ return 1;
+
+ return 0;
+}
+
/*
* THP definitions.
*/

#ifdef CONFIG_TRANSPARENT_HUGEPAGE
-#define pmd_trans_huge(pmd) (pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
+static inline int pmd_trans_huge(pmd_t pmd)
+{
+ if (!pmd_present(pmd))
+ return 0;
+
+ if (!pmd_val(pmd))
+ return 0;
+
+ if (pmd_mapped(pmd))
+ return 1;
+
+ if (pmd_splitting(pmd))
+ return 1;
+
+ return 0;
+}
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

-#define pmd_present(pmd) pte_present(pmd_pte(pmd))
#define pmd_dirty(pmd) pte_dirty(pmd_pte(pmd))
#define pmd_young(pmd) pte_young(pmd_pte(pmd))
#define pmd_valid(pmd) pte_valid(pmd_pte(pmd))
@@ -366,7 +423,12 @@ static inline int pmd_protnone(pmd_t pmd)
#define pmd_mkclean(pmd) pte_pmd(pte_mkclean(pmd_pte(pmd)))
#define pmd_mkdirty(pmd) pte_pmd(pte_mkdirty(pmd_pte(pmd)))
#define pmd_mkyoung(pmd) pte_pmd(pte_mkyoung(pmd_pte(pmd)))
-#define pmd_mknotvalid(pmd) (__pmd(pmd_val(pmd) & ~PMD_SECT_VALID))
+
+static inline pmd_t pmd_mknotvalid(pmd_t pmd)
+{
+ BUG_ON(pmd_table(pmd));
+ return pmd_mksplitting(pmd);
+}

#define pmd_thp_or_huge(pmd) (pmd_huge(pmd) || pmd_trans_huge(pmd))

@@ -437,10 +499,6 @@ extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,

#define pmd_bad(pmd) (!(pmd_val(pmd) & PMD_TABLE_BIT))

-#define pmd_table(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
- PMD_TYPE_TABLE)
-#define pmd_sect(pmd) ((pmd_val(pmd) & PMD_TYPE_MASK) == \
- PMD_TYPE_SECT)
#define pmd_leaf(pmd) pmd_sect(pmd)

#if defined(CONFIG_ARM64_64K_PAGES) || CONFIG_PGTABLE_LEVELS < 3
@@ -834,7 +892,7 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,

#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
#define __pmd_to_swp_entry(pmd) ((swp_entry_t) { pmd_val(pmd) })
-#define __swp_entry_to_pmd(swp) __pmd((swp).val)
+#define __swp_entry_to_pmd(swp) __pmd((swp).val & ~PMD_TABLE_BIT)
#endif

/*
diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
index bbeb6a5a6ba6..056fe716b9f8 100644
--- a/arch/arm64/mm/hugetlbpage.c
+++ b/arch/arm64/mm/hugetlbpage.c
@@ -283,7 +283,7 @@ pte_t *huge_pte_offset(struct mm_struct *mm,
if (!(sz == PMD_SIZE || sz == CONT_PMD_SIZE) &&
pmd_none(pmd))
return NULL;
- if (pmd_huge(pmd) || !pmd_present(pmd))
+ if (pmd_huge(pmd) || !pte_present(pmd_pte(pmd)))
return (pte_t *)pmdp;

if (sz == CONT_PTE_SIZE)
--
2.20.1