linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode()
@ 2023-04-03  6:52 Björn Töpel
  2023-04-03 16:21 ` Conor Dooley
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Björn Töpel @ 2023-04-03  6:52 UTC (permalink / raw)
  To: Albert Ou, Paul Walmsley, Palmer Dabbelt, linux-riscv, Guo Ren
  Cc: Björn Töpel, Heiko Stübner, Conor Dooley,
	Yipeng Zou, linux-kernel, Palmer Dabbelt, Heiko Stuebner

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(-)

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode()
  2023-04-03  6:52 [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Björn Töpel
@ 2023-04-03 16:21 ` Conor Dooley
  2023-04-04 15:06 ` Geert Uytterhoeven
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Conor Dooley @ 2023-04-03 16:21 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Albert Ou, Paul Walmsley, Palmer Dabbelt, linux-riscv, Guo Ren,
	Björn Töpel, Heiko Stübner, Yipeng Zou,
	linux-kernel, Palmer Dabbelt, Heiko Stuebner

[-- Attachment #1: Type: text/plain, Size: 2150 bytes --]

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
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode()
  2023-04-03  6:52 [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Björn Töpel
  2023-04-03 16:21 ` Conor Dooley
@ 2023-04-04 15:06 ` Geert Uytterhoeven
  2023-04-10  9:41 ` Andy Chiu
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Geert Uytterhoeven @ 2023-04-04 15:06 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Albert Ou, Paul Walmsley, Palmer Dabbelt, linux-riscv, Guo Ren,
	Björn Töpel, Heiko Stübner, Conor Dooley,
	Yipeng Zou, linux-kernel, Palmer Dabbelt, Heiko Stuebner

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

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode()
  2023-04-03  6:52 [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Björn Töpel
  2023-04-03 16:21 ` Conor Dooley
  2023-04-04 15:06 ` Geert Uytterhoeven
@ 2023-04-10  9:41 ` Andy Chiu
  2023-04-11 17:39 ` Palmer Dabbelt
  2023-04-11 17:40 ` patchwork-bot+linux-riscv
  4 siblings, 0 replies; 6+ messages in thread
From: Andy Chiu @ 2023-04-10  9:41 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Albert Ou, Paul Walmsley, Palmer Dabbelt, linux-riscv, Guo Ren,
	Björn Töpel, Heiko Stübner, Conor Dooley,
	Yipeng Zou, linux-kernel, Palmer Dabbelt, Heiko Stuebner

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>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode()
  2023-04-03  6:52 [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Björn Töpel
                   ` (2 preceding siblings ...)
  2023-04-10  9:41 ` Andy Chiu
@ 2023-04-11 17:39 ` Palmer Dabbelt
  2023-04-11 17:40 ` patchwork-bot+linux-riscv
  4 siblings, 0 replies; 6+ messages in thread
From: Palmer Dabbelt @ 2023-04-11 17:39 UTC (permalink / raw)
  To: Albert Ou, Paul Walmsley, Palmer Dabbelt, linux-riscv, Guo Ren,
	=C3=B6rn_T=C3=B6pel?=
  Cc: =C3=B6rn_T=C3=B6pel?=, iko_St=C3=BCbner?=, Conor Dooley,
	Yipeng Zou, linux-kernel, Heiko Stuebner


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,
-- 
Palmer Dabbelt <palmer@rivosinc.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode()
  2023-04-03  6:52 [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Björn Töpel
                   ` (3 preceding siblings ...)
  2023-04-11 17:39 ` Palmer Dabbelt
@ 2023-04-11 17:40 ` patchwork-bot+linux-riscv
  4 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+linux-riscv @ 2023-04-11 17:40 UTC (permalink / raw)
  To: =?utf-8?b?QmrDtnJuIFTDtnBlbCA8Ympvcm5Aa2VybmVsLm9yZz4=?=
  Cc: linux-riscv, aou, paul.walmsley, palmer, guoren, bjorn, heiko,
	conor, zouyipeng, linux-kernel, palmer, heiko.stuebner

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-04-11 17:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-03  6:52 [PATCH] riscv: entry: Save a0 prior syscall_enter_from_user_mode() Björn Töpel
2023-04-03 16:21 ` Conor Dooley
2023-04-04 15:06 ` Geert Uytterhoeven
2023-04-10  9:41 ` Andy Chiu
2023-04-11 17:39 ` Palmer Dabbelt
2023-04-11 17:40 ` patchwork-bot+linux-riscv

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).