From nobody Wed Apr 15 00:01:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12034C3F6B0 for ; Thu, 28 Jul 2022 22:18:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232505AbiG1WSQ (ORCPT ); Thu, 28 Jul 2022 18:18:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232022AbiG1WSF (ORCPT ); Thu, 28 Jul 2022 18:18:05 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BD3378DFF for ; Thu, 28 Jul 2022 15:18:04 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id z15-20020a170903018f00b0016d6e7a043dso1876702plg.12 for ; Thu, 28 Jul 2022 15:18:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:from:to:cc; bh=pMK326Dzz47oLnHyTt02WlUweZBXPrgublIXKnjlXLs=; b=IvZ6eY8kvJcRYMajPsxpgxvKuckIcFz2KmQGWJT3C+CW0DMU6PL4Bzlo/ENRyL7+Eh G4XPhKndip03jPsiYTgmDx7Nqkrir7S6G7+PU38jq9t0OfJMV/WtPrODlMQLvyxUTufx 5TMmb2kdBBkjAgG77RIctTQrYXKdbBvxxRYbJ/KWqNRuVVucT4ygC5ft/NB48/1XcDKa SmXFcw84BJD/PCg2WMCvUY04QtzHYuURIkkpCB1B9Xs0pW0mv+XsLjMy+Y1kcu+sFxCL 6JKgb7FNFJ/iSDx1rtTBO7UjxPqWQhDVHFDNm2ErVESeA5KRhqkS6eatTOc6YLOUkhR0 nznQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=pMK326Dzz47oLnHyTt02WlUweZBXPrgublIXKnjlXLs=; b=NNGj54B00zPpHiRFucQqaHbhh4icnUI7m0qwQAREx0nI+IP37ruMmLRp9DBurz9MBV tLYgpoxXP4kF4nIKaJmvtH2Z1sRworti3nPPvO4FCEX1RJdKWAuoOl1Fc23Fu7p59jUv 0FYsrvFhPJx1XO+0P7FCOtpehuPiYzYlA7d/ZMDPB/7JVfvEu2Mj1zr5iR+qpWS6IKVw ctRNQuopGIjzz/vXRwlUvqB/+MkZkQhXP3l5QpcvvYZN80QRy3p+5ehrzNCRqUD+0DIa qdcXKgl39v82VL3xWU5IZITIAn29NFoDdpPet+DPKM9AgP7RRXdGspsH3BcDJpcdE0ve sMgA== X-Gm-Message-State: ACgBeo0wWOJ3A2pbDV0dMWdUdmFYRcXiHtloXVtJrv9F4lkOzfkd3WGH ihxFvGoBMPr4hoLglPsftPC2wWXvYyw= X-Google-Smtp-Source: AA6agR5J0r9RC8k4KPKoyp4aR5OSquWQ0jV/zZ0nJrOMiCYTbB6yzXApQ30OuliGZc5Et0WralgBhkcYjrM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:1d90:b0:1f2:5f47:ca6c with SMTP id pf16-20020a17090b1d9000b001f25f47ca6cmr1522492pjb.162.1659046683830; Thu, 28 Jul 2022 15:18:03 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 28 Jul 2022 22:17:56 +0000 In-Reply-To: <20220728221759.3492539-1-seanjc@google.com> Message-Id: <20220728221759.3492539-2-seanjc@google.com> Mime-Version: 1.0 References: <20220728221759.3492539-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH 1/4] KVM: x86: Tag kvm_mmu_x86_module_init() with __init From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Roth , Tom Lendacky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mark kvm_mmu_x86_module_init() with __init, the entire reason it exists is to initialize variables when kvm.ko is loaded, i.e. it must never be called after module initialization. Fixes: 1d0e84806047 ("KVM: x86/mmu: Resolve nx_huge_pages when kvm.ko is lo= aded") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Tested-by: Michael Roth --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index e8281d64a431..5ffa578cafe1 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1704,7 +1704,7 @@ static inline int kvm_arch_flush_remote_tlb(struct kv= m *kvm) #define kvm_arch_pmi_in_guest(vcpu) \ ((vcpu) && (vcpu)->arch.handling_intr_from_guest) =20 -void kvm_mmu_x86_module_init(void); +void __init kvm_mmu_x86_module_init(void); int kvm_mmu_vendor_module_init(void); void kvm_mmu_vendor_module_exit(void); =20 diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 8e477333a263..2975fcb14c86 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6700,7 +6700,7 @@ static int set_nx_huge_pages(const char *val, const s= truct kernel_param *kp) * nx_huge_pages needs to be resolved to true/false when kvm.ko is loaded,= as * its default value of -1 is technically undefined behavior for a boolean. */ -void kvm_mmu_x86_module_init(void) +void __init kvm_mmu_x86_module_init(void) { if (nx_huge_pages =3D=3D -1) __set_nx_huge_pages(get_nx_auto_mode()); --=20 2.37.1.455.g008518b4e5-goog From nobody Wed Apr 15 00:01:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1769DC04A68 for ; Thu, 28 Jul 2022 22:18:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232535AbiG1WSV (ORCPT ); Thu, 28 Jul 2022 18:18:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231845AbiG1WSM (ORCPT ); Thu, 28 Jul 2022 18:18:12 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F69785AB for ; Thu, 28 Jul 2022 15:18:05 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-31dfe25bd47so27116867b3.18 for ; Thu, 28 Jul 2022 15:18:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:from:to:cc; bh=2h7quCwA+U8RPYleefIstCQInHVnFoLtGc6jewkyotU=; b=GI+DN4SdbIj1EzFV04CUuXGNvjR0s9JzqDD2uP+mNHbJlTeyrLffiGdDKVcd6y8RyC YPUG6jowPUQnpME6OXH36dsJcEN3mawmcalBKcR/7AXxVpf0e9ka+NKtwBVbl7T9Gwwc nOOiNLWH7RHs9eRt7L5XaTbva2h0ldHF2iYmyprZ0Nf+ulyq7eJm7rTxF7CmJDQ5oIld mrqWq1e5QkjcnHmK5eY7t/5MPlj5ErQP7/XjuOrx1NrkS8xJZtMIWHSDry3qhRx/XgDq i2mcMtjnYGc/SRQUBQNJx3LyPbe8aodC1byanFyCriDYIzYDOi7Ask175BzzNbuhLQhD b8SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=2h7quCwA+U8RPYleefIstCQInHVnFoLtGc6jewkyotU=; b=t3GVXcTg8E3yvdz9ZFfXjX//CE/DdEchUnSXAt0e6lYcmPe2kTf8AqHLRKmyeaXPfC dKdxUE9IrbxpamNg6DmVFbtTGn3UpH3U3/e8aKhiyf2XuW6sTFNUMztRD8q2kG44OZG3 gJnXB9iXCXVMDXIq3rAEPPpdIjBd27qlxvkO63LJH5BCkNkEOsn4RBwLbXQrchRGmSRq RR68Ltc+aHYzWNx+zj0d++gO6aozjafFbYl4tK4i/xtguWqY4kJguXjJQpj4e7KQFSyI Y0s2CMcN8C9HAMScfbtUQE6AOTZvTHBQeSAP5gT5AZXXeEGB93rTaCp7mvqYdJTcht60 J6Zw== X-Gm-Message-State: ACgBeo0Sr0YOLidEn7jCcsKtRi1iQbNzSBXR4m7YSfZVhyswGs2K3NAe USns4Z+Gljk+movIVsF7q+KhhrK8nqo= X-Google-Smtp-Source: AA6agR59D4S6bJffvnjXS+SAAFZFxnspfNvlSkUHdfP3Tc7+DuOCTC1os453r0qtOQZO0/XOzmx/6KM672E= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:4a42:0:b0:31e:9954:ff83 with SMTP id x63-20020a814a42000000b0031e9954ff83mr798277ywa.82.1659046685202; Thu, 28 Jul 2022 15:18:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 28 Jul 2022 22:17:57 +0000 In-Reply-To: <20220728221759.3492539-1-seanjc@google.com> Message-Id: <20220728221759.3492539-3-seanjc@google.com> Mime-Version: 1.0 References: <20220728221759.3492539-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH 2/4] KVM: x86/mmu: Fully re-evaluate MMIO caching when SPTE masks change From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Roth , Tom Lendacky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Fully re-evaluate whether or not MMIO caching can be enabled when SPTE masks change; simply clearing enable_mmio_caching when a configuration isn't compatible with caching fails to handle the scenario where the masks are updated, e.g. by VMX for EPT or by SVM to account for the C-bit location, and toggle compatibility from false=3D>true. Snapshot the original module param so that re-evaluating MMIO caching preserves userspace's desire to allow caching. Use a snapshot approach so that enable_mmio_caching still reflects KVM's actual behavior. Fixes: 8b9e74bfbf8c ("KVM: x86/mmu: Use enable_mmio_caching to track if MMI= O caching is enabled") Reported-by: Michael Roth Cc: Tom Lendacky Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Tested-by: Michael Roth --- arch/x86/kvm/mmu/mmu.c | 4 ++++ arch/x86/kvm/mmu/spte.c | 19 +++++++++++++++++++ arch/x86/kvm/mmu/spte.h | 1 + 3 files changed, 24 insertions(+) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2975fcb14c86..660f58928252 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6699,11 +6699,15 @@ static int set_nx_huge_pages(const char *val, const= struct kernel_param *kp) /* * nx_huge_pages needs to be resolved to true/false when kvm.ko is loaded,= as * its default value of -1 is technically undefined behavior for a boolean. + * Forward the module init call to SPTE code so that it too can handle mod= ule + * params that need to be resolved/snapshot. */ void __init kvm_mmu_x86_module_init(void) { if (nx_huge_pages =3D=3D -1) __set_nx_huge_pages(get_nx_auto_mode()); + + kvm_mmu_spte_module_init(); } =20 /* diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index 7314d27d57a4..66f76f5a15bd 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -20,6 +20,7 @@ #include =20 bool __read_mostly enable_mmio_caching =3D true; +static bool __ro_after_init allow_mmio_caching; module_param_named(mmio_caching, enable_mmio_caching, bool, 0444); =20 u64 __read_mostly shadow_host_writable_mask; @@ -43,6 +44,18 @@ u64 __read_mostly shadow_nonpresent_or_rsvd_lower_gfn_ma= sk; =20 u8 __read_mostly shadow_phys_bits; =20 +void __init kvm_mmu_spte_module_init(void) +{ + /* + * Snapshot userspace's desire to allow MMIO caching. Whether or not + * KVM can actually enable MMIO caching depends on vendor-specific + * hardware capabilities and other module params that can't be resolved + * until the vendor module is loaded, i.e. enable_mmio_caching can and + * will change when the vendor module is (re)loaded. + */ + allow_mmio_caching =3D enable_mmio_caching; +} + static u64 generation_mmio_spte_mask(u64 gen) { u64 mask; @@ -340,6 +353,12 @@ void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mm= io_mask, u64 access_mask) BUG_ON((u64)(unsigned)access_mask !=3D access_mask); WARN_ON(mmio_value & shadow_nonpresent_or_rsvd_lower_gfn_mask); =20 + /* + * Reset to the original module param value to honor userspace's desire + * to (dis)allow MMIO caching. Update the param itself so that + * userspace can see whether or not KVM is actually using MMIO caching. + */ + enable_mmio_caching =3D allow_mmio_caching; if (!enable_mmio_caching) mmio_value =3D 0; =20 diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index cabe3fbb4f39..26b144ffd146 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -450,6 +450,7 @@ static inline u64 restore_acc_track_spte(u64 spte) =20 u64 kvm_mmu_changed_pte_notifier_make_spte(u64 old_spte, kvm_pfn_t new_pfn= ); =20 +void __init kvm_mmu_spte_module_init(void); void kvm_mmu_reset_all_pte_masks(void); =20 #endif --=20 2.37.1.455.g008518b4e5-goog From nobody Wed Apr 15 00:01:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0CAECC04A68 for ; Thu, 28 Jul 2022 22:18:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232852AbiG1WSa (ORCPT ); Thu, 28 Jul 2022 18:18:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232478AbiG1WSQ (ORCPT ); Thu, 28 Jul 2022 18:18:16 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E00BC7968F for ; Thu, 28 Jul 2022 15:18:07 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31ea3f0e357so27201587b3.16 for ; Thu, 28 Jul 2022 15:18:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:from:to:cc; bh=dlAQ25g7cHOrtPCAbZedzQqF/rNgEGei+alqD2VO+U0=; b=ranUuoK5voHAwixMI/x/m9wejbxqlpuz1JUNeOylf+rBUm8WJMT/OqkOdy1q5Eb6Dr 3JR7xS4Cb91uCyZl/BnVBFA6ZBZ70u24gdmu35wwVkYOEXskLd7TSLZ56bymQ3DBRgIv fgHzaKbKOA1SJTw0JcEm0LOA8ZuLsWaDMweTh2vUz4LgEPmmqcSkKNi59UYoyEP8Kchn 6198Ja4Yxa/LKkTkcbmKL0dK0mfdD/moAs4VaaEnOeIGscD+r31xHr+OxH4CJ3UxRlPt 5nzqwOmitScFftivZL4DtMG1BVSQ+cHmrF40SEyqoe/3E5VGH4fRXYJt6nWGRAubgjPK 7Eeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=dlAQ25g7cHOrtPCAbZedzQqF/rNgEGei+alqD2VO+U0=; b=AXmo/1yYE3yKjDOo81WtFYM3Ik69p87+ntkQHmQDrEzwa54+RfuR7VtwuBs3rAzlSH fEBTCCt0ug48iNjTfTVz4m/J9hqjheAaYYqdQ92yaiQKrZgf1+7vudSdWXy2yDiuWT7D mS/JovbzhcU5+U83ByWIFEdLlFbQQm5ZH360Yg1dGpQs7ShtF0kgzDR3v1d1zlb0ISkM SdPFH2LrJyOEXxFrt2tzUDRc+YxwTc7mAVfX1PDsTHgRejoiv/si1lQ64wWkjmYDDMEo /7JQr5CPwNLdqGm49OcWoP/OxM+lAEYOi8Ajha/R+N2EY6eEI/sBJ8EO266Aw7Ol0y3G JYWQ== X-Gm-Message-State: ACgBeo1fJBFyqFDw34VBe9jSDSUXqpkDeljh6RDaJIqeNtjv0rZWjPTg 4AwP/1Rx9ms2/I9GzSdRVOyMW1kkPHY= X-Google-Smtp-Source: AA6agR4I1F5vmYYMNMXJj9swMHJA4N7Yx/Yl22BHI4TcelA/kfsehihejzDsmTQCFZAm5dVxzayZxDkP9LQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:2613:0:b0:671:810e:8e75 with SMTP id m19-20020a252613000000b00671810e8e75mr575929ybm.625.1659046687139; Thu, 28 Jul 2022 15:18:07 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 28 Jul 2022 22:17:58 +0000 In-Reply-To: <20220728221759.3492539-1-seanjc@google.com> Message-Id: <20220728221759.3492539-4-seanjc@google.com> Mime-Version: 1.0 References: <20220728221759.3492539-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH 3/4] KVM: SVM: Adjust MMIO masks (for caching) before doing SEV(-ES) setup From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Roth , Tom Lendacky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Adjust KVM's MMIO masks to account for the C-bit location prior to doing SEV(-ES) setup. A future patch will consume enable_mmio caching during SEV setup as SEV-ES _requires_ MMIO caching, i.e. KVM needs to disallow SEV-ES if MMIO caching is disabled. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Tested-by: Michael Roth --- arch/x86/kvm/svm/svm.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index aef63aae922d..62e89db83bc1 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5034,13 +5034,16 @@ static __init int svm_hardware_setup(void) /* Setup shadow_me_value and shadow_me_mask */ kvm_mmu_set_me_spte_mask(sme_me_mask, sme_me_mask); =20 - /* Note, SEV setup consumes npt_enabled. */ + svm_adjust_mmio_mask(); + + /* + * Note, SEV setup consumes npt_enabled and enable_mmio_caching (which + * may be modified by svm_adjust_mmio_mask()). + */ sev_hardware_setup(); =20 svm_hv_hardware_setup(); =20 - svm_adjust_mmio_mask(); - for_each_possible_cpu(cpu) { r =3D svm_cpu_init(cpu); if (r) --=20 2.37.1.455.g008518b4e5-goog From nobody Wed Apr 15 00:01:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74E82C04A68 for ; Thu, 28 Jul 2022 22:18:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232308AbiG1WSZ (ORCPT ); Thu, 28 Jul 2022 18:18:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232316AbiG1WSN (ORCPT ); Thu, 28 Jul 2022 18:18:13 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33BDA79695 for ; Thu, 28 Jul 2022 15:18:09 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id n8-20020a170902e54800b0016d4dffb9d6so1880673plf.17 for ; Thu, 28 Jul 2022 15:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:from:to:cc; bh=RPzTmS/0r8A1CyXZM2Rci6TWGc35txpn0qeX6XuTsqs=; b=qHhsT9hs16IF/EHMDyGRcUT6ZEGIKVqDhn0BuFZVNej7rTgNck6aieiRMk+e3jn79j tdzfk/AMZvzu05J19+IxR2qC+FvHnu6AzWYspgHXLYA5QuN3v/BhEc3ZMGVNoBWqQbCY 2gYo2Se6S8YTikr/r/lEI5p7OtL/OhMPnYSmzQe52Y7h6Ju1stuM6HVVS4h35sfrVgKW nLexqMIzeEa8H4Rizy3jGkbl/LcbM2CLCtj3vv6PrYLTk4JDV5MItVwmBra5vrEhBfOx gZTp7ubiZ2ZSn1KNAVEXBxWZ/t8GpVCMwy/nYp2IBIu0jCLosgvMB3Kl1qHgAuucx6oe S5vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc; bh=RPzTmS/0r8A1CyXZM2Rci6TWGc35txpn0qeX6XuTsqs=; b=NfiN9PdDD7yxbMfRN9tL/GHmfYkq2IhiQLcFxwWxXCEGVfba4BRUek3tT9BW+EE6Y8 NF+Zqu2B0yRB1tB+Qw4tsCAf4ADbUlqZpy71zF+KzGIhU4Yme6QF1v9dc9OWNf6Wbw4J MDr+GhcnawNLaq5azkCxrcUBIEUVTOXpF0Vw5mIJymkXp8nW/oylCqQrRs3FY0fnOz3T J1XO4dzAz0sv8LKotMcBVJAsZsN9JtTN+4rpJifU70pLOuC80BkcmgzmGk2WkVJtGjaP k+KVZ49yossusb+9rdw19K85J1mZS5ye1RBUhaHlEFXU15w/moT4UWlhudMWErUvrXSn 3FgQ== X-Gm-Message-State: AJIora+x+e+OcNPLpbcEInKjoHBCdQQFY0bE4pGqFR25hrzmfFrpvpXN qOAj0qxBVjzLwur5/UlJjjcln1qvbfA= X-Google-Smtp-Source: AGRyM1sIcKz8IHgdc4YVb7+rQA93yxD8RqktXwO2yoYSVwKQ/47pl+miOoDUZ1Cy3wmRrZAVPHNga7rX/k0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:fd4f:0:b0:41b:539b:ba8e with SMTP id m15-20020a63fd4f000000b0041b539bba8emr638124pgj.17.1659046688633; Thu, 28 Jul 2022 15:18:08 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 28 Jul 2022 22:17:59 +0000 In-Reply-To: <20220728221759.3492539-1-seanjc@google.com> Message-Id: <20220728221759.3492539-5-seanjc@google.com> Mime-Version: 1.0 References: <20220728221759.3492539-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.455.g008518b4e5-goog Subject: [PATCH 4/4] KVM: SVM: Disable SEV-ES support if MMIO caching is disable From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Michael Roth , Tom Lendacky Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Disable SEV-ES if MMIO caching is disabled as SEV-ES relies on MMIO SPTEs generating #NPF(RSVD), which are reflected by the CPU into the guest as a #VC. With SEV-ES, the untrusted host, a.k.a. KVM, doesn't have access to the guest instruction stream or register state and so can't directly emulate in response to a #NPF on an emulated MMIO GPA. Disabling MMIO caching means guest accesses to emulated MMIO ranges cause #NPF(!PRESENT), and those flavors of #NPF cause automatic VM-Exits, not #VC. Fixes: b09763da4dd8 ("KVM: x86/mmu: Add module param to disable MMIO cachin= g (for testing)") Reported-by: Michael Roth Cc: Tom Lendacky Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Tested-by: Michael Roth --- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/mmu/spte.c | 1 + arch/x86/kvm/mmu/spte.h | 2 -- arch/x86/kvm/svm/sev.c | 10 ++++++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index a99acec925eb..6bdaacb6faa0 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -6,6 +6,8 @@ #include "kvm_cache_regs.h" #include "cpuid.h" =20 +extern bool __read_mostly enable_mmio_caching; + #define PT_WRITABLE_SHIFT 1 #define PT_USER_SHIFT 2 =20 diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c index 66f76f5a15bd..03ca740bf721 100644 --- a/arch/x86/kvm/mmu/spte.c +++ b/arch/x86/kvm/mmu/spte.c @@ -22,6 +22,7 @@ bool __read_mostly enable_mmio_caching =3D true; static bool __ro_after_init allow_mmio_caching; module_param_named(mmio_caching, enable_mmio_caching, bool, 0444); +EXPORT_SYMBOL_GPL(enable_mmio_caching); =20 u64 __read_mostly shadow_host_writable_mask; u64 __read_mostly shadow_mmu_writable_mask; diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index 26b144ffd146..9a9414b8d1d6 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -5,8 +5,6 @@ =20 #include "mmu_internal.h" =20 -extern bool __read_mostly enable_mmio_caching; - /* * A MMU present SPTE is backed by actual memory and may or may not be pre= sent * in hardware. E.g. MMIO SPTEs are not considered present. Use bit 11, = as it diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 309bcdb2f929..05bf6301acac 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -22,6 +22,7 @@ #include #include =20 +#include "mmu.h" #include "x86.h" #include "svm.h" #include "svm_ops.h" @@ -2205,6 +2206,15 @@ void __init sev_hardware_setup(void) if (!sev_es_enabled) goto out; =20 + /* + * SEV-ES requires MMIO caching as KVM doesn't have access to the guest + * instruction stream, i.e. can't emulate in response to a #NPF and + * instead relies on #NPF(RSVD) being reflected into the guest as #VC + * (the guest can then do a #VMGEXIT to request MMIO emulation). + */ + if (!enable_mmio_caching) + goto out; + /* Does the CPU support SEV-ES? */ if (!boot_cpu_has(X86_FEATURE_SEV_ES)) goto out; --=20 2.37.1.455.g008518b4e5-goog