Skip to content

Commit 6c605f8

Browse files
melverPeter Zijlstra
authored andcommitted
perf: Fix data race between pin_count increment/decrement
KCSAN reports a data race between increment and decrement of pin_count: write to 0xffff888237c2d4e0 of 4 bytes by task 15740 on cpu 1: find_get_context kernel/events/core.c:4617 __do_sys_perf_event_open kernel/events/core.c:12097 [inline] __se_sys_perf_event_open kernel/events/core.c:11933 ... read to 0xffff888237c2d4e0 of 4 bytes by task 15743 on cpu 0: perf_unpin_context kernel/events/core.c:1525 [inline] __do_sys_perf_event_open kernel/events/core.c:12328 [inline] __se_sys_perf_event_open kernel/events/core.c:11933 ... Because neither read-modify-write here is atomic, this can lead to one of the operations being lost, resulting in an inconsistent pin_count. Fix it by adding the missing locking in the CPU-event case. Fixes: fe4b04f ("perf: Cure task_oncpu_function_call() races") Reported-by: [email protected] Signed-off-by: Marco Elver <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 8124c8a commit 6c605f8

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

kernel/events/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4609,7 +4609,9 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
46094609
cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
46104610
ctx = &cpuctx->ctx;
46114611
get_ctx(ctx);
4612+
raw_spin_lock_irqsave(&ctx->lock, flags);
46124613
++ctx->pin_count;
4614+
raw_spin_unlock_irqrestore(&ctx->lock, flags);
46134615

46144616
return ctx;
46154617
}

0 commit comments

Comments
 (0)