Skip to contentSkip to conversation

riscv: entry: Save a0 prior syscall_enter_from_user_mode()

PATCH Applied Björn Töpel 6 participants 6 messages Apr 3, 2023 – Apr 11, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: entry: Save a0 prior syscall_enter_from_user_mode()

From: Björn Töpel <[email protected]>

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")
---
 arch/riscv/kernel/traps.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/traps.c +2 -2
index 1f4e37be7eb3..8c258b78c925 100644
@@ -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

Signature · 1 line
2.37.2
Reviewed-by Tested-by on v1 1/1
#1 lore raw
Quoted 13 lines · Björn Töpel

From: Björn Töpel <[email protected]>

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.

Quoted 7 lines

This is resolved, by saving a0 prior doing the
syscall_enter_from_user_mode() call.

Fixes f0bddf50586d ("riscv: entry: Convert to generic entry")

As you pointed out, v12 did indeed have this ordering, so *deep breath*

Thanks for fixing this Björn!

Quoted 27 lines
---
 arch/riscv/kernel/traps.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/traps.c +2 -2
index 1f4e37be7eb3..8c258b78c925 100644
@@ -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

Signature · 1 line
2.37.2
Tested-by on v1 1/1
#2 lore raw
In reply to Björn Töpel
Quoted 13 lines · Björn Töpel

From: Björn Töpel <[email protected]>

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.

Quoted 7 lines

This is resolved, by saving a0 prior doing the
syscall_enter_from_user_mode() call.

Fixes f0bddf50586d ("riscv: entry: Convert to generic entry")

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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
Tested-by on v1 1/1
#3 lore raw
In reply to Björn Töpel
Quoted 14 lines · Björn Töpel

From: Björn Töpel <[email protected]>

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.

Quoted 7 lines

This is resolved, by saving a0 prior doing the
syscall_enter_from_user_mode() call.

Fixes f0bddf50586d ("riscv: entry: Convert to generic entry")

This fix works for me, thanks!

#4 lore raw
In reply to Björn Töpel
Quoted 6 lines · Björn Töpel

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.

Quoted 6 lines

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,

Signature · 7 lines
Palmer Dabbelt <[email protected]>


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw
In reply to Björn Töpel
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 9 lines · On Mon, 3 Apr 2023 08:52:07 +0200 you wrote:

From: Björn Töpel <[email protected]>

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!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv