[RFC PATCH 3/6] riscv: insn: __read_insn use copy_from_X_nofault

Jesse Taube posted 6 patches 2 months, 2 weeks ago
There is a newer version of this series
[RFC PATCH 3/6] riscv: insn: __read_insn use copy_from_X_nofault
Posted by Jesse Taube 2 months, 2 weeks ago
__read_insn was using get_user for user space reads and direct
dereferencing for kernel space reads.
Update to use copy_from_user_nofault, copy_from_kernel_nofault for this
as get_user is user context only and may sleep.

Signed-off-by: Jesse Taube <jesse@rivosinc.com>
---
Squash with previous commit as it's breaks bisectability.
Separated as i'm unsure if copy_from_user_nofault is an acceptable
replacement.
---
 arch/riscv/kernel/insn.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/insn.c b/arch/riscv/kernel/insn.c
index dd2a6ef9fd25..b8e5202ddced 100644
--- a/arch/riscv/kernel/insn.c
+++ b/arch/riscv/kernel/insn.c
@@ -2,6 +2,9 @@
 /*
  * Copyright 2025 Rivos, Inc
  */
+
+#include <linux/uaccess.h>
+
 #include <asm/insn.h>
 #include <asm/ptrace.h>
 #include <asm/uaccess.h>
@@ -11,10 +14,9 @@
 	int __ret;					\
 							\
 	if (user_mode(regs)) {				\
-		__ret = get_user(insn, (type __user *) insn_addr); \
+		__ret = copy_from_user_nofault(&insn, (const type __user *) insn_addr, sizeof(type)); \
 	} else {					\
-		insn = *(type *)insn_addr;		\
-		__ret = 0;				\
+		__ret = copy_from_kernel_nofault(&insn, (const type *) insn_addr, sizeof(type)); \
 	}						\
 							\
 	__ret;						\
-- 
2.43.0
Re: [RFC PATCH 3/6] riscv: insn: __read_insn use copy_from_X_nofault
Posted by Clément Léger 2 months ago

On 22/07/2025 19:38, Jesse Taube wrote:
> __read_insn was using get_user for user space reads and direct
> dereferencing for kernel space reads.
> Update to use copy_from_user_nofault, copy_from_kernel_nofault for this
> as get_user is user context only and may sleep.
> 
> Signed-off-by: Jesse Taube <jesse@rivosinc.com>
> ---
> Squash with previous commit as it's breaks bisectability.
> Separated as i'm unsure if copy_from_user_nofault is an acceptable
> replacement.
> ---
>  arch/riscv/kernel/insn.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/riscv/kernel/insn.c b/arch/riscv/kernel/insn.c
> index dd2a6ef9fd25..b8e5202ddced 100644
> --- a/arch/riscv/kernel/insn.c
> +++ b/arch/riscv/kernel/insn.c
> @@ -2,6 +2,9 @@
>  /*
>   * Copyright 2025 Rivos, Inc
>   */
> +
> +#include <linux/uaccess.h>
> +
>  #include <asm/insn.h>
>  #include <asm/ptrace.h>
>  #include <asm/uaccess.h>
> @@ -11,10 +14,9 @@
>  	int __ret;					\
>  							\
>  	if (user_mode(regs)) {				\
> -		__ret = get_user(insn, (type __user *) insn_addr); \
> +		__ret = copy_from_user_nofault(&insn, (const type __user *) insn_addr, sizeof(type)); \
>  	} else {					\
> -		insn = *(type *)insn_addr;		\
> -		__ret = 0;				\
> +		__ret = copy_from_kernel_nofault(&insn, (const type *) insn_addr, sizeof(type)); \
>  	}						\

Hi Jesse,

This can not be used for misaligned accesses handling. As pointed out by
Samuel in a previous misaligned access handling patch, using the
no_fault() version would lead to access failure if the user memory is
swapped out. For that reason it was swapped from the no_fault() version
to the standard version.

Thanks,

Clément

>  							\
>  	__ret;						\