bpf: Adjust BPF stack helper functions to accommodate skip > 0
Namhyung Kim authored
commit ee2a0988 upstream.

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.

Fixes: c195651e

 ("bpf: add bpf_get_stack helper")
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/30a7b5d5-6726-1cc2-eaee-8da2828a9a9c@oracle.com
Link: https://lore.kernel.org/bpf/20220314182042.71025-1-namhyung@kernel.org

Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Based-on-patch-by: default avatarEugene Loh <eugene.loh@oracle.com>
6c4f243b
Name Last commit Last update
..
preload bpf/preload: Clean up .gitignore and "clean-files" target
Kconfig bpf: Disallow unprivileged bpf by default
Makefile bpf: Prepare relo_core.c for kernel duty.
arraymap.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
bloom_filter.c bpf: Add missing map_get_next_key method to bloom filter map.
bpf_inode_storage.c bpf: Allow bpf_local_storage to be used by sleepable programs
bpf_iter.c bpf: Add bpf_loop helper
bpf_local_storage.c bpf: Allow bpf_local_storage to be used by sleepable programs
bpf_lru_list.c bpf_lru_list: Read double-checked variable once without lock
bpf_lru_list.h bpf: Fix a typo "inacitve" -> "inactive"
bpf_lsm.c bpf: Fix renaming task_getsecid_subj->current_getsecid_subj.
bpf_struct_ops.c bpf: Rename btf_member accessors.
bpf_struct_ops_types.h bpf: Add dummy BPF STRUCT_OPS for test purpose
bpf_task_storage.c bpf: Allow bpf_local_storage to be used by sleepable programs
btf.c bpf: Fix a btf decl_tag bug when tagging a function
cgroup.c bpf: Add MEM_RDONLY for helper args that are pointers to rdonly mem.
core.c bpf, docs: Prune all references to "internal BPF"
cpumap.c xdp: Move conversion to xdp_frame out of map functions
devmap.c xdp: Move conversion to xdp_frame out of map functions
disasm.c bpf: Relicense disassembler as GPL-2.0-only OR BSD-2-Clause
disasm.h bpf: Relicense disassembler as GPL-2.0-only OR BSD-2-Clause
dispatcher.c bpf: Remove bpf_image tree
hashtab.c bpf: Replace callers of BPF_CAST_CALL with proper function typedef
helpers.c bpf: Emit bpf_timer in vmlinux BTF
inode.c bpf: Fix mount source show for bpffs
local_storage.c bpf: Use struct_size() helper
lpm_trie.c bpf: Fix typo in a comment in bpf lpm_trie.
map_in_map.c bpf: Remember BTF of inner maps.
map_in_map.h bpf: Add map_meta_equal map ops
map_iter.c bpf: Introduce MEM_RDONLY flag
mmap_unlock_work.h bpf: Introduce helper bpf_find_vma
net_namespace.c net: Add includes masked by netdevice.h including uapi/bpf.h
offload.c bpf, offload: Replace bitwise AND by logical AND in bpf_prog_offload_info_fill
percpu_freelist.c bpf: Use raw_spin_trylock() for pcpu_freelist_push/pop in NMI
percpu_freelist.h bpf: Use raw_spin_trylock() for pcpu_freelist_push/pop in NMI
prog_iter.c bpf: Refactor bpf_iter_reg to have separate seq_info member
queue_stack_maps.c bpf: Eliminate rlimit-based memory accounting for queue_stack_maps maps
reuseport_array.c bpf: Use struct_size() helper
ringbuf.c bpf: Use VM_MAP instead of VM_ALLOC for ringbuf
stackmap.c bpf: Adjust BPF stack helper functions to accommodate skip > 0
syscall.c bpf: Add schedule points in batch ops
sysfs_btf.c bpf: Load and verify kernel module BTFs
task_iter.c bpf: Introduce btf_tracing_ids
tnum.c bpf, tnums: Provably sound, faster, and more precise algorithm for tnum_mul
trampoline.c bpf: Fix possible race in inc_misses_counter
verifier.c bpf: Fix ringbuf memory type confusion when passing to helpers