Skip to content

Commit a434854

Browse files
Alexandre Ghitipalmer-dabbelt
authored andcommitted
riscv: make unsafe user copy routines use existing assembly routines
The current implementation is underperforming and in addition, it triggers misaligned access traps on platforms which do not handle misaligned accesses in hardware. Use the existing assembly routines to solve both problems at once. Signed-off-by: Alexandre Ghiti <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 259aaf0 commit a434854

File tree

5 files changed

+63
-48
lines changed

5 files changed

+63
-48
lines changed

arch/riscv/include/asm/asm-prototypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ long long __ashlti3(long long a, int b);
1212
#ifdef CONFIG_RISCV_ISA_V
1313

1414
#ifdef CONFIG_MMU
15-
asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n);
15+
asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n, bool enable_sum);
1616
#endif /* CONFIG_MMU */
1717

1818
void xor_regs_2_(unsigned long bytes, unsigned long *__restrict p1,

arch/riscv/include/asm/uaccess.h

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -450,35 +450,18 @@ static inline void user_access_restore(unsigned long enabled) { }
450450
(x) = (__force __typeof__(*(ptr)))__gu_val; \
451451
} while (0)
452452

453-
#define unsafe_copy_loop(dst, src, len, type, op, label) \
454-
while (len >= sizeof(type)) { \
455-
op(*(type *)(src), (type __user *)(dst), label); \
456-
dst += sizeof(type); \
457-
src += sizeof(type); \
458-
len -= sizeof(type); \
459-
}
453+
unsigned long __must_check __asm_copy_to_user_sum_enabled(void __user *to,
454+
const void *from, unsigned long n);
455+
unsigned long __must_check __asm_copy_from_user_sum_enabled(void *to,
456+
const void __user *from, unsigned long n);
460457

461458
#define unsafe_copy_to_user(_dst, _src, _len, label) \
462-
do { \
463-
char __user *__ucu_dst = (_dst); \
464-
const char *__ucu_src = (_src); \
465-
size_t __ucu_len = (_len); \
466-
unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u64, unsafe_put_user, label); \
467-
unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u32, unsafe_put_user, label); \
468-
unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u16, unsafe_put_user, label); \
469-
unsafe_copy_loop(__ucu_dst, __ucu_src, __ucu_len, u8, unsafe_put_user, label); \
470-
} while (0)
459+
if (__asm_copy_to_user_sum_enabled(_dst, _src, _len)) \
460+
goto label;
471461

472462
#define unsafe_copy_from_user(_dst, _src, _len, label) \
473-
do { \
474-
char *__ucu_dst = (_dst); \
475-
const char __user *__ucu_src = (_src); \
476-
size_t __ucu_len = (_len); \
477-
unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u64, unsafe_get_user, label); \
478-
unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u32, unsafe_get_user, label); \
479-
unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u16, unsafe_get_user, label); \
480-
unsafe_copy_loop(__ucu_src, __ucu_dst, __ucu_len, u8, unsafe_get_user, label); \
481-
} while (0)
463+
if (__asm_copy_from_user_sum_enabled(_dst, _src, _len)) \
464+
goto label;
482465

483466
#else /* CONFIG_MMU */
484467
#include <asm-generic/uaccess.h>

arch/riscv/lib/riscv_v_helpers.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
#ifdef CONFIG_MMU
1717
size_t riscv_v_usercopy_threshold = CONFIG_RISCV_ISA_V_UCOPY_THRESHOLD;
1818
int __asm_vector_usercopy(void *dst, void *src, size_t n);
19+
int __asm_vector_usercopy_sum_enabled(void *dst, void *src, size_t n);
1920
int fallback_scalar_usercopy(void *dst, void *src, size_t n);
20-
asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n)
21+
int fallback_scalar_usercopy_sum_enabled(void *dst, void *src, size_t n);
22+
asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n,
23+
bool enable_sum)
2124
{
2225
size_t remain, copied;
2326

@@ -26,7 +29,8 @@ asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n)
2629
goto fallback;
2730

2831
kernel_vector_begin();
29-
remain = __asm_vector_usercopy(dst, src, n);
32+
remain = enable_sum ? __asm_vector_usercopy(dst, src, n) :
33+
__asm_vector_usercopy_sum_enabled(dst, src, n);
3034
kernel_vector_end();
3135

3236
if (remain) {
@@ -40,6 +44,7 @@ asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n)
4044
return remain;
4145

4246
fallback:
43-
return fallback_scalar_usercopy(dst, src, n);
47+
return enable_sum ? fallback_scalar_usercopy(dst, src, n) :
48+
fallback_scalar_usercopy_sum_enabled(dst, src, n);
4449
}
4550
#endif

