On Mon, Sep 11, 2023 at 08:34:57AM -0700, Dave Hansen wrote: > On 9/11/23 06:26, Matthew Wilcox wrote: > > @@ -231,7 +235,10 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr, > > if (--nr == 0) > > break; > > ptep++; > > - pte = __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); > > + if (__pte_needs_invert(pte_val(pte))) > > + pte = __pte(pte_val(pte) - (1UL << PFN_PTE_SHIFT)); > > + else > > + pte = __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); > > } > > arch_leave_lazy_mmu_mode(); > > } > > This is much better than a whole x86 fork of set_ptes(). But it's still > a bit wonky because it exposes the PTE inversion logic to generic code. I saw that as an advantage ... let people know that it exists as a concept. > static inline void set_ptes(struct mm_struct *mm, unsigned long addr, > pte_t *ptep, pte_t pte, unsigned int nr) > { > pgprot_t prot = pte_pgprot(x); > unsigned long pfn = pte_pfn(pte); > > page_table_check_ptes_set(mm, ptep, pte, nr); > > arch_enter_lazy_mmu_mode(); > for (;;) { > set_pte(ptep, pte); > if (--nr == 0) > break; > ptep++; > pfn++; > pte = pfn_pte(pfn, pgprot); > } > arch_leave_lazy_mmu_mode(); > } > > Obviously completely untested. :) After fixing your two typos, this assembles to 176 bytes more code than my version. Not sure that's great. How about this? Keeps the inverted knowledge entirely in arch/x86. Compiles to exactly the same code as the version I sent earlier. diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index d6ad98ca1288..c9781b8b14af 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -955,6 +955,14 @@ static inline int pte_same(pte_t a, pte_t b) return a.pte == b.pte; } +static inline pte_t pte_next(pte_t pte) +{ + if (__pte_needs_invert(pte_val(pte))) + return __pte(pte_val(pte) - (1UL << PFN_PTE_SHIFT)); + return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); +} +#define pte_next pte_next + static inline int pte_present(pte_t a) { return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE); diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 1fba072b3dac..7a932ed59c27 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -205,6 +205,10 @@ static inline int pmd_young(pmd_t pmd) #define arch_flush_lazy_mmu_mode() do {} while (0) #endif +#ifndef pte_next +#define pte_next(pte) ((pte) + (1UL << PFN_PTE_SHIFT)) +#endif + #ifndef set_ptes /** * set_ptes - Map consecutive pages to a contiguous range of addresses. @@ -231,7 +235,7 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr, if (--nr == 0) break; ptep++; - pte = __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); + pte = pte_next(pte); } arch_leave_lazy_mmu_mode(); }