Message ID | 20230403065207.1070974-1-bjorn@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2107558vqo; Mon, 3 Apr 2023 00:00:31 -0700 (PDT) X-Google-Smtp-Source: AKy350ZyOjg1t7p6T/J6pCmE8gT5mL9mIqMMUXqGmGNcp4xTZXzsMOztP4nOxZ+mDF6GH3QosK9i X-Received: by 2002:a62:2905:0:b0:62d:e5eb:2d73 with SMTP id p5-20020a622905000000b0062de5eb2d73mr10592537pfp.34.1680505230793; Mon, 03 Apr 2023 00:00:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680505230; cv=none; d=google.com; s=arc-20160816; b=oi2ncIgR7kH7Qwxzn3YAi1OdwGdYMNS9slZl8oDYpmBrbFGMgnKiEpM3B++qmF+K52 mOxJHm9CqdD2CDSCXxqJVmrfcpFWQHLxSx88jRz6MnTsUsntQ6M3s6yGqRxnj6g92FN+ EL5NrNf2y3I9XfmEIxKPbnn5sRG43Gl0wF84+K99J9W3UEkBIB2TSVcLjLARRQTyQpBs aRfYdQz3yyJx8ThvoOop5/2a7Z6/dhLFEDdeG5Zyffkw9vdN/ip8JmUe6otjY72oi47+ sFab8/Hka9saKvaDRQSkoBiWCdf1dqY2yCZjeard68mJi8ooLVTBw1eDzxGtqFwA/PJ0 xynQ== 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=8JiLb/CTlYwMlTR/fEQO4QMmGN9oM+UKPs1VNM196UY=; b=ykpC0cjVuHtkk9pBlHLIeSyVzz2Gqh84eNKzdjNYt/GkgfbBgLMRNRO0WBpbk94hOf kzhXMnFy+d2E6TD/CR/OW6s7JTDfb6zQBme9hMS6ibH+5UrrWmYh35PWUEHRHPJOP/2j XTSHQVre5J9chMfoVMevI58MyyOdzaLS64Dftnvy+oOYGEr2Vr2sJB7blfO+jd5+FkCG HPptocj+lShvoVeAYBW5E1ke7hSPgmkqJy6gB6xrJHy7oBy5tON/PixTWyE5VRnp8wqg uKMQ5lBjFFoh38wsSLBq2k8ZWxuMbBbY4o0QCvKUrPK86+Qoe/TtjYzJL0Kefv7TLcvt 5Jmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=PZjCGGdy; 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 k6-20020aa79d06000000b0062e14211e54si1657061pfp.240.2023.04.03.00.00.16; Mon, 03 Apr 2023 00:00:30 -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=PZjCGGdy; 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 S231425AbjDCGwS (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Mon, 3 Apr 2023 02:52:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229797AbjDCGwQ (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 3 Apr 2023 02:52:16 -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 DEE4F2727 for <linux-kernel@vger.kernel.org>; Sun, 2 Apr 2023 23:52:15 -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 2FA50614FB for <linux-kernel@vger.kernel.org>; Mon, 3 Apr 2023 06:52:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4493EC433EF; Mon, 3 Apr 2023 06:52:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1680504734; bh=a/R10jH30vYnh+1wVVgBZesVEWx9VEabSFxoWE4WGaQ=; h=From:To:Cc:Subject:Date:From; b=PZjCGGdy918fsCIV2X7ji6gb3Dy7kLUZvge78w6piYQ0DQT26vfQkZHg1ffZkorWO Ep6okNEzf7o5LlKEoBg1ercBGYLyUlE4CNG9/sJ/RlIkr7ADq0Yeov3preu9D67xGd A/Fkj3TPKWa+uU/fkqkeeIODnngpg6TYD0M94porHImd6B20kTGQeIwW9yPuATQd27 pg+kp41sRKtr+SaqbATMRdx4khZSNtThQnE0EwW89wGCNUmnquYEW/HQnjl9W2wCLf O9HhPb7WXLGbnP62cMrVTQui4zNO6YrU+zxA5HVieNzHsMNOzS7f71gUYvMGFKbrcs 0vPeN8NRU/FWw== From: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= <bjorn@kernel.org> To: Albert Ou <aou@eecs.berkeley.edu>, Paul Walmsley <paul.walmsley@sifive.com>, Palmer Dabbelt <palmer@dabbelt.com>, linux-riscv@lists.infradead.org, Guo Ren <guoren@linux.alibaba.com> Cc: =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= <bjorn@rivosinc.com>, =?utf-8?q?Heiko_S?= =?utf-8?q?t=C3=BCbner?= <heiko@sntech.de>, Conor Dooley <conor@kernel.org>, Yipeng Zou <zouyipeng@huawei.com>, linux-kernel@vger.kernel.org, Palmer Dabbelt <palmer@rivosinc.com>, Heiko Stuebner <heiko.stuebner@vrull.eu> Subject: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Date: Mon, 3 Apr 2023 08:52:07 +0200 Message-Id: <20230403065207.1070974-1-bjorn@kernel.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=unavailable 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?1762137452619710070?= X-GMAIL-MSGID: =?utf-8?q?1762137452619710070?= |
Series |
riscv: entry: Save a0 prior syscall_enter_from_user_mode()
|
|
Commit Message
Björn Töpel
April 3, 2023, 6:52 a.m. UTC
From: Björn Töpel <bjorn@rivosinc.com> The RISC-V calling convention passes the first argument, and the return value in the a0 register. For this reason, the a0 register needs some extra care; When handling syscalls, the a0 register is saved into regs->orig_a0, so a0 can be properly restored for, e.g. interrupted syscalls. This functionality was broken with the introduction of the generic entry patches. Here, a0 was saved into orig_a0 after calling syscall_enter_from_user_mode(), which can change regs->a0 for some paths, incorrectly restoring a0. This is resolved, by saving a0 prior doing the syscall_enter_from_user_mode() call. Fixes: f0bddf50586d ("riscv: entry: Convert to generic entry") Reviewed-by: Heiko Stuebner <heiko.stuebner@vrull.eu> Tested-by: Heiko Stuebner <heiko.stuebner@vrull.eu> Signed-off-by: Björn Töpel <bjorn@rivosinc.com> --- arch/riscv/kernel/traps.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) base-commit: d34a6b715a23ccd9c9d0bc7a475bea59dc3e28b2
Comments
On Mon, Apr 03, 2023 at 08:52:07AM +0200, Björn Töpel wrote: > From: Björn Töpel <bjorn@rivosinc.com> > > The RISC-V calling convention passes the first argument, and the > return value in the a0 register. For this reason, the a0 register > needs some extra care; When handling syscalls, the a0 register is > saved into regs->orig_a0, so a0 can be properly restored for, > e.g. interrupted syscalls. > > This functionality was broken with the introduction of the generic > entry patches. Here, a0 was saved into orig_a0 after calling > syscall_enter_from_user_mode(), which can change regs->a0 for some > paths, incorrectly restoring a0. > > This is resolved, by saving a0 prior doing the > syscall_enter_from_user_mode() call. > > Fixes: f0bddf50586d ("riscv: entry: Convert to generic entry") > Reviewed-by: Heiko Stuebner <heiko.stuebner@vrull.eu> > Tested-by: Heiko Stuebner <heiko.stuebner@vrull.eu> > Signed-off-by: Björn Töpel <bjorn@rivosinc.com> As you pointed out, v12 did indeed have this ordering, so *deep breath* Reported-by: Conor Dooley <conor.dooley@microchip.com> Link: https://lore.kernel.org/linux-riscv/60ee7c26-1a70-427d-beaf-92e2989fc479@spud/ Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Tested-by: Conor Dooley <conor.dooley@microchip.com> Thanks for fixing this Björn! > --- > arch/riscv/kernel/traps.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c > index 1f4e37be7eb3..8c258b78c925 100644 > --- a/arch/riscv/kernel/traps.c > +++ b/arch/riscv/kernel/traps.c > @@ -270,11 +270,11 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs) > if (user_mode(regs)) { > ulong syscall = regs->a7; > > - syscall = syscall_enter_from_user_mode(regs, syscall); > - > regs->epc += 4; > regs->orig_a0 = regs->a0; > > + syscall = syscall_enter_from_user_mode(regs, syscall); > + > if (syscall < NR_syscalls) > syscall_handler(regs, syscall); > else > > base-commit: d34a6b715a23ccd9c9d0bc7a475bea59dc3e28b2 > -- > 2.37.2 >
On Mon, Apr 3, 2023 at 8:54 AM Björn Töpel <bjorn@kernel.org> wrote: > From: Björn Töpel <bjorn@rivosinc.com> > > The RISC-V calling convention passes the first argument, and the > return value in the a0 register. For this reason, the a0 register > needs some extra care; When handling syscalls, the a0 register is > saved into regs->orig_a0, so a0 can be properly restored for, > e.g. interrupted syscalls. > > This functionality was broken with the introduction of the generic > entry patches. Here, a0 was saved into orig_a0 after calling > syscall_enter_from_user_mode(), which can change regs->a0 for some > paths, incorrectly restoring a0. > > This is resolved, by saving a0 prior doing the > syscall_enter_from_user_mode() call. > > Fixes: f0bddf50586d ("riscv: entry: Convert to generic entry") > Reviewed-by: Heiko Stuebner <heiko.stuebner@vrull.eu> > Tested-by: Heiko Stuebner <heiko.stuebner@vrull.eu> > Signed-off-by: Björn Töpel <bjorn@rivosinc.com> Tested-by: Geert Uytterhoeven <geert+renesas@glider.be> Gr{oetje,eeting}s, Geert
On Mon, Apr 3, 2023 at 2:54 PM Björn Töpel <bjorn@kernel.org> wrote: > > From: Björn Töpel <bjorn@rivosinc.com> > > The RISC-V calling convention passes the first argument, and the > return value in the a0 register. For this reason, the a0 register > needs some extra care; When handling syscalls, the a0 register is > saved into regs->orig_a0, so a0 can be properly restored for, > e.g. interrupted syscalls. > > This functionality was broken with the introduction of the generic > entry patches. Here, a0 was saved into orig_a0 after calling > syscall_enter_from_user_mode(), which can change regs->a0 for some > paths, incorrectly restoring a0. > > This is resolved, by saving a0 prior doing the > syscall_enter_from_user_mode() call. > > Fixes: f0bddf50586d ("riscv: entry: Convert to generic entry") > Reviewed-by: Heiko Stuebner <heiko.stuebner@vrull.eu> > Tested-by: Heiko Stuebner <heiko.stuebner@vrull.eu> > Signed-off-by: Björn Töpel <bjorn@rivosinc.com> This fix works for me, thanks! Tested-by: Andy Chiu <andy.chiu@sifive.com>
On Mon, 03 Apr 2023 08:52:07 +0200, Björn Töpel wrote: > The RISC-V calling convention passes the first argument, and the > return value in the a0 register. For this reason, the a0 register > needs some extra care; When handling syscalls, the a0 register is > saved into regs->orig_a0, so a0 can be properly restored for, > e.g. interrupted syscalls. > > This functionality was broken with the introduction of the generic > entry patches. Here, a0 was saved into orig_a0 after calling > syscall_enter_from_user_mode(), which can change regs->a0 for some > paths, incorrectly restoring a0. > > [...] Applied, thanks! [1/1] riscv: entry: Save a0 prior syscall_enter_from_user_mode() https://git.kernel.org/palmer/c/9c2598d43510 Best regards,
Hello: This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <palmer@rivosinc.com>: On Mon, 3 Apr 2023 08:52:07 +0200 you wrote: > From: Björn Töpel <bjorn@rivosinc.com> > > The RISC-V calling convention passes the first argument, and the > return value in the a0 register. For this reason, the a0 register > needs some extra care; When handling syscalls, the a0 register is > saved into regs->orig_a0, so a0 can be properly restored for, > e.g. interrupted syscalls. > > [...] Here is the summary with links: - riscv: entry: Save a0 prior syscall_enter_from_user_mode() https://git.kernel.org/riscv/c/9c2598d43510 You are awesome, thank you!
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c index 1f4e37be7eb3..8c258b78c925 100644 --- a/arch/riscv/kernel/traps.c +++ b/arch/riscv/kernel/traps.c @@ -270,11 +270,11 @@ asmlinkage __visible __trap_section void do_trap_ecall_u(struct pt_regs *regs) if (user_mode(regs)) { ulong syscall = regs->a7; - syscall = syscall_enter_from_user_mode(regs, syscall); - regs->epc += 4; regs->orig_a0 = regs->a0; + syscall = syscall_enter_from_user_mode(regs, syscall); + if (syscall < NR_syscalls) syscall_handler(regs, syscall); else