From nobody Tue Dec 2 02:49:41 2025 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 604403A9C01 for ; Tue, 18 Nov 2025 17:11:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763485887; cv=none; b=Y+LrYHZk2HnryKUrkrs4hUrm1xJS5Px/fE3ed93YBs493qMF9ZHcNlXv5oNOhGFeMAdDNF7NdohSDjgdtqn4S8fhRkcz7Gf9t86KJMYRcMAuNPrIpEZz8vC8nGpL61u9ul3a7bMSmOY3KVPvrQD1OYzAtL8DiHe4a+O9Qhyr8Wo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763485887; c=relaxed/simple; bh=PJXfOYUgQ/koH9RKoJHaRKsC+CsEh7NIL8+BGeZYdZc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uQTWG4ihaa6ciohUO/W323bqBLagJlN0dsWiEbvOJ19yjlB1S84BLFnu2v6rKQ40/t0OUs/d6W8b/mdJIzGLoG1upD115LzZo3Aoh5qneGgRMmmBRNPi5AT+JW/mMRZB7SgKVSeadwBrEuYTzbO/6FQxa4h+Molhx5LieWMsjMM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RvPdnyOA; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RvPdnyOA" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-4775e891b5eso30502595e9.2 for ; Tue, 18 Nov 2025 09:11:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1763485883; x=1764090683; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=siFHznuHrhv1xGgfDI5kE0TqLDfCEZnyaPZRcY6rR3Y=; b=RvPdnyOAMSIXCg5BDXB3EL0IDKorAWnXWdHbzUL1zfrhBE2SH6Gr1tQMOaAPZSpL0d BZ3pgqQGlIZ7jAfr210ymmjdgR4oXmcJfral+ZZltiShQ/I6N9GqUICh4ZmmG3za9d96 J7z0lpAdxumeseY7d7yixRueQJwxMgE+P5TSeH2B4mhiPON4uWYkpvUTWM1miCz/IDB0 Q6LusKbL0ipdyigAkAheslEztooOaYJXHPFlohznGC22MxSjRiWD5BjsElMUIqDIrn85 PlFeQM+oGAPHUAQ1XEfHmz6vrtaodVwV+2B6fyPAW2IfAi9TDD2JK0lRnkEwXjwrXvqB AS9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763485883; x=1764090683; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=siFHznuHrhv1xGgfDI5kE0TqLDfCEZnyaPZRcY6rR3Y=; b=Br4UbceYb5Wdg8g6elNXl2p01KEzddh2MR5pyCkRpl16v9eRbYU/NmwPfQDnQvs5en 9MDZxoEfdeYVgIg2aShurmwNBtkQB8T4o6YYxizpcL2ZgLGS4Il7xdIvRL2L25H79Zwi MaMs6pMdMMpoFqLLgbZBUsrr8Hqrr/F8O0+9tC3CX4CSIohZzdk4VNWavC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05:600c:45d5:b0:477:7af8:c88b with SMTP id 5b1f17b1804b1-4778fe57241mr164751365e9.11.1763485883079; Tue, 18 Nov 2025 09:11:23 -0800 (PST) Received: from ip-10-0-150-200.eu-west-1.compute.internal (ec2-52-49-196-232.eu-west-1.compute.amazonaws.com. [52.49.196.232]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-477b103d312sm706385e9.13.2025.11.18.09.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Nov 2025 09:11:22 -0800 (PST) From: griffoul@gmail.com X-Google-Original-From: griffoul@gmail.org To: kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com, shuah@kernel.org, dwmw@amazon.co.uk, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Fred Griffoul Subject: [PATCH v2 03/10] KVM: x86: Add nested state validation for pfncache support Date: Tue, 18 Nov 2025 17:11:06 +0000 Message-ID: <20251118171113.363528-4-griffoul@gmail.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251118171113.363528-1-griffoul@gmail.org> References: <20251118171113.363528-1-griffoul@gmail.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Fred Griffoul Implement state validation for nested virtualization to enable pfncache support for L1 guest pages. This adds a new nested_ops callback 'is_nested_state_invalid()' that detects when KVM needs to reload nested virtualization state. A KVM_REQ_GET_NESTED_STATE_PAGES request is triggered to reload affected pages before L2 execution when it detects invalid state. The callback monitors L1 guest pages during guest entry/exit while the vCPU runs in IN_GUEST_MODE. Currently, VMX implementations return false, with full support planned for the next patch. Signed-off-by: Fred Griffoul --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/nested.c | 6 ++++++ arch/x86/kvm/x86.c | 14 +++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 48598d017d6f..4675e71b33a7 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1960,6 +1960,7 @@ struct kvm_x86_nested_ops { struct kvm_nested_state __user *user_kvm_nested_state, struct kvm_nested_state *kvm_state); bool (*get_nested_state_pages)(struct kvm_vcpu *vcpu); + bool (*is_nested_state_invalid)(struct kvm_vcpu *vcpu); int (*write_log_dirty)(struct kvm_vcpu *vcpu, gpa_t l2_gpa); =20 int (*enable_evmcs)(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 0de84b30c41d..627a6c24625d 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -3588,6 +3588,11 @@ static bool vmx_get_nested_state_pages(struct kvm_vc= pu *vcpu) return true; } =20 +static bool vmx_is_nested_state_invalid(struct kvm_vcpu *vcpu) +{ + return false; +} + static int nested_vmx_write_pml_buffer(struct kvm_vcpu *vcpu, gpa_t gpa) { struct vmcs12 *vmcs12; @@ -7527,6 +7532,7 @@ struct kvm_x86_nested_ops vmx_nested_ops =3D { .get_state =3D vmx_get_nested_state, .set_state =3D vmx_set_nested_state, .get_nested_state_pages =3D vmx_get_nested_state_pages, + .is_nested_state_invalid =3D vmx_is_nested_state_invalid, .write_log_dirty =3D nested_vmx_write_pml_buffer, #ifdef CONFIG_KVM_HYPERV .enable_evmcs =3D nested_enable_evmcs, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4b8138bd4857..1a9c1171df49 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2262,12 +2262,24 @@ int kvm_emulate_monitor(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_monitor); =20 +static inline bool kvm_invalid_nested_state(struct kvm_vcpu *vcpu) +{ + if (is_guest_mode(vcpu) && + kvm_x86_ops.nested_ops->is_nested_state_invalid && + kvm_x86_ops.nested_ops->is_nested_state_invalid(vcpu)) { + kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu); + return true; + } + return false; +} + static inline bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu) { xfer_to_guest_mode_prepare(); =20 return READ_ONCE(vcpu->mode) =3D=3D EXITING_GUEST_MODE || - kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending(); + kvm_request_pending(vcpu) || xfer_to_guest_mode_work_pending() || + kvm_invalid_nested_state(vcpu); } =20 static fastpath_t __handle_fastpath_wrmsr(struct kvm_vcpu *vcpu, u32 msr, = u64 data) --=20 2.43.0