Skip to content

Commit 753c8d9

Browse files
committed
Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 fixes from Thomas Gleixner: "A collection of assorted fixes: - Fix for the pinned cr0/4 fallout which escaped all testing efforts because the kvm-intel module was never loaded when the kernel was compiled with CONFIG_PARAVIRT=n. The cr0/4 accessors are moved out of line and static key is now solely used in the core code and therefore can stay in the RO after init section. So the kvm-intel and other modules do not longer reference the (read only) static key which the module loader tried to update. - Prevent an infinite loop in arch_stack_walk_user() by breaking out of the loop once the return address is detected to be 0. - Prevent the int3_emulate_call() selftest from corrupting the stack when KASAN is enabled. KASASN clobbers more registers than covered by the emulated call implementation. Convert the int3_magic() selftest to a ASM function so the compiler cannot KASANify it. - Unbreak the build with old GCC versions and with the Gold linker by reverting the 'Move of _etext to the actual end of .text'. In both cases the build fails with 'Invalid absolute R_X86_64_32S relocation: _etext' - Initialize the context lock for init_mm, which was never an issue until the alternatives code started to use a temporary mm for patching. - Fix a build warning vs. the LOWMEM_PAGES constant where clang complains rightfully about a signed integer overflow in the shift operation by converting the operand to an ULL. - Adjust the misnamed ENDPROC() of common_spurious in the 32bit entry code" * 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/stacktrace: Prevent infinite loop in arch_stack_walk_user() x86/asm: Move native_write_cr0/4() out of line x86/pgtable/32: Fix LOWMEM_PAGES constant x86/alternatives: Fix int3_emulate_call() selftest stack corruption x86/entry/32: Fix ENDPROC of common_spurious Revert "x86/build: Move _etext to actual end of .text" x86/ldt: Initialize the context lock for init_mm
2 parents d7fe42a + cbf5b73 commit 753c8d9

11 files changed

Lines changed: 90 additions & 83 deletions

File tree

‎arch/x86/entry/entry_32.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ common_spurious:
11891189
movl %esp, %eax
11901190
call smp_spurious_interrupt
11911191
jmp ret_from_intr
1192-
ENDPROC(common_interrupt)
1192+
ENDPROC(common_spurious)
11931193
#endif
11941194

11951195
/*

‎arch/x86/include/asm/mmu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ typedef struct {
5959
#define INIT_MM_CONTEXT(mm) \
6060
.context = { \
6161
.ctx_id = 1, \
62+
.lock = __MUTEX_INITIALIZER(mm.context.lock), \
6263
}
6364

6465
void leave_mm(int cpu);

‎arch/x86/include/asm/pgtable_32.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ do { \
106106
* with only a host target support using a 32-bit type for internal
107107
* representation.
108108
*/
109-
#define LOWMEM_PAGES ((((2<<31) - __PAGE_OFFSET) >> PAGE_SHIFT))
109+
#define LOWMEM_PAGES ((((_ULL(2)<<31) - __PAGE_OFFSET) >> PAGE_SHIFT))
110110

111111
#endif /* _ASM_X86_PGTABLE_32_H */

‎arch/x86/include/asm/processor.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,7 @@ extern void load_direct_gdt(int);
741741
extern void load_fixmap_gdt(int);
742742
extern void load_percpu_segment(int);
743743
extern void cpu_init(void);
744+
extern void cr4_init(void);
744745

745746
static inline unsigned long get_debugctlmsr(void)
746747
{

‎arch/x86/include/asm/special_insns.h‎

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
*/
1919
extern unsigned long __force_order;
2020

21-
/* Starts false and gets enabled once CPU feature detection is done. */
22-
DECLARE_STATIC_KEY_FALSE(cr_pinning);
23-
extern unsigned long cr4_pinned_bits;
21+
void native_write_cr0(unsigned long val);
2422

2523
static inline unsigned long native_read_cr0(void)
2624
{
@@ -29,24 +27,6 @@ static inline unsigned long native_read_cr0(void)
2927
return val;
3028
}
3129

32-
static inline void native_write_cr0(unsigned long val)
33-
{
34-
unsigned long bits_missing = 0;
35-
36-
set_register:
37-
asm volatile("mov %0,%%cr0": "+r" (val), "+m" (__force_order));
38-
39-
if (static_branch_likely(&cr_pinning)) {
40-
if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
41-
bits_missing = X86_CR0_WP;
42-
val |= bits_missing;
43-
goto set_register;
44-
}
45-
/* Warn after we've set the missing bits. */
46-
WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
47-
}
48-
}
49-
5030
static inline unsigned long native_read_cr2(void)
5131
{
5232
unsigned long val;
@@ -91,24 +71,7 @@ static inline unsigned long native_read_cr4(void)
9171
return val;
9272
}
9373

