Skip to contentSkip to conversation

riscv: misaligned: fix interruptible context and add tests

PATCH Superseded Clément Léger 4 participants 15 messages Apr 14, 2025 – May 1, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover riscv: misaligned: fix interruptible context and add tests

This series fixes misaligned access handling when in non interruptible context by reenabling interrupts when possible. A previous commit changed raw_copy_from_user() with copy_from_user() which enables page faulting and thus can sleep. While correct, a warning is now triggered due to being called in an invalid context (sleeping in non-interruptible). This series fixes that problem by factorizing misaligned load/store entry in a single function than reenables interrupt if the interrupted context had interrupts enabled. In order for misaligned handling problems to be caught sooner, add a kselftest for all the currently supported instructions .

Note: these commits were actually part of another larger series for misaligned request delegation but was split since it isn't directly required.

Clément Léger (5):
riscv: misaligned: factorize trap handling
riscv: misaligned: enable IRQs while handling misaligned accesses
riscv: misaligned: use get_user() instead of __get_user() Documentation/sysctl: add riscv to unaligned-trap supported archs
selftests: riscv: add misaligned access testing

 Documentation/admin-guide/sysctl/kernel.rst   |   4 +-
 arch/riscv/kernel/traps.c                     |  57 ++--
 arch/riscv/kernel/traps_misaligned.c          |   2 +-
 .../selftests/riscv/misaligned/.gitignore     |   1 +
 .../selftests/riscv/misaligned/Makefile       |  12 +
 .../selftests/riscv/misaligned/common.S       |  33 +++
 .../testing/selftests/riscv/misaligned/fpu.S  | 180 +++++++++++++
 tools/testing/selftests/riscv/misaligned/gp.S | 103 +++++++
 .../selftests/riscv/misaligned/misaligned.c   | 254 ++++++++++++++++++
 9 files changed, 614 insertions(+), 32 deletions(-)
 create mode 100644 tools/testing/selftests/riscv/misaligned/.gitignore
 create mode 100644 tools/testing/selftests/riscv/misaligned/Makefile
 create mode 100644 tools/testing/selftests/riscv/misaligned/common.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/fpu.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/gp.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/misaligned.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/5 riscv: misaligned: factorize trap handling

misaligned accesses traps are not nmi and should be treated as normal one using irqentry_enter()/exit(). Since both load/store and user/kernel should use almost the same path and that we are going to add some code around that, factorize it.

---
 arch/riscv/kernel/traps.c | 49 ++++++++++++++++-----------------------
 1 file changed, 20 insertions(+), 29 deletions(-)
arch/riscv/kernel/traps.c +20 -29
index 8ff8e8b36524..55d9f3450398 100644
@@ -198,47 +198,38 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
 DO_ERROR_INFO(do_trap_load_fault,
 	SIGSEGV, SEGV_ACCERR, "load access fault");
 
-asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
+enum misaligned_access_type {
+	MISALIGNED_STORE,
+	MISALIGNED_LOAD,
+};
+
+static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type type)
 {
-	if (user_mode(regs)) {
-		irqentry_enter_from_user_mode(regs);
+	irqentry_state_t state = irqentry_enter(regs);
 
+	if (type ==  MISALIGNED_LOAD) {
 		if (handle_misaligned_load(regs))
 			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-			      "Oops - load address misaligned");
-
-		irqentry_exit_to_user_mode(regs);
+				      "Oops - load address misaligned");
 	} else {
-		irqentry_state_t state = irqentry_nmi_enter(regs);
-
-		if (handle_misaligned_load(regs))
+		if (handle_misaligned_store(regs))
 			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-			      "Oops - load address misaligned");
-
-		irqentry_nmi_exit(regs, state);
+				      "Oops - store (or AMO) address misaligned");
 	}
+
+	irqentry_exit(regs, state);
 }
 
-asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
+asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
 {
-	if (user_mode(regs)) {
-		irqentry_enter_from_user_mode(regs);
-
-		if (handle_misaligned_store(regs))
-			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-				"Oops - store (or AMO) address misaligned");
-
-		irqentry_exit_to_user_mode(regs);
-	} else {
-		irqentry_state_t state = irqentry_nmi_enter(regs);
-
-		if (handle_misaligned_store(regs))
-			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-				"Oops - store (or AMO) address misaligned");
+	do_trap_misaligned(regs, MISALIGNED_LOAD);
+}
 
