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 0000000000000000000000000000000000000000..30050bb381f41cadc6f352eae74d0748bbcc8bb9 --- /dev/null +++ b/backport-libbpf-Adaption-for-sched-type-eBPF-program.patch @@ -0,0 +1,225 @@ +From 5bd4bbb9c8e064a814eb040bcd3d0b06bbd44d11 Mon Sep 17 00:00:00 2001 +From: tianmuyang +Date: Thu, 26 Jun 2025 20:09:30 +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/82c25c3e94148b36df697180ae299e131fdd13b7 +[1] https://gitee.com/openeuler/kernel/commit/f3422f4a201ef5405d6b6bc7ea2c93686adfca6f +[2] https://gitee.com/openeuler/kernel/commit/31cbc3a703b0483dce23ebb1cb9df9ba21b014f0 + +Signed-off-by: tianmuyang +--- + include/uapi/linux/bpf.h | 4 ++++ + src/bpf.c | 2 ++ + src/bpf_helper_defs.h | 41 ++++++++++++++++++++++++++++++++++++++++ + src/libbpf.c | 24 +++++++++++++++++++++-- + src/libbpf.h | 2 ++ + src/libbpf.map | 1 + + 6 files changed, 72 insertions(+), 2 deletions(-) + +diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h +index 0210f85..31bb027 100644 +--- a/include/uapi/linux/bpf.h ++++ b/include/uapi/linux/bpf.h +@@ -951,6 +951,7 @@ enum bpf_prog_type { + BPF_PROG_TYPE_EXT, + BPF_PROG_TYPE_LSM, + BPF_PROG_TYPE_SK_LOOKUP, ++ BPF_PROG_TYPE_SCHED, + BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */ + }; + +@@ -993,6 +994,7 @@ enum bpf_attach_type { + BPF_XDP_CPUMAP, + BPF_SK_LOOKUP, + BPF_XDP, ++ BPF_SCHED, + BPF_SK_SKB_VERDICT, + BPF_SK_REUSEPORT_SELECT, + BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, +@@ -5330,6 +5332,8 @@ union bpf_attr { + FN(per_cpu_ptr), \ + FN(this_cpu_ptr), \ + FN(redirect_peer), \ ++ FN(nodemask_op), \ ++ FN(sched_set_task_prefer_cpumask), \ + FN(task_storage_get), \ + FN(task_storage_delete), \ + FN(get_current_task_btf), \ +diff --git a/src/bpf.c b/src/bpf.c +index 4677644..6f12d02 100644 +--- a/src/bpf.c ++++ b/src/bpf.c +@@ -484,6 +484,7 @@ static int bpf_load_program_xattr2(const struct bpf_load_program_attr *load_attr + break; + case BPF_PROG_TYPE_TRACING: + case BPF_PROG_TYPE_EXT: ++ case BPF_PROG_TYPE_SCHED: + p.attach_btf_id = load_attr->attach_btf_id; + p.attach_prog_fd = load_attr->attach_prog_fd; + break; +@@ -919,6 +920,7 @@ proceed: + case BPF_TRACE_FENTRY: + case BPF_TRACE_FEXIT: + case BPF_MODIFY_RETURN: ++ case BPF_SCHED: + return bpf_raw_tracepoint_open(NULL, prog_fd); + default: + return libbpf_err(err); +diff --git a/src/bpf_helper_defs.h b/src/bpf_helper_defs.h +index c8b3b33..05729b3 100644 +--- a/src/bpf_helper_defs.h ++++ b/src/bpf_helper_defs.h +@@ -3679,3 +3679,44 @@ static __u64 (*bpf_get_sockops_uid_gid)(void *sockops) = (void *) 156; + */ + static int (*bpf_sk_original_addr)(void *bpf_socket, int optname, char *optval, int optlen) = (void *) 157; + ++/* ++ * bpf_nodemask_op ++ * ++ * A series of nodemask-related operations. Perform different ++ * operations base on *op*->type. User also need fill other ++ * *op* field base on *op*->type. *op*->type is one of them ++ * ++ * **NODEMASK_EMPTY** ++ * nodes_empty(op->arg1) returned. ++ * **NODEMASK_NODE_ISSET** ++ * node_isset(op->arg1, op->arg2) returned ++ * **NODEMASK_NODES_CLEAR** ++ * 0 returned ++ * **NODEMASK_NODE_CLEAR** ++ * unset op->arg1 from op->arg2, 0 returned ++ * **NODEMASK_NODE_SET** ++ * set op->arg1 to op->arg2, 0 returned ++ * **NODEMASK_WEIGHT** ++ * nodes_weight(op->arg1) returned ++ * **NODEMASK_NODELIST_PARSE** ++ * str *op->arg1* to nodemask_t *op->arg2*, ++ * 0 on success, or a negative error in case of failure. ++ * **NODEMASK_TO_CPUMASK** ++ * nodemask_t *arg1* to cpumask_t *op->arg2*, 0 returned. ++ * **NODEMASK_ONLINE** ++ * set online nodes to nodemask_t *op->arg1*, 0 returned. ++ * ++ * Returns ++ * View above. ++ */ ++static int (*bpf_nodemask_op)(struct nodemask_op_args *op, int len) = (void *) 170; ++ ++/* ++ * bpf_sched_set_task_prefer_cpumask ++ * ++ * set prefer cpumask for the task. ++ * ++ * Returns ++ * 0 on success, or a negative error in case of failure. ++ */ ++static int (*bpf_sched_set_task_prefer_cpumask)(struct task_struct *tsk, struct cpumask *mask, int len) = (void *) 175; +diff --git a/src/libbpf.c b/src/libbpf.c +index ef7f302..fa71acc 100644 +--- a/src/libbpf.c ++++ b/src/libbpf.c +@@ -2929,7 +2929,8 @@ static int bpf_object__finalize_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 +@@ -7312,7 +7313,8 @@ static int bpf_object_init_progs(struct bpf_object *obj, const struct bpf_object + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wdeprecated-declarations" + if (prog->sec_def->prog_type == BPF_PROG_TYPE_TRACING || +- prog->sec_def->prog_type == BPF_PROG_TYPE_EXT) ++ prog->sec_def->prog_type == BPF_PROG_TYPE_EXT || ++ prog->sec_def->prog_type == BPF_PROG_TYPE_SCHED) + prog->attach_prog_fd = OPTS_GET(opts, attach_prog_fd, 0); + #pragma GCC diagnostic pop + +@@ -8998,6 +9000,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 | SEC_SLOPPY_PFX), +@@ -9078,6 +9081,7 @@ static const struct bpf_sec_def section_defs[] = { + SEC_DEF("cgroup/setsockopt", CGROUP_SOCKOPT, BPF_CGROUP_SETSOCKOPT, SEC_ATTACHABLE | SEC_SLOPPY_PFX), + SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE), + SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE | SEC_SLOPPY_PFX), ++ SEC_DEF("sched/", SCHED, BPF_SCHED, SEC_ATTACH_BTF, attach_sched), + }; + + static size_t custom_sec_def_cnt; +@@ -9439,6 +9443,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, +@@ -9457,6 +9462,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; +@@ -11888,6 +11897,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 9e9a3fd..4841646 100644 +--- a/src/libbpf.h ++++ b/src/libbpf.h +@@ -646,6 +646,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 6b36f46..393dafe 100644 +--- a/src/libbpf.map ++++ b/src/libbpf.map +@@ -337,6 +337,7 @@ LIBBPF_0.2.0 { + perf_buffer__epoll_fd; + perf_buffer__consume_buffer; + xsk_socket__create_shared; ++ bpf_program__attach_sched; + } LIBBPF_0.1.0; + + LIBBPF_0.3.0 { +-- +2.33.0 + diff --git a/libbpf.spec b/libbpf.spec index e9614b317e27eedc602f2f3db847d972996805bc..f14346f0ed3add9cfb45d9d595d9aabf5d544c1a 100644 --- a/libbpf.spec +++ b/libbpf.spec @@ -4,7 +4,7 @@ Name: %{githubname} Version: %{githubver} -Release: 18 +Release: 19 Summary: Libbpf library License: LGPLv2 or BSD @@ -54,6 +54,7 @@ Patch0037: backport-libbpf-Fix-segfault-due-to-libelf-functions-not-sett.patch Patch0038: backport-libbpf-Fixed-getting-wrong-return-address-on-arm64-a.patch Patch0039: backport-libbpf-fix-sym_is_subprog-logic-for-weak-global-subp.patch Patch0040: backport-libbpf-move-global-data-mmap-ing-into-bpf_object__lo.patch +Patch0041: 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 @@ -106,6 +107,10 @@ developing applications that use %{name} %{_libdir}/libbpf.a %changelog +* Mon Jun 23 2025 tianmuyang 2:0.8.1-19 +- Adaption for sched type eBPF program: + backport-libbpf-Adaption-for-sched-type-eBPF-program.patch + * Fri Mar 14 2025 zhangmingyi 2:0.8.1-18 - backport patch from upstream: backport-libbpf-Do-not-resolve-size-on-duplicate-FUNCs.patch