Add support for IBS virtualization (VIBS). VIBS feature allows the guest to collect IBS samples without exiting the guest. There are 2 parts to it [1]. - Virtualizing the IBS register state. - Ensuring the IBS interrupt is handled in the guest without exiting the hypervisor. To deliver virtualized IBS interrupts to the guest, VIBS requires either AVIC or Virtual NMI (VNMI) support [1]. During IBS sampling, the hardware signals a VNMI. The source of this VNMI depends on the AVIC configuration: - With AVIC disabled, the virtual NMI is hardware-accelerated. - With AVIC enabled, the virtual NMI is delivered via AVIC using Extended LVT. The local interrupts are extended to include more LVT registers, to allow additional interrupt sources, like instruction based sampling etc. [3]. Although IBS virtualization requires either AVIC or VNMI to be enabled in order to successfully deliver IBS NMIs to the guest, VNMI must be enabled to ensure reliable delivery. This requirement stems from the dynamic behavior of AVIC (This is needed because AVIC can change its state while the guest is running). While a guest is launched with AVIC enabled, AVIC can be inhibited at runtime. When AVIC is inhibited and VNMI is disabled, there is no mechanism to deliver IBS NMIs to the guest. Therefore, enabling VNMI is necessary to support IBS virtualization reliably. Note that, since IBS registers are swap type C [2], the hypervisor is responsible for saving and restoring of IBS host state. Hypervisor needs to disable host IBS before saving the state and enter the guest. After a guest exit, the hypervisor needs to restore host IBS state and re-enable IBS. The mediated PMU has the capability to save the host context when entering the guest by scheduling out all exclude_guest events, and to restore the host context when exiting the guest by scheduling in the previously scheduled-out events. This behavior aligns with the requirement for IBS registers being of swap type C. Therefore, the mediated PMU design can be leveraged to implement IBS virtualization. As a result, enabling the mediated PMU is a necessary requirement for IBS virtualization. The initial version of this series has been posted here: https://lore.kernel.org/kvm/f98687e0-1fee-8208-261f-d93152871f00@xxxxxxx/ Since then, the mediated PMU patches [4] have matured significantly. This series is a resurrection of previous VIBS series and leverages the mediated PMU infrastructure to enable IBS virtualization. How to enable VIBS? ---------------------------------------------- sudo echo 0 | sudo tee /proc/sys/kernel/nmi_watchdog sudo modprobe -r kvm_amd sudo modprobe kvm_amd enable_mediated_pmu=1 vnmi=1 Qemu changes can be found at below location: ---------------------------------------------- https://github.com/AMDESE/qemu/tree/vibs_v1 Qemu commandline to enable IBS virtualization: ------------------------------------------------ qemu-system-x86_64 -enable-kvm -cpu host \ .. Testing done: ------------------------------------------------ - Following tests were executed on guest sudo perf record -e ibs_op// -c 100000 -a sudo perf record -e ibs_op// -c 100000 -C 10 sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -a sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -a --raw-samples sudo perf record -e ibs_op/cnt_ctl=1,l3missonly=1/ -c 100000 -a sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -p 1234 sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -- ls sudo perf record -e ibs_op// -e ibs_fetch// -a --raw-samples -c 100000 sudo perf report sudo perf script sudo perf report -D | grep -P "LdOp 1.*StOp 0" | wc -l sudo perf report -D | grep -P "LdOp 1.*StOp 0.*DcMiss 1" | wc -l sudo perf report -D | grep -P "LdOp 1.*StOp 0.*DcMiss 1.*L2Miss 1" | wc -l sudo perf report -D | grep -B1 -P "LdOp 1.*StOp 0.*DcMiss 1.*L2Miss 1" | grep -P "DataSrc ([02-9]|1[0-2])=" | wc -l - perf_fuzzer was run for 12hrs, no softlockups or unknown NMIs were seen. - Ran xapic_ipi_test and xapic_state_test to verify there was no regression after changes were made to the APIC register mask to accommodate extended APIC registers. TO-DO: ----------------------------------- Enable IBS virtualization on SEV-ES and SEV-SNP guests. base-commit: https://github.com/sean-jc/linux.git tags/mediated-vpmu-v5 [1]: https://bugzilla.kernel.org/attachment.cgi?id=306250 AMD64 Architecture Programmer’s Manual, Vol 2, Section 15.38 Instruction-Based Sampling Virtualization. [2]: https://bugzilla.kernel.org/attachment.cgi?id=306250 AMD64 Architecture Programmer’s Manual, Vol 2, Appendix B Layout of VMCB, Table B-3 Swap Types. [3]: https://bugzilla.kernel.org/attachment.cgi?id=306250 AMD64 Architecture Programmer’s Manual, Vol 2, Section 16.4.5 Extended Interrupts. [4]: https://lore.kernel.org/kvm/463a0265-e854-4677-92f2-be17e46a3426@xxxxxxxxxxxxxxx/T/#t v1->v2 - Incorporated review comments from Mi Dapeng - Change the name of kvm_lapic_state_w_extapic to kvm_ext_lapic_state. - Refactor APIC register mask handling in order to support extended APIC registers. - Miscellaneous changes v1: https://lore.kernel.org/kvm/afafc865-b42f-4a9d-82d7-a72de16bb47b@xxxxxxx/T/ Manali Shukla (7): perf/amd/ibs: Fix race condition in IBS KVM: x86: Refactor APIC register mask handling to support extended ranges KVM: Add KVM_GET_EXT_LAPIC and KVM_SET_EXT_LAPIC for extapic KVM: x86/cpuid: Add a KVM-only leaf for IBS capabilities KVM: x86: Extend CPUID range to include new leaf perf/x86/amd: Enable VPMU passthrough capability for IBS PMU perf/x86/amd: Remove exclude_guest check from perf_ibs_init() Santosh Shukla (5): x86/cpufeatures: Add CPUID feature bit for Extended LVT KVM: x86: Add emulation support for Extented LVT registers x86/cpufeatures: Add CPUID feature bit for VIBS in SVM/SEV guests KVM: SVM: Extend VMCB area for virtualized IBS registers KVM: SVM: Add support for IBS Virtualization Documentation/virt/kvm/api.rst | 23 +++++ arch/x86/events/amd/ibs.c | 8 +- arch/x86/include/asm/apicdef.h | 17 ++++ arch/x86/include/asm/cpufeatures.h | 2 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/svm.h | 16 ++- arch/x86/include/uapi/asm/kvm.h | 5 + arch/x86/kvm/cpuid.c | 13 +++ arch/x86/kvm/lapic.c | 152 +++++++++++++++++++++-------- arch/x86/kvm/lapic.h | 9 +- arch/x86/kvm/reverse_cpuid.h | 16 +++ arch/x86/kvm/svm/avic.c | 4 + arch/x86/kvm/svm/svm.c | 98 +++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 9 +- arch/x86/kvm/x86.c | 37 +++++-- include/uapi/linux/kvm.h | 10 ++ 16 files changed, 359 insertions(+), 61 deletions(-) base-commit: 196d9e72c4b0bd68b74a4ec7f52d248f37d0f030