Message ID | 20240301084046.3370376-1-xin@zytor.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-88096-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp941068dyb; Fri, 1 Mar 2024 00:42:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWyM8aC3hJiCZmD+wM+wrdj5XXbUNe/PXaqam15FXLur5YhAx6YNFQPM1E8qguxawee/lJpl1o04Rr/16CLs0RLwaY4ow== X-Google-Smtp-Source: AGHT+IG2+xAHtW4Q8V2rsVW3Svr9LNXUFlLj1UfLS95/Kuk5btKPXVgbWGWcdqihryPf1l23C8WE X-Received: by 2002:a05:6402:323:b0:566:4ba7:157e with SMTP id q3-20020a056402032300b005664ba7157emr786007edw.14.1709282542847; Fri, 01 Mar 2024 00:42:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709282542; cv=pass; d=google.com; s=arc-20160816; b=ogTyPuYQHlPGdJcr1crIkVy0yp5l1NZ35FiCmh35Ph+WKw8nEB4K+GdE32x3jfSvY0 gW5H9JEz7R3SPmbON1GpiFUDSeS4esW2tieGcpe9ZoZTX0Du3YOs5Qt5D+OiVo2kQyWT 2+/nlhZEhOKN7e2iZ5kUGDYFX4L35HIgpGkEFuPe3f2d9vRwZu7JQz7CM5JcBMFqqAMR xy+Rhh791CCgo5AZzn/z+8/ly8SQTgjXvK6SULT/GK1+PtvePp0J1Xw7ZIq1a8CgPt/P LFqjceEdx9qV49nEQeZcwX0ph3mubUnf736fxalKhi13OjO0z+i1+oraqt6CKG5/zWvO +ORg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter; bh=XHDUSdp3fzgtKbz1MB2i6kRiFtguTPR1L2wgsX/y370=; fh=i2KdxqsaXzwmrZcS/SvEO6y/qU8O9VI6x6zqJRa+1JI=; b=nk0+r6QsKNwo5V8Z94tsTP3a5y61pwYXe2eckM4toTXYXQFqqZPNLLY9dSD4+TDLPW a8ZwWbogAaqPwh3NzBYizE0wnc4paMMy3DqxoZJoAoNHpUWMHGiNkhzdME9unkCWivDb q2+Iw7cS4NpwDL2oVuGsT2cTm8ECegpp8qzGdyNQZVBrqTBqsOBXr8w8RCFZPp0OoQX1 xok8/aP9fqHXGnp72dONlEo1EtCSV3zHBeiwo2nmkjSM/CMQoafLkLvzO1wjxh+ubLZd 0Whg3AjIcXVoFH6AdElu+qUrWyZVAYFprruq1AnIq5+3V3hE2Q492UKM2Y6IMS70LAkH Jalg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@zytor.com header.s=2024021201 header.b="IgxvAT/b"; arc=pass (i=1 spf=pass spfdomain=zytor.com dkim=pass dkdomain=zytor.com dmarc=pass fromdomain=zytor.com); spf=pass (google.com: domain of linux-kernel+bounces-88096-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88096-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id l11-20020a056402254b00b005664dc1157esi1317307edb.143.2024.03.01.00.42.22 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 00:42:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88096-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@zytor.com header.s=2024021201 header.b="IgxvAT/b"; arc=pass (i=1 spf=pass spfdomain=zytor.com dkim=pass dkdomain=zytor.com dmarc=pass fromdomain=zytor.com); spf=pass (google.com: domain of linux-kernel+bounces-88096-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88096-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 6FDB71F2202C for <ouuuleilei@gmail.com>; Fri, 1 Mar 2024 08:42:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 90DF469D25; Fri, 1 Mar 2024 08:41:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b="IgxvAT/b" Received: from mail.zytor.com (terminus.zytor.com [198.137.202.136]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5710969E11 for <linux-kernel@vger.kernel.org>; Fri, 1 Mar 2024 08:41:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.136 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709282489; cv=none; b=PzQ7LGG2FOHYMZVS+jWSd8s5GTVGtMq0MGyVCpeiISQIOSQrrsP9VXLNXRqIVQurkx75MUrcOTNVbAqS64HrcXH3gHLC9ccPvi0CEXaoZ3QMDy/Co2qGRwVQ+0drsXsxVkxocAhRUZQWXfJ11f6ifMufe6wqu4NOnPOt679xIok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709282489; c=relaxed/simple; bh=ZU05d8rgL+KouMacXKQ7YsJZM3JuNUeEZVl84wyTLE0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=k5fhMW1Q6YN5O/LkwPL+HuBt/bAzx4NIrgPfDLFN6wcdvyg1BfcmfomMIPDfiVOKeEUk2CSagcY9ZXc+jS0NaLdDVVWGvmppu13LestTOzU3jnVY/oIaE15Hymj1PF6u8MMD8vujGi//LzGoPzBxWEwtIQIeqoi+ZBELTBNGWxw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com; spf=pass smtp.mailfrom=zytor.com; dkim=pass (2048-bit key) header.d=zytor.com header.i=@zytor.com header.b=IgxvAT/b; arc=none smtp.client-ip=198.137.202.136 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zytor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zytor.com Received: from terminus.zytor.com (terminus.zytor.com [IPv6:2607:7c80:54:3:0:0:0:136]) (authenticated bits=0) by mail.zytor.com (8.17.2/8.17.1) with ESMTPSA id 4218elje3370386 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Fri, 1 Mar 2024 00:40:51 -0800 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 4218elje3370386 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2024021201; t=1709282452; bh=XHDUSdp3fzgtKbz1MB2i6kRiFtguTPR1L2wgsX/y370=; h=From:To:Cc:Subject:Date:From; b=IgxvAT/b5pPtrkD2o+Qs/LXL4q5C7xWmeYT0I0Nsswgzi6K6V9wDP80kZ/ett1+/6 +c+7DO2i9jYYDi9XGNQSNs3Hm927ZUR5Sf25qCQaMdFJbJFjokI12uTTYQjxX0zL8F XFf9TuC+fsoEppfz2iSCyrnlc3CLGsfzmqHL3smI5W1EnNadAlpUHuKVYyblQQss2s y0rFAZTyJdE0y8qC7fbKiKIE6VyZp7202CS4p1rkn4luH/VlHEfsIL3VgzUeLByNRY wpSinfHmpQ2TNp3RfbklxovH3aRMMrPwVe8B/VcXrw09CQu5LVmrczq7UDMnzlaKhK OIPCFXhK7wfjQ== From: "Xin Li (Intel)" <xin@zytor.com> To: linux-kernel@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, brgerst@gmail.com Subject: [PATCH v1 1/1] x86/fred: Fix init_task thread stack pointer initialization Date: Fri, 1 Mar 2024 00:40:46 -0800 Message-ID: <20240301084046.3370376-1-xin@zytor.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792312651637710214 X-GMAIL-MSGID: 1792312651637710214 |
Series |
[v1,1/1] x86/fred: Fix init_task thread stack pointer initialization
|
|
Commit Message
Xin Li (Intel)
March 1, 2024, 8:40 a.m. UTC
As TOP_OF_KERNEL_STACK_PADDING is defined as 0 on x86_64, no one noticed
it's missing in the calculation of the .sp field in INIT_THREAD until it
is defined to 16 with CONFIG_X86_FRED=y.
Subtract TOP_OF_KERNEL_STACK_PADDING from the .sp field of INIT_THREAD.
Fixes: 65c9cc9e2c14 ("x86/fred: Reserve space for the FRED stack frame")
Fixes: 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit")
Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202402262159.183c2a37-lkp@intel.com
Signed-off-by: Xin Li (Intel) <xin@zytor.com>
---
arch/x86/include/asm/processor.h | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
base-commit: e13841907b8fda0ae0ce1ec03684665f578416a8
Comments
On Fri, Mar 1, 2024 at 3:41 AM Xin Li (Intel) <xin@zytor.com> wrote: > > As TOP_OF_KERNEL_STACK_PADDING is defined as 0 on x86_64, no one noticed > it's missing in the calculation of the .sp field in INIT_THREAD until it > is defined to 16 with CONFIG_X86_FRED=y. > > Subtract TOP_OF_KERNEL_STACK_PADDING from the .sp field of INIT_THREAD. > > Fixes: 65c9cc9e2c14 ("x86/fred: Reserve space for the FRED stack frame") > Fixes: 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit") > Reported-by: kernel test robot <oliver.sang@intel.com> > Closes: https://lore.kernel.org/oe-lkp/202402262159.183c2a37-lkp@intel.com > Signed-off-by: Xin Li (Intel) <xin@zytor.com> > --- > arch/x86/include/asm/processor.h | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h > index 26620d7642a9..17fe81998ce4 100644 > --- a/arch/x86/include/asm/processor.h > +++ b/arch/x86/include/asm/processor.h > @@ -664,8 +664,10 @@ static __always_inline void prefetchw(const void *x) > #else > extern unsigned long __end_init_task[]; > > -#define INIT_THREAD { \ > - .sp = (unsigned long)&__end_init_task - sizeof(struct pt_regs), \ > +#define INIT_THREAD { \ > + .sp = (unsigned long)&__end_init_task - \ > + TOP_OF_KERNEL_STACK_PADDING - \ > + sizeof(struct pt_regs), \ > } > > extern unsigned long KSTK_ESP(struct task_struct *task); > There is another spot in head_64.S that also needs this offset: /* Set up the stack for verify_cpu() */ leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp Brian Gerst
On 3/1/2024 5:15 AM, Brian Gerst wrote: > On Fri, Mar 1, 2024 at 3:41 AM Xin Li (Intel) <xin@zytor.com> wrote: > There is another spot in head_64.S that also needs this offset: I checked all references to __end_init_task before sending out this patch, and I doubt we need to make more similar changes. First of all, "movq TASK_threadsp(%rcx), %rsp" you added in 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit") is exactly what we need to set up %rsp for the init task. > /* Set up the stack for verify_cpu() */ > leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp As the comment says, it's a _temporary_ stack for calling verify_cpu() (but only for BSP, as APs use a different bring up stack), at which stage the concept of "task" has not formed. I'm thinking maybe it's better to do: /* Set up the stack for verify_cpu() */ leaq __end_init_task(%rip), %rsp Previously it was "leaq (__end_init_task - FRAME_SIZE)(%rip), %rsp", but the kernel unwinder goes up only to secondary_startup_64_no_verify() after the new way you introduced to set up %rsp for the init task, and it seems to me that there is no point to subtract FRAME_SIZE or PTREGS_SIZE. On the other hand, TOP_OF_KERNEL_STACK_PADDING is required for x86_32, but probably not for x86_64 (defined as 0 before FRED). The most important usage of TOP_OF_KERNEL_STACK_PADDING is to get the pt_regs pointer for a task, i.e., task_pt_regs(task), which assumes a fixed offset from the top of a task stack, but also limits the space that could be used by future hardware above the pt_regs structure. Thus I prefer to limit the usage of TOP_OF_KERNEL_STACK_PADDING on x86_64. Thanks! Xin
On Fri, Mar 1, 2024 at 11:18 PM Xin Li <xin@zytor.com> wrote: > > On 3/1/2024 5:15 AM, Brian Gerst wrote: > > On Fri, Mar 1, 2024 at 3:41 AM Xin Li (Intel) <xin@zytor.com> wrote: > > There is another spot in head_64.S that also needs this offset: > > I checked all references to __end_init_task before sending out this > patch, and I doubt we need to make more similar changes. > > First of all, "movq TASK_threadsp(%rcx), %rsp" you added in > 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit") is exactly > what we need to set up %rsp for the init task. > > > /* Set up the stack for verify_cpu() */ > > leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp > > As the comment says, it's a _temporary_ stack for calling verify_cpu() > (but only for BSP, as APs use a different bring up stack), at which > stage the concept of "task" has not formed. I'm thinking maybe it's > better to do: > > /* Set up the stack for verify_cpu() */ > leaq __end_init_task(%rip), %rsp > > Previously it was "leaq (__end_init_task - FRAME_SIZE)(%rip), %rsp", > but the kernel unwinder goes up only to secondary_startup_64_no_verify() > after the new way you introduced to set up %rsp for the init task, and > it seems to me that there is no point to subtract FRAME_SIZE or > PTREGS_SIZE. > > On the other hand, TOP_OF_KERNEL_STACK_PADDING is required for x86_32, > but probably not for x86_64 (defined as 0 before FRED). The most > important usage of TOP_OF_KERNEL_STACK_PADDING is to get the pt_regs > pointer for a task, i.e., task_pt_regs(task), which assumes a fixed > offset from the top of a task stack, but also limits the space that > could be used by future hardware above the pt_regs structure. Thus I > prefer to limit the usage of TOP_OF_KERNEL_STACK_PADDING on x86_64. The point is to keep consistency with other kernel threads, which have the pt_regs area cleared (see copy_thread()). In particular, the CS field can't have junk in it or else user_mode(regs) could return the wrong result. So the stack needs to start below pt_regs, or we need to explicitly zero pt_regs later. Ideally, the load from thread->sp should just shift RSP by phys_base, pointing to the same memory location in the virtual mapping. Brian Gerst
On 3/2/2024 5:20 AM, Brian Gerst wrote: > On Fri, Mar 1, 2024 at 11:18 PM Xin Li <xin@zytor.com> wrote: >> >> On 3/1/2024 5:15 AM, Brian Gerst wrote: >>> On Fri, Mar 1, 2024 at 3:41 AM Xin Li (Intel) <xin@zytor.com> wrote: >>> There is another spot in head_64.S that also needs this offset: >> >> I checked all references to __end_init_task before sending out this >> patch, and I doubt we need to make more similar changes. >> >> First of all, "movq TASK_threadsp(%rcx), %rsp" you added in >> 3adee777ad0d ("x86/smpboot: Remove initial_stack on 64-bit") is exactly >> what we need to set up %rsp for the init task. >> >>> /* Set up the stack for verify_cpu() */ >>> leaq (__end_init_task - PTREGS_SIZE)(%rip), %rsp >> >> As the comment says, it's a _temporary_ stack for calling verify_cpu() >> (but only for BSP, as APs use a different bring up stack), at which >> stage the concept of "task" has not formed. I'm thinking maybe it's >> better to do: >> >> /* Set up the stack for verify_cpu() */ >> leaq __end_init_task(%rip), %rsp >> >> Previously it was "leaq (__end_init_task - FRAME_SIZE)(%rip), %rsp", >> but the kernel unwinder goes up only to secondary_startup_64_no_verify() >> after the new way you introduced to set up %rsp for the init task, and >> it seems to me that there is no point to subtract FRAME_SIZE or >> PTREGS_SIZE. >> >> On the other hand, TOP_OF_KERNEL_STACK_PADDING is required for x86_32, >> but probably not for x86_64 (defined as 0 before FRED). The most >> important usage of TOP_OF_KERNEL_STACK_PADDING is to get the pt_regs >> pointer for a task, i.e., task_pt_regs(task), which assumes a fixed >> offset from the top of a task stack, but also limits the space that >> could be used by future hardware above the pt_regs structure. Thus I >> prefer to limit the usage of TOP_OF_KERNEL_STACK_PADDING on x86_64. > > The point is to keep consistency with other kernel threads, which have > the pt_regs area cleared (see copy_thread()). In particular, the CS > field can't have junk in it or else user_mode(regs) could return the > wrong result. So the stack needs to start below pt_regs, or we need > to explicitly zero pt_regs later. Okay, I will add TOP_OF_KERNEL_STACK_PADDING to the spot in arch/x86/kernel/head_64.S, plus another spot in arch/x86/xen/xen-head.S. However, I still think it would be better to not have TOP_OF_KERNEL_STACK_PADDING in these spots, instead we should explicitly zero pt_regs later; any *implicit* protocol is NOT welcome. I will find a timer later to check with the x86 maintainers :) > Ideally, the load from thread->sp should just shift RSP by phys_base, > pointing to the same memory location in the virtual mapping. I prefer to do this explicitly as what's done now; it may be easier to understand for future kernel developers. Thanks! Xin
diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h index 26620d7642a9..17fe81998ce4 100644 --- a/arch/x86/include/asm/processor.h +++ b/arch/x86/include/asm/processor.h @@ -664,8 +664,10 @@ static __always_inline void prefetchw(const void *x) #else extern unsigned long __end_init_task[]; -#define INIT_THREAD { \ - .sp = (unsigned long)&__end_init_task - sizeof(struct pt_regs), \ +#define INIT_THREAD { \ + .sp = (unsigned long)&__end_init_task - \ + TOP_OF_KERNEL_STACK_PADDING - \ + sizeof(struct pt_regs), \ } extern unsigned long KSTK_ESP(struct task_struct *task);