[PATCHv3 8/9] kexec: Integrate bpf light skeleton to load zboot image
Pingfan Liu
piliu at redhat.com
Mon Jun 30 05:40:25 PDT 2025
On Wed, Jun 25, 2025 at 08:10:11PM +0200, Philipp Rudo wrote:
> Hi Pingfan,
>
> On Thu, 29 May 2025 12:17:43 +0800
> Pingfan Liu <piliu at redhat.com> wrote:
>
> > All kexec PE bpf prog should align with the interface exposed by the
> > light skeleton
> > four maps:
> > struct bpf_map_desc ringbuf_1;
> > struct bpf_map_desc ringbuf_2;
> > struct bpf_map_desc ringbuf_3;
> > struct bpf_map_desc ringbuf_4;
> > four sections:
> > struct bpf_map_desc rodata;
> > struct bpf_map_desc data;
> > struct bpf_map_desc bss;
> > struct bpf_map_desc rodata_str1_1;
> > two progs:
> > SEC("fentry.s/bpf_handle_pefile")
> > SEC("fentry.s/bpf_post_handle_pefile")
> >
> > With the above presumption, the integration consists of two parts:
> > -1. Call API exposed by light skeleton from kexec
> > -2. The opts_insn[] and opts_data[] are bpf-prog dependent and
> > can be extracted and passed in from the user space. In the
> > kexec_file_load design, a PE file has a .bpf section, which data
> > content is a ELF, and the ELF contains opts_insn[] opts_data[].
> > As a bonus, BPF bytecode can be placed under the protection of the
> > entire PE signature.
> > (Note, since opts_insn[] contains the information of the ringbuf
> > size, the bpf-prog writer can change its proper size according to
> > the kernel image size without modifying the kernel code)
> >
> > Signed-off-by: Pingfan Liu <piliu at redhat.com>
> > Cc: Alexei Starovoitov <ast at kernel.org>
> > Cc: Baoquan He <bhe at redhat.com>
> > Cc: Dave Young <dyoung at redhat.com>
> > Cc: Andrew Morton <akpm at linux-foundation.org>
> > Cc: Philipp Rudo <prudo at redhat.com>
> > Cc: bpf at vger.kernel.org
> > To: kexec at lists.infradead.org
> > ---
> > kernel/Makefile | 1 +
> > kernel/kexec_bpf/Makefile | 8 +
> > kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h | 292 +------------------
> > kernel/kexec_pe_image.c | 70 +++++
> > 4 files changed, 83 insertions(+), 288 deletions(-)
> >
> [...]
>
> > diff --git a/kernel/kexec_pe_image.c b/kernel/kexec_pe_image.c
> > index e49d6db3c329d..f47c1e46dba97 100644
> > --- a/kernel/kexec_pe_image.c
> > +++ b/kernel/kexec_pe_image.c
> > @@ -13,6 +13,7 @@
> > #include <linux/kernel.h>
> > #include <linux/vmalloc.h>
> > #include <linux/kexec.h>
> > +#include <linux/elf.h>
> > #include <linux/pe.h>
> > #include <linux/string.h>
> > #include <linux/bpf.h>
> > @@ -21,6 +22,7 @@
> > #include <asm/image.h>
> > #include <asm/memory.h>
> >
> > +#include "kexec_bpf/kexec_pe_parser_bpf.lskel.h"
> >
> > static LIST_HEAD(phase_head);
> >
> > @@ -163,14 +165,82 @@ static bool pe_has_bpf_section(char *file_buf, unsigned long pe_sz)
> > return true;
> > }
> >
> > +static struct kexec_pe_parser_bpf *pe_parser;
> > +
> > +static void *get_symbol_from_elf(const char *elf_data, size_t elf_size,
> > + const char *symbol_name, unsigned int *symbol_size)
> > +{
> > + Elf_Ehdr *ehdr = (Elf_Ehdr *)elf_data;
> > + Elf_Shdr *shdr, *symtab_shdr, *strtab_shdr, *dst_shdr;
> > + Elf64_Sym *sym, *symtab = NULL;
> > + char *strtab = NULL;
> > + void *symbol_data = NULL;
> > + int i;
> > +
> > + symtab_shdr = strtab_shdr = NULL;
> > + if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
> > + pr_err("Not a valid ELF file\n");
> > + goto out;
> > + }
> > +
> > + shdr = (struct elf_shdr *)(elf_data + ehdr->e_shoff);
> > + for (i = 0; i < ehdr->e_shnum; i++) {
> > + if (shdr[i].sh_type == SHT_SYMTAB)
> > + symtab_shdr = &shdr[i];
> > + else if (shdr[i].sh_type == SHT_STRTAB && i != ehdr->e_shstrndx)
> > + strtab_shdr = &shdr[i];
> > + }
> > +
> > + if (!symtab_shdr || !strtab_shdr) {
> > + pr_err("Symbol table or string table not found\n");
> > + goto out;
> > + }
> > + symtab = (Elf64_Sym *)(elf_data + symtab_shdr->sh_offset);
> > + strtab = (char *)(elf_data + strtab_shdr->sh_offset);
> > + for (i = 0; i < symtab_shdr->sh_size / sizeof(Elf64_Sym); i++) {
> > + sym = &symtab[i];
> > + if (strcmp(&strtab[sym->st_name], symbol_name) == 0) {
> > + if (sym->st_shndx >= SHN_LORESERVE)
> > + return NULL; // No section data for these
> > + dst_shdr = &shdr[sym->st_shndx];
> > + symbol_data = (void *)(elf_data + dst_shdr->sh_offset + sym->st_value);
> > + *symbol_size = symtab[i].st_size;
> > + break;
> > + }
> > + }
> > +
> > +out:
> > + return symbol_data;
> > +}
>
> In kernel/kexec_file.c there is kexec_purgatory_find_symbol which is
> basically identical to this function. With a little bit of refractoring
> it should work for both cases. I prefer using
> kexec_purgatory_find_symbol as your implementation cannot handle elf
> files with multiple symtab and strtab sections.
>
Thanks for your suggestion. I will refractor the code in next version.
Regards,
Pingfan
> Thanks
> Philipp
>
> > +
> > /* Load a ELF */
> > static int arm_bpf_prog(char *bpf_elf, unsigned long sz)
> > {
> > + opts_data = get_symbol_from_elf(bpf_elf, sz, "opts_data", &opts_data_sz);
> > + opts_insn = get_symbol_from_elf(bpf_elf, sz, "opts_insn", &opts_insn_sz);
> > + if (!opts_data || !opts_insn)
> > + return -1;
> > + /*
> > + * When light skeleton generates opts_data[] and opts_insn[], it appends a
> > + * NULL terminator at the end of string
> > + */
> > + opts_data_sz = opts_data_sz - 1;
> > + opts_insn_sz = opts_insn_sz - 1;
> > +
> > + pe_parser = kexec_pe_parser_bpf__open_and_load();
> > + if (!pe_parser)
> > + return -1;
> > + kexec_pe_parser_bpf__attach(pe_parser);
> > +
> > return 0;
> > }
> >
> > static void disarm_bpf_prog(void)
> > {
> > + kexec_pe_parser_bpf__destroy(pe_parser);
> > + pe_parser = NULL;
> > + opts_data = NULL;
> > + opts_insn = NULL;
> > }
> >
> > struct kexec_context {
>
More information about the kexec
mailing list