pmdp_invalidate() sets _SEGMENT_ENTRY_INVALID, but that bit alone does not make the entry pmd_protnone(). Also clear the _SEGMENT_ENTRY_READ bit to achieve it, so that GUP-fast can no longer grab a reference on the folio mapped by the invalidated pmd. Code can invalidate a PMD and then based on folio refcount decides whether it may drop the mapping. That relies on GUP-fast backing off from an invalidated PMD, as it does on x86, arm64, and powerpc. For example, lazyfree THP discard is about to use pmdp_invalidate() to prevent GUP-fast before dropping a lazyfree folio. With CONFIG_PAGE_TABLE_CHECK, pmdp_invalidate() already clears _SEGMENT_ENTRY_READ, which makes the entry pmd_protnone(), so GUP-fast does not follow it. Do that unconditionally. As noted in commit 7b4dde5e40ad6 ("s390/pgtable: Add s390 support for page table check"), an invalidated PMD that is pmd_protnone() is fine for the remaining pmd_protnone() users, which run under the page table lock or are lockless walkers that should not follow it anyway. To: Alexander Gordeev To: Gerald Schaefer To: Heiko Carstens To: Vasily Gorbik Cc: Christian Borntraeger Cc: Sven Schnelle Cc: Jason Gunthorpe Cc: John Hubbard Cc: Peter Xu Cc: linux-s390@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Gerald Schaefer Signed-off-by: Zi Yan --- arch/s390/include/asm/pgtable.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/s390/include/asm/pgtable.h b/arch/s390/include/asm/pgtable.h index 2d5c2ab06de98..fe1ba746203ab 100644 --- a/arch/s390/include/asm/pgtable.h +++ b/arch/s390/include/asm/pgtable.h @@ -1781,10 +1781,14 @@ static inline pmd_t pmdp_invalidate(struct vm_area_struct *vma, pmd_t pmd = *pmdp; VM_WARN_ON_ONCE(!pmd_present(pmd)); + /* + * _SEGMENT_ENTRY_INVALID alone also marks an old (not young) entry, + * which lockless walkers like GUP-fast may follow. Clear + * _SEGMENT_ENTRY_READ too, so the invalidated entry is pmd_protnone() + * and is not followed, and page table check does not count it. + */ pmd = set_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_INVALID)); -#ifdef CONFIG_PAGE_TABLE_CHECK pmd = clear_pmd_bit(pmd, __pgprot(_SEGMENT_ENTRY_READ)); -#endif page_table_check_pmd_set(vma->vm_mm, addr, pmdp, pmd); pmd = pmdp_xchg_direct(vma->vm_mm, addr, pmdp, pmd); return pmd; -- 2.53.0