1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/init.h>
4 #include <linux/module.h>
7 #include <linux/sched/signal.h>
8 #include "bpf_preload.h"
10 extern char bpf_preload_umd_start;
11 extern char bpf_preload_umd_end;
13 static int preload(struct bpf_preload_info *obj);
14 static int finish(void);
16 static struct bpf_preload_ops umd_ops = {
17 .info.driver_name = "bpf_preload",
23 static int preload(struct bpf_preload_info *obj)
25 int magic = BPF_PRELOAD_START;
30 err = fork_usermode_driver(&umd_ops.info);
34 /* send the start magic to let UMD proceed with loading BPF progs */
35 n = kernel_write(umd_ops.info.pipe_to_umh,
36 &magic, sizeof(magic), &pos);
37 if (n != sizeof(magic))
40 /* receive bpf_link IDs and names from UMD */
42 for (i = 0; i < BPF_PRELOAD_LINKS; i++) {
43 n = kernel_read(umd_ops.info.pipe_from_umh,
44 &obj[i], sizeof(*obj), &pos);
45 if (n != sizeof(*obj))
51 static int finish(void)
53 int magic = BPF_PRELOAD_END;
58 /* send the last magic to UMD. It will do a normal exit. */
59 n = kernel_write(umd_ops.info.pipe_to_umh,
60 &magic, sizeof(magic), &pos);
61 if (n != sizeof(magic))
63 tgid = umd_ops.info.tgid;
64 wait_event(tgid->wait_pidfd, thread_group_exited(tgid));
65 umd_ops.info.tgid = NULL;
69 static int __init load_umd(void)
73 err = umd_load_blob(&umd_ops.info, &bpf_preload_umd_start,
74 &bpf_preload_umd_end - &bpf_preload_umd_start);
77 bpf_preload_ops = &umd_ops;
81 static void __exit fini_umd(void)
83 bpf_preload_ops = NULL;
84 /* kill UMD in case it's still there due to earlier error */
85 kill_pid(umd_ops.info.tgid, SIGKILL, 1);
86 umd_ops.info.tgid = NULL;
87 umd_unload_blob(&umd_ops.info);
89 late_initcall(load_umd);
90 module_exit(fini_umd);
91 MODULE_LICENSE("GPL");