Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: weichenchen <weichen.chen@linux.alibaba.com>, kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: x86: Fix stalled descriptor table accesses to MMIO
Date: Thu, 19 Nov 2026 03:56:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-222423@kernel.org> (raw)
In-Reply-To: <20261119032912.94364-1-weichen.chen@linux.alibaba.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: x86: split single-page reads in em_lgdt_lidt() miss
  saved GPA
--

commit db71a6d851801384a40299e7d8acce741f81c256
Author: weichenchen <weichen.chen@linux.alibaba.com>

KVM: x86: Fix stalled descriptor table accesses to MMIO

This commit switches LGDT, LIDT, SGDT and SIDT from standard guest memory
accessors to emulated memory accessors so that descriptor table accesses
targeting MMIO queue MMIO fragments and complete properly across userspace
exits.

> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
> index c1b21282187f3..3bd720d983f8e 100644
> --- a/arch/x86/kvm/emulate.c
> +++ b/arch/x86/kvm/emulate.c
[ ... ]
> @@ -3465,14 +3447,35 @@ static int em_sidt(struct x86_emulate_ctxt *ctxt)
>  
>  static int em_lgdt_lidt(struct x86_emulate_ctxt *ctxt, bool lgdt)
>  {
> -	struct desc_ptr desc_ptr;
> +	struct segmented_address addr = ctxt->src.addr.mem;
> +	struct desc_ptr desc_ptr = {};
> +	unsigned long limit_addr, base_addr;
> +	unsigned int base_bytes, desc_bytes;
>  	int rc;
>  
>  	if (ctxt->mode == X86EMUL_MODE_PROT64)
>  		ctxt->op_bytes = 8;
> -	rc = read_descriptor(ctxt, ctxt->src.addr.mem,
> -			     &desc_ptr.size, &desc_ptr.address,
> -			     ctxt->op_bytes);
> +	base_bytes = ctxt->op_bytes == 2 ? 3 : ctxt->op_bytes;
> +	desc_bytes = sizeof(desc_ptr.size) + base_bytes;
> +
> +	/* Check both fields before an MMIO read can have side effects. */
> +	rc = linearize(ctxt, addr, sizeof(desc_ptr.size), false, &limit_addr);
> +	if (rc != X86EMUL_CONTINUE)
> +		return rc;
> +	addr.ea += sizeof(desc_ptr.size);
> +	rc = linearize(ctxt, addr, base_bytes, false, &base_addr);
> +	if (rc != X86EMUL_CONTINUE)
> +		return rc;
> +
> +	/* Defer userspace MMIO until both pages have been translated. */
> +	if (offset_in_page(limit_addr) + desc_bytes > PAGE_SIZE) {
> +		rc = read_emulated(ctxt, limit_addr, &desc_ptr, desc_bytes);
> +	} else {
> +		rc = read_emulated(ctxt, limit_addr, &desc_ptr.size, sizeof(desc_ptr.size));
> +		if (rc != X86EMUL_CONTINUE)
> +			return rc;
> +		rc = read_emulated(ctxt, base_addr, &desc_ptr.address, base_bytes);
> +	}

[Severity: Medium]
Why split the single-page case into two read_emulated() calls instead of
always issuing a single read_emulated(ctxt, limit_addr, &desc_ptr,
desc_bytes)?

On a single-page MMIO operand, the first read at limit_addr matches the
hardware-saved GPA in emulator_read_write_onepage(), but the second read on
resume passes base_addr (limit_addr + 2), which misses the page-offset
check:

arch/x86/kvm/x86.c:emulator_read_write_onepage() {
    ...
	if ((ctxt->gpa_access & (write ? ACC_WRITE_MASK : ACC_READ_MASK)) &&
	    emulator_can_use_gpa(ctxt) &&
	    (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) {
		gpa = ctxt->gpa_val;
		ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
	} else {
		ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
    ...
}

Because vcpu->arch.mmio_gva is 0 when TDP is enabled,
vcpu_mmio_gva_to_gpa() falls back to walking the guest page tables in
software after the first 2-byte MMIO read has already exited to userspace.
On SEV guests with encrypted guest page tables, that software walk reads
ciphertext as PTEs and injects a spurious #PF or translates a bogus GPA; on
SMP guests, a concurrent PTE update between the two MMIO exits can fault mid-
instruction after the first MMIO read has already taken effect.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119032912.94364-1-weichen.chen@linux.alibaba.com?part=1

      reply	other threads:[~2026-11-19  3:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  3:29 [PATCH] KVM: x86: Fix stalled descriptor table accesses to MMIO weichenchen
2026-11-19  3:56 ` sashiko-bot [this message]

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=sashiko-outbox-222423@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=weichen.chen@linux.alibaba.com \
    /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