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 <pi...@redhat.com> Cc: Alexei Starovoitov <a...@kernel.org> Cc: Philipp Rudo <pr...@redhat.com> Cc: Baoquan He <b...@redhat.com> Cc: Dave Young <dyo...@redhat.com> Cc: Andrew Morton <a...@linux-foundation.org> Cc: b...@vger.kernel.org To: ke...@lists.infradead.org --- 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; + + 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. -- 2.49.0