Skip to content

Commit

Permalink
libbpf-tools: fix readahead, support v5.10+ kernel
Browse files Browse the repository at this point in the history
Signed-off-by: Wenbo Zhang <ethercflow@gmail.com>
  • Loading branch information
ethercflow committed Feb 5, 2021
1 parent f2f1a36 commit d5b3d1f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
8 changes: 4 additions & 4 deletions libbpf-tools/readahead.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
62 changes: 59 additions & 3 deletions libbpf-tools/readahead.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit d5b3d1f

Please sign in to comment.