Skip to content

Commit 8e27e87

Browse files
Kefeng Wangakpm00
authored andcommitted
mm: hwpoison: coredump: support recovery from dump_user_range()
dump_user_range() is used to copy the user page to a coredump file, but if a hardware memory error occurred during copy, which called from __kernel_write_iter() in dump_user_range(), it crashes, CPU: 112 PID: 7014 Comm: mca-recover Not tainted 6.3.0-rc2 torvalds#425 pc : __memcpy+0x110/0x260 lr : _copy_from_iter+0x3bc/0x4c8 ... Call trace: __memcpy+0x110/0x260 copy_page_from_iter+0xcc/0x130 pipe_write+0x164/0x6d8 __kernel_write_iter+0x9c/0x210 dump_user_range+0xc8/0x1d8 elf_core_dump+0x308/0x368 do_coredump+0x2e8/0xa40 get_signal+0x59c/0x788 do_signal+0x118/0x1f8 do_notify_resume+0xf0/0x280 el0_da+0x130/0x138 el0t_64_sync_handler+0x68/0xc0 el0t_64_sync+0x188/0x190 Generally, the '->write_iter' of file ops will use copy_page_from_iter() and copy_page_from_iter_atomic(), change memcpy() to copy_mc_to_kernel() in both of them to handle #MC during source read, which stop coredump processing and kill the task instead of kernel panic, but the source address may not always a user address, so introduce a new copy_mc flag in struct iov_iter{} to indicate that the iter could do a safe memory copy, also introduce the helpers to set/cleck the flag, for now, it's only used in coredump's dump_user_range(), but it could expand to any other scenarios to fix the similar issue. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Kefeng Wang <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Christian Brauner <[email protected]> Cc: Miaohe Lin <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: Tong Tiangen <[email protected]> Cc: Jens Axboe <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 6c4614d commit 8e27e87

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

fs/coredump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ static int dump_emit_page(struct coredump_params *cprm, struct page *page)
882882
pos = file->f_pos;
883883
bvec_set_page(&bvec, page, PAGE_SIZE, 0);
884884
iov_iter_bvec(&iter, ITER_SOURCE, &bvec, 1, PAGE_SIZE);
885+
iov_iter_set_copy_mc(&iter);
885886
n = __kernel_write_iter(cprm->file, &iter, &pos);
886887
if (n != PAGE_SIZE)
887888
return 0;

include/linux/uio.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct iov_iter_state {
4242

4343
struct iov_iter {
4444
u8 iter_type;
45+
bool copy_mc;
4546
bool nofault;
4647
bool data_source;
4748
bool user_backed;
@@ -234,8 +235,22 @@ size_t _copy_from_iter_flushcache(void *addr, size_t bytes, struct iov_iter *i);
234235

235236
#ifdef CONFIG_ARCH_HAS_COPY_MC
236237
size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i);
238+
static inline void iov_iter_set_copy_mc(struct iov_iter *i)
239+
{
240+
i->copy_mc = true;
241+
}
242+
243+
static inline bool iov_iter_is_copy_mc(const struct iov_iter *i)
244+
{
245+
return i->copy_mc;
246+
}
237247
#else
238248
#define _copy_mc_to_iter _copy_to_iter
249+
static inline void iov_iter_set_copy_mc(struct iov_iter *i) { }
250+
static inline bool iov_iter_is_copy_mc(const struct iov_iter *i)
251+
{
252+
return false;
253+
}
239254
#endif
240255

241256
size_t iov_iter_zero(size_t bytes, struct iov_iter *);
@@ -358,6 +373,7 @@ static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction,
358373
WARN_ON(direction & ~(READ | WRITE));
359374
*i = (struct iov_iter) {
360375
.iter_type = ITER_UBUF,
376+
.copy_mc = false,
361377
.user_backed = true,
362378
.data_source = direction,
363379
.ubuf = buf,

lib/iov_iter.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction,
434434
WARN_ON(direction & ~(READ | WRITE));
435435
*i = (struct iov_iter) {
436436
.iter_type = ITER_IOVEC,
437+
.copy_mc = false,
437438
.nofault = false,
438439
.user_backed = true,
439440
.data_source = direction,
@@ -630,6 +631,14 @@ size_t _copy_mc_to_iter(const void *addr, size_t bytes, struct iov_iter *i)
630631
EXPORT_SYMBOL_GPL(_copy_mc_to_iter);
631632
#endif /* CONFIG_ARCH_HAS_COPY_MC */
632633

634+
static void *memcpy_from_iter(struct iov_iter *i, void *to, const void *from,
635+
size_t size)
636+
{
637+
if (iov_iter_is_copy_mc(i))
638+
return (void *)copy_mc_to_kernel(to, from, size);
639+
return memcpy(to, from, size);
640+
}
641+
633642
size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
634643
{
635644
if (WARN_ON_ONCE(!i->data_source))
@@ -639,7 +648,7 @@ size_t _copy_from_iter(void *addr, size_t bytes, struct iov_iter *i)
639648
might_fault();
640649
iterate_and_advance(i, bytes, base, len, off,
641650
copyin(addr + off, base, len),
642-
memcpy(addr + off, base, len)
651+
memcpy_from_iter(i, addr + off, base, len)
643652
)
644653

645654
return bytes;
@@ -862,7 +871,7 @@ size_t copy_page_from_iter_atomic(struct page *page, unsigned offset, size_t byt
862871
}
863872
iterate_and_advance(i, bytes, base, len, off,
864873
copyin(p + off, base, len),
865-
memcpy(p + off, base, len)
874+
memcpy_from_iter(i, p + off, base, len)
866875
)
867876
kunmap_atomic(kaddr);
868877
return bytes;
@@ -1043,6 +1052,7 @@ void iov_iter_kvec(struct iov_iter *i, unsigned int direction,
10431052
WARN_ON(direction & ~(READ | WRITE));
10441053
*i = (struct iov_iter){
10451054
.iter_type = ITER_KVEC,
1055+
.copy_mc = false,
10461056
.data_source = direction,
10471057
.kvec = kvec,
10481058
.nr_segs = nr_segs,
@@ -1059,6 +1069,7 @@ void iov_iter_bvec(struct iov_iter *i, unsigned int direction,
10591069
WARN_ON(direction & ~(READ | WRITE));
10601070
*i = (struct iov_iter){
10611071
.iter_type = ITER_BVEC,
1072+
.copy_mc = false,
10621073
.data_source = direction,
10631074
.bvec = bvec,
10641075
.nr_segs = nr_segs,
@@ -1105,6 +1116,7 @@ void iov_iter_xarray(struct iov_iter *i, unsigned int direction,
11051116
BUG_ON(direction & ~1);
11061117
*i = (struct iov_iter) {
11071118
.iter_type = ITER_XARRAY,
1119+
.copy_mc = false,
11081120
.data_source = direction,
11091121
.xarray = xarray,
11101122
.xarray_start = start,
@@ -1128,6 +1140,7 @@ void iov_iter_discard(struct iov_iter *i, unsigned int direction, size_t count)
11281140
BUG_ON(direction != READ);
11291141
*i = (struct iov_iter){
11301142
.iter_type = ITER_DISCARD,
1143+
.copy_mc = false,
11311144
.data_source = false,
11321145
.count = count,
11331146
.iov_offset = 0

0 commit comments

Comments
 (0)