Skip to content

Commit 0db7058

Browse files
committed
x86/clear_user: Make it faster
Based on a patch by Mark Hemment <[email protected]> and incorporating very sane suggestions from Linus. The point here is to have the default case with FSRM - which is supposed to be the majority of x86 hw out there - if not now then soon - be directly inlined into the instruction stream so that no function call overhead is taking place. Drop the early clobbers from the @SiZe and @addr operands as those are not needed anymore since we have single instruction alternatives. The benchmarks I ran would show very small improvements and a PF benchmark would even show weird things like slowdowns with higher core counts. So for a ~6m running the git test suite, the function gets called under 700K times, all from padzero(): <...>-2536 [006] ..... 261.208801: padzero: to: 0x55b0663ed214, size: 3564, cycles: 21900 <...>-2536 [006] ..... 261.208819: padzero: to: 0x7f061adca078, size: 3976, cycles: 17160 <...>-2537 [008] ..... 261.211027: padzero: to: 0x5572d019e240, size: 3520, cycles: 23850 <...>-2537 [008] ..... 261.211049: padzero: to: 0x7f1288dc9078, size: 3976, cycles: 15900 ... which is around 1%-ish of the total time and which is consistent with the benchmark numbers. So Mel gave me the idea to simply measure how fast the function becomes. I.e.: start = rdtsc_ordered(); ret = __clear_user(to, n); end = rdtsc_ordered(); Computing the mean average of all the samples collected during the test suite run then shows some improvement: clear_user_original: Amean: 9219.71 (Sum: 6340154910, samples: 687674) fsrm: Amean: 8030.63 (Sum: 5522277720, samples: 687652) That's on Zen3. The situation looks a lot more confusing on Intel: Icelake: clear_user_original: Amean: 19679.4 (Sum: 13652560764, samples: 693750) Amean: 19743.7 (Sum: 13693470604, samples: 693562) (I ran it twice just to be sure.) ERMS: Amean: 20374.3 (Sum: 13910601024, samples: 682752) Amean: 20453.7 (Sum: 14186223606, samples: 693576) FSRM: Amean: 20458.2 (Sum: 13918381386, sample s: 680331) The original microbenchmark which people were complaining about: for i in $(seq 1 10); do dd if=/dev/zero of=/dev/null bs=1M status=progress count=65536; done 2>&1 | grep copied 32207011840 bytes (32 GB, 30 GiB) copied, 1 s, 32.2 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.93069 s, 35.6 GB/s 37597741056 bytes (38 GB, 35 GiB) copied, 1 s, 37.6 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.78017 s, 38.6 GB/s 62020124672 bytes (62 GB, 58 GiB) copied, 2 s, 31.0 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 2.13716 s, 32.2 GB/s 60010004480 bytes (60 GB, 56 GiB) copied, 1 s, 60.0 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.14129 s, 60.2 GB/s 53212086272 bytes (53 GB, 50 GiB) copied, 1 s, 53.2 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.28398 s, 53.5 GB/s 55698259968 bytes (56 GB, 52 GiB) copied, 1 s, 55.7 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.22507 s, 56.1 GB/s 55306092544 bytes (55 GB, 52 GiB) copied, 1 s, 55.3 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.23647 s, 55.6 GB/s 54387539968 bytes (54 GB, 51 GiB) copied, 1 s, 54.4 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.25693 s, 54.7 GB/s 50566529024 bytes (51 GB, 47 GiB) copied, 1 s, 50.6 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.35096 s, 50.9 GB/s 58308165632 bytes (58 GB, 54 GiB) copied, 1 s, 58.3 GB/s 68719476736 bytes (69 GB, 64 GiB) copied, 1.17394 s, 58.5 GB/s Now the same thing with smaller buffers: for i in $(seq 1 10); do dd if=/dev/zero of=/dev/null bs=1M status=progress count=8192; done 2>&1 | grep copied 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.28485 s, 30.2 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.276112 s, 31.1 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.29136 s, 29.5 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.283803 s, 30.3 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.306503 s, 28.0 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.349169 s, 24.6 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.276912 s, 31.0 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.265356 s, 32.4 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.28464 s, 30.2 GB/s 8589934592 bytes (8.6 GB, 8.0 GiB) copied, 0.242998 s, 35.3 GB/s is also not conclusive because it all depends on the buffer sizes, their alignments and when the microcode detects that cachelines can be aggregated properly and copied in bigger sizes. Signed-off-by: Borislav Petkov <[email protected]> Link: https://lore.kernel.org/r/CAHk-=wh=Mu_EYhtOmPn6AxoQZyEh-4fo2Zx3G7rBv1g7vwoKiw@mail.gmail.com
1 parent 568035b commit 0db7058

File tree

5 files changed

+188
-43
lines changed

5 files changed

+188
-43
lines changed

arch/x86/include/asm/uaccess.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,6 @@ strncpy_from_user(char *dst, const char __user *src, long count);
502502

503503
extern __must_check long strnlen_user(const char __user *str, long n);
504504

