mbox series

[00/10] KVM: x86/pmu: Optimize triggering of emulated events

Message ID 20231110022857.1273836-1-seanjc@google.com (mailing list archive)
Headers show
Series KVM: x86/pmu: Optimize triggering of emulated events | expand

Message

Sean Christopherson Nov. 10, 2023, 2:28 a.m. UTC
Optimize code used by, or which impacts, kvm_pmu_trigger_event() to try
and make a dent in the overhead of emulating PMU events in software, which
is quite noticeable due to it kicking in anytime the guest has a vPMU and
KVM is skipping an instruction.

Note, Jim has a proposal/idea[*] (that I supported) to make
kvm_pmu_trigger_event() even more performant.  I opted not to do that as
it's a bit more invasive, and I started chewing on this not so much because
I care _that_ much about performance, but because it irritates me that the
PMU code makes things way harder than they need to be.

Note #2, this applies on top of my other two PMU series:

  https://lore.kernel.org/all/20231103230541.352265-1-seanjc@google.com
  https://lore.kernel.org/all/20231110021306.1269082-1-seanjc@google.com

Those series fix actual functional issues, i.e. I'll definitely apply them
first (there's definitely no rush on this one).

[*] https://lore.kernel.org/all/CALMp9eQGqqo66fQGwFJMc3y+9XdUrL7ageE8kvoAOV6NJGfJpw@mail.gmail.com

Sean Christopherson (10):
  KVM: x86/pmu: Zero out PMU metadata on AMD if PMU is disabled
  KVM: x86/pmu: Add common define to capture fixed counters offset
  KVM: x86/pmu: Move pmc_idx => pmc translation helper to common code
  KVM: x86/pmu: Snapshot and clear reprogramming bitmap before
    reprogramming
  KVM: x86/pmu: Add macros to iterate over all PMCs given a bitmap
  KVM: x86/pmu: Process only enabled PMCs when emulating events in
    software
  KVM: x86/pmu: Snapshot event selectors that KVM emulates in software
  KVM: x86/pmu: Expand the comment about what bits are check emulating
    events
  KVM: x86/pmu: Check eventsel first when emulating (branch) insns
    retired
  KVM: x86/pmu: Avoid CPL lookup if PMC enabline for USER and KERNEL is
    the same

 arch/x86/include/asm/kvm-x86-pmu-ops.h |   1 -
 arch/x86/include/asm/kvm_host.h        |   1 +
 arch/x86/kvm/pmu.c                     | 143 +++++++++++++++----------
 arch/x86/kvm/pmu.h                     |  52 ++++++++-
 arch/x86/kvm/svm/pmu.c                 |   7 +-
 arch/x86/kvm/vmx/nested.c              |   2 +-
 arch/x86/kvm/vmx/pmu_intel.c           |  44 ++------
 arch/x86/kvm/x86.c                     |   6 +-
 8 files changed, 154 insertions(+), 102 deletions(-)


base-commit: ef1883475d4a24d8eaebb84175ed46206a688103

Comments

Dongli Zhang Nov. 10, 2023, 6:37 a.m. UTC | #1
Hi Sean,

On 11/9/23 18:28, Sean Christopherson wrote:
> 
> base-commit: ef1883475d4a24d8eaebb84175ed46206a688103

May I have a silly question?

May I have the tree that the commit is the base? I do not find it in kvm-x86.

https://github.com/kvm-x86/linux/commit/ef1883475d4a24d8eaebb84175ed46206a688103

Thank you very much!

Dongli Zhang
Sean Christopherson Nov. 10, 2023, 2:56 p.m. UTC | #2
On Thu, Nov 09, 2023, Dongli Zhang wrote:
> Hi Sean,
> 
> On 11/9/23 18:28, Sean Christopherson wrote:
> > 
> > base-commit: ef1883475d4a24d8eaebb84175ed46206a688103
> 
> May I have a silly question?
> 
> May I have the tree that the commit is the base? I do not find it in kvm-x86.
> 
> https://github.com/kvm-x86/linux/commit/ef1883475d4a24d8eaebb84175ed46206a688103

It's kvm-x86/next (which I failed to explicitly call out), plus two PMU series
as mentioned in the cover letter.

  https://lore.kernel.org/all/20231103230541.352265-1-seanjc@google.com
  https://lore.kernel.org/all/20231110021306.1269082-1-seanjc@google.com
Konstantin Khorenko Nov. 16, 2023, 4:37 p.m. UTC | #3
On 10.11.2023 15:56, Sean Christopherson wrote:
> On Thu, Nov 09, 2023, Dongli Zhang wrote:
>> Hi Sean,
>>
>> On 11/9/23 18:28, Sean Christopherson wrote:
>>> base-commit: ef1883475d4a24d8eaebb84175ed46206a688103
>> May I have a silly question?
>>
>> May I have the tree that the commit is the base? I do not find it in kvm-x86.
>>
>> https://github.com/kvm-x86/linux/commit/ef1883475d4a24d8eaebb84175ed46206a688103
> It's kvm-x86/next (which I failed to explicitly call out), plus two PMU series
> as mentioned in the cover letter.
>
>    https://lore.kernel.org/all/20231103230541.352265-1-seanjc@google.com
>    https://lore.kernel.org/all/20231110021306.1269082-1-seanjc@google.com

Hi Sean,

i have tested my testcase on your patchset, results look impressive, thank you for your work!
Note, for AMD node PMU totally disabled case gave a bit worse results on kvm-next kernel vs stock 
latest mainstream.

The difference is tiny though.

*AMD node*: CPU: AMD Zen 3 (three!): AMD EPYC 7443P 24-Core Processor
-----------------------------------------------------------------------------------------
| Kernel							| CPUID rate		|
-----------------------------------------------------------------------------------------
| stock ms 6.6.0+ (commit 305230142ae0) 			| 1360250		|
| stock ms 6.6.0+ + kvm.enable_pmu=0				| 1542894 (+13.4%)	|
| kvm-next ms 6.6.0+ + patches from Sean + kvm.enable_pmu=1	| 1498864		|
| kvm-next ms 6.6.0+ + patches from Sean + kvm.enable_pmu=0	| 1526396 (+1.84%)	|
-----------------------------------------------------------------------------------------


*Intel node*: CPU: Intel(R) Xeon(R) E-2136 CPU @ 3.30GHz
-----------------------------------------------------------------------------------------
| Kernel							| CPUID rate		|
-----------------------------------------------------------------------------------------
| stock ms 6.6.0+						| 1431608		|
| stock ms 6.6.0+ + kvm.enable_pmu=0				| 1553839 (+8.5%)	|
| kvm-next ms 6.6.0+ + patches from Sean + kvm.enable_pmu=1	| 1559365		|
| kvm-next ms 6.6.0+ + patches from Sean + kvm.enable_pmu=0	| 1582281 (+1.5%)	|
-----------------------------------------------------------------------------------------


Note: in order to disable PMU completely i used "kvm" module "enable_pmu=0" option
(not used KVM_PMU_CAP_DISABLEthis time).

Hope that helps.

--
Best regards,

Konstantin Khorenko,
Virtuozzo Linux Kernel Team
Sean Christopherson Feb. 3, 2024, 12:11 a.m. UTC | #4
On Thu, 09 Nov 2023 18:28:47 -0800, Sean Christopherson wrote:
> Optimize code used by, or which impacts, kvm_pmu_trigger_event() to try
> and make a dent in the overhead of emulating PMU events in software, which
> is quite noticeable due to it kicking in anytime the guest has a vPMU and
> KVM is skipping an instruction.
> 
> Note, Jim has a proposal/idea[*] (that I supported) to make
> kvm_pmu_trigger_event() even more performant.  I opted not to do that as
> it's a bit more invasive, and I started chewing on this not so much because
> I care _that_ much about performance, but because it irritates me that the
> PMU code makes things way harder than they need to be.
> 
> [...]

Applied to kvm-x86 pmu, thanks!

[01/10] KVM: x86/pmu: Zero out PMU metadata on AMD if PMU is disabled
        https://github.com/kvm-x86/linux/commit/f933b88e2015
[02/10] KVM: x86/pmu: Add common define to capture fixed counters offset
        https://github.com/kvm-x86/linux/commit/be6b067dae15
[03/10] KVM: x86/pmu: Move pmc_idx => pmc translation helper to common code
        https://github.com/kvm-x86/linux/commit/b31880ca2f41
[04/10] KVM: x86/pmu: Snapshot and clear reprogramming bitmap before reprogramming
        https://github.com/kvm-x86/linux/commit/004a0aa56ede
[05/10] KVM: x86/pmu: Add macros to iterate over all PMCs given a bitmap
        https://github.com/kvm-x86/linux/commit/e5a65d4f723a
[06/10] KVM: x86/pmu: Process only enabled PMCs when emulating events in software
        https://github.com/kvm-x86/linux/commit/d2b321ea9380
[07/10] KVM: x86/pmu: Snapshot event selectors that KVM emulates in software
        https://github.com/kvm-x86/linux/commit/f19063b1ca05
[08/10] KVM: x86/pmu: Expand the comment about what bits are check emulating events
        https://github.com/kvm-x86/linux/commit/afda2d7666f8
[09/10] KVM: x86/pmu: Check eventsel first when emulating (branch) insns retired
        https://github.com/kvm-x86/linux/commit/e35529fb4ac9
[10/10] KVM: x86/pmu: Avoid CPL lookup if PMC enabline for USER and KERNEL is the same
        https://github.com/kvm-x86/linux/commit/83bdfe04c968

--
https://github.com/kvm-x86/linux/tree/next