Commit c450c8f5 authored by Thomas Gleixner's avatar Thomas Gleixner Committed by Borislav Petkov

x86/dumpstack/64: Speedup in_exception_stack()

The current implementation of in_exception_stack() iterates over the
exception stacks array. Most of the time this is an useless exercise, but
even for the actual use cases (perf and ftrace) it takes at least 2
iterations to get to the NMI stack.

As the exception stacks and the guard pages are page aligned the loop can
be avoided completely.

Add a initial check whether the stack pointer is inside the full exception
stack area and leave early if not.

Create a lookup table which describes the stack area. The table index is
the page offset from the beginning of the exception stacks. So for any
given stack pointer the page offset is computed and a lookup in the
description table is performed. If it is inside a guard page, return. If
not, use the descriptor to fill in the info structure.

The table is filled at compile time and for the !KASAN case the interesting
page descriptors exactly fit into a single cache line. Just the last guard
page descriptor is in the next cacheline, but that should not be accessed
in the regular case.
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Signed-off-by: default avatarBorislav Petkov <bp@suse.de>
Acked-by: default avatarJosh Poimboeuf <jpoimboe@redhat.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Sean Christopherson <sean.j.christopherson@intel.com>
Cc: x86-ml <x86@kernel.org>
Link: https://lkml.kernel.org/r/20190414160145.543320386@linutronix.de
parent 2a594d4c
...@@ -50,52 +50,72 @@ const char *stack_type_name(enum stack_type type) ...@@ -50,52 +50,72 @@ const char *stack_type_name(enum stack_type type)
return NULL; return NULL;
} }
struct estack_layout { /**
unsigned int begin; * struct estack_pages - Page descriptor for exception stacks
unsigned int end; * @offs: Offset from the start of the exception stack area
* @size: Size of the exception stack
* @type: Type to store in the stack_info struct
*/
struct estack_pages {
u32 offs;
u16 size;
u16 type;
}; };
#define ESTACK_ENTRY(x) { \ #define EPAGERANGE(st) \
.begin = offsetof(struct cea_exception_stacks, x## _stack), \ [PFN_DOWN(CEA_ESTACK_OFFS(st)) ... \
.end = offsetof(struct cea_exception_stacks, x## _stack_guard) \ PFN_DOWN(CEA_ESTACK_OFFS(st) + CEA_ESTACK_SIZE(st) - 1)] = { \
} .offs = CEA_ESTACK_OFFS(st), \
.size = CEA_ESTACK_SIZE(st), \
.type = STACK_TYPE_EXCEPTION + ESTACK_ ##st, }
static const struct estack_layout layout[] = { /*
[ ESTACK_DF ] = ESTACK_ENTRY(DF), * Array of exception stack page descriptors. If the stack is larger than
[ ESTACK_NMI ] = ESTACK_ENTRY(NMI), * PAGE_SIZE, all pages covering a particular stack will have the same
[ ESTACK_DB2 ] = { .begin = 0, .end = 0}, * info. The guard pages including the not mapped DB2 stack are zeroed
[ ESTACK_DB1 ] = ESTACK_ENTRY(DB1), * out.
[ ESTACK_DB ] = ESTACK_ENTRY(DB), */
[ ESTACK_MCE ] = ESTACK_ENTRY(MCE), static const
struct estack_pages estack_pages[CEA_ESTACK_PAGES] ____cacheline_aligned = {
EPAGERANGE(DF),
EPAGERANGE(NMI),
EPAGERANGE(DB1),
EPAGERANGE(DB),
EPAGERANGE(MCE),
}; };
static bool in_exception_stack(unsigned long *stack, struct stack_info *info) static bool in_exception_stack(unsigned long *stack, struct stack_info *info)
{ {
unsigned long estacks, begin, end, stk = (unsigned long)stack; unsigned long begin, end, stk = (unsigned long)stack;
const struct estack_pages *ep;
struct pt_regs *regs; struct pt_regs *regs;
unsigned int k; unsigned int k;
BUILD_BUG_ON(N_EXCEPTION_STACKS != 6); BUILD_BUG_ON(N_EXCEPTION_STACKS != 6);
estacks = (unsigned long)__this_cpu_read(cea_exception_stacks); begin = (unsigned long)__this_cpu_read(cea_exception_stacks);
end = begin + sizeof(struct cea_exception_stacks);
for (k = 0; k < N_EXCEPTION_STACKS; k++) { /* Bail if @stack is outside the exception stack area. */
begin = estacks + layout[k].begin; if (stk < begin || stk >= end)
end = estacks + layout[k].end; return false;
regs = (struct pt_regs *)end - 1;
if (stk < begin || stk >= end) /* Calc page offset from start of exception stacks */
continue; k = (stk - begin) >> PAGE_SHIFT;
/* Lookup the page descriptor */
ep = &estack_pages[k];
/* Guard page? */
if (!ep->size)
return false;
info->type = STACK_TYPE_EXCEPTION + k; begin += (unsigned long)ep->offs;
info->begin = (unsigned long *)begin; end = begin + (unsigned long)ep->size;
info->end = (unsigned long *)end; regs = (struct pt_regs *)end - 1;
info->next_sp = (unsigned long *)regs->sp;
return true; info->type = ep->type;
} info->begin = (unsigned long *)begin;
info->end = (unsigned long *)end;
return false; info->next_sp = (unsigned long *)regs->sp;
return true;
} }
static bool in_irq_stack(unsigned long *stack, struct stack_info *info) static bool in_irq_stack(unsigned long *stack, struct stack_info *info)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment