Re: [PATCH 1/2] bpf: Adjust BPF stack helper functions to accommodate skip > 0

From: Yonghong Song
Date: Thu Mar 10 2022 - 17:55:30 EST




On 3/10/22 12:22 AM, Namhyung Kim wrote:
Let's say that the caller has storage for num_elem stack frames. Then,
the BPF stack helper functions walk the stack for only num_elem frames.
This means that if skip > 0, one keeps only 'num_elem - skip' frames.

This is because it sets init_nr in the perf_callchain_entry to the end
of the buffer to save num_elem entries only. I believe it was because
the perf callchain code unwound the stack frames until it reached the
global max size (sysctl_perf_event_max_stack).

However it now has perf_callchain_entry_ctx.max_stack to limit the
iteration locally. This simplifies the code to handle init_nr in the
BPF callstack entries and removes the confusion with the perf_event's
__PERF_SAMPLE_CALLCHAIN_EARLY which sets init_nr to 0.

Also change the comment on bpf_get_stack() in the header file to be
more explicit what the return value means.

Based-on-patch-by: Eugene Loh <eugene.loh@xxxxxxxxxx>
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>

The change looks good to me. This patch actually fixed a bug
discussed below:


https://lore.kernel.org/bpf/30a7b5d5-6726-1cc2-eaee-8da2828a9a9c@xxxxxxxxxx/

A reference to the above link in the commit message
will be useful for people to understand better with an
example.

Also, the following fixes tag should be added:

Fixes: c195651e565a ("bpf: add bpf_get_stack helper")

Since the bug needs skip > 0 which is seldomly used,
and the current returned stack is still correct although
with less entries, I guess that is why less people
complains.

Anyway, ack the patch:
Acked-by: Yonghong Song <yhs@xxxxxx>


---
include/uapi/linux/bpf.h | 4 +--
kernel/bpf/stackmap.c | 56 +++++++++++++++++-----------------------
2 files changed, 26 insertions(+), 34 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index b0383d371b9a..77f4a022c60c 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -2975,8 +2975,8 @@ union bpf_attr {
*
* # sysctl kernel.perf_event_max_stack=<new value>
* Return
- * A non-negative value equal to or less than *size* on success,
- * or a negative error in case of failure.
+ * The non-negative copied *buf* length equal to or less than
+ * *size* on success, or a negative error in case of failure.
*
* long bpf_skb_load_bytes_relative(const void *skb, u32 offset, void *to, u32 len, u32 start_header)
[...]