-		irqentry_nmi_exit(regs, state);
-	}
+asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
+{
+	do_trap_misaligned(regs, MISALIGNED_STORE);
 }
+
 DO_ERROR_INFO(do_trap_store_fault,
 	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
 DO_ERROR_INFO(do_trap_ecall_s,
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH 1/5] riscv: misaligned: factorize trap handling

Hi Clément,

Quoted 2 lines · Clément Léger

misaligned accesses traps are not nmi and should be treated as normal one using irqentry_enter()/exit().

All the traps that come from kernel mode are treated as nmi as it was suggested by Peter here: https://lore.kernel.org/linux-riscv/[email protected]/

I don't know the differences between irq_nmi_entry/exit() and irq_entry/exit(), so is that still correct to now treat the kernel traps as non-nmi?

Thanks,

Alex

Quoted 81 lines

Since both load/store and user/kernel
should use almost the same path and that we are going to add some code around that, factorize it.

---
  arch/riscv/kernel/traps.c | 49 ++++++++++++++++-----------------------
  1 file changed, 20 insertions(+), 29 deletions(-)
arch/riscv/kernel/traps.c +20 -29
index 8ff8e8b36524..55d9f3450398 100644
@@ -198,47 +198,38 @@ asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *re
  DO_ERROR_INFO(do_trap_load_fault,
  	SIGSEGV, SEGV_ACCERR, "load access fault");
 
-asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
+enum misaligned_access_type {
+	MISALIGNED_STORE,
+	MISALIGNED_LOAD,
+};
+
+static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type type)
  {
-	if (user_mode(regs)) {
-		irqentry_enter_from_user_mode(regs);
+	irqentry_state_t state = irqentry_enter(regs);
 
+	if (type ==  MISALIGNED_LOAD) {
  		if (handle_misaligned_load(regs))
  			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-			      "Oops - load address misaligned");
-
-		irqentry_exit_to_user_mode(regs);
+				      "Oops - load address misaligned");
  	} else {
-		irqentry_state_t state = irqentry_nmi_enter(regs);
-
-		if (handle_misaligned_load(regs))
+		if (handle_misaligned_store(regs))
  			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-			      "Oops - load address misaligned");
-
-		irqentry_nmi_exit(regs, state);
+				      "Oops - store (or AMO) address misaligned");
  	}
+
+	irqentry_exit(regs, state);
  }
 
-asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
+asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
  {
-	if (user_mode(regs)) {
-		irqentry_enter_from_user_mode(regs);
-
-		if (handle_misaligned_store(regs))
-			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-				"Oops - store (or AMO) address misaligned");
-
-		irqentry_exit_to_user_mode(regs);
-	} else {
-		irqentry_state_t state = irqentry_nmi_enter(regs);
-
-		if (handle_misaligned_store(regs))
-			do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-				"Oops - store (or AMO) address misaligned");
+	do_trap_misaligned(regs, MISALIGNED_LOAD);
+}
 
-		irqentry_nmi_exit(regs, state);
-	}
+asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
+{
+	do_trap_misaligned(regs, MISALIGNED_STORE);
  }
