Skip to content

Commit afc63a9

Browse files
thejhtorvalds
authored andcommitted
coredump: refactor page range dumping into common helper
Both fs/binfmt_elf.c and fs/binfmt_elf_fdpic.c need to dump ranges of pages into the coredump file. Extract that logic into a common helper. Signed-off-by: Jann Horn <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Acked-by: Linus Torvalds <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Alexander Viro <[email protected]> Cc: "Eric W . Biederman" <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Hugh Dickins <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent df0c09c commit afc63a9

File tree

4 files changed

+41
-35
lines changed

4 files changed

+41
-35
lines changed

fs/binfmt_elf.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,26 +2444,8 @@ static int elf_core_dump(struct coredump_params *cprm)
24442444

24452445
for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
24462446
vma = next_vma(vma, gate_vma)) {
2447-
unsigned long addr;
2448-
unsigned long end;
2449-
2450-
end = vma->vm_start + vma_filesz[i++];
2451-
2452-
for (addr = vma->vm_start; addr < end; addr += PAGE_SIZE) {
2453-
struct page *page;
2454-
int stop;
2455-
2456-
page = get_dump_page(addr);
2457-
if (page) {
2458-
void *kaddr = kmap(page);
2459-
stop = !dump_emit(cprm, kaddr, PAGE_SIZE);
2460-
kunmap(page);
2461-
put_page(page);
2462-
} else
2463-
stop = !dump_skip(cprm, PAGE_SIZE);
2464-
if (stop)
2465-
goto end_coredump;
2466-
}
2447+
if (!dump_user_range(cprm, vma->vm_start, vma_filesz[i++]))
2448+
goto end_coredump;
24672449
}
24682450
dump_truncate(cprm);
24692451

fs/binfmt_elf_fdpic.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,21 +1534,9 @@ static bool elf_fdpic_dump_segments(struct coredump_params *cprm)
15341534
if (!maydump(vma, cprm->mm_flags))
15351535
continue;
15361536

1537-
for (addr = vma->vm_start; addr < vma->vm_end;
1538-
addr += PAGE_SIZE) {
1539-
bool res;
1540-
struct page *page = get_dump_page(addr);
1541-
if (page) {
1542-
void *kaddr = kmap(page);
1543-
res = dump_emit(cprm, kaddr, PAGE_SIZE);
1544-
kunmap(page);
1545-
put_page(page);
1546-
} else {
1547-
res = dump_skip(cprm, PAGE_SIZE);
1548-
}
1549-
if (!res)
1550-
return false;
1551-
}
1537+
if (!dump_user_range(cprm, vma->vm_start,
1538+
vma->vma_end - vma->vm_start))
1539+
return false;
15521540
}
15531541
return true;
15541542
}

fs/coredump.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,40 @@ int dump_skip(struct coredump_params *cprm, size_t nr)
876876
}
877877
EXPORT_SYMBOL(dump_skip);
878878

879+
#ifdef CONFIG_ELF_CORE
880+
int dump_user_range(struct coredump_params *cprm, unsigned long start,
881+
unsigned long len)
882+
{
883+
unsigned long addr;
884+
885+
for (addr = start; addr < start + len; addr += PAGE_SIZE) {
886+
struct page *page;
887+
int stop;
888+
889+
/*
890+
* To avoid having to allocate page tables for virtual address
891+
* ranges that have never been used yet, and also to make it
892+
* easy to generate sparse core files, use a helper that returns
893+
* NULL when encountering an empty page table entry that would
894+
* otherwise have been filled with the zero page.
895+
*/
896+
page = get_dump_page(addr);
897+
if (page) {
898+
void *kaddr = kmap(page);
899+
900+
stop = !dump_emit(cprm, kaddr, PAGE_SIZE);
901+
kunmap(page);
902+
put_page(page);
903+
} else {
904+
stop = !dump_skip(cprm, PAGE_SIZE);
905+
}
906+
if (stop)
907+
return 0;
908+
}
909+
return 1;
910+
}
911+
#endif
912+
879913
int dump_align(struct coredump_params *cprm, int align)
880914
{
881915
unsigned mod = cprm->pos & (align - 1);

include/linux/coredump.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern int dump_skip(struct coredump_params *cprm, size_t nr);
1616
extern int dump_emit(struct coredump_params *cprm, const void *addr, int nr);
1717
extern int dump_align(struct coredump_params *cprm, int align);
1818
extern void dump_truncate(struct coredump_params *cprm);
19+
int dump_user_range(struct coredump_params *cprm, unsigned long start,
20+
unsigned long len);
1921
#ifdef CONFIG_COREDUMP
2022
extern void do_coredump(const kernel_siginfo_t *siginfo);
2123
#else

0 commit comments

Comments
 (0)