Skip to content

Commit e37e43a

Browse files
amlutoIngo Molnar
authored andcommitted
x86/mm/64: Enable vmapped stacks (CONFIG_HAVE_ARCH_VMAP_STACK=y)
This allows x86_64 kernels to enable vmapped stacks by setting HAVE_ARCH_VMAP_STACK=y - which enables the CONFIG_VMAP_STACK=y high level Kconfig option. There are a couple of interesting bits: First, x86 lazily faults in top-level paging entries for the vmalloc area. This won't work if we get a page fault while trying to access the stack: the CPU will promote it to a double-fault and we'll die. To avoid this problem, probe the new stack when switching stacks and forcibly populate the pgd entry for the stack when switching mms. Second, once we have guard pages around the stack, we'll want to detect and handle stack overflow. I didn't enable it on x86_32. We'd need to rework the double-fault code a bit and I'm concerned about running out of vmalloc virtual addresses under some workloads. This patch, by itself, will behave somewhat erratically when the stack overflows while RSP is still more than a few tens of bytes above the bottom of the stack. Specifically, we'll get #PF and make it to no_context and them oops without reliably triggering a double-fault, and no_context doesn't know about stack overflows. The next patch will improve that case. Thank you to Nadav and Brian for helping me pay enough attention to the SDM to hopefully get this right. Signed-off-by: Andy Lutomirski <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Brian Gerst <[email protected]> Cc: Denys Vlasenko <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Nadav Amit <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: http://lkml.kernel.org/r/c88f3e2920b18e6cc621d772a04a62c06869037e.1470907718.git.luto@kernel.org [ Minor edits. ] Signed-off-by: Ingo Molnar <[email protected]>
1 parent b4a0f53 commit e37e43a

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ config X86
9494
select HAVE_ARCH_TRANSPARENT_HUGEPAGE
9595
select HAVE_ARCH_WITHIN_STACK_FRAMES
9696
select HAVE_EBPF_JIT if X86_64
97+
select HAVE_ARCH_VMAP_STACK if X86_64
9798
select HAVE_CC_STACKPROTECTOR
9899
select HAVE_CMPXCHG_DOUBLE
99100
select HAVE_CMPXCHG_LOCAL

arch/x86/include/asm/switch_to.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@ struct tss_struct;
88
void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
99
struct tss_struct *tss);
1010

11+
/* This runs runs on the previous thread's stack. */
12+
static inline void prepare_switch_to(struct task_struct *prev,
13+
struct task_struct *next)
14+
{
15+
#ifdef CONFIG_VMAP_STACK
16+
/*
17+
* If we switch to a stack that has a top-level paging entry
18+
* that is not present in the current mm, the resulting #PF will
19+
* will be promoted to a double-fault and we'll panic. Probe
20+
* the new stack now so that vmalloc_fault can fix up the page
21+
* tables if needed. This can only happen if we use a stack
22+
* in vmap space.
23+
*
24+
* We assume that the stack is aligned so that it never spans
25+
* more than one top-level paging entry.
26+
*
27+
* To minimize cache pollution, just follow the stack pointer.
28+
*/
29+
READ_ONCE(*(unsigned char *)next->thread.sp);
30+
#endif
31+
}
32+
1133
#ifdef CONFIG_X86_32
1234

