bpf: Disable preemption in bpf_get_stackid

The get_perf_callchain call needs disabled preemption plus we need
it disabled as long as we access its returned trace entries buffer.

Note the bpf_get_stackid_pe function is executed already with
preemption disabled.

Fixes: d5a3b1f691 ("bpf: introduce BPF_MAP_TYPE_STACK_TRACE")
Reported-by: Tao Chen <chen.dylane@linux.dev>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/bpf/20260803210149.296496-6-jolsa@kernel.org

Closes: https://lore.kernel.org/bpf/20260206090653.1336687-2-chen.dylane@linux.dev/
This commit is contained in:
Jiri Olsa 2026-08-03 23:01:42 +02:00 committed by Andrii Nakryiko
parent 09b3fd6caa
commit 15f1bd8574

View File

@ -632,20 +632,22 @@ BPF_CALL_3(bpf_get_stackid, struct pt_regs *, regs, struct bpf_map *, map,
return -EINVAL;
max_depth = stack_map_calculate_max_depth(map->value_size, elem_size, flags);
trace = get_perf_callchain(regs, kernel, user, max_depth,
false, false, 0);
if (unlikely(!trace))
/* couldn't fetch the stack trace */
return -EFAULT;
scoped_guard(preempt) {
trace = get_perf_callchain(regs, kernel, user, max_depth,
false, false, 0);
if (unlikely(!trace))
/* couldn't fetch the stack trace */
return -EFAULT;
err = stackid_fastpath(&stackid, map, trace, flags);
if (err != -ENOENT)
return err;
err = stackid_fastpath(&stackid, map, trace, flags);
if (err != -ENOENT)
return err;
new_bucket = stackid_new_bucket(&stackid, map);
if (!new_bucket)
return -ENOMEM;
new_bucket = stackid_new_bucket(&stackid, map);
if (!new_bucket)
return -ENOMEM;
}
return stackid_install(&stackid, map, new_bucket, flags);
}