From nobody Thu Apr 2 20:09:44 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D83943A0EB3 for ; Thu, 26 Mar 2026 18:18:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774549108; cv=none; b=tWh42JDAyEhwfVLI+xIu/Jrpbhy4zMraZckfK4nCcy3TUodSu1IE/sOqM7Dkqb2tfS47gCyJuAdaeNTD+B8NWP+JcsToX40X2g4f9i4ZVPW7TCsJfqQMKae175n6daAkmqsX04Go+DktfItfVxFrq94C43Xq3NIRMFORNn0IjCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774549108; c=relaxed/simple; bh=SGJyEL+g3PK8q99YNoRrWIpj0f8lp2CxdOfUmoS4uIY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Dzb4OIrN+HQeoMaph008SywlV1nT8fYCzYYspz9CKNDNCgvVg15IyRLaEUMtQA64AaPZDjNe8R+oclHdN0mw2ijbsRkC5JlWj3FmFFOxQ0TCgGNh+NOT6qyBmgAoXHyYKlJcjfklXRyBEl36vDtqw5B9YmntIL3xPEYCku/DJd8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=C8FFfCE3; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=RbcOJ4pJ; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="C8FFfCE3"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="RbcOJ4pJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774549106; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dNZdUK8Qyt6OMDJgqqvESaDgnX6+IknpbbC3gSjOCX8=; b=C8FFfCE33jU0TqJZi9tc7duDp5hZZADxupFYC9IKAI7S77SdqWrMyvYoC0SnL6GSxxrIQP U7Q3dUIc1fIq33FCwajC3LjOgVqivMgE0fAEtwT+JD/HU/bVX/0GcDPHnc8pO2vFS0E2e+ MdxJoEUDavl6Aaj4W9MPM+UnopdlCZ8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-587-Tv99tVUuPp6fu8wDf2zWaQ-1; Thu, 26 Mar 2026 14:18:24 -0400 X-MC-Unique: Tv99tVUuPp6fu8wDf2zWaQ-1 X-Mimecast-MFC-AGG-ID: Tv99tVUuPp6fu8wDf2zWaQ_1774549103 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-48544725bdeso21040985e9.2 for ; Thu, 26 Mar 2026 11:18:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1774549103; x=1775153903; 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=dNZdUK8Qyt6OMDJgqqvESaDgnX6+IknpbbC3gSjOCX8=; b=RbcOJ4pJbTMLEHYCGEKxUwyq3U2gd9/2CSLJHZtsl0jKyiAxi2uHf5f1CNcu8tik2Q S7DCOnfUmEzaQq/aeWy8ymL6Hu8S/csPyHglizFQAua3h2I1ReV5g5AOzwW3MDNtQFce LmB+JpP39b/O8tC4HqwYaWkO/VHx7qdXZ1Gcm6G/MK5Zule/ndCajubTVOTzY17tGMHx Eu/mbNDEUPIi4HD+DK9DCoqRV7EJApwijFHMsBQWOTjtbxiRSKBTFtixfv9x94/j0kb5 rAcoONVBFr2u5YPX2LeRiDiUa9N04tkchzUyxCvoCWTaSwzGqPd/aUbc5JQe3uSjrlKo culg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774549103; x=1775153903; 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=dNZdUK8Qyt6OMDJgqqvESaDgnX6+IknpbbC3gSjOCX8=; b=swPGBJR+L2CqSx96MJhXV2Iv9/t25vqH5qmbpn2eqx9uZaxQwXqlGqUC90nQ+vqA0q q7Si3Wl3vxNBEuk3hLcSJJvMSf+ALEVPHVSGT6vb2Gi2sNHXoa8Hd2vVS/EBd8UmzwlR scGV5MM7VGscuYxdXpK6l1AvwpTUQ4fjraiKmTAP3apPMFm04GK+iFe/TruHmwBP6kuw 7Tn1/K/2POeedWyErrPrrG4hNUOdBPODBeun/aOe9X/ZpQ4bkp67jNvb9vX8qe3A057N EaIpBaSV3Ce5U8qHP9vbkUe8+TLiqPl4haUQQqfdzo7/205i7BYkRNxNuDQxQgXlDM0O u3hg== X-Gm-Message-State: AOJu0Yz9otw6KvSmDO5iuKs96d4GXGfVGI/xDcsFOYLYPrnfJJ/4R76V 7WJjOp+2AvsrTUQMD7wJA8cDJ7XAEZT69MZS4pF84CLb7lZD+ncpw+O+oBYhNr/b+htdjS3vuFz hKMvTOGEQ1/a/hUPh24BLZHYRqEvB3+eGlfoDeRgscwY50Jry1hzZNAzJnAjoUMlE68HOoQtwpO X2KcHlEde+gHaSnhCAIbJxF8ZqPazViEv7t0Jw2Gd2CMmdbdU0Pg== X-Gm-Gg: ATEYQzxTR/i7flOSLHRzX7RECWQkpTx0YNdqDddhyUub3cZ8BZR/jS5FBg+NcmAlSP0 /VjYOBmeU8F4pFrGRZB3YzxhnGIQoxVqbPzvQN7hk05jFt+YwHR8sTGydHDjkDLM1chlnowZVZT zC1fP/vws1jc70JuLDaYMePis7NOYIuKEYL4tmBt7zE9Dt27UJX+vUp5cvvqq1eO4mVCEAtlZNH YoXNfWv5Wc1u4bsGI4bZ4ffNOIKzrWKNngbUyMSmYs7732uqPhlCbuahc5hx5fxjR5Ycr1sDLDb u1AFEMATNmONyWSYqs6llC5Kq8IESoJE+is6T0qwmsOJrlTQjtZyKBMAtHCgKGVGEGIRpYeBz0P ORbXSUhxaI683LXEWazgH9BSlAkW+ZCyOwtETV5PCZBoEysZRmOEObcf3lDclnVSghB3GoZTaCD /9NFUm9kAPZsJ59/QxpiIDzHmU X-Received: by 2002:a05:600c:5296:b0:483:8062:b43 with SMTP id 5b1f17b1804b1-4871605a92dmr127516045e9.19.1774549102487; Thu, 26 Mar 2026 11:18:22 -0700 (PDT) X-Received: by 2002:a05:600c:5296:b0:483:8062:b43 with SMTP id 5b1f17b1804b1-4871605a92dmr127515465e9.19.1774549102000; Thu, 26 Mar 2026 11:18:22 -0700 (PDT) Received: from [192.168.10.48] ([151.49.85.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48722be608bsm58241325e9.0.2026.03.26.11.18.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Mar 2026 11:18:20 -0700 (PDT) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Jon Kohler , Nikunj A Dadhania , Amit Shah , Sean Christopherson , Marcelo Tosatti Subject: [PATCH 21/24] KVM: x86/mmu: add support for GMET to NPT page table walks Date: Thu, 26 Mar 2026 19:17:19 +0100 Message-ID: <20260326181723.218115-22-pbonzini@redhat.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260326181723.218115-1-pbonzini@redhat.com> References: <20260326181723.218115-1-pbonzini@redhat.com> 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" GMET allows page table entries to be created with U=3D0 in NPT. However, when GMET=3D1 U=3D0 only affects execution, not reads or writes. Ignore user faults on non-fetch accesses for NPT GMET. Signed-off-by: Paolo Bonzini --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 3 ++- arch/x86/kvm/mmu/mmu.c | 19 +++++++++++++------ arch/x86/kvm/svm/nested.c | 3 ++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index df46ee605b9b..2a26c8fe3f4b 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -369,6 +369,8 @@ union kvm_mmu_page_role { * cr4_smep is also set for EPT MBEC. Because it affects * which pages are considered non-present (bit 10 additionally * must be zero if MBEC is on) it has to be in the base role. + * It also has to be in the base role for AMD GMET because + * kernel-executable pages need to have U=3D0 with GMET enabled. */ unsigned cr4_smep:1; =20 diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index fa1942b126fb..ddca3e3e4eb2 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -90,7 +90,8 @@ void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_mbe= c); =20 void kvm_init_mmu(struct kvm_vcpu *vcpu); void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0, - unsigned long cr4, u64 efer, gpa_t nested_cr3); + unsigned long cr4, u64 efer, gpa_t nested_cr3, + u64 nested_ctl); void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, int huge_page_level, bool accessed_dirty, bool mbec, gpa_t new_eptp); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 94d7e39a9417..d9eb059d24de 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -55,6 +55,7 @@ #include #include #include +#include #include =20 #include "trace.h" @@ -5553,7 +5554,7 @@ reset_ept_shadow_zero_bits_mask(struct kvm_mmu *conte= xt, bool execonly) (14 & (access) ? 1 << 14 : 0) | \ (15 & (access) ? 1 << 15 : 0)) =20 -static void update_permission_bitmask(struct kvm_mmu *mmu, bool ept) +static void update_permission_bitmask(struct kvm_mmu *mmu, bool tdp, bool = ept) { unsigned byte; =20 @@ -5614,7 +5615,12 @@ static void update_permission_bitmask(struct kvm_mmu= *mmu, bool ept) /* Faults from kernel mode accesses to user pages */ u16 kf =3D (pfec & PFERR_USER_MASK) ? 0 : u; =20 - uf =3D (pfec & PFERR_USER_MASK) ? (u16)~u : 0; + /* + * For NPT GMET, U=3D0 does not affect reads and writes. Fetches + * are handled below via cr4_smep. + */ + if (!(tdp && cr4_smep)) + uf =3D (pfec & PFERR_USER_MASK) ? (u16)~u : 0; =20 if (efer_nx) ff =3D (pfec & PFERR_FETCH_MASK) ? (u16)~x : 0; @@ -5725,7 +5731,7 @@ static void reset_guest_paging_metadata(struct kvm_vc= pu *vcpu, return; =20 reset_guest_rsvds_bits_mask(vcpu, mmu); - update_permission_bitmask(mmu, false); + update_permission_bitmask(mmu, mmu =3D=3D &vcpu->arch.guest_mmu, false); update_pkru_bitmask(mmu); } =20 @@ -5921,7 +5927,8 @@ static void kvm_init_shadow_mmu(struct kvm_vcpu *vcpu, } =20 void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0, - unsigned long cr4, u64 efer, gpa_t nested_cr3) + unsigned long cr4, u64 efer, gpa_t nested_cr3, + u64 nested_ctl) { struct kvm_mmu *context =3D &vcpu->arch.guest_mmu; struct kvm_mmu_role_regs regs =3D { @@ -5934,7 +5941,7 @@ void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, u= nsigned long cr0, =20 /* NPT requires CR0.PG=3D1. */ WARN_ON_ONCE(cpu_role.base.direct || !cpu_role.base.guest_mode); - cpu_role.base.cr4_smep =3D false; + cpu_role.base.cr4_smep =3D (nested_ctl & SVM_NESTED_CTL_GMET_ENABLE) !=3D= 0; =20 root_role =3D cpu_role.base; root_role.level =3D kvm_mmu_get_tdp_level(vcpu); @@ -5992,7 +5999,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, b= ool execonly, context->gva_to_gpa =3D ept_gva_to_gpa; context->sync_spte =3D ept_sync_spte; =20 - update_permission_bitmask(context, true); + update_permission_bitmask(context, true, true); context->pkru_mask =3D 0; reset_rsvds_bits_mask_ept(vcpu, context, execonly, huge_page_level); reset_ept_shadow_zero_bits_mask(context, execonly); diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index b36c33255bed..99edcca7ee64 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -95,7 +95,8 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *= vcpu) */ kvm_init_shadow_npt_mmu(vcpu, X86_CR0_PG, svm->vmcb01.ptr->save.cr4, svm->vmcb01.ptr->save.efer, - svm->nested.ctl.nested_cr3); + svm->nested.ctl.nested_cr3, + svm->nested.ctl.nested_ctl); vcpu->arch.mmu->get_guest_pgd =3D nested_svm_get_tdp_cr3; vcpu->arch.mmu->get_pdptr =3D nested_svm_get_tdp_pdptr; vcpu->arch.mmu->inject_page_fault =3D nested_svm_inject_npf_exit; --=20 2.53.0