Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: abstract moving to the next PFN

In order to fix the L1TF vulnerability, x86 can invert the PTE bits for
PROT_NONE VMAs, which means we cannot move from one PTE to the next by
adding 1 to the PFN field of the PTE. This results in the BUG reported at
[1].

Abstract advancing the PTE to the next PFN through a pte_next_pfn()
function/macro.

Link: https://lkml.kernel.org/r/20230920040958.866520-1-willy@infradead.org
Fixes: bcc6cc832573 ("mm: add default definition of set_ptes()")
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reported-by: syzbot+55cc72f8cc3a549119df@syzkaller.appspotmail.com
Closes: https://lkml.kernel.org/r/000000000000d099fa0604f03351@google.com [1]
Reviewed-by: Yin Fengwei <fengwei.yin@intel.com>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Matthew Wilcox (Oracle) and committed by
Andrew Morton
ce60f27b a501a070

+17 -1
+8
arch/x86/include/asm/pgtable.h
··· 955 955 return a.pte == b.pte; 956 956 } 957 957 958 + static inline pte_t pte_next_pfn(pte_t pte) 959 + { 960 + if (__pte_needs_invert(pte_val(pte))) 961 + return __pte(pte_val(pte) - (1UL << PFN_PTE_SHIFT)); 962 + return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); 963 + } 964 + #define pte_next_pfn pte_next_pfn 965 + 958 966 static inline int pte_present(pte_t a) 959 967 { 960 968 return pte_flags(a) & (_PAGE_PRESENT | _PAGE_PROTNONE);
+9 -1
include/linux/pgtable.h
··· 206 206 #endif 207 207 208 208 #ifndef set_ptes 209 + 210 + #ifndef pte_next_pfn 211 + static inline pte_t pte_next_pfn(pte_t pte) 212 + { 213 + return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); 214 + } 215 + #endif 216 + 209 217 /** 210 218 * set_ptes - Map consecutive pages to a contiguous range of addresses. 211 219 * @mm: Address space to map the pages into. ··· 239 231 if (--nr == 0) 240 232 break; 241 233 ptep++; 242 - pte = __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); 234 + pte = pte_next_pfn(pte); 243 235 } 244 236 arch_leave_lazy_mmu_mode(); 245 237 }