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 To: Huacai Chen Cc: WANG Xuerui Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu Cc: loongarch@lists.linux.dev Cc: linux-kernel@vger.kernel.org --- 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