From d5b3d1f8268f40e410040a5d3efc2076ff79abc7 Mon Sep 17 00:00:00 2001 From: Wenbo Zhang Date: Fri, 5 Feb 2021 22:08:06 +0800 Subject: [PATCH] libbpf-tools: fix readahead, support v5.10+ kernel Signed-off-by: Wenbo Zhang --- libbpf-tools/readahead.bpf.c | 8 ++--- libbpf-tools/readahead.c | 62 ++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/libbpf-tools/readahead.bpf.c b/libbpf-tools/readahead.bpf.c index 4f4e5eee93db..c4018ccf89af 100644 --- a/libbpf-tools/readahead.bpf.c +++ b/libbpf-tools/readahead.bpf.c @@ -26,8 +26,8 @@ struct { static struct hist hist; -SEC("fentry/__do_page_cache_readahead") -int BPF_PROG(do_page_cache_readahead) +SEC("kprobe/do_page_cache_ra") +int BPF_KPROBE(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("kretprobe/do_page_cache_ra") +int BPF_KRETPROBE(do_page_cache_ra_ret) { u32 pid = bpf_get_current_pid_tgid(); diff --git a/libbpf-tools/readahead.c b/libbpf-tools/readahead.c index f2460af8bbd3..0534c4f6bed2 100644 --- a/libbpf-tools/readahead.c +++ b/libbpf-tools/readahead.c @@ -97,13 +97,69 @@ int main(int argc, char **argv) obj = readahead_bpf__open_and_load(); if (!obj) { - fprintf(stderr, "failed to open and/or load BPF ojbect\n"); + fprintf(stderr, "failed to open and/or load BPF object\n"); return 1; } - err = readahead_bpf__attach(obj); + /* + * Starting from v5.10-rc1 (8238287), __do_page_cache_readahead has + * renamed to do_page_cache_ra. We try to attach to do_page_cache_ra + * first, and if it fails, we try __do_page_cache_readahead again. + * + * If attach to do_page_cache_ra fails due to non-existence, you will + * see libbpf print related warn information. You can just ignore it. + */ + obj->links.do_page_cache_ra = + bpf_program__attach_kprobe(obj->progs.do_page_cache_ra, + false, + "do_page_cache_ra"); + err = libbpf_get_error(obj->links.do_page_cache_ra); if (err) { - fprintf(stderr, "failed to attach BPF programs\n"); + obj->links.do_page_cache_ra = + bpf_program__attach_kprobe(obj->progs.do_page_cache_ra, + false, + "__do_page_cache_readahead"); + err = libbpf_get_error(obj->links.do_page_cache_ra); + if (err) { + fprintf(stderr, "failed to attach kprobe prog to " + "do_page_cache_ra or __do_page_cache_readahead\n"); + goto cleanup; + } + } + + obj->links.do_page_cache_ra_ret = + bpf_program__attach_kprobe(obj->progs.do_page_cache_ra_ret, + true, + "do_page_cache_ra"); + err = libbpf_get_error(obj->links.do_page_cache_ra_ret); + if (err) { + obj->links.do_page_cache_ra_ret = + bpf_program__attach_kprobe(obj->progs.do_page_cache_ra_ret, + true, + "__do_page_cache_readahead"); + err = libbpf_get_error(obj->links.do_page_cache_ra_ret); + if (err) { + fprintf(stderr, "failed to attach kretprobe prog to " + "do_page_cache_ra or __do_page_cache_readahead\n"); + goto cleanup; + } + } + + obj->links.page_cache_alloc_ret = + bpf_program__attach_trace(obj->progs.page_cache_alloc_ret); + err = libbpf_get_error(obj->links.page_cache_alloc_ret); + if (err) { + fprintf(stderr, "failed to attach to tracing prog to " + "page_dache_alloc\n"); + goto cleanup; + } + + obj->links.mark_page_accessed = + bpf_program__attach_trace(obj->progs.mark_page_accessed); + err = libbpf_get_error(obj->links.mark_page_accessed); + if (err) { + fprintf(stderr, "failed to attach to tracing prog to " + "mark_page_accessed\n"); goto cleanup; }