1335
#ifdef CONFIG_CC_STACKPROTECTOR
@@ -39,6 +61,8 @@ do { \
3961
*/ \
4062
unsigned long ebx, ecx, edx, esi, edi; \
4163
\
64+
prepare_switch_to(prev, next); \
65+
\
4266
asm volatile("pushl %%ebp\n\t" /* save EBP */ \
4367
"movl %%esp,%[prev_sp]\n\t" /* save ESP */ \
4468
"movl %[next_sp],%%esp\n\t" /* restore ESP */ \
@@ -103,7 +127,9 @@ do { \
103127
* clean in kernel mode, with the possible exception of IOPL. Kernel IOPL
104128
* has no effect.
105129
*/
106-
#define switch_to(prev, next, last) \
130+
#define switch_to(prev, next, last) \
131+
prepare_switch_to(prev, next); \
132+
\
107133
asm volatile(SAVE_CONTEXT \
108134
"movq %%rsp,%P[threadrsp](%[prev])\n\t" /* save RSP */ \
109135
"movq %P[threadrsp](%[next]),%%rsp\n\t" /* restore RSP */ \

arch/x86/kernel/traps.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,30 @@ DO_ERROR(X86_TRAP_NP, SIGBUS, "segment not present", segment_not_present)
292292
DO_ERROR(X86_TRAP_SS, SIGBUS, "stack segment", stack_segment)
293293
DO_ERROR(X86_TRAP_AC, SIGBUS, "alignment check", alignment_check)
294294

295+
#ifdef CONFIG_VMAP_STACK
296+
static void __noreturn handle_stack_overflow(const char *message,
297+
struct pt_regs *regs,
298+
unsigned long fault_address)
299+
{
300+
printk(KERN_EMERG "BUG: stack guard page was hit at %p (stack is %p..%p)\n",
301+
(void *)fault_address, current->stack,
302+
(char *)current->stack + THREAD_SIZE - 1);
303+
die(message, regs, 0);
304+
305+
/* Be absolutely certain we don't return. */
306+
panic(message);
307+
}
308+
#endif
309+
295310
#ifdef CONFIG_X86_64
296311
/* Runs on IST stack */
297312
dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
298313
{
299314
static const char str[] = "double fault";
300315
struct task_struct *tsk = current;
316+
#ifdef CONFIG_VMAP_STACK
317+
unsigned long cr2;
318+
#endif
301319

302320
#ifdef CONFIG_X86_ESPFIX64
303321
extern unsigned char native_irq_return_iret[];
@@ -332,6 +350,49 @@ dotraplinkage void do_double_fault(struct pt_regs *regs, long error_code)
332350
tsk->thread.error_code = error_code;
333351
tsk->thread.trap_nr = X86_TRAP_DF;
334352

353+
#ifdef CONFIG_VMAP_STACK
354+
/*
355+
* If we overflow the stack into a guard page, the CPU will fail
356+
* to deliver #PF and will send #DF instead. Similarly, if we
357+
* take any non-IST exception while too close to the bottom of
358+
* the stack, the processor will get a page fault while
359+
* delivering the exception and will generate a double fault.
360+
*
361+
* According to the SDM (footnote in 6.15 under "Interrupt 14 -
362+
* Page-Fault Exception (#PF):
363+
*
364+
* Processors update CR2 whenever a page fault is detected. If a
365+
* second page fault occurs while an earlier page fault is being
366+
* deliv- ered, the faulting linear address of the second fault will
367+
* overwrite the contents of CR2 (replacing the previous
368+
* address). These updates to CR2 occur even if the page fault
369+
* results in a double fault or occurs during the delivery of a
370+
* double fault.
371+
*
372+
* The logic below has a small possibility of incorrectly diagnosing
373+
* some errors as stack overflows. For example, if the IDT or GDT
374+
* gets corrupted such that #GP delivery fails due to a bad descriptor
375+
* causing #GP and we hit this condition while CR2 coincidentally
376+
* points to the stack guard page, we'll think we overflowed the
377+
* stack. Given that we're going to panic one way or another
378+
* if this happens, this isn't necessarily worth fixing.
379+
*
380+
* If necessary, we could improve the test by only diagnosing
381+
* a stack overflow if the saved RSP points within 47 bytes of
382+
* the bottom of the stack: if RSP == tsk_stack + 48 and we
383+
* take an exception, the stack is already aligned and there
384+
* will be enough room SS, RSP, RFLAGS, CS, RIP, and a
385+
* possible error code, so a stack overflow would *not* double
386+
* fault. With any less space left, exception delivery could
387+
* fail, and, as a practical matter, we've overflowed the
388+
* stack even if the actual trigger for the double fault was
389+
* something else.
390+
*/
391+
cr2 = read_cr2();
392+
if ((unsigned long)task_stack_page(tsk) - 1 - cr2 < PAGE_SIZE)
393+
handle_stack_overflow("kernel stack overflow (double-fault)", regs, cr2);
394+
#endif
395+
335396
#ifdef CONFIG_DOUBLEFAULT
336397
df_debug(regs, error_code);
337398
#endif

arch/x86/mm/tlb.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,25 @@ void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
7777
unsigned cpu = smp_processor_id();
7878

7979
if (likely(prev != next)) {
80+
if (IS_ENABLED(CONFIG_VMAP_STACK)) {
81+
/*
82+
* If our current stack is in vmalloc space and isn't
83+
* mapped in the new pgd, we'll double-fault. Forcibly
84+
* map it.
85+
*/
86+
unsigned int stack_pgd_index = pgd_index(current_stack_pointer());
87+
88+
pgd_t *pgd = next->pgd + stack_pgd_index;
89+
90+
if (unlikely(pgd_none(*pgd)))
91+
set_pgd(pgd, init_mm.pgd[stack_pgd_index]);
92+
}
93+
8094
#ifdef CONFIG_SMP
8195
this_cpu_write(cpu_tlbstate.state, TLBSTATE_OK);
8296
this_cpu_write(cpu_tlbstate.active_mm, next);
8397
#endif
98+
8499
cpumask_set_cpu(cpu, mm_cpumask(next));
85100

86101
/*

0 commit comments

Comments
 (0)