505-
unsigned long __must_check clear_user(void __user *mem, unsigned long len);
506-
unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
507-
508505
#ifdef CONFIG_ARCH_HAS_COPY_MC
509506
unsigned long __must_check
510507
copy_mc_to_kernel(void *to, const void *from, unsigned len);
@@ -526,6 +523,8 @@ extern struct movsl_mask {
526523
#define ARCH_HAS_NOCACHE_UACCESS 1
527524

528525
#ifdef CONFIG_X86_32
526+
unsigned long __must_check clear_user(void __user *mem, unsigned long len);
527+
unsigned long __must_check __clear_user(void __user *mem, unsigned long len);
529528
# include <asm/uaccess_32.h>
530529
#else
531530
# include <asm/uaccess_64.h>

arch/x86/include/asm/uaccess_64.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,49 @@ __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
7979
kasan_check_write(dst, size);
8080
return __copy_user_flushcache(dst, src, size);
8181
}
82+
83+
/*
84+
* Zero Userspace.
85+
*/
86+
87+
__must_check unsigned long
88+
clear_user_original(void __user *addr, unsigned long len);
89+
__must_check unsigned long
90+
clear_user_rep_good(void __user *addr, unsigned long len);
91+
__must_check unsigned long
92+
clear_user_erms(void __user *addr, unsigned long len);
93+
94+
static __always_inline __must_check unsigned long __clear_user(void __user *addr, unsigned long size)
95+
{
96+
might_fault();
97+
stac();
98+
99+
/*
100+
* No memory constraint because it doesn't change any memory gcc
101+
* knows about.
102+
*/
103+
asm volatile(
104+
"1:\n\t"
105+
ALTERNATIVE_3("rep stosb",
106+
"call clear_user_erms", ALT_NOT(X86_FEATURE_FSRM),
107+
"call clear_user_rep_good", ALT_NOT(X86_FEATURE_ERMS),
108+
"call clear_user_original", ALT_NOT(X86_FEATURE_REP_GOOD))
109+
"2:\n"
110+
_ASM_EXTABLE_UA(1b, 2b)
111+
: "+c" (size), "+D" (addr), ASM_CALL_CONSTRAINT
112+
: "a" (0)
113+
/* rep_good clobbers %rdx */
114+
: "rdx");
115+
116+
clac();
117+
118+
return size;
119+
}
120+
121+
static __always_inline unsigned long clear_user(void __user *to, unsigned long n)
122+
{
123+
if (access_ok(to, n))
124+
return __clear_user(to, n);
125+
return n;
126+
}
82127
#endif /* _ASM_X86_UACCESS_64_H */

arch/x86/lib/clear_page_64.S

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* SPDX-License-Identifier: GPL-2.0-only */
22
#include <linux/linkage.h>
3+
#include <asm/asm.h>
34
#include <asm/export.h>
45

