Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2004135pxa; Mon, 3 Aug 2020 05:09:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyV8zRuaqNZcNjdO9PvGWMwwTPX/G5VoogLcdZmVFLMTSdg73XlrXn4OJs3GfmXongDCBca X-Received: by 2002:a17:906:840c:: with SMTP id n12mr15700645ejx.246.1596456568021; Mon, 03 Aug 2020 05:09:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596456568; cv=none; d=google.com; s=arc-20160816; b=eWPggOWf5IsJeTTdDHizfD7C8mf0UWkF6mR+sJT1HpK6IWyi2BIDOlHMUkfBSXJ+j9 /smLRstEIQH3z0tV9vJ4t/uHzxcvT5M2QWkDUocr/vsyWRyvuV+LwZjpqGsqrZEIvU2T 4fn3MuqXhiOOz41x1XupGnlt7V7lyTA+qqu+O5xSsPg0DN3ZiYr9Vbk3ZGu3WCgZtq1Z pnq7M9o9AlQv05OWpipbVO8R90dBBfIAC44T5IsA4KmeJa6ozpFxt12nNxZ05zcC0pSy 4PxvTgJsxWiwFD2zmQmEAJANhoFf7C03pPBj8wiEQFtTh65NFw8GPH7r/F7HNZ/Yz6vs 7lQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:cms-type:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=2WBgkLxsDeTPNmtuUxW8D2JTNPrc4cq3+DknlqZ+k0M=; b=IECQ5xmQdLZBV/maGYlq8Wt07UzlACzESbMnEKPD5LCsmpIXWEcuGW8H1v2CXJ0znn r+2m5DrF0S3W16ZkdC522ad9S3EXicA5oUGGWeTbhZAXYJVb8SSTQYHHhLzAgq0HkCkl RJpVbXmvZhSvhrswSlaNEljB0CtlKfUtuuVnqRfXTSfHCoihJeU7QBA9JtZLlTqwDv/7 Anb94L60rqzIKjWgdNHJn+4VlwJy/Rdto6/7Hs2YRjLdQ6KZhKkvdY2GwEifCn9ai6Wo AplqzXR9oGUiXQ/G9rL77AeurXoa3KtotgY8+Grqlf113KIjALS/PBB9R8SS7mDGWcAt yiaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=I4z5p8hI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id do21si6981077ejc.92.2020.08.03.05.09.05; Mon, 03 Aug 2020 05:09:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=I4z5p8hI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726775AbgHCMIY (ORCPT + 99 others); Mon, 3 Aug 2020 08:08:24 -0400 Received: from mailout4.samsung.com ([203.254.224.34]:53238 "EHLO mailout4.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726394AbgHCMIT (ORCPT ); Mon, 3 Aug 2020 08:08:19 -0400 Received: from epcas5p3.samsung.com (unknown [182.195.41.41]) by mailout4.samsung.com (KnoxPortal) with ESMTP id 20200803120815epoutp04d4c81d6740e0bd5c8e26c342d135ab10~nwLl04AZY0999609996epoutp04U; Mon, 3 Aug 2020 12:08:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout4.samsung.com 20200803120815epoutp04d4c81d6740e0bd5c8e26c342d135ab10~nwLl04AZY0999609996epoutp04U DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1596456495; bh=2WBgkLxsDeTPNmtuUxW8D2JTNPrc4cq3+DknlqZ+k0M=; h=From:To:Cc:Subject:Date:References:From; b=I4z5p8hIGDOhENavDCg3Lb+U2nwpNCgMJbCcT2Oo9XZJyhdNlIENaBtYYxnLuFbWY 52nxjhyn+vBVebz+9ohdY06QPT913KCSdFf9WZV2KoknGVa7SlkfeAFZ6Gmm0VxrG8 Jy3UO3nI7/NelfDDmC1J+sl9S7qoQk79KW8woss0= Received: from epsmges5p1new.samsung.com (unknown [182.195.42.73]) by epcas5p4.samsung.com (KnoxPortal) with ESMTP id 20200803120814epcas5p46afbcf9a96442d7ed620110adac61d45~nwLk7fkfy1252012520epcas5p4N; Mon, 3 Aug 2020 12:08:14 +0000 (GMT) Received: from epcas5p2.samsung.com ( [182.195.41.40]) by epsmges5p1new.samsung.com (Symantec Messaging Gateway) with SMTP id 5E.4D.09467.E2EF72F5; Mon, 3 Aug 2020 21:08:14 +0900 (KST) Received: from epsmtrp2.samsung.com (unknown [182.195.40.14]) by epcas5p3.samsung.com (KnoxPortal) with ESMTPA id 20200802165825epcas5p3a2127be681530fdd785db0f8961eaf96~ngfp0TD2F0804708047epcas5p3y; Sun, 2 Aug 2020 16:58:25 +0000 (GMT) Received: from epsmgms1p2.samsung.com (unknown [182.195.42.42]) by epsmtrp2.samsung.com (KnoxPortal) with ESMTP id 20200802165825epsmtrp223281b42c2c5ee489579389e6073e44b~ngfpy8fOh2950229502epsmtrp2U; Sun, 2 Aug 2020 16:58:25 +0000 (GMT) X-AuditID: b6c32a49-a3fff700000024fb-23-5f27fe2ec2f4 Received: from epsmtip2.samsung.com ( [182.195.34.31]) by epsmgms1p2.samsung.com (Symantec Messaging Gateway) with SMTP id 1E.A7.08303.1B0F62F5; Mon, 3 Aug 2020 01:58:25 +0900 (KST) Received: from localhost.localdomain (unknown [107.108.92.210]) by epsmtip2.samsung.com (KnoxPortal) with ESMTPA id 20200802165819epsmtip240ae74d9e1a0130cf77f0e043f0612a6~ngfkZE_zL2814028140epsmtip2e; Sun, 2 Aug 2020 16:58:19 +0000 (GMT) From: Maninder Singh To: catalin.marinas@arm.com, will@kernel.org, oleg@redhat.com, keescook@chromium.org, arnd@arndb.de, gregkh@linuxfoundation.org, adobriyan@gmail.com, rostedt@goodmis.org, mingo@redhat.com, steve.capper@arm.com, mark.rutland@arm.com, vincenzo.frascino@arm.com, anshuman.khandual@arm.com, ardb@kernel.org, james.morse@arm.com, broonie@kernel.org, maz@kernel.org, kristina.martsenko@arm.com, samitolvanen@google.com, ebiederm@xmission.com, akpm@linux-foundation.org, gladkov.alexey@gmail.com, daniel.m.jordan@oracle.com, walken@google.com, bernd.edlinger@hotmail.de, laoar.shao@gmail.com, avagin@gmail.com, john.johansen@canonical.com Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, a.sahrawat@samsung.com, Maninder Singh , Vaneet narang Subject: [PATCH 1/1] arm64: add support for PAGE_SIZE aligned kernel stack Date: Sun, 2 Aug 2020 22:05:15 +0530 Message-Id: <1596386115-47228-1-git-send-email-maninder1.s@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA0WSf0xTVxTHva+v7z06qw807gpBRg06mwmaKbt2ottcwtMlizPEJUytjbwh G6VdCxtbMFSpZCAolJ8iImBB1pXVdbVUfhQCWmDCtrIVB5PAAktVnLSUIULErTzM/vuc8/3e 7zknuRQvKI8IppJSUllViixZRAhwa9eWV7dGLm6Wbpu3bkTOFhaVnT6AKk1GAtl7TqCnM1MA PdM5SNT3/CEPfVPiwlHJnxMEmqrPA6hitpxEz61aEtVMfYCyrpoINGq/hqGembME6jsnR6Pd 2Xx0zX2PRObxQT5qbevF0a/NlQTqupKNo7q7TgzdrOzlo4GOagzdaD8LkP3rMQzVNfzNR7pH Vgx19l4GqG3xKY4WdaeQ9t7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Generator: CA Content-Type: text/plain; charset="utf-8" X-Sendblock-Type: REQ_APPROVE CMS-TYPE: 105P X-CMS-RootMailID: 20200802165825epcas5p3a2127be681530fdd785db0f8961eaf96 References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org currently THREAD_SIZE is always in power of 2, which will waste memory in cases there is need to increase of stack size. Thus adding support for PAGE_SIZE(not power of 2) stacks for arm64. User can decide any value 12KB, 16KB, 20 KB etc. based on value of THREAD_SHIFT. User can set any value which is PAGE_SIZE aligned for PAGE_ALIGNED_STACK_SIZE config. Value of THREAD_SIZE is defined as 12KB for now, since with irq stacks it is enough and it will save 4KB per thread. IRQ stack size is not changed and alignement of IRQ stack and kernel stack is maintained same to catch stack overflow faults as earlier. THREAD_SIZE masking in common files is changed to THREAD_SIZE_ALIGNED. Co-developed-by: Vaneet narang Signed-off-by: Vaneet narang Signed-off-by: Maninder Singh --- arch/arm64/Kconfig | 9 +++++++++ arch/arm64/include/asm/memory.h | 29 +++++++++++++++++++++++++---- arch/arm64/kernel/entry.S | 4 ++-- arch/arm64/kernel/ptrace.c | 4 ++-- drivers/misc/lkdtm/stackleak.c | 2 +- fs/proc/base.c | 4 ++-- include/linux/thread_info.h | 4 ++++ kernel/trace/trace_stack.c | 4 ++-- 8 files changed, 47 insertions(+), 13 deletions(-) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index c970171..301e068 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -977,6 +977,15 @@ config NODES_SHIFT Specify the maximum number of NUMA Nodes available on the target system. Increases memory reserved to accommodate various tables. +config PAGE_ALIGNED_STACK_SIZE + int "set per thread stack size (THREAD_SIZE)" + default 12288 + depends on VMAP_STACK && ARM64_4K_PAGES && !KASAN + help + Per Thread stack size, value must be PAGE_SIZE aligned. + make sure value should be less than (1 << THREAD_SHIFT), + otherwise increase THREAD_SHIFT also. + config USE_PERCPU_NUMA_NODE_ID def_bool y depends on NUMA diff --git a/arch/arm64/include/asm/memory.h b/arch/arm64/include/asm/memory.h index 5767836..597071e 100644 --- a/arch/arm64/include/asm/memory.h +++ b/arch/arm64/include/asm/memory.h @@ -93,6 +93,7 @@ */ #if defined(CONFIG_VMAP_STACK) && (MIN_THREAD_SHIFT < PAGE_SHIFT) #define THREAD_SHIFT PAGE_SHIFT +#define THREAD_SIZE (UL(1) << THREAD_SHIFT) #else #define THREAD_SHIFT MIN_THREAD_SHIFT #endif @@ -101,7 +102,15 @@ #define THREAD_SIZE_ORDER (THREAD_SHIFT - PAGE_SHIFT) #endif -#define THREAD_SIZE (UL(1) << THREAD_SHIFT) +#define THREAD_SIZE_ALIGNED (UL(1) << THREAD_SHIFT) + +#ifndef THREAD_SIZE +#if defined(CONFIG_VMAP_STACK) && (CONFIG_PAGE_ALIGNED_STACK_SIZE) +#define THREAD_SIZE CONFIG_PAGE_ALIGNED_STACK_SIZE +#else +#define THREAD_SIZE THREAD_SIZE_ALIGNED +#endif +#endif /* * By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by @@ -109,12 +118,24 @@ * assembly. */ #ifdef CONFIG_VMAP_STACK -#define THREAD_ALIGN (2 * THREAD_SIZE) +#define THREAD_ALIGN (2 * THREAD_SIZE_ALIGNED) #else -#define THREAD_ALIGN THREAD_SIZE +#define THREAD_ALIGN THREAD_SIZE_ALIGNED +#endif + +#ifdef CONFIG_PAGE_ALIGNED_STACK_SIZE + +#if (THREAD_SIZE_ALIGNED < THREAD_SIZE) +#error "PAGE_ALIGNED_STACK_SIZE is more than THREAD_SIZE_ALIGNED, increase THREAD_SHIFT" +#endif + +#if (THREAD_SIZE % PAGE_SIZE) +#error "PAGE_ALIGNED_STACK_SIZE must be PAGE_SIZE align" +#endif + #endif -#define IRQ_STACK_SIZE THREAD_SIZE +#define IRQ_STACK_SIZE THREAD_SIZE_ALIGNED #define OVERFLOW_STACK_SIZE SZ_4K diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S index 13458c2..5190573 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -444,12 +444,12 @@ alternative_insn eret, nop, ARM64_UNMAP_KERNEL_AT_EL0 /* * Compare sp with the base of the task stack. - * If the top ~(THREAD_SIZE - 1) bits match, we are on a task stack, + * If the top ~(THREAD_SIZE_ALIGNED - 1) bits match, we are on a task stack, * and should switch to the irq stack. */ ldr x25, [tsk, TSK_STACK] eor x25, x25, x19 - and x25, x25, #~(THREAD_SIZE - 1) + and x25, x25, #~(THREAD_SIZE_ALIGNED - 1) cbnz x25, 9998f ldr_this_cpu x25, irq_stack_ptr, x26 diff --git a/arch/arm64/kernel/ptrace.c b/arch/arm64/kernel/ptrace.c index b82eb50..800bb84 100644 --- a/arch/arm64/kernel/ptrace.c +++ b/arch/arm64/kernel/ptrace.c @@ -120,8 +120,8 @@ int regs_query_register_offset(const char *name) */ static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) { - return ((addr & ~(THREAD_SIZE - 1)) == - (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) || + return ((addr & ~(THREAD_SIZE_ALIGNED - 1)) == + (kernel_stack_pointer(regs) & ~(THREAD_SIZE_ALIGNED - 1))) || on_irq_stack(addr, NULL); } diff --git a/drivers/misc/lkdtm/stackleak.c b/drivers/misc/lkdtm/stackleak.c index d1a5c07..f4ab60a 100644 --- a/drivers/misc/lkdtm/stackleak.c +++ b/drivers/misc/lkdtm/stackleak.c @@ -24,7 +24,7 @@ void lkdtm_STACKLEAK_ERASING(void) */ sp = PTR_ALIGN(&i, sizeof(unsigned long)); - left = ((unsigned long)sp & (THREAD_SIZE - 1)) / sizeof(unsigned long); + left = ((unsigned long)sp & (THREAD_SIZE_ALIGNED - 1)) / sizeof(unsigned long); sp--; /* diff --git a/fs/proc/base.c b/fs/proc/base.c index f3b6e12..f89e2c5 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3135,9 +3135,9 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task) { unsigned long prev_depth = THREAD_SIZE - - (task->prev_lowest_stack & (THREAD_SIZE - 1)); + (task->prev_lowest_stack & (THREAD_SIZE_ALIGNED - 1)); unsigned long depth = THREAD_SIZE - - (task->lowest_stack & (THREAD_SIZE - 1)); + (task->lowest_stack & (THREAD_SIZE_ALIGNED - 1)); seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n", prev_depth, depth); diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h index e93e249..35a73b5 100644 --- a/include/linux/thread_info.h +++ b/include/linux/thread_info.h @@ -43,6 +43,10 @@ enum { #define THREAD_ALIGN THREAD_SIZE #endif +#ifndef THREAD_SIZE_ALIGNED +#define THREAD_SIZE_ALIGNED THREAD_SIZE +#endif + #define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO) /* diff --git a/kernel/trace/trace_stack.c b/kernel/trace/trace_stack.c index 5810fb8..ef3d442 100644 --- a/kernel/trace/trace_stack.c +++ b/kernel/trace/trace_stack.c @@ -159,7 +159,7 @@ static void check_stack(unsigned long ip, unsigned long *stack) int frame_size = READ_ONCE(tracer_frame); int i, x; - this_size = ((unsigned long)stack) & (THREAD_SIZE-1); + this_size = ((unsigned long)stack) & (THREAD_SIZE_ALIGNED - 1); this_size = THREAD_SIZE - this_size; /* Remove the frame of the tracer */ this_size -= frame_size; @@ -211,7 +211,7 @@ static void check_stack(unsigned long ip, unsigned long *stack) x = 0; start = stack; top = (unsigned long *) - (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE); + (((unsigned long)start & ~(THREAD_SIZE_ALIGNED - 1)) + THREAD_SIZE); /* * Loop through all the entries. One of the entries may -- 1.9.1