From nobody Mon Feb 9 19:31:43 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 A2CA8277017 for ; Wed, 21 Jan 2026 00:49:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768956554; cv=none; b=a4GnMrbSDLspbjGz3AgVIi+MKrr1CGTiaqvGnGPW9v7A4Ov10CNkhNdWtIf5auGALNl1XZ2v0Y/5o2WX+YpnFJ+I3DGNVpoE/ZjPjZGfS2W6rKqN2l06hS7TzkTC8jMV5ou7l0/gBYPyAfPKrrNG6SwW/jiQEW/TyLYa6i6rL8g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768956554; c=relaxed/simple; bh=YRfX9Q0a0jQNu704pRguyi2Mmku9HkppIDZwldLPdlo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=W7IxVEB4PseMCmBdzk7k0kjnZb0Z8UluhNEschBXOuwC0eFvjeDLdaD1us9H5dDKZ1k1YUgD1wdtEFVa1YPp6tqusiT1E2vul2JA9xkcoKJ6VxuAbBNVMv5dOBw457efLDVXzyRY1NsfFSs4oVP+LBOugj5JnY6aqKlzqgQSMho= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--chengkev.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=371Bvvnt; arc=none smtp.client-ip=209.85.214.201 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--chengkev.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="371Bvvnt" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a0e952f153so127623245ad.0 for ; Tue, 20 Jan 2026 16:49:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768956552; x=1769561352; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Si4ixKO/dBgwRmsNU8E+S8GgZCzFW5296/aB45rRxK8=; b=371Bvvnt4UJIdt5MguXnWvGhJiHZFXPEoEY4qb/bj3iHp+qQN5pu2Kg+n3tsX/dgKk Z1bB3B3Fiy35i+VlQVS5u6G0c9zdfvBLu0mLWjNaLVdE02EvgdboycAkRfc37YR4Xcxm F+h8QAp26jwrm/+ViT1CkQVc6GxhlNCUaBd6XuREVzP5dIrnJBhrM86q08zUteUI6LZC X5QeGAKuxpqIQZTugYtpOzIiqWYZ6biVUyeySibhMuAIw2Ij/VQcmzgC0VEp2cZmmf5g glg32m3wglZh7pL9i0SPmjKAUp2dV54UI0Itn5SFzAOaYLh4SnHvX751HuwHxHOBqzQG pW4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768956552; x=1769561352; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Si4ixKO/dBgwRmsNU8E+S8GgZCzFW5296/aB45rRxK8=; b=AimH+F79IODJmD9IUXYs0KtJ0kb85oCS1xdspQk6GDLzZJZKVB7tH1N9MfaBbfTCiA oD8zUOPazcR7qVUlgjUZD3V1x5wfU8jTPdG7jta+jfvn0pPw7mva9AGivpQM/VRhXkw4 p91xUtL4hkUn7Ez4DBENX37vjehfzFJX5ANB1oy2yB+DMHTiOptxYpTcO0PS9lD0YYMn qZR9/ZKcQUfBPNSkjSQyY2+E2ZEpUFG4Zqjcq+7rcFgKlknCT06OuKYHQ8uQ+c5zS4u/ sYZGfRob9/AXQofYqWhs6eMAvHC6K2F8O9FDrnTSkZ4kgXW5kqfgNIzRNFLSueDEEn4m g4Bg== X-Forwarded-Encrypted: i=1; AJvYcCWQYe1g99/Rb08eYdFgVr/3oso/842dSCiHMgXRUew3UhZUbOIm9NM0oGpxzaSIeR52e5Lv0RQBMe5LQSA=@vger.kernel.org X-Gm-Message-State: AOJu0YxONtCzFFZ62tlB6UuW1shXSAwXp8zXWqDmPTxMg3Qb5M1W7Hab n7/Di7bFiuaOl/phDVVucdTHoi2bq3DM8IkP7xTIWxnFPz2FTkTMQjm9kTaqLt+TjySbMN5SgfL 4pSzlZzD8Cqq9ag== X-Received: from plse9.prod.google.com ([2002:a17:902:b789:b0:2a7:63e0:6039]) (user=chengkev job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:22d1:b0:2a0:d05d:e4f with SMTP id d9443c01a7336-2a76b055c8cmr33982245ad.45.1768956551900; Tue, 20 Jan 2026 16:49:11 -0800 (PST) Date: Wed, 21 Jan 2026 00:49:04 +0000 In-Reply-To: <20260121004906.2373989-1-chengkev@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260121004906.2373989-1-chengkev@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260121004906.2373989-2-chengkev@google.com> Subject: [PATCH 1/3] KVM: SVM: Fix nested NPF injection to set PFERR_GUEST_{PAGE,FINAL}_MASK From: Kevin Cheng To: seanjc@google.com, pbonzini@redhat.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, yosry.ahmed@linux.dev, Kevin Cheng Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When KVM emulates an instruction for L2 and encounters a nested page fault (e.g., during string I/O emulation), nested_svm_inject_npf_exit() injects an NPF to L1. However, the code incorrectly hardcodes (1ULL << 32) for exit_info_1's upper bits when the original exit was not an NPF. This always sets PFERR_GUEST_FINAL_MASK even when the fault occurred on a page table page, preventing L1 from correctly identifying the cause of the fault. Set PFERR_GUEST_PAGE_MASK in the error code when a nested page fault occurs during a guest page table walk, and PFERR_GUEST_FINAL_MASK when the fault occurs on the final GPA-to-HPA translation. Widen error_code in struct x86_exception from u16 to u64 to accommodate the PFERR_GUEST_* bits (bits 32 and 33). Update nested_svm_inject_npf_exit() to use fault->error_code directly instead of hardcoding the upper bits. Also add a WARN_ON_ONCE if neither PFERR_GUEST_FINAL_MASK nor PFERR_GUEST_PAGE_MASK is set, as this would indicate a bug in the page fault handling code. Signed-off-by: Kevin Cheng --- arch/x86/kvm/kvm_emulate.h | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 22 ++++++++++------------ arch/x86/kvm/svm/nested.c | 11 +++++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index fb3dab4b5a53e..ff4f9b0a01ff7 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -22,7 +22,7 @@ enum x86_intercept_stage; struct x86_exception { u8 vector; bool error_code_valid; - u16 error_code; + u64 error_code; bool nested_page_fault; u64 address; /* cr2 or nested page fault gpa */ u8 async_page_fault; diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 901cd2bd40b84..923179bfd5c74 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -379,18 +379,12 @@ static int FNAME(walk_addr_generic)(struct guest_walk= er *walker, real_gpa =3D kvm_translate_gpa(vcpu, mmu, gfn_to_gpa(table_gfn), nested_access, &walker->fault); =20 - /* - * FIXME: This can happen if emulation (for of an INS/OUTS - * instruction) triggers a nested page fault. The exit - * qualification / exit info field will incorrectly have - * "guest page access" as the nested page fault's cause, - * instead of "guest page structure access". To fix this, - * the x86_exception struct should be augmented with enough - * information to fix the exit_qualification or exit_info_1 - * fields. - */ - if (unlikely(real_gpa =3D=3D INVALID_GPA)) + if (unlikely(real_gpa =3D=3D INVALID_GPA)) { +#if PTTYPE !=3D PTTYPE_EPT + walker->fault.error_code |=3D PFERR_GUEST_PAGE_MASK; +#endif return 0; + } =20 slot =3D kvm_vcpu_gfn_to_memslot(vcpu, gpa_to_gfn(real_gpa)); if (!kvm_is_visible_memslot(slot)) @@ -446,8 +440,12 @@ static int FNAME(walk_addr_generic)(struct guest_walke= r *walker, #endif =20 real_gpa =3D kvm_translate_gpa(vcpu, mmu, gfn_to_gpa(gfn), access, &walke= r->fault); - if (real_gpa =3D=3D INVALID_GPA) + if (real_gpa =3D=3D INVALID_GPA) { +#if PTTYPE !=3D PTTYPE_EPT + walker->fault.error_code |=3D PFERR_GUEST_FINAL_MASK; +#endif return 0; + } =20 walker->gfn =3D real_gpa >> PAGE_SHIFT; =20 diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index de90b104a0dd5..f8dfd5c333023 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -40,18 +40,17 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu = *vcpu, struct vmcb *vmcb =3D svm->vmcb; =20 if (vmcb->control.exit_code !=3D SVM_EXIT_NPF) { - /* - * TODO: track the cause of the nested page fault, and - * correctly fill in the high bits of exit_info_1. - */ - vmcb->control.exit_code =3D SVM_EXIT_NPF; - vmcb->control.exit_info_1 =3D (1ULL << 32); + vmcb->control.exit_info_1 =3D fault->error_code; vmcb->control.exit_info_2 =3D fault->address; } =20 + vmcb->control.exit_code =3D SVM_EXIT_NPF; vmcb->control.exit_info_1 &=3D ~0xffffffffULL; vmcb->control.exit_info_1 |=3D fault->error_code; =20 + WARN_ON_ONCE(!(vmcb->control.exit_info_1 & + (PFERR_GUEST_FINAL_MASK | PFERR_GUEST_PAGE_MASK))); + nested_svm_vmexit(svm); } =20 --=20 2.52.0.457.g6b5491de43-goog