Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1354277rwi; Wed, 19 Oct 2022 09:34:07 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4RhXpHBoBMtiifSdvXxN7Y9e0WCKgOIsUYbe4beIv0yk8wjyeLl3Gh4pNZcyA0c078Q+89 X-Received: by 2002:a17:90b:3b45:b0:20c:2eae:e70 with SMTP id ot5-20020a17090b3b4500b0020c2eae0e70mr10824551pjb.240.1666197247030; Wed, 19 Oct 2022 09:34:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666197247; cv=none; d=google.com; s=arc-20160816; b=SWJlH6FkwsvS3BeH5cB2T9tuqqy1SlVgrorsmtBBJbt2oWv30yOUM+3LxBvNo6lJ0+ qVm3YTqgN0TGuFSriVoUL3ILiMYz/mgR/Y3Pv3pdXaM9LsF4YIORb7GQ4ZEJJiCfDkTo aAlu2KZE79Wl3q0QaXlWyJsh4ryqxYrdJZ2FReuRvBka50bMQPgqeHRbD1AT11GJKhVV EiQ+RwvxhuuRn6Y364B1e0Fg5ThCIH/ZdpPSnvEe9VZHuChpJbblWqdSL1xzvbHdH2w7 qiDw+4kjFVScJcqPesSkhgvjHGubPuxRM7AsiJppv8ZYprV8wj4/90++H+aNL3opJZi9 BLVA== 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=p6KWbj9I8bNY3vn/X42lwrZTpKWdgIenHyphn6Aqyto=; b=RnNU+SzGkIShZE9ee2jxPyD0T70OBnj+c/6D22/VwofkyCzpzMUNIOWYee1rVIhDlI 5DSB1t4lpFbzKK8cfEaVSSIwWB1zdk8PYb23d7b55c2zCw0yVk7wS47OoCEBbI0LLpPV p6sY11Chqi3e/XCOa67Y0iFvGuyp+uefFRm1pjTzNp7g2T8SVCfoL8hcTBDymYskdqpm DrZLkCRdUIeXdnIBOxm28GRLIQHYJM7uk8dnhkeNHL8r7dEdvphexOFvjlj6BJpihq66 ZApMDQe8Odl+bmaDbdOCCRq54D14qggZi6+CQv2ShS1aZ0OEj/JwQamS8jI+tnYI/JOi ftiQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LXQ3DBXy; 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 h2-20020a056a00230200b005429324b543si19809452pfh.360.2022.10.19.09.33.53; Wed, 19 Oct 2022 09:34:07 -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=LXQ3DBXy; 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 S230175AbiJSP7b (ORCPT + 99 others); Wed, 19 Oct 2022 11:59:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231934AbiJSP6e (ORCPT ); Wed, 19 Oct 2022 11:58:34 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 746B21CCCE0 for ; Wed, 19 Oct 2022 08:57:24 -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 ams.source.kernel.org (Postfix) with ESMTPS id 2653BB824FA for ; Wed, 19 Oct 2022 15:57:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EF9FC433C1; Wed, 19 Oct 2022 15:57:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666195024; bh=tl/t0JSFx6B/W7U7U+lCNfIdDELRIvrRpnIkdxf7UO8=; h=From:To:Cc:Subject:Date:From; b=LXQ3DBXykd8cxJe+nDeTMuzZh2SioIauX440gB0kLifBqT436A3SwZ8AqZAu0d5Ks QaH7PcJ7Zc5hE1yZ2imldpdk31TSsBPmyqtqIUmi/FDTM7tlChYsG3UdkuAm43zcOZ RAvDnZZCtCzMzQWa2vGzAFjZlg84MC1gKhu9nK2YdtXDm2qiYt7ZuDVhgD6so4+eab ItBZ4nypwK6bA1YqZxImaHz4C5ugTj8o4tT4JXmFk+xq+iKF2uoHw9fPykRoMDNju8 p4Ah1KhNSiZSfM7VwAmm6T8F5k5PTyhH7sfIJdBw4ZMGoCPp3RMnU1QXuOVwaKr0tn j/GdetFOTgQsA== From: Jisheng Zhang To: Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Guo Ren Subject: [PATCH] riscv: fix race when vmap stack overflow Date: Wed, 19 Oct 2022 23:47:27 +0800 Message-Id: <20221019154727.2395-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: X-Mailing-List: linux-kernel@vger.kernel.org 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 make the shadow stack usage serialized. Fixes: 31da94c25aea ("riscv: add VMAP_STACK overflow detection") Signed-off-by: Jisheng Zhang Suggested-by: Guo Ren --- 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..88a54947dffb 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; } +atomic_t 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); + atomic_set_release(&spin_shadow_stack, 0); + console_verbose(); pr_emerg("Insufficient stack space to handle exception!\n"); -- 2.37.2