[PATCH v2 4/9] LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs
From: Zi Yan
Date: Thu Dec 03 2026 - 23:18:41 EST
pmdp_invalidate() does not change a PMD to make pmd_present() returns
false. So the generic pmd_access_permitted() returns true for an
invalidated PMD. This allows GUP-fast to grab a reference on the folio.
Code that invalidates a PMD and then decides whether it may drop the
mapping relies on GUP-fast backing off from an invalidated PMD. x86, arm64
and powerpc already reject invalidated PMDs in pmd_access_permitted().
Provide a LoongArch pmd_access_permitted() that requires _PAGE_PRESENT,
so invalidated (and PROT_NONE) PMDs are not followed by GUP-fast.
Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
To: Huacai Chen <chenhuacai@xxxxxxxxxx>
Cc: WANG Xuerui <kernel@xxxxxxxxxx>
Cc: Jason Gunthorpe <jgg@xxxxxxxxxx>
Cc: John Hubbard <jhubbard@xxxxxxxxxx>
Cc: Peter Xu <peterx@xxxxxxxxxx>
Cc: loongarch@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
arch/loongarch/include/asm/pgtable.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h
index 9a62b87c246de..ac2178fad0952 100644
--- a/arch/loongarch/include/asm/pgtable.h
+++ b/arch/loongarch/include/asm/pgtable.h
@@ -522,6 +522,12 @@ static inline int pmd_write(pmd_t pmd)
return !!(pmd_val(pmd) & _PAGE_WRITE);
}
+#define pmd_access_permitted pmd_access_permitted
+static inline bool pmd_access_permitted(pmd_t pmd, bool write)
+{
+ return (pmd_val(pmd) & _PAGE_PRESENT) && (!write || pmd_write(pmd));
+}
+
static inline pmd_t pmd_mkwrite_novma(pmd_t pmd)
{
pmd_val(pmd) |= _PAGE_WRITE;
--
2.53.0