+
  DO_ERROR_INFO(do_trap_store_fault,
  	SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
  DO_ERROR_INFO(do_trap_ecall_s,
#3 lore raw

Re: [PATCH 1/5] riscv: misaligned: factorize trap handling

Quoted 8 lines · Alexandre Ghiti

Hi Clément,

Quoted 2 lines · Clément Léger

misaligned accesses traps are not nmi and should be treated as normal one using irqentry_enter()/exit().

Quoted 6 lines

All the traps that come from kernel mode are treated as nmi as it was suggested by Peter here: https://lore.kernel.org/linux-riscv/
[email protected]/

I don't know the differences between irq_nmi_entry/exit() and irq_entry/ exit(), so is that still correct to now treat the kernel traps as non-nmi?

Hi Alex,

Actually, this discussion was raised on a previous series [1] by Maciej which replied that we should actually reenable interrupt depending on the state that was interrupted. Looking at other architecture/code, it seems like treating misaligned accesses as NMI is probably not the right way. For instance, loongarch treats them as normal IRQ using a irqentry_enter()/exit() and reenabling IRQS if possible.

Moreover, it looks like to me that misaligned access traps are similar (in the way they can be handled) to how the page fault are handled in RISC-V. It uses irqentry_enter()/exit() and reenables interrupts if needed. Additionally, the misaligned access handling does will not take any locks if handling a misaligned access taken while in kernel so even if we interrupt a kernel critical section that does not have interrupts disable, we are good to go. But my understanding might be wrong as well and I might be missing specific cases :)

Thanks,

Clément

https://lore.kernel.org/lkml/[email protected]/
[1]

Quoted 89 lines

Thanks,

Alex

Quoted 83 lines

Since both load/store and user/kernel
should use almost the same path and that we are going to add some code around that, factorize it.

---
  arch/riscv/kernel/traps.c | 49 ++++++++++++++++-----------------------

  1 file changed, 20 insertions(+), 29 deletions(-)
arch/riscv/kernel/traps.c +0 -0
index 8ff8e8b36524..55d9f3450398 100644
@@ -198,47 +198,38 @@ asmlinkage __visible __trap_section void

do_trap_insn_illegal(struct pt_regs *re
  DO_ERROR_INFO(do_trap_load_fault,
      SIGSEGV, SEGV_ACCERR, "load access fault");
  -asmlinkage __visible __trap_section void
do_trap_load_misaligned(struct pt_regs *regs)

arch/riscv/kernel/traps.c +6 -0
+enum misaligned_access_type {
+    MISALIGNED_STORE,
+    MISALIGNED_LOAD,
+};
+
+static void do_trap_misaligned(struct pt_regs *regs, enum

misaligned_access_type type)
  {
-    if (user_mode(regs)) {
-        irqentry_enter_from_user_mode(regs);
+    irqentry_state_t state = irqentry_enter(regs);
  +    if (type ==  MISALIGNED_LOAD) {
          if (handle_misaligned_load(regs)) do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc, -                  "Oops - load address misaligned");
-
-        irqentry_exit_to_user_mode(regs);
+                      "Oops - load address misaligned"); } else {
-        irqentry_state_t state = irqentry_nmi_enter(regs); -
-        if (handle_misaligned_load(regs))
+        if (handle_misaligned_store(regs)) do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc, -                  "Oops - load address misaligned");
-
-        irqentry_nmi_exit(regs, state);
+                      "Oops - store (or AMO) address misaligned"); }
+
+    irqentry_exit(regs, state);
  }
  -asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs) +asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
  {
-    if (user_mode(regs)) {
-        irqentry_enter_from_user_mode(regs);

arch/riscv/kernel/traps.c +2 -12
-
-        if (handle_misaligned_store(regs))
-            do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-                "Oops - store (or AMO) address misaligned");
-
-        irqentry_exit_to_user_mode(regs);
-    } else {
-        irqentry_state_t state = irqentry_nmi_enter(regs);
-
-        if (handle_misaligned_store(regs))
-            do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
-                "Oops - store (or AMO) address misaligned");
+    do_trap_misaligned(regs, MISALIGNED_LOAD);
+}

  -        irqentry_nmi_exit(regs, state);
-    }
+asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
+{
+    do_trap_misaligned(regs, MISALIGNED_STORE);
  }
