Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf-tools: Allow biosnoop to run on old kernels #4549

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
libbpf-tools: Allow biosnoop to run on old kernels
Fallback to use kprobes if kernel lacks support for fentry/fexit.

Signed-off-by: mickey_zhu <[email protected]>
michael-chuh committed May 19, 2023
commit bc45d7afc0d1cad4312f7de4f7bb0b6fc93b1621
29 changes: 28 additions & 1 deletion libbpf-tools/biosnoop.bpf.c
Original file line number Diff line number Diff line change
@@ -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;
80 changes: 29 additions & 51 deletions libbpf-tools/biosnoop.c
Original file line number Diff line number Diff line change
@@ -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;
@@ -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) {