Skip to content

Commit ceac059

Browse files
haoluo1022borkmann
authored andcommitted
bpf: Cache the last valid build_id
For binaries that are statically linked, consecutive stack frames are likely to be in the same VMA and therefore have the same build id. On a real-world workload, we observed that 66% of CPU cycles in __bpf_get_stackid() were spent on build_id_parse() and find_vma(). As an optimization for this case, we can cache the previous frame's VMA, if the new frame has the same VMA as the previous one, reuse the previous one's build id. We are holding the MM locks as reader across the entire loop, so we don't need to worry about VMA going away. Tested through "stacktrace_build_id" and "stacktrace_build_id_nmi" in test_progs. Suggested-by: Greg Thelen <gthelen@google.com> Signed-off-by: Hao Luo <haoluo@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com> Acked-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Song Liu <songliubraving@fb.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Link: https://lore.kernel.org/bpf/20220224000531.1265030-1-haoluo@google.com
1 parent a4fbfdd commit ceac059

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

kernel/bpf/stackmap.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
132132
int i;
133133
struct mmap_unlock_irq_work *work = NULL;
134134
bool irq_work_busy = bpf_mmap_unlock_get_irq_work(&work);
135-
struct vm_area_struct *vma;
135+
struct vm_area_struct *vma, *prev_vma = NULL;
136+
const char *prev_build_id;
136137

137138
/* If the irq_work is in use, fall back to report ips. Same
138139
* fallback is used for kernel stack (!user) on a stackmap with
@@ -150,6 +151,12 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
150151
}
151152

152153
for (i = 0; i < trace_nr; i++) {
154+
if (range_in_vma(prev_vma, ips[i], ips[i])) {
155+
vma = prev_vma;
156+
memcpy(id_offs[i].build_id, prev_build_id,
157+
BUILD_ID_SIZE_MAX);
158+
goto build_id_valid;
159+
}
153160
vma = find_vma(current->mm, ips[i]);
154161
if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) {
155162
/* per entry fall back to ips */
@@ -158,9 +165,12 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
158165
memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
159166
continue;
160167
}
168+
build_id_valid:
161169
id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
162170
- vma->vm_start;
163171
id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
172+
prev_vma = vma;
173+
prev_build_id = id_offs[i].build_id;
164174
}
165175
bpf_mmap_unlock_mm(work, current->mm);
166176
}

0 commit comments

Comments
 (0)