H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_MMU_CONTEXT_H |
| 2 | #define _ASM_X86_MMU_CONTEXT_H |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 3 | |
| 4 | #include <asm/desc.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 5 | #include <linux/atomic.h> |
Dave Hansen | d17d8f9 | 2014-07-31 08:40:59 -0700 | [diff] [blame] | 6 | #include <linux/mm_types.h> |
| 7 | |
| 8 | #include <trace/events/tlb.h> |
| 9 | |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 10 | #include <asm/pgalloc.h> |
| 11 | #include <asm/tlbflush.h> |
| 12 | #include <asm/paravirt.h> |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 13 | #include <asm/mpx.h> |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 14 | #ifndef CONFIG_PARAVIRT |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 15 | static inline void paravirt_activate_mm(struct mm_struct *prev, |
| 16 | struct mm_struct *next) |
| 17 | { |
| 18 | } |
| 19 | #endif /* !CONFIG_PARAVIRT */ |
| 20 | |
| 21 | /* |
| 22 | * Used for LDT copy/destruction. |
| 23 | */ |
| 24 | int init_new_context(struct task_struct *tsk, struct mm_struct *mm); |
| 25 | void destroy_context(struct mm_struct *mm); |
| 26 | |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 27 | |
| 28 | static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) |
| 29 | { |
| 30 | #ifdef CONFIG_SMP |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 31 | if (this_cpu_read(cpu_tlbstate.state) == TLBSTATE_OK) |
| 32 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_LAZY); |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 33 | #endif |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next, |
| 37 | struct task_struct *tsk) |
| 38 | { |
| 39 | unsigned cpu = smp_processor_id(); |
| 40 | |
| 41 | if (likely(prev != next)) { |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 42 | #ifdef CONFIG_SMP |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 43 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); |
| 44 | this_cpu_write(cpu_tlbstate.active_mm, next); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 45 | #endif |
Rusty Russell | 78f1c4d | 2009-09-24 09:34:51 -0600 | [diff] [blame] | 46 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 47 | |
| 48 | /* Re-load page tables */ |
| 49 | load_cr3(next->pgd); |
Dave Hansen | d17d8f9 | 2014-07-31 08:40:59 -0700 | [diff] [blame] | 50 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 51 | |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 52 | /* Stop flush ipis for the previous mm */ |
Suresh Siddha | 831d52bc1 | 2011-02-03 12:20:04 -0800 | [diff] [blame] | 53 | cpumask_clear_cpu(cpu, mm_cpumask(prev)); |
| 54 | |
Andy Lutomirski | c4a7bba | 2014-10-06 12:36:47 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Load the LDT, if the LDT is different. |
| 57 | * |
| 58 | * It's possible leave_mm(prev) has been called. If so, |
| 59 | * then prev->context.ldt could be out of sync with the |
| 60 | * LDT descriptor or the LDT register. This can only happen |
| 61 | * if prev->context.ldt is non-null, since we never free |
| 62 | * an LDT. But LDTs can't be shared across mms, so |
| 63 | * prev->context.ldt won't be equal to next->context.ldt. |
| 64 | */ |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 65 | if (unlikely(prev->context.ldt != next->context.ldt)) |
| 66 | load_LDT_nolock(&next->context); |
| 67 | } |
| 68 | #ifdef CONFIG_SMP |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 69 | else { |
Alex Shi | c6ae41e | 2012-05-11 15:35:27 +0800 | [diff] [blame] | 70 | this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK); |
| 71 | BUG_ON(this_cpu_read(cpu_tlbstate.active_mm) != next); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 72 | |
Rik van Riel | 8f898fb | 2013-07-31 22:14:21 -0400 | [diff] [blame] | 73 | if (!cpumask_test_cpu(cpu, mm_cpumask(next))) { |
| 74 | /* |
| 75 | * On established mms, the mm_cpumask is only changed |
| 76 | * from irq context, from ptep_clear_flush() while in |
| 77 | * lazy tlb mode, and here. Irqs are blocked during |
| 78 | * schedule, protecting us from simultaneous changes. |
| 79 | */ |
| 80 | cpumask_set_cpu(cpu, mm_cpumask(next)); |
| 81 | /* |
| 82 | * We were in lazy tlb mode and leave_mm disabled |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 83 | * tlb flush IPI delivery. We must reload CR3 |
| 84 | * to make sure to use no freed page tables. |
| 85 | */ |
| 86 | load_cr3(next->pgd); |
Dave Hansen | d17d8f9 | 2014-07-31 08:40:59 -0700 | [diff] [blame] | 87 | trace_tlb_flush(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL); |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 88 | load_LDT_nolock(&next->context); |
| 89 | } |
| 90 | } |
| 91 | #endif |
| 92 | } |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 93 | |
| 94 | #define activate_mm(prev, next) \ |
| 95 | do { \ |
| 96 | paravirt_activate_mm((prev), (next)); \ |
| 97 | switch_mm((prev), (next), NULL); \ |
| 98 | } while (0); |
| 99 | |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 100 | #ifdef CONFIG_X86_32 |
| 101 | #define deactivate_mm(tsk, mm) \ |
| 102 | do { \ |
Tejun Heo | ccbeed3 | 2009-02-09 22:17:40 +0900 | [diff] [blame] | 103 | lazy_load_gs(0); \ |
Brian Gerst | 6826c8f | 2009-01-21 17:26:06 +0900 | [diff] [blame] | 104 | } while (0) |
| 105 | #else |
| 106 | #define deactivate_mm(tsk, mm) \ |
| 107 | do { \ |
| 108 | load_gs_index(0); \ |
| 109 | loadsegment(fs, 0); \ |
| 110 | } while (0) |
| 111 | #endif |
Jeremy Fitzhardinge | c3c2fee | 2008-06-25 00:19:07 -0400 | [diff] [blame] | 112 | |
Dave Hansen | a1ea1c0 | 2014-11-18 10:23:49 -0800 | [diff] [blame] | 113 | static inline void arch_dup_mmap(struct mm_struct *oldmm, |
| 114 | struct mm_struct *mm) |
| 115 | { |
| 116 | paravirt_arch_dup_mmap(oldmm, mm); |
| 117 | } |
| 118 | |
| 119 | static inline void arch_exit_mmap(struct mm_struct *mm) |
| 120 | { |
| 121 | paravirt_arch_exit_mmap(mm); |
| 122 | } |
| 123 | |
Dave Hansen | fe3d197 | 2014-11-14 07:18:29 -0800 | [diff] [blame] | 124 | static inline void arch_bprm_mm_init(struct mm_struct *mm, |
| 125 | struct vm_area_struct *vma) |
| 126 | { |
| 127 | mpx_mm_init(mm); |
| 128 | } |
| 129 | |
Dave Hansen | 1de4fa1 | 2014-11-14 07:18:31 -0800 | [diff] [blame] | 130 | static inline void arch_unmap(struct mm_struct *mm, struct vm_area_struct *vma, |
| 131 | unsigned long start, unsigned long end) |
| 132 | { |
Dave Hansen | c922228 | 2015-01-08 14:30:21 -0800 | [diff] [blame] | 133 | /* |
| 134 | * mpx_notify_unmap() goes and reads a rarely-hot |
| 135 | * cacheline in the mm_struct. That can be expensive |
| 136 | * enough to be seen in profiles. |
| 137 | * |
| 138 | * The mpx_notify_unmap() call and its contents have been |
| 139 | * observed to affect munmap() performance on hardware |
| 140 | * where MPX is not present. |
| 141 | * |
| 142 | * The unlikely() optimizes for the fast case: no MPX |
| 143 | * in the CPU, or no MPX use in the process. Even if |
| 144 | * we get this wrong (in the unlikely event that MPX |
| 145 | * is widely enabled on some system) the overhead of |
| 146 | * MPX itself (reading bounds tables) is expected to |
| 147 | * overwhelm the overhead of getting this unlikely() |
| 148 | * consistently wrong. |
| 149 | */ |
| 150 | if (unlikely(cpu_feature_enabled(X86_FEATURE_MPX))) |
| 151 | mpx_notify_unmap(mm, vma, start, end); |
Dave Hansen | 1de4fa1 | 2014-11-14 07:18:31 -0800 | [diff] [blame] | 152 | } |
| 153 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 154 | #endif /* _ASM_X86_MMU_CONTEXT_H */ |