Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / kernel / bpf / preload / bpf_preload_kern.c
blob79c5772465f14f1e64a2b962e684b5dfa5338790
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>
5 #include <linux/pid.h>
6 #include <linux/fs.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",
18 .preload = preload,
19 .finish = finish,
20 .owner = THIS_MODULE,
23 static int preload(struct bpf_preload_info *obj)
25 int magic = BPF_PRELOAD_START;
26 loff_t pos = 0;
27 int i, err;
28 ssize_t n;
30 err = fork_usermode_driver(&umd_ops.info);
31 if (err)
32 return err;
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))
38 return -EPIPE;
40 /* receive bpf_link IDs and names from UMD */
41 pos = 0;
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))
46 return -EPIPE;
48 return 0;
51 static int finish(void)
53 int magic = BPF_PRELOAD_END;
54 struct pid *tgid;
55 loff_t pos = 0;
56 ssize_t n;
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))
62 return -EPIPE;
63 tgid = umd_ops.info.tgid;
64 wait_event(tgid->wait_pidfd, thread_group_exited(tgid));
65 umd_ops.info.tgid = NULL;
66 return 0;
69 static int __init load_umd(void)
71 int err;
73 err = umd_load_blob(&umd_ops.info, &bpf_preload_umd_start,
74 &bpf_preload_umd_end - &bpf_preload_umd_start);
75 if (err)
76 return err;
77 bpf_preload_ops = &umd_ops;
78 return err;
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");