From 55b0562f00d36a7ca36f126f38a89588a6106930 Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Tue, 13 Sep 2022 19:27:14 +0800 Subject: [PATCH] libbpf-tools: Allow softirqs to run on old kernels This is part of efforts towards #4231. Fallback to raw tracepoints if tp_btf is not available. Signed-off-by: Hengqi Chen --- libbpf-tools/softirqs.bpf.c | 39 +++++++++++++++++++++++++++---------- libbpf-tools/softirqs.c | 8 ++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/libbpf-tools/softirqs.bpf.c b/libbpf-tools/softirqs.bpf.c index faa009c77ab8..8ce7a539933e 100644 --- a/libbpf-tools/softirqs.bpf.c +++ b/libbpf-tools/softirqs.bpf.c @@ -20,31 +20,26 @@ struct { __u64 counts[NR_SOFTIRQS] = {}; struct hist hists[NR_SOFTIRQS] = {}; -SEC("tp_btf/softirq_entry") -int BPF_PROG(softirq_entry, unsigned int vec_nr) +static int handle_entry(unsigned int vec_nr) { u64 ts = bpf_ktime_get_ns(); u32 key = 0; - bpf_map_update_elem(&start, &key, &ts, 0); + bpf_map_update_elem(&start, &key, &ts, BPF_ANY); return 0; } -SEC("tp_btf/softirq_exit") -int BPF_PROG(softirq_exit, unsigned int vec_nr) +static int handle_exit(unsigned int vec_nr) { + u64 delta, *tsp; u32 key = 0; - s64 delta; - u64 *tsp; if (vec_nr >= NR_SOFTIRQS) return 0; tsp = bpf_map_lookup_elem(&start, &key); - if (!tsp || !*tsp) + if (!tsp) return 0; delta = bpf_ktime_get_ns() - *tsp; - if (delta < 0) - return 0; if (!targ_ns) delta /= 1000U; @@ -64,4 +59,28 @@ int BPF_PROG(softirq_exit, unsigned int vec_nr) return 0; } +SEC("tp_btf/softirq_entry") +int BPF_PROG(softirq_entry_btf, unsigned int vec_nr) +{ + return handle_entry(vec_nr); +} + +SEC("tp_btf/softirq_exit") +int BPF_PROG(softirq_exit_btf, unsigned int vec_nr) +{ + return handle_exit(vec_nr); +} + +SEC("raw_tp/softirq_entry") +int BPF_PROG(softirq_entry, unsigned int vec_nr) +{ + return handle_entry(vec_nr); +} + +SEC("raw_tp/softirq_exit") +int BPF_PROG(softirq_exit, unsigned int vec_nr) +{ + return handle_exit(vec_nr); +} + char LICENSE[] SEC("license") = "GPL"; diff --git a/libbpf-tools/softirqs.c b/libbpf-tools/softirqs.c index 833bc1a5adc7..701784d44dca 100644 --- a/libbpf-tools/softirqs.c +++ b/libbpf-tools/softirqs.c @@ -204,6 +204,14 @@ int main(int argc, char **argv) return 1; } + if (probe_tp_btf("softirq_entry")) { + bpf_program__set_autoload(obj->progs.softirq_entry, false); + bpf_program__set_autoload(obj->progs.softirq_exit, false); + } else { + bpf_program__set_autoload(obj->progs.softirq_entry_btf, false); + bpf_program__set_autoload(obj->progs.softirq_exit_btf, false); + } + /* initialize global data (filtering options) */ obj->rodata->targ_dist = env.distributed; obj->rodata->targ_ns = env.nanoseconds;