x86/mm: Tidy up "x86/mm: Flush more aggressively in lazy TLB mode"

Due to timezones, commit:

b956575bed91 ("x86/mm: Flush more aggressively in lazy TLB mode")

was an outdated patch that well tested and fixed the bug but didn't
address Borislav's review comments.

Tidy it up:

- The name "tlb_use_lazy_mode()" was highly confusing. Change it to
"tlb_defer_switch_to_init_mm()", which describes what it actually
means.

- Move the static_branch crap into a helper.

- Improve comments.

Actually removing the debugfs option is in the next patch.

Reported-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Fixes: b956575bed91 ("x86/mm: Flush more aggressively in lazy TLB mode")
Link: http://lkml.kernel.org/r/154ef95428d4592596b6e98b0af1d2747d6cfbf8.1508000261.git.luto@kernel.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by Andy Lutomirski and committed by Ingo Molnar 4e57b946 e8b9b0cc

+24 -13
+6 -1
arch/x86/include/asm/tlbflush.h
··· 87 * to init_mm when we switch to a kernel thread (e.g. the idle thread). If 88 * it's false, then we immediately switch CR3 when entering a kernel thread. 89 */ 90 - DECLARE_STATIC_KEY_TRUE(tlb_use_lazy_mode); 91 92 /* 93 * 6 because 6 should be plenty and struct tlb_state will fit in
··· 87 * to init_mm when we switch to a kernel thread (e.g. the idle thread). If 88 * it's false, then we immediately switch CR3 when entering a kernel thread. 89 */ 90 + DECLARE_STATIC_KEY_TRUE(__tlb_defer_switch_to_init_mm); 91 + 92 + static inline bool tlb_defer_switch_to_init_mm(void) 93 + { 94 + return static_branch_unlikely(&__tlb_defer_switch_to_init_mm); 95 + } 96 97 /* 98 * 6 because 6 should be plenty and struct tlb_state will fit in
+18 -12
arch/x86/mm/tlb.c
··· 30 31 atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1); 32 33 - DEFINE_STATIC_KEY_TRUE(tlb_use_lazy_mode); 34 35 static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, 36 u16 *new_asid, bool *need_flush) ··· 213 } 214 215 /* 216 * enter_lazy_tlb() is a hint from the scheduler that we are entering a 217 * kernel thread or other context without an mm. Acceptable implementations 218 * include doing nothing whatsoever, switching to init_mm, or various clever ··· 230 if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm) 231 return; 232 233 - if (static_branch_unlikely(&tlb_use_lazy_mode)) { 234 /* 235 * There's a significant optimization that may be possible 236 * here. We have accurate enough TLB flush tracking that we ··· 635 { 636 char buf[2]; 637 638 - buf[0] = static_branch_likely(&tlb_use_lazy_mode) ? '1' : '0'; 639 buf[1] = '\n'; 640 641 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); ··· 651 return -EINVAL; 652 653 if (val) 654 - static_branch_enable(&tlb_use_lazy_mode); 655 else 656 - static_branch_disable(&tlb_use_lazy_mode); 657 658 return count; 659 } ··· 664 .llseek = default_llseek, 665 }; 666 667 - static int __init init_tlb_use_lazy_mode(void) 668 { 669 if (boot_cpu_has(X86_FEATURE_PCID)) { 670 /* 671 - * Heuristic: with PCID on, switching to and from 672 - * init_mm is reasonably fast, but remote flush IPIs 673 - * as expensive as ever, so turn off lazy TLB mode. 674 * 675 * We can't do this in setup_pcid() because static keys 676 * haven't been initialized yet, and it would blow up 677 * badly. 678 */ 679 - static_branch_disable(&tlb_use_lazy_mode); 680 } 681 682 - debugfs_create_file("tlb_use_lazy_mode", S_IRUSR | S_IWUSR, 683 arch_debugfs_dir, NULL, &fops_tlblazy); 684 return 0; 685 } 686 - late_initcall(init_tlb_use_lazy_mode);
··· 30 31 atomic64_t last_mm_ctx_id = ATOMIC64_INIT(1); 32 33 + DEFINE_STATIC_KEY_TRUE(__tlb_defer_switch_to_init_mm); 34 35 static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, 36 u16 *new_asid, bool *need_flush) ··· 213 } 214 215 /* 216 + * Please ignore the name of this function. It should be called 217 + * switch_to_kernel_thread(). 218 + * 219 * enter_lazy_tlb() is a hint from the scheduler that we are entering a 220 * kernel thread or other context without an mm. Acceptable implementations 221 * include doing nothing whatsoever, switching to init_mm, or various clever ··· 227 if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm) 228 return; 229 230 + if (tlb_defer_switch_to_init_mm()) { 231 /* 232 * There's a significant optimization that may be possible 233 * here. We have accurate enough TLB flush tracking that we ··· 632 { 633 char buf[2]; 634 635 + buf[0] = static_branch_likely(&__tlb_defer_switch_to_init_mm) 636 + ? '1' : '0'; 637 buf[1] = '\n'; 638 639 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); ··· 647 return -EINVAL; 648 649 if (val) 650 + static_branch_enable(&__tlb_defer_switch_to_init_mm); 651 else 652 + static_branch_disable(&__tlb_defer_switch_to_init_mm); 653 654 return count; 655 } ··· 660 .llseek = default_llseek, 661 }; 662 663 + static int __init init_tlblazy(void) 664 { 665 if (boot_cpu_has(X86_FEATURE_PCID)) { 666 /* 667 + * If we have PCID, then switching to init_mm is reasonably 668 + * fast. If we don't have PCID, then switching to init_mm is 669 + * quite slow, so we default to trying to defer it in the 670 + * hopes that we can avoid it entirely. The latter approach 671 + * runs the risk of receiving otherwise unnecessary IPIs. 672 * 673 * We can't do this in setup_pcid() because static keys 674 * haven't been initialized yet, and it would blow up 675 * badly. 676 */ 677 + static_branch_disable(&__tlb_defer_switch_to_init_mm); 678 } 679 680 + debugfs_create_file("tlb_defer_switch_to_init_mm", S_IRUSR | S_IWUSR, 681 arch_debugfs_dir, NULL, &fops_tlblazy); 682 return 0; 683 } 684 + late_initcall(init_tlblazy);