Skip to contentSkip to conversation

riscv: misaligned: Add ZCB handling and fix sleeping function

PATCH In review Nylon Chen 3 participants 10 messages Apr 11, 2025 – Apr 29, 2025 RISC-V

lore
Share & tools
lore

Nylon Chen

author
#0 lore raw

cover riscv: misaligned: Add ZCB handling and fix sleeping function

1. Adds support for ZCB compressed instructions (C.LHU, C.LH, C.SH).

2. Fixes a bug where copy_from/to_user() calls in non-sleepable contexts triggered attempts to sleep.

Nylon Chen (2):
riscv: misaligned: Add handling for ZCB instructions
riscv: misaligned: fix sleeping function called during misaligned
access handling

 arch/riscv/kernel/traps_misaligned.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 riscv: misaligned: Add handling for ZCB instructions

Add support for the Zcb extension's compressed half-word instructions (C.LHU, C.LH, and C.SH) in the RISC-V misaligned access trap handler.

---
 arch/riscv/kernel/traps_misaligned.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/riscv/kernel/traps_misaligned.c +17 -0
index 7cc108aed74e..d7275dfb6b7e 100644
@@ -88,6 +88,13 @@
 #define INSN_MATCH_C_FSWSP		0xe002
 #define INSN_MASK_C_FSWSP		0xe003
 
+#define INSN_MATCH_C_LHU		0x8400
+#define INSN_MASK_C_LHU			0xfc43
+#define INSN_MATCH_C_LH			0x8440
+#define INSN_MASK_C_LH			0xfc43
+#define INSN_MATCH_C_SH			0x8c00
+#define INSN_MASK_C_SH			0xfc43
+
 #define INSN_LEN(insn)			((((insn) & 0x3) < 0x3) ? 2 : 4)
 
 #if defined(CONFIG_64BIT)
@@ -431,6 +438,13 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 		fp = 1;
 		len = 4;
 #endif
+	} else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+		len = 2;
+		insn = RVC_RS2S(insn) << SH_RD;
+	} else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+		len = 2;
+		shift = 8 * (sizeof(ulong) - len);
+		insn = RVC_RS2S(insn) << SH_RD;
 	} else {
 		regs->epc = epc;
 		return -1;
@@ -530,6 +544,9 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
 		len = 4;
 		val.data_ulong = GET_F32_RS2C(insn, regs);
 #endif
+	} else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+		len = 2;
+		val.data_ulong = GET_RS2S(insn, regs);
 	} else {
 		regs->epc = epc;
 		return -1;
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] riscv: misaligned: Add handling for ZCB instructions

Hi Nylon,

Quoted 5 lines · Nylon Chen

Add support for the Zcb extension's compressed half-word instructions (C.LHU, C.LH, and C.SH) in the RISC-V misaligned access trap handler.

I would add the following fixes tag:

Fixes 956d705dd279 ("riscv: Unaligned load/store handling for M_MODE")
Quoted 37 lines
---
  arch/riscv/kernel/traps_misaligned.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
arch/riscv/kernel/traps_misaligned.c +14 -0
index 7cc108aed74e..d7275dfb6b7e 100644
@@ -88,6 +88,13 @@
  #define INSN_MATCH_C_FSWSP		0xe002
  #define INSN_MASK_C_FSWSP		0xe003
 
+#define INSN_MATCH_C_LHU		0x8400
+#define INSN_MASK_C_LHU			0xfc43
+#define INSN_MATCH_C_LH			0x8440
+#define INSN_MASK_C_LH			0xfc43
+#define INSN_MATCH_C_SH			0x8c00
+#define INSN_MASK_C_SH			0xfc43
+
  #define INSN_LEN(insn)			((((insn) & 0x3) < 0x3) ? 2 : 4)
 
  #if defined(CONFIG_64BIT)
@@ -431,6 +438,13 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
  		fp = 1;
  		len = 4;
  #endif
