0182-x86-mm-Create-asm-invpcid.h.patch 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. From a67ab82a8f60f725b002034dff10f28c7e2ac88e Mon Sep 17 00:00:00 2001
  2. From: Peter Zijlstra <[email protected]>
  3. Date: Tue, 5 Dec 2017 13:34:47 +0100
  4. Subject: [PATCH 182/231] x86/mm: Create asm/invpcid.h
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. Unclutter tlbflush.h a little.
  10. Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
  11. Cc: Andy Lutomirski <[email protected]>
  12. Cc: Boris Ostrovsky <[email protected]>
  13. Cc: Borislav Petkov <[email protected]>
  14. Cc: Brian Gerst <[email protected]>
  15. Cc: Dave Hansen <[email protected]>
  16. Cc: David Laight <[email protected]>
  17. Cc: Denys Vlasenko <[email protected]>
  18. Cc: Eduardo Valentin <[email protected]>
  19. Cc: Greg KH <[email protected]>
  20. Cc: H. Peter Anvin <[email protected]>
  21. Cc: Josh Poimboeuf <[email protected]>
  22. Cc: Juergen Gross <[email protected]>
  23. Cc: Linus Torvalds <[email protected]>
  24. Cc: Peter Zijlstra <[email protected]>
  25. Cc: Thomas Gleixner <[email protected]>
  26. Cc: Will Deacon <[email protected]>
  27. Cc: [email protected]
  28. Cc: [email protected]
  29. Cc: [email protected]
  30. Cc: [email protected]
  31. Cc: [email protected]
  32. Signed-off-by: Ingo Molnar <[email protected]>
  33. (cherry picked from commit 1a3b0caeb77edeac5ce5fa05e6a61c474c9a9745)
  34. Signed-off-by: Andy Whitcroft <[email protected]>
  35. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  36. (cherry picked from commit 5af02a8c43ce521f460891f6ba68af69428abe90)
  37. Signed-off-by: Fabian Grünbichler <[email protected]>
  38. ---
  39. arch/x86/include/asm/invpcid.h | 53 +++++++++++++++++++++++++++++++++++++++++
  40. arch/x86/include/asm/tlbflush.h | 49 +------------------------------------
  41. 2 files changed, 54 insertions(+), 48 deletions(-)
  42. create mode 100644 arch/x86/include/asm/invpcid.h
  43. diff --git a/arch/x86/include/asm/invpcid.h b/arch/x86/include/asm/invpcid.h
  44. new file mode 100644
  45. index 000000000000..989cfa86de85
  46. --- /dev/null
  47. +++ b/arch/x86/include/asm/invpcid.h
  48. @@ -0,0 +1,53 @@
  49. +/* SPDX-License-Identifier: GPL-2.0 */
  50. +#ifndef _ASM_X86_INVPCID
  51. +#define _ASM_X86_INVPCID
  52. +
  53. +static inline void __invpcid(unsigned long pcid, unsigned long addr,
  54. + unsigned long type)
  55. +{
  56. + struct { u64 d[2]; } desc = { { pcid, addr } };
  57. +
  58. + /*
  59. + * The memory clobber is because the whole point is to invalidate
  60. + * stale TLB entries and, especially if we're flushing global
  61. + * mappings, we don't want the compiler to reorder any subsequent
  62. + * memory accesses before the TLB flush.
  63. + *
  64. + * The hex opcode is invpcid (%ecx), %eax in 32-bit mode and
  65. + * invpcid (%rcx), %rax in long mode.
  66. + */
  67. + asm volatile (".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
  68. + : : "m" (desc), "a" (type), "c" (&desc) : "memory");
  69. +}
  70. +
  71. +#define INVPCID_TYPE_INDIV_ADDR 0
  72. +#define INVPCID_TYPE_SINGLE_CTXT 1
  73. +#define INVPCID_TYPE_ALL_INCL_GLOBAL 2
  74. +#define INVPCID_TYPE_ALL_NON_GLOBAL 3
  75. +
  76. +/* Flush all mappings for a given pcid and addr, not including globals. */
  77. +static inline void invpcid_flush_one(unsigned long pcid,
  78. + unsigned long addr)
  79. +{
  80. + __invpcid(pcid, addr, INVPCID_TYPE_INDIV_ADDR);
  81. +}
  82. +
  83. +/* Flush all mappings for a given PCID, not including globals. */
  84. +static inline void invpcid_flush_single_context(unsigned long pcid)
  85. +{
  86. + __invpcid(pcid, 0, INVPCID_TYPE_SINGLE_CTXT);
  87. +}
  88. +
  89. +/* Flush all mappings, including globals, for all PCIDs. */
  90. +static inline void invpcid_flush_all(void)
  91. +{
  92. + __invpcid(0, 0, INVPCID_TYPE_ALL_INCL_GLOBAL);
  93. +}
  94. +
  95. +/* Flush all mappings for all PCIDs except globals. */
  96. +static inline void invpcid_flush_all_nonglobals(void)
  97. +{
  98. + __invpcid(0, 0, INVPCID_TYPE_ALL_NON_GLOBAL);
  99. +}
  100. +
  101. +#endif /* _ASM_X86_INVPCID */
  102. diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
  103. index ecd634f87e4e..503f87c30c15 100644
  104. --- a/arch/x86/include/asm/tlbflush.h
  105. +++ b/arch/x86/include/asm/tlbflush.h
  106. @@ -8,54 +8,7 @@
  107. #include <asm/cpufeature.h>
  108. #include <asm/special_insns.h>
  109. #include <asm/smp.h>
  110. -
  111. -static inline void __invpcid(unsigned long pcid, unsigned long addr,
  112. - unsigned long type)
  113. -{
  114. - struct { u64 d[2]; } desc = { { pcid, addr } };
  115. -
  116. - /*
  117. - * The memory clobber is because the whole point is to invalidate
  118. - * stale TLB entries and, especially if we're flushing global
  119. - * mappings, we don't want the compiler to reorder any subsequent
  120. - * memory accesses before the TLB flush.
  121. - *
  122. - * The hex opcode is invpcid (%ecx), %eax in 32-bit mode and
  123. - * invpcid (%rcx), %rax in long mode.
  124. - */
  125. - asm volatile (".byte 0x66, 0x0f, 0x38, 0x82, 0x01"
  126. - : : "m" (desc), "a" (type), "c" (&desc) : "memory");
  127. -}
  128. -
  129. -#define INVPCID_TYPE_INDIV_ADDR 0
  130. -#define INVPCID_TYPE_SINGLE_CTXT 1
  131. -#define INVPCID_TYPE_ALL_INCL_GLOBAL 2
  132. -#define INVPCID_TYPE_ALL_NON_GLOBAL 3
  133. -
  134. -/* Flush all mappings for a given pcid and addr, not including globals. */
  135. -static inline void invpcid_flush_one(unsigned long pcid,
  136. - unsigned long addr)
  137. -{
  138. - __invpcid(pcid, addr, INVPCID_TYPE_INDIV_ADDR);
  139. -}
  140. -
  141. -/* Flush all mappings for a given PCID, not including globals. */
  142. -static inline void invpcid_flush_single_context(unsigned long pcid)
  143. -{
  144. - __invpcid(pcid, 0, INVPCID_TYPE_SINGLE_CTXT);
  145. -}
  146. -
  147. -/* Flush all mappings, including globals, for all PCIDs. */
  148. -static inline void invpcid_flush_all(void)
  149. -{
  150. - __invpcid(0, 0, INVPCID_TYPE_ALL_INCL_GLOBAL);
  151. -}
  152. -
  153. -/* Flush all mappings for all PCIDs except globals. */
  154. -static inline void invpcid_flush_all_nonglobals(void)
  155. -{
  156. - __invpcid(0, 0, INVPCID_TYPE_ALL_NON_GLOBAL);
  157. -}
  158. +#include <asm/invpcid.h>
  159. static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
  160. {
  161. --
  162. 2.14.2