Skip to content

Commit 930218a

Browse files
committed
Merge branch 'for-4.18/mcsafe' into libnvdimm-for-next
2 parents b568457 + 5d8beee commit 930218a

23 files changed

Lines changed: 485 additions & 79 deletions

File tree

‎arch/x86/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ config X86
6060
select ARCH_HAS_PMEM_API if X86_64
6161
select ARCH_HAS_REFCOUNT
6262
select ARCH_HAS_UACCESS_FLUSHCACHE if X86_64
63+
select ARCH_HAS_UACCESS_MCSAFE if X86_64
6364
select ARCH_HAS_SET_MEMORY
6465
select ARCH_HAS_SG_CHAIN
6566
select ARCH_HAS_STRICT_KERNEL_RWX

‎arch/x86/Kconfig.debug‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ config EARLY_PRINTK_USB_XDBC
7272
You should normally say N here, unless you want to debug early
7373
crashes or need a very simple printk logging facility.
7474

75+
config MCSAFE_TEST
76+
def_bool n
77+
7578
config X86_PTDUMP_CORE
7679
def_bool n
7780

‎arch/x86/include/asm/mcsafe_test.h‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _MCSAFE_TEST_H_
3+
#define _MCSAFE_TEST_H_
4+
5+
#ifndef __ASSEMBLY__
6+
#ifdef CONFIG_MCSAFE_TEST
7+
extern unsigned long mcsafe_test_src;
8+
extern unsigned long mcsafe_test_dst;
9+
10+
static inline void mcsafe_inject_src(void *addr)
11+
{
12+
if (addr)
13+
mcsafe_test_src = (unsigned long) addr;
14+
else
15+
mcsafe_test_src = ~0UL;
16+
}
17+
18+
static inline void mcsafe_inject_dst(void *addr)
19+
{
20+
if (addr)
21+
mcsafe_test_dst = (unsigned long) addr;
22+
else
23+
mcsafe_test_dst = ~0UL;
24+
}
25+
#else /* CONFIG_MCSAFE_TEST */
26+
static inline void mcsafe_inject_src(void *addr)
27+
{
28+
}
29+
30+
static inline void mcsafe_inject_dst(void *addr)
31+
{
32+
}
33+
#endif /* CONFIG_MCSAFE_TEST */
34+
35+
#else /* __ASSEMBLY__ */
36+
#include <asm/export.h>
37+
38+
#ifdef CONFIG_MCSAFE_TEST
39+
.macro MCSAFE_TEST_CTL
40+
.pushsection .data
41+
.align 8
42+
.globl mcsafe_test_src
43+
mcsafe_test_src:
44+
.quad 0
45+
EXPORT_SYMBOL_GPL(mcsafe_test_src)
46+
.globl mcsafe_test_dst
47+
mcsafe_test_dst:
48+
.quad 0
49+
EXPORT_SYMBOL_GPL(mcsafe_test_dst)
50+
.popsection
51+
.endm
52+
53+
.macro MCSAFE_TEST_SRC reg count target
54+
leaq \count(\reg), %r9
55+
cmp mcsafe_test_src, %r9
56+
ja \target
57+
.endm
58+
59+
.macro MCSAFE_TEST_DST reg count target
60+
leaq \count(\reg), %r9
61+
cmp mcsafe_test_dst, %r9
62+
ja \target
63+
.endm
64+
#else
65+
.macro MCSAFE_TEST_CTL
66+
.endm
67+
68+
.macro MCSAFE_TEST_SRC reg count target
69+
.endm
70+
71+
.macro MCSAFE_TEST_DST reg count target
72+
.endm
73+
#endif /* CONFIG_MCSAFE_TEST */
74+
#endif /* __ASSEMBLY__ */
75+
#endif /* _MCSAFE_TEST_H_ */

‎arch/x86/include/asm/string_64.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ int strcmp(const char *cs, const char *ct);
116116
#endif
117117

118118
#define __HAVE_ARCH_MEMCPY_MCSAFE 1
119-
__must_check int memcpy_mcsafe_unrolled(void *dst, const void *src, size_t cnt);
119+
__must_check unsigned long __memcpy_mcsafe(void *dst, const void *src,
120+
size_t cnt);
120121
DECLARE_STATIC_KEY_FALSE(mcsafe_key);
121122

