Re: [PATCHv5 06/12] kexec: Integrate with the introduced bpf kfuncs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Pingfan,


On Tue, 19 Aug 2025 09:24:22 +0800
Pingfan Liu <piliu@xxxxxxxxxx> wrote:

> This 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.
> 
> Signed-off-by: Pingfan Liu <piliu@xxxxxxxxxx>
> Cc: Alexei Starovoitov <ast@xxxxxxxxxx>
> Cc: Philipp Rudo <prudo@xxxxxxxxxx>
> Cc: Baoquan He <bhe@xxxxxxxxxx>
> Cc: Dave Young <dyoung@xxxxxxxxxx>
> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> Cc: bpf@xxxxxxxxxxxxxxx
> To: kexec@xxxxxxxxxxxxxxxxxxx
> ---
>  kernel/kexec_pe_image.c | 67 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 67 insertions(+)
> 
> diff --git a/kernel/kexec_pe_image.c b/kernel/kexec_pe_image.c
> index b0cf9942e68d2..f8debcde6b516 100644
> --- a/kernel/kexec_pe_image.c
> +++ b/kernel/kexec_pe_image.c
> @@ -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;

Can you please replace the magic '3' by ARRAY_SIZE, just like you did
below when (un-)registering the listener.

Thanks
Philipp

> +
> +	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.





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux