Message ID | 20221020143329.3276-1-jszhang@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp150711wrr; Thu, 20 Oct 2022 07:45:32 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6WMFbA+ax2W4xs1CYovyHPnYkuUpbyGtKMD+3m1oHZPu14Q7+mng3nbc/rckA9PO4s/y9u X-Received: by 2002:a17:903:11c7:b0:178:afa2:43fd with SMTP id q7-20020a17090311c700b00178afa243fdmr14316581plh.126.1666277132411; Thu, 20 Oct 2022 07:45:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666277132; cv=none; d=google.com; s=arc-20160816; b=Ll7NnBeBlX9004U8MixmiHmT5fQNFXVTuuv4dw3nQlThWJjvh378uIVotOcA7Nr/Xo rHAYNEF/R55+a8w3wRcC9PaXbi8NLXdYhuBVQKDMDw0RxG2Fy1msOuEKdOegYaelq6Ki i1gYmHSQMSOvBP7fVSj0gsCzhE5gdrY18BGmVkzO7FZvUT+wRQqanzzQQcGRV9xkY3DP y0N00pIRohZGAEaeJt5dQIywA9VOMbEAuEtR5m5rt0NwLHC5bzTDJjE+wr3g2K16lDet 9r7SCiUBZJyUnp7WIYsOnJ5KyyBLuEDO4J8EktLQW7Rkykfr0PaEijFJwJVdvd7JYtRJ 7rXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=YHm1JmiQ2wKxhdfLyZS4pgijO7TQkdMrphfDB4oFhKM=; b=axHSBUDXVAzmsuVj31ViQJQ3jk6PeKHhfQWoTSpr+znppOzcLkygAlBsWlyrtiYYGJ DJh4wSN10j8oFSk6r4AxFkAYiFtonA/pu7oUVzyVzGVg4WK9P6F26dREGMh9q3aDXGhW Z6ohBpAUzSXkfqTxXubjeZbgHsSB2nIW/rUb9aRbfwv1O0zQNVq4yOyBB3kKdqnR96Dq /wPK3Jiac/d/KlU1JiVb28llTpMFlPoWbj0XFqZvCelRiFM0dzEZ2yTKD6EPViVACwUk OnumyRYLAPg+rlBXWsMaRgMYDj4zLcH9eIWf+RBIkfO+gMWezrzKOM893aUENGOZ6H0e RLRw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Se7BLwYG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l6-20020a17090a850600b001f31f339134si3032375pjn.152.2022.10.20.07.45.17; Thu, 20 Oct 2022 07:45:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Se7BLwYG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230131AbiJTOnO (ORCPT <rfc822;ruipengqi7@gmail.com> + 99 others); Thu, 20 Oct 2022 10:43:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229972AbiJTOnM (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 20 Oct 2022 10:43:12 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22D4E1A1B28 for <linux-kernel@vger.kernel.org>; Thu, 20 Oct 2022 07:43:07 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A07A561BC2 for <linux-kernel@vger.kernel.org>; Thu, 20 Oct 2022 14:43:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8C9F1C433C1; Thu, 20 Oct 2022 14:43:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666276986; bh=1mkLwMduon6mFlXdZ1go1kC/+lZJsRhnP5cXjU0CP9Y=; h=From:To:Cc:Subject:Date:From; b=Se7BLwYGeyBpaIOaeMvTnpIfQPcLMy/HG8PNLOANqEHWjQO00EEcU/9jHx1e4NEcE Jrau7ya8nIOCwPX2I7qExozEfboSDqcOn/awK3TURUhTT2f2APsBtlkLkdvqmDKULG K8nwJidULg8IeFaF35l1ft1sh4gh/z4m8wFDlhPCz2zGNIq3pU3+PhEp7rainQg7Hr urKMdQLW61oxmVeixPwxqNTKEHGKvekeyHEZUzCTm5mity78SeuRqwM9TbbJbX8ghh 6gixtq4lXPVK8ApMSpbFxhJihlKxt412Cp6TsnaNX6NlkVanE1KXYkJUE6ryLVqbwV XcYIjkGmKMpFw== From: Jisheng Zhang <jszhang@kernel.org> To: Paul Walmsley <paul.walmsley@sifive.com>, Palmer Dabbelt <palmer@dabbelt.com>, Albert Ou <aou@eecs.berkeley.edu> Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Guo Ren <guoren@kernel.org> Subject: [PATCH v2] riscv: fix race when vmap stack overflow Date: Thu, 20 Oct 2022 22:33:29 +0800 Message-Id: <20221020143329.3276-1-jszhang@kernel.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747218210530584140?= X-GMAIL-MSGID: =?utf-8?q?1747218210530584140?= |
Series |
[v2] riscv: fix race when vmap stack overflow
|
|
Commit Message
Jisheng Zhang
Oct. 20, 2022, 2:33 p.m. UTC
Currently, when detecting vmap stack overflow, riscv firstly switches
to the so called shadow stack, then use this shadow stack to call the
get_overflow_stack() to get the overflow stack. However, there's
a race here if two or more harts use the same shadow stack at the same
time.
To solve this race, we introduce spin_shadow_stack atomic var, which
will be swap between its own address and 0 in atomic way, when the
var is set, it means the shadow_stack is being used; when the var
is cleared, it means the shadow_stack isn't being used.
Fixes: 31da94c25aea ("riscv: add VMAP_STACK overflow detection")
Signed-off-by: Jisheng Zhang <jszhang@kernel.org>
Suggested-by: Guo Ren <guoren@kernel.org>
---
Since v1:
- use smp_store_release directly
- use unsigned int instead of atomic_t
arch/riscv/kernel/entry.S | 4 ++++
arch/riscv/kernel/traps.c | 4 ++++
2 files changed, 8 insertions(+)
Comments
On Thu, Oct 20, 2022 at 10:43 PM Jisheng Zhang <jszhang@kernel.org> wrote: > > Currently, when detecting vmap stack overflow, riscv firstly switches > to the so called shadow stack, then use this shadow stack to call the > get_overflow_stack() to get the overflow stack. However, there's > a race here if two or more harts use the same shadow stack at the same > time. > > To solve this race, we introduce spin_shadow_stack atomic var, which > will be swap between its own address and 0 in atomic way, when the > var is set, it means the shadow_stack is being used; when the var > is cleared, it means the shadow_stack isn't being used. > > Fixes: 31da94c25aea ("riscv: add VMAP_STACK overflow detection") > Signed-off-by: Jisheng Zhang <jszhang@kernel.org> > Suggested-by: Guo Ren <guoren@kernel.org> > --- > Since v1: > - use smp_store_release directly > - use unsigned int instead of atomic_t > > arch/riscv/kernel/entry.S | 4 ++++ > arch/riscv/kernel/traps.c | 4 ++++ > 2 files changed, 8 insertions(+) > > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > index b9eda3fcbd6d..7b924b16792b 100644 > --- a/arch/riscv/kernel/entry.S > +++ b/arch/riscv/kernel/entry.S > @@ -404,6 +404,10 @@ handle_syscall_trace_exit: > > #ifdef CONFIG_VMAP_STACK > handle_kernel_stack_overflow: > +1: la sp, spin_shadow_stack > + amoswap.w sp, sp, (sp) Using ".w" is tricky for 64 BIT, I've hesitated. ';) Let's move to REG_AMOSWAP, or you must make sure the address of spin_shadow_stack won't be 4GB aligned. > + bnez sp, 1b > + > la sp, shadow_stack > addi sp, sp, SHADOW_OVERFLOW_STACK_SIZE > > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c > index f3e96d60a2ff..f1f57c1241b6 100644 > --- a/arch/riscv/kernel/traps.c > +++ b/arch/riscv/kernel/traps.c > @@ -221,11 +221,15 @@ asmlinkage unsigned long get_overflow_stack(void) > OVERFLOW_STACK_SIZE; > } > > +unsigned int spin_shadow_stack; > + > asmlinkage void handle_bad_stack(struct pt_regs *regs) > { > unsigned long tsk_stk = (unsigned long)current->stack; > unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack); > > + smp_store_release(&spin_shadow_stack, 0); > + > console_verbose(); > > pr_emerg("Insufficient stack space to handle exception!\n"); > -- > 2.37.2 >
On Thu, Oct 20, 2022 at 10:33:29PM +0800, Jisheng Zhang wrote: > Currently, when detecting vmap stack overflow, riscv firstly switches > to the so called shadow stack, then use this shadow stack to call the > get_overflow_stack() to get the overflow stack. However, there's > a race here if two or more harts use the same shadow stack at the same > time. > > To solve this race, we introduce spin_shadow_stack atomic var, which > will be swap between its own address and 0 in atomic way, when the > var is set, it means the shadow_stack is being used; when the var > is cleared, it means the shadow_stack isn't being used. > > Fixes: 31da94c25aea ("riscv: add VMAP_STACK overflow detection") > Signed-off-by: Jisheng Zhang <jszhang@kernel.org> > Suggested-by: Guo Ren <guoren@kernel.org> > --- > Since v1: > - use smp_store_release directly > - use unsigned int instead of atomic_t > > arch/riscv/kernel/entry.S | 4 ++++ > arch/riscv/kernel/traps.c | 4 ++++ > 2 files changed, 8 insertions(+) > > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > index b9eda3fcbd6d..7b924b16792b 100644 > --- a/arch/riscv/kernel/entry.S > +++ b/arch/riscv/kernel/entry.S > @@ -404,6 +404,10 @@ handle_syscall_trace_exit: > > #ifdef CONFIG_VMAP_STACK > handle_kernel_stack_overflow: > +1: la sp, spin_shadow_stack > + amoswap.w sp, sp, (sp) > + bnez sp, 1b > + > la sp, shadow_stack > addi sp, sp, SHADOW_OVERFLOW_STACK_SIZE > > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c > index f3e96d60a2ff..f1f57c1241b6 100644 > --- a/arch/riscv/kernel/traps.c > +++ b/arch/riscv/kernel/traps.c > @@ -221,11 +221,15 @@ asmlinkage unsigned long get_overflow_stack(void) > OVERFLOW_STACK_SIZE; > } > > +unsigned int spin_shadow_stack; > + > asmlinkage void handle_bad_stack(struct pt_regs *regs) > { > unsigned long tsk_stk = (unsigned long)current->stack; > unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack); > > + smp_store_release(&spin_shadow_stack, 0); > + Maybe add a comment integrating Guo's remarks, https://lore.kernel.org/all/CAJF2gTRAEX_jQ_w5H05dyafZzHq+P5j05TJ=C+v+OL__GQam4A@mail.gmail.com/T/#u https://lore.kernel.org/all/CAJF2gTRdtcpccL5W48O8VEXCMvxNAyyrKJzhwNJkc8js+H2iJg@mail.gmail.com/T/#u It will come in handy to future reviewers (and it would address the checkpatch.pl's "memory barrier without comment" warning). Thanks, Andrea > console_verbose(); > > pr_emerg("Insufficient stack space to handle exception!\n"); > -- > 2.37.2 > > > _______________________________________________ > linux-riscv mailing list > linux-riscv@lists.infradead.org > http://lists.infradead.org/mailman/listinfo/linux-riscv
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index b9eda3fcbd6d..7b924b16792b 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -404,6 +404,10 @@ handle_syscall_trace_exit: #ifdef CONFIG_VMAP_STACK handle_kernel_stack_overflow: +1: la sp, spin_shadow_stack + amoswap.w sp, sp, (sp) + bnez sp, 1b + la sp, shadow_stack addi sp, sp, SHADOW_OVERFLOW_STACK_SIZE diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index f3e96d60a2ff..f1f57c1241b6 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -221,11 +221,15 @@ asmlinkage unsigned long get_overflow_stack(void) OVERFLOW_STACK_SIZE; } +unsigned int spin_shadow_stack; + asmlinkage void handle_bad_stack(struct pt_regs *regs) { unsigned long tsk_stk = (unsigned long)current->stack; unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack); + smp_store_release(&spin_shadow_stack, 0); + console_verbose(); pr_emerg("Insufficient stack space to handle exception!\n");