From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.43 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796089980; b=R4kW72xXgL8LfrccAIMyONDUzTWYmNjJTUkIa8ulOq3C3IA1fgIKirE22mYXfxcT7iLy hpvb+ysTt5w/qwJMU3W93bcI/SmaLf2OhcAkcsPNJvcqi+v6y+8jEWzz+y0fn1klGgFmZ 2lnNUpBX6By6A4hjnbta+CtB0qQ47hZljXEJuwGUlRGaR41n1Qu79q+cETJj0J2pgo4bq q4daZBhTnYlERqceRcy9Gs3M34ta1u6DZw2oR7J6IwxtvkaW6z0puOaD3sdthayl6VPu2 txwIv8aa3vUHK7/ZWNwYTw3KDjRc/HluNnQjPM4YGLiraZKiHWOOkZJ/xHtHG7okySw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796089980; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=UOk8WqKw3dNzDIhyg79OJ5O9B11pdpwZkNvz2sz4XAI=; b=SMpnIIU4azcYnlmws428ztXHkH4QhapXvc/z3YWDviDeoAoRD0PZByZVzosyKOZfO5x1 NuXX13y0jhQhCI6bW6Bk/54jq029suBygS79gqpu0Mq0fGFXEsWhS7szdLabzNbkXU850 QTZ/ICveGRk/wLyTKuJDgYaUWF8Xoiq79rBdebLo9SeuPdc+XSkP5hweVjIz2wwZgHli7 rocwxM6/+99v+iCDdCVs9UmYmb4ubeVBV3yfy1QZhx/LpFUOmaWQgw+vAH/IxDOpH2g1C 8SOLGosUoGNElbRGaedxEOuPYP+5t9ubY+8A9EsDwZSxwS6RN9eq6RYzvMc8QwyyKEQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fb+1e2OW; arc=none smtp.remote-ip=209.85.128.43 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA34B606A325 for ; Tue, 1 Dec 2026 01:52:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AA34B606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fb+1e2OW Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-4bb61185057so70918735e9.1 for ; Mon, 30 Nov 2026 17:52:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796089978; x=1796694778; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=UOk8WqKw3dNzDIhyg79OJ5O9B11pdpwZkNvz2sz4XAI=; b=fb+1e2OWUDaxROIZtD1RY8U9L+D1GnK+yA68RYcpo/loXwoD5FnX+rTacJZ8qe+Zxb MyN7W7XRPqSph69gdzznEdjS3waBwlqxFnZcX0WRg3ThyZ/lSqzD96XGxMC+xhkUeoK2 zRKXNe3jl9AqDZjU9F4pKPRv0w1dzzuceLWK9Rg9IYk4z0R7IqXswRXCTEDvuG1fjZyv 16HWIL61KmvmmkVGinhu7spxdhwkG4grFC2XGHGnonNAGPfsDSOEp9KS9iKg6cGhMJ16 f8oV8215YEGCMJq++xqvQrdxwxpZlMkWS6joYU3iVYCCGCIq8u7l+7aKjPrRt4CVBqUs BC6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796089978; x=1796694778; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=UOk8WqKw3dNzDIhyg79OJ5O9B11pdpwZkNvz2sz4XAI=; b=Hju879mne0PBj7SmFqPN8sG+h1YTFevGGfkEiiEq4vnlcrMXTIYOYhupAoD1G+sGUJ o9g6bta4eq2o2e/TGuCppdlOv+ok+GlpSo6SBdA56+WR5P7vc7HxQEMt2b+LgEIgfYHb dNyjWgntL9q/IYX+QZAhCuUJD871M7QQ47V8hrMd3elFUPhtz2CSI8/ZLN2B8rKLMuAZ 86gX1oEIReS5vK5sc8HBqlzBre6dZJIm4+dANYCwJR2RM9Ei2Gxes69CtxFPwk4BTtJv tSNKSq4Y5koQyK7jmIa1rc5H3r+v9h/Wo1SyCJoV13Fiv9E3C3Ada5ReLk5u24tmGEH1 blXg== X-Forwarded-Encrypted: i=1; AL+hErGtfR1CG7iWhnl+Z/03Tviv1mLK1AQh8rDPpES4NBT5ULAOHGwFa5QHmS0wkktmN9xNy5Sk0kNF3lriNbE=@vger.kernel.org X-Gm-Message-State: APuU4Z91nurbr/s2S17M5C553/oPJtBswX9g+hA4Z3zrqLxCnYS/aC3h tF8RnmSJkU9HFmLwI/Wqe+8c+3SZpNwcdaS0tNMlO4xx3+0jj1X/DUH3 X-Gm-Gg: AZFdTZcrf3AX7uYIJTufFhCqBgSrvMN/NoDDRenwmqkktEqHXvMhOSWHWShfM5nE4qb N/QP5fEVyIPquGboPj4n0e23VtXEujzb5nFvdDZWncf4GOpB9sgtf4lU3wSYDoEXVkjx5Kt8YBs Yxl7FvAJC/Q2n8U1ZKzOYeMwRce0SJLkMHD86+lqHjuDERhEw9z7pbquofuHeTSHhvdszaoiFj5 Mohme/CQPol9T8XgohJ2JJb4jQbHXuTUc1010a6vvS0FHa0Ik19qQcyONXK449nEF1Zgxk2FVgh 6IvupLwzdIaUlHhO02Z9dDKjtieVX90frVSA9qccu65+nmbjbTlcXmoQh0CGKgp4qbRTc23dumn /TEGBpZUcbP1YGa+++2rMQrkO07N0AQRbzfrAnkgeIqQCPwWWcDR3VXx95GZ4A8ajKtBO2U8znX TT+dvu1EfhN8T+kRiLwzS5e8sonm4GwmQ8N/mHHR6AYJCdVLxWgBC+xRfUV5t1grCIb/FGu+RQs kVN+AqsZRfyI80Hj6VXgXZEuGbtsBw+4RIZt/W0k0XsR0cv2sxYduY5ozNB5XY2nx5Em2WfB2Dg u3kuC5R9l384qR56xXtqgIu9W+W2i0Wo0iRLlw== X-Received: by 2002:a05:600c:8210:b0:4bb:6a2d:b438 with SMTP id 5b1f17b1804b1-4bb88786c81mr697835255e9.0.1796089977931; Mon, 30 Nov 2026 17:52:57 -0800 (PST) Received: from NULL.Home.net ([2001:8a0:7280:4000:12d:c3b9:cecc:8966]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb884911e3sm1199037055e9.6.2026.11.30.17.52.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 17:52:57 -0800 (PST) From: Eric Curtin To: linux-arm-kernel@lists.infradead.org Cc: Ryan Roberts , Vlastimil Babka , Arnd Bergmann , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , David Hildenbrand , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Kees Cook , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kasan-dev@googlegroups.com Subject: [RFC PATCH v2 07/43] fork: Permit boot-time THREAD_SIZE determination Date: Tue, 1 Dec 2026 01:51:39 +0000 Message-ID: <20261201015240.1017259-8-ericcurtin17@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261201015240.1017259-1-ericcurtin17@gmail.com> References: <20261201015240.1017259-1-ericcurtin17@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ryan Roberts THREAD_SIZE is the size of a kernel thread stack. It has been a compile-time constant, but with vmap stacks it must be a multiple of PAGE_SIZE, so with boot-time page size selection it can no longer be fixed at build time. Defining THREAD_SIZE for the largest supported page size would waste memory with smaller pages. For example, arm64 needs 16K stacks, but 64K pages with a vmap stack require 64K; paying that with 4K or 16K pages would waste 48K per kernel thread. So allow THREAD_SIZE to be a runtime value. Add THREAD_SIZE_MAX and THREAD_ALIGN_MAX, defaulting to THREAD_SIZE and max(THREAD_ALIGN, THREAD_SIZE_MAX), and use THREAD_SIZE_MAX to size init_stack, thread_union and the init stack in the linker script. In fork.c, replace the compile-time split between the page allocator and the kmem_cache based stack allocator with run-time checks of THREAD_SIZE >= PAGE_SIZE, and make thread_stack_cache_init() return early when stacks are page based. The weak stub in init/main.c is now conditional on CONFIG_VMAP_STACK. KASAN's init_task_stack_addr() uses THREAD_SIZE rather than sizeof() of the stack array. No functional change when THREAD_SIZE is a compile-time constant. Signed-off-by: Ryan Roberts Acked-by: Vlastimil Babka Signed-off-by: Eric Curtin (cherry picked from commit 13e7b56091910f3533cee539b7c1ef857f61fe4d) --- include/asm-generic/vmlinux.lds.h | 6 +++- include/linux/sched.h | 4 +-- include/linux/thread_info.h | 10 +++++- init/main.c | 2 +- kernel/fork.c | 53 ++++++++++--------------------- mm/kasan/report.c | 3 +- 6 files changed, 35 insertions(+), 43 deletions(-) diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index 2a86599cb620..4df6c8fd38cb 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -56,6 +56,10 @@ #define LOAD_OFFSET 0 #endif +#ifndef THREAD_SIZE_MAX +#define THREAD_SIZE_MAX THREAD_SIZE +#endif + /* * Only some architectures want to have the .notes segment visible in * a separate PT_NOTE ELF Program Header. When this happens, it needs @@ -428,7 +432,7 @@ init_thread_union = .; \ init_stack = .; \ KEEP(*(.data..init_thread_info)) \ - . = __start_init_stack + THREAD_SIZE; \ + . = __start_init_stack + THREAD_SIZE_MAX; \ __end_init_stack = .; #define JUMP_TABLE_DATA \ diff --git a/include/linux/sched.h b/include/linux/sched.h index e4f6323d7855..2b649bbd668a 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1998,14 +1998,14 @@ union thread_union { #ifndef CONFIG_THREAD_INFO_IN_TASK struct thread_info thread_info; #endif - unsigned long stack[THREAD_SIZE/sizeof(long)]; + unsigned long stack[THREAD_SIZE_MAX/sizeof(long)]; }; #ifndef CONFIG_THREAD_INFO_IN_TASK extern struct thread_info init_thread_info; #endif -extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)]; +extern unsigned long init_stack[THREAD_SIZE_MAX / sizeof(unsigned long)]; #ifdef CONFIG_THREAD_INFO_IN_TASK # define task_thread_info(task) (&(task)->thread_info) diff --git a/include/linux/thread_info.h b/include/linux/thread_info.h index 307b8390fc67..73d0a77a3c2c 100644 --- a/include/linux/thread_info.h +++ b/include/linux/thread_info.h @@ -89,7 +89,15 @@ static inline long set_restart_fn(struct restart_block *restart, } #ifndef THREAD_ALIGN -#define THREAD_ALIGN THREAD_SIZE +#define THREAD_ALIGN THREAD_SIZE +#endif + +#ifndef THREAD_SIZE_MAX +#define THREAD_SIZE_MAX THREAD_SIZE +#endif + +#ifndef THREAD_ALIGN_MAX +#define THREAD_ALIGN_MAX max(THREAD_ALIGN, THREAD_SIZE_MAX) #endif #define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_SKIP_KASAN) diff --git a/init/main.c b/init/main.c index 3d619be281aa..2a44990521eb 100644 --- a/init/main.c +++ b/init/main.c @@ -849,7 +849,7 @@ void __init __weak smp_prepare_boot_cpu(void) { } -# if THREAD_SIZE >= PAGE_SIZE +#ifdef CONFIG_VMAP_STACK void __init __weak thread_stack_cache_init(void) { } diff --git a/kernel/fork.c b/kernel/fork.c index 5a0db5a89615..87c102ea8d62 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -393,45 +393,15 @@ static void free_thread_stack(struct task_struct *tsk) * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a * kmemcache based allocator. */ -#if THREAD_SIZE >= PAGE_SIZE - -static void thread_stack_free_rcu(struct rcu_head *rh) -{ - __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER); -} - -static void thread_stack_delayed_free(struct task_struct *tsk) -{ - struct rcu_head *rh = tsk->stack; - - call_rcu(rh, thread_stack_free_rcu); -} - -static int alloc_thread_stack_node(struct task_struct *tsk, int node) -{ - struct page *page = alloc_pages_node(node, THREADINFO_GFP, - THREAD_SIZE_ORDER); - - if (likely(page)) { - tsk->stack = kasan_reset_tag(page_address(page)); - return 0; - } - return -ENOMEM; -} - -static void free_thread_stack(struct task_struct *tsk) -{ - thread_stack_delayed_free(tsk); - tsk->stack = NULL; -} - -#else /* !(THREAD_SIZE >= PAGE_SIZE) */ static struct kmem_cache *thread_stack_cache; static void thread_stack_free_rcu(struct rcu_head *rh) { - kmem_cache_free(thread_stack_cache, rh); + if (THREAD_SIZE >= PAGE_SIZE) + __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER); + else + kmem_cache_free(thread_stack_cache, rh); } static void thread_stack_delayed_free(struct task_struct *tsk) @@ -444,7 +414,16 @@ static void thread_stack_delayed_free(struct task_struct *tsk) static int alloc_thread_stack_node(struct task_struct *tsk, int node) { unsigned long *stack; - stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); + struct page *page; + + if (THREAD_SIZE >= PAGE_SIZE) { + page = alloc_pages_node(node, THREADINFO_GFP, THREAD_SIZE_ORDER); + stack = likely(page) ? page_address(page) : NULL; + } else { + stack = kmem_cache_alloc_node(thread_stack_cache, + THREADINFO_GFP, node); + } + stack = kasan_reset_tag(stack); tsk->stack = stack; return stack ? 0 : -ENOMEM; @@ -458,13 +437,15 @@ static void free_thread_stack(struct task_struct *tsk) void thread_stack_cache_init(void) { + if (THREAD_SIZE >= PAGE_SIZE) + return; + thread_stack_cache = kmem_cache_create_usercopy("thread_stack", THREAD_SIZE, THREAD_SIZE, 0, 0, THREAD_SIZE, NULL); BUG_ON(thread_stack_cache == NULL); } -#endif /* THREAD_SIZE >= PAGE_SIZE */ #endif /* CONFIG_VMAP_STACK */ /* SLAB cache for signal_struct structures (tsk->signal) */ diff --git a/mm/kasan/report.c b/mm/kasan/report.c index e804b1e1f886..7b170efd224b 100644 --- a/mm/kasan/report.c +++ b/mm/kasan/report.c @@ -366,8 +366,7 @@ static inline bool kernel_or_module_addr(const void *addr) static inline bool init_task_stack_addr(const void *addr) { return addr >= (void *)&init_thread_union.stack && - (addr <= (void *)&init_thread_union.stack + - sizeof(init_thread_union.stack)); + (addr <= (void *)&init_thread_union.stack + THREAD_SIZE); } static void print_address_description(void *addr, u8 tag,