From e62c68b424902f568ff34fce51224dcc3d836005 Mon Sep 17 00:00:00 2001 From: tianmuyang Date: Wed, 2 Jul 2025 10:40:47 +0800 Subject: [PATCH] libbpf: Adaption for sched type eBPF program In order to run eBPF program hooked at "sched", we need to backport some code from openEuler-kernel. Related openEuler-kernel commits: [0] https://gitee.com/openeuler/kernel/commit/f3422f4a201ef5405d6b6bc7ea2c93686adfca6f [1] https://gitee.com/openeuler/kernel/commit/95ef108bb84d9de3331b65f840a4e620e01df9fd Signed-off-by: tianmuyang (cherry picked from commit de141a72ca46a9b6fead7ff68407441644aaaca3) --- ...Adaption-for-sched-type-eBPF-program.patch | 131 ++++++++++++++++++ libbpf.spec | 7 +- 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 backport-libbpf-Adaption-for-sched-type-eBPF-program.patch diff --git a/backport-libbpf-Adaption-for-sched-type-eBPF-program.patch b/backport-libbpf-Adaption-for-sched-type-eBPF-program.patch new file mode 100644 index 0000000..5f6f585 --- /dev/null +++ b/backport-libbpf-Adaption-for-sched-type-eBPF-program.patch @@ -0,0 +1,131 @@ +From 71e55012b764b5340403a49c21eeeca47d27b851 Mon Sep 17 00:00:00 2001 +From: tianmuyang +Date: Mon, 28 Jul 2025 11:41:42 +0800 +Subject: [PATCH] libbpf: Adaption for sched type eBPF program + +In order to run eBPF program hooked at "sched", we need to backport +some code from openEuler-kernel. + +Signed-off-by: tianmuyang +--- + include/uapi/linux/bpf.h | 2 ++ + src/libbpf.c | 21 ++++++++++++++++++++- + src/libbpf.h | 2 ++ + src/libbpf.map | 1 + + 4 files changed, 25 insertions(+), 1 deletion(-) + +diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h +index 4b20a72..b6f4bdb 100644 +--- a/include/uapi/linux/bpf.h ++++ b/include/uapi/linux/bpf.h +@@ -986,6 +986,7 @@ enum bpf_prog_type { + BPF_PROG_TYPE_LSM, + BPF_PROG_TYPE_SK_LOOKUP, + BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ ++ BPF_PROG_TYPE_SCHED = 33, + }; + + enum bpf_attach_type { +@@ -1034,6 +1035,7 @@ enum bpf_attach_type { + BPF_TRACE_KPROBE_MULTI, + BPF_LSM_CGROUP, + BPF_STRUCT_OPS, ++ BPF_SCHED = 49, + __MAX_BPF_ATTACH_TYPE + }; + +diff --git a/src/libbpf.c b/src/libbpf.c +index a39d565..3cc2150 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -2993,7 +2993,8 @@ static int bpf_object_fixup_btf(struct bpf_object *obj) + static bool prog_needs_vmlinux_btf(struct bpf_program *prog) + { + if (prog->type == BPF_PROG_TYPE_STRUCT_OPS || +- prog->type == BPF_PROG_TYPE_LSM) ++ prog->type == BPF_PROG_TYPE_LSM || ++ prog->type == BPF_PROG_TYPE_SCHED) + return true; + + /* BPF_PROG_TYPE_TRACING programs which do not attach to other programs +@@ -8690,6 +8691,7 @@ static int attach_trace(const struct bpf_program *prog, long cookie, struct bpf_ + static int attach_kprobe_multi(const struct bpf_program *prog, long cookie, struct bpf_link **link); + static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_link **link); + static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_link **link); ++static int attach_sched(const struct bpf_program *prog, long cookie, struct bpf_link **link); + + static const struct bpf_sec_def section_defs[] = { + SEC_DEF("socket", SOCKET_FILTER, 0, SEC_NONE), +@@ -8774,6 +8776,7 @@ static const struct bpf_sec_def section_defs[] = { + SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE), + SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE), + SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE), ++ SEC_DEF("sched/", SCHED, BPF_SCHED, SEC_ATTACH_BTF, attach_sched), + }; + + int libbpf_register_prog_handler(const char *sec, +@@ -9155,6 +9158,7 @@ static int bpf_object__collect_st_ops_relos(struct bpf_object *obj, + #define BTF_TRACE_PREFIX "btf_trace_" + #define BTF_LSM_PREFIX "bpf_lsm_" + #define BTF_ITER_PREFIX "bpf_iter_" ++#define BTF_SCHED_PREFIX "bpf_sched_" + #define BTF_MAX_NAME_SIZE 128 + + void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, +@@ -9174,6 +9178,10 @@ void btf_get_kernel_prefix_kind(enum bpf_attach_type attach_type, + *prefix = BTF_ITER_PREFIX; + *kind = BTF_KIND_FUNC; + break; ++ case BPF_SCHED: ++ *prefix = BTF_SCHED_PREFIX; ++ *kind = BTF_KIND_FUNC; ++ break; + default: + *prefix = ""; + *kind = BTF_KIND_FUNC; +@@ -11753,6 +11761,17 @@ static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_l + return libbpf_get_error(*link); + } + ++struct bpf_link *bpf_program__attach_sched(const struct bpf_program *prog) ++{ ++ return bpf_program__attach_btf_id(prog, NULL); ++} ++ ++static int attach_sched(const struct bpf_program *prog, long cookie, struct bpf_link **link) ++{ ++ *link = bpf_program__attach_sched(prog); ++ return libbpf_get_error(*link); ++} ++ + struct bpf_link *bpf_program__attach(const struct bpf_program *prog) + { + struct bpf_link *link = NULL; +diff --git a/src/libbpf.h b/src/libbpf.h +index 0b73623..ea015bf 100644 +--- a/src/libbpf.h ++++ b/src/libbpf.h +@@ -717,6 +717,8 @@ bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex); + LIBBPF_API struct bpf_link * + bpf_program__attach_freplace(const struct bpf_program *prog, + int target_fd, const char *attach_func_name); ++LIBBPF_API struct bpf_link * ++bpf_program__attach_sched(const struct bpf_program *prog); + + struct bpf_map; + +diff --git a/src/libbpf.map b/src/libbpf.map +index a5aa3a3..5e7ce61 100644 +--- a/src/libbpf.map ++++ b/src/libbpf.map +@@ -236,6 +236,7 @@ LIBBPF_0.2.0 { + perf_buffer__buffer_fd; + perf_buffer__epoll_fd; + perf_buffer__consume_buffer; ++ bpf_program__attach_sched; + } LIBBPF_0.1.0; + + LIBBPF_0.3.0 { +-- +2.43.0 + diff --git a/libbpf.spec b/libbpf.spec index 374eb9f..839c84a 100644 --- a/libbpf.spec +++ b/libbpf.spec @@ -4,7 +4,7 @@ Name: %{githubname} Version: %{githubver} -Release: 10 +Release: 11 Summary: Libbpf library License: LGPLv2 or BSD @@ -33,6 +33,7 @@ Patch0016: backport-libbpf-Use-OPTS_SET-macro-in-bpf_xdp_query.patch Patch0017: backport-libbpf-Fix-out-of-bound-read.patch Patch0018: backport-libbpf-fix-LDX_STX_ST-CO-RE-relocation-size-adjustment-logic.patch Patch0019: backport-libbpf-Fix-hypothetical-STT_SECTION-extern-NULL-deref-case.patch +Patch0020: backport-libbpf-Adaption-for-sched-type-eBPF-program.patch # This package supersedes libbpf from kernel-tools, # which has default Epoch: 0. By having Epoch: 1 @@ -85,6 +86,10 @@ developing applications that use %{name} %{_libdir}/libbpf.a %changelog +* Mon Jun 23 2025 tianmuyang 2:1.2.2-11 +- Adaption for sched type eBPF program: + backport-libbpf-Adaption-for-sched-type-eBPF-program.patch + * Mon Jul 14 2025 andy 2:1.2.2-10 - backport patch from upstream: backport-libbpf-Fix-out-of-bound-read.patch -- Gitee