public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <bhe@redhat.com>
To: Eric DeVolder <eric.devolder@oracle.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
	kexec@lists.infradead.org, ebiederm@xmission.com,
	dyoung@redhat.com, vgoyal@redhat.com, tglx@linutronix.de,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	hpa@zytor.com, nramas@linux.microsoft.com,
	thomas.lendacky@amd.com, robh@kernel.org, efault@gmx.de,
	rppt@kernel.org, david@redhat.com, sourabhjain@linux.ibm.com,
	konrad.wilk@oracle.com, boris.ostrovsky@oracle.com
Subject: Re: [PATCH v11 1/7] crash: move crash_prepare_elf64_headers
Date: Wed, 31 Aug 2022 10:50:45 +0800	[thread overview]
Message-ID: <Yw7MhU957+cPQ50/@MiWiFi-R3L-srv> (raw)
In-Reply-To: <20220826173704.1895-2-eric.devolder@oracle.com>

On 08/26/22 at 01:36pm, Eric DeVolder wrote:
> At the outcome of this patch set, the crash_prepare_elf64_headers()
> is utilized on both the kexec_file_load and kexec_load paths. As
> such, need to move this function out of kexec_file.c and into a
> common location crash_core.c.
> 
> No functionality change.
> 
> Signed-off-by: Eric DeVolder <eric.devolder@oracle.com>

LGTM,

Baoquan He <bhe@redhat.com>

