Skip to content

Commit e8cecf8

Browse files
haoluo1022Nobody
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. 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 <[email protected]> Signed-off-by: Hao Luo <[email protected]>
1 parent 51c0bbc commit e8cecf8

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

kernel/bpf/stackmap.c

Lines changed: 10 additions & 1 deletion
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
@@ -151,16 +152,24 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs,
151152

152153
for (i = 0; i < trace_nr; i++) {
153154
vma = find_vma(current->mm, ips[i]);
155+
if (vma && vma == prev_vma) {
156+
memcpy(id_offs[i].build_id, prev_build_id,
157+
BUILD_ID_SIZE_MAX);
158+
goto build_id_valid;
159+
}
154160
if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) {
155161
/* per entry fall back to ips */
156162
id_offs[i].status = BPF_STACK_BUILD_ID_IP;
157163
id_offs[i].ip = ips[i];
158164
memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX);
159165
continue;
160166
}
167+
build_id_valid:
161168
id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i]
162169
- vma->vm_start;
163170
id_offs[i].status = BPF_STACK_BUILD_ID_VALID;
171+
prev_vma = vma;
172+
prev_build_id = id_offs[i].build_id;
164173
}
165174
bpf_mmap_unlock_mm(work, current->mm);
166175
}

0 commit comments

Comments
 (0)