Skip to contentSkip to conversation

riscv: misaligned: fix misaligned accesses handling in put/get_user()

PATCH Superseded Clément Léger 4 participants 13 messages May 30, 2025 – Jun 2, 2025 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover riscv: misaligned: fix misaligned accesses handling in put/get_user()

While debugging a few problems with the misaligned access kselftest, Alexandre discovered some crash with the current code. Indeed, some misaligned access was done by the kernel using put_user(). This was resulting in trap and a kernel crash since. The path was the following:
user -> kernel -> access to user memory -> misaligned trap -> trap -> kernel -> misaligned handling -> memcpy -> crash due to failed page fault while in interrupt disabled section.

Last discussion about kernel misaligned handling and interrupt reenabling were actually not to reenable interrupt when handling misaligned access being done by kernel. The best solution being not to do any misaligned accesses to userspace memory, we considered a few options:

- Remove any call to put/get_user() potientally doing misaligned accesses
- Do not do any misaligned accesses in put/get_user() itself

The second solution was the one chosen as there are too many callsite to put/get_user() that could potentially do misaligned accesses. We tried two approaches for that, either split access in two aligned accesses (and do RMW for put_user()) or call copy_from/to_user() which does not do any misaligned accesses. The later one was the simpler to implement (although the performances are probably lower than split aligned accesses but still way better than doing misaligned access emulation) and allows to support what we wanted.

These commits are based on top of Alex dev/alex/get_user_misaligned_v1 branch.

Clément Léger (2):
riscv: process: use unsigned int instead of unsigned long for

    put_user()

riscv: uaccess: do not do misaligned accesses in get/put_user()

 arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
 arch/riscv/kernel/process.c      |  2 +-
 2 files changed, 23 insertions(+), 7 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 riscv: process: use unsigned int instead of unsigned long for put_user()

The specification of prctl() for GET_UNALIGN_CTL states that the value is returned in an unsigned int * address passed as an unsigned long. Change the type to match that and avoid an unaligned access as well.

---
 arch/riscv/kernel/process.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/process.c +1 -1
index 15d8f75902f8..9ee6d816b98b 100644
@@ -57,7 +57,7 @@ int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
 	if (!unaligned_ctl_available())
 		return -EINVAL;
 
-	return put_user(tsk->thread.align_ctl, (unsigned long __user *)adr);
+	return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
 }
 
 void __show_regs(struct pt_regs *regs)
Signature · 1 line
2.49.0
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] riscv: process: use unsigned int instead of unsigned long for put_user()

Hi Clément,

Quoted 14 lines · Clément Léger

The specification of prctl() for GET_UNALIGN_CTL states that the value is returned in an unsigned int * address passed as an unsigned long. Change the type to match that and avoid an unaligned access as well.

---
  arch/riscv/kernel/process.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/kernel/process.c +0 -0
index 15d8f75902f8..9ee6d816b98b 100644

arch/riscv/kernel/process.c int get_unalign_ctl(struct task_struct *tsk, unsigned long adr) · line 63

Quoted 8 lines
arch/riscv/kernel/process.c +1 -1
@@ -57,7 +57,7 @@ int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  	if (!unaligned_ctl_available())
  		return -EINVAL;
 
-	return put_user(tsk->thread.align_ctl, (unsigned long __user *)adr);
+	return put_user(tsk->thread.align_ctl, (unsigned int __user *)adr);
  }
 
  void __show_regs(struct pt_regs *regs)

Thanks,

Alex

#3 lore raw

PATCH 2/2 riscv: uaccess: do not do misaligned accesses in get/put_user()

In reply to Clément Léger

Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

---
 arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