+
  DO_ERROR_INFO(do_trap_store_fault,
      SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault"); DO_ERROR_INFO(do_trap_ecall_s,

#4 lore raw

Re: [PATCH 1/5] riscv: misaligned: factorize trap handling

Quoted 18 lines · Clément Léger
Quoted 8 lines · Alexandre Ghiti

Hi Clément,

Quoted 2 lines · Clément Léger

misaligned accesses traps are not nmi and should be treated as normal one using irqentry_enter()/exit().

Quoted 6 lines

All the traps that come from kernel mode are treated as nmi as it was suggested by Peter here: https://lore.kernel.org/linux-riscv/
[email protected]/

I don't know the differences between irq_nmi_entry/exit() and irq_entry/ exit(), so is that still correct to now treat the kernel traps as non-nmi?

Quoted 8 lines

Hi Alex,

Actually, this discussion was raised on a previous series [1] by Maciej which replied that we should actually reenable interrupt depending on the state that was interrupted. Looking at other architecture/code, it seems like treating misaligned accesses as NMI is probably not the right way. For instance, loongarch treats them as normal IRQ using a irqentry_enter()/exit() and reenabling IRQS if possible.

So, a trap that happens in kernel space while IRQs are disabled, SHOULD really be NMI-like.

You then have a choice, make all such traps from kernel space NMI-like; this makes it easy on the trap handler, since the context is always the same. Mistakes are 'easy' to find.

Or,.. do funny stuff and only make it NMI like if IRQs were disabled. Which gives inconsistent context for the handler and you'll find yourself scratching your head at some point in the future wondering why this one rare occasion goes BOOM.

x86 mostly does the first, any trap that can happen with IRQs disabled is treated unconditionally as NMI like. The obvious exception is page-fault, but that already has a from-non-preemptible-context branch that is 'careful'.

As to unaligned traps from kernel space, I would imagine they mostly BUG the kernel, except when there's an exception entry for that location, in which case it might do a fixup?

Anyway, the reason these exceptions should be NMI like, is because interrupts are not allowed to nest. Notably something like:

  raw_spin_lock_irqsave(&foo);
  <IRQ>
    raw_spin_lock_irqsave(&foo);
    ...

Is an obvious problem. Exceptions that can run while IRQs are disabled, must not use locks -- treating them as NMI-like (they are non-maskable after all), ensures this.

#5 lore raw

Re: [PATCH 1/5] riscv: misaligned: factorize trap handling

Quoted 31 lines · Peter Zijlstra
Quoted 18 lines · Clément Léger
Quoted 8 lines · Alexandre Ghiti

Hi Clément,

Quoted 2 lines · Clément Léger

misaligned accesses traps are not nmi and should be treated as normal one using irqentry_enter()/exit().

Quoted 6 lines

All the traps that come from kernel mode are treated as nmi as it was suggested by Peter here: https://lore.kernel.org/linux-riscv/
[email protected]/

I don't know the differences between irq_nmi_entry/exit() and irq_entry/ exit(), so is that still correct to now treat the kernel traps as non-nmi?

Quoted 8 lines

Hi Alex,

Actually, this discussion was raised on a previous series [1] by Maciej which replied that we should actually reenable interrupt depending on the state that was interrupted. Looking at other architecture/code, it seems like treating misaligned accesses as NMI is probably not the right way. For instance, loongarch treats them as normal IRQ using a irqentry_enter()/exit() and reenabling IRQS if possible.

So, a trap that happens in kernel space while IRQs are disabled, SHOULD really be NMI-like.

Quoted 8 lines

You then have a choice, make all such traps from kernel space NMI-like; this makes it easy on the trap handler, since the context is always the same. Mistakes are 'easy' to find.

Or,.. do funny stuff and only make it NMI like if IRQs were disabled. Which gives inconsistent context for the handler and you'll find yourself scratching your head at some point in the future wondering why this one rare occasion goes BOOM.

Hi Peter,

Yeah agreed, that might be hazardous.

Quoted 9 lines

x86 mostly does the first, any trap that can happen with IRQs disabled is treated unconditionally as NMI like. The obvious exception is page-fault, but that already has a from-non-preemptible-context branch that is 'careful'.

As to unaligned traps from kernel space, I would imagine they mostly BUG the kernel, except when there's an exception entry for that location, in which case it might do a fixup?

The misaligned access exception handling currently handles misaligned access for the kernel as well (except if explicitly disabled).

Quoted 6 lines

Anyway, the reason these exceptions should be NMI like, is because interrupts are not allowed to nest. Notably something like:

Quoted 8 lines
  raw_spin_lock_irqsave(&foo);
  <IRQ>
    raw_spin_lock_irqsave(&foo);
    ...

Is an obvious problem. Exceptions that can run while IRQs are disabled, must not use locks -- treating them as NMI-like (they are non-maskable after all), ensures this.

As said in my previous reply, the misaligned handling code does not currently access any locks (when handling kernel misaligned accesses) and is self contained. That being said, that assumption might not be true in future so that might be better to take your approach and treat the misaligned access like an NMI.

Thanks,

Clément

#6 lore raw

Re: [PATCH 1/5] riscv: misaligned: factorize trap handling

Quoted 11 lines · Clément Léger
Quoted 8 lines

x86 mostly does the first, any trap that can happen with IRQs disabled is treated unconditionally as NMI like. The obvious exception is page-fault, but that already has a from-non-preemptible-context branch that is 'careful'.

As to unaligned traps from kernel space, I would imagine they mostly BUG the kernel, except when there's an exception entry for that location, in which case it might do a fixup?

The misaligned access exception handling currently handles misaligned access for the kernel as well (except if explicitly disabled).

It's currently not clear that a kernel mode unaligned access is indeed a
bug, as some network protocol stacks may still rely on unaligned accesses for performance reasons for the regular case where network headers do come out aligned[1][2].

Hopefully not in the hardirq context though, and the usual approach is to
keep interrupts disabled in the emulation path if arriving from the kernel mode as we don't expect kernel code to be ever paged out (the same applies to all kinds of machine instruction emulation).

References:

[1] "TCP SYNs broken in 2.3.41",

    <https://marc.info/?l=linux-kernel&m=94927689929463>

[2] "Alpha: Emulate unaligned LDx_L/STx_C for data consistency",

    <https://lore.kernel.org/lkml/[email protected]/>

HTH,

Maciej

#7 lore raw

Re: [PATCH 1/5] riscv: misaligned: factorize trap handling

Quoted 4 lines · Maciej W. Rozycki

Hopefully not in the hardirq context though, and the usual approach is to
keep interrupts disabled in the emulation path if arriving from the kernel mode as we don't expect kernel code to be ever paged out (the same applies to all kinds of machine instruction emulation).

s/code/data/, obviously.

Maciej

#8 lore raw

PATCH 2/5 riscv: misaligned: enable IRQs while handling misaligned accesses

In reply to Clément Léger

We can safely reenable IRQs if they were enabled in the previous context. This allows to access user memory that could potentially trigger a page fault.

Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to kernel memory")
---
 arch/riscv/kernel/traps.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/kernel/traps.c +8 -0
index 55d9f3450398..3eecc2addc41 100644
@@ -206,6 +206,11 @@ enum misaligned_access_type {
 static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type type)
 {
 	irqentry_state_t state = irqentry_enter(regs);
+	bool enable_irqs = !regs_irqs_disabled(regs);
+
+	/* Enable interrupts if they were enabled in the interrupted context. */
+	if (enable_irqs)
+		local_irq_enable();
 
 	if (type ==  MISALIGNED_LOAD) {
 		if (handle_misaligned_load(regs))
@@ -217,6 +222,9 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
 				      "Oops - store (or AMO) address misaligned");
 	}
 
+	if (enable_irqs)
+		local_irq_disable();
+
 	irqentry_exit(regs, state);
 }
 