+	} else if ((insn & INSN_MASK_C_LHU) == INSN_MATCH_C_LHU) {
+		len = 2;
+		insn = RVC_RS2S(insn) << SH_RD;
+	} else if ((insn & INSN_MASK_C_LH) == INSN_MATCH_C_LH) {
+		len = 2;
+		shift = 8 * (sizeof(ulong) - len);
+		insn = RVC_RS2S(insn) << SH_RD;
  	} else {
  		regs->epc = epc;
  		return -1;

arch/riscv/kernel/traps_misaligned.c static int handle_scalar_misaligned_store(struct pt_regs *regs) · line 552

Quoted 9 lines
arch/riscv/kernel/traps_misaligned.c +3 -0
@@ -530,6 +544,9 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
  		len = 4;
  		val.data_ulong = GET_F32_RS2C(insn, regs);
  #endif
+	} else if ((insn & INSN_MASK_C_SH) == INSN_MATCH_C_SH) {
+		len = 2;
+		val.data_ulong = GET_RS2S(insn, regs);
  	} else {
  		regs->epc = epc;
  		return -1;

Thanks,

Alex

author
#3 lore raw

PATCH 2/2 riscv: misaligned: fix sleeping function called during misaligned access handling

In reply to Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
 [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
 [<ffffffff80002304>] show_stack+0x28/0x34
 [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
 [<ffffffff80010fe0>] dump_stack+0x14/0x1c
 [<ffffffff8004e44e>] __might_resched+0xfa/0x104
 [<ffffffff8004e496>] __might_sleep+0x3e/0x62
 [<ffffffff801963c4>] __might_fault+0x1c/0x24
 [<ffffffff80425352>] _copy_from_user+0x28/0xaa
 [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
 [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
 [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to kernel memory")
Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE specific code")
---
 arch/riscv/kernel/traps_misaligned.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/traps_misaligned.c +2 -2
index d7275dfb6b7e..563f73f88fa8 100644
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 
 	val.data_u64 = 0;
 	if (user_mode(regs)) {
-		if (copy_from_user(&val, (u8 __user *)addr, len))
+		if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
 			return -1;
 	} else {
 		memcpy(&val, (u8 *)addr, len);
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
 		return -EOPNOTSUPP;
 
 	if (user_mode(regs)) {
-		if (copy_to_user((u8 __user *)addr, &val, len))
+		if (copy_to_user_nofault((u8 __user *)addr, &val, len))
 			return -1;
 	} else {
 		memcpy((u8 *)addr, &val, len);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] riscv: misaligned: fix sleeping function called during misaligned access handling

Hi Nylon,

I already have a pending fix for that bug which is to reenable interrupts while handling misaligned faults. Please see:
https://lore.kernel.org/linux-riscv/[email protected]/

Thanks,

Clément

Quoted 55 lines · Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
 [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
 [<ffffffff80002304>] show_stack+0x28/0x34
 [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
 [<ffffffff80010fe0>] dump_stack+0x14/0x1c
 [<ffffffff8004e44e>] __might_resched+0xfa/0x104
 [<ffffffff8004e496>] __might_sleep+0x3e/0x62
 [<ffffffff801963c4>] __might_fault+0x1c/0x24
 [<ffffffff80425352>] _copy_from_user+0x28/0xaa
 [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
 [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
 [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to kernel memory")
Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE specific code")
---
 arch/riscv/kernel/traps_misaligned.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/traps_misaligned.c +2 -2
index d7275dfb6b7e..563f73f88fa8 100644
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 
 	val.data_u64 = 0;
 	if (user_mode(regs)) {
-		if (copy_from_user(&val, (u8 __user *)addr, len))
+		if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
 			return -1;
 	} else {
 		memcpy(&val, (u8 *)addr, len);
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
 		return -EOPNOTSUPP;
 
 	if (user_mode(regs)) {
-		if (copy_to_user((u8 __user *)addr, &val, len))
+		if (copy_to_user_nofault((u8 __user *)addr, &val, len))
 			return -1;
 	} else {
 		memcpy((u8 *)addr, &val, len);
author
#5 lore raw

Re: [PATCH 2/2] riscv: misaligned: fix sleeping function called during misaligned access handling

Hi Clément,

Thanks for your information

I will test your patch as well, and if no other issues arise, I'll remove this change from the patchset in the next version

Thanks

Nylon
Clément Léger <[email protected]> 於 2025年4月11日 週五 下午3:37寫道:

Quoted 68 lines

Hi Nylon,

I already have a pending fix for that bug which is to reenable interrupts while handling misaligned faults. Please see:
https://lore.kernel.org/linux-riscv/[email protected]/

Thanks,

Clément

Quoted 55 lines · Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
 [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
 [<ffffffff80002304>] show_stack+0x28/0x34
 [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
 [<ffffffff80010fe0>] dump_stack+0x14/0x1c
 [<ffffffff8004e44e>] __might_resched+0xfa/0x104
 [<ffffffff8004e496>] __might_sleep+0x3e/0x62
 [<ffffffff801963c4>] __might_fault+0x1c/0x24
 [<ffffffff80425352>] _copy_from_user+0x28/0xaa
 [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
 [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
 [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to kernel memory")
Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE specific code")
---
 arch/riscv/kernel/traps_misaligned.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/traps_misaligned.c +2 -2
index d7275dfb6b7e..563f73f88fa8 100644
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 
      val.data_u64 = 0;
      if (user_mode(regs)) {
-             if (copy_from_user(&val, (u8 __user *)addr, len))
+             if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
                      return -1;
      } else {
              memcpy(&val, (u8 *)addr, len);
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
              return -EOPNOTSUPP;
 
      if (user_mode(regs)) {
-             if (copy_to_user((u8 __user *)addr, &val, len))
+             if (copy_to_user_nofault((u8 __user *)addr, &val, len))
                      return -1;
      } else {
              memcpy((u8 *)addr, &val, len);
#6 lore raw

Re: [PATCH 2/2] riscv: misaligned: fix sleeping function called during misaligned access handling

In reply to Clément Léger

Hi Clément,

Quoted 5 lines · Clément Léger

Hi Nylon,

I already have a pending fix for that bug which is to reenable interrupts while handling misaligned faults. Please see:
https://lore.kernel.org/linux-riscv/[email protected]/

Can you extract this fix from the series so that it can be merged in 6.15?

Thanks,

Alex

Quoted 66 lines

Thanks,

Clément

Quoted 47 lines · Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
  [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
  [<ffffffff80002304>] show_stack+0x28/0x34
  [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
  [<ffffffff80010fe0>] dump_stack+0x14/0x1c
  [<ffffffff8004e44e>] __might_resched+0xfa/0x104
  [<ffffffff8004e496>] __might_sleep+0x3e/0x62
  [<ffffffff801963c4>] __might_fault+0x1c/0x24
  [<ffffffff80425352>] _copy_from_user+0x28/0xaa
  [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
  [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
  [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to kernel memory")
Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE specific code")
---
  arch/riscv/kernel/traps_misaligned.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/kernel/traps_misaligned.c +1 -1
index d7275dfb6b7e..563f73f88fa8 100644
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct pt_regs *regs)
 
  	val.data_u64 = 0;
  	if (user_mode(regs)) {
-		if (copy_from_user(&val, (u8 __user *)addr, len))
+		if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
  			return -1;
  	} else {
  		memcpy(&val, (u8 *)addr, len);

arch/riscv/kernel/traps_misaligned.c static int handle_scalar_misaligned_store(struct pt_regs *regs) · line 562

Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct pt_regs *regs)
  		return -EOPNOTSUPP;
 
  	if (user_mode(regs)) {
-		if (copy_to_user((u8 __user *)addr, &val, len))
+		if (copy_to_user_nofault((u8 __user *)addr, &val, len))
  			return -1;
  	} else {
  		memcpy((u8 *)addr, &val, len);

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

#7 lore raw

Re: [PATCH 2/2] riscv: misaligned: fix sleeping function called during misaligned access handling

Quoted 5 lines · Alexandre Ghiti

Hi Clément,

Quoted 2 lines · Clément Léger

Hi Nylon,

Quoted 7 lines
Quoted 4 lines

I already have a pending fix for that bug which is to reenable interrupts while handling misaligned faults. Please see:
https://lore.kernel.org/linux-riscv/20250317170625.1142870-12-
[email protected]/

Can you extract this fix from the series so that it can be merged in 6.15?

Hi Alex,

Yes sure, I can send a small series as well. However, I'd like the associated kselftest to be reviewed since it would allow to catch such behavior (there is no test for misaligned delegation yet).

Thanks,

Clément

Quoted 76 lines

Thanks,

Alex

Quoted 70 lines

Thanks,

Clément

Quoted 51 lines · Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/
linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
  [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
  [<ffffffff80002304>] show_stack+0x28/0x34
  [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
  [<ffffffff80010fe0>] dump_stack+0x14/0x1c
  [<ffffffff8004e44e>] __might_resched+0xfa/0x104
  [<ffffffff8004e496>] __might_sleep+0x3e/0x62
  [<ffffffff801963c4>] __might_fault+0x1c/0x24
  [<ffffffff80425352>] _copy_from_user+0x28/0xaa
  [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
  [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
  [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to

kernel memory")

Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE

specific code")

---
  arch/riscv/kernel/traps_misaligned.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/ renamed from arch/riscv/kernel/traps_misaligned.c +0 -0

kernel/traps_misaligned.c
index d7275dfb6b7e..563f73f88fa8 100644

arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct
 pt_regs *regs)
         val.data_u64 = 0;
       if (user_mode(regs)) {
-        if (copy_from_user(&val, (u8 __user *)addr, len))
+        if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
               return -1;
       } else {
           memcpy(&val, (u8 *)addr, len);

arch/riscv/kernel/traps_misaligned.c static int handle_scalar_misaligned_store(struct · line 562

Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct
 pt_regs *regs)
           return -EOPNOTSUPP;
         if (user_mode(regs)) {
-        if (copy_to_user((u8 __user *)addr, &val, len))
+        if (copy_to_user_nofault((u8 __user *)addr, &val, len))
               return -1;
       } else {
           memcpy((u8 *)addr, &val, len);

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

author
#8 lore raw

Re: [PATCH 2/2] riscv: misaligned: fix sleeping function called during misaligned access handling

Hi Clément,

Thank you for sharing your patch. I’ve reviewed the changes, but I’m not sure I fully grasp the design rationale.
Could you please briefly explain the main considerations behind this modification?

We’re also discussing internally the differences between my _nofault approach and your IRQ-enable approach, and I’d appreciate your perspective on the pros and cons of each.

Looking forward to your suggestions!

Nylon

Clément Léger <[email protected]> 於 2025年4月11日 週五 下午4:38寫道:

Quoted 104 lines · Alexandre Ghiti
Quoted 5 lines

Hi Clément,

Quoted 2 lines · Clément Léger

Hi Nylon,

Quoted 7 lines
Quoted 4 lines

I already have a pending fix for that bug which is to reenable interrupts while handling misaligned faults. Please see:
https://lore.kernel.org/linux-riscv/20250317170625.1142870-12-
[email protected]/

Can you extract this fix from the series so that it can be merged in 6.15?

Hi Alex,

Yes sure, I can send a small series as well. However, I'd like the associated kselftest to be reviewed since it would allow to catch such behavior (there is no test for misaligned delegation yet).

Thanks,

Clément

Quoted 76 lines

Thanks,

Alex

Quoted 70 lines

Thanks,

Clément

Quoted 51 lines · Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/
linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
  [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
  [<ffffffff80002304>] show_stack+0x28/0x34
  [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
  [<ffffffff80010fe0>] dump_stack+0x14/0x1c
  [<ffffffff8004e44e>] __might_resched+0xfa/0x104
  [<ffffffff8004e496>] __might_sleep+0x3e/0x62
  [<ffffffff801963c4>] __might_fault+0x1c/0x24
  [<ffffffff80425352>] _copy_from_user+0x28/0xaa
  [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
  [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
  [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to

kernel memory")

Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE

specific code")

---
  arch/riscv/kernel/traps_misaligned.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/ renamed from arch/riscv/kernel/traps_misaligned.c +0 -0

kernel/traps_misaligned.c
index d7275dfb6b7e..563f73f88fa8 100644

arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct
 pt_regs *regs)
        val.data_u64 = 0;
      if (user_mode(regs)) {
-        if (copy_from_user(&val, (u8 __user *)addr, len))
+        if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
              return -1;
      } else {
          memcpy(&val, (u8 *)addr, len);

arch/riscv/kernel/traps_misaligned.c static int handle_scalar_misaligned_store(struct · line 562

Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct
 pt_regs *regs)
          return -EOPNOTSUPP;
        if (user_mode(regs)) {
-        if (copy_to_user((u8 __user *)addr, &val, len))
+        if (copy_to_user_nofault((u8 __user *)addr, &val, len))
              return -1;
      } else {
          memcpy((u8 *)addr, &val, len);

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

#9 lore raw

Re: [PATCH 2/2] riscv: misaligned: fix sleeping function called during misaligned access handling

Quoted 10 lines · Nylon Chen

Hi Clément,

Thank you for sharing your patch. I’ve reviewed the changes, but I’m not sure I fully grasp the design rationale.
Could you please briefly explain the main considerations behind this modification?

We’re also discussing internally the differences between my _nofault approach and your IRQ-enable approach, and I’d appreciate your perspective on the pros and cons of each.

Hi Nylon,

There is not really pro vs cons. Using nofault would lead to failure to read from user memory that is paged out for instance. This is not really acceptable, we should handle user misaligned access even at an address that would generate a page fault. One way to do so is to reenable IRQs when coming from userspace and use copy_from/to_user() since it can sleep while accessing memory. My latest version of the series [1] now reenables interrupts only when coming from userspace.

Thanks,

Clément

[1]
https://lore.kernel.org/linux-riscv/[email protected]/

Quoted 110 lines

Looking forward to your suggestions!

Nylon

Clément Léger <[email protected]> 於 2025年4月11日 週五 下午4:38寫道:

Quoted 104 lines · Alexandre Ghiti
Quoted 5 lines

Hi Clément,

Quoted 2 lines · Clément Léger

Hi Nylon,

Quoted 7 lines
Quoted 4 lines

I already have a pending fix for that bug which is to reenable interrupts while handling misaligned faults. Please see:
https://lore.kernel.org/linux-riscv/20250317170625.1142870-12-
[email protected]/

Can you extract this fix from the series so that it can be merged in 6.15?

Hi Alex,

Yes sure, I can send a small series as well. However, I'd like the associated kselftest to be reviewed since it would allow to catch such behavior (there is no test for misaligned delegation yet).

Thanks,

Clément

Quoted 76 lines

Thanks,

Alex

Quoted 70 lines

Thanks,

Clément

Quoted 51 lines · Nylon Chen

Use copy_from_user_nofault() and copy_to_user_nofault() instead of copy_from/to_user functions in the misaligned access trap handlers.

The following bug report was found when executing misaligned memory accesses:

BUG: sleeping function called from invalid context at ./include/
linux/uaccess.h:162
in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 115, name: two
preempt_count: 0, expected: 0
CPU: 0 UID: 0 PID: 115 Comm: two Not tainted 6.14.0-rc5 #24
Hardware name: riscv-virtio,qemu (DT)
Call Trace:
  [<ffffffff800160ea>] dump_backtrace+0x1c/0x24
  [<ffffffff80002304>] show_stack+0x28/0x34
  [<ffffffff80010fae>] dump_stack_lvl+0x4a/0x68
  [<ffffffff80010fe0>] dump_stack+0x14/0x1c
  [<ffffffff8004e44e>] __might_resched+0xfa/0x104
  [<ffffffff8004e496>] __might_sleep+0x3e/0x62
  [<ffffffff801963c4>] __might_fault+0x1c/0x24
  [<ffffffff80425352>] _copy_from_user+0x28/0xaa
  [<ffffffff8000296c>] handle_misaligned_store+0x204/0x254
  [<ffffffff809eae82>] do_trap_store_misaligned+0x24/0xee
  [<ffffffff809f4f1a>] handle_exception+0x146/0x152
Fixes b686ecdeacf6 ("riscv: misaligned: Restrict user access to

kernel memory")

Fixes 441381506ba7 ("riscv: misaligned: remove CONFIG_RISCV_M_MODE

specific code")

---
  arch/riscv/kernel/traps_misaligned.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
arch/riscv/ renamed from arch/riscv/kernel/traps_misaligned.c +0 -0

kernel/traps_misaligned.c
index d7275dfb6b7e..563f73f88fa8 100644

arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -455,7 +455,7 @@ static int handle_scalar_misaligned_load(struct
 pt_regs *regs)
        val.data_u64 = 0;
      if (user_mode(regs)) {
-        if (copy_from_user(&val, (u8 __user *)addr, len))
+        if (copy_from_user_nofault(&val, (u8 __user *)addr, len))
              return -1;
      } else {
          memcpy(&val, (u8 *)addr, len);

arch/riscv/kernel/traps_misaligned.c static int handle_scalar_misaligned_store(struct · line 562

Quoted 8 lines
arch/riscv/kernel/traps_misaligned.c +1 -1
@@ -556,7 +556,7 @@ static int handle_scalar_misaligned_store(struct
 pt_regs *regs)
          return -EOPNOTSUPP;
        if (user_mode(regs)) {
-        if (copy_to_user((u8 __user *)addr, &val, len))
+        if (copy_to_user_nofault((u8 __user *)addr, &val, len))
              return -1;
      } else {
          memcpy((u8 *)addr, &val, len);

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