Received: by 2002:a05:6358:795:b0:dc:4c66:fc3e with SMTP id n21csp1799315rwj; Sun, 30 Oct 2022 06:39:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7jLKiWUmah3+rZxfRQ95050+hfa9B7nzSQAAZ5myGxaXfGlLvjrKlUqlvlPI0kgG0cWOyH X-Received: by 2002:a17:907:3f0c:b0:78e:2090:6924 with SMTP id hq12-20020a1709073f0c00b0078e20906924mr8392650ejc.332.1667137154421; Sun, 30 Oct 2022 06:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667137154; cv=none; d=google.com; s=arc-20160816; b=FIuCa69+08udvMNs7+a8AamOLOSI2KE44YcOBY2oNRKv0Df5GZBmasiReIgjQpJpCY WepH/rwgFXhHxcFbNUICv0njzoHVAlWHoFN+FL72etyiWQR26PF2mJCGUMlg5aASZ2Et eIjx1KmdrD5P8s3XVCbUikfcTFcZr3VkhJ6yYi9Ti77bw+bybmLL2O9KmK8xYBUtna1p dIj7BsALPUZ2vQk2DnHevMxQUEqqSeVZVYzBHxZeREpRm4mCrca+6GSef2E9Crf7P3aO VgiTJ4rJ++4cWYzHZ4prA8J4Z3j/2Uz3JrWpirKPfdgzRNQJOam21w38TOydWmDE6eCS 4asw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=E6tMRnPncn1DlFGdVr8fQR9t/9rp1zqZrcF5MBeadE8=; b=oUS71FHi6CXEzOP+b5kI06jzEvODe1gXs4zn8EMzDsHCWAgXUWeXsGC2QUD/Be69AM u8XRamy3wzJaAsn2RvPhiikAuvk5PutmM+5ADn/VhdInvRPGsi3aehZsCeP/XdhlI0ox sLHT9vVODhRwcq3YbsIHVNe8pLNorWHEty3FbM3IfNmX2irqZ7bjTWq8FHCZPZxpwkTW bJuO+qP3EGULEMc1O97vfIAbipymbdll808qXQcP45O959NsMr120zVsCcbQR+b9a5Sc v3wKcqh0bEiPoUc/E7vOZTVboTVHjAOJ6F4cpZD9MHbk70owZOvj7dWrbkBglVh6+TXH DsuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=a3GKiaC0; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o22-20020a170906289600b0078b8bc456dcsi3747672ejd.315.2022.10.30.06.38.49; Sun, 30 Oct 2022 06:39:14 -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=a3GKiaC0; 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 S229744AbiJ3NYI (ORCPT + 99 others); Sun, 30 Oct 2022 09:24:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229441AbiJ3NYG (ORCPT ); Sun, 30 Oct 2022 09:24:06 -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 F208DC51 for ; Sun, 30 Oct 2022 06:24:05 -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 8B0C160CE8 for ; Sun, 30 Oct 2022 13:24:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DCD62C433D7 for ; Sun, 30 Oct 2022 13:24:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1667136244; bh=R5uAAOwfhBbjV6oM76LGmmVk9MxkU7/sTXwusw3/Mxs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=a3GKiaC0hK08JreV1E2wvjYIXFxsyC0FotLYKaAMaPxNtqizbDuLeYwFdQS7Dchtt SaqA827YASFLTyHkjIWoAIlSGbzZiqdp+d3GX7cMEnxG5t4MJg1RhqHkmJTkLdeS4c Lf96++REpXouQkDkxJMoaibzFnY3svy2r8Me1PYmgv/tgjnSkhqte8hICQ+UbcEPSH nB6n1YfJ5Lmd6Jg+ZuUnSCjO/7YBA9XWVL91jnjmDMo9AG7Xpc+mWYT7m7vbPRpX81 hOklM0gE5GJV0sPE7EUS/TAL+7GbELYtsueIaBz3BDD9buSQ4l0Mu38c1c3arxuJiC We9fPTw8O6+1Q== Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-13b6c1c89bdso11087943fac.13 for ; Sun, 30 Oct 2022 06:24:04 -0700 (PDT) X-Gm-Message-State: ACrzQf3ZRwPq4GI5VGWU2QG/+KrZWLLtojcux+wIHhvKJYsGd66m5ihM Tvv4TkNq8hPSoNfTpn5Wo6vYOzRtt4N0BU35RkU= X-Received: by 2002:a05:6870:5803:b0:12c:c3e0:99dc with SMTP id r3-20020a056870580300b0012cc3e099dcmr13430439oap.19.1667136243989; Sun, 30 Oct 2022 06:24:03 -0700 (PDT) MIME-Version: 1.0 References: <20221030124517.2370-1-jszhang@kernel.org> In-Reply-To: <20221030124517.2370-1-jszhang@kernel.org> From: Guo Ren Date: Sun, 30 Oct 2022 21:23:51 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3] riscv: fix race when vmap stack overflow To: Jisheng Zhang Cc: Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.2 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: X-Mailing-List: linux-kernel@vger.kernel.org Reviewed-by: Guo Ren On Sun, Oct 30, 2022 at 8:54 PM 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 > Suggested-by: Guo Ren > --- > Since v2: > - use REG_AMOSWAP > - add comment to the purpose of smp_store_release() > > Since v1: > - use smp_store_release directly > - use unsigned int instead of atomic_t > > > arch/riscv/include/asm/asm.h | 1 + > arch/riscv/kernel/entry.S | 4 ++++ > arch/riscv/kernel/traps.c | 9 +++++++++ > 3 files changed, 14 insertions(+) > > diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h > index 1b471ff73178..acf563072b8b 100644 > --- a/arch/riscv/include/asm/asm.h > +++ b/arch/riscv/include/asm/asm.h > @@ -23,6 +23,7 @@ > #define REG_L __REG_SEL(ld, lw) > #define REG_S __REG_SEL(sd, sw) > #define REG_SC __REG_SEL(sc.d, sc.w) > +#define REG_AMOSWAP __REG_SEL(amoswap.d, amoswap.w) > #define REG_ASM __REG_SEL(.dword, .word) > #define SZREG __REG_SEL(8, 4) > #define LGREG __REG_SEL(3, 2) > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > index b9eda3fcbd6d..ea6b78dac739 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 > + REG_AMOSWAP 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..dea47f329708 100644 > --- a/arch/riscv/kernel/traps.c > +++ b/arch/riscv/kernel/traps.c > @@ -221,11 +221,20 @@ asmlinkage unsigned long get_overflow_stack(void) > OVERFLOW_STACK_SIZE; > } > > +unsigned long 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); > > + /* > + * to ensure spin flag is set after the sp is used in entry.S: > + * //load per-cpu overflow stack. > + * REG_L sp, -8(sp) > + */ > + smp_store_release(&spin_shadow_stack, 0); > + > console_verbose(); > > pr_emerg("Insufficient stack space to handle exception!\n"); > -- > 2.37.2 > -- Best Regards Guo Ren