arch/riscv/include/asm/uaccess.h +22 -6
index 046de7ced09c..b542c05f394f 100644
@@ -169,8 +169,21 @@ do {								\
 
 #endif /* CONFIG_64BIT */
 
+unsigned long __must_check __asm_copy_to_user(void __user *to,
+	const void *from, unsigned long n);
+unsigned long __must_check __asm_copy_from_user(void *to,
+	const void __user *from, unsigned long n);
+
 #define __get_user_nocheck(x, __gu_ptr, label)			\
 do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			if (__asm_copy_from_user(&(x), __gu_ptr, sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
 	switch (sizeof(*__gu_ptr)) {				\
 	case 1:							\
 		__get_user_asm("lb", (x), __gu_ptr, label);	\
@@ -297,6 +310,15 @@ do {								\
 
 #define __put_user_nocheck(x, __gu_ptr, label)			\
 do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			unsigned long val = (unsigned long)(x);				\
+			if (__asm_copy_to_user(__gu_ptr, &(val), sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
 	switch (sizeof(*__gu_ptr)) {				\
 	case 1:							\
 		__put_user_asm("sb", (x), __gu_ptr, label);	\
@@ -385,12 +407,6 @@ err_label:							\
 		-EFAULT;					\
 })
 
-
-unsigned long __must_check __asm_copy_to_user(void __user *to,
-	const void *from, unsigned long n);
-unsigned long __must_check __asm_copy_from_user(void *to,
-	const void __user *from, unsigned long n);
-
 static inline unsigned long
 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
 {
Signature · 1 line
2.49.0
#4 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

Quoted 27 lines · Clément Léger

Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

---
  arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
  1 file changed, 22 insertions(+), 6 deletions(-)
arch/riscv/include/asm/uaccess.h +0 -0
index 046de7ced09c..b542c05f394f 100644
@@ -169,8 +169,21 @@ do {								\
 
  #endif /* CONFIG_64BIT */

arch/riscv/include/asm/uaccess.h do { \ · line 180

Quoted 10 lines
arch/riscv/include/asm/uaccess.h +7 -0
 
+unsigned long __must_check __asm_copy_to_user(void __user *to,
+	const void *from, unsigned long n);
+unsigned long __must_check __asm_copy_from_user(void *to,
+	const void __user *from, unsigned long n);
+
  #define __get_user_nocheck(x, __gu_ptr, label)			\
  do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\

Nit: I would use && instead of 2 ifs.

arch/riscv/include/asm/uaccess.h do { \ · line 184

Quoted 4 lines
arch/riscv/include/asm/uaccess.h +4 -0
+			if (__asm_copy_from_user(&(x), __gu_ptr, sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\

Here I would remove the else

arch/riscv/include/asm/uaccess.h do { \ · line 315

Quoted 12 lines
arch/riscv/include/asm/uaccess.h +5 -0
+		}						\
+	}							\
  	switch (sizeof(*__gu_ptr)) {				\
  	case 1:							\
  		__get_user_asm("lb", (x), __gu_ptr, label);	\
@@ -297,6 +310,15 @@ do {								\
 
  #define __put_user_nocheck(x, __gu_ptr, label)			\
  do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			unsigned long val = (unsigned long)(x);				\

Here it sems like __inttype(*(__gu_ptr)) is more accurate than unsigned long, even though I think unsigned long works fine too.

Quoted 22 lines
arch/riscv/include/asm/uaccess.h +6 -6
+			if (__asm_copy_to_user(__gu_ptr, &(val), sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
  	switch (sizeof(*__gu_ptr)) {				\
  	case 1:							\
  		__put_user_asm("sb", (x), __gu_ptr, label);	\
@@ -385,12 +407,6 @@ err_label:							\
  		-EFAULT;					\
  })
 
-
-unsigned long __must_check __asm_copy_to_user(void __user *to,
-	const void *from, unsigned long n);
-unsigned long __must_check __asm_copy_from_user(void *to,
-	const void __user *from, unsigned long n);
-
  static inline unsigned long
  raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  {
#5 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

Quoted 44 lines · Alexandre Ghiti
Quoted 28 lines · Clément Léger

Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

---
  arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------

  1 file changed, 22 insertions(+), 6 deletions(-)
arch/riscv/include/ renamed from arch/riscv/include/asm/uaccess.h +0 -0

asm/uaccess.h
index 046de7ced09c..b542c05f394f 100644

arch/riscv/include/asm/uaccess.h +0 -0
@@ -169,8 +169,21 @@ do {                                \
     #endif /* CONFIG_64BIT */
   +unsigned long __must_check __asm_copy_to_user(void __user *to,

arch/riscv/include/asm/uaccess.h do {                                \ · line 180

Quoted 10 lines
arch/riscv/include/asm/uaccess.h +6 -0
+    const void *from, unsigned long n);
+unsigned long __must_check __asm_copy_from_user(void *to,
+    const void __user *from, unsigned long n);
+
   #define __get_user_nocheck(x, __gu_ptr, label)            \
   do {                                \
+    if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
 {            \
+        if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr)))
 {        \

Nit: I would use && instead of 2 ifs.

Quoted 8 lines

arch/riscv/include/asm/uaccess.h do { \ · line 184

Quoted 5 lines
arch/riscv/include/asm/uaccess.h +4 -0
+            if (__asm_copy_from_user(&(x), __gu_ptr,
 sizeof(*__gu_ptr)))    \
+                goto label;            \
+            else                    \
+                break;                \

Here I would remove the else

Hi Alex,

The "else" is needed to break from the outer do/while loop or it will go though the next switch case (and it will crash due to misaligned accesses).

Quoted 11 lines
Quoted 11 lines
arch/riscv/include/asm/uaccess.h +2 -0
+        }                        \
+    }                            \
       switch (sizeof(*__gu_ptr)) {                \

      case 1:                            \
          __get_user_asm("lb", (x), __gu_ptr, label);    \

arch/riscv/include/asm/uaccess.h +1 -0
@@ -297,6 +310,15 @@ do {                                \
     #define __put_user_nocheck(x, __gu_ptr, label)            \
   do {                                \
+    if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
Quoted 8 lines

arch/riscv/include/asm/uaccess.h do {                                \ · line 316

Quoted 4 lines
arch/riscv/include/asm/uaccess.h +2 -0
 {            \
+        if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr)))
 {        \
+            unsigned long val = (unsigned long)(x);                \

Here it sems like __inttype(*(__gu_ptr)) is more accurate than unsigned long, even though I think unsigned long works fine too.

Wasn't aware of __inttype, but it sounds good.

Thanks,

Clément

Quoted 24 lines
Quoted 24 lines
arch/riscv/include/asm/uaccess.h +6 -0
+            if (__asm_copy_to_user(__gu_ptr, &(val),
 sizeof(*__gu_ptr)))    \
+                goto label;            \
+            else                    \
+                break;                \
+        }                        \
+    }                            \
       switch (sizeof(*__gu_ptr)) {                \

      case 1:                            \
          __put_user_asm("sb", (x), __gu_ptr, label);    \

arch/riscv/include/asm/uaccess.h +0 -0
@@ -385,12 +407,6 @@ err_label:                            \
           -EFAULT;                    \
   })

  -

arch/riscv/include/asm/uaccess.h +0 -5
-unsigned long __must_check __asm_copy_to_user(void __user *to,
-    const void *from, unsigned long n);
-unsigned long __must_check __asm_copy_from_user(void *to,
-    const void __user *from, unsigned long n);
-

  static inline unsigned long
  raw_copy_from_user(void *to, const void __user *from, unsigned long n) {

#6 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

Hi Clément,

Quoted 51 lines · Clément Léger
Quoted 49 lines · Alexandre Ghiti
Quoted 28 lines · Clément Léger

Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

---
   arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
   1 file changed, 22 insertions(+), 6 deletions(-)
arch/riscv/include/ renamed from arch/riscv/include/asm/uaccess.h +0 -0

asm/uaccess.h
index 046de7ced09c..b542c05f394f 100644

arch/riscv/include/asm/uaccess.h +0 -0
@@ -169,8 +169,21 @@ do {                                \
     #endif /* CONFIG_64BIT */
   +unsigned long __must_check __asm_copy_to_user(void __user *to,

arch/riscv/include/asm/uaccess.h do {                                \ · line 180

Quoted 10 lines
arch/riscv/include/asm/uaccess.h +6 -0
+    const void *from, unsigned long n);
+unsigned long __must_check __asm_copy_from_user(void *to,
+    const void __user *from, unsigned long n);
+
   #define __get_user_nocheck(x, __gu_ptr, label)            \
   do {                                \
+    if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
 {            \
+        if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr)))
 {        \

Nit: I would use && instead of 2 ifs.

Quoted 5 lines
arch/riscv/include/asm/uaccess.h +4 -0
+            if (__asm_copy_from_user(&(x), __gu_ptr,
 sizeof(*__gu_ptr)))    \
+                goto label;            \
+            else                    \
+                break;                \
Quoted 5 lines
Quoted 1 line

Here I would remove the else

Hi Alex,

The "else" is needed to break from the outer do/while loop or it will go though the next switch case (and it will crash due to misaligned accesses).

I meant only the "else", not the "break"!

Thanks,

Alex

Quoted 52 lines
Quoted 18 lines
Quoted 8 lines
arch/riscv/include/asm/uaccess.h +2 -0
+        }                        \
+    }                            \
       switch (sizeof(*__gu_ptr)) {                \
       case 1:                            \
           __get_user_asm("lb", (x), __gu_ptr, label);    \

arch/riscv/include/asm/uaccess.h do {                                \ · line 316

Quoted 7 lines
arch/riscv/include/asm/uaccess.h +3 -0
@@ -297,6 +310,15 @@ do {                                \
     #define __put_user_nocheck(x, __gu_ptr, label)            \
   do {                                \
+    if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS))
 {            \
+        if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr)))
 {        \
+            unsigned long val = (unsigned long)(x);                \

Here it sems like __inttype(*(__gu_ptr)) is more accurate than unsigned long, even though I think unsigned long works fine too.

Wasn't aware of __inttype, but it sounds good.

Thanks,

Clément

Quoted 15 lines
Quoted 15 lines
arch/riscv/include/asm/uaccess.h +6 -0
+            if (__asm_copy_to_user(__gu_ptr, &(val),
 sizeof(*__gu_ptr)))    \
+                goto label;            \
+            else                    \
+                break;                \
+        }                        \
+    }                            \
       switch (sizeof(*__gu_ptr)) {                \

      case 1:                            \
          __put_user_asm("sb", (x), __gu_ptr, label);    \

arch/riscv/include/asm/uaccess.h +0 -0
@@ -385,12 +407,6 @@ err_label:                            \
           -EFAULT;                    \
   })
   -
Quoted 8 lines

arch/riscv/include/asm/uaccess.h err_label: \ · line 412

Quoted 8 lines
arch/riscv/include/asm/uaccess.h +0 -5
-unsigned long __must_check __asm_copy_to_user(void __user *to,
-    const void *from, unsigned long n);
-unsigned long __must_check __asm_copy_from_user(void *to,
-    const void __user *from, unsigned long n);
-
   static inline unsigned long
   raw_copy_from_user(void *to, const void __user *from, unsigned long n)
   {

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

#7 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

In reply to Clément Léger
Quoted 6 lines · On Fri, 30 May 2025 22:56:58 +0200 Clément Léger <[email protected]> wrote:

Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.

Quoted 8 lines

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

For get_user() you are much better off reading the two words that contain the value and then doing 'shift' and 'or' to get the correct value.

Even for put_user() doing the explicit byte accesses will be faster than going though the generic copy_to/from_user() function.

David

Quoted 61 lines
---
 arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
arch/riscv/include/asm/uaccess.h +22 -6
index 046de7ced09c..b542c05f394f 100644
@@ -169,8 +169,21 @@ do {								\
 
 #endif /* CONFIG_64BIT */
 
+unsigned long __must_check __asm_copy_to_user(void __user *to,
+	const void *from, unsigned long n);
+unsigned long __must_check __asm_copy_from_user(void *to,
+	const void __user *from, unsigned long n);
+
 #define __get_user_nocheck(x, __gu_ptr, label)			\
 do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			if (__asm_copy_from_user(&(x), __gu_ptr, sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
 	switch (sizeof(*__gu_ptr)) {				\
 	case 1:							\
 		__get_user_asm("lb", (x), __gu_ptr, label);	\
@@ -297,6 +310,15 @@ do {								\
 
 #define __put_user_nocheck(x, __gu_ptr, label)			\
 do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			unsigned long val = (unsigned long)(x);				\
+			if (__asm_copy_to_user(__gu_ptr, &(val), sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
 	switch (sizeof(*__gu_ptr)) {				\
 	case 1:							\
 		__put_user_asm("sb", (x), __gu_ptr, label);	\
@@ -385,12 +407,6 @@ err_label:							\
 		-EFAULT;					\
 })
 
-
-unsigned long __must_check __asm_copy_to_user(void __user *to,
-	const void *from, unsigned long n);
-unsigned long __must_check __asm_copy_from_user(void *to,
-	const void __user *from, unsigned long n);
-
 static inline unsigned long
 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
 {
#8 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

Quoted 9 lines · David Laight
Quoted 8 lines

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

Quoted 5 lines

For get_user() you are much better off reading the two words that contain the value and then doing 'shift' and 'or' to get the correct value.

Even for put_user() doing the explicit byte accesses will be faster than going though the generic copy_to/from_user() function.

FWIW I think optimising copy_to/from_user for such cases would be a more
robust approach moving forward than sprinkling open-coded implementations across code.

Maciej

#9 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

Quoted 14 lines · Maciej W. Rozycki
Quoted 12 lines · David Laight
Quoted 8 lines

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

For get_user() you are much better off reading the two words that contain the value and then doing 'shift' and 'or' to get the correct value.

Quoted 6 lines
Quoted 2 lines

Even for put_user() doing the explicit byte accesses will be faster than going though the generic copy_to/from_user() function.

FWIW I think optimising copy_to/from_user for such cases would be a more
robust approach moving forward than sprinkling open-coded implementations across code.

Hi Maciej,

Indeed, that's a good idea, we could optimize small copy in copy_from/to_user so that will benefit all the users as well.

Thanks,

Clément

Quoted 2 lines

Maciej

#10 lore raw

Re: [PATCH 2/2] riscv: uaccess: do not do misaligned accesses in get/put_user()

In reply to David Laight
Quoted 18 lines · David Laight
Quoted 6 lines · On Fri, 30 May 2025 22:56:58 +0200 Clément Léger <[email protected]> wrote:

Doing misaligned access to userspace memory would make a trap on platform where it is emulated. Latest fixes removed the kernel capability to do unaligned accesses to userspace memory safely since interrupts are kept disabled at all time during that. Thus doing so would crash the kernel.

Quoted 8 lines

Such behavior was detected with GET_UNALIGN_CTL() that was doing a put_user() with an unsigned long* address that should have been an unsigned int*. Reenabling kernel misaligned access emulation is a bit risky and it would also degrade performances. Rather than doing that, we will try to avoid any misaligned accessed by using copy_from/to_user() which does not do any misaligned accesses. This can be done only for !CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS and thus allows to only generate a bit more code for this config.

Quoted 5 lines

For get_user() you are much better off reading the two words that contain the value and then doing 'shift' and 'or' to get the correct value.

Even for put_user() doing the explicit byte accesses will be faster than going though the generic copy_to/from_user() function.

Hi David,

Alexandre tried that approach as well but that added a bit more code and it was more complex than just calling copy_from/to_user(). That can still be done in another commit if we need more performance later. As a side note, prior to that patch, these misaligned accesses were using trap-and-emulate so that is still a performance improvement.

Thanks,

Clément

Quoted 65 lines

David

Quoted 61 lines
---
 arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
arch/riscv/include/asm/uaccess.h +22 -6
index 046de7ced09c..b542c05f394f 100644
@@ -169,8 +169,21 @@ do {								\
 
 #endif /* CONFIG_64BIT */
 
+unsigned long __must_check __asm_copy_to_user(void __user *to,
+	const void *from, unsigned long n);
+unsigned long __must_check __asm_copy_from_user(void *to,
+	const void __user *from, unsigned long n);
+
 #define __get_user_nocheck(x, __gu_ptr, label)			\
 do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			if (__asm_copy_from_user(&(x), __gu_ptr, sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
 	switch (sizeof(*__gu_ptr)) {				\
 	case 1:							\
 		__get_user_asm("lb", (x), __gu_ptr, label);	\
@@ -297,6 +310,15 @@ do {								\
 
 #define __put_user_nocheck(x, __gu_ptr, label)			\
 do {								\
+	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {			\
+		if (!IS_ALIGNED((uintptr_t)__gu_ptr, sizeof(*__gu_ptr))) {		\
+			unsigned long val = (unsigned long)(x);				\
+			if (__asm_copy_to_user(__gu_ptr, &(val), sizeof(*__gu_ptr)))	\
+				goto label;			\
+			else					\
+				break;				\
+		}						\
+	}							\
 	switch (sizeof(*__gu_ptr)) {				\
 	case 1:							\
 		__put_user_asm("sb", (x), __gu_ptr, label);	\
@@ -385,12 +407,6 @@ err_label:							\
 		-EFAULT;					\
 })
 
-
-unsigned long __must_check __asm_copy_to_user(void __user *to,
-	const void *from, unsigned long n);
-unsigned long __must_check __asm_copy_from_user(void *to,
-	const void __user *from, unsigned long n);
-
 static inline unsigned long
 raw_copy_from_user(void *to, const void __user *from, unsigned long n)
 {
#11 lore raw
In reply to Clément Léger
Quoted 30 lines · Clément Léger

While debugging a few problems with the misaligned access kselftest, Alexandre discovered some crash with the current code. Indeed, some misaligned access was done by the kernel using put_user(). This was resulting in trap and a kernel crash since. The path was the following:
user -> kernel -> access to user memory -> misaligned trap -> trap -> kernel -> misaligned handling -> memcpy -> crash due to failed page fault while in interrupt disabled section.

Last discussion about kernel misaligned handling and interrupt reenabling were actually not to reenable interrupt when handling misaligned access being done by kernel. The best solution being not to do any misaligned accesses to userspace memory, we considered a few options:

- Remove any call to put/get_user() potientally doing misaligned accesses
- Do not do any misaligned accesses in put/get_user() itself

The second solution was the one chosen as there are too many callsite to put/get_user() that could potentially do misaligned accesses. We tried two approaches for that, either split access in two aligned accesses (and do RMW for put_user()) or call copy_from/to_user() which does not do any misaligned accesses. The later one was the simpler to implement (although the performances are probably lower than split aligned accesses but still way better than doing misaligned access emulation) and allows to support what we wanted.

These commits are based on top of Alex dev/alex/get_user_misaligned_v1 branch.

Quoted 8 lines

Clément Léger (2):
riscv: process: use unsigned int instead of unsigned long for

     put_user()

riscv: uaccess: do not do misaligned accesses in get/put_user()

  arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------
  arch/riscv/kernel/process.c      |  2 +-
  2 files changed, 23 insertions(+), 7 deletions(-)

We also need to prevent unsafe routines to trigger misaligned accesses, I have a patch for this here https://github.com/linux-riscv/linux/commit/7c172121aeb235dedeb6f5e06740527530edd6af

Clément, can you add this one to the series please?

I have just  triggered a CI with those fixes on top of my sbi 3.0 branch.

Thanks,

Alex

#12 lore raw
Quoted 41 lines · Alexandre Ghiti
Quoted 30 lines · Clément Léger

While debugging a few problems with the misaligned access kselftest, Alexandre discovered some crash with the current code. Indeed, some misaligned access was done by the kernel using put_user(). This was resulting in trap and a kernel crash since. The path was the following:
user -> kernel -> access to user memory -> misaligned trap -> trap -> kernel -> misaligned handling -> memcpy -> crash due to failed page fault while in interrupt disabled section.

Last discussion about kernel misaligned handling and interrupt reenabling were actually not to reenable interrupt when handling misaligned access being done by kernel. The best solution being not to do any misaligned accesses to userspace memory, we considered a few options:

- Remove any call to put/get_user() potientally doing misaligned accesses
- Do not do any misaligned accesses in put/get_user() itself

The second solution was the one chosen as there are too many callsite to put/get_user() that could potentially do misaligned accesses. We tried two approaches for that, either split access in two aligned accesses (and do RMW for put_user()) or call copy_from/to_user() which does not do any misaligned accesses. The later one was the simpler to implement (although the performances are probably lower than split aligned accesses but still way better than doing misaligned access emulation) and allows to support what we wanted.

These commits are based on top of Alex dev/alex/get_user_misaligned_v1 branch.

Quoted 8 lines

Clément Léger (2):
   riscv: process: use unsigned int instead of unsigned long for put_user()
   riscv: uaccess: do not do misaligned accesses in get/put_user()

  arch/riscv/include/asm/uaccess.h | 28 ++++++++++++++++++++++------ arch/riscv/kernel/process.c      |  2 +-

  2 files changed, 23 insertions(+), 7 deletions(-)
Quoted 5 lines

We also need to prevent unsafe routines to trigger misaligned accesses, I have a patch for this here https://github.com/linux-riscv/linux/
commit/7c172121aeb235dedeb6f5e06740527530edd6af

Clément, can you add this one to the series please?

Yep sure.

Quoted 8 lines

I have just  triggered a CI with those fixes on top of my sbi 3.0 branch.

Thanks,

Alex