public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Henry Zhang <henryzhangjcle@gmail.com>
Cc: mingo@redhat.com, acme@kernel.org,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	syzkaller-bugs@googlegroups.com, Henry Zhang <zeri@umich.edu>,
	syzbot+2a077cb788749964cf68@syzkaller.appspotmail.com,
	andrii@kernel.org
Subject: Re: [PATCH] perf: Fix data race in perf_event_set_bpf_handler()
Date: Fri, 30 Jan 2026 11:23:00 +0100	[thread overview]
Message-ID: <20260130102300.GA171111@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20260127023618.1469937-1-zeri@umich.edu>

On Mon, Jan 26, 2026 at 09:36:18PM -0500, Henry Zhang wrote:
> KCSAN reported a data race where perf_event_set_bpf_handler() writes
> event->prog while __perf_event_overflow() reads it concurrently from
> interrupt context:
> 
> BUG: KCSAN: data-race in __perf_event_overflow / __perf_event_set_bpf_prog
> 
> write to 0xffff88811b219168 of 8 bytes by task 13065 on cpu 0:
>  perf_event_set_bpf_handler kernel/events/core.c:10352 [inline]
>  __perf_event_set_bpf_prog+0x418/0x470 kernel/events/core.c:11303
> ...
> 
> read to 0xffff88811b219168 of 8 bytes by interrupt on cpu 1:
>  __perf_event_overflow+0x252/0x920 kernel/events/core.c:10410
> ...
> 
> Annotate event->prog access with WRITE_ONCE/READ_ONCE.
> 
> Reported-by: syzbot+2a077cb788749964cf68@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=2a077cb788749964cf68
> Signed-off-by: Henry Zhang <zeri@umich.edu>
> ---
>  kernel/events/core.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index a0fa488bce84..1f3ed9e87507 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -10349,7 +10349,7 @@ static inline int perf_event_set_bpf_handler(struct perf_event *event,
>  		return -EPROTO;
>  	}
>  
> -	event->prog = prog;
> +	WRITE_ONCE(event->prog, prog);
>  	event->bpf_cookie = bpf_cookie;

What about that cookie thing? The consumer seems to be a bpf function
(bpf_get_attach_cookie_pe) which can equally run concurrently, no?

Also, there seems to be a coherency issue here, if prog runs, it expects
cookie to be present and all that.

Would that not suggest something like:

	WRITE_ONCE(event->bpf_cookie, bpf_cookie);
	smp_store_release(&event->prog, prog);

>  	return 0;
>  }
> @@ -10407,7 +10407,9 @@ static int __perf_event_overflow(struct perf_event *event,
>  	if (event->attr.aux_pause)
>  		perf_event_aux_pause(event->aux_event, true);
>  
> -	if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT &&
> +	struct bpf_prog *prog = READ_ONCE(event->prog);

smp_load_acquire(&event->prog);

> +
> +	if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT &&
>  	    !bpf_overflow_handler(event, data, regs))
>  		goto out;
>  

Hmm?

  parent reply	other threads:[~2026-01-30 10:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-27  2:36 [PATCH] perf: Fix data race in perf_event_set_bpf_handler() Henry Zhang
2026-01-27  8:37 ` Qing Wang
2026-01-30 10:07   ` Peter Zijlstra
2026-02-24 12:29     ` Peter Zijlstra
2026-01-27 10:36 ` Qing Wang
2026-01-27 21:29   ` Henry Zhang
2026-01-28  2:48     ` Qing Wang
2026-01-30  3:34     ` Qing Wang
2026-01-30 10:23 ` Peter Zijlstra [this message]
2026-01-30 13:48   ` Henry Zhang
2026-01-30 20:32   ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260130102300.GA171111@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=andrii@kernel.org \
    --cc=henryzhangjcle@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=syzbot+2a077cb788749964cf68@syzkaller.appspotmail.com \
    --cc=syzkaller-bugs@googlegroups.com \
    --cc=zeri@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox