Skip to content

Commit e7088ae

Browse files
Kefeng Wangakpm00
authored andcommitted
mm: hwpoison: coredump: support recovery from dump_user_range()
The 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/clear/check 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 a3e482a commit e7088ae

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

fs/coredump.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,9 @@ 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);
887+
iov_iter_clear_copy_mc(&iter);
886888
if (n != PAGE_SIZE)
887889
return 0;
888890
file->f_pos = pos;

include/linux/uio.h

Lines changed: 26 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;
@@ -126,6 +127,30 @@ static inline bool user_backed_iter(const struct iov_iter *i)
126127
return i->user_backed;
127128
}
128129

130+
#ifdef CONFIG_ARCH_HAS_COPY_MC
131+
static inline void iov_iter_set_copy_mc(struct iov_iter *i)
132+
{
133+
i->copy_mc = true;
134+
}
135+
136+
static inline void iov_iter_clear_copy_mc(struct iov_iter *i)
137+
{
138+
i->copy_mc = false;
139+
}
140+
141+
static inline bool iov_iter_is_copy_mc(const struct iov_iter *i)
142+
{
143+
return i->copy_mc;
144+
}
145+
#else
146+
static inline void iov_iter_set_copy_mc(struct iov_iter *i) { }
147+
static inline void iov_iter_clear_copy_mc(struct iov_iter *i) {}
148+
static inline bool iov_iter_is_copy_mc(const struct iov_iter *i)
149+
{
150+
return false;
151+
}
152+
#endif
153+
129154
/*
130155
* Total number of bytes covered by an iovec.
131156
*
@@ -360,6 +385,7 @@ static inline void iov_iter_ubuf(struct iov_iter *i, unsigned int direction,
360385
.iter_type = ITER_UBUF,
361386
.user_backed = true,
362387
.data_source = direction,
388+
.copy_mc = false,
363389
.ubuf = buf,
364390
.count = count
365391
};

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)