path: root/kernel/events/core.c
diff options
authorMarco Elver <>2021-05-27 12:47:11 +0200
committerPeter Zijlstra <>2021-05-31 10:14:51 +0200
commit6c605f8371159432ec61cbb1488dcf7ad24ad19a (patch)
tree7cd23bab66a8989b82dd239bffaf33de229a1e82 /kernel/events/core.c
parent8124c8a6b35386f73523d27eacb71b5364a68c4c (diff)
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: fe4b04fa31a6 ("perf: Cure task_oncpu_function_call() races") Reported-by: Signed-off-by: Marco Elver <> Signed-off-by: Peter Zijlstra (Intel) <> Link:
Diffstat (limited to 'kernel/events/core.c')
1 files changed, 2 insertions, 0 deletions
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 6fee4a7e88d7..fe88d6eea3c2 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -4609,7 +4609,9 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
ctx = &cpuctx->ctx;
+ raw_spin_lock_irqsave(&ctx->lock, flags);
+ raw_spin_unlock_irqrestore(&ctx->lock, flags);
return ctx;