kexec: Use BPF lskel to enable kexec to load PE format boot image
Subsystems (8)
v4 cover kexec: Use BPF lskel to enable kexec to load PE format boot image
*** Review the history ***
Nowadays UEFI PE bootable image is more and more popular on the distribution. But it is still an open issue to load that kind of image by kexec with IMA enabled
There are several approaches to reslove this issue, but none of them are accepted in upstream till now.
The summary of those approaches:
-1. UEFI service emulator for UEFI stub
-2. PE format parser in kernel
For the first one, I have tried a purgatory-style emulator [1]. But it confronts the hardware scaling trouble. For the second one, there are two choices, one is to implement it inside the kernel, the other is inside the user space. Both zboot-format [2] and UKI-format [3] parsers are rejected due to the concern that the variant format parsers will inflate the kernel code. And finally, we have these kinds of parsers in the user space 'kexec-tools'.
*** The approach in this series ***
This approach allows the various PE boot image to be parsed in the bpf-prog, as a result, the kexec kernel code to remain relatively stable.
Benefits
And it abstracts architecture independent part and the API is limitted
To protect against malicious attacks on the BPF loader in user space, it employs BPF lskel to load and execute BPF programs from within the kernel.
Each type of PE image contains a dedicated section '.bpf', which stores the bpf-prog designed to parse the format. This ensures that the PE's signature also protects the integrity of the '.bpf' section.
The parsing process operates as a pipeline. The current BPF program parser attaches to bpf_handle_pefile() and detaches at the end of the current stage via disarm_bpf_prog(). The results parsed by the current BPF program are buffered in the kernel through prepare_nested_pe() and then delivered to the next stage. For each stage of the pipeline, the BPF bytecode is stored in the '.bpf' section of the PE file. That means a vmlinuz.efi embeded in UKI format can be handled.
Special thanks to Philipp Rudo, who spent significant time evaluating the practicality of my solution, and to Viktor Malik, who guided me toward using BPF light skeleton to prevent malicious attacks from user space.
*** Test result ***
Configured with RHEL kernel debug file, which turns on most of locking, memory debug option, I have not seen any warning or bug for 1000 times.
Test approach:
-1. compile kernel
-2. get the zboot image with bpf-prog by 'make -C tools/kexec zboot' -3. compile kexec-tools from https://github.com/pfliu/kexec-tools/pull/new/pe_bpf
The rest process is the common convention to use kexec.
[1]: https://lore.kernel.org/lkml/[email protected]/T/
[2]: https://lore.kernel.org/kexec/[email protected]/
[3]: https://lore.kernel.org/lkml/[email protected]/
[4]: https://lore.kernel.org/linux-arm-kernel/[email protected]/T/
v3 -> v4
- Use dynamic allocator in decompression ([4/12])
- Fix issue caused by Identical Code Folding ([5/12])
- Integrate the image generator tool in the kernel tree ([11,12/12])
- Address the issue according to Philipp's comments in v3 reviewing. Thanks Philipp!
RFCv2 -> v3
- move the introduced bpf kfuncs to kernel/bpf/* and mark them sleepable
- use listener and publisher model to implement bpf_copy_to_kernel()
- keep each introduced kfunc under the control of memcg
RFCv1 -> RFCv2
- Use bpf kfunc instead of helper
- Use C source code to generate the light skeleton file
Pingfan Liu (12):
kexec_file: Make kexec_image_load_default global visible lib/decompress: Keep decompressor when CONFIG_KEXEC_PE_IMAGE
bpf: Introduce bpf_copy_to_kernel() to buffer the content from
bpf-prog
bpf: Introduce decompressor kfunc
kexec: Introduce kexec_pe_image to parse and load PE file
kexec: Integrate with the introduced bpf kfuncs
kexec: Introduce a bpf-prog lskel to parse PE file
kexec: Factor out routine to find a symbol in ELF
kexec: Integrate bpf light skeleton to load zboot image arm64/kexec: Add PE image format support tools/kexec: Introduce a bpf-prog to parse zboot image format tools/kexec: Add a zboot image building tool
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kexec.h | 1 + arch/arm64/kernel/machine_kexec_file.c | 3 + include/linux/bpf.h | 39 ++ include/linux/decompress/mm.h | 7 + include/linux/kexec.h | 6 + kernel/Kconfig.kexec | 8 + kernel/Makefile | 2 + kernel/bpf/Makefile | 2 +- kernel/bpf/helpers.c | 225 +++++++++ kernel/bpf/helpers_carrier.c | 211 +++++++++ kernel/kexec_bpf/Makefile | 71 +++ kernel/kexec_bpf/kexec_pe_parser_bpf.c | 67 +++ kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h | 147 ++++++ kernel/kexec_file.c | 88 ++-- kernel/kexec_pe_image.c | 463 +++++++++++++++++++ lib/decompress.c | 6 +- tools/kexec/Makefile | 90 ++++ tools/kexec/pe.h | 177 +++++++ tools/kexec/zboot_image_builder.c | 280 +++++++++++ tools/kexec/zboot_parser_bpf.c | 158 +++++++ 21 files changed, 2007 insertions(+), 45 deletions(-) create mode 100644 kernel/bpf/helpers_carrier.c create mode 100644 kernel/kexec_bpf/Makefile create mode 100644 kernel/kexec_bpf/kexec_pe_parser_bpf.c create mode 100644 kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h create mode 100644 kernel/kexec_pe_image.c create mode 100644 tools/kexec/Makefile create mode 100644 tools/kexec/pe.h create mode 100644 tools/kexec/zboot_image_builder.c create mode 100644 tools/kexec/zboot_parser_bpf.c
Signature · 1 line
2.49.0
PATCH v4 1/12 kexec_file: Make kexec_image_load_default global visible
In latter patches, PE format parser will extract the linux kernel inside and try its real format parser. So making kexec_image_load_default global.
--- include/linux/kexec.h | 1 + kernel/kexec_file.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
include/linux/kexec.h +1 -0
@@ -152,6 +152,7 @@ extern const struct kexec_file_ops * const kexec_file_loaders[];int kexec_image_probe_default (struct kimage *image,void *buf,unsigned long buf_len); +void *kexec_image_load_default (struct kimage *image);int kexec_image_post_load_cleanup_default (struct kimage *image);/*
kernel/kexec_file.c +1 -1
@@ -79,7 +79,7 @@ int kexec_image_probe_default(struct kimage *image, void *buf,return ret; } -static void *kexec_image_load_default (struct kimage *image) +void *kexec_image_load_default (struct kimage *image) {if (!image->fops || !image->fops->load)return ERR_PTR (-ENOEXEC );
Signature · 1 line
2.49.0
PATCH v4 2/12 lib/decompress: Keep decompressor when CONFIG_KEXEC_PE_IMAGE
In reply to Pingfan LiuThe KEXE PE format parser needs the kernel built-in decompressor to decompress the kernel image. So moving the decompressor out of __init sections.
--- include/linux/decompress/mm.h | 7 +++++++ lib/decompress.c | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/decompress/mm.h +7 -0
@@ -92,7 +92,14 @@ MALLOC_VISIBLE void free(void *where)#define large_malloc (a) vmalloc(a)#define large_free (a) vfree(a) +#ifdef CONFIG_KEXEC_PE_IMAGE +#define INIT +#define INITCONST +#else #define INIT __init +#define INITCONST __initconst +#endif +#define STATIC #include <linux/init.h>
lib/decompress.c +3 -3
@@ -6,7 +6,7 @@*/ #include <linux/decompress/generic.h> - +#include <linux/decompress/mm.h> #include <linux/decompress/bunzip2.h> #include <linux/decompress/unlzma.h> #include <linux/decompress/unxz.h>
@@ -48,7 +48,7 @@ struct compress_format {decompress_fn decompressor; }; -static const struct compress_format compressed_formats[]__initconst = { +static const struct compress_format compressed_formats[] INITCONST = { { {0x1f ,0x8b },"gzip" , gunzip }, { {0x1f ,0x9e },"gzip" , gunzip }, { {0x42 ,0x5a },"bzip2" , bunzip2 },
@@ -60,7 +60,7 @@ static const struct compress_format compressed_formats[] __initconst = { { {0, 0}, NULL, NULL } }; -decompress_fn __init decompress_method (const unsigned char *inbuf,long len, +decompress_fn INIT decompress_method(const unsigned char *inbuf, long len,const char **name) {const struct compress_format *cf;
Signature · 1 line
2.49.0
PATCH v4 3/12 bpf: Introduce bpf_copy_to_kernel() to buffer the content from bpf-prog
In reply to Pingfan LiuIn the security kexec_file_load case, the buffer which holds the kernel image should not be accessible from the userspace.
Typically, BPF data flow occurs between user space and kernel space in either direction. However, kexec_file_load presents a unique case where user-originated data must be parsed and then forwarded to the kernel for subsequent parsing stages. This necessitates a mechanism to channel the intermedia data from the BPF program directly to the kernel.
bpf_kexec_carrier() is introduced to serve that purpose.
--- include/linux/bpf.h | 39 +++++++ kernel/bpf/Makefile | 2 +- kernel/bpf/helpers.c | 2 + kernel/bpf/helpers_carrier.c | 211 +++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 kernel/bpf/helpers_carrier.c
include/linux/bpf.h +39 -0
@@ -3588,4 +3588,43 @@ static inline bool bpf_is_subprog(const struct bpf_prog *prog)return prog->aux->func_idx !=0 ; } +enum alloc_type { +TYPE_KALLOC , +TYPE_VMALLOC , +TYPE_VMAP , +}; + +struct mem_range_result { +struct kref ref; +char *buf; +uint32_t buf_sz; +uint32_t data_sz; +/* kmalloc-ed, vmalloc-ed, or vmap-ed */ +enum alloc_type alloc_type; +/* Valid if vmap-ed */ +struct page **pages; +unsigned int pg_cnt; +int status; +struct mem_cgroup *memcg; +}; + +struct mem_range_result *mem_range_result_alloc (void ); +void mem_range_result_get (struct mem_range_result *r); +void mem_range_result_put (struct mem_range_result *r); + +typedef int (*resource_handler )(const char *name,struct mem_range_result *r); + +struct carrier_listener { +struct hlist_node node; +char *name; +resource_handler handler; +/* +* bpf_copy_to_kernel() knows the size in advance, so vmap-ed is not +* supported. +*/ +enum alloc_type alloc_type; +}; + +int register_carrier_listener (struct carrier_listener *listener); +int unregister_carrier_listener (char *str);#endif /* _LINUX_BPF_H */
kernel/bpf/Makefile +1 -1
@@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o log.o token.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o helpers_carrier.o tnum.o log.o token.o obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o
kernel/bpf/helpers.c +2 -0
@@ -3284,6 +3284,8 @@ BTF_KFUNCS_START(generic_btf_ids)#ifdef CONFIG_CRASH_DUMP BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)#endif +BTF_ID_FLAGS(func, bpf_mem_range_result_put, KF_RELEASE | KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_copy_to_kernel, KF_TRUSTED_ARGS | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_obj_new_impl, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_percpu_obj_new_impl, KF_ACQUIRE | KF_RET_NULL)BTF_ID_FLAGS (func, bpf_obj_drop_impl,KF_RELEASE )
kernel/bpf/helpers_carrier.c new file +211 -0
211 lines not on this page · raw message
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCHv4 03/12] bpf: Introduce bpf_copy_to_kernel() to buffer the content from bpf-prog
kernel test robot noticed the following build errors:
Hi Pingfan,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/net]
[also build test ERROR on bpf/master arm64/for-next/core linus/master v6.16-rc7] [cannot apply to bpf-next/master next-20250722]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pingfan-Liu/kexec_file-Make-kexec_image_load_default-global-visible/20250722-100843 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git net patch link: https://lore.kernel.org/r/20250722020319.5837-4-piliu%40redhat.com patch subject: [PATCHv4 03/12] bpf: Introduce bpf_copy_to_kernel() to buffer the content from bpf-prog config: x86_64-buildonly-randconfig-003-20250722 (https://download.01.org/0day-ci/archive/20250723/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250723/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All error/warnings (new ones prefixed by >>):
kernel/bpf/helpers_carrier.c:84:17: warning: no previous prototype for 'bpf_mem_range_result_put' [-Wmissing-prototypes]
84 | __bpf_kfunc int bpf_mem_range_result_put(struct mem_range_result *result)
| ^~~~~~~~~~~~~~~~~~~~~~~~
kernel/bpf/helpers_carrier.c:93:17: warning: no previous prototype for 'bpf_copy_to_kernel' [-Wmissing-prototypes]
93 | __bpf_kfunc int bpf_copy_to_kernel(const char *name, char *buf, int size)
| ^~~~~~~~~~~~~~~~~~
kernel/bpf/helpers_carrier.c: In function 'bpf_copy_to_kernel':
>> kernel/bpf/helpers_carrier.c:124:9: warning: enumeration value 'TYPE_VMAP' not handled in switch [-Wswitch]
124 | switch (alloc_type) {
| ^~~~~~
--
>> ld: mm/mmu_notifier.o:mm/mmu_notifier.c:25: multiple definition of `__pcpu_unique_srcu_srcu_data'; kernel/bpf/helpers_carrier.o:kernel/bpf/helpers_carrier.c:13: first defined hereSignature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 4/12 bpf: Introduce decompressor kfunc
In reply to Pingfan LiuThis commit bridges the gap between bpf-prog and the kernel decompression routines. At present, only a global memory allocator is used for the decompression. Later, if needed, the decompress_fn's prototype can be changed to pass in a task related allocator.
This memory allocator can allocate 2MB each time with a transient virtual address, up to a 1GB limit. After decompression finishes, it presents all of the decompressed data in a new unified virtual address space.
--- kernel/bpf/helpers.c | 223 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+)
kernel/bpf/helpers.c +223 -0
@@ -24,6 +24,7 @@#include <linux/bpf_mem_alloc.h> #include <linux/kasan.h> #include <linux/bpf_verifier.h> +#include <linux/decompress/generic.h> #include "../../lib/kstrtox.h"
@@ -3278,12 +3279,234 @@ __bpf_kfunc void __bpf_trap(void) { } +#define MAX_UNCOMPRESSED_BUF_SIZE (1 << 28) +/* a chunk should be large enough to contain a decompressing */ +#define CHUNK_SIZE (1 << 23) + +/* +* At present, one global allocator for decompression. Later if needed, changing the +* prototype of decompress_fn to introduce each task's allocator. +*/ +static DEFINE_MUTEX (output_buf_mutex); + +struct decompress_mem_allocator { +struct page **pages; +unsigned int pg_idx; +void *chunk_start; +unsigned int chunk_size; +void *chunk_cur; +}; + +static struct decompress_mem_allocator dcmpr_allocator; + +/* +* Set up an active chunk to hold partial decompressed data. +*/ +static void *vmap_decompressed_chunk (void ) +{ +struct decompress_mem_allocator *a = &dcmpr_allocator; +unsigned int i, pg_cnt = a->chunk_size >>PAGE_SHIFT ; +struct page **pg_start = &a->pages[a->pg_idx]; + +for (i =0 ; i < pg_cnt; i++) + a->pages[a->pg_idx++] =alloc_page (GFP_KERNEL | __GFP_ACCOUNT); + +return vmap (pg_start, pg_cnt,VM_MAP ,PAGE_KERNEL ); +} + +/* +* Present the scattered pages containing decompressed data at a unified virtual +* address. +*/ +static int decompress_mem_allocator_handover (struct decompress_mem_allocator *a, +struct mem_range_result *range) +{ +unsigned long pg_array_sz = a->pg_idx *sizeof (struct page *); + + range->pages =vmalloc (pg_array_sz); +if (!range->pages) +return -ENOMEM ; + + range->pg_cnt = a->pg_idx; +memcpy (range->pages, a->pages, pg_array_sz); + range->buf =vmap (range->pages, range->pg_cnt,VM_MAP ,PAGE_KERNEL ); +if (!range->buf) { +vfree (range->pages); +return -1 ; + } +/* +* Free the tracing pointer; The pages are freed when mem_range_result +* is released. +*/ +vfree (a->pages); + a->pages =NULL ; + +/* vmap-ed */ + range->alloc_type =TYPE_VMAP ; + range->buf_sz = a->pg_idx <<PAGE_SHIFT ; + range->data_sz = range->buf_sz - a->chunk_size; + range->data_sz += a->chunk_cur - a->chunk_start; + +return 0 ; +} + +static int decompress_mem_allocator_init ( +struct decompress_mem_allocator *allocator, +unsigned int chunk_size) +{ +unsigned long sz = (MAX_UNCOMPRESSED_BUF_SIZE >>PAGE_SHIFT ) *sizeof (struct page *); + + allocator->pages =__vmalloc (sz,GFP_KERNEL | __GFP_ACCOUNT); +if (!allocator->pages) +return -ENOMEM ; + + allocator->pg_idx =0 ; + allocator->chunk_start =NULL ; + allocator->chunk_size = chunk_size; + allocator->chunk_cur =NULL ; +return 0 ; +} + +static void decompress_mem_allocator_fini (struct decompress_mem_allocator *allocator) +{ +unsigned int i; + +/* unmap the active chunk */ +if (!!allocator->chunk_start) +vunmap (allocator->chunk_start); +if (!!allocator->pages) { +for (i =0 ; i < allocator->pg_idx; i++) +__free_pages (allocator->pages[i],0 ); +vfree (allocator->pages); + } +} + +/* +* This is a callback for decompress_fn. +* +* It copies the partial decompressed content in [buf, buf + len) to dst. If the +* active chunk is not large enough, retire it and activate a new chunk to hold +* the remaining data. +*/ +static long flush (void *buf,unsigned long len) +{ +struct decompress_mem_allocator *a = &dcmpr_allocator; +long free, copied =0 ; + +/* The first time allocation */ +if (unlikely (!a->chunk_start)) { + a->chunk_start = a->chunk_cur =vmap_decompressed_chunk (); +if (unlikely (!a->chunk_start)) +return -1 ; + } + + free = a->chunk_start + a->chunk_size - a->chunk_cur; +BUG_ON (free <0 ); +if (free < len) { +/* +* If the totoal size exceeds MAX_UNCOMPRESSED_BUF_SIZE, +* return -1 to indicate the decompress method that something +* is wrong +*/ +if (unlikely ((a->pg_idx >=MAX_UNCOMPRESSED_BUF_SIZE >>PAGE_SHIFT ))) +return -1 ; +memcpy (a->chunk_cur, buf, free); + copied += free; + a->chunk_cur += free; + len -= free; +/* +* When retiring the active chunk, release its virtual address +* but do not release the contents in the pages. +*/ +vunmap (a->chunk_start); + a->chunk_start = a->chunk_cur =vmap_decompressed_chunk (); +if (unlikely (!a->chunk_start)) +return -1 ; + } +memcpy (a->chunk_cur, buf, len); + copied += len; + a->chunk_cur += len; +return copied; +} + +__bpf_kfunc struct mem_range_result *bpf_decompress(char *image_gz_payload, int image_gz_sz) +{ +struct decompress_mem_allocator *a = &dcmpr_allocator; +decompress_fn decompressor; +struct mem_cgroup *memcg, *old_memcg; +struct mem_range_result *range; +const char *name; +char *input_buf; +int ret; + + memcg =get_mem_cgroup_from_current (); + old_memcg =set_active_memcg (memcg); + range =mem_range_result_alloc (); +if (!range) { +pr_err ("fail to allocate mem_range_result\n" ); + goto error; + } + + input_buf =__vmalloc (image_gz_sz,GFP_KERNEL | __GFP_ACCOUNT); +if (!input_buf) { +kfree (range); +pr_err ("fail to allocate input buffer\n" ); + goto error; + } + + ret =copy_from_kernel_nofault (input_buf, image_gz_payload, image_gz_sz); +if (ret <0 ) { +kfree (range); +vfree (input_buf); +pr_err ("Error when copying from 0x%p, size:0x%x\n" , + image_gz_payload, image_gz_sz); + goto error; + } + +mutex_lock (&output_buf_mutex); +decompress_mem_allocator_init (a,CHUNK_SIZE ); + decompressor =decompress_method (input_buf, image_gz_sz, &name); +if (!decompressor) { +kfree (range); +vfree (input_buf); +pr_err ("Can not find decompress method\n" ); + goto error; + } + ret =decompressor (input_buf, image_gz_sz,NULL , flush, +NULL ,NULL ,NULL ); + +vfree (input_buf); +if (ret ==0 ) { + ret =decompress_mem_allocator_handover (a, range); +if (!!ret) + goto fail; + range->status =0 ; +mem_cgroup_tryget (memcg); + range->memcg = memcg; +set_active_memcg (old_memcg); + } +fail: +decompress_mem_allocator_fini (a); +mutex_unlock (&output_buf_mutex); +if (!!ret) { +kfree (range); + range =NULL ; +pr_err ("Decompress error\n" ); + } + +error: +set_active_memcg (old_memcg); +mem_cgroup_put (memcg); +return range; +} +__bpf_kfunc_end_defs (); BTF_KFUNCS_START(generic_btf_ids)#ifdef CONFIG_CRASH_DUMP BTF_ID_FLAGS(func, crash_kexec, KF_DESTRUCTIVE)#endif +BTF_ID_FLAGS(func, bpf_decompress, KF_TRUSTED_ARGS | KF_ACQUIRE | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_mem_range_result_put, KF_RELEASE | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_copy_to_kernel, KF_TRUSTED_ARGS | KF_SLEEPABLE)BTF_ID_FLAGS (func, bpf_obj_new_impl,KF_ACQUIRE |KF_RET_NULL )
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCHv4 04/12] bpf: Introduce decompressor kfunc
kernel test robot noticed the following build warnings:
Hi Pingfan,
kernel test robot noticed the following build warnings:
[auto build test WARNING on bpf-next/net]
[also build test WARNING on bpf/master arm64/for-next/core linus/master v6.16-rc7] [cannot apply to bpf-next/master next-20250722]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pingfan-Liu/kexec_file-Make-kexec_image_load_default-global-visible/20250722-100843 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git net patch link: https://lore.kernel.org/r/20250722020319.5837-5-piliu%40redhat.com patch subject: [PATCHv4 04/12] bpf: Introduce decompressor kfunc config: arm-randconfig-004-20250723 (https://download.01.org/0day-ci/archive/20250723/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 853c343b45b3e83cc5eeef5a52fc8cc9d8a09252) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250723/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>, old ones prefixed by <<):
>> WARNING: modpost: vmlinux: section mismatch in reference: bpf_decompress+0xbc (section: .text.bpf_decompress) -> decompress_method (section: .init.text)
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 5/12 kexec: Introduce kexec_pe_image to parse and load PE file
In reply to Pingfan LiuAs UEFI becomes popular, a few architectures support to boot a PE format kernel image directly. But the internal of PE format varies, which means each parser for each format.
This patch (with the rest in this series) introduces a common skeleton to all parsers, and leave the format parsing in
bpf-prog, so the kernel code can keep relative stable.
A new kexec_file_ops is implementation, named pe_image_ops.
There are some place holder function in this patch. (They will take effect after the introduction of kexec bpf light skeleton and bpf helpers). Overall the parsing progress is a pipeline, the current bpf-prog parser is attached to bpf_handle_pefile(), and detatched at the end of the current stage 'disarm_bpf_prog()' the current parsed result by the current bpf-prog will be buffered in kernel 'prepare_nested_pe()' , and deliver to the next stage. For each stage, the bpf bytecode is extracted from the '.bpf' section in the PE file.
--- include/linux/kexec.h | 1 + kernel/Kconfig.kexec | 8 + kernel/Makefile | 1 + kernel/kexec_pe_image.c | 348 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 358 insertions(+) create mode 100644 kernel/kexec_pe_image.c
include/linux/kexec.h +1 -0
@@ -434,6 +434,7 @@ static inline int machine_kexec_post_load(struct kimage *image) { return 0; }extern struct kimage *kexec_image;extern struct kimage *kexec_crash_image; +extern const struct kexec_file_ops pe_image_ops;bool kexec_load_permitted (int kexec_image_type);
kernel/Kconfig.kexec +8 -0
@@ -46,6 +46,14 @@ config KEXEC_FILE for kernel and initramfs as opposed to list of segments as accepted by kexec system call. +config KEXEC_PE_IMAGE + bool "Enable parsing UEFI PE file through kexec file based system call" + depends on KEXEC_FILE + depends on DEBUG_INFO_BTF && BPF_SYSCALL + help + This option makes the kexec_file_load() syscall cooperates with bpf-prog + to parse PE format file + config KEXEC_SIG bool "Verify kernel signature during kexec_file_load() syscall" depends on ARCH_SUPPORTS_KEXEC_SIG
kernel/Makefile +1 -0
@@ -80,6 +80,7 @@ obj-$(CONFIG_CRASH_DUMP) += crash_core.o obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o +obj-$(CONFIG_KEXEC_PE_IMAGE) += kexec_pe_image.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o obj-$(CONFIG_KEXEC_HANDOVER) += kexec_handover.o obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o
kernel/kexec_pe_image.c new file +348 -0
348 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v4 6/12 kexec: Integrate with the introduced bpf kfuncs
In reply to Pingfan LiuThis patch does two things:
First, register as a listener on bpf_copy_to_kernel() Second, in order that the hooked bpf-prog can call the sleepable kfuncs, bpf_handle_pefile and bpf_post_handle_pefile are marked as KF_SLEEPABLE.
--- kernel/kexec_pe_image.c | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
kernel/kexec_pe_image.c +67 -0
@@ -38,6 +38,51 @@ static struct kexec_res parsed_resource[3] = { { KEXEC_RES_CMDLINE_NAME, }, }; +/* +* @name should be one of : kernel, initrd, cmdline +*/ +static int bpf_kexec_carrier (const char *name,struct mem_range_result *r) +{ +struct kexec_res *res; +int i; + +if (!r || !name) +return -EINVAL ; + +for (i =0 ; i <3 ; i++) { +if (!strcmp (parsed_resource[i].name, name)) +break ; + } +if (i >=3 ) +return -EINVAL ; + + res = &parsed_resource[i]; +/* +* Replace the intermediate resource generated by the previous step. +*/ +if (!!res->r) +mem_range_result_put (res->r); +mem_range_result_get (r); + res->r = r; +return 0 ; +} + +static struct carrier_listener kexec_res_listener[3 ] = { + { .name =KEXEC_RES_KERNEL_NAME , + .alloc_type =1 , + .handler = bpf_kexec_carrier, + }, + { .name =KEXEC_RES_INITRD_NAME , + .alloc_type =1 , + .handler = bpf_kexec_carrier, + }, + { .name =KEXEC_RES_CMDLINE_NAME , +/* kmalloc-ed */ + .alloc_type =0 , + .handler = bpf_kexec_carrier, + }, +}; +static bool pe_has_bpf_section (const char *file_buf,unsigned long pe_sz);static bool is_valid_pe (const char *kernel_buf,unsigned long kernel_len)
@@ -159,6 +204,22 @@ __attribute__((used, optimize("O0"))) void bpf_post_handle_pefile(struct kexec_c dummy +=2 ; } +BTF_KFUNCS_START (kexec_modify_return_ids) +BTF_ID_FLAGS (func ,bpf_handle_pefile ,KF_SLEEPABLE ) +BTF_ID_FLAGS (func, bpf_post_handle_pefile,KF_SLEEPABLE ) +BTF_KFUNCS_END (kexec_modify_return_ids) + +static const struct btf_kfunc_id_set kexec_modify_return_set = { + .owner =THIS_MODULE , + .set = &kexec_modify_return_ids, +}; + +static int __init kexec_bpf_prog_run_init (void ) +{ +return register_btf_fmodret_id_set (&kexec_modify_return_set); +} +late_initcall (kexec_bpf_prog_run_init); +/* * PE file may be nested and should be unfold one by one. * Query 'kernel', 'initrd', 'cmdline' in cur_phase, as they are inputs for the
@@ -213,6 +274,9 @@ static void *pe_image_load(struct kimage *image, cmdline_start = cmdline; cmdline_sz = cmdline_len; +for (int i =0 ; i <ARRAY_SIZE (kexec_res_listener); i++) +register_carrier_listener (&kexec_res_listener[i]); +while (is_valid_format (linux_start, linux_sz) &&pe_has_bpf_section (linux_start, linux_sz)) {struct kexec_context context;
@@ -250,6 +314,9 @@ static void *pe_image_load(struct kimage *image,disarm_bpf_prog (); } +for (int i =0 ; i <ARRAY_SIZE (kexec_res_listener); i++) +unregister_carrier_listener (kexec_res_listener[i].name); +/* * image's kernel_buf, initrd_buf, cmdline_buf are set. Now they should * be updated to the new content.
Signature · 1 line
2.49.0
PATCH v4 7/12 kexec: Introduce a bpf-prog lskel to parse PE file
In reply to Pingfan LiuAnalague to kernel/bpf/preload/iterators/Makefile,
this Makefile is not invoked by the Kbuild system. It needs to be invoked manually when kexec_pe_parser_bpf.c is changed so that kexec_pe_parser_bpf.lskel.h can be re-generated by the command "bpftool gen skeleton -L kexec_pe_parser_bpf.o".
kexec_pe_parser_bpf.lskel.h is used directly by the kernel kexec code in later patch. For this patch, there are bpf bytecode contained in opts_data[] and opts_insn[] in kexec_pe_parser_bpf.lskel.h, but in the following patch, they will be removed and only the function API in kexec_pe_parser_bpf.lskel.h left.
As exposed in kexec_pe_parser_bpf.lskel.h, the interface between bpf-prog and the kernel are constituted by:
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")
They are fixed and provided for all kinds of bpf-prog which interacts with the kexec kernel component.
--- kernel/kexec_bpf/Makefile | 63 +++ kernel/kexec_bpf/kexec_pe_parser_bpf.c | 67 +++ kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h | 431 +++++++++++++++++++ 3 files changed, 561 insertions(+) create mode 100644 kernel/kexec_bpf/Makefile create mode 100644 kernel/kexec_bpf/kexec_pe_parser_bpf.c create mode 100644 kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h
kernel/kexec_bpf/Makefile new file +63 -0
@@ -0,0 +1,63 @@ +# SPDX-License-Identifier: GPL-2.0 +OUTPUT := .output +CLANG ?= clang +LLC ?= llc +LLVM_STRIP ?= llvm-strip +DEFAULT_BPFTOOL := $(OUTPUT)/sbin/bpftool +BPFTOOL ?= $(DEFAULT_BPFTOOL) +LIBBPF_SRC := $(abspath ../../tools/lib/bpf) +BPFOBJ := $(OUTPUT)/libbpf.a +BPF_INCLUDE := $(OUTPUT) +INCLUDES := -I$(OUTPUT) -I$(BPF_INCLUDE) -I$(abspath ../../tools/lib) \ + -I$(abspath ../../tools/include/uapi) +CFLAGS := -g -Wall + +srctree := $(patsubst %/kernel/kexec_bpf,%,$(CURDIR)) +VMLINUX = $(srctree)/vmlinux + +abs_out := $(abspath $(OUTPUT)) +ifeq ($(V),1) +Q = +msg = +else +Q = @ +msg = @printf ' %-8s %s%s\n' "$(1)" "$(notdir $(2))" "$(if $(3), $(3))"; +MAKEFLAGS += --no-print-directory +submake_extras := feature_display=0 +endif + +.DELETE_ON_ERROR: + +.PHONY: all clean + +all: kexec_pe_parser_bpf.lskel.h + +clean: + $(call msg,CLEAN) + $(Q)rm -rf $(OUTPUT) kexec_pe_parser_bpf.lskel.h + +kexec_pe_parser_bpf.lskel.h: $(OUTPUT)/kexec_pe_parser_bpf.o | $(BPFTOOL) + $(call msg,GEN-SKEL,$@) + $(Q)$(BPFTOOL) gen skeleton -L $< > $@ + +$(OUTPUT)/vmlinux.h: $(VMLINUX) $(BPFOBJ) | $(OUTPUT) + @$(BPFTOOL) btf dump file $(VMLINUX) format c > $(OUTPUT)/vmlinux.h + + +$(OUTPUT)/kexec_pe_parser_bpf.o: kexec_pe_parser_bpf.c $(BPFOBJ) | $(OUTPUT) + $(call msg,BPF,$@) + $(Q)$(CLANG) -g -O2 -target bpf $(INCLUDES) \ + -c $(filter %.c,$^) -o $@ && \ + $(LLVM_STRIP) -g $@ + +$(OUTPUT): + $(call msg,MKDIR,$@) + $(Q)mkdir -p $(OUTPUT) + +$(BPFOBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPUT) + $(Q)$(MAKE) $(submake_extras) -C $(LIBBPF_SRC) \ + OUTPUT=$(abspath $(dir $@))/ $(abspath $@) + +$(DEFAULT_BPFTOOL): + $(Q)$(MAKE) $(submake_extras) -C ../../tools/bpf/bpftool \ + prefix= OUTPUT=$(abs_out)/ DESTDIR=$(abs_out) install
kernel/kexec_bpf/kexec_pe_parser_bpf.c new file +67 -0
67 lines not on this page · raw message
kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h new file +431 -0
431 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v4 8/12 kexec: Factor out routine to find a symbol in ELF
In reply to Pingfan LiuThe routine to search a symbol in ELF can be shared, so split it out.
--- include/linux/kexec.h | 4 ++ kernel/kexec_file.c | 86 +++++++++++++++++++++++-------------------- 2 files changed, 50 insertions(+), 40 deletions(-)
include/linux/kexec.h +4 -0
@@ -540,6 +540,10 @@ void set_kexec_sig_enforced(void);static inline void set_kexec_sig_enforced (void ) {}#endif +#if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY ) || defined(CONFIG_KEXEC_PE_IMAGE ) +const Elf_Sym *elf_find_symbol (const Elf_Ehdr *ehdr,const char *name); +#endif +#endif /* !defined(__ASSEBMLY__) */ #endif /* LINUX_KEXEC_H */
kernel/kexec_file.c +46 -40
101 lines not on this page · raw message
Signature · 1 line
2.49.0
kernel test robot
build botRe: [PATCHv4 08/12] kexec: Factor out routine to find a symbol in ELF
kernel test robot noticed the following build errors:
Hi Pingfan,
kernel test robot noticed the following build errors:
[auto build test ERROR on bpf-next/net]
[also build test ERROR on bpf/master arm64/for-next/core linus/master v6.16-rc7] [cannot apply to bpf-next/master next-20250722]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pingfan-Liu/kexec_file-Make-kexec_image_load_default-global-visible/20250722-100843 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git net patch link: https://lore.kernel.org/r/20250722020319.5837-9-piliu%40redhat.com patch subject: [PATCHv4 08/12] kexec: Factor out routine to find a symbol in ELF config: x86_64-buildonly-randconfig-006-20250722 (https://download.01.org/0day-ci/archive/20250723/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250723/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from include/linux/crash_dump.h:5,
from drivers/of/fdt.c:11:
>> include/linux/kexec.h:544:7: error: unknown type name 'Elf_Sym'
544 | const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name);
| ^~~~~~~
>> include/linux/kexec.h:544:38: error: unknown type name 'Elf_Ehdr'
544 | const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name);
| ^~~~~~~~vim +/Elf_Sym +544 include/linux/kexec.h
542
543 #if defined(CONFIG_ARCH_SUPPORTS_KEXEC_PURGATORY) || defined(CONFIG_KEXEC_PE_IMAGE)
> 544 const Elf_Sym *elf_find_symbol(const Elf_Ehdr *ehdr, const char *name);
545 #endif
546
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v4 9/12 kexec: Integrate bpf light skeleton to load zboot image
In reply to Pingfan LiuAll 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)
--- kernel/Makefile | 1 + kernel/kexec_bpf/Makefile | 8 + kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h | 292 +------------------ kernel/kexec_pe_image.c | 48 +++ 4 files changed, 61 insertions(+), 288 deletions(-)
kernel/Makefile +1 -0
@@ -141,6 +141,7 @@ obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o CFLAGS_stackleak.o += $(DISABLE_STACKLEAK_PLUGIN) +CFLAGS_kexec_pe_image.o += -I$(srctree)/tools/lib obj-$(CONFIG_GCC_PLUGIN_STACKLEAK) += stackleak.o KASAN_SANITIZE_stackleak.o := n KCSAN_SANITIZE_stackleak.o := n
kernel/kexec_bpf/Makefile +8 -0
@@ -39,6 +39,14 @@ clean: kexec_pe_parser_bpf.lskel.h: $(OUTPUT)/kexec_pe_parser_bpf.o | $(BPFTOOL) $(call msg,GEN-SKEL,$@) $(Q)$(BPFTOOL) gen skeleton -L $< > $@ + @# The following sed commands make opts_data[] and opts_insn[] visible in a file instead of only in a function. + @# And it removes the bytecode + $(Q) sed -i '/static const char opts_data\[\].*=/,/";$$/d' $@ + $(Q) sed -i '/static const char opts_insn\[\].*=/,/";$$/d' $@ + $(Q) sed -i \ + -e 's/opts\.data_sz = sizeof(opts_data) - 1;/opts.data_sz = opts_data_sz;/' \ + -e 's/opts\.insns_sz = sizeof(opts_insn) - 1;/opts.insns_sz = opts_insn_sz;/' $@ + $(Q) sed -i '7i static char *opts_data, *opts_insn;\nstatic unsigned int opts_data_sz, opts_insn_sz;' $@ $(OUTPUT)/vmlinux.h: $(VMLINUX) $(BPFOBJ) | $(OUTPUT) @$(BPFTOOL) btf dump file $(VMLINUX) format c > $(OUTPUT)/vmlinux.h
kernel/kexec_bpf/kexec_pe_parser_bpf.lskel.h +4 -288
307 lines not on this page · raw message
kernel/kexec_pe_image.c +48 -0
74 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v4 10/12 arm64/kexec: Add PE image format support
In reply to Pingfan LiuNow everything is ready for kexec PE image parser. Select it on arm64 for zboot and UKI image support.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/kexec.h | 1 + arch/arm64/kernel/machine_kexec_file.c | 3 +++ 3 files changed, 5 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -1606,6 +1606,7 @@ config ARCH_SELECTS_KEXEC_FILE def_bool y depends on KEXEC_FILE select HAVE_IMA_KEXEC if IMA + select KEXEC_PE_IMAGE config ARCH_SUPPORTS_KEXEC_SIG def_bool y
arch/arm64/include/asm/kexec.h +1 -0
@@ -120,6 +120,7 @@ struct kimage_arch {#ifdef CONFIG_KEXEC_FILE extern const struct kexec_file_ops kexec_image_ops; +extern const struct kexec_file_ops kexec_pe_image_ops;int arch_kimage_file_post_load_cleanup (struct kimage *image);#define arch_kimage_file_post_load_cleanup arch_kimage_file_post_load_cleanup
arch/arm64/kernel/machine_kexec_file.c +3 -0
@@ -24,6 +24,9 @@const struct kexec_file_ops *const kexec_file_loaders[] = { &kexec_image_ops, +#ifdef CONFIG_KEXEC_PE_IMAGE + &kexec_pe_image_ops, +#endifNULL };
Signature · 1 line
2.49.0
Re: [PATCHv4 10/12] arm64/kexec: Add PE image format support
Quoted 7 lines · Pingfan Liu
Now everything is ready for kexec PE image parser. Select it on arm64 for zboot and UKI image support.
Signed-off-by Pingfan Liu <[email protected]>Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>
PATCH v4 11/12 tools/kexec: Introduce a bpf-prog to parse zboot image format
In reply to Pingfan LiuThis BPF program aligns with the convention defined in the kernel file kexec_pe_parser_bpf.lskel.h, where the interface between the BPF program and the kernel is established, and is composed of:
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")This BPF program only uses ringbuf_1, so it minimizes the size of the other three ringbufs to one byte. The size of ringbuf_1 is deduced from the size of the uncompressed file 'vmlinux.bin', which is usually less than 64MB. With the help of a group of bpf kfuncs: bpf_decompress(), bpf_copy_to_kernel(), bpf_mem_range_result_put(), this bpf-prog stores the uncompressed kernel image inside the kernel space.
--- tools/kexec/Makefile | 82 +++++++++++++++++ tools/kexec/zboot_parser_bpf.c | 158 +++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 tools/kexec/Makefile create mode 100644 tools/kexec/zboot_parser_bpf.c
tools/kexec/Makefile new file +82 -0
@@ -0,0 +1,82 @@ +# SPDX-License-Identifier: GPL-2.0 + +# Ensure Kbuild variables are available +include ../scripts/Makefile.include + +srctree := $(patsubst %/tools/kexec,%,$(CURDIR)) +VMLINUX = $(srctree)/vmlinux +TOOLSDIR := $(srctree)/tools +LIBDIR := $(TOOLSDIR)/lib +BPFDIR := $(LIBDIR)/bpf +ARCH ?= $(shell uname -m | sed -e s/i.86/x86/ -e s/x86_64/x86/ -e s/aarch64.*/arm64/ -e s/riscv64/riscv/ -e s/loongarch.*/loongarch/) +# At present, zboot image format is used by arm64, riscv, loongarch +# And arch/$(ARCH)/boot/vmlinux.bin is the uncompressed file instead of arch/$(ARCH)/boot/Image +ifeq ($(ARCH),$(filter $(ARCH),arm64 riscv loongarch)) + EFI_IMAGE := $(srctree)/arch/$(ARCH)/boot/vmlinuz.efi + KERNEL_IMAGE := $(srctree)/arch/$(ARCH)/boot/vmlinux.bin +else + @echo "Unsupported architecture: $(ARCH)" + @exit 1 +endif + + +CC = clang +CFLAGS = -O2 +BPF_PROG_CFLAGS = -g -O2 -target bpf -Wall -I $(BPFDIR) -I . +BPFTOOL = bpftool + +# List of generated target files +HEADERS = vmlinux.h bpf_helper_defs.h image_size.h +ZBOOT_TARGETS = bytecode.c zboot_parser_bpf.o bytecode.o + + +# Targets +zboot: $(HEADERS) $(ZBOOT_TARGETS) + +# Rule to generate vmlinux.h from vmlinux +vmlinux.h: $(VMLINUX) + @command -v $(BPFTOOL) >/dev/null 2>&1 || { echo >&2 "$(BPFTOOL) is required but not found. Please install it."; exit 1; } + @$(BPFTOOL) btf dump file $(VMLINUX) format c > vmlinux.h + +bpf_helper_defs.h: $(srctree)/tools/include/uapi/linux/bpf.h + @$(QUIET_GEN)$(srctree)/scripts/bpf_doc.py --header \ + --file $(srctree)/tools/include/uapi/linux/bpf.h > bpf_helper_defs.h + +image_size.h: $(KERNEL_IMAGE) + @{ \ + if [ ! -f "$(KERNEL_IMAGE)" ]; then \ + echo "Error: File '$(KERNEL_IMAGE)' does not exist"; \ + exit 1; \ + fi; \ + FILE_SIZE=$$(stat -c '%s' "$(KERNEL_IMAGE)" 2>/dev/null); \ + POWER=4096; \ + while [ $$POWER -le $$FILE_SIZE ]; do \ + POWER=$$((POWER * 2)); \ + done; \ + RINGBUF_SIZE=$$POWER; \ + echo "#define RINGBUF1_SIZE $$RINGBUF_SIZE" > $@; \ + echo "#define IMAGE_SIZE $$FILE_SIZE" >> $@; \ + } + + +# Rule to generate zboot_parser_bpf.o, depends on vmlinux.h +zboot_parser_bpf.o: zboot_parser_bpf.c vmlinux.h bpf_helper_defs.h + @$(CC) $(BPF_PROG_CFLAGS) -c zboot_parser_bpf.c -o zboot_parser_bpf.o + +# Generate zboot_parser_bpf.lskel.h using bpftool +# Then, extract the opts_data[] and opts_insn[] arrays and remove 'static' +# keywords to avoid being optimized away. +bytecode.c: zboot_parser_bpf.o + @$(BPFTOOL) gen skeleton -L zboot_parser_bpf.o > zboot_parser_bpf.lskel.h + @sed -n '/static const char opts_data\[\]/,/;/p' zboot_parser_bpf.lskel.h | sed 's/static const/const/' > $@ + @sed -n '/static const char opts_insn\[\]/,/;/p' zboot_parser_bpf.lskel.h | sed 's/static const/const/' >> $@ + @rm -f zboot_parser_bpf.lskel.h + +bytecode.o: bytecode.c + @$(CC) -c $< -o $@ + +# Clean up generated files +clean: + @rm -f $(HEADERS) $(ZBOOT_TARGETS) + +.PHONY: all clean
tools/kexec/zboot_parser_bpf.c new file +158 -0
158 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v4 12/12 tools/kexec: Add a zboot image building tool
In reply to Pingfan LiuThe objcopy binary can append an section into PE file, but it disregards the DOS header. While the zboot format carries important information:
payload offset and size in the DOS header.
In order to keep track and update such information, here introducing a dedicated binary tool to build zboot image. The payload offset is determined by the fact that its offset inside the .data section is unchanged. Hence the offset of .data section in the new PE file plus the payload offset within section renders the offset within the new PE file.
The objcopy binary can append a section to a PE file, but it disregards the DOS header. However, the zboot format carries important information in the DOS header: payload offset and size.
To track this information and append a new PE section, here a dedicated binary tool is introduced to build zboot images. The payload's relative offset within the .data section remains unchanged. Therefore, the .data section offset in the new PE file, plus the payload offset within that section, yields the payload offset within the new PE file.
Finally, the new PE file 'zboot.efi' can be got by the command:
make -C tools/kexec zboot
--- tools/kexec/Makefile | 10 +- tools/kexec/pe.h | 177 +++++++++++++++++++ tools/kexec/zboot_image_builder.c | 280 ++++++++++++++++++++++++++++++ 3 files changed, 466 insertions(+), 1 deletion(-) create mode 100644 tools/kexec/pe.h create mode 100644 tools/kexec/zboot_image_builder.c
tools/kexec/Makefile +9 -1
@@ -27,7 +27,7 @@ BPFTOOL = bpftool # List of generated target files HEADERS = vmlinux.h bpf_helper_defs.h image_size.h -ZBOOT_TARGETS = bytecode.c zboot_parser_bpf.o bytecode.o +ZBOOT_TARGETS = bytecode.c zboot_parser_bpf.o bytecode.o zboot_image_builder zboot.efi # Targets
@@ -75,6 +75,14 @@ bytecode.c: zboot_parser_bpf.o bytecode.o: bytecode.c @$(CC) -c $< -o $@ +# Rule to build zboot_image_builder executable +zboot_image_builder: zboot_image_builder.c + @$(CC) $(CFLAGS) $< -o $@ + +zboot.efi: zboot_image_builder bytecode.o + @chmod +x zboot_image_builder + @./zboot_image_builder $(EFI_IMAGE) bytecode.o $@ + # Clean up generated files clean: @rm -f $(HEADERS) $(ZBOOT_TARGETS)
tools/kexec/pe.h new file +177 -0
177 lines not on this page · raw message
tools/kexec/zboot_image_builder.c new file +280 -0
280 lines not on this page · raw message
Signature · 1 line
2.49.0