[PATCH] x86/uaccess: small optimization in unsafe_copy_to_user()

From: Eric Dumazet
Date: Fri Apr 16 2021 - 15:24:21 EST


From: Eric Dumazet <edumazet@xxxxxxxxxx>

We have to loop only to copy u64 values.
After this first loop, we copy at most one u32, one u16 and one byte.

Signed-off-by: Eric Dumazet <edumazet@xxxxxxxxxx>
---
arch/x86/include/asm/uaccess.h | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index c9fa7be3df82ddb9495961b3e2f22b1ac07edafa..ddb19bb8c86786d78407dcfb59623943ccbce8a8 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -517,15 +517,23 @@ do { \
len -= sizeof(type); \
}

+#define unsafe_copy_elem(dst, src, len, type, label) \
+ if (len >= sizeof(type)) { \
+ unsafe_put_user(*(type *)(src),(type __user *)(dst),label); \
+ dst += sizeof(type); \
+ src += sizeof(type); \
+ len -= sizeof(type); \
+ }
+
#define unsafe_copy_to_user(_dst,_src,_len,label) \
do { \
char __user *__ucu_dst = (_dst); \
const char *__ucu_src = (_src); \
size_t __ucu_len = (_len); \
unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, label); \
- unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, label); \
- unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, label); \
- unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, label); \
+ unsafe_copy_elem(__ucu_dst, __ucu_src, __ucu_len, u32, label); \
+ unsafe_copy_elem(__ucu_dst, __ucu_src, __ucu_len, u16, label); \
+ unsafe_copy_elem(__ucu_dst, __ucu_src, __ucu_len, u8, label); \
} while (0)

#define HAVE_GET_KERNEL_NOFAULT
--
2.31.1.368.gbe11c130af-goog