122123
/**
@@ -131,14 +132,15 @@ DECLARE_STATIC_KEY_FALSE(mcsafe_key);
131132
* actually do machine check recovery. Everyone else can just
132133
* use memcpy().
133134
*
134-
* Return 0 for success, -EFAULT for fail
135+
* Return 0 for success, or number of bytes not copied if there was an
136+
* exception.
135137
*/
136-
static __always_inline __must_check int
138+
static __always_inline __must_check unsigned long
137139
memcpy_mcsafe(void *dst, const void *src, size_t cnt)
138140
{
139141
#ifdef CONFIG_X86_MCE
140142
if (static_branch_unlikely(&mcsafe_key))
141-
return memcpy_mcsafe_unrolled(dst, src, cnt);
143+
return __memcpy_mcsafe(dst, src, cnt);
142144
else
143145
#endif
144146
memcpy(dst, src, cnt);

‎arch/x86/include/asm/uaccess_64.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ copy_user_generic(void *to, const void *from, unsigned len)
4646
return ret;
4747
}
4848

49+
static __always_inline __must_check unsigned long
50+
copy_to_user_mcsafe(void *to, const void *from, unsigned len)
51+
{
52+
unsigned long ret;
53+
54+
__uaccess_begin();
55+
ret = memcpy_mcsafe(to, from, len);
56+
__uaccess_end();
57+
return ret;
58+
}
59+
4960
static __always_inline __must_check unsigned long
5061
raw_copy_from_user(void *dst, const void __user *src, unsigned long size)
5162
{
@@ -194,4 +205,7 @@ __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
194205
unsigned long
195206
copy_user_handle_tail(char *to, char *from, unsigned len);
196207

208+
unsigned long
209+
mcsafe_handle_tail(char *to, char *from, unsigned len);
210+
197211
#endif /* _ASM_X86_UACCESS_64_H */

‎arch/x86/lib/memcpy_64.S‎

Lines changed: 54 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/linkage.h>
44
#include <asm/errno.h>
55
#include <asm/cpufeatures.h>
6+
#include <asm/mcsafe_test.h>
67
#include <asm/alternative-asm.h>
78
#include <asm/export.h>
89

@@ -183,12 +184,15 @@ ENTRY(memcpy_orig)
183184
ENDPROC(memcpy_orig)
184185

185186
#ifndef CONFIG_UML
187+
188+
MCSAFE_TEST_CTL
189+
186190
/*
187-
* memcpy_mcsafe_unrolled - memory copy with machine check exception handling
191+
* __memcpy_mcsafe - memory copy with machine check exception handling
188192
* Note that we only catch machine checks when reading the source addresses.
189193
* Writes to target are posted and don't generate machine checks.
190194
*/
191-
ENTRY(memcpy_mcsafe_unrolled)
195+
ENTRY(__memcpy_mcsafe)
192196
cmpl $8, %edx
193197
/* Less than 8 bytes? Go to byte copy loop */
194198
jb .L_no_whole_words
@@ -204,58 +208,33 @@ ENTRY(memcpy_mcsafe_unrolled)
204208
subl $8, %ecx
205209
negl %ecx
206210
subl %ecx, %edx
207-
.L_copy_leading_bytes:
211+
.L_read_leading_bytes:
208212
movb (%rsi), %al
213+
MCSAFE_TEST_SRC %rsi 1 .E_leading_bytes
214+
MCSAFE_TEST_DST %rdi 1 .E_leading_bytes
215+
.L_write_leading_bytes:
209216
movb %al, (%rdi)
210217
incq %rsi
211218
incq %rdi
212219
decl %ecx
213-
jnz .L_copy_leading_bytes
220+
jnz .L_read_leading_bytes
214221

215222
.L_8byte_aligned:
216-
/* Figure out how many whole cache lines (64-bytes) to copy */
217-
movl %edx, %ecx
218-
andl $63, %edx
219-
shrl $6, %ecx
220-
jz .L_no_whole_cache_lines
221-
222-
/* Loop copying whole cache lines */
223-
.L_cache_w0: movq (%rsi), %r8
224-
.L_cache_w1: movq 1*8(%rsi), %r9
225-
.L_cache_w2: movq 2*8(%rsi), %r10
226-
.L_cache_w3: movq 3*8(%rsi), %r11
227-
movq %r8, (%rdi)
228-
movq %r9, 1*8(%rdi)
229-
movq %r10, 2*8(%rdi)
230-
movq %r11, 3*8(%rdi)
231-
.L_cache_w4: movq 4*8(%rsi), %r8
232-
.L_cache_w5: movq 5*8(%rsi), %r9
233-
.L_cache_w6: movq 6*8(%rsi), %r10
234-
.L_cache_w7: movq 7*8(%rsi), %r11
235-
movq %r8, 4*8(%rdi)
236-
movq %r9, 5*8(%rdi)
237-
movq %r10, 6*8(%rdi)
238-
movq %r11, 7*8(%rdi)
239-
leaq 64(%rsi), %rsi
240-
leaq 64(%rdi), %rdi
241-
decl %ecx
242-
jnz .L_cache_w0
243-
244-
/* Are there any trailing 8-byte words? */
245-
.L_no_whole_cache_lines:
246223
movl %edx, %ecx
247224
andl $7, %edx
248225
shrl $3, %ecx
249226
jz .L_no_whole_words
250227

251-
/* Copy trailing words */
252-
.L_copy_trailing_words:
228+
.L_read_words:
253229
movq (%rsi), %r8
254-
mov %r8, (%rdi)
255-
leaq 8(%rsi), %rsi
256-
leaq 8(%rdi), %rdi
230+
MCSAFE_TEST_SRC %rsi 8 .E_read_words
231+
MCSAFE_TEST_DST %rdi 8 .E_write_words
232+
.L_write_words:
233+
movq %r8, (%rdi)
234+
addq $8, %rsi
235+
addq $8, %rdi
257236
decl %ecx
258-
jnz .L_copy_trailing_words
237+
jnz .L_read_words
259238

260239
/* Any trailing bytes? */
261240
.L_no_whole_words:
@@ -264,38 +243,55 @@ ENTRY(memcpy_mcsafe_unrolled)
264243

265244
/* Copy trailing bytes */
266245
movl %edx, %ecx
267-
.L_copy_trailing_bytes:
246+
.L_read_trailing_bytes:
268247
movb (%rsi), %al
248+
MCSAFE_TEST_SRC %rsi 1 .E_trailing_bytes
249+
MCSAFE_TEST_DST %rdi 1 .E_trailing_bytes
250+
.L_write_trailing_bytes:
269251
movb %al, (%rdi)
270252
incq %rsi
271253
incq %rdi
272254
decl %ecx
273-
jnz .L_copy_trailing_bytes
255+
jnz .L_read_trailing_bytes
274256

275257
/* Copy successful. Return zero */
276258
.L_done_memcpy_trap:
277259
xorq %rax, %rax
278260
ret
279-
ENDPROC(memcpy_mcsafe_unrolled)
280-
EXPORT_SYMBOL_GPL(memcpy_mcsafe_unrolled)
261+
ENDPROC(__memcpy_mcsafe)
262+
EXPORT_SYMBOL_GPL(__memcpy_mcsafe)
281263

282264
.section .fixup, "ax"
283-
/* Return -EFAULT for any failure */
284-
.L_memcpy_mcsafe_fail:
285-
mov $-EFAULT, %rax
265+
/*
266+
* Return number of bytes not copied for any failure. Note that
267+
* there is no "tail" handling since the source buffer is 8-byte
268+
* aligned and poison is cacheline aligned.
269+
*/
270+
.E_read_words:
271+
shll $3, %ecx
272+
.E_leading_bytes:
273+
addl %edx, %ecx
274+
.E_trailing_bytes:
275+
mov %ecx, %eax
286276
ret
287277

278+
/*
279+
* For write fault handling, given the destination is unaligned,
280+
* we handle faults on multi-byte writes with a byte-by-byte
281+
* copy up to the write-protected page.
282+
*/
283+
.E_write_words:
284+
shll $3, %ecx
285+
addl %edx, %ecx
286+
movl %ecx, %edx
287+
jmp mcsafe_handle_tail
288+
288289
.previous
289290

290-
_ASM_EXTABLE_FAULT(.L_copy_leading_bytes, .L_memcpy_mcsafe_fail)
291-
_ASM_EXTABLE_FAULT(.L_cache_w0, .L_memcpy_mcsafe_fail)
292-
_ASM_EXTABLE_FAULT(.L_cache_w1, .L_memcpy_mcsafe_fail)
293-
_ASM_EXTABLE_FAULT(.L_cache_w2, .L_memcpy_mcsafe_fail)
294-
_ASM_EXTABLE_FAULT(.L_cache_w3, .L_memcpy_mcsafe_fail)
295-
_ASM_EXTABLE_FAULT(.L_cache_w4, .L_memcpy_mcsafe_fail)
296-
_ASM_EXTABLE_FAULT(.L_cache_w5, .L_memcpy_mcsafe_fail)
297-
_ASM_EXTABLE_FAULT(.L_cache_w6, .L_memcpy_mcsafe_fail)
298-
_ASM_EXTABLE_FAULT(.L_cache_w7, .L_memcpy_mcsafe_fail)
299-
_ASM_EXTABLE_FAULT(.L_copy_trailing_words, .L_memcpy_mcsafe_fail)
300-
_ASM_EXTABLE_FAULT(.L_copy_trailing_bytes, .L_memcpy_mcsafe_fail)
291+
_ASM_EXTABLE_FAULT(.L_read_leading_bytes, .E_leading_bytes)
292+
_ASM_EXTABLE_FAULT(.L_read_words, .E_read_words)
293+
_ASM_EXTABLE_FAULT(.L_read_trailing_bytes, .E_trailing_bytes)
294+
_ASM_EXTABLE(.L_write_leading_bytes, .E_leading_bytes)
295+
_ASM_EXTABLE(.L_write_words, .E_write_words)
296+
_ASM_EXTABLE(.L_write_trailing_bytes, .E_trailing_bytes)
301297
#endif

‎arch/x86/lib/usercopy_64.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ copy_user_handle_tail(char *to, char *from, unsigned len)
7575
return len;
7676
}
7777

