mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: "mingo@kernel.org" <mingo@kernel.org>,
	"lucas.demarchi@intel.com" <lucas.demarchi@intel.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"willy@infradead.org" <willy@infradead.org>,
	"acme@kernel.org" <acme@kernel.org>,
	"namhyung@kernel.org" <namhyung@kernel.org>,
	"mark.rutland@arm.com" <mark.rutland@arm.com>,
	"alexander.shishkin@linux.intel.com"
	<alexander.shishkin@linux.intel.com>,
	"jolsa@kernel.org" <jolsa@kernel.org>,
	"irogers@google.com" <irogers@google.com>,
	"adrian.hunter@intel.com" <adrian.hunter@intel.com>,
	"kan.liang@linux.intel.com" <kan.liang@linux.intel.com>
Subject: Re: [PATCH 19/19] perf: Make perf_pmu_unregister() useable
Date: Fri, 17 Jan 2025 01:03:16 +0100	[thread overview]
Message-ID: <20250117000316.GB33629@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <bf8ed70f-e250-4319-86f9-9a7bc9aadb05@amd.com>

On Fri, Jan 03, 2025 at 09:54:09AM +0530, Ravi Bangoria wrote:
> Hi Peter,
> 
> Sorry for the delay. Was on vacation.

Yeah, me too :-)

> Both of these are incorrect. They just reduce the race window, doesn't
> actually solve the race. Anyway, I could spot few other races:
> 
> 1) A race between event creation and perf_pmu_unregister(). Any event
>    create code path (perf_event_open(), perf_event_create_kernel_counter()
>    and inherit_event()) allocates event with perf_event_alloc() which adds
>    an event to the pmu->events list. However, the event is still immature,
>    for ex, event->ctx is still NULL. In the mean time, perf_pmu_unregister()
>    finds this event and tries to detach it.
> 
>    perf_event_open()                              perf_pmu_unregister()
>      event = perf_event_alloc()                     pmu_detach_event(event)
>        list_add(&event->pmu_list, &pmu->events);      perf_event_ctx_lock(event)
>        /*                                               perf_event_ctx_lock_nested(ctx)
>         * event->ctx is NULL.                             ctx = READ_ONCE(event->ctx); /* event->ctx is NULL */
>         */                                                if (!refcount_inc_not_zero(&ctx->refcount)) { /* Crash */
>      perf_install_in_context(ctx, event);

Ah, that puts the lie to the guard(srcu) comment there, doesn't it :/

So the intent was for that SRCU section to cover the creation, so that
perf_pmu_unregister() can take out the pmu to avoid creating more
events, then srcu-sync to wait on all in-progress creation and then go
detach everything.

I suppose the simplest thing here is to grow that SRCU section.

> 2) A race with perf_event_release_kernel(). perf_event_release_kernel()
>    prepares a separate "free_list" of all children events under ctx->mutex
>    and event->child_mutex. However, the "free_list" uses the same
>    "event->child_list" for entries. OTOH, perf_pmu_unregister() ultimately
>    calls __perf_remove_from_context() with DETACH_CHILD, which checks if
>    the event being removed is a child event, and if so, it will try to
>    detach the child from parent using list_del_init(&event->child_list);
>    i.e. two code path doing list_del on the same list entry.
> 
>    perf_event_release_kernel()                                        perf_pmu_unregister()
>      /* Move children events to free_list */                            ...
>      list_for_each_entry_safe(child, tmp, &free_list, child_list) {       perf_remove_from_context() /* with DETACH_CHILD */
>        ...                                                                  __perf_remove_from_context()
>        list_del(&child->child_list);                                          perf_child_detach()
>                                                                                 list_del_init(&event->child_list);

Bah, I had figured it was taken care of, because perf_event_exit_event()
has a similar race. I'll try and figure out what to do there.

> 3) A WARN(), not a race. perf_pmu_unregister() increments event->refcount
>    before detaching the event. If perf_pmu_unregister() picks up a child
>    event, perf_event_exit_event() called through perf_pmu_unregister()
>    will try to free it. Since event->refcount would be 2, free_event()
>    will trigger a WARN().
> 
>    perf_pmu_unregister()
>      event = pmu_get_event() /* event->refcount => 2 */
>        ...
>          perf_event_exit_event()
> 	   if (parent_event) { /* true, because `event` is a child */
> 	     free_event(event);
> 	       if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
>                         "unexpected event refcount: %ld; ptr=%p\n",
>                         atomic_long_read(&event->refcount), event))

I'll make that something like:

	if (revoke)
		put_event(event);
	else
		free_event(event);

or so.

> 4) A race with perf_event_set_bpf_prog(). perf_event_set_bpf_prog() might
>    be in process of setting event->prog, where as perf_pmu_unregister(),
>    which internally calls perf_event_free_bpf_prog(), will clear the
>    event->prog pointer.
> 
>    perf_pmu_unregister()                perf_event_set_bpf_prog()
>      ...                                  perf_event_set_bpf_handler()
>        perf_event_free_bpf_prog()           event->prog = prog;
>          event->prog = NULL;
> 
> I've yet to inspect other code paths, so there might be more races.