Signature · 1 line
2.49.0
reviewer Reviewed-by on v1 2/5
#9 lore raw

Re: [PATCH 2/5] riscv: misaligned: enable IRQs while handling misaligned accesses

Quoted 27 lines · Clément Léger

We can safely reenable IRQs if they were enabled in the previous context. This allows to access user memory that could potentially trigger a page fault.

Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to kernel memory")
---
  arch/riscv/kernel/traps.c | 8 ++++++++
  1 file changed, 8 insertions(+)
arch/riscv/kernel/traps.c +5 -0
index 55d9f3450398..3eecc2addc41 100644
@@ -206,6 +206,11 @@ enum misaligned_access_type {
  static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type type)
  {
  	irqentry_state_t state = irqentry_enter(regs);
+	bool enable_irqs = !regs_irqs_disabled(regs);
+
+	/* Enable interrupts if they were enabled in the interrupted context. */
+	if (enable_irqs)
+		local_irq_enable();
 
  	if (type ==  MISALIGNED_LOAD) {
  		if (handle_misaligned_load(regs))

arch/riscv/kernel/traps.c static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type · line 230

Quoted 9 lines
arch/riscv/kernel/traps.c +3 -0
@@ -217,6 +222,9 @@ static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type
  				      "Oops - store (or AMO) address misaligned");
  	}
 
