1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_X86_TLBFLUSH_H 3 #define _ASM_X86_TLBFLUSH_H 4 5 #include <linux/mm_types.h> 6 #include <linux/mmu_notifier.h> 7 #include <linux/minmax.h> 8 #include <linux/sched.h> 9 10 #include <asm/barrier.h> 11 #include <asm/processor.h> 12 #include <asm/cpufeature.h> 13 #include <asm/special_insns.h> 14 #include <asm/smp.h> 15 #include <asm/invpcid.h> 16 #include <asm/pti.h> 17 #include <asm/processor-flags.h> 18 #include <asm/pgtable.h> 19 20 DECLARE_PER_CPU(u64, tlbstate_untag_mask); 21 22 void __flush_tlb_all(void); 23 24 #define TLB_FLUSH_ALL -1UL 25 #define TLB_GENERATION_INVALID 0 26 27 void cr4_update_irqsoff(unsigned long set, unsigned long clear); 28 unsigned long cr4_read_shadow(void); 29 30 /* Set in this cpu's CR4. */ 31 static inline void cr4_set_bits_irqsoff(unsigned long mask) 32 { 33 cr4_update_irqsoff(mask, 0); 34 } 35 36 /* Clear in this cpu's CR4. */ 37 static inline void cr4_clear_bits_irqsoff(unsigned long mask) 38 { 39 cr4_update_irqsoff(0, mask); 40 } 41 42 /* Set in this cpu's CR4. */ 43 static inline void cr4_set_bits(unsigned long mask) 44 { 45 unsigned long flags; 46 47 local_irq_save(flags); 48 cr4_set_bits_irqsoff(mask); 49 local_irq_restore(flags); 50 } 51 52 /* Clear in this cpu's CR4. */ 53 static inline void cr4_clear_bits(unsigned long mask) 54 { 55 unsigned long flags; 56 57 local_irq_save(flags); 58 cr4_clear_bits_irqsoff(mask); 59 local_irq_restore(flags); 60 } 61 62 #ifndef MODULE 63 /* 64 * 6 because 6 should be plenty and struct tlb_state will fit in two cache 65 * lines. 66 */ 67 #define TLB_NR_DYN_ASIDS 6 68 69 struct tlb_context { 70 u64 ctx_id; 71 u64 tlb_gen; 72 }; 73 74 struct tlb_state { 75 /* 76 * cpu_tlbstate.loaded_mm should match CR3 whenever interrupts 77 * are on. This means that it may not match current->active_mm, 78 * which will contain the previous user mm when we're in lazy TLB 79 * mode even if we've already switched back to swapper_pg_dir. 80 * 81 * During switch_mm_irqs_off(), loaded_mm will be set to 82 * LOADED_MM_SWITCHING during the brief interrupts-off window 83 * when CR3 and loaded_mm would otherwise be inconsistent. This 84 * is for nmi_uaccess_okay()'s benefit. 85 */ 86 struct mm_struct *loaded_mm; 87 88 #define LOADED_MM_SWITCHING ((struct mm_struct *)1UL) 89 90 /* Last user mm for optimizing IBPB */ 91 union { 92 struct mm_struct *last_user_mm; 93 unsigned long last_user_mm_spec; 94 }; 95 96 u16 loaded_mm_asid; 97 u16 next_asid; 98 99 /* 100 * If set we changed the page tables in such a way that we 101 * needed an invalidation of all contexts (aka. PCIDs / ASIDs). 102 * This tells us to go invalidate all the non-loaded ctxs[] 103 * on the next context switch. 104 * 105 * The current ctx was kept up-to-date as it ran and does not 106 * need to be invalidated. 107 */ 108 bool invalidate_other; 109 110 #ifdef CONFIG_ADDRESS_MASKING 111 /* 112 * Active LAM mode. 113 * 114 * X86_CR3_LAM_U57/U48 shifted right by X86_CR3_LAM_U57_BIT or 0 if LAM 115 * disabled. 116 */ 117 u8 lam; 118 #endif 119 120 /* 121 * Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate 122 * the corresponding user PCID needs a flush next time we 123 * switch to it; see SWITCH_TO_USER_CR3. 124 */ 125 unsigned short user_pcid_flush_mask; 126 127 /* 128 * Access to this CR4 shadow and to H/W CR4 is protected by 129 * disabling interrupts when modifying either one. 130 */ 131 unsigned long cr4; 132 133 /* 134 * This is a list of all contexts that might exist in the TLB. 135 * There is one per ASID that we use, and the ASID (what the 136 * CPU calls PCID) is the index into ctxts. 137 * 138 * For each context, ctx_id indicates which mm the TLB's user 139 * entries came from. As an invariant, the TLB will never 140 * contain entries that are out-of-date as when that mm reached 141 * the tlb_gen in the list. 142 * 143 * To be clear, this means that it's legal for the TLB code to 144 * flush the TLB without updating tlb_gen. This can happen 145 * (for now, at least) due to paravirt remote flushes. 146 * 147 * NB: context 0 is a bit special, since it's also used by 148 * various bits of init code. This is fine -- code that 149 * isn't aware of PCID will end up harmlessly flushing 150 * context 0. 151 */ 152 struct tlb_context ctxs[TLB_NR_DYN_ASIDS]; 153 }; 154 DECLARE_PER_CPU_ALIGNED(struct tlb_state, cpu_tlbstate); 155 156 struct tlb_state_shared { 157 /* 158 * We can be in one of several states: 159 * 160 * - Actively using an mm. Our CPU's bit will be set in 161 * mm_cpumask(loaded_mm) and is_lazy == false; 162 * 163 * - Not using a real mm. loaded_mm == &init_mm. Our CPU's bit 164 * will not be set in mm_cpumask(&init_mm) and is_lazy == false. 165 * 166 * - Lazily using a real mm. loaded_mm != &init_mm, our bit 167 * is set in mm_cpumask(loaded_mm), but is_lazy == true. 168 * We're heuristically guessing that the CR3 load we 169 * skipped more than makes up for the overhead added by 170 * lazy mode. 171 */ 172 bool is_lazy; 173 }; 174 DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state_shared, cpu_tlbstate_shared); 175 176 /* 177 * Please ignore the name of this function. It should be called 178 * switch_to_kernel_thread(). 179 * 180 * enter_lazy_tlb() is a hint from the scheduler that we are entering a 181 * kernel thread or other context without an mm. Acceptable implementations 182 * include doing nothing whatsoever, switching to init_mm, or various clever 183 * lazy tricks to try to minimize TLB flushes. 184 * 185 * The scheduler reserves the right to call enter_lazy_tlb() several times 186 * in a row. It will notify us that we're going back to a real mm by 187 * calling switch_mm_irqs_off(). 188 */ 189 #define enter_lazy_tlb enter_lazy_tlb 190 static __always_inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) 191 { 192 if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm) 193 return; 194 195 this_cpu_write(cpu_tlbstate_shared.is_lazy, true); 196 } 197 198 bool nmi_uaccess_okay(void); 199 #define nmi_uaccess_okay nmi_uaccess_okay 200 201 /* Initialize cr4 shadow for this CPU. */ 202 static inline void cr4_init_shadow(void) 203 { 204 this_cpu_write(cpu_tlbstate.cr4, __read_cr4()); 205 } 206 207 extern unsigned long mmu_cr4_features; 208 extern u32 *trampoline_cr4_features; 209 210 /* How many pages can be invalidated with one INVLPGB. */ 211 extern u16 invlpgb_count_max; 212 213 extern void initialize_tlbstate_and_flush(void); 214 215 /* 216 * Keep stack-allocated flush_tlb_info cacheline aligned, but cap the 217 * alignment to avoid excessive stack usage on large-cacheline systems. 218 */ 219 #define FLUSH_TLB_INFO_ALIGN MIN(SMP_CACHE_BYTES, 64) 220 221 /* 222 * TLB flushing: 223 * 224 * - flush_tlb_all() flushes all processes TLBs 225 * - flush_tlb_mm(mm) flushes the specified mm context TLB's 226 * - flush_tlb_page(vma, vmaddr) flushes one page 227 * - flush_tlb_range(vma, start, end) flushes a range of pages 228 * - flush_tlb_kernel_range(start, end) flushes a range of kernel pages 229 * - flush_tlb_multi(cpumask, info) flushes TLBs on multiple cpus 230 * 231 * ..but the i386 has somewhat limited tlb flushing capabilities, 232 * and page-granular flushes are available only on i486 and up. 233 */ 234 struct flush_tlb_info { 235 /* 236 * We support several kinds of flushes. 237 * 238 * - Fully flush a single mm. .mm will be set, .end will be 239 * TLB_FLUSH_ALL, and .new_tlb_gen will be the tlb_gen to 240 * which the IPI sender is trying to catch us up. 241 * 242 * - Partially flush a single mm. .mm will be set, .start and 243 * .end will indicate the range, and .new_tlb_gen will be set 244 * such that the changes between generation .new_tlb_gen-1 and 245 * .new_tlb_gen are entirely contained in the indicated range. 246 * 247 * - Fully flush all mms whose tlb_gens have been updated. .mm 248 * will be NULL, .end will be TLB_FLUSH_ALL, and .new_tlb_gen 249 * will be zero. 250 */ 251 struct mm_struct *mm; 252 unsigned long start; 253 unsigned long end; 254 u64 new_tlb_gen; 255 unsigned int initiating_cpu; 256 u8 stride_shift; 257 u8 freed_tables; 258 u8 trim_cpumask; 259 } __aligned(FLUSH_TLB_INFO_ALIGN); 260 261 void flush_tlb_local(void); 262 void flush_tlb_one_user(unsigned long addr); 263 void flush_tlb_one_kernel(unsigned long addr); 264 void flush_tlb_multi(const struct cpumask *cpumask, 265 const struct flush_tlb_info *info); 266 267 static inline bool is_dyn_asid(u16 asid) 268 { 269 return asid < TLB_NR_DYN_ASIDS; 270 } 271 272 static inline bool is_global_asid(u16 asid) 273 { 274 return !is_dyn_asid(asid); 275 } 276 277 #ifdef CONFIG_BROADCAST_TLB_FLUSH 278 static inline u16 mm_global_asid(struct mm_struct *mm) 279 { 280 u16 asid; 281 282 if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) 283 return 0; 284 285 asid = smp_load_acquire(&mm->context.global_asid); 286 287 /* mm->context.global_asid is either 0, or a global ASID */ 288 VM_WARN_ON_ONCE(asid && is_dyn_asid(asid)); 289 290 return asid; 291 } 292 293 static inline void mm_init_global_asid(struct mm_struct *mm) 294 { 295 if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) { 296 mm->context.global_asid = 0; 297 mm->context.asid_transition = false; 298 } 299 } 300 301 static inline void mm_assign_global_asid(struct mm_struct *mm, u16 asid) 302 { 303 /* 304 * Notably flush_tlb_mm_range() -> broadcast_tlb_flush() -> 305 * finish_asid_transition() needs to observe asid_transition = true 306 * once it observes global_asid. 307 */ 308 mm->context.asid_transition = true; 309 smp_store_release(&mm->context.global_asid, asid); 310 } 311 312 static inline void mm_clear_asid_transition(struct mm_struct *mm) 313 { 314 WRITE_ONCE(mm->context.asid_transition, false); 315 } 316 317 static inline bool mm_in_asid_transition(struct mm_struct *mm) 318 { 319 if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) 320 return false; 321 322 return mm && READ_ONCE(mm->context.asid_transition); 323 } 324 325 extern void mm_free_global_asid(struct mm_struct *mm); 326 #else 327 static inline u16 mm_global_asid(struct mm_struct *mm) { return 0; } 328 static inline void mm_init_global_asid(struct mm_struct *mm) { } 329 static inline void mm_free_global_asid(struct mm_struct *mm) { } 330 static inline void mm_assign_global_asid(struct mm_struct *mm, u16 asid) { } 331 static inline void mm_clear_asid_transition(struct mm_struct *mm) { } 332 static inline bool mm_in_asid_transition(struct mm_struct *mm) { return false; } 333 #endif /* CONFIG_BROADCAST_TLB_FLUSH */ 334 335 #define flush_tlb_mm(mm) \ 336 flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL, true) 337 338 #define flush_tlb_range(vma, start, end) \ 339 flush_tlb_mm_range((vma)->vm_mm, start, end, \ 340 ((vma)->vm_flags & VM_HUGETLB) \ 341 ? huge_page_shift(hstate_vma(vma)) \ 342 : PAGE_SHIFT, true) 343 344 extern void flush_tlb_all(void); 345 extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, 346 unsigned long end, unsigned int stride_shift, 347 bool freed_tables); 348 extern void flush_tlb_kernel_range(unsigned long start, unsigned long end); 349 350 static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a) 351 { 352 flush_tlb_mm_range(vma->vm_mm, a, a + PAGE_SIZE, PAGE_SHIFT, false); 353 } 354 355 static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm) 356 { 357 bool should_defer = false; 358 359 /* If remote CPUs need to be flushed then defer batch the flush */ 360 if (cpumask_any_but(mm_cpumask(mm), get_cpu()) < nr_cpu_ids) 361 should_defer = true; 362 put_cpu(); 363 364 return should_defer; 365 } 366 367 static inline u64 inc_mm_tlb_gen(struct mm_struct *mm) 368 { 369 /* 370 * Bump the generation count. This also serves as a full barrier 371 * that synchronizes with switch_mm(): callers are required to order 372 * their read of mm_cpumask after their writes to the paging 373 * structures. 374 */ 375 return atomic64_inc_return(&mm->context.tlb_gen); 376 } 377 378 static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, 379 struct mm_struct *mm, unsigned long start, unsigned long end) 380 { 381 inc_mm_tlb_gen(mm); 382 cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); 383 batch->unmapped_pages = true; 384 mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); 385 } 386 387 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); 388 389 static inline bool pte_flags_need_flush(unsigned long oldflags, 390 unsigned long newflags, 391 bool ignore_access) 392 { 393 /* 394 * Flags that require a flush when cleared but not when they are set. 395 * Only include flags that would not trigger spurious page-faults. 396 * Non-present entries are not cached. Hardware would set the 397 * dirty/access bit if needed without a fault. 398 */ 399 const pteval_t flush_on_clear = _PAGE_DIRTY | _PAGE_PRESENT | 400 _PAGE_ACCESSED; 401 const pteval_t software_flags = _PAGE_SOFTW1 | _PAGE_SOFTW2 | 402 _PAGE_SOFTW3 | _PAGE_SOFTW4 | 403 _PAGE_SAVED_DIRTY; 404 const pteval_t flush_on_change = _PAGE_RW | _PAGE_USER | _PAGE_PWT | 405 _PAGE_PCD | _PAGE_PSE | _PAGE_GLOBAL | _PAGE_PAT | 406 _PAGE_PAT_LARGE | _PAGE_PKEY_BIT0 | _PAGE_PKEY_BIT1 | 407 _PAGE_PKEY_BIT2 | _PAGE_PKEY_BIT3 | _PAGE_NX; 408 unsigned long diff = oldflags ^ newflags; 409 410 BUILD_BUG_ON(flush_on_clear & software_flags); 411 BUILD_BUG_ON(flush_on_clear & flush_on_change); 412 BUILD_BUG_ON(flush_on_change & software_flags); 413 414 /* Ignore software flags */ 415 diff &= ~software_flags; 416 417 if (ignore_access) 418 diff &= ~_PAGE_ACCESSED; 419 420 /* 421 * Did any of the 'flush_on_clear' flags was clleared set from between 422 * 'oldflags' and 'newflags'? 423 */ 424 if (diff & oldflags & flush_on_clear) 425 return true; 426 427 /* Flush on modified flags. */ 428 if (diff & flush_on_change) 429 return true; 430 431 /* Ensure there are no flags that were left behind */ 432 if (IS_ENABLED(CONFIG_DEBUG_VM) && 433 (diff & ~(flush_on_clear | software_flags | flush_on_change))) { 434 VM_WARN_ON_ONCE(1); 435 return true; 436 } 437 438 return false; 439 } 440 441 /* 442 * pte_needs_flush() checks whether permissions were demoted and require a 443 * flush. It should only be used for userspace PTEs. 444 */ 445 static inline bool pte_needs_flush(pte_t oldpte, pte_t newpte) 446 { 447 /* !PRESENT -> * ; no need for flush */ 448 if (!(pte_flags(oldpte) & _PAGE_PRESENT)) 449 return false; 450 451 /* PFN changed ; needs flush */ 452 if (pte_pfn(oldpte) != pte_pfn(newpte)) 453 return true; 454 455 /* 456 * check PTE flags; ignore access-bit; see comment in 457 * ptep_clear_flush_young(). 458 */ 459 return pte_flags_need_flush(pte_flags(oldpte), pte_flags(newpte), 460 true); 461 } 462 #define pte_needs_flush pte_needs_flush 463 464 /* 465 * huge_pmd_needs_flush() checks whether permissions were demoted and require a 466 * flush. It should only be used for userspace huge PMDs. 467 */ 468 static inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd) 469 { 470 /* !PRESENT -> * ; no need for flush */ 471 if (!(pmd_flags(oldpmd) & _PAGE_PRESENT)) 472 return false; 473 474 /* PFN changed ; needs flush */ 475 if (pmd_pfn(oldpmd) != pmd_pfn(newpmd)) 476 return true; 477 478 /* 479 * check PMD flags; do not ignore access-bit; see 480 * pmdp_clear_flush_young(). 481 */ 482 return pte_flags_need_flush(pmd_flags(oldpmd), pmd_flags(newpmd), 483 false); 484 } 485 #define huge_pmd_needs_flush huge_pmd_needs_flush 486 487 #ifdef CONFIG_ADDRESS_MASKING 488 static inline u64 tlbstate_lam_cr3_mask(void) 489 { 490 u64 lam = this_cpu_read(cpu_tlbstate.lam); 491 492 return lam << X86_CR3_LAM_U57_BIT; 493 } 494 495 static inline void cpu_tlbstate_update_lam(unsigned long lam, u64 untag_mask) 496 { 497 this_cpu_write(cpu_tlbstate.lam, lam >> X86_CR3_LAM_U57_BIT); 498 this_cpu_write(tlbstate_untag_mask, untag_mask); 499 } 500 501 #else 502 503 static inline u64 tlbstate_lam_cr3_mask(void) 504 { 505 return 0; 506 } 507 508 static inline void cpu_tlbstate_update_lam(unsigned long lam, u64 untag_mask) 509 { 510 } 511 #endif 512 #else /* !MODULE */ 513 #define enter_lazy_tlb enter_lazy_tlb 514 extern void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) 515 __compiletime_error("enter_lazy_tlb() should not be used in modules"); 516 #endif /* !MODULE */ 517 518 static inline void __native_tlb_flush_global(unsigned long cr4) 519 { 520 native_write_cr4(cr4 ^ X86_CR4_PGE); 521 native_write_cr4(cr4); 522 } 523 #endif /* _ASM_X86_TLBFLUSH_H */ 524