Weird, that should be serialized by perf_event_ctx_lock(), both
__pmu_detach_event() and _perf_ioctl() are called under that.

Thanks for going over this!

  reply	other threads:[~2025-01-17  0:03 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-04 13:39 [PATCH 00/19] perf: Make perf_pmu_unregister() usable Peter Zijlstra
2024-11-04 13:39 ` [PATCH 01/19] lockdep: Fix might_fault() Peter Zijlstra
2025-03-01 20:06   ` [tip: locking/core] lockdep/mm: Fix might_fault() lockdep check of current->mm->mmap_lock tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 02/19] perf: Fix pmus_lock vs pmus_srcu ordering Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Fix pmus_lock vs. " tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 03/19] perf: Fix perf_pmu_register() vs perf_init_event() Peter Zijlstra
2024-11-04 15:36   ` Uros Bizjak
2024-11-05 12:01     ` Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Fix perf_pmu_register() vs. perf_init_event() tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 04/19] perf: Simplify perf_event_alloc() error path Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Simplify the " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2025-03-06  7:57   ` [PATCH 04/19] perf: Simplify " Lai, Yi
2025-03-06  9:24     ` Ingo Molnar
2025-03-07  3:16       ` Lai, Yi
2025-03-07 11:33         ` Ingo Molnar
2024-11-04 13:39 ` [PATCH 05/19] perf: Simplify perf_pmu_register() " Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Simplify the " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 06/19] perf: Simplify perf_pmu_register() Peter Zijlstra
2024-11-20 13:06   ` Ravi Bangoria
2024-11-20 14:46     ` Peter Zijlstra
2024-11-20 15:53       ` Ravi Bangoria
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2025-03-25 19:47   ` [PATCH 06/19] perf: " Sidhartha Kumar
2024-11-04 13:39 ` [PATCH 07/19] perf: Simplify perf_init_event() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 08/19] perf: Simplify perf_event_alloc() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 09/19] perf: Merge pmu_disable_count into cpu_pmu_context Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Merge struct pmu::pmu_disable_count into struct perf_cpu_pmu_context::pmu_disable_count tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 10/19] perf: Add this_cpc() helper Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 11/19] perf: Detach perf_cpu_pmu_context and pmu lifetimes Peter Zijlstra
2025-03-03 12:29   ` [tip: perf/core] perf/core: Detach 'struct perf_cpu_pmu_context' and 'struct pmu' lifetimes tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 12/19] perf: Introduce perf_free_addr_filters() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 13/19] perf: Robustify perf_event_free_bpf_prog() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/bpf: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 14/19] perf: Simplify perf_mmap() control flow Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: Simplify the " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 15/19] perf: Fix perf_mmap() failure path Peter Zijlstra
2025-03-01 19:17   ` Ingo Molnar
2025-03-03 12:38     ` Lorenzo Stoakes
2025-03-04  8:46   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 16/19] perf: Further simplify perf_mmap() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:57   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 17/19] perf: Remove retry loop from perf_mmap() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 18/19] perf: Lift event->mmap_mutex in perf_mmap() Peter Zijlstra
2025-03-01 20:07   ` [tip: perf/core] perf/core: " tip-bot2 for Peter Zijlstra
2025-03-04  8:56   ` tip-bot2 for Peter Zijlstra
2024-11-04 13:39 ` [PATCH 19/19] perf: Make perf_pmu_unregister() useable Peter Zijlstra
2024-11-05 15:08   ` Liang, Kan
2024-11-05 15:16     ` Peter Zijlstra
2024-11-05 15:25       ` Liang, Kan
2024-11-25  4:10   ` Ravi Bangoria
2024-12-17  9:12     ` Peter Zijlstra
2024-12-17 11:52       ` Peter Zijlstra
2024-12-19  9:33         ` Ravi Bangoria
2024-12-19 10:56           ` Ravi Bangoria
2025-01-03  4:24           ` Ravi Bangoria
2025-01-17  0:03             ` Peter Zijlstra [this message]
2025-01-17  5:20               ` Ravi Bangoria
2025-01-17  8:36                 ` Peter Zijlstra
2025-01-17 13:04               ` Peter Zijlstra
2025-01-17 21:04                 ` Peter Zijlstra
2025-01-20 11:15                   ` Ravi Bangoria
2025-01-03  4:29   ` Ravi Bangoria
2024-12-16 18:02 ` [PATCH 00/19] perf: Make perf_pmu_unregister() usable Lucas De Marchi
2025-03-01 20:00 ` Ingo Molnar
2025-03-03  3:25   ` Ravi Bangoria
2025-03-03  9:16     ` Peter Zijlstra

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=20250117000316.GB33629@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=mark.rutland@arm.com \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=ravi.bangoria@amd.com \
    --cc=willy@infradead.org \
    /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

all inboxes | Powered by JetHome®