94-
static inline void native_write_cr4(unsigned long val)
95-
{
96-
unsigned long bits_missing = 0;
97-
98-
set_register:
99-
asm volatile("mov %0,%%cr4": "+r" (val), "+m" (cr4_pinned_bits));
100-
101-
if (static_branch_likely(&cr_pinning)) {
102-
if (unlikely((val & cr4_pinned_bits) != cr4_pinned_bits)) {
103-
bits_missing = ~val & cr4_pinned_bits;
104-
val |= bits_missing;
105-
goto set_register;
106-
}
107-
/* Warn after we've set the missing bits. */
108-
WARN_ONCE(bits_missing, "CR4 bits went missing: %lx!?\n",
109-
bits_missing);
110-
}
111-
}
74+
void native_write_cr4(unsigned long val);
11275

11376
#ifdef CONFIG_X86_64
11477
static inline unsigned long native_read_cr8(void)

‎arch/x86/kernel/alternative.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,23 @@ extern struct paravirt_patch_site __start_parainstructions[],
625625
*
626626
* See entry_{32,64}.S for more details.
627627
*/
628-
static void __init int3_magic(unsigned int *ptr)
629-
{
630-
*ptr = 1;
631-
}
628+
629+
/*
630+
* We define the int3_magic() function in assembly to control the calling
631+
* convention such that we can 'call' it from assembly.
632+
*/
633+
634+
extern void int3_magic(unsigned int *ptr); /* defined in asm */
635+
636+
asm (
637+
" .pushsection .init.text, \"ax\", @progbits\n"
638+
" .type int3_magic, @function\n"
639+
"int3_magic:\n"
640+
" movl $1, (%" _ASM_ARG1 ")\n"
641+
" ret\n"
642+
" .size int3_magic, .-int3_magic\n"
643+
" .popsection\n"
644+
);
632645

633646
extern __initdata unsigned long int3_selftest_ip; /* defined in asm below */
634647

@@ -676,7 +689,9 @@ static void __init int3_selftest(void)
676689
"int3_selftest_ip:\n\t"
677690
__ASM_SEL(.long, .quad) " 1b\n\t"
678691
".popsection\n\t"
679-
: : __ASM_SEL_RAW(a, D) (&val) : "memory");
692+
: ASM_CALL_CONSTRAINT
693+
: __ASM_SEL_RAW(a, D) (&val)
694+
: "memory");
680695

681696
BUG_ON(val != 1);
682697

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,62 @@ static __always_inline void setup_umip(struct cpuinfo_x86 *c)
366366
cr4_clear_bits(X86_CR4_UMIP);
367367
}
368368