56
/*
@@ -50,3 +51,140 @@ SYM_FUNC_START(clear_page_erms)
5051
RET
5152
SYM_FUNC_END(clear_page_erms)
5253
EXPORT_SYMBOL_GPL(clear_page_erms)
54+
55+
/*
56+
* Default clear user-space.
57+
* Input:
58+
* rdi destination
59+
* rcx count
60+
*
61+
* Output:
62+
* rcx: uncleared bytes or 0 if successful.
63+
*/
64+
SYM_FUNC_START(clear_user_original)
65+
/*
66+
* Copy only the lower 32 bits of size as that is enough to handle the rest bytes,
67+
* i.e., no need for a 'q' suffix and thus a REX prefix.
68+
*/
69+
mov %ecx,%eax
70+
shr $3,%rcx
71+
jz .Lrest_bytes
72+
73+
# do the qwords first
74+
.p2align 4
75+
.Lqwords:
76+
movq $0,(%rdi)
77+
lea 8(%rdi),%rdi
78+
dec %rcx
79+
jnz .Lqwords
80+
81+
.Lrest_bytes:
82+
and $7, %eax
83+
jz .Lexit
84+
85+
# now do the rest bytes
86+
.Lbytes:
87+
movb $0,(%rdi)
88+
inc %rdi
89+
dec %eax
90+
jnz .Lbytes
91+
92+
.Lexit:
93+
/*
94+
* %rax still needs to be cleared in the exception case because this function is called
95+
* from inline asm and the compiler expects %rax to be zero when exiting the inline asm,
96+
* in case it might reuse it somewhere.
97+
*/
98+
xor %eax,%eax
99+
RET
100+
101+
.Lqwords_exception:
102+
# convert remaining qwords back into bytes to return to caller
103+
shl $3, %rcx
104+
and $7, %eax
105+
add %rax,%rcx
106+
jmp .Lexit
107+
108+
.Lbytes_exception:
109+
mov %eax,%ecx
110+
jmp .Lexit
111+
112+
_ASM_EXTABLE_UA(.Lqwords, .Lqwords_exception)
113+
_ASM_EXTABLE_UA(.Lbytes, .Lbytes_exception)
114+
SYM_FUNC_END(clear_user_original)
115+
EXPORT_SYMBOL(clear_user_original)
116+
117+
/*
118+
* Alternative clear user-space when CPU feature X86_FEATURE_REP_GOOD is
119+
* present.
120+
* Input:
121+
* rdi destination
122+
* rcx count
123+
*
124+
* Output:
125+
* rcx: uncleared bytes or 0 if successful.
126+
*/
127+
SYM_FUNC_START(clear_user_rep_good)
128+
# call the original thing for less than a cacheline
129+
cmp $64, %rcx
130+
jb clear_user_original
131+
132+
.Lprep:
133+
# copy lower 32-bits for rest bytes
134+
mov %ecx, %edx
135+
shr $3, %rcx
136+
jz .Lrep_good_rest_bytes
137+
138+
.Lrep_good_qwords:
139+
rep stosq
140+
141+
.Lrep_good_rest_bytes:
142+
and $7, %edx
143+
jz .Lrep_good_exit
144+
145+
.Lrep_good_bytes:
146+
mov %edx, %ecx
147+
rep stosb
148+
149+
.Lrep_good_exit:
150+
# see .Lexit comment above
151+
xor %eax, %eax
152+
RET
153+
154+
.Lrep_good_qwords_exception:
155+
# convert remaining qwords back into bytes to return to caller
156+
shl $3, %rcx
157+
and $7, %edx
158+
add %rdx, %rcx
159+
jmp .Lrep_good_exit
160+
161+
_ASM_EXTABLE_UA(.Lrep_good_qwords, .Lrep_good_qwords_exception)
162+
_ASM_EXTABLE_UA(.Lrep_good_bytes, .Lrep_good_exit)
163+
SYM_FUNC_END(clear_user_rep_good)
164+
EXPORT_SYMBOL(clear_user_rep_good)
165+
166+
/*
167+
* Alternative clear user-space when CPU feature X86_FEATURE_ERMS is present.
168+
* Input:
169+
* rdi destination
170+
* rcx count
171+
*
172+
* Output:
173+
* rcx: uncleared bytes or 0 if successful.
174+
*
175+
*/
176+
SYM_FUNC_START(clear_user_erms)
177+
# call the original thing for less than a cacheline
178+
cmp $64, %rcx
179+
jb clear_user_original
180+
181+
.Lerms_bytes:
182+
rep stosb
183+
184+
.Lerms_exit:
185+
xorl %eax,%eax
186+
RET
187+
188+
_ASM_EXTABLE_UA(.Lerms_bytes, .Lerms_exit)
189+
SYM_FUNC_END(clear_user_erms)
190+
EXPORT_SYMBOL(clear_user_erms)

arch/x86/lib/usercopy_64.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,6 @@
1414
* Zero Userspace
1515
*/
1616

17-
unsigned long __clear_user(void __user *addr, unsigned long size)
18-
{
19-
long __d0;
20-
might_fault();
21-
/* no memory constraint because it doesn't change any memory gcc knows
22-
about */
23-
stac();
24-
asm volatile(
25-
" testq %[size8],%[size8]\n"
26-
" jz 4f\n"
27-
" .align 16\n"
28-
"0: movq $0,(%[dst])\n"
29-
" addq $8,%[dst]\n"
30-
" decl %%ecx ; jnz 0b\n"
31-
"4: movq %[size1],%%rcx\n"
32-
" testl %%ecx,%%ecx\n"
33-
" jz 2f\n"
34-
"1: movb $0,(%[dst])\n"
35-
" incq %[dst]\n"
36-
" decl %%ecx ; jnz 1b\n"
37-
"2:\n"
38-
39-
_ASM_EXTABLE_TYPE_REG(0b, 2b, EX_TYPE_UCOPY_LEN8, %[size1])
40-
_ASM_EXTABLE_UA(1b, 2b)
41-
42-
: [size8] "=&c"(size), [dst] "=&D" (__d0)
43-
: [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr));
44-
clac();
45-
return size;
46-
}
47-
EXPORT_SYMBOL(__clear_user);
48-
49-
unsigned long clear_user(void __user *to, unsigned long n)
50-
{
51-
if (access_ok(to, n))
52-
return __clear_user(to, n);
53-
return n;
54-
}
55-
EXPORT_SYMBOL(clear_user);
56-
5717
#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
5818
/**
5919
* clean_cache_range - write back a cache range with CLWB

tools/objtool/check.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,9 @@ static const char *uaccess_safe_builtin[] = {
10711071
"copy_mc_fragile_handle_tail",
10721072
"copy_mc_enhanced_fast_string",
10731073
"ftrace_likely_update", /* CONFIG_TRACE_BRANCH_PROFILING */
1074+
"clear_user_erms",
1075+
"clear_user_rep_good",
1076+
"clear_user_original",
10741077
NULL
10751078
};
10761079

0 commit comments

Comments
 (0)