Skip to content

Commit 6a07057

Browse files
committed
perf lock contention: Check race in tstamp elem creation
When pelem is NULL, it'd create a new entry with zero data. But it might be preempted by IRQ/NMI just before calling bpf_map_update_elem() then there's a chance to call it twice for the same pid. So it'd be better to use BPF_NOEXIST flag and check the return value to prevent the race. Signed-off-by: Namhyung Kim <namhyung@kernel.org> Acked-by: Ian Rogers <irogers@google.com> Cc: Hao Luo <haoluo@google.com> Cc: Song Liu <song@kernel.org> Cc: bpf@vger.kernel.org Link: https://lore.kernel.org/r/20231020204741.1869520-2-namhyung@kernel.org
1 parent d99317f commit 6a07057

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

tools/perf/util/bpf_skel/lock_contention.bpf.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,11 @@ int contention_begin(u64 *ctx)
328328
if (pelem == NULL) {
329329
struct tstamp_data zero = {};
330330

331-
bpf_map_update_elem(&tstamp, &pid, &zero, BPF_ANY);
331+
if (bpf_map_update_elem(&tstamp, &pid, &zero, BPF_NOEXIST) < 0) {
332+
__sync_fetch_and_add(&task_fail, 1);
333+
return 0;
334+
}
335+
332336
pelem = bpf_map_lookup_elem(&tstamp, &pid);
333337
if (pelem == NULL) {
334338
__sync_fetch_and_add(&task_fail, 1);

0 commit comments

Comments
 (0)