From 196bde84896c6d7e0099195b235b3461d804c13a Mon Sep 17 00:00:00 2001 From: ZouTao Date: Wed, 28 Jun 2023 11:20:08 +0800 Subject: [PATCH 1/2] pmubpf: Add support for linux-5.10 Signed-off-by: ZouTao --- source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c b/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c index 861c9b34..78ff522d 100644 --- a/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c +++ b/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c @@ -76,6 +76,9 @@ struct sched_switch_tp_args { char __data[0]; }; +#define PERF_SUBSYS_ID 1 + +#if 0 union kernfs_node_id { struct { u32 ino; @@ -104,7 +107,6 @@ struct kernfs_node___419 { struct kernfs_iattrs *iattr; }; -#define PERF_SUBSYS_ID 1 static u64 get_cgroup_id(struct task_struct *t) { struct cgroup *cgrp; @@ -122,15 +124,15 @@ static u64 get_cgroup_id(struct task_struct *t) return knid; } -#if 0 +#endif + +#if 1 static inline __u64 get_cgroup_id(struct task_struct *t) { struct cgroup *cgrp; cgrp = BPF_CORE_READ(t, cgroups, subsys[PERF_SUBSYS_ID], cgroup); - //cgrp = BPF_CORE_READ(t, cgroups, subsys, cgroup); return BPF_CORE_READ(cgrp, kn, id); - //return PERF_SUBSYS_ID; } #endif -- Gitee From 288d588a97366f826de91c141129117ac4550ce9 Mon Sep 17 00:00:00 2001 From: ZouTao Date: Wed, 28 Jun 2023 13:31:50 +0800 Subject: [PATCH 2/2] pmubpf: Add surpport for both 4.19 and 5.10 Signed-off-by: ZouTao --- .../detect/sched/pmubpf/bpf/pmubpf.bpf.c | 42 ++++++++++++++++- source/tools/detect/sched/pmubpf/pmubpf.c | 47 ++++++++++++++++++- 2 files changed, 86 insertions(+), 3 deletions(-) diff --git a/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c b/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c index 78ff522d..b4f98136 100644 --- a/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c +++ b/source/tools/detect/sched/pmubpf/bpf/pmubpf.bpf.c @@ -78,7 +78,7 @@ struct sched_switch_tp_args { #define PERF_SUBSYS_ID 1 -#if 0 +#if 1 union kernfs_node_id { struct { u32 ino; @@ -107,7 +107,7 @@ struct kernfs_node___419 { struct kernfs_iattrs *iattr; }; -static u64 get_cgroup_id(struct task_struct *t) +static u64 get_cgroup_id_419(struct task_struct *t) { struct cgroup *cgrp; struct kernfs_node___419 *node; @@ -174,6 +174,44 @@ int sysak_pmubpf__sched_switch(struct bpf_raw_tracepoint_args *ctx) return 0; } +SEC("raw_tracepoint/sched_switch") +int sysak_pmubpf__sched_switch_419(struct bpf_raw_tracepoint_args *ctx) +{ + struct cg_key pkey, nkey; + bool preempt = (bool)(ctx->args[0]); + struct task_struct *prev, *next; + u32 cpu = bpf_get_smp_processor_id(); + + __builtin_memset(&pkey, 0, sizeof(struct cg_key)); + __builtin_memset(&nkey, 0, sizeof(struct cg_key)); + prev = (struct task_struct *)(ctx->args[1]); + next = (struct task_struct *)(ctx->args[2]); + + pkey.cgid = get_cgroup_id_419(prev); + nkey.cgid = get_cgroup_id_419(next); + + if (nkey.cgid != pkey.cgid) { /* cgroup changed */ + s64 delta = 0; + u64 *valp, *last, val; + + last = bpf_map_lookup_elem(&cg_schedin, &pkey); + if (last && (*last != 0)) { + val = bpf_perf_event_read(&event, cpu); + delta = val - *last; + valp = bpf_map_lookup_elem(&cg_counter, &pkey); + if (valp) + *valp = *valp + delta; + else + bpf_map_update_elem(&cg_counter, &pkey, &delta, 0); + } + + /* record the start value of new sched_in task */ + val = bpf_perf_event_read(&event, cpu); + bpf_map_update_elem(&cg_schedin, &nkey, &val, 0); + } + return 0; +} + SEC("tp/sched/sched_switch") int handle_switch(struct sched_switch_tp_args *ctx) { diff --git a/source/tools/detect/sched/pmubpf/pmubpf.c b/source/tools/detect/sched/pmubpf/pmubpf.c index 390ee43f..101fa295 100644 --- a/source/tools/detect/sched/pmubpf/pmubpf.c +++ b/source/tools/detect/sched/pmubpf/pmubpf.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "pmubpf.h" #include "bpf/pmubpf.skel.h" @@ -390,6 +392,45 @@ void output_cgroup_counter(void) } } +int check_kprobe(struct pmubpf_bpf *obj) +{ + int i, ret = 0; + char *str, *endptr; + unsigned long ver[4]; + struct utsname ut; + + ret = uname(&ut); + if (ret < 0) + return -errno; + + str = ut.release; + for (i = 0; i < 4; i++) { + errno = 0; + ver[i] = strtoul(str, &endptr, 10); + if ((errno == ERANGE && (ver[i] == LONG_MAX || ver[i] == LONG_MIN)) + || (errno != 0 && ver[i] == 0)) { + perror("strtol"); + return -errno; + } + str = endptr+1; + } + + if (ver[0] >= 5 && ver[1] >= 10) { + ret = bpf_program__set_autoload(obj->progs.sysak_pmubpf__sched_switch_419, false); + if (ret < 0) { + printf("FAIL:bpf_program__set_autoload kp_ttwu_do_wakeup\n"); + return ret; + } + } else { + ret = bpf_program__set_autoload(obj->progs.sysak_pmubpf__sched_switch, false); + if (ret < 0) { + printf("FAIL:bpf_program__set_autoload raw_tp__sched_wakeup\n"); + return ret; + } + } + return 0; +} + int main(int argc, char **argv) { int i, err, arg_fd, cpu; @@ -426,7 +467,11 @@ int main(int argc, char **argv) fprintf(stderr, "failed to open BPF object\n"); return 1; } - + err = check_kprobe(obj); + if (err) { + fprintf(stderr, "failed to filter object: %d\n", err); + goto cleanup; + } err = pmubpf_bpf__load(obj); if (err) { fprintf(stderr, "failed to load BPF object: %d\n", err); -- Gitee