From: Like Xu <like.xu.linux@gmail.com>
To: Sean Christopherson <seanjc@google.com>, Roman Kagan <rkagan@amazon.de>
Cc: Jim Mattson <jmattson@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Eric Hankland <ehankland@google.com>,
kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Mingwei Zhang <mizhang@google.com>
Subject: Re: [PATCH] KVM: x86: vPMU: truncate counter value to allowed width
Date: Tue, 22 Aug 2023 17:29:56 +0800 [thread overview]
Message-ID: <bdc2be50-c8c4-ff06-196f-d9b67e61a6b5@gmail.com> (raw)
In-Reply-To: <ZJ9IaskpbIK9q4rt@google.com>
On 1/7/2023 5:26 am, Sean Christopherson wrote:
> Ugh, yeah, de0f619564f4 created a bit of a mess. The underlying issue that it
> was solving is that perf_event_read_value() and friends might sleep (yay mutex),
> and so can't be called from KVM's fastpath (IRQs disabled).
Updating pmu counters for emulated instructions cause troubles.
>
> However, detecting overflow requires reading perf_event_read_value() to gather
> the accumulated count from the hardware event in order to add it to the emulated
> count from software. E.g. if pmc->counter is X and the perf event counter is Y,
> KVM needs to factor in Y because X+Y+1 might overflow even if X+1 does not.
>
> Trying to snapshot the previous counter value is a bit of a mess. It could probably
> made to work, but it's hard to reason about what the snapshot actually contains
> and when it should be cleared, especially when factoring in the wrapping logic.
>
> Rather than snapshot the previous counter, I think it makes sense to:
>
> 1) Track the number of emulated counter events
If events are counted separately, the challenge here is to correctly time
the emulation of counter overflows, which can occur on both sides of the
counter values out of sync.
> 2) Accumulate and reset the counts from perf_event and emulated_counter into
> pmc->counter when pausing the PMC
> 3) Pause and reprogram the PMC on writes (instead of the current approach of
> blindly updating the sample period)
Updating the sample period is the only interface for KVM to configure hw
behaviour on hw-ctr. I note that perf_event_set_count() will be proposed,
and I'm pessimistic about this change.
> 4) Pause the counter when stopping the perf_event to ensure pmc->counter is
> fresh (instead of manually updating pmc->counter)
>
> IMO, that yields more intuitive logic, and makes it easier to reason about
> correctness since the behavior is easily define: pmc->counter holds the counts
> that have been gathered and processed, perf_event and emulated_counter hold
> outstanding counts on top. E.g. on a WRMSR to the counter, both the emulated
> counter and the hardware counter are reset, because whatever counts existed
> previously are irrelevant.
If we take the hardware view, a counter, emulated or not, just increments
and overflows at the threshold. The missing logic here is when the counter
is truncated when writing high bit-width values, and how to deal with the
value of pmc->prev_counter was before pmc->counter was truncated.
>
> Pausing the counter_might_ make WRMSR slower, but we need to get this all
> functionally correct before worrying too much about performance.
Performance, security and correctness should all be considered at the beginning.
>
> Diff below for what I'm thinking (needs to be split into multiple patches). It's
> *very* lightly tested.
It saddens me that no one has come up with an actual low-level counter-test for
this issue.
>
> I'm about to disappear for a week, I'll pick this back up when I get return. In
> the meantime, any testing and/or input would be much appreciated!
How about accepting Roman's original fix and then exercising the rewriting genius ?
next prev parent reply other threads:[~2023-08-22 9:30 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-04 12:00 Roman Kagan
2023-05-23 12:40 ` Like Xu
2023-05-23 16:42 ` Roman Kagan
2023-06-06 0:26 ` Sean Christopherson
2023-06-06 0:51 ` Sean Christopherson
2023-06-29 21:16 ` Jim Mattson
2023-06-30 0:11 ` Sean Christopherson
2023-06-30 0:31 ` Jim Mattson
2023-06-30 11:14 ` Roman Kagan
2023-06-30 14:28 ` Sean Christopherson
2023-06-30 15:21 ` Roman Kagan
2023-06-30 15:45 ` Sean Christopherson
2023-06-30 17:07 ` Mingwei Zhang
2023-06-30 17:16 ` Jim Mattson
2023-06-30 17:32 ` Mingwei Zhang
2023-06-30 18:03 ` Mingwei Zhang
2023-06-30 21:26 ` Sean Christopherson
2023-07-01 19:51 ` Mingwei Zhang
2023-08-11 8:30 ` Dapeng Mi
2023-08-22 9:29 ` Like Xu [this message]
2023-08-23 18:28 ` Mingwei Zhang
2023-07-03 13:33 ` Roman Kagan
2023-06-30 16:40 ` Jim Mattson
2023-06-30 23:25 ` Jim Mattson
2023-09-28 16:41 ` Sean Christopherson
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=bdc2be50-c8c4-ff06-196f-d9b67e61a6b5@gmail.com \
--to=like.xu.linux@gmail.com \
--cc=ehankland@google.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mizhang@google.com \
--cc=pbonzini@redhat.com \
--cc=rkagan@amazon.de \
--cc=seanjc@google.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®