Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1157795yba; Tue, 2 Apr 2019 03:34:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqyqmuL7fSTyodAKsTd5mjy/R0VsrmrGs8As5YX1fmcJvEEEg4aN7OpgrNt13+ss2wJzX7tK X-Received: by 2002:a63:fc43:: with SMTP id r3mr47964712pgk.44.1554201278709; Tue, 02 Apr 2019 03:34:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554201278; cv=none; d=google.com; s=arc-20160816; b=Q2b0vWkM60lUJJa1oy5aDVn9xFUeXTJPitnA6eSil3eBrCkFpn9vmx8xHI6wRIdpaT 9YhtlqUUYKvFNt1tDZqmCyHlVHov5jy/S5WNIyXe9zT9ckXLQUOdHWj0b5BNGUdFktb3 dcni29p9aTAZOAMMrQ4LQCxSjevZ1H80YmCP5w/4AmdHx8hrNasFIzADvHmZmvTsUOM5 nSPsONkSGiqDxBQBEGsYF9l1QvrT9t1h9g6KRXAvPRUIspMQpzV/AP+F3ARjSD/C1o7B EhctWUBNuE6uBEuDwrJ47cizjuNPhi0jcMkYw0tjGwS8xFdj8Qndql7o8L6YIk8386Y4 nirg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=dDQCgAen02JGrsmxQpEvAo2k/MLKzX4iTDeLc+5sUT8=; b=TgvVg+gvTTJ3bgmNt5z5+LsPr6AWe+agL2Y8aDu03C7WVkiaoV+PN8Hk9p/js8kjcP BTTQgkYq4cJ1g4CjtLqgyL/GYakfaYKhU/oTMWMlc955TMVsPOSxuC9XP25YNJsTRlPS DD5HUkv6eTr8Rfe6MzynkeCQdmLMz2dJleIasfFz3Z8WXj7gz4+b5BWOc/poletq3+By h1+boo4sbiYhmSc5bw6Yy1c1VfJR3RUUwnreyaM99ZZLJXpCUHvkmARTWZtgAVKs9xOp XWytoGJEOUNbCS48sxXZ0QzBarOMi+PCYG4HbTVy9RT82MY2+2KO06lx+XCWCgQ67FK1 UKBQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y9si10320073pgg.15.2019.04.02.03.34.23; Tue, 02 Apr 2019 03:34:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730081AbfDBKTu (ORCPT + 99 others); Tue, 2 Apr 2019 06:19:50 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:34388 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730065AbfDBKTt (ORCPT ); Tue, 2 Apr 2019 06:19:49 -0400 Received: from [5.158.153.52] (helo=nanos.tec.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1hBGWA-000233-N2; Tue, 02 Apr 2019 12:19:46 +0200 Date: Tue, 2 Apr 2019 12:19:46 +0200 (CEST) From: Thomas Gleixner To: LKML cc: x86@kernel.org, Andy Lutomirski , Josh Poimboeuf Subject: [patch 15/14] x86/dumpstack/64: Speedup in_exception_stack() In-Reply-To: <20190331215136.039902969@linutronix.de> Message-ID: References: <20190331214020.836098943@linutronix.de> <20190331215136.039902969@linutronix.de> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 with nasty macro magic 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: Thomas Gleixner --- arch/x86/kernel/dumpstack_64.c | 97 +++++++++++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 31 deletions(-) --- a/arch/x86/kernel/dumpstack_64.c +++ b/arch/x86/kernel/dumpstack_64.c @@ -48,50 +48,85 @@ const char *stack_type_name(enum stack_t return NULL; } -struct estack_layout { - unsigned int begin; - unsigned int end; +#define ESTACK_S(st) \ + (offsetof(struct cea_exception_stacks, st## _stack)) + +#define ESTACK_E(st) \ + (offsetof(struct cea_exception_stacks, st## _stack_guard)) + +#define PAGENR(offs) ((offs) / PAGE_SIZE) +#define PAGERANGE(st) PAGENR(ESTACK_S(st)) ... PAGENR(ESTACK_E(st) - 1) + +#if EXCEPTION_STKSZ == PAGE_SIZE +# define CONDRANGE(st) PAGENR(ESTACK_S(st)) +#else +# define CONDRANGE(st) PAGERANGE(st) +#endif + +/** + * struct estack_pages - Page descriptor for exception stacks + * @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) { \ - .begin = offsetof(struct cea_exception_stacks, x## _stack), \ - .end = offsetof(struct cea_exception_stacks, x## _stack_guard) \ +#define ESTACK_PAGE(ist, est) { \ + .offs = ESTACK_S(est), \ + .size = ESTACK_E(est) - ESTACK_S(est), \ + .type = STACK_TYPE_EXCEPTION + ist, \ } -static const struct estack_layout layout[N_EXCEPTION_STACKS] = { - [ DOUBLEFAULT_IST ] = ESTACK_ENTRY(DF), - [ NMI_IST ] = ESTACK_ENTRY(NMI), - [ DEBUG_IST ] = ESTACK_ENTRY(DB), - [ MCE_IST ] = ESTACK_ENTRY(MCE), +#define ESTACK_PAGES (sizeof(struct cea_exception_stacks) / PAGE_SIZE) + +/* + * Array of exception stack page descriptors. If the stack is larger than + * PAGE_SIZE, all pages covering a particular stack will have the same + * info. + */ +static const struct estack_pages estack_pages[ESTACK_PAGES] ____cacheline_aligned = { + [CONDRANGE(DF)] = ESTACK_PAGE(DOUBLEFAULT_IST, DF), + [CONDRANGE(NMI)] = ESTACK_PAGE(NMI_IST, NMI), + [PAGERANGE(DB)] = ESTACK_PAGE(DEBUG_IST, DB), + [CONDRANGE(MCE)] = ESTACK_PAGE(MCE_IST, MCE), }; 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; unsigned int k; BUILD_BUG_ON(N_EXCEPTION_STACKS != 4); - estacks = (unsigned long)__this_cpu_read(cea_exception_stacks); - - for (k = 0; k < N_EXCEPTION_STACKS; k++) { - begin = estacks + layout[k].begin; - end = estacks + layout[k].end; - regs = (struct pt_regs *)end - 1; - - if (stk <= begin || stk >= end) - continue; - - info->type = STACK_TYPE_EXCEPTION + k; - info->begin = (unsigned long *)begin; - info->end = (unsigned long *)end; - info->next_sp = (unsigned long *)regs->sp; - - return true; - } - - return false; + begin = (unsigned long)__this_cpu_read(cea_exception_stacks); + end = begin + sizeof(struct cea_exception_stacks); + /* Bail if @stack is outside the exception stack area. */ + if (stk <= begin || stk >= end) + return false; + + /* Calc page offset from start of exception stacks */ + k = (stk - begin) >> PAGE_SHIFT; + /* Lookup the page descriptor */ + ep = &estack_pages[k]; + /* Guard page? */ + if (unlikely(!ep->size)) + return false; + + begin += (unsigned long)ep->offs; + end = begin + (unsigned long)ep->size; + regs = (struct pt_regs *)end - 1; + + info->type = ep->type; + info->begin = (unsigned long *)begin; + info->end = (unsigned long *)end; + info->next_sp = (unsigned long *)regs->sp; + return true; } static bool in_irq_stack(unsigned long *stack, struct stack_info *info)