Re: [PATCH v2] kvm: x86: emulate monitor and mwait instructions as nop

Nadav Amit
2014-06-04T20:55:45Z

On Jun 4, 2014, at 11:11 PM, Gabriel L. Somlo <gsomlo@gmail.com> wrote:

> On Wed, Jun 04, 2014 at 11:01:50PM +0300, Nadav Amit wrote:
>> On Jun 4, 2014, at 10:43 PM, Gabriel L. Somlo <gsomlo@gmail.com> wrote:
>>
>> My implementation still emulates the instruction as a NOP, but first checks for an exception.
>
> [...]
>
>> Anyhow, if you want a real mwait emulation, you can write-protect the page of the monitored memory area in the EPT of the other VCPUs and set a callback once a write to the area takes place. You may want the host to cause a spurious wakeup after you do the write-protection, so you will not miss a write of another VCPU to the monitored area. After the spurious wake-up, the VM is likely to issue an additional mwait, using the same monitored cache-line.
>>
>> Additional care for DMAs (emulated and paravirtual) might be needed with the assistance of QEMU. The complicated case is dealing with the DMAs of assigned devices due to the lack of support for I/O page-faules.
>
> I took a stab at something like that a while ago:
>
> http://www.contrib.andrew.cmu.edu/~somlo/OSXKVM/kvm-mwait-emu-20140205.patch
>
> with limited success, probably due to my lack of familiarity with
> the fine details of the KVM code base... :)
>
> My main interest was to get it working well enough to be useful for
> idle loops (which is the only thing I know of that either Linux or
> OS X use monitor and mwait for, currently -- so DMA wasn't a huge
> priority).
>
> Even if we got it working well enough in the general case (any number
> of vcpus, etc) I think it would still suck for idle loops when
> compared to simply falling back to HLT (mainly due to all the TLB
> shootdowns required to make it work) :)

MWAIT is likely not to perform as good as HLT, but you may play with other techniques, like clearing the dirty flag of the page on other VCPUs and periodically check it.
You will still need TLB shootdowns at that case, and wakeup might be slower, but at least you will have fewer emulations and exits.
Anyhow, I would try to avoid using the emulator for your purpose (especially since your guest crashes), and try to use the MTF feature instead - at least to get the system to work.

Regards,
Nadav