From: Ingo Molnar <mingo@kernel.org>
To: Duoming Zhou <duoming@zju.edu.cn>
Cc: peterz@infradead.org, linux-perf-users@vger.kernel.org,
linux-kernel@vger.kernel.org, mingo@redhat.com, acme@kernel.org,
mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@kernel.org, namhyung@kernel.org, tglx@linutronix.de,
bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org,
hpa@zytor.com
Subject: Re: [PATCH V2] perf/x86/rapl: fix deadlock in rapl_pmu_event_stop
Date: Wed, 21 Sep 2022 09:53:04 +0200 [thread overview]
Message-ID: <YyrC4OyP2BtY8tNl@gmail.com> (raw)
In-Reply-To: <20220920014447.71946-1-duoming@zju.edu.cn>
* Duoming Zhou <duoming@zju.edu.cn> wrote:
> There is a deadlock in rapl_pmu_event_stop(), the process is
> shown below:
>
> (thread 1) | (thread 2)
> rapl_pmu_event_stop() | rapl_hrtimer_handle()
> ... | if (!pmu->n_active)
> raw_spin_lock_irqsave() //(1) | ...
> ... |
> hrtimer_cancel() | raw_spin_lock_irqsave() //(2)
> (block forever)
>
> We hold pmu->lock in position (1) and use hrtimer_cancel() to wait
> rapl_hrtimer_handle() to stop, but rapl_hrtimer_handle() also need
> pmu->lock in position (2). As a result, the rapl_pmu_event_stop()
> will be blocked forever.
>
> This patch extracts hrtimer_cancel() from the protection of
> raw_spin_lock_irqsave(). As a result, the rapl_hrtimer_handle()
> could obtain the pmu->lock.
>
> Fixes: 65661f96d3b3 ("perf/x86: Add RAPL hrtimer support")
> Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
> ---
> Changes in v2:
> - Move hrtimer_cancel() to the end of rapl_pmu_event_stop() function.
>
> arch/x86/events/rapl.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/events/rapl.c b/arch/x86/events/rapl.c
> index 77e3a47af5a..7c110092c83 100644
> --- a/arch/x86/events/rapl.c
> +++ b/arch/x86/events/rapl.c
> @@ -281,8 +281,6 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
> if (!(hwc->state & PERF_HES_STOPPED)) {
> WARN_ON_ONCE(pmu->n_active <= 0);
> pmu->n_active--;
> - if (pmu->n_active == 0)
> - hrtimer_cancel(&pmu->hrtimer);
>
> list_del(&event->active_entry);
>
> @@ -300,6 +298,11 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)
> hwc->state |= PERF_HES_UPTODATE;
> }
>
> + if (!pmu->n_active) {
> + raw_spin_unlock_irqrestore(&pmu->lock, flags);
> + hrtimer_cancel(&pmu->hrtimer);
> + return;
> + }
Looks racy now: AFAICS now it's possible for rapl_hrtimer_handle() to
execute at an arbitrary moment after pmu->lock is dropped - which could be
use-after-free after cleanup_rapl_pmus() executes and the PMU is freed,
right?
There's also the quality-of-implementation issue of the hrtimer executing
in a delayed fashion for the *next* event that may have been added, leading
to possibly unexpected results.
Thanks,
Ingo
next prev parent reply other threads:[~2022-09-21 7:53 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-20 1:44 [PATCH V2] perf/x86/rapl: fix deadlock in rapl_pmu_event_stop Duoming Zhou
2022-09-21 7:53 ` Ingo Molnar [this message]
2022-09-21 9:50 ` duoming
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=YyrC4OyP2BtY8tNl@gmail.com \
--to=mingo@kernel.org \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=duoming@zju.edu.cn \
--cc=hpa@zytor.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=x86@kernel.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