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

Expose kprobe's maxactive parameter in bcc #2224

Merged
merged 1 commit into from
Feb 21, 2019

Conversation

pchaigno
Copy link
Contributor

When a kretprobe is installed on a kernel function, there is a limit on how many parallel calls it can catch. You can change that limit with the maxactive parameter. Since commit 696ced4 ("tracing/kprobes: expose maxactive for kretprobe in kprobe_events") in the Linux kernel, debugfs exposes that parameter.

This pull request exposes that parameter in bcc as well, through bpf_attach_kprobe in libbcc and BPF.attach_kretprobe at the Python level. If a maxactive value is given, we fallback to using debugfs to install the kprobe event, instead of the usual perf_event_open.

For kernels without maxactive supports in debugfs, the error resolution is not straightforward. The kernel will still install a probe, but under a different name. We therefore need to check for our expected name, delete the probe with the different name, and try to attach the probe again, without the maxactive value.

Fixes #1072 #1250

/cc @alban @anisse

When a kretprobe is installed on a kernel function, there is a limit on
how many parallel calls it can catch.  You can change that limit with the
maxactive parameter.  Since commit 696ced4 ("tracing/kprobes: expose
maxactive for kretprobe in kprobe_events") in the Linux kernel, debugfs
exposes that parameter.

This commit exposes that parameter in bcc as well, through
bpf_attach_kprobe in libbcc and BPF.attach_kretprobe at the Python level.
If a maxactive value is given, we fallback to using debugfs to install the
kprobe event, instead of the usual perf_event_open.

For kernels without maxactive supports in debugfs, the error resolution is
not straightforward.  The kernel will still install a probe, but under a
different name.  We therefore need to check for our expected name, delete
the probe with the different name, and try to attach the probe again,
without the maxactive value.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
@trollgithubaccount
Copy link

[buildbot, test this please]

1 similar comment
@yonghong-song
Copy link
Collaborator

[buildbot, test this please]

Copy link
Member

@palmtenor palmtenor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late in the party:) I just have some minor questions

else if (maxactive > 0 && attach_type == BPF_PROBE_RETURN)
snprintf(buf, buf_size, "r%d:kprobes/%s %s",
maxactive, ev_alias, fn_name);
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does maxactive do anything in k(entry)probe? If not, maybe we should check that the value should be 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maxactive doesn't do anything for kprobes. Do you mean check that the value is 0 at the beginning of bpf_attach_kprobe?

strerror(errno));
return -1;
}
snprintf(fname, sizeof(fname), "-:kprobes/%s_0", ev_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to re-use detach kprobe logic here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered it, but bpf_detach_probe also contains some code to handle the case where we attached the probe using perf_event_open (it iterates on the list of events). We could break bpf_detach_probe down to refactor, but I want to try and add support for maxactive in perf_event_open, so I thought I'd refactor after that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to bug a closed issue.

But where is the documentation for maxactive as referred in "See the kprobes documentation for its default value."?

We run into issue similar to #1920 where bpf_trace_printk() states that the perf_submit() succeeded, but user-space did not read the events from the perf buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value is discussed in the kprobes documentation in the kernel:

If maxactive <= 0, it is
set to a default value. If CONFIG_PREEMPT is enabled, the default
is max(10, 2*NR_CPUS). Otherwise, the default is NR_CPUS.

close(kfd);

// Re-creating kprobe event without maxactive.
if (create_kprobe_event(buf, ev_name, attach_type, fn_name,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... ideally here we should go back and try use Perf Event based approach?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not needed. pref event based approach is supported in 4.17 and maxactive is supported in 4.12...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I missed that. I should've called bpf_attach_kprobe with maxactive=0 instead. I could refactor if you think it's worth it.

Also, I might be missing something, but is there any reason the perf_event_open API is preferred to the debugfs'?

schu added a commit to kinvolk/gobpf that referenced this pull request Mar 8, 2019
schu pushed a commit to kinvolk/gobpf that referenced this pull request Mar 11, 2019
mmarchini added a commit to mmarchini/bpftrace that referenced this pull request Mar 13, 2019
bcc changed the signature of bpf_attach_kprobe to accept a new argument:
`maxactive`, which determines the maximum limit of how many calls in
parallel a kretprobe attached to a kernel function can catch. The new
signature is not backwards compatible, thus our build broke. Since
there's no way to feature detect the existance of an argument in a
function using cmake, we use a function pointer with the long signature
and cast bpf_attach_kprobe to it. If we're on an older bcc version, the
last argument will be allocated in a register (according to the
platform's calling convention), but should be ignored inside the
function.

Ref: torvalds/linux@696ced4
Ref: iovisor/bcc#2224
Fixes: bpftrace#453
arighi pushed a commit to arighi/bcc that referenced this pull request Mar 21, 2019
When a kretprobe is installed on a kernel function, there is a limit on
how many parallel calls it can catch.  You can change that limit with the
maxactive parameter.  Since commit 696ced4 ("tracing/kprobes: expose
maxactive for kretprobe in kprobe_events") in the Linux kernel, debugfs
exposes that parameter.

This commit exposes that parameter in bcc as well, through
bpf_attach_kprobe in libbcc and BPF.attach_kretprobe at the Python level.
If a maxactive value is given, we fallback to using debugfs to install the
kprobe event, instead of the usual perf_event_open.

For kernels without maxactive supports in debugfs, the error resolution is
not straightforward.  The kernel will still install a probe, but under a
different name.  We therefore need to check for our expected name, delete
the probe with the different name, and try to attach the probe again,
without the maxactive value.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
palexster pushed a commit to palexster/bcc that referenced this pull request Jul 7, 2019
When a kretprobe is installed on a kernel function, there is a limit on
how many parallel calls it can catch.  You can change that limit with the
maxactive parameter.  Since commit 696ced4 ("tracing/kprobes: expose
maxactive for kretprobe in kprobe_events") in the Linux kernel, debugfs
exposes that parameter.

This commit exposes that parameter in bcc as well, through
bpf_attach_kprobe in libbcc and BPF.attach_kretprobe at the Python level.
If a maxactive value is given, we fallback to using debugfs to install the
kprobe event, instead of the usual perf_event_open.

For kernels without maxactive supports in debugfs, the error resolution is
not straightforward.  The kernel will still install a probe, but under a
different name.  We therefore need to check for our expected name, delete
the probe with the different name, and try to attach the probe again,
without the maxactive value.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
CrackerCat pushed a commit to CrackerCat/bcc that referenced this pull request Jul 31, 2024
When a kretprobe is installed on a kernel function, there is a limit on
how many parallel calls it can catch.  You can change that limit with the
maxactive parameter.  Since commit 696ced4 ("tracing/kprobes: expose
maxactive for kretprobe in kprobe_events") in the Linux kernel, debugfs
exposes that parameter.

This commit exposes that parameter in bcc as well, through
bpf_attach_kprobe in libbcc and BPF.attach_kretprobe at the Python level.
If a maxactive value is given, we fallback to using debugfs to install the
kprobe event, instead of the usual perf_event_open.

For kernels without maxactive supports in debugfs, the error resolution is
not straightforward.  The kernel will still install a probe, but under a
different name.  We therefore need to check for our expected name, delete
the probe with the different name, and try to attach the probe again,
without the maxactive value.

Signed-off-by: Paul Chaignon <paul.chaignon@orange.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants