mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Lucas De Marchi <lucas.demarchi@intel.com>
Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Umesh Nerlige Ramappa <umesh.nerlige.ramappa@intel.com>,
	Ian Rogers <irogers@google.com>,
	Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Subject: Re: [PATCH 3/5] perf: Add pmu get/put
Date: Mon, 14 Oct 2024 19:32:46 +0200	[thread overview]
Message-ID: <20241014173246.GI16066@noisy.programming.kicks-ass.net> (raw)
In-Reply-To: <20241008183501.1354695-4-lucas.demarchi@intel.com>

On Tue, Oct 08, 2024 at 01:34:59PM -0500, Lucas De Marchi wrote:
> If a pmu is unregistered while there's an active event, perf will still
> access the pmu via event->pmu, even after the event is destroyed. This
> makes it difficult for drivers like i915 that can be unbound from the
> HW.
> 
> 	BUG: KASAN: use-after-free in exclusive_event_destroy+0xd8/0xf0
> 	Read of size 4 at addr ffff88816e2bb63c by task perf/7748
> 
> i915 tries to cope with it by installing a event->destroy, but that is
> not sufficient: if pmu is released by the driver, it will still crash
> since event->pmu is still used.
> 
> Moreover, even with that use-after-free fixed by adjusting the order in
> _free_event() or delaying the free by the driver, kernel still oops when
> closing the event fd related to a unregistered pmu: the percpu variables
> allocated on perf_pmu_register() would already be released. One such
> crash is:
> 
> 	BUG: KASAN: user-memory-access in _raw_spin_lock_irqsave+0x88/0x100
> 	Write of size 4 at addr 00000000ffffffff by task perf/727
> 
> 	CPU: 0 UID: 0 PID: 727 Comm: perf Not tainted 6.12.0-rc1-DEMARCHI-dxnf+ #9
> 	Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS unknown 2/2/2022
> 	Call Trace:
> 	 <TASK>
> 	 dump_stack_lvl+0x5f/0x90
> 	 print_report+0x4d3/0x50a
> 	 ? __pfx__raw_spin_lock_irqsave+0x10/0x10
> 	 ? kasan_addr_to_slab+0xd/0xb0
> 	 kasan_report+0xe2/0x170
> 	 ? _raw_spin_lock_irqsave+0x88/0x100
> 	 ? _raw_spin_lock_irqsave+0x88/0x100
> 	 kasan_check_range+0x125/0x230
> 	 __kasan_check_write+0x14/0x30
> 	 _raw_spin_lock_irqsave+0x88/0x100
> 	 ? __pfx__raw_spin_lock_irqsave+0x10/0x10
> 	 _atomic_dec_and_raw_lock_irqsave+0x89/0x110
> 	 ? __kasan_check_write+0x14/0x30
> 	 put_pmu_ctx+0x98/0x330
> 
> The fix here is to provide a set of get/put hooks that drivers can
> implement to piggy back the perf's pmu lifecycle to the driver's
> instance lifecycle.  With this, perf_pmu_unregister() can be called by
> the driver, which is then responsible for freeing the resources.

I'm confused.. probably because I still don't have any clue about
drivers and the above isn't really telling me much either.

I don't see how you get rid of the try_module_get() we do per event;
without that you can't unload the module.

And I don't see how you think it is safe to free a pmu while there are
still events around.

Nor do I really see what these new get/put methods do. I see you call
->put() where we do module_put(), and ->get() near try_module_get(), but
how is that helping?



  parent reply	other threads:[~2024-10-14 17:33 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-08 18:34 [PATCH 0/5] perf: Fix pmu for drivers with bind/unbind Lucas De Marchi
2024-10-08 18:34 ` [PATCH 1/5] perf: Add dummy pmu module Lucas De Marchi
2024-10-15  0:26   ` Jeff Johnson
2024-10-15  4:23     ` Lucas De Marchi
2024-10-16  8:51   ` Peter Zijlstra
2024-10-18 19:30     ` Lucas De Marchi
2024-10-08 18:34 ` [PATCH 2/5] perf: Move free outside of the mutex Lucas De Marchi
2024-10-08 18:34 ` [PATCH 3/5] perf: Add pmu get/put Lucas De Marchi
2024-10-09 10:44   ` kernel test robot
2024-10-14 17:32   ` Peter Zijlstra [this message]
2024-10-14 18:20     ` Lucas De Marchi
2024-10-14 19:25       ` Peter Zijlstra
2024-10-14 20:12         ` Lucas De Marchi
2024-10-16 12:03         ` Peter Zijlstra
2024-10-18 19:46           ` Lucas De Marchi
2024-10-22 21:52             ` Peter Zijlstra
2024-10-23  5:07               ` Lucas De Marchi
2024-10-31  5:07                 ` Lucas De Marchi
2024-10-31  9:45                   ` Peter Zijlstra
2024-10-08 18:35 ` [PATCH 4/5] perf/dummy_pmu: Tie pmu to device lifecycle Lucas De Marchi
2024-10-08 18:35 ` [PATCH 5/5] perf/dummy_pmu: Track and disable active events Lucas De Marchi
2024-10-11 22:21 ` [PATCH 0/5] perf: Fix pmu for drivers with bind/unbind Umesh Nerlige Ramappa
2024-10-11 23:03   ` Lucas De Marchi
2024-10-11 22:59 ` Lucas De Marchi

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=20241014173246.GI16066@noisy.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=acme@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=irogers@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lucas.demarchi@intel.com \
    --cc=mingo@redhat.com \
    --cc=tvrtko.ursulin@igalia.com \
    --cc=umesh.nerlige.ramappa@intel.com \
    /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®