0209-x86-mm-Use-Fix-PCID-to-optimize-user-kernel-switches.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. From 99351a96543de29896fdc6e8a41fb60ae97b18e1 Mon Sep 17 00:00:00 2001
  2. From: Peter Zijlstra <[email protected]>
  3. Date: Mon, 4 Dec 2017 15:07:59 +0100
  4. Subject: [PATCH 209/241] x86/mm: Use/Fix PCID to optimize user/kernel switches
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. CVE-2017-5754
  9. We can use PCID to retain the TLBs across CR3 switches; including those now
  10. part of the user/kernel switch. This increases performance of kernel
  11. entry/exit at the cost of more expensive/complicated TLB flushing.
  12. Now that we have two address spaces, one for kernel and one for user space,
  13. we need two PCIDs per mm. We use the top PCID bit to indicate a user PCID
  14. (just like we use the PFN LSB for the PGD). Since we do TLB invalidation
  15. from kernel space, the existing code will only invalidate the kernel PCID,
  16. we augment that by marking the corresponding user PCID invalid, and upon
  17. switching back to userspace, use a flushing CR3 write for the switch.
  18. In order to access the user_pcid_flush_mask we use PER_CPU storage, which
  19. means the previously established SWAPGS vs CR3 ordering is now mandatory
  20. and required.
  21. Having to do this memory access does require additional registers, most
  22. sites have a functioning stack and we can spill one (RAX), sites without
  23. functional stack need to otherwise provide the second scratch register.
  24. Note: PCID is generally available on Intel Sandybridge and later CPUs.
  25. Note: Up until this point TLB flushing was broken in this series.
  26. Based-on-code-from: Dave Hansen <[email protected]>
  27. Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
  28. Signed-off-by: Thomas Gleixner <[email protected]>
  29. Cc: Andy Lutomirski <[email protected]>
  30. Cc: Boris Ostrovsky <[email protected]>
  31. Cc: Borislav Petkov <[email protected]>
  32. Cc: Brian Gerst <[email protected]>
  33. Cc: Dave Hansen <[email protected]>
  34. Cc: David Laight <[email protected]>
  35. Cc: Denys Vlasenko <[email protected]>
  36. Cc: Eduardo Valentin <[email protected]>
  37. Cc: Greg KH <[email protected]>
  38. Cc: H. Peter Anvin <[email protected]>
  39. Cc: Josh Poimboeuf <[email protected]>
  40. Cc: Juergen Gross <[email protected]>
  41. Cc: Linus Torvalds <[email protected]>
  42. Cc: Peter Zijlstra <[email protected]>
  43. Cc: Will Deacon <[email protected]>
  44. Cc: [email protected]
  45. Cc: [email protected]
  46. Cc: [email protected]
  47. Cc: [email protected]
  48. Signed-off-by: Ingo Molnar <[email protected]>
  49. (backported from commit 6fd166aae78c0ab738d49bda653cbd9e3b1491cf)
  50. Signed-off-by: Andy Whitcroft <[email protected]>
  51. Signed-off-by: Kleber Sacilotto de Souza <[email protected]>
  52. (cherry picked from commit ac7471365d49c0a91d4b63453eb848cc19f17589)
  53. Signed-off-by: Fabian Grünbichler <[email protected]>
  54. ---
  55. arch/x86/entry/calling.h | 72 ++++++++++++++++++-----
  56. arch/x86/include/asm/processor-flags.h | 5 ++
  57. arch/x86/include/asm/tlbflush.h | 91 +++++++++++++++++++++++++----
  58. arch/x86/include/uapi/asm/processor-flags.h | 7 ++-
  59. arch/x86/kernel/asm-offsets.c | 4 ++
  60. arch/x86/mm/init.c | 2 +-
  61. arch/x86/mm/tlb.c | 1 +
  62. arch/x86/entry/entry_64.S | 9 +--
  63. arch/x86/entry/entry_64_compat.S | 4 +-
  64. 9 files changed, 162 insertions(+), 33 deletions(-)
  65. diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
  66. index bb56f5346ae8..ce5fb309926d 100644
  67. --- a/arch/x86/entry/calling.h
  68. +++ b/arch/x86/entry/calling.h
  69. @@ -2,6 +2,9 @@
  70. #include <asm/unwind_hints.h>
  71. #include <asm/cpufeatures.h>
  72. #include <asm/page_types.h>
  73. +#include <asm/percpu.h>
  74. +#include <asm/asm-offsets.h>
  75. +#include <asm/processor-flags.h>
  76. /*
  77. @@ -190,17 +193,21 @@ For 32-bit we have the following conventions - kernel is built with
  78. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  79. -/* PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two halves: */
  80. -#define PTI_SWITCH_MASK (1<<PAGE_SHIFT)
  81. +/*
  82. + * PAGE_TABLE_ISOLATION PGDs are 8k. Flip bit 12 to switch between the two
  83. + * halves:
  84. + */
  85. +#define PTI_SWITCH_PGTABLES_MASK (1<<PAGE_SHIFT)
  86. +#define PTI_SWITCH_MASK (PTI_SWITCH_PGTABLES_MASK|(1<<X86_CR3_PTI_SWITCH_BIT))
  87. -.macro ADJUST_KERNEL_CR3 reg:req
  88. - /* Clear "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  89. - andq $(~PTI_SWITCH_MASK), \reg
  90. +.macro SET_NOFLUSH_BIT reg:req
  91. + bts $X86_CR3_PCID_NOFLUSH_BIT, \reg
  92. .endm
  93. -.macro ADJUST_USER_CR3 reg:req
  94. - /* Move CR3 up a page to the user page tables: */
  95. - orq $(PTI_SWITCH_MASK), \reg
  96. +.macro ADJUST_KERNEL_CR3 reg:req
  97. + ALTERNATIVE "", "SET_NOFLUSH_BIT \reg", X86_FEATURE_PCID
  98. + /* Clear PCID and "PAGE_TABLE_ISOLATION bit", point CR3 at kernel pagetables: */
  99. + andq $(~PTI_SWITCH_MASK), \reg
  100. .endm
  101. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  102. @@ -211,21 +218,58 @@ For 32-bit we have the following conventions - kernel is built with
  103. .Lend_\@:
  104. .endm
  105. -.macro SWITCH_TO_USER_CR3 scratch_reg:req
  106. +#define THIS_CPU_user_pcid_flush_mask \
  107. + PER_CPU_VAR(cpu_tlbstate) + TLB_STATE_user_pcid_flush_mask
  108. +
  109. +.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  110. ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
  111. mov %cr3, \scratch_reg
  112. - ADJUST_USER_CR3 \scratch_reg
  113. +
  114. + ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
  115. +
  116. + /*
  117. + * Test if the ASID needs a flush.
  118. + */
  119. + movq \scratch_reg, \scratch_reg2
  120. + andq $(0x7FF), \scratch_reg /* mask ASID */
  121. + bt \scratch_reg, THIS_CPU_user_pcid_flush_mask
  122. + jnc .Lnoflush_\@
  123. +
  124. + /* Flush needed, clear the bit */
  125. + btr \scratch_reg, THIS_CPU_user_pcid_flush_mask
  126. + movq \scratch_reg2, \scratch_reg
  127. + jmp .Lwrcr3_\@
  128. +
  129. +.Lnoflush_\@:
  130. + movq \scratch_reg2, \scratch_reg
  131. + SET_NOFLUSH_BIT \scratch_reg
  132. +
  133. +.Lwrcr3_\@:
  134. + /* Flip the PGD and ASID to the user version */
  135. + orq $(PTI_SWITCH_MASK), \scratch_reg
  136. mov \scratch_reg, %cr3
  137. .Lend_\@:
  138. .endm
  139. +.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  140. + pushq %rax
  141. + SWITCH_TO_USER_CR3_NOSTACK scratch_reg=\scratch_reg scratch_reg2=%rax
  142. + popq %rax
  143. +.endm
  144. +
  145. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  146. ALTERNATIVE "jmp .Ldone_\@", "", X86_FEATURE_PTI
  147. movq %cr3, \scratch_reg
  148. movq \scratch_reg, \save_reg
  149. /*
  150. - * Is the switch bit zero? This means the address is
  151. - * up in real PAGE_TABLE_ISOLATION patches in a moment.
  152. + * Is the "switch mask" all zero? That means that both of
  153. + * these are zero:
  154. + *
  155. + * 1. The user/kernel PCID bit, and
  156. + * 2. The user/kernel "bit" that points CR3 to the
  157. + * bottom half of the 8k PGD
  158. + *
  159. + * That indicates a kernel CR3 value, not a user CR3.
  160. */
  161. testq $(PTI_SWITCH_MASK), \scratch_reg
  162. jz .Ldone_\@
  163. @@ -250,7 +294,9 @@ For 32-bit we have the following conventions - kernel is built with
  164. .macro SWITCH_TO_KERNEL_CR3 scratch_reg:req
  165. .endm
  166. -.macro SWITCH_TO_USER_CR3 scratch_reg:req
  167. +.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
  168. +.endm
  169. +.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
  170. .endm
  171. .macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req
  172. .endm
  173. diff --git a/arch/x86/include/asm/processor-flags.h b/arch/x86/include/asm/processor-flags.h
  174. index 791b60199aa4..fb9708d13761 100644
  175. --- a/arch/x86/include/asm/processor-flags.h
  176. +++ b/arch/x86/include/asm/processor-flags.h
  177. @@ -36,6 +36,11 @@
  178. #define CR3_ADDR_MASK 0x7FFFFFFFFFFFF000ull
  179. #define CR3_PCID_MASK 0xFFFull
  180. #define CR3_NOFLUSH (1UL << 63)
  181. +
  182. +#ifdef CONFIG_PAGE_TABLE_ISOLATION
  183. +# define X86_CR3_PTI_SWITCH_BIT 11
  184. +#endif
  185. +
  186. #else
  187. /*
  188. * CR3_ADDR_MASK needs at least bits 31:5 set on PAE systems, and we save
  189. diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
  190. index 3769ce182eac..2b7b32c243f1 100644
  191. --- a/arch/x86/include/asm/tlbflush.h
  192. +++ b/arch/x86/include/asm/tlbflush.h
  193. @@ -9,6 +9,8 @@
  194. #include <asm/special_insns.h>
  195. #include <asm/smp.h>
  196. #include <asm/invpcid.h>
  197. +#include <asm/pti.h>
  198. +#include <asm/processor-flags.h>
  199. static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
  200. {
  201. @@ -23,24 +25,54 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
  202. /* There are 12 bits of space for ASIDS in CR3 */
  203. #define CR3_HW_ASID_BITS 12
  204. +
  205. /*
  206. * When enabled, PAGE_TABLE_ISOLATION consumes a single bit for
  207. * user/kernel switches
  208. */
  209. -#define PTI_CONSUMED_ASID_BITS 0
  210. +#ifdef CONFIG_PAGE_TABLE_ISOLATION
  211. +# define PTI_CONSUMED_PCID_BITS 1
  212. +#else
  213. +# define PTI_CONSUMED_PCID_BITS 0
  214. +#endif
  215. +
  216. +#define CR3_AVAIL_PCID_BITS (X86_CR3_PCID_BITS - PTI_CONSUMED_PCID_BITS)
  217. -#define CR3_AVAIL_ASID_BITS (CR3_HW_ASID_BITS - PTI_CONSUMED_ASID_BITS)
  218. /*
  219. * ASIDs are zero-based: 0->MAX_AVAIL_ASID are valid. -1 below to account
  220. * for them being zero-based. Another -1 is because ASID 0 is reserved for
  221. * use by non-PCID-aware users.
  222. */
  223. -#define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_ASID_BITS) - 2)
  224. +#define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_PCID_BITS) - 2)
  225. +
  226. +/*
  227. + * 6 because 6 should be plenty and struct tlb_state will fit in two cache
  228. + * lines.
  229. + */
  230. +#define TLB_NR_DYN_ASIDS 6
  231. static inline u16 kern_pcid(u16 asid)
  232. {
  233. VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
  234. +
  235. +#ifdef CONFIG_PAGE_TABLE_ISOLATION
  236. + /*
  237. + * Make sure that the dynamic ASID space does not confict with the
  238. + * bit we are using to switch between user and kernel ASIDs.
  239. + */
  240. + BUILD_BUG_ON(TLB_NR_DYN_ASIDS >= (1 << X86_CR3_PTI_SWITCH_BIT));
  241. +
  242. /*
  243. + * The ASID being passed in here should have respected the
  244. + * MAX_ASID_AVAILABLE and thus never have the switch bit set.
  245. + */
  246. + VM_WARN_ON_ONCE(asid & (1 << X86_CR3_PTI_SWITCH_BIT));
  247. +#endif
  248. + /*
  249. + * The dynamically-assigned ASIDs that get passed in are small
  250. + * (<TLB_NR_DYN_ASIDS). They never have the high switch bit set,
  251. + * so do not bother to clear it.
  252. + *
  253. * If PCID is on, ASID-aware code paths put the ASID+1 into the
  254. * PCID bits. This serves two purposes. It prevents a nasty
  255. * situation in which PCID-unaware code saves CR3, loads some other
  256. @@ -85,12 +117,6 @@ static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
  257. */
  258. DECLARE_STATIC_KEY_TRUE(tlb_use_lazy_mode);
  259. -/*
  260. - * 6 because 6 should be plenty and struct tlb_state will fit in
  261. - * two cache lines.
  262. - */
  263. -#define TLB_NR_DYN_ASIDS 6
  264. -
  265. struct tlb_context {
  266. u64 ctx_id;
  267. u64 tlb_gen;
  268. @@ -135,6 +161,13 @@ struct tlb_state {
  269. */
  270. bool invalidate_other;
  271. + /*
  272. + * Mask that contains TLB_NR_DYN_ASIDS+1 bits to indicate
  273. + * the corresponding user PCID needs a flush next time we
  274. + * switch to it; see SWITCH_TO_USER_CR3.
  275. + */
  276. + unsigned short user_pcid_flush_mask;
  277. +
  278. /*
  279. * Access to this CR4 shadow and to H/W CR4 is protected by
  280. * disabling interrupts when modifying either one.
  281. @@ -238,15 +271,42 @@ static inline void cr4_set_bits_and_update_boot(unsigned long mask)
  282. }
  283. +/*
  284. + * Given an ASID, flush the corresponding user ASID. We can delay this
  285. + * until the next time we switch to it.
  286. + *
  287. + * See SWITCH_TO_USER_CR3.
  288. + */
  289. +static inline void invalidate_user_asid(u16 asid)
  290. +{
  291. + /* There is no user ASID if address space separation is off */
  292. + if (!IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
  293. + return;
  294. +
  295. + /*
  296. + * We only have a single ASID if PCID is off and the CR3
  297. + * write will have flushed it.
  298. + */
  299. + if (!cpu_feature_enabled(X86_FEATURE_PCID))
  300. + return;
  301. +
  302. + if (!static_cpu_has(X86_FEATURE_PTI))
  303. + return;
  304. +
  305. + __set_bit(kern_pcid(asid),
  306. + (unsigned long *)this_cpu_ptr(&cpu_tlbstate.user_pcid_flush_mask));
  307. +}
  308. +
  309. /*
  310. * flush the entire current user mapping
  311. */
  312. static inline void __native_flush_tlb(void)
  313. {
  314. + invalidate_user_asid(this_cpu_read(cpu_tlbstate.loaded_mm_asid));
  315. /*
  316. - * If current->mm == NULL then we borrow a mm which may change during a
  317. - * task switch and therefore we must not be preempted while we write CR3
  318. - * back:
  319. + * If current->mm == NULL then we borrow a mm which may change
  320. + * during a task switch and therefore we must not be preempted
  321. + * while we write CR3 back:
  322. */
  323. preempt_disable();
  324. native_write_cr3(__native_read_cr3());
  325. @@ -290,7 +350,14 @@ static inline void __native_flush_tlb_global(void)
  326. */
  327. static inline void __native_flush_tlb_single(unsigned long addr)
  328. {
  329. + u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
  330. +
  331. asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
  332. +
  333. + if (!static_cpu_has(X86_FEATURE_PTI))
  334. + return;
  335. +
  336. + invalidate_user_asid(loaded_mm_asid);
  337. }
  338. /*
  339. diff --git a/arch/x86/include/uapi/asm/processor-flags.h b/arch/x86/include/uapi/asm/processor-flags.h
  340. index 39946d0a1d41..69077da3dbf1 100644
  341. --- a/arch/x86/include/uapi/asm/processor-flags.h
  342. +++ b/arch/x86/include/uapi/asm/processor-flags.h
  343. @@ -77,7 +77,12 @@
  344. #define X86_CR3_PWT _BITUL(X86_CR3_PWT_BIT)
  345. #define X86_CR3_PCD_BIT 4 /* Page Cache Disable */
  346. #define X86_CR3_PCD _BITUL(X86_CR3_PCD_BIT)
  347. -#define X86_CR3_PCID_MASK _AC(0x00000fff,UL) /* PCID Mask */
  348. +
  349. +#define X86_CR3_PCID_BITS 12
  350. +#define X86_CR3_PCID_MASK (_AC((1UL << X86_CR3_PCID_BITS) - 1, UL))
  351. +
  352. +#define X86_CR3_PCID_NOFLUSH_BIT 63 /* Preserve old PCID */
  353. +#define X86_CR3_PCID_NOFLUSH _BITULL(X86_CR3_PCID_NOFLUSH_BIT)
  354. /*
  355. * Intel CPU features in CR4
  356. diff --git a/arch/x86/kernel/asm-offsets.c b/arch/x86/kernel/asm-offsets.c
  357. index 25b4832e9c28..87c3bafcef2c 100644
  358. --- a/arch/x86/kernel/asm-offsets.c
  359. +++ b/arch/x86/kernel/asm-offsets.c
  360. @@ -16,6 +16,7 @@
  361. #include <asm/sigframe.h>
  362. #include <asm/bootparam.h>
  363. #include <asm/suspend.h>
  364. +#include <asm/tlbflush.h>
  365. #ifdef CONFIG_XEN
  366. #include <xen/interface/xen.h>
  367. @@ -93,6 +94,9 @@ void common(void) {
  368. BLANK();
  369. DEFINE(PTREGS_SIZE, sizeof(struct pt_regs));
  370. + /* TLB state for the entry code */
  371. + OFFSET(TLB_STATE_user_pcid_flush_mask, tlb_state, user_pcid_flush_mask);
  372. +
  373. /* Layout info for cpu_entry_area */
  374. OFFSET(CPU_ENTRY_AREA_tss, cpu_entry_area, tss);
  375. OFFSET(CPU_ENTRY_AREA_entry_trampoline, cpu_entry_area, entry_trampoline);
  376. diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
  377. index af75069fb116..caeb8a7bf0a4 100644
  378. --- a/arch/x86/mm/init.c
  379. +++ b/arch/x86/mm/init.c
  380. @@ -855,7 +855,7 @@ void __init zone_sizes_init(void)
  381. free_area_init_nodes(max_zone_pfns);
  382. }
  383. -DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate) = {
  384. +__visible DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate) = {
  385. .loaded_mm = &init_mm,
  386. .next_asid = 1,
  387. .cr4 = ~0UL, /* fail hard if we screw up cr4 shadow initialization */
  388. diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
  389. index 353f2f4e1d96..06f3854d0a4f 100644
  390. --- a/arch/x86/mm/tlb.c
  391. +++ b/arch/x86/mm/tlb.c
  392. @@ -106,6 +106,7 @@ static void load_new_mm_cr3(pgd_t *pgdir, u16 new_asid, bool need_flush)
  393. unsigned long new_mm_cr3;
  394. if (need_flush) {
  395. + invalidate_user_asid(new_asid);
  396. new_mm_cr3 = build_cr3(pgdir, new_asid);
  397. } else {
  398. new_mm_cr3 = build_cr3_noflush(pgdir, new_asid);
  399. diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
  400. index 292ccc6ec48d..fb43f14ed299 100644
  401. --- a/arch/x86/entry/entry_64.S
  402. +++ b/arch/x86/entry/entry_64.S
  403. @@ -22,7 +22,6 @@
  404. #include <asm/segment.h>
  405. #include <asm/cache.h>
  406. #include <asm/errno.h>
  407. -#include "calling.h"
  408. #include <asm/asm-offsets.h>
  409. #include <asm/msr.h>
  410. #include <asm/unistd.h>
  411. @@ -39,6 +38,8 @@
  412. #include <asm/frame.h>
  413. #include <linux/err.h>
  414. +#include "calling.h"
  415. +
  416. .code64
  417. .section .entry.text, "ax"
  418. @@ -405,7 +406,7 @@ syscall_return_via_sysret:
  419. * We are on the trampoline stack. All regs except RDI are live.
  420. * We can do future final exit work right here.
  421. */
  422. - SWITCH_TO_USER_CR3 scratch_reg=%rdi
  423. + SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  424. popq %rdi
  425. popq %rsp
  426. @@ -743,7 +744,7 @@ GLOBAL(swapgs_restore_regs_and_return_to_usermode)
  427. * We can do future final exit work right here.
  428. */
  429. - SWITCH_TO_USER_CR3 scratch_reg=%rdi
  430. + SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  431. /* Restore RDI. */
  432. popq %rdi
  433. @@ -856,7 +857,7 @@ native_irq_return_ldt:
  434. */
  435. orq PER_CPU_VAR(espfix_stack), %rax
  436. - SWITCH_TO_USER_CR3 scratch_reg=%rdi /* to user CR3 */
  437. + SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
  438. SWAPGS /* to user GS */
  439. popq %rdi /* Restore user RDI */
  440. diff --git a/arch/x86/entry/entry_64_compat.S b/arch/x86/entry/entry_64_compat.S
  441. index 43f856aeee67..973527e34887 100644
  442. --- a/arch/x86/entry/entry_64_compat.S
  443. +++ b/arch/x86/entry/entry_64_compat.S
  444. @@ -274,9 +274,9 @@ sysret32_from_system_call:
  445. * switch until after after the last reference to the process
  446. * stack.
  447. *
  448. - * %r8 is zeroed before the sysret, thus safe to clobber.
  449. + * %r8/%r9 are zeroed before the sysret, thus safe to clobber.
  450. */
  451. - SWITCH_TO_USER_CR3 scratch_reg=%r8
  452. + SWITCH_TO_USER_CR3_NOSTACK scratch_reg=%r8 scratch_reg2=%r9
  453. xorq %r8, %r8
  454. xorq %r9, %r9
  455. --
  456. 2.14.2