Message ID | 20230904095347.14994-1-manali.shukla@amd.com (mailing list archive) |
---|---|
Headers | show |
Series | Implement support for IBS virtualization | expand |
On Mon, Sep 04, 2023 at 09:53:34AM +0000, Manali Shukla wrote: > Note that, since IBS registers are swap type C [2], the hypervisor is > responsible for saving and restoring of IBS host state. Hypervisor > does so only when IBS is active on the host to avoid unnecessary > rdmsrs/wrmsrs. 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. Why do you think it is OK for a guest to disable the host IBS when entering a guest? Perhaps the host was wanting to profile the guest. Only when perf_event_attr::exclude_guest is set is this allowed, otherwise you have to respect the host running IBS and you're not allowed to touch it. Host trumps guest etc..
Hi Peter, Thank you for looking into this. On 9/5/2023 9:17 PM, Peter Zijlstra wrote: > On Mon, Sep 04, 2023 at 09:53:34AM +0000, Manali Shukla wrote: > >> Note that, since IBS registers are swap type C [2], the hypervisor is >> responsible for saving and restoring of IBS host state. Hypervisor >> does so only when IBS is active on the host to avoid unnecessary >> rdmsrs/wrmsrs. 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. > > Why do you think it is OK for a guest to disable the host IBS when > entering a guest? Perhaps the host was wanting to profile the guest. > 1. Since IBS registers are of swap type C [1], only guest state is saved and restored by the hardware. Host state needs to be saved and restored by hypervisor. In order to save IBS registers correctly, IBS needs to be disabled before saving the IBS registers. 2. As per APM [2], "When a VMRUN is executed to an SEV-ES guest with IBS virtualization enabled, the IbsFetchCtl[IbsFetchEn] and IbsOpCtl[IbsOpEn] MSR bits must be 0. If either of these bits are not 0, the VMRUN will fail with a VMEXIT_INVALID error code." This is enforced by hardware on SEV-ES guests when VIBS is enabled on SEV-ES guests. 3. VIBS is not enabled by default. It can be enabled by an explicit qemu command line option "-cpu +ibs". Guest should be invoked without this option when host wants to profile the guest. [1] https://bugzilla.kernel.org/attachment.cgi?id=304653 AMD64 Architecture Programmer’s Manual, Vol 2, Appendix B. Layout of VMCB, Table B-2. VMCB Layout, State Save Area Table B-4. VMSA Layout, State Save Area for SEV-ES [2] https://bugzilla.kernel.org/attachment.cgi?id=304653 AMD64 Architecture Programmer’s Manual, Vol 2, Section 15.38, Instruction-Based Sampling Virtualization > Only when perf_event_attr::exclude_guest is set is this allowed, > otherwise you have to respect the host running IBS and you're not > allowed to touch it. > > Host trumps guest etc.. - Manali
On Wed, Sep 06, 2023 at 09:08:25PM +0530, Manali Shukla wrote: > Hi Peter, > > Thank you for looking into this. > > On 9/5/2023 9:17 PM, Peter Zijlstra wrote: > > On Mon, Sep 04, 2023 at 09:53:34AM +0000, Manali Shukla wrote: > > > >> Note that, since IBS registers are swap type C [2], the hypervisor is > >> responsible for saving and restoring of IBS host state. Hypervisor > >> does so only when IBS is active on the host to avoid unnecessary > >> rdmsrs/wrmsrs. 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. > > > > Why do you think it is OK for a guest to disable the host IBS when > > entering a guest? Perhaps the host was wanting to profile the guest. > > > > 1. Since IBS registers are of swap type C [1], only guest state is saved > and restored by the hardware. Host state needs to be saved and restored by > hypervisor. In order to save IBS registers correctly, IBS needs to be > disabled before saving the IBS registers. > > 2. As per APM [2], > "When a VMRUN is executed to an SEV-ES guest with IBS virtualization enabled, the > IbsFetchCtl[IbsFetchEn] and IbsOpCtl[IbsOpEn] MSR bits must be 0. If either of > these bits are not 0, the VMRUN will fail with a VMEXIT_INVALID error code." > This is enforced by hardware on SEV-ES guests when VIBS is enabled on SEV-ES > guests. I'm not sure I'm fluent in virt speak (in fact, I'm sure I'm not). Is the above saying that a host can never IBS profile a guest? Does the current IBS thing assert perf_event_attr::exclude_guest is set? I can't quickly find anything :-(
Hi Peter, On 9/7/2023 1:26 AM, Peter Zijlstra wrote: > On Wed, Sep 06, 2023 at 09:08:25PM +0530, Manali Shukla wrote: >> Hi Peter, >> >> Thank you for looking into this. >> >> On 9/5/2023 9:17 PM, Peter Zijlstra wrote: >>> On Mon, Sep 04, 2023 at 09:53:34AM +0000, Manali Shukla wrote: >>> >>>> Note that, since IBS registers are swap type C [2], the hypervisor is >>>> responsible for saving and restoring of IBS host state. Hypervisor >>>> does so only when IBS is active on the host to avoid unnecessary >>>> rdmsrs/wrmsrs. 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. >>> >>> Why do you think it is OK for a guest to disable the host IBS when >>> entering a guest? Perhaps the host was wanting to profile the guest. >>> >> >> 1. Since IBS registers are of swap type C [1], only guest state is saved >> and restored by the hardware. Host state needs to be saved and restored by >> hypervisor. In order to save IBS registers correctly, IBS needs to be >> disabled before saving the IBS registers. >> >> 2. As per APM [2], >> "When a VMRUN is executed to an SEV-ES guest with IBS virtualization enabled, the >> IbsFetchCtl[IbsFetchEn] and IbsOpCtl[IbsOpEn] MSR bits must be 0. If either of >> these bits are not 0, the VMRUN will fail with a VMEXIT_INVALID error code." >> This is enforced by hardware on SEV-ES guests when VIBS is enabled on SEV-ES >> guests. > > I'm not sure I'm fluent in virt speak (in fact, I'm sure I'm not). Is > the above saying that a host can never IBS profile a guest? Host can profile a guest with IBS if VIBS is disabled for the guest. This is the default behavior. Host can not profile guest if VIBS is enabled for guest. > > Does the current IBS thing assert perf_event_attr::exclude_guest is set? Unlike AMD core pmu, IBS doesn't have Host/Guest filtering capability, thus perf_event_open() fails if exclude_guest is set for an IBS event. > > I can't quickly find anything :-( Thank you, Manali
On Thu, Sep 07, 2023 at 09:19:51PM +0530, Manali Shukla wrote: > > I'm not sure I'm fluent in virt speak (in fact, I'm sure I'm not). Is > > the above saying that a host can never IBS profile a guest? > > Host can profile a guest with IBS if VIBS is disabled for the guest. This is > the default behavior. Host can not profile guest if VIBS is enabled for guest. > > > > > Does the current IBS thing assert perf_event_attr::exclude_guest is set? > > Unlike AMD core pmu, IBS doesn't have Host/Guest filtering capability, thus > perf_event_open() fails if exclude_guest is set for an IBS event. Then you must not allow VIBS if a host cpu-wide IBS counter exists. Also, VIBS reads like it can be (ab)used as a filter.
On 9/8/2023 7:01 PM, Peter Zijlstra wrote: > On Thu, Sep 07, 2023 at 09:19:51PM +0530, Manali Shukla wrote: > >>> I'm not sure I'm fluent in virt speak (in fact, I'm sure I'm not). Is >>> the above saying that a host can never IBS profile a guest? >> >> Host can profile a guest with IBS if VIBS is disabled for the guest. This is >> the default behavior. Host can not profile guest if VIBS is enabled for guest. >> >>> >>> Does the current IBS thing assert perf_event_attr::exclude_guest is set? >> >> Unlike AMD core pmu, IBS doesn't have Host/Guest filtering capability, thus >> perf_event_open() fails if exclude_guest is set for an IBS event. > > Then you must not allow VIBS if a host cpu-wide IBS counter exists. > > Also, VIBS reads like it can be (ab)used as a filter. I think I get your point: If host IBS with exclude_guest=0 doesn't capture guest samples because of VIBS, it is an unintended behavior. But if a guest cannot use IBS because a host is using it, that is also unacceptable behavior. Let me think over it and come back. - Manali
On 9/11/2023 6:02 PM, Manali Shukla wrote: > On 9/8/2023 7:01 PM, Peter Zijlstra wrote: >> On Thu, Sep 07, 2023 at 09:19:51PM +0530, Manali Shukla wrote: >> >>>> I'm not sure I'm fluent in virt speak (in fact, I'm sure I'm not). Is >>>> the above saying that a host can never IBS profile a guest? >>> >>> Host can profile a guest with IBS if VIBS is disabled for the guest. This is >>> the default behavior. Host can not profile guest if VIBS is enabled for guest. >>> >>>> >>>> Does the current IBS thing assert perf_event_attr::exclude_guest is set? >>> >>> Unlike AMD core pmu, IBS doesn't have Host/Guest filtering capability, thus >>> perf_event_open() fails if exclude_guest is set for an IBS event. >> >> Then you must not allow VIBS if a host cpu-wide IBS counter exists. >> >> Also, VIBS reads like it can be (ab)used as a filter. > > I think I get your point: If host IBS with exclude_guest=0 doesn't capture > guest samples because of VIBS, it is an unintended behavior. > > But if a guest cannot use IBS because a host is using it, that is also > unacceptable behavior. > > Let me think over it and come back. > > - Manali Hi Peter, Apologies for the delay in response. It took me a while to think about various possible solutions and their feasibility. Problem with current design is, exclude_guest setting of the host IBS event is not honored. Essentially, guest samples become invisible to the host IBS even when exclude_guest=0, when VIBS is enabled on the guest. Solution 1: Enforce exclude_guest=1 for host IBS when hw supports VIBS, i.e. if (cpuid[VIBS]) enforce exclude_guest=1 else enforce exclude_guest=0 Disable/enable host IBS at VM Entry/VM Exit if cpuid[VIBS] is set and an active IBS event exists on that cpu. The major downside of this approach is, it will break all currently working scripts which are using perf_event_open() to start an ibs event, since new kernel will suddenly start failing for IBS events with exclude_guest=0. The other issue is that the host with cpuid[VIBS] set, would not allow profiling any guest from the host. Solution 1.1: This is an extension to Solution 1. Instead of keying off based on just cpuid[VIBS] bit, introduce a kvm-amd module parameter and use both: if (cpuid[VIBS] && kvm-amd.ko loaded with vibs=1) enforce exclude_guest=1 else enforce exclude_guest=0 KVM AMD vibs module parameter determines whether guest will be able to use VIBS or not. The kvm-amd.ko should be loaded with vibs=0, if a host wants to profile guest and the kvm-amd.ko should be loaded with vibs=1, if a guest wants to use VIBS. However, both are mutually exclusive. The issue of digressing from current exclude_guest behavior remains with this solution. Other issues are, 1) If the host IBS is active, with vibs=0, reloading of the kvm-amd.ko with vibs=1 will fail until IBS is running on the host. 2) If the host IBS is active, with vibs=1, reloading of the kvm-amd.ko with vibs=0 will fail until IBS is running on the host. Solution 2: Dynamically disable/enable VIBS per vCPU basis, i.e. when a host IBS is active, guest will not be able to use VIBS _for that vCPU_. If the host is not using IBS, VIBS will be enabled at VM Entry. Although this solution does not digress from existing exclude_guest behavior, it has its own limitations: 1) VIBS inside the guest is unreliable because host IBS can dynamically change VIBS behavior. 2) It works only for SVM and SEV guests, but not for SEV-ES and SEV-SNP guests since there is no way to disable VIBS dynamically. From all the above solutions, we would be more inclined on implementing solution 1.1. -Manali