libbpf-tools: fix cpufreq.bpf.c and update cpufreq for libbpf 1.0
authorAndrii Nakryiko <andrii@kernel.org>
Fri, 17 Dec 2021 22:20:32 +0000 (14:20 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 20 Dec 2021 21:21:26 +0000 (13:21 -0800)
Switch to libbpf 1.0 mode and adapt libbpf API usage accordingly.

Also fix cachestat.bpf.c by adding a BPF assembly trick to ensure that
BPF verifier sees proper value bounds for cpu ID.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
libbpf-tools/cpufreq.bpf.c
libbpf-tools/cpufreq.c

index 697620bac4caea8324f058c22e3696007033cb9e..88a1bd255c0d346e6103960f429abe4558aa7ddf 100644 (file)
@@ -17,11 +17,21 @@ struct {
        __type(value, struct hist);
 } hists SEC(".maps");
 
+#define clamp_umax(VAR, UMAX)                                          \
+       asm volatile (                                                  \
+               "if %0 <= %[max] goto +1\n"                             \
+               "%0 = %[max]\n"                                         \
+               : "+r"(VAR)                                             \
+               : [max]"i"(UMAX)                                        \
+       )
+
 SEC("tp_btf/cpu_frequency")
 int BPF_PROG(cpu_frequency, unsigned int state, unsigned int cpu_id)
 {
        if (cpu_id >= MAX_CPU_NR)
                return 0;
+
+       clamp_umax(cpu_id, MAX_CPU_NR - 1);
        freqs_mhz[cpu_id] = state / 1000;
        return 0;
 }
@@ -36,6 +46,7 @@ int do_sample(struct bpf_perf_event_data *ctx)
 
        if (cpu >= MAX_CPU_NR)
                return 0;
+       clamp_umax(cpu, MAX_CPU_NR - 1);
        freq_mhz = freqs_mhz[cpu];
        if (!freq_mhz)
                return 0;
index b12d2b78b0978e7614b9b38013c8e71151ca3c6f..e42bca948e7bea600f579d70afac0defe6f278bb 100644 (file)
@@ -102,10 +102,8 @@ static int open_and_attach_perf_event(int freq, struct bpf_program *prog,
                        return -1;
                }
                links[i] = bpf_program__attach_perf_event(prog, fd);
-               if (libbpf_get_error(links[i])) {
-                       fprintf(stderr, "failed to attach perf event on cpu: "
-                               "%d\n", i);
-                       links[i] = NULL;
+               if (!links[i]) {
+                       fprintf(stderr, "failed to attach perf event on cpu: %d\n", i);
                        close(fd);
                        return -1;
                }
@@ -176,7 +174,7 @@ static void print_linear_hists(struct bpf_map *hists,
 
        printf("\n");
        print_linear_hist(bss->syswide.slots, MAX_SLOTS, 0, HIST_STEP_SIZE,
-                       "syswide");
+                         "syswide");
 }
 
 int main(int argc, char **argv)
@@ -194,14 +192,9 @@ int main(int argc, char **argv)
        if (err)
                return err;
 
+       libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
        libbpf_set_print(libbpf_print_fn);
 
-       err = bump_memlock_rlimit();
-       if (err) {
-               fprintf(stderr, "failed to increase rlimit: %d\n", err);
-               return 1;
-       }
-
        nr_cpus = libbpf_num_possible_cpus();
        if (nr_cpus < 0) {
                fprintf(stderr, "failed to get # of possible cpus: '%s'!\n",