arch/riscv/lib/uaccess.S

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,43 @@ SYM_FUNC_START(__asm_copy_to_user)
1717
ALTERNATIVE("j fallback_scalar_usercopy", "nop", 0, RISCV_ISA_EXT_ZVE32X, CONFIG_RISCV_ISA_V)
1818
REG_L t0, riscv_v_usercopy_threshold
1919
bltu a2, t0, fallback_scalar_usercopy
20-
tail enter_vector_usercopy
20+
li a3, 1
21+
tail enter_vector_usercopy
2122
#endif
22-
SYM_FUNC_START(fallback_scalar_usercopy)
23+
SYM_FUNC_END(__asm_copy_to_user)
24+
EXPORT_SYMBOL(__asm_copy_to_user)
25+
SYM_FUNC_ALIAS(__asm_copy_from_user, __asm_copy_to_user)
26+
EXPORT_SYMBOL(__asm_copy_from_user)
2327

28+
SYM_FUNC_START(fallback_scalar_usercopy)
2429
/* Enable access to user memory */
25-
li t6, SR_SUM
26-
csrs CSR_STATUS, t6
30+
li t6, SR_SUM
31+
csrs CSR_STATUS, t6
32+
mv t6, ra
2733

34+
call fallback_scalar_usercopy_sum_enabled
35+
36+
/* Disable access to user memory */
37+
mv ra, t6
38+
li t6, SR_SUM
39+
csrc CSR_STATUS, t6
40+
ret
41+
SYM_FUNC_END(fallback_scalar_usercopy)
42+
43+
SYM_FUNC_START(__asm_copy_to_user_sum_enabled)
44+
#ifdef CONFIG_RISCV_ISA_V
45+
ALTERNATIVE("j fallback_scalar_usercopy_sum_enabled", "nop", 0, RISCV_ISA_EXT_ZVE32X, CONFIG_RISCV_ISA_V)
46+
REG_L t0, riscv_v_usercopy_threshold
47+
bltu a2, t0, fallback_scalar_usercopy_sum_enabled
48+
li a3, 0
49+
tail enter_vector_usercopy
50+
#endif
51+
SYM_FUNC_END(__asm_copy_to_user_sum_enabled)
52+
SYM_FUNC_ALIAS(__asm_copy_from_user_sum_enabled, __asm_copy_to_user_sum_enabled)
53+
EXPORT_SYMBOL(__asm_copy_from_user_sum_enabled)
54+
EXPORT_SYMBOL(__asm_copy_to_user_sum_enabled)
55+
56+
SYM_FUNC_START(fallback_scalar_usercopy_sum_enabled)
2857
/*
2958
* Save the terminal address which will be used to compute the number
3059
* of bytes copied in case of a fixup exception.
@@ -178,23 +207,12 @@ SYM_FUNC_START(fallback_scalar_usercopy)
178207
bltu a0, t0, 4b /* t0 - end of dst */
179208

180209
.Lout_copy_user:
181-
/* Disable access to user memory */
182-
csrc CSR_STATUS, t6
183210
li a0, 0
184211
ret
185-
186-
/* Exception fixup code */
187212
10:
188-
/* Disable access to user memory */
189-
csrc CSR_STATUS, t6
190213
sub a0, t5, a0
191214
ret
192-
SYM_FUNC_END(__asm_copy_to_user)
193-
SYM_FUNC_END(fallback_scalar_usercopy)
194-
EXPORT_SYMBOL(__asm_copy_to_user)
195-
SYM_FUNC_ALIAS(__asm_copy_from_user, __asm_copy_to_user)
196-
EXPORT_SYMBOL(__asm_copy_from_user)
197-
215+
SYM_FUNC_END(fallback_scalar_usercopy_sum_enabled)
198216

199217
SYM_FUNC_START(__clear_user)
200218

arch/riscv/lib/uaccess_vector.S

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ SYM_FUNC_START(__asm_vector_usercopy)
2424
/* Enable access to user memory */
2525
li t6, SR_SUM
2626
csrs CSR_STATUS, t6
27+
mv t6, ra
2728

29+
call __asm_vector_usercopy_sum_enabled
30+
31+
/* Disable access to user memory */
32+
mv ra, t6
33+
li t6, SR_SUM
34+
csrc CSR_STATUS, t6
35+
ret
36+
SYM_FUNC_END(__asm_vector_usercopy)
37+
38+
SYM_FUNC_START(__asm_vector_usercopy_sum_enabled)
2839
loop:
2940
vsetvli iVL, iNum, e8, ELEM_LMUL_SETTING, ta, ma
3041
fixup vle8.v vData, (pSrc), 10f
@@ -36,8 +47,6 @@ loop:
3647

3748
/* Exception fixup for vector load is shared with normal exit */
3849
10:
39-
/* Disable access to user memory */
40-
csrc CSR_STATUS, t6
4150
mv a0, iNum
4251
ret
4352

@@ -49,4 +58,4 @@ loop:
4958
csrr t2, CSR_VSTART
5059
sub iNum, iNum, t2
5160
j 10b
52-
SYM_FUNC_END(__asm_vector_usercopy)
61+
SYM_FUNC_END(__asm_vector_usercopy_sum_enabled)

0 commit comments

Comments
 (0)