From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 731D82609C5 for ; Wed, 25 Feb 2026 01:21:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771982465; cv=none; b=XgOBjX9pOF5wGlLQJfbPC8t+1VbKO1majdzbYfGiCI4n4t4r1bqUHAAZMLeuvwi/BGiNb4vrgWc+GX1zOGZFOLfnH+OVwmtd41D86c9ZRlBbZP39VpBCpsEYOa+pCDr0KUbIXS89sAiu32x/eMSea/oJHYz2rrJxedbLAP075Gg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771982465; c=relaxed/simple; bh=iyxIPblZQF7VigUXYeAExOzxkERFEm+SJWV+/cinaYk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=creTHSWMOf9xf1tq/fMzz61OcGkai+2pn375b2WZLpJTlyVjRvWWSbo/yADwHGibFrf34CM9vOVtYInvl7akaKEgC2Mz+xpcDRSwFpl1EoOodGzSufplQL8g27wfgB2DewpUjixgk0iOC4m2gs3NoMgRvEuCIWQGZl4EYxsbL7Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zYMeJgCP; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zYMeJgCP" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b630753cc38so34290126a12.1 for ; Tue, 24 Feb 2026 17:21:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771982462; x=1772587262; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=iAM51bp4Q7vtLVTuwPEvx5LguTW6LfJ4+fu/BmQleqk=; b=zYMeJgCPC8e8ZZrN9bYnKZA0FtR5BhIDDC+9MUmF7IxvUihatePxZLes3O+KkV+QI1 byVnyhqlR1NKdBMY4HvbViqcDPi3pYV2HQlknMs8A20llalvf4RfQBMPpZ2ho1pw3nzc zWw5xtEniGJDVvfOYPO0tC5JhvurbxIJzFYM9oGuXEC5Qe+mQzflIqBUXerC4mbQfH8r iPtEo3MOHj1ZP130rUz1MeZi8EWiPnegbdfp2/T3Xeva4xToXCUp1Y+OEwEyq0QAWge4 C3S+mSoks4j6FFHwJLVl/L4gMO2WZEl4Ns6q27SNR/ztA0ZLNfL/GRJyE2LZ8oY8BPUd o0QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771982462; x=1772587262; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iAM51bp4Q7vtLVTuwPEvx5LguTW6LfJ4+fu/BmQleqk=; b=e+3C/4GwFpUtUOtrNpDOlAAhROeNTdhFnsrKuCWsFwnn5WfSIBTFU5PakAPhWWtHev ccVmLDuXgzSpaPOn8071a5J5v/1hBFNMUJQ7tY0oLhoskcD4lBSOLyAFhyVlqjgYSPIt ZfU3jQhhTl36EsGEsujLhv7rlo6lvUp7vJ9I9nR8Tg/syYXHlPcHYD3b7jshzjvZznif A0X0ViOHGSOe3UTOKGviug88gVlcjywy68+FUW40TWWj1+c4VzZipLQ14igiRjYaMYkk yCN7Gy1UBqPtpwgq06btcC02QAXLCCwCHaEosQP4MTLrr2WE7O5KiyyPErEHLxl0gp1c Ncaw== X-Forwarded-Encrypted: i=1; AJvYcCWS9CFDkcQbfSVLqhQWxCmBULXnWhHclo7Y/WMs8XgptWcCX1VFODodjnrOBP3+Ww+R08D0o20JpzF19I0=@vger.kernel.org X-Gm-Message-State: AOJu0YyOTVQxc19RY+9L3bmAN7JkWJWt6P3HPWffprScilJJo+4BH9ij q3vdianqtneWRftqsH7wj2w3vw49BJs0pJh9PAswYfc92Q2Sa8E1Z55tXHSv9dcVAshVL6svX3c lNIrnLA== X-Received: from pgla15.prod.google.com ([2002:a63:b4f:0:b0:c07:89a0:6746]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:4c0e:b0:393:7575:a8d5 with SMTP id adf61e73a8af0-39545ed0443mr12719399637.22.1771982461746; Tue, 24 Feb 2026 17:21:01 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 24 Feb 2026 17:20:40 -0800 In-Reply-To: <20260225012049.920665-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260225012049.920665-1-seanjc@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260225012049.920665-6-seanjc@google.com> Subject: [PATCH 05/14] KVM: x86: Open code read vs. write userspace MMIO exits in emulator_read_write() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Kiryl Shutsemau Cc: kvm@vger.kernel.org, x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Yashu Zhang , Rick Edgecombe , Binbin Wu , Xiaoyao Li , Tom Lendacky , Michael Roth Content-Type: text/plain; charset="UTF-8" Open code the differences in read vs. write userspace MMIO exits instead of burying three lines of code behind indirect callbacks, as splitting the logic makes it extremely hard to track that KVM's handling of reads vs. write is _significantly_ different. Add a comment to explain why the semantics are different, and how on earth an MMIO write ends up triggering an exit to userspace. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7cbd6f7d8578..5fde5bb010e7 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8116,8 +8116,6 @@ struct read_write_emulator_ops { void *val, int bytes); int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val); - int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, - void *val, int bytes); bool write; }; @@ -8139,31 +8137,14 @@ static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) return vcpu_mmio_write(vcpu, gpa, bytes, val); } -static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, - void *val, int bytes) -{ - return X86EMUL_IO_NEEDED; -} - -static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, - void *val, int bytes) -{ - struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; - - memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); - return X86EMUL_CONTINUE; -} - static const struct read_write_emulator_ops read_emultor = { .read_write_emulate = read_emulate, .read_write_mmio = vcpu_mmio_read, - .read_write_exit_mmio = read_exit_mmio, }; static const struct read_write_emulator_ops write_emultor = { .read_write_emulate = write_emulate, .read_write_mmio = write_mmio, - .read_write_exit_mmio = write_exit_mmio, .write = true, }; @@ -8296,7 +8277,19 @@ static int emulator_read_write(struct x86_emulate_ctxt *ctxt, vcpu->run->exit_reason = KVM_EXIT_MMIO; vcpu->run->mmio.phys_addr = frag->gpa; - return ops->read_write_exit_mmio(vcpu, frag->gpa, val, bytes); + /* + * For MMIO reads, stop emulating and immediately exit to userspace, as + * KVM needs the value to correctly emulate the instruction. For MMIO + * writes, continue emulating as the write to MMIO is a side effect for + * all intents and purposes. KVM will still exit to userspace, but + * after completing emulation (see the check on vcpu->mmio_needed in + * x86_emulate_instruction()). + */ + if (!ops->write) + return X86EMUL_IO_NEEDED; + + memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); + return X86EMUL_CONTINUE; } static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, -- 2.53.0.414.gf7e9f6c205-goog