369-
DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
370-
EXPORT_SYMBOL(cr_pinning);
371-
unsigned long cr4_pinned_bits __ro_after_init;
372-
EXPORT_SYMBOL(cr4_pinned_bits);
369+
static DEFINE_STATIC_KEY_FALSE_RO(cr_pinning);
370+
static unsigned long cr4_pinned_bits __ro_after_init;
371+
372+
void native_write_cr0(unsigned long val)
373+
{
374+
unsigned long bits_missing = 0;
375+
376+
set_register:
377+
asm volatile("mov %0,%%cr0": "+r" (val), "+m" (__force_order));
378+
379+
if (static_branch_likely(&cr_pinning)) {
380+
if (unlikely((val & X86_CR0_WP) != X86_CR0_WP)) {
381+
bits_missing = X86_CR0_WP;
382+
val |= bits_missing;
383+
goto set_register;
384+
}
385+
/* Warn after we've set the missing bits. */
386+
WARN_ONCE(bits_missing, "CR0 WP bit went missing!?\n");
387+
}
388+
}
389+
EXPORT_SYMBOL(native_write_cr0);
390+
391+
void native_write_cr4(unsigned long val)
392+
{
393+
unsigned long bits_missing = 0;
394+
395+
set_register:
396+
asm volatile("mov %0,%%cr4": "+r" (val), "+m" (cr4_pinned_bits));
397+
398+
if (static_branch_likely(&cr_pinning)) {
399+
if (unlikely((val & cr4_pinned_bits) != cr4_pinned_bits)) {
400+
bits_missing = ~val & cr4_pinned_bits;
401+
val |= bits_missing;
402+
goto set_register;
403+
}
404+
/* Warn after we've set the missing bits. */
405+
WARN_ONCE(bits_missing, "CR4 bits went missing: %lx!?\n",
406+
bits_missing);
407+
}
408+
}
409+
EXPORT_SYMBOL(native_write_cr4);
410+
411+
void cr4_init(void)
412+
{
413+
unsigned long cr4 = __read_cr4();
414+
415+
if (boot_cpu_has(X86_FEATURE_PCID))
416+
cr4 |= X86_CR4_PCIDE;
417+
if (static_branch_likely(&cr_pinning))
418+
cr4 |= cr4_pinned_bits;
419+
420+
__write_cr4(cr4);
421+
422+
/* Initialize cr4 shadow for this CPU. */
423+
this_cpu_write(cpu_tlbstate.cr4, cr4);
424+
}
373425

374426
/*
375427
* Once CPU feature detection is finished (and boot params have been
@@ -1723,12 +1775,6 @@ void cpu_init(void)
17231775

17241776
wait_for_master_cpu(cpu);
17251777

1726-
/*
1727-
* Initialize the CR4 shadow before doing anything that could
1728-
* try to read it.
1729-
*/
1730-
cr4_init_shadow();
1731-
17321778
if (cpu)
17331779
load_ucode_ap();
17341780

@@ -1823,12 +1869,6 @@ void cpu_init(void)
18231869

18241870
wait_for_master_cpu(cpu);
18251871

1826-
/*
1827-
* Initialize the CR4 shadow before doing anything that could
1828-
* try to read it.
1829-
*/
1830-
cr4_init_shadow();
1831-
18321872
show_ucode_info_early();
18331873

18341874
pr_info("Initializing CPU#%d\n", cpu);

‎arch/x86/kernel/smpboot.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,28 +210,16 @@ static int enable_start_cpu0;
210210
*/
211211
static void notrace start_secondary(void *unused)
212212
{
213-
unsigned long cr4 = __read_cr4();
214-
215213
/*
216214
* Don't put *anything* except direct CPU state initialization
217215
* before cpu_init(), SMP booting is too fragile that we want to
218216
* limit the things done here to the most necessary things.
219217
*/
220-
if (boot_cpu_has(X86_FEATURE_PCID))
221-
cr4 |= X86_CR4_PCIDE;
222-
if (static_branch_likely(&cr_pinning))
223-
cr4 |= cr4_pinned_bits;
224-
225-
__write_cr4(cr4);
218+
cr4_init();
226219

227220
#ifdef CONFIG_X86_32
228221
/* switch away from the initial page table */
229222
load_cr3(swapper_pg_dir);
230-
/*
231-
* Initialize the CR4 shadow before doing anything that could
232-
* try to read it.
233-
*/
234-
cr4_init_shadow();
235223
__flush_tlb_all();
236224
#endif
237225
load_current_idt();

‎arch/x86/kernel/stacktrace.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,9 @@ void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
129129
break;
130130
if ((unsigned long)fp < regs->sp)
131131
break;
132-
if (frame.ret_addr) {
133-
if (!consume_entry(cookie, frame.ret_addr, false))
134-
return;
135-
}
136-
if (fp == frame.next_fp)
132+
if (!frame.ret_addr)
133+
break;
134+
if (!consume_entry(cookie, frame.ret_addr, false))
137135
break;
138136
fp = frame.next_fp;
139137
}

‎arch/x86/kernel/vmlinux.lds.S‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ SECTIONS
141141
*(.text.__x86.indirect_thunk)
142142
__indirect_thunk_end = .;
143143
#endif
144-
} :text = 0x9090
145144

146-
/* End of text section */
147-
_etext = .;
145+
/* End of text section */
146+
_etext = .;
147+
} :text = 0x9090
148148

149149
NOTES :text :note
150150

0 commit comments

Comments
 (0)