+	if (enable_irqs)
+		local_irq_disable();
+
  	irqentry_exit(regs, state);
  }
 

Thanks,

Alex

#10 lore raw

PATCH 3/5 riscv: misaligned: use get_user() instead of __get_user()

In reply to Clément Léger

Now that we can safely handle user memory accesses while in the misaligned access handlers, use get_user() instead of __get_user() to have user memory access checks.

---
 arch/riscv/kernel/traps_misaligned.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps_misaligned.c +1 -1
index 4354c87c0376..97c674d7d34f 100644
@@ -268,7 +268,7 @@ static unsigned long get_f32_rs(unsigned long insn, u8 fp_reg_offset,
 	int __ret;					\
 							\
 	if (user_mode(regs)) {				\
-		__ret = __get_user(insn, (type __user *) insn_addr); \
+		__ret = get_user(insn, (type __user *) insn_addr); \
 	} else {					\
 		insn = *(type *)insn_addr;		\
 		__ret = 0;				\
Signature · 1 line
2.49.0
reviewer Reviewed-by on v1 3/5
#11 lore raw

Re: [PATCH 3/5] riscv: misaligned: use get_user() instead of __get_user()

Quoted 14 lines · Clément Léger

Now that we can safely handle user memory accesses while in the misaligned access handlers, use get_user() instead of __get_user() to have user memory access checks.

---
  arch/riscv/kernel/traps_misaligned.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/traps_misaligned.c +0 -0
index 4354c87c0376..97c674d7d34f 100644

arch/riscv/kernel/traps_misaligned.c static unsigned long get_f32_rs(unsigned long insn, u8 fp_reg_offset, · line 274

Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -268,7 +268,7 @@ static unsigned long get_f32_rs(unsigned long insn, u8 fp_reg_offset,
  	int __ret;					\
  							\
  	if (user_mode(regs)) {				\
-		__ret = __get_user(insn, (type __user *) insn_addr); \
+		__ret = get_user(insn, (type __user *) insn_addr); \
  	} else {					\
  		insn = *(type *)insn_addr;		\
  		__ret = 0;				\

Thanks,

Alex

#12 lore raw

PATCH 4/5 Documentation/sysctl: add riscv to unaligned-trap supported archs

In reply to Clément Léger

riscv supports the "unaligned-trap" sysctl variable, add it to the list of supported architectures.

---
 Documentation/admin-guide/sysctl/kernel.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/admin-guide/sysctl/kernel.rst +2 -2
index dd49a89a62d3..a38e91c4d92c 100644
@@ -1595,8 +1595,8 @@ unaligned-trap
 
 On architectures where unaligned accesses cause traps, and where this
 feature is supported (``CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW``; currently,
-``arc``, ``parisc`` and ``loongarch``), controls whether unaligned traps
-are caught and emulated (instead of failing).
+``arc``, ``parisc``, ``loongarch`` and ``riscv``), controls whether unaligned
+traps are caught and emulated (instead of failing).
 
 = ========================================================
 0 Do not emulate unaligned accesses.
Signature · 1 line
2.49.0
reviewer Reviewed-by on v1 4/5
#13 lore raw

Re: [PATCH 4/5] Documentation/sysctl: add riscv to unaligned-trap supported archs

Quoted 13 lines · Clément Léger

riscv supports the "unaligned-trap" sysctl variable, add it to the list of supported architectures.

---
  Documentation/admin-guide/sysctl/kernel.rst | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Documentation/admin-guide/sysctl/kernel.rst +0 -0
index dd49a89a62d3..a38e91c4d92c 100644

Documentation/admin-guide/sysctl/kernel.rst unaligned-trap · line 1602

Quoted 10 lines
Documentation/admin-guide/sysctl/kernel.rst +2 -2
@@ -1595,8 +1595,8 @@ unaligned-trap
 
  On architectures where unaligned accesses cause traps, and where this
  feature is supported (``CONFIG_SYSCTL_ARCH_UNALIGN_ALLOW``; currently,
-``arc``, ``parisc`` and ``loongarch``), controls whether unaligned traps
-are caught and emulated (instead of failing).
+``arc``, ``parisc``, ``loongarch`` and ``riscv``), controls whether unaligned
+traps are caught and emulated (instead of failing).
 
  = ========================================================
  0 Do not emulate unaligned accesses.

Thanks,

Alex

#14 lore raw

PATCH 5/5 selftests: riscv: add misaligned access testing

In reply to Clément Léger

This selftest tests (almost) all the currently emulated instruction (except for the RV32 compressed ones which are left as a future exercise for a RV32 user). For the FPU instructions, all the FPU registers are tested.

---
 .../selftests/riscv/misaligned/.gitignore     |   1 +
 .../selftests/riscv/misaligned/Makefile       |  12 +
 .../selftests/riscv/misaligned/common.S       |  33 +++
 .../testing/selftests/riscv/misaligned/fpu.S  | 180 +++++++++++++
 tools/testing/selftests/riscv/misaligned/gp.S | 103 +++++++
 .../selftests/riscv/misaligned/misaligned.c   | 254 ++++++++++++++++++
 6 files changed, 583 insertions(+)
 create mode 100644 tools/testing/selftests/riscv/misaligned/.gitignore
 create mode 100644 tools/testing/selftests/riscv/misaligned/Makefile
 create mode 100644 tools/testing/selftests/riscv/misaligned/common.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/fpu.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/gp.S
 create mode 100644 tools/testing/selftests/riscv/misaligned/misaligned.c
tools/testing/selftests/riscv/misaligned/.gitignore new file +1 -0
new file mode 100644
index 000000000000..5eff15a1f981
@@ -0,0 +1 @@
+misaligned
tools/testing/selftests/riscv/misaligned/Makefile new file +12 -0
new file mode 100644
index 000000000000..1aa40110c50d
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2021 ARM Limited
+# Originally tools/testing/arm64/abi/Makefile
+
+CFLAGS += -I$(top_srcdir)/tools/include
+
+TEST_GEN_PROGS := misaligned
+
+include ../../lib.mk
+
+$(OUTPUT)/misaligned: misaligned.c fpu.S gp.S
+	$(CC) -g3 -static -o$@ -march=rv64imafdc $(CFLAGS) $(LDFLAGS) $^
tools/testing/selftests/riscv/misaligned/common.S new file +33 -0
new file mode 100644
index 000000000000..8fa00035bd5d
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2025 Rivos Inc.
+ *
+ * Authors:
+ *     Clément Léger <[email protected]>
+ */
+
+.macro lb_sb temp, offset, src, dst
+	lb \temp, \offset(\src)
+	sb \temp, \offset(\dst)
+.endm
+
+.macro copy_long_to temp, src, dst
+	lb_sb \temp, 0, \src, \dst,
+	lb_sb \temp, 1, \src, \dst,
+	lb_sb \temp, 2, \src, \dst,
+	lb_sb \temp, 3, \src, \dst,
+	lb_sb \temp, 4, \src, \dst,
+	lb_sb \temp, 5, \src, \dst,
+	lb_sb \temp, 6, \src, \dst,
+	lb_sb \temp, 7, \src, \dst,
+.endm
+
+.macro sp_stack_prologue offset
+	addi sp, sp, -8
+	sub sp, sp, \offset
+.endm
+
+.macro sp_stack_epilogue offset
+	add sp, sp, \offset
+	addi sp, sp, 8
+.endm
tools/testing/selftests/riscv/misaligned/fpu.S new file +180 -0

180 lines not on this page · raw message

tools/testing/selftests/riscv/misaligned/gp.S new file +103 -0

103 lines not on this page · raw message

tools/testing/selftests/riscv/misaligned/misaligned.c new file +254 -0

254 lines not on this page · raw message

Signature · 1 line
2.49.0