[PATCH v1 1/3] LoongArch: Give more information if memory access failed

Tiezhu Yang posted 3 patches 1 month ago
[PATCH v1 1/3] LoongArch: Give more information if memory access failed
Posted by Tiezhu Yang 1 month ago
If memory access such as copy_{from, to}_kernel_nofault failed,
the users do not know what happened, it is very useful to print
the exception code for such cases. Furthermore, it is better to
print the caller function to know where is the entry.

Here are the low level call chains:

  copy_from_kernel_nofault()
    copy_from_kernel_nofault_loop()
      __get_kernel_nofault()

  copy_to_kernel_nofault()
    copy_to_kernel_nofault_loop()
      __put_kernel_nofault()

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 arch/loongarch/include/asm/uaccess.h | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/arch/loongarch/include/asm/uaccess.h b/arch/loongarch/include/asm/uaccess.h
index 4e259d490e45..4dacbb32854c 100644
--- a/arch/loongarch/include/asm/uaccess.h
+++ b/arch/loongarch/include/asm/uaccess.h
@@ -253,8 +253,13 @@ do {									\
 									\
 	__get_kernel_common(*((type *)(dst)), sizeof(type),		\
 			    (__force type *)(src));			\
-	if (unlikely(__gu_err))						\
+	if (unlikely(__gu_err))	{					\
+		pr_info("%s: memory access failed, ecode is 0x%x\n",	\
+			__func__, read_csr_excode());			\
+		pr_info("%s: caller is %pS\n",				\
+			__func__, __builtin_return_address(0));		\
 		goto err_label;						\
+	}								\
 } while (0)
 
 #define __put_kernel_nofault(dst, src, type, err_label)			\
@@ -264,8 +269,13 @@ do {									\
 									\
 	__pu_val = *(__force type *)(src);				\
 	__put_kernel_common(((type *)(dst)), sizeof(type));		\
-	if (unlikely(__pu_err))						\
+	if (unlikely(__pu_err))	{					\
+		pr_info("%s: memory access failed, ecode is 0x%x\n",	\
+			__func__, read_csr_excode());			\
+		pr_info("%s: caller is %pS\n",				\
+			__func__, __builtin_return_address(0));		\
 		goto err_label;						\
+	}								\
 } while (0)
 
 extern unsigned long __copy_user(void *to, const void *from, __kernel_size_t n);
-- 
2.42.0
Re: [PATCH v1 1/3] LoongArch: Give more information if memory access failed
Posted by Huacai Chen 3 weeks, 4 days ago
Applied with small modifications, thanks.

Huacai

On Tue, Mar 10, 2026 at 2:55 PM Tiezhu Yang <yangtiezhu@loongson.cn> wrote:
>
> If memory access such as copy_{from, to}_kernel_nofault failed,
> the users do not know what happened, it is very useful to print
> the exception code for such cases. Furthermore, it is better to
> print the caller function to know where is the entry.
>
> Here are the low level call chains:
>
>   copy_from_kernel_nofault()
>     copy_from_kernel_nofault_loop()
>       __get_kernel_nofault()
>
>   copy_to_kernel_nofault()
>     copy_to_kernel_nofault_loop()
>       __put_kernel_nofault()
>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  arch/loongarch/include/asm/uaccess.h | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/arch/loongarch/include/asm/uaccess.h b/arch/loongarch/include/asm/uaccess.h
> index 4e259d490e45..4dacbb32854c 100644
> --- a/arch/loongarch/include/asm/uaccess.h
> +++ b/arch/loongarch/include/asm/uaccess.h
> @@ -253,8 +253,13 @@ do {                                                                       \
>                                                                         \
>         __get_kernel_common(*((type *)(dst)), sizeof(type),             \
>                             (__force type *)(src));                     \
> -       if (unlikely(__gu_err))                                         \
> +       if (unlikely(__gu_err)) {                                       \
> +               pr_info("%s: memory access failed, ecode is 0x%x\n",    \
> +                       __func__, read_csr_excode());                   \
> +               pr_info("%s: caller is %pS\n",                          \
> +                       __func__, __builtin_return_address(0));         \
>                 goto err_label;                                         \
> +       }                                                               \
>  } while (0)
>
>  #define __put_kernel_nofault(dst, src, type, err_label)                        \
> @@ -264,8 +269,13 @@ do {                                                                       \
>                                                                         \
>         __pu_val = *(__force type *)(src);                              \
>         __put_kernel_common(((type *)(dst)), sizeof(type));             \
> -       if (unlikely(__pu_err))                                         \
> +       if (unlikely(__pu_err)) {                                       \
> +               pr_info("%s: memory access failed, ecode is 0x%x\n",    \
> +                       __func__, read_csr_excode());                   \
> +               pr_info("%s: caller is %pS\n",                          \
> +                       __func__, __builtin_return_address(0));         \
>                 goto err_label;                                         \
> +       }                                                               \
>  } while (0)
>
>  extern unsigned long __copy_user(void *to, const void *from, __kernel_size_t n);
> --
> 2.42.0
>