0179-x86-mm-Move-the-CR3-construction-functions-to-tlbflu.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. From f978416e1df8d655e6ac7ae848928441cf33d598 Mon Sep 17 00:00:00 2001
  2. From: Dave Hansen <[email protected]>
  3. Date: Mon, 4 Dec 2017 15:07:54 +0100
  4. Subject: [PATCH 179/242] x86/mm: Move the CR3 construction functions to
  5. tlbflush.h
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. CVE-2017-5754
  10. For flushing the TLB, the ASID which has been programmed into the hardware
  11. must be known. That differs from what is in 'cpu_tlbstate'.
  12. Add functions to transform the 'cpu_tlbstate' values into to the one
  13. programmed into the hardware (CR3).
  14. It's not easy to include mmu_context.h into tlbflush.h, so just move the
  15. CR3 building over to tlbflush.h.
  16. Signed-off-by: Dave Hansen <[email protected]>
  17. Signed-off-by: Thomas Gleixner <[email protected]>
  18. Cc: Andy Lutomirski <[email protected]>
  19. Cc: Boris Ostrovsky <[email protected]>
  20. Cc: Borislav Petkov <[email protected]>
  21. Cc: Brian Gerst <[email protected]>
  22. Cc: David Laight <[email protected]>
  23. Cc: Denys Vlasenko <[email protected]>
  24. Cc: Eduardo Valentin <[email protected]>
  25. Cc: Greg KH <[email protected]>
  26. Cc: H. Peter Anvin <[email protected]>
  27. Cc: Josh Poimboeuf <[email protected]>
  28. Cc: Juergen Gross <[email protected]>
  29. Cc: Linus Torvalds <[email protected]>
  30. Cc: Peter Zijlstra <[email protected]>
  31. Cc: Will Deacon <[email protected]>
  32. Cc: [email protected]
  33. Cc: [email protected]
  34. Cc: [email protected]
  35. Cc: [email protected]
  36. Cc: [email protected]
  37. Signed-off-by: Ingo Molnar <[email protected]>
  38. (cherry picked from commit 50fb83a62cf472dc53ba23bd3f7bd6c1b2b3b53e)
  39. Signed-off-by: Andy Whitcroft <[email protected]>
  40. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  41. (cherry picked from commit f741923acf51c1061c11b45a168f8864d37dc5cd)
  42. Signed-off-by: Fabian Grünbichler <[email protected]>
  43. ---
  44. arch/x86/include/asm/mmu_context.h | 29 +----------------------------
  45. arch/x86/include/asm/tlbflush.h | 26 ++++++++++++++++++++++++++
  46. arch/x86/mm/tlb.c | 8 ++++----
  47. 3 files changed, 31 insertions(+), 32 deletions(-)
  48. diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h
  49. index 47ec51a821e8..89a01ad7e370 100644
  50. --- a/arch/x86/include/asm/mmu_context.h
  51. +++ b/arch/x86/include/asm/mmu_context.h
  52. @@ -289,33 +289,6 @@ static inline bool arch_vma_access_permitted(struct vm_area_struct *vma,
  53. return __pkru_allows_pkey(vma_pkey(vma), write);
  54. }
  55. -/*
  56. - * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID
  57. - * bits. This serves two purposes. It prevents a nasty situation in
  58. - * which PCID-unaware code saves CR3, loads some other value (with PCID
  59. - * == 0), and then restores CR3, thus corrupting the TLB for ASID 0 if
  60. - * the saved ASID was nonzero. It also means that any bugs involving
  61. - * loading a PCID-enabled CR3 with CR4.PCIDE off will trigger
  62. - * deterministically.
  63. - */
  64. -
  65. -static inline unsigned long build_cr3(struct mm_struct *mm, u16 asid)
  66. -{
  67. - if (static_cpu_has(X86_FEATURE_PCID)) {
  68. - VM_WARN_ON_ONCE(asid > 4094);
  69. - return __sme_pa(mm->pgd) | (asid + 1);
  70. - } else {
  71. - VM_WARN_ON_ONCE(asid != 0);
  72. - return __sme_pa(mm->pgd);
  73. - }
  74. -}
  75. -
  76. -static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
  77. -{
  78. - VM_WARN_ON_ONCE(asid > 4094);
  79. - return __sme_pa(mm->pgd) | (asid + 1) | CR3_NOFLUSH;
  80. -}
  81. -
  82. /*
  83. * This can be used from process context to figure out what the value of
  84. * CR3 is without needing to do a (slow) __read_cr3().
  85. @@ -325,7 +298,7 @@ static inline unsigned long build_cr3_noflush(struct mm_struct *mm, u16 asid)
  86. */
  87. static inline unsigned long __get_current_cr3_fast(void)
  88. {
  89. - unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm),
  90. + unsigned long cr3 = build_cr3(this_cpu_read(cpu_tlbstate.loaded_mm)->pgd,
  91. this_cpu_read(cpu_tlbstate.loaded_mm_asid));
  92. /* For now, be very restrictive about when this can be called. */
  93. diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
  94. index ed5d483c4a1b..3a421b164868 100644
  95. --- a/arch/x86/include/asm/tlbflush.h
  96. +++ b/arch/x86/include/asm/tlbflush.h
  97. @@ -68,6 +68,32 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
  98. return atomic64_inc_return(&mm->context.tlb_gen);
  99. }
  100. +/*
  101. + * If PCID is on, ASID-aware code paths put the ASID+1 into the PCID bits.
  102. + * This serves two purposes. It prevents a nasty situation in which
  103. + * PCID-unaware code saves CR3, loads some other value (with PCID == 0),
  104. + * and then restores CR3, thus corrupting the TLB for ASID 0 if the saved
  105. + * ASID was nonzero. It also means that any bugs involving loading a
  106. + * PCID-enabled CR3 with CR4.PCIDE off will trigger deterministically.
  107. + */
  108. +struct pgd_t;
  109. +static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
  110. +{
  111. + if (static_cpu_has(X86_FEATURE_PCID)) {
  112. + VM_WARN_ON_ONCE(asid > 4094);
  113. + return __sme_pa(pgd) | (asid + 1);
  114. + } else {
  115. + VM_WARN_ON_ONCE(asid != 0);
  116. + return __sme_pa(pgd);
  117. + }
  118. +}
  119. +
  120. +static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
  121. +{
  122. + VM_WARN_ON_ONCE(asid > 4094);
  123. + return __sme_pa(pgd) | (asid + 1) | CR3_NOFLUSH;
  124. +}
  125. +
  126. #ifdef CONFIG_PARAVIRT
  127. #include <asm/paravirt.h>
  128. #else
  129. diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
  130. index 5b4342c5039c..87d4f961bcb4 100644
  131. --- a/arch/x86/mm/tlb.c
  132. +++ b/arch/x86/mm/tlb.c
  133. @@ -126,7 +126,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  134. * does something like write_cr3(read_cr3_pa()).
  135. */
  136. #ifdef CONFIG_DEBUG_VM
  137. - if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev, prev_asid))) {
  138. + if (WARN_ON_ONCE(__read_cr3() != build_cr3(real_prev->pgd, prev_asid))) {
  139. /*
  140. * If we were to BUG here, we'd be very likely to kill
  141. * the system so hard that we don't see the call trace.
  142. @@ -193,7 +193,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  143. if (need_flush) {
  144. this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id);
  145. this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen);
  146. - write_cr3(build_cr3(next, new_asid));
  147. + write_cr3(build_cr3(next->pgd, new_asid));
  148. /*
  149. * NB: This gets called via leave_mm() in the idle path
  150. @@ -206,7 +206,7 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  151. trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, TLB_FLUSH_ALL);
  152. } else {
  153. /* The new ASID is already up to date. */
  154. - write_cr3(build_cr3_noflush(next, new_asid));
  155. + write_cr3(build_cr3_noflush(next->pgd, new_asid));
  156. /* See above wrt _rcuidle. */
  157. trace_tlb_flush_rcuidle(TLB_FLUSH_ON_TASK_SWITCH, 0);
  158. @@ -283,7 +283,7 @@ void initialize_tlbstate_and_flush(void)
  159. !(cr4_read_shadow() & X86_CR4_PCIDE));
  160. /* Force ASID 0 and force a TLB flush. */
  161. - write_cr3(build_cr3(mm, 0));
  162. + write_cr3(build_cr3(mm->pgd, 0));
  163. /* Reinitialize tlbstate. */
  164. this_cpu_write(cpu_tlbstate.loaded_mm_asid, 0);
  165. --
  166. 2.14.2