Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH] perf/core: mark the lockless accesses to event->state
@ 2026-12-02 18:13 Jiayi Zhang
  2026-12-02 18:29 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-12-02 18:13 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim
  Cc: Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers,
	Adrian Hunter, James Clark, Ravi Bangoria, linux-perf-users,
	linux-kernel, syzbot+d2f5271536cfba9f371d,
	syzbot+ca170c97cd5de8125b23, syzkaller-bugs

perf_event_set_state() changes event->state with WRITE_ONCE() when the
event's task is switched in or out, but perf_poll(), is_event_hup() and
perf_event_release_kernel() read the state with plain loads and without
ctx->lock. KCSAN reports both readers:

  BUG: KCSAN: data-race in event_sched_out / perf_poll

  write (marked) to 0xffff888126916da8 of 4 bytes by task 7004 on cpu 0:
   perf_event_set_state kernel/events/core.c:790 [inline]
   event_sched_out+0x534/0x740 kernel/events/core.c:2506
   ...
  read to 0xffff888126916da8 of 4 bytes by task 7006 on cpu 1:
   perf_poll+0x36/0x200 kernel/events/core.c:6322
   vfs_poll include/linux/poll.h:82 [inline]
   ...

  BUG: KCSAN: data-race in event_sched_in / perf_event_release_kernel

  write (marked) to 0xffff88814dc96928 of 4 bytes by task 20026 on cpu 1:
   perf_event_set_state kernel/events/core.c:790 [inline]
   event_sched_in+0x360/0xc70 kernel/events/core.c:2849
   ...
  read to 0xffff88814dc96928 of 4 bytes by task 20025 on cpu 0:
   perf_event_release_kernel+0x23f/0x550 kernel/events/core.c:5990
   perf_release+0x1f/0x30 kernel/events/core.c:6066
   ...

The races have no functional effect: the readers only compare the
state with PERF_EVENT_STATE_REVOKED or PERF_EVENT_STATE_EXIT, and the
scheduler only moves an event among ACTIVE, INACTIVE, OFF and ERROR,
which all lie above both. The plain loads still let the compiler tear or
reload them, and KCSAN reports them.

Read the state with READ_ONCE() in perf_poll(), is_event_hup() and
perf_event_release_kernel(), as perf_poll() already does for its other
check, and use WRITE_ONCE() for the two stores that do not go through
perf_event_set_state().

Fixes: da916e96e2de ("perf: Make perf_pmu_unregister() useable")
Reported-by: syzbot+d2f5271536cfba9f371d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=d2f5271536cfba9f371d
Reported-by: syzbot+ca170c97cd5de8125b23@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=ca170c97cd5de8125b23
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
Both syzbot reports are in syzbot's moderation queue and were not sent
to the lists; the Closes: links show them.

syzbot has no reproducer for either report. Tested in QEMU (2 CPUs, the
syzbot config, kernel f802b6558eb8) with a test program in which two
threads pinned to different CPUs wake each other over pipes, a
software event (context switches) is attached to each, one thread
polls such an event and two threads open and close events on them in
a loop. KCSAN was switched on at run time with a report filter for the
functions on both sides. Without the patch all 5 instances reported a
race on event->state within about a minute (4 perf_poll, 1
perf_event_release_kernel; the kGym build does not inline
perf_event_set_state(), so the titles name it instead of
event_sched_in/out); with the patch none of 5 did in 10 minutes each.

AI tools assisted with the analysis, the code and the changelog.

 kernel/events/core.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index 4ab3919..74d5666 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -3334,7 +3334,7 @@ out:
 		    event->group_leader == event)
 			goto out;
 
-		event->state = PERF_EVENT_STATE_OFF;
+		WRITE_ONCE(event->state, PERF_EVENT_STATE_OFF);
 	}
 	raw_spin_unlock_irq(&ctx->lock);
 
@@ -5993,10 +5993,10 @@ int perf_event_release_kernel(struct perf_event *event)
 	 * Thus this guarantees that we will in fact observe and kill _ALL_
 	 * child events.
 	 */
-	if (event->state > PERF_EVENT_STATE_REVOKED) {
+	if (READ_ONCE(event->state) > PERF_EVENT_STATE_REVOKED) {
 		perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD);
 	} else {
-		event->state = PERF_EVENT_STATE_DEAD;
+		WRITE_ONCE(event->state, PERF_EVENT_STATE_DEAD);
 	}
 
 	perf_event_ctx_unlock(event, ctx);
@@ -6263,7 +6263,7 @@ static bool is_event_hup(struct perf_event *event)
 {
 	bool no_children;
 
-	if (event->state > PERF_EVENT_STATE_EXIT)
+	if (READ_ONCE(event->state) > PERF_EVENT_STATE_EXIT)
 		return false;
 
 	mutex_lock(&event->child_mutex);
@@ -6325,12 +6325,12 @@ static __poll_t perf_poll(struct file *file, poll_table *wait)
 	struct perf_buffer *rb;
 	__poll_t events = EPOLLHUP;
 
-	if (event->state <= PERF_EVENT_STATE_REVOKED)
+	if (READ_ONCE(event->state) <= PERF_EVENT_STATE_REVOKED)
 		return EPOLLERR;
 
 	poll_wait(file, &event->waitq, wait);
 
-	if (event->state <= PERF_EVENT_STATE_REVOKED)
+	if (READ_ONCE(event->state) <= PERF_EVENT_STATE_REVOKED)
 		return EPOLLERR;
 
 	if (is_event_hup(event))
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02 18:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:13 [PATCH] perf/core: mark the lockless accesses to event->state Jiayi Zhang
2026-12-02 18:29 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox