kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: stsp <stsp2@yandex.ru>
Cc: Jim Mattson <jmattson@google.com>, kvm list <kvm@vger.kernel.org>
Subject: Re: guest/host mem out of sync on core2duo?
Date: Sat, 19 Jun 2021 00:54:50 +0000	[thread overview]
Message-ID: <YM1AWuoRm6xh+OVr@google.com> (raw)
In-Reply-To: <4834cc76-72d5-4d23-7a56-63e455683db5@yandex.ru>

On Sat, Jun 19, 2021, stsp wrote:
> 19.06.2021 01:32, Sean Christopherson пишет:
> > Argh!  Check out this gem:
> > 
> > 	/*
> > 	 *   Fix the "Accessed" bit in AR field of segment registers for older
> > 	 * qemu binaries.
> > 	 *   IA32 arch specifies that at the time of processor reset the
> > 	 * "Accessed" bit in the AR field of segment registers is 1. And qemu
> > 	 * is setting it to 0 in the userland code. This causes invalid guest
> > 	 * state vmexit when "unrestricted guest" mode is turned on.
> > 	 *    Fix for this setup issue in cpu_reset is being pushed in the qemu
> > 	 * tree. Newer qemu binaries with that qemu fix would not need this
> > 	 * kvm hack.
> > 	 */
> > 	if (is_unrestricted_guest(vcpu) && (seg != VCPU_SREG_LDTR))
> > 		var->type |= 0x1; /* Accessed */
> > 
> > 
> > KVM fixes up segs when unrestricted guest is enabled, but otherwise leaves 'em
> > be, presumably because it has the emulator to fall back on for invalid state.
> > Guess what's missing in the invalid state check...
> > 
> > I think this should do it:
> Until when will it run on an emulator in this case?  Will it be too slow
> without a slightest hint to the user?

KVM would emulate until the invalid state went away, i.e. until the offending
register was loaded with a new segment that set the Accessed bit.

> If it is indeed the performance penalty for no good reason, then my
> preference would be to get an error right from KVM_SET_SREGS instead, or
> maybe from KVM_RUN, but not run everything on an emulator.

Sadly, to be consistent with other segments (SS and CS), I believe detecting and
emulating is the right "fix".  Ideally, KVM would differentiate between "invalid
for !unrestricted_guest" and "always invalid", with the latter being rejected and
punted to userspace.  E.g. I don't think it's possible for a physical CPU to have
a loaded segment with the Accessed bit set.  Unfortunately that ship sailed long,
long ago.

One possibility would be to try disabling emulate_invalid_guest_state via module
param.  That should cause failure instead of emulating.  But I suspect that that
appraoch will cause explosions for your core2duo users as KVM is probably
emulating at other points for them.  :-/

The other thing you could do would be to add a bit instrumention to query the
number of instructions KVM has emulated and alert the user if it exceeds some
arbitrary threshold.  The hiccup there is that KVM's stats are currently on
debugfs, which is usually root-only.

  $ tail /sys/kernel/debug/kvm/insn_emulation
  0

  reply	other threads:[~2021-06-19  0:54 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-12 22:49 guest/host mem out of sync on core2duo? stsp
2021-06-13 12:36 ` stsp
2021-06-14 17:06 ` Sean Christopherson
2021-06-14 17:32   ` stsp
2021-06-17 14:42     ` Sean Christopherson
2021-06-18 15:59       ` stsp
2021-06-18 21:07         ` Jim Mattson
2021-06-18 21:55           ` stsp
2021-06-18 22:06             ` Jim Mattson
2021-06-18 22:26               ` stsp
2021-06-18 22:32               ` Sean Christopherson
2021-06-19  0:11                 ` stsp
2021-06-19  0:54                   ` Sean Christopherson [this message]
2021-06-19  9:18                     ` stsp
2021-06-21  2:34           ` exception vs SIGALRM race (was: Re: guest/host mem out of sync on core2duo?) stsp
2021-06-21 22:33             ` Jim Mattson
2021-06-21 23:32               ` stsp
2021-06-22  0:27               ` stsp
2021-06-28 21:47                 ` Jim Mattson
2021-06-28 21:50                   ` stsp
2021-06-28 22:00                   ` stsp
2021-06-28 22:27                     ` Jim Mattson
2021-07-06 16:28                       ` Paolo Bonzini
2021-07-06 22:22                         ` stsp
2021-07-06 23:41                           ` Paolo Bonzini
2021-06-23 23:38               ` exception vs SIGALRM race (with test-case now!) stsp
2021-06-24  0:11                 ` stsp
2021-06-24  0:25                   ` stsp
2021-06-24 18:05                     ` exception vs SIGALRM race on core2 CPUs (with qemu-based test-case this time!) stsp
2021-06-24 18:07                     ` stsp
2021-06-25 23:35                       ` exception vs SIGALRM race on core2 CPUs (with fix!) stsp
2021-06-26  0:15                         ` Jim Mattson
2021-06-26  0:35                           ` stsp
2021-06-26 21:50                           ` stsp
2021-06-27 12:13                           ` stsp
2021-06-26 14:03               ` exception vs SIGALRM race (another patch) stsp

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=YM1AWuoRm6xh+OVr@google.com \
    --to=seanjc@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=stsp2@yandex.ru \
    /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;
as well as URLs for NNTP newsgroup(s).