From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1B2A286D72 for ; Mon, 30 Jun 2025 12:40:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751287241; cv=none; b=QqDxWqsedmpE0mrqVgRS1V6KndT3QD3RKzmgQRZw/GXEQwVMzbZ8QXWxnhOa3F5/59+bZHRiZrG77wpUmEYPXsoTx9+oKWgEb53Agbl2FpG+IRa6ZQC6G5vlKat/sPfz2PeNS/YVMLp95WjNFnDxojjF/JRE4OiNnwFP7GYZLhE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751287241; c=relaxed/simple; bh=QbnVPlHhJJl4RnvS8LwVefyJnUmerxSE/qMUXNZArcY=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=sl+zrx4P1LHIWWjlSBaJpw4usJa2/1rAaiyJ909rgvq0OgrOv0m9FiB9BJB+Sp1H2GkYKGJsZ2rqnMvejF3CPFDevDWcQObXn55qkkCwqAONqe9KHCTISI7oqY+Z6Z1uZYWQ1b9sjp/ADWyanOCNLMiq9k8jITbPsZlMOaH4aeU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Tidhfrky; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Tidhfrky" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1751287239; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=RSER/kKr09Fwp9Z6FAD98av39gXaDcNtylfht9QBtAY=; b=TidhfrkyEnVvgFJWrFUfIwJMWlAdbLcPNUTCV5H88BPC3/PGa6BpNNNrYgAuu0oCc/Hle0 R81sPJFg34s9N3O0e2I+KwcJK0fXfhdUuWCObiLa5DCKcylkJXl+dmyt20IwiVfHrdEW7F +ckXfgiMU7tV5DOm9oMmAz4qZtLuanQ= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-520-oTGT0iEhMlqriOp8T1-kGQ-1; Mon, 30 Jun 2025 08:40:34 -0400 X-MC-Unique: oTGT0iEhMlqriOp8T1-kGQ-1 X-Mimecast-MFC-AGG-ID: oTGT0iEhMlqriOp8T1-kGQ_1751287232 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1B0681809CBD; Mon, 30 Jun 2025 12:40:31 +0000 (UTC) Received: from localhost (unknown [10.72.112.34]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 71DC91956095; Mon, 30 Jun 2025 12:40:28 +0000 (UTC) Date: Mon, 30 Jun 2025 20:40:25 +0800 From: Pingfan Liu To: Philipp Rudo Cc: kexec@lists.infradead.org, Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , Jeremy Linton , Catalin Marinas , Will Deacon , Ard Biesheuvel , Simon Horman , Gerd Hoffmann , Vitaly Kuznetsov , Viktor Malik , Jan Hendrik Farr , Baoquan He , Dave Young , Andrew Morton , bpf@vger.kernel.org Subject: Re: [PATCHv3 8/9] kexec: Integrate bpf light skeleton to load zboot image Message-ID: References: <20250529041744.16458-1-piliu@redhat.com> <20250529041744.16458-9-piliu@redhat.com> <20250625201011.1eab16a5@rotkaeppchen> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250625201011.1eab16a5@rotkaeppchen> X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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 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 > > Cc: Alexei Starovoitov > > Cc: Baoquan He > > Cc: Dave Young > > Cc: Andrew Morton > > Cc: Philipp Rudo > > Cc: bpf@vger.kernel.org > > To: kexec@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 > > #include > > #include > > +#include > > #include > > #include > > #include > > @@ -21,6 +22,7 @@ > > #include > > #include > > > > +#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 { >