78+
/*
79+
* Similar to copy_user_handle_tail, probe for the write fault point,
80+
* but reuse __memcpy_mcsafe in case a new read error is encountered.
81+
* clac() is handled in _copy_to_iter_mcsafe().
82+
*/
83+
__visible unsigned long
84+
mcsafe_handle_tail(char *to, char *from, unsigned len)
85+
{
86+
for (; len; --len, to++, from++) {
87+
/*
88+
* Call the assembly routine back directly since
89+
* memcpy_mcsafe() may silently fallback to memcpy.
90+
*/
91+
unsigned long rem = __memcpy_mcsafe(to, from, 1);
92+
93+
if (rem)
94+
break;
95+
}
96+
return len;
97+
}
98+
7899
#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
79100
/**
80101
* clean_cache_range - write back a cache range with CLWB

‎drivers/dax/super.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,16 @@ size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
287287
}
288288
EXPORT_SYMBOL_GPL(dax_copy_from_iter);
289289

290+
size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
291+
size_t bytes, struct iov_iter *i)
292+
{
293+
if (!dax_alive(dax_dev))
294+
return 0;
295+
296+
return dax_dev->ops->copy_to_iter(dax_dev, pgoff, addr, bytes, i);
297+
}
298+
EXPORT_SYMBOL_GPL(dax_copy_to_iter);
299+
290300
#ifdef CONFIG_ARCH_HAS_PMEM_API
291301
void arch_wb_cache_pmem(void *addr, size_t size);
292302
void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)

‎drivers/md/dm-linear.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,24 @@ static size_t linear_dax_copy_from_iter(struct dm_target *ti, pgoff_t pgoff,
185185
return dax_copy_from_iter(dax_dev, pgoff, addr, bytes, i);
186186
}
187187

188+
static size_t linear_dax_copy_to_iter(struct dm_target *ti, pgoff_t pgoff,
189+
void *addr, size_t bytes, struct iov_iter *i)
190+
{
191+
struct linear_c *lc = ti->private;
192+
struct block_device *bdev = lc->dev->bdev;
193+
struct dax_device *dax_dev = lc->dev->dax_dev;
194+
sector_t dev_sector, sector = pgoff * PAGE_SECTORS;
195+
196+
dev_sector = linear_map_sector(ti, sector);
197+
if (bdev_dax_pgoff(bdev, dev_sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
198+
return 0;
199+
return dax_copy_to_iter(dax_dev, pgoff, addr, bytes, i);
200+
}
201+
188202
#else
189203
#define linear_dax_direct_access NULL
190204
#define linear_dax_copy_from_iter NULL
205+
#define linear_dax_copy_to_iter NULL
191206
#endif
192207

193208
static struct target_type linear_target = {
@@ -204,6 +219,7 @@ static struct target_type linear_target = {
204219
.iterate_devices = linear_iterate_devices,
205220
.direct_access = linear_dax_direct_access,
206221
.dax_copy_from_iter = linear_dax_copy_from_iter,
222+
.dax_copy_to_iter = linear_dax_copy_to_iter,
207223
};
208224

209225
int __init dm_linear_init(void)

0 commit comments

Comments
 (0)