> ---
>  kernel/crash_core.c | 100 ++++++++++++++++++++++++++++++++++++++++++++
>  kernel/kexec_file.c |  99 -------------------------------------------
>  2 files changed, 100 insertions(+), 99 deletions(-)
> 
> diff --git a/kernel/crash_core.c b/kernel/crash_core.c
> index 07b26df453a9..8c8c82386e8d 100644
> --- a/kernel/crash_core.c
> +++ b/kernel/crash_core.c
> @@ -10,6 +10,7 @@
>  #include <linux/utsname.h>
>  #include <linux/vmalloc.h>
>  #include <linux/sizes.h>
> +#include <linux/kexec.h>
>  
>  #include <asm/page.h>
>  #include <asm/sections.h>
> @@ -314,6 +315,105 @@ static int __init parse_crashkernel_dummy(char *arg)
>  }
>  early_param("crashkernel", parse_crashkernel_dummy);
>  
> +int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
> +			  void **addr, unsigned long *sz)
> +{
> +	Elf64_Ehdr *ehdr;
> +	Elf64_Phdr *phdr;
> +	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
> +	unsigned char *buf;
> +	unsigned int cpu, i;
> +	unsigned long long notes_addr;
> +	unsigned long mstart, mend;
> +
> +	/* extra phdr for vmcoreinfo ELF note */
> +	nr_phdr = nr_cpus + 1;
> +	nr_phdr += mem->nr_ranges;
> +
> +	/*
> +	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
> +	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
> +	 * I think this is required by tools like gdb. So same physical
> +	 * memory will be mapped in two ELF headers. One will contain kernel
> +	 * text virtual addresses and other will have __va(physical) addresses.
> +	 */
> +
> +	nr_phdr++;
> +	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
> +	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
> +
> +	buf = vzalloc(elf_sz);
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	ehdr = (Elf64_Ehdr *)buf;
> +	phdr = (Elf64_Phdr *)(ehdr + 1);
> +	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
> +	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
> +	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
> +	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
> +	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
> +	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
> +	ehdr->e_type = ET_CORE;
> +	ehdr->e_machine = ELF_ARCH;
> +	ehdr->e_version = EV_CURRENT;
> +	ehdr->e_phoff = sizeof(Elf64_Ehdr);
> +	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
> +	ehdr->e_phentsize = sizeof(Elf64_Phdr);
> +
> +	/* Prepare one phdr of type PT_NOTE for each present CPU */
> +	for_each_present_cpu(cpu) {
> +		phdr->p_type = PT_NOTE;
> +		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
> +		phdr->p_offset = phdr->p_paddr = notes_addr;
> +		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
> +		(ehdr->e_phnum)++;
> +		phdr++;
> +	}
> +
> +	/* Prepare one PT_NOTE header for vmcoreinfo */
> +	phdr->p_type = PT_NOTE;
> +	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
> +	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
> +	(ehdr->e_phnum)++;
> +	phdr++;
> +
> +	/* Prepare PT_LOAD type program header for kernel text region */
> +	if (need_kernel_map) {
> +		phdr->p_type = PT_LOAD;
> +		phdr->p_flags = PF_R|PF_W|PF_X;
> +		phdr->p_vaddr = (unsigned long) _text;
> +		phdr->p_filesz = phdr->p_memsz = _end - _text;
> +		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
> +		ehdr->e_phnum++;
> +		phdr++;
> +	}
> +
> +	/* Go through all the ranges in mem->ranges[] and prepare phdr */
> +	for (i = 0; i < mem->nr_ranges; i++) {
> +		mstart = mem->ranges[i].start;
> +		mend = mem->ranges[i].end;
> +
> +		phdr->p_type = PT_LOAD;
> +		phdr->p_flags = PF_R|PF_W|PF_X;
> +		phdr->p_offset  = mstart;
> +
> +		phdr->p_paddr = mstart;
> +		phdr->p_vaddr = (unsigned long) __va(mstart);
> +		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
> +		phdr->p_align = 0;
> +		ehdr->e_phnum++;
> +		pr_debug("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
> +			phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
> +			ehdr->e_phnum, phdr->p_offset);
> +		phdr++;
> +	}
> +
> +	*addr = buf;
> +	*sz = elf_sz;
> +	return 0;
> +}
> +
>  Elf_Word *append_elf_note(Elf_Word *buf, char *name, unsigned int type,
>  			  void *data, size_t data_len)
>  {
> diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
> index 1d546dc97c50..8017eeb43036 100644
> --- a/kernel/kexec_file.c
> +++ b/kernel/kexec_file.c
> @@ -1217,102 +1217,3 @@ int crash_exclude_mem_range(struct crash_mem *mem,
>  	mem->nr_ranges++;
>  	return 0;
>  }
> -
> -int crash_prepare_elf64_headers(struct crash_mem *mem, int need_kernel_map,
> -			  void **addr, unsigned long *sz)
> -{
> -	Elf64_Ehdr *ehdr;
> -	Elf64_Phdr *phdr;
> -	unsigned long nr_cpus = num_possible_cpus(), nr_phdr, elf_sz;
> -	unsigned char *buf;
> -	unsigned int cpu, i;
> -	unsigned long long notes_addr;
> -	unsigned long mstart, mend;
> -
> -	/* extra phdr for vmcoreinfo ELF note */
> -	nr_phdr = nr_cpus + 1;
> -	nr_phdr += mem->nr_ranges;
> -
> -	/*
> -	 * kexec-tools creates an extra PT_LOAD phdr for kernel text mapping
> -	 * area (for example, ffffffff80000000 - ffffffffa0000000 on x86_64).
> -	 * I think this is required by tools like gdb. So same physical
> -	 * memory will be mapped in two ELF headers. One will contain kernel
> -	 * text virtual addresses and other will have __va(physical) addresses.
> -	 */
> -
> -	nr_phdr++;
> -	elf_sz = sizeof(Elf64_Ehdr) + nr_phdr * sizeof(Elf64_Phdr);
> -	elf_sz = ALIGN(elf_sz, ELF_CORE_HEADER_ALIGN);
> -
> -	buf = vzalloc(elf_sz);
> -	if (!buf)
> -		return -ENOMEM;
> -
> -	ehdr = (Elf64_Ehdr *)buf;
> -	phdr = (Elf64_Phdr *)(ehdr + 1);
> -	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
> -	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
> -	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
> -	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
> -	ehdr->e_ident[EI_OSABI] = ELF_OSABI;
> -	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
> -	ehdr->e_type = ET_CORE;
> -	ehdr->e_machine = ELF_ARCH;
> -	ehdr->e_version = EV_CURRENT;
> -	ehdr->e_phoff = sizeof(Elf64_Ehdr);
> -	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
> -	ehdr->e_phentsize = sizeof(Elf64_Phdr);
> -
> -	/* Prepare one phdr of type PT_NOTE for each present CPU */
> -	for_each_present_cpu(cpu) {
> -		phdr->p_type = PT_NOTE;
> -		notes_addr = per_cpu_ptr_to_phys(per_cpu_ptr(crash_notes, cpu));
> -		phdr->p_offset = phdr->p_paddr = notes_addr;
> -		phdr->p_filesz = phdr->p_memsz = sizeof(note_buf_t);
> -		(ehdr->e_phnum)++;
> -		phdr++;
> -	}
> -
> -	/* Prepare one PT_NOTE header for vmcoreinfo */
> -	phdr->p_type = PT_NOTE;
> -	phdr->p_offset = phdr->p_paddr = paddr_vmcoreinfo_note();
> -	phdr->p_filesz = phdr->p_memsz = VMCOREINFO_NOTE_SIZE;
> -	(ehdr->e_phnum)++;
> -	phdr++;
> -
> -	/* Prepare PT_LOAD type program header for kernel text region */
> -	if (need_kernel_map) {
> -		phdr->p_type = PT_LOAD;
> -		phdr->p_flags = PF_R|PF_W|PF_X;
> -		phdr->p_vaddr = (unsigned long) _text;
> -		phdr->p_filesz = phdr->p_memsz = _end - _text;
> -		phdr->p_offset = phdr->p_paddr = __pa_symbol(_text);
> -		ehdr->e_phnum++;
> -		phdr++;
> -	}
> -
> -	/* Go through all the ranges in mem->ranges[] and prepare phdr */
> -	for (i = 0; i < mem->nr_ranges; i++) {
> -		mstart = mem->ranges[i].start;
> -		mend = mem->ranges[i].end;
> -
> -		phdr->p_type = PT_LOAD;
> -		phdr->p_flags = PF_R|PF_W|PF_X;
> -		phdr->p_offset  = mstart;
> -
> -		phdr->p_paddr = mstart;
> -		phdr->p_vaddr = (unsigned long) __va(mstart);
> -		phdr->p_filesz = phdr->p_memsz = mend - mstart + 1;
> -		phdr->p_align = 0;
> -		ehdr->e_phnum++;
> -		pr_debug("Crash PT_LOAD ELF header. phdr=%p vaddr=0x%llx, paddr=0x%llx, sz=0x%llx e_phnum=%d p_offset=0x%llx\n",
> -			phdr, phdr->p_vaddr, phdr->p_paddr, phdr->p_filesz,
> -			ehdr->e_phnum, phdr->p_offset);
> -		phdr++;
> -	}
> -
> -	*addr = buf;
> -	*sz = elf_sz;
> -	return 0;
> -}
> -- 
> 2.31.1
> 


  reply	other threads:[~2022-08-31  2:51 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-26 17:36 [PATCH v11 0/7] crash: Kernel handling of CPU and memory hot un/plug Eric DeVolder
2022-08-26 17:36 ` [PATCH v11 1/7] crash: move crash_prepare_elf64_headers Eric DeVolder
2022-08-31  2:50   ` Baoquan He [this message]
2022-08-31 17:20     ` Eric DeVolder
2022-08-26 17:36 ` [PATCH v11 2/7] crash: prototype change for crash_prepare_elf64_headers Eric DeVolder
2022-08-26 17:37 ` [PATCH v11 3/7] crash: add generic infrastructure for crash hotplug support Eric DeVolder
2022-08-31  3:26   ` Baoquan He
2022-08-31 17:24     ` Eric DeVolder
2022-09-09 21:28     ` Eric DeVolder
2022-08-26 17:37 ` [PATCH v11 4/7] kexec: exclude elfcorehdr from the segment digest Eric DeVolder
2022-08-26 17:37 ` [PATCH v11 5/7] kexec: exclude hot remove cpu from elfcorehdr notes Eric DeVolder
2022-08-26 17:37 ` [PATCH v11 6/7] crash: memory and cpu hotplug sysfs attributes Eric DeVolder
2022-08-26 17:37 ` [PATCH v11 7/7] x86/crash: Add x86 crash hotplug support Eric DeVolder
2022-09-05  3:19   ` Baoquan He
2022-09-06 15:37     ` Eric DeVolder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yw7MhU957+cPQ50/@MiWiFi-R3L-srv \
    --to=bhe@redhat.com \
    --cc=boris.ostrovsky@oracle.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=david@redhat.com \
    --cc=dyoung@redhat.com \
    --cc=ebiederm@xmission.com \
    --cc=efault@gmx.de \
    --cc=eric.devolder@oracle.com \
    --cc=hpa@zytor.com \
    --cc=kexec@lists.infradead.org \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=nramas@linux.microsoft.com \
    --cc=robh@kernel.org \
    --cc=rppt@kernel.org \
    --cc=sourabhjain@linux.ibm.com \
    --cc=tglx@linutronix.de \
    --cc=thomas.lendacky@amd.com \
    --cc=vgoyal@redhat.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox