Skip to content

Commit

Permalink
libbpf-tools: Allow biosnoop to run on old kernels
Browse files Browse the repository at this point in the history
Fallback to use kprobes if kernel lacks support for fentry/fexit.

Signed-off-by: mickey_zhu <[email protected]>
  • Loading branch information
michael-chuh committed May 19, 2023
1 parent 46a125d commit bc45d7a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
29 changes: 28 additions & 1 deletion libbpf-tools/biosnoop.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,35 @@ int BPF_PROG(blk_account_io_start, struct request *rq)
return trace_pid(rq);
}

SEC("fentry/blk_account_io_merge_bio")
int BPF_PROG(blk_account_io_merge_bio, struct request *rq)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

return trace_pid(rq);
}

SEC("kprobe/blk_account_io_start")
int BPF_KPROBE(kprobe_blk_account_io_start, struct request *rq)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

return trace_pid(rq);
}

SEC("kprobe/__blk_account_io_start")
int BPF_KPROBE(kprobe___blk_account_io_start, struct request *rq)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;

return trace_pid(rq);
}

SEC("kprobe/blk_account_io_merge_bio")
int BPF_KPROBE(blk_account_io_merge_bio, struct request *rq)
int BPF_KPROBE(kprobe_blk_account_io_merge_bio, struct request *rq)
{
if (filter_cg && !bpf_current_task_under_cgroup(&cgroup_map, 0))
return 0;
Expand Down
80 changes: 29 additions & 51 deletions libbpf-tools/biosnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,42 @@ int main(int argc, char **argv)
obj->rodata->targ_queued = env.queued;
obj->rodata->filter_cg = env.cg;

if (fentry_can_attach("blk_account_io_start", NULL))
bpf_program__set_attach_target(obj->progs.blk_account_io_start, 0,
"blk_account_io_start");
else
bpf_program__set_attach_target(obj->progs.blk_account_io_start, 0,
"__blk_account_io_start");
if (fentry_can_attach("blk_account_io_merge_bio", NULL)) {
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_merge_bio, false);
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_start, false);
bpf_program__set_autoload(obj->progs.kprobe___blk_account_io_start, false);

if (fentry_can_attach("blk_account_io_start", NULL))
bpf_program__set_attach_target(obj->progs.blk_account_io_start, 0,
"blk_account_io_start");
else
bpf_program__set_attach_target(obj->progs.blk_account_io_start, 0,
"__blk_account_io_start");
} else {
bpf_program__set_autoload(obj->progs.blk_account_io_merge_bio, false);
bpf_program__set_autoload(obj->progs.blk_account_io_start, false);

if (!kprobe_exists("blk_account_io_start"))
bpf_program__set_autoload(obj->progs.kprobe_blk_account_io_start, false);
else
bpf_program__set_autoload(obj->progs.kprobe___blk_account_io_start, false);
}

if (!env.queued)
bpf_program__set_autoload(obj->progs.block_rq_insert, false);

err = biosnoop_bpf__load(obj);
if (err) {
fprintf(stderr, "failed to load BPF object: %d\n", err);
goto cleanup;
}

err = biosnoop_bpf__attach(obj);
if (err) {
fprintf(stderr, "failed to attach BPF programs\n");
goto cleanup;
}

/* update cgroup path fd to map */
if (env.cg) {
idx = 0;
Expand All @@ -257,51 +280,6 @@ int main(int argc, char **argv)
}
}

obj->links.blk_account_io_start = bpf_program__attach(obj->progs.blk_account_io_start);
if (!obj->links.blk_account_io_start) {
err = -errno;
fprintf(stderr, "failed to attach blk_account_io_start: %s\n",
strerror(-err));
goto cleanup;
}
ksyms = ksyms__load();
if (!ksyms) {
err = -ENOMEM;
fprintf(stderr, "failed to load kallsyms\n");
goto cleanup;
}
if (ksyms__get_symbol(ksyms, "blk_account_io_merge_bio")) {
obj->links.blk_account_io_merge_bio =
bpf_program__attach(obj->progs.blk_account_io_merge_bio);
if (!obj->links.blk_account_io_merge_bio) {
err = -errno;
fprintf(stderr, "failed to attach blk_account_io_merge_bio: %s\n",
strerror(-err));
goto cleanup;
}
}
if (env.queued) {
obj->links.block_rq_insert =
bpf_program__attach(obj->progs.block_rq_insert);
if (!obj->links.block_rq_insert) {
err = -errno;
fprintf(stderr, "failed to attach block_rq_insert: %s\n", strerror(-err));
goto cleanup;
}
}
obj->links.block_rq_issue = bpf_program__attach(obj->progs.block_rq_issue);
if (!obj->links.block_rq_issue) {
err = -errno;
fprintf(stderr, "failed to attach block_rq_issue: %s\n", strerror(-err));
goto cleanup;
}
obj->links.block_rq_complete = bpf_program__attach(obj->progs.block_rq_complete);
if (!obj->links.block_rq_complete) {
err = -errno;
fprintf(stderr, "failed to attach block_rq_complete: %s\n", strerror(-err));
goto cleanup;
}

pb = perf_buffer__new(bpf_map__fd(obj->maps.events), PERF_BUFFER_PAGES,
handle_event, handle_lost_events, NULL, NULL);
if (!pb) {
Expand Down

0 comments on commit bc45d7a

Please sign in to comment.