x86/mm: Add barriers and document switch_mm()-vs-flush synchronization
[pandora-kernel.git] / arch / x86 / include / asm / mmu_context.h
1 #ifndef _ASM_X86_MMU_CONTEXT_H
2 #define _ASM_X86_MMU_CONTEXT_H
3
4 #include <asm/desc.h>
5 #include <linux/atomic.h>
6 #include <asm/pgalloc.h>
7 #include <asm/tlbflush.h>
8 #include <asm/paravirt.h>
9 #ifndef CONFIG_PARAVIRT
10 #include <asm-generic/mm_hooks.h>
11
12 static inline void paravirt_activate_mm(struct mm_struct *prev,
13                                         struct mm_struct *next)
14 {
15 }
16 #endif  /* !CONFIG_PARAVIRT */
17
18 /*
19  * ldt_structs can be allocated, used, and freed, but they are never
20  * modified while live.
21  */
22 struct ldt_struct {
23         /*
24          * Xen requires page-aligned LDTs with special permissions.  This is
25          * needed to prevent us from installing evil descriptors such as
26          * call gates.  On native, we could merge the ldt_struct and LDT
27          * allocations, but it's not worth trying to optimize.
28          */
29         struct desc_struct *entries;
30         int size;
31 };
32
33 static inline void load_mm_ldt(struct mm_struct *mm)
34 {
35         struct ldt_struct *ldt;
36
37         /* smp_read_barrier_depends synchronizes with barrier in install_ldt */
38         ldt = ACCESS_ONCE(mm->context.ldt);
39         smp_read_barrier_depends();
40
41         /*
42          * Any change to mm->context.ldt is followed by an IPI to all
43          * CPUs with the mm active.  The LDT will not be freed until
44          * after the IPI is handled by all such CPUs.  This means that,
45          * if the ldt_struct changes before we return, the values we see
46          * will be safe, and the new values will be loaded before we run
47          * any user code.
48          *
49          * NB: don't try to convert this to use RCU without extreme care.
50          * We would still need IRQs off, because we don't want to change
51          * the local LDT after an IPI loaded a newer value than the one
52          * that we can see.
53          */
54
55         if (unlikely(ldt))
56                 set_ldt(ldt->entries, ldt->size);
57         else
58                 clear_LDT();
59
60         DEBUG_LOCKS_WARN_ON(preemptible());
61 }
62
63 /*
64  * Used for LDT copy/destruction.
65  */
66 int init_new_context(struct task_struct *tsk, struct mm_struct *mm);
67 void destroy_context(struct mm_struct *mm);
68
69
70 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
71 {
72 #ifdef CONFIG_SMP
73         if (percpu_read(cpu_tlbstate.state) == TLBSTATE_OK)
74                 percpu_write(cpu_tlbstate.state, TLBSTATE_LAZY);
75 #endif
76 }
77
78 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
79                              struct task_struct *tsk)
80 {
81         unsigned cpu = smp_processor_id();
82
83         if (likely(prev != next)) {
84 #ifdef CONFIG_SMP
85                 percpu_write(cpu_tlbstate.state, TLBSTATE_OK);
86                 percpu_write(cpu_tlbstate.active_mm, next);
87 #endif
88                 cpumask_set_cpu(cpu, mm_cpumask(next));
89
90                 /*
91                  * Re-load page tables.
92                  *
93                  * This logic has an ordering constraint:
94                  *
95                  *  CPU 0: Write to a PTE for 'next'
96                  *  CPU 0: load bit 1 in mm_cpumask.  if nonzero, send IPI.
97                  *  CPU 1: set bit 1 in next's mm_cpumask
98                  *  CPU 1: load from the PTE that CPU 0 writes (implicit)
99                  *
100                  * We need to prevent an outcome in which CPU 1 observes
101                  * the new PTE value and CPU 0 observes bit 1 clear in
102                  * mm_cpumask.  (If that occurs, then the IPI will never
103                  * be sent, and CPU 0's TLB will contain a stale entry.)
104                  *
105                  * The bad outcome can occur if either CPU's load is
106                  * reordered before that CPU's store, so both CPUs much
107                  * execute full barriers to prevent this from happening.
108                  *
109                  * Thus, switch_mm needs a full barrier between the
110                  * store to mm_cpumask and any operation that could load
111                  * from next->pgd.  This barrier synchronizes with
112                  * remote TLB flushers.  Fortunately, load_cr3 is
113                  * serializing and thus acts as a full barrier.
114                  *
115                  */
116                 load_cr3(next->pgd);
117
118                 /* stop flush ipis for the previous mm */
119                 cpumask_clear_cpu(cpu, mm_cpumask(prev));
120
121                 /*
122                  * load the LDT, if the LDT is different:
123                  */
124                 if (unlikely(prev->context.ldt != next->context.ldt))
125                         load_mm_ldt(next);
126         }
127 #ifdef CONFIG_SMP
128         else {
129                 percpu_write(cpu_tlbstate.state, TLBSTATE_OK);
130                 BUG_ON(percpu_read(cpu_tlbstate.active_mm) != next);
131
132                 if (!cpumask_test_and_set_cpu(cpu, mm_cpumask(next))) {
133                         /* We were in lazy tlb mode and leave_mm disabled
134                          * tlb flush IPI delivery. We must reload CR3
135                          * to make sure to use no freed page tables.
136                          *
137                          * As above, this is a barrier that forces
138                          * TLB repopulation to be ordered after the
139                          * store to mm_cpumask.
140                          */
141                         load_cr3(next->pgd);
142                         load_mm_ldt(next);
143                 }
144         }
145 #endif
146 }
147
148 #define activate_mm(prev, next)                 \
149 do {                                            \
150         paravirt_activate_mm((prev), (next));   \
151         switch_mm((prev), (next), NULL);        \
152 } while (0);
153
154 #ifdef CONFIG_X86_32
155 #define deactivate_mm(tsk, mm)                  \
156 do {                                            \
157         lazy_load_gs(0);                        \
158 } while (0)
159 #else
160 #define deactivate_mm(tsk, mm)                  \
161 do {                                            \
162         load_gs_index(0);                       \
163         loadsegment(fs, 0);                     \
164 } while (0)
165 #endif
166
167 #endif /* _ASM_X86_MMU_CONTEXT_H */