libbpf-tools: fix readahead, support v5.10+ kernel
authorWenbo Zhang <ethercflow@gmail.com>
Sat, 6 Feb 2021 10:27:53 +0000 (18:27 +0800)
committeryonghong-song <ys114321@gmail.com>
Sat, 6 Feb 2021 21:56:21 +0000 (13:56 -0800)
Signed-off-by: Wenbo Zhang <ethercflow@gmail.com>
libbpf-tools/readahead.bpf.c
libbpf-tools/readahead.c

index 4f4e5eee93db743e907473814ce5a80cb8eb849d..92b2831e5c15448616a5c233eebfcbc709b5bd46 100644 (file)
@@ -26,8 +26,8 @@ struct {
 
 static struct hist hist;
 
-SEC("fentry/__do_page_cache_readahead")
-int BPF_PROG(do_page_cache_readahead)
+SEC("fentry/do_page_cache_ra")
+int BPF_PROG(do_page_cache_ra)
 {
        u32 pid = bpf_get_current_pid_tgid();
        u64 one = 1;
@@ -53,8 +53,8 @@ int BPF_PROG(page_cache_alloc_ret, gfp_t gfp, struct page *ret)
        return 0;
 }
 
-SEC("fexit/__do_page_cache_readahead")
-int BPF_PROG(do_page_cache_readahead_ret)
+SEC("fexit/do_page_cache_ra")
+int BPF_PROG(do_page_cache_ra_ret)
 {
        u32 pid = bpf_get_current_pid_tgid();
 
index f2460af8bbd3907408a2380bf38f0ee76cac688d..1b29b50f75e4b867ad60c8010a41021cd3e576b0 100644 (file)
@@ -72,6 +72,24 @@ static void sig_handler(int sig)
        exiting = true;
 }
 
+static int readahead__set_attach_target(struct bpf_program *prog)
+{
+       int err;
+
+       err = bpf_program__set_attach_target(prog, 0, "do_page_cache_ra");
+       if (!err)
+               return 0;
+
+       err = bpf_program__set_attach_target(prog, 0,
+                                       "__do_page_cache_readahead");
+       if (!err)
+               return 0;
+
+       fprintf(stderr, "failed to set attach target to %s: %s\n",
+               bpf_program__section_name(prog), strerror(-err));
+       return err;
+}
+
 int main(int argc, char **argv)
 {
        static const struct argp argp = {
@@ -95,12 +113,29 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       obj = readahead_bpf__open_and_load();
+       obj = readahead_bpf__open();
        if (!obj) {
-               fprintf(stderr, "failed to open and/or load BPF ojbect\n");
+               fprintf(stderr, "failed to open BPF object\n");
                return 1;
        }
 
+       /*
+        * Starting from v5.10-rc1 (8238287), __do_page_cache_readahead has
+        * renamed to do_page_cache_ra. So we specify the function dynamically.
+        */
+       err = readahead__set_attach_target(obj->progs.do_page_cache_ra);
+       if (err)
+               goto cleanup;
+       err = readahead__set_attach_target(obj->progs.do_page_cache_ra_ret);
+       if (err)
+               goto cleanup;
+
+       err = readahead_bpf__load(obj);
+       if (err) {
+               fprintf(stderr, "failed to load BPF object\n");
+               goto cleanup;
+       }
+
        err = readahead_bpf__attach(obj);
        if (err) {
                fprintf(stderr, "failed to attach BPF programs\n");