[PATCH v2 03/13] mm: Provide generic pmd_thp_or_huge()

From: peterx
Date: Wed Jan 03 2024 - 04:16:25 EST


From: Peter Xu <peterx@xxxxxxxxxx>

ARM defines pmd_thp_or_huge(), detecting either a THP or a huge PMD. It
can be a helpful helper if we want to merge more THP and hugetlb code
paths. Make it a generic default implementation, only exist when
CONFIG_MMU. Arch can overwrite it by defining its own version.

For example, ARM's pgtable-2level.h defines it to always return false.

Keep the macro declared with all config, it should be optimized to a false
anyway if !THP && !HUGETLB.

Signed-off-by: Peter Xu <peterx@xxxxxxxxxx>
---
include/linux/pgtable.h | 4 ++++
mm/gup.c | 3 +--
2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 466cf477551a..2b42e95a4e3a 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -1362,6 +1362,10 @@ static inline int pmd_write(pmd_t pmd)
#endif /* pmd_write */
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */

+#ifndef pmd_thp_or_huge
+#define pmd_thp_or_huge(pmd) (pmd_huge(pmd) || pmd_trans_huge(pmd))
+#endif
+
#ifndef pud_write
static inline int pud_write(pud_t pud)
{
diff --git a/mm/gup.c b/mm/gup.c
index df83182ec72d..eebae70d2465 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -3004,8 +3004,7 @@ static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned lo
if (!pmd_present(pmd))
return 0;

- if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd) ||
- pmd_devmap(pmd))) {
+ if (unlikely(pmd_thp_or_huge(pmd) || pmd_devmap(pmd))) {
/* See gup_pte_range() */
if (pmd_protnone(pmd))
return 0;
--
2.41.0