diff --git a/arch/powerpc/kexec/crash.c b/arch/powerpc/kexec/crash.c
index 775895f31037..fc0105c7af4c 100644
--- a/arch/powerpc/kexec/crash.c
+++ b/arch/powerpc/kexec/crash.c
@@ -478,7 +478,7 @@ unsigned int arch_crash_get_elfcorehdr_size(void)
if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG))
phdr_cnt += CONFIG_CRASH_MAX_MEMORY_RANGES;
- return sizeof(struct elfhdr) + (phdr_cnt * sizeof(Elf64_Phdr));
+ return elf64_phdr_size(phdr_cnt);
}
/**
diff --git a/arch/powerpc/platforms/powernv/opal-core.c b/arch/powerpc/platforms/powernv/opal-core.c
index 32662d30d70f..fc0aad61504b 100644
--- a/arch/powerpc/platforms/powernv/opal-core.c
+++ b/arch/powerpc/platforms/powernv/opal-core.c
@@ -309,8 +309,7 @@ static int __init create_opalcore(void)
char *bufp;
/* Get size of header & CPU notes for OPAL core */
- hdr_size = (sizeof(Elf64_Ehdr) +
- ((oc_conf->ptload_cnt + 1) * sizeof(Elf64_Phdr)));
+ hdr_size = elf64_phdr_size(oc_conf->ptload_cnt + 1);
cpu_notes_size = ((oc_conf->num_cpus * (CRASH_CORE_NOTE_HEAD_BYTES +
CRASH_CORE_NOTE_NAME_BYTES +
CRASH_CORE_NOTE_DESC_BYTES)) +
diff --git a/arch/x86/kernel/crash.c b/arch/x86/kernel/crash.c
index d6430a593778..e164749c5c1d 100644
--- a/arch/x86/kernel/crash.c
+++ b/arch/x86/kernel/crash.c
@@ -374,8 +374,7 @@ int crash_load_segments(struct kimage *image)
pnum += 2 + CONFIG_NR_CPUS_DEFAULT;
if (pnum < (unsigned long)PN_XNUM) {
- kbuf.memsz = pnum * sizeof(Elf64_Phdr);
- kbuf.memsz += sizeof(Elf64_Ehdr);
+ kbuf.memsz = elf64_phdr_size(pnum);
image->elfcorehdr_index = image->nr_segments;
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c
index 44d15436439f..ff324969d798 100644
--- a/fs/proc/vmcore.c
+++ b/fs/proc/vmcore.c
@@ -1238,8 +1238,7 @@ static int __init parse_crash_elf64_headers(void)
}
/* Read in all elf headers. */
- elfcorebuf_sz_orig = sizeof(Elf64_Ehdr) +
- ehdr.e_phnum * sizeof(Elf64_Phdr);
+ elfcorebuf_sz_orig = elf64_phdr_size(ehdr.e_phnum);
elfcorebuf_sz = elfcorebuf_sz_orig;
elfcorebuf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
get_order(elfcorebuf_sz_orig));
@@ -1605,8 +1604,7 @@ static int vmcore_add_device_ram_elf64(struct list_head *list, size_t count)
}
/* elfcorebuf_sz must always cover full pages. */
- new_size = sizeof(Elf64_Ehdr) +
- (ehdr->e_phnum + count) * sizeof(Elf64_Phdr);
+ new_size = elf64_phdr_size(ehdr->e_phnum + count);
new_size = roundup(new_size, PAGE_SIZE);
/*
diff --git a/include/linux/elf.h b/include/linux/elf.h
index 5c402788da19..400f58a13d92 100644
--- a/include/linux/elf.h
+++ b/include/linux/elf.h
@@ -109,4 +109,8 @@ static inline int arch_elf_adjust_prot(int prot,
}
#endif
+static inline unsigned long elf64_phdr_size(unsigned long phdr_cnt)
+{
+ return phdr_cnt * sizeof(Elf64_Phdr) + sizeof(Elf64_Ehdr);
+}
#endif /* _LINUX_ELF_H */diff --git a/kernel/crash_core.c b/kernel/crash_core.c
index 39feb39b53be..b57a81f38ef4 100644
--- a/kernel/crash_core.c
+++ b/kernel/crash_core.c
@@ -193,7 +193,7 @@ int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
*/
nr_phdr++;
- elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
+ elf_sz = elf64_phdr_size(nr_phdr);
elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
buf = vzalloc(elf_sz);
--
2.34.1