From patchwork Fri Jun 16 02:37:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 108813 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1073990vqr; Thu, 15 Jun 2023 21:03:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5lAK1zxigU0XRnkQzBEMup1Sn3fs+HBv/k656alDTPrb4s2zCrYUSSbhjoV/F2oivmv+U7 X-Received: by 2002:a05:6a20:a125:b0:114:af27:41dd with SMTP id q37-20020a056a20a12500b00114af2741ddmr1326948pzk.34.1686888194197; Thu, 15 Jun 2023 21:03:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686888194; cv=none; d=google.com; s=arc-20160816; b=GMeVypoFTKmnyfw/LMNRvC6xx7X2MIiWf/g5N97vQm0vzFqDwMS5ZNjBgFm6lmi9GJ AVT0RiMNiNV2kLrk8K3xI8rtyoPzMUJZlt1OFrbPNGKS6AFILqckQdxVuMF8PgH05K32 3ggHQFVTFX9EjoCdPSC0P7Fs1fpP5GV0VvMDPo5Wm4vEK2kWjZUgiqBmx/gwZskUCBAV 6IeDEgFY0kc/yzg+IznaKHQnGuFNCvlSJgkyOa+EpVVL22QGp4m+cDngnQSec34llI3d 6qGLFFIZyad9I5EItdSrZ1sqmDB6KO2iYcWMuWy5j2oxKTvSNJ5hwy8wr/RaJFQN9Dlq Ikeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=R3n1GEot05nOcwcu+MAz9apcLO2phCCWui8K3Q36Nrw=; b=MkGz+tmGBJR1hljNg1y7R0kYytHu15oGjsbl4bnvOyt/HTBEvx1I8WX+AHGvmdL/tF L3xZyUPZYddb8aNDcLt933/266tHZBMNnqsFUH8J/Ha7hIgBBjOPRA8LF/sdbMqAQSf4 7Fr1APSi9pEqJVsOjddCDmDc4s3qL0yr/YOXL4T0A91njsfcvfxze9TueRr1ldLkib3q UPalnqFsM6yFhyncYZgvQVMLrEemdeg/qnLFM69hEZ+6nBWNCAU83YyhW4zTVXJMsn2/ G/2rop7uuoeJF5KgHDr5+nSRUJRGD7t5Re4Vp1yIt0cqGU8z4OGWsNtbTbQi8lI2A14y tCng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UXVqXD2t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k2-20020a170902d58200b001b041114a62si10525765plh.355.2023.06.15.21.02.57; Thu, 15 Jun 2023 21:03:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UXVqXD2t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239978AbjFPDCv (ORCPT + 99 others); Thu, 15 Jun 2023 23:02:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241505AbjFPDCe (ORCPT ); Thu, 15 Jun 2023 23:02:34 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EF672D4C; Thu, 15 Jun 2023 20:02:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1686884541; x=1718420541; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=mQjyugDM9ae/+xsV/na3qkdiKTAXgzQ9oL683PGryPA=; b=UXVqXD2tI9XLBeR26o/oTh7rmsvzXwV9xse9p+8HHkFxLjpkwzspqw1l NcCPL5TuDRh8OA9Td07yzA0FHlg/VRAxis1E0Ke8dvjyXyP8yby+u2T0W 8xZovVbcitfFju+5ltpmHyEHo/JTC9ndpALc1wGghyDyIxHEMsJsTGQcR oJ4bWyVZU26Jjtk5rDz5mv7jlgJVTN7QPJpjtThTrt5mZeblDMgTdOdtI G6pIKavuEUNhlKcfzwFQzv3/5IH1+hNzdRFUJUyjmMmoBPjbar5JdsKBO oe9d0kJZLJDxCQd7Le/s42oRCqdreMQ+askczP8yXxOu4zPeyyH8ywhD/ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10742"; a="348809867" X-IronPort-AV: E=Sophos;i="6.00,246,1681196400"; d="scan'208";a="348809867" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jun 2023 20:02:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10742"; a="857213767" X-IronPort-AV: E=Sophos;i="6.00,246,1681196400"; d="scan'208";a="857213767" Received: from yzhao56-desk.sh.intel.com ([10.239.159.62]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jun 2023 20:02:18 -0700 From: Yan Zhao To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, kai.huang@intel.com, robert.hoo.linux@gmail.com, Yan Zhao Subject: [PATCH v3 05/11] KVM: x86/mmu: zap KVM TDP when noncoherent DMA assignment starts/stops Date: Fri, 16 Jun 2023 10:37:09 +0800 Message-Id: <20230616023709.7318-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230616023101.7019-1-yan.y.zhao@intel.com> References: <20230616023101.7019-1-yan.y.zhao@intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768830475120274586?= X-GMAIL-MSGID: =?utf-8?q?1768830475120274586?= Zap KVM TDP when noncoherent DMA assignment starts (noncoherent dma count transitions from 0 to 1) or stops (noncoherent dma count transistions from 1 to 0). Before the zap, test if guest MTRR is to be honored after the assignment starts or was honored before the assignment stops. When there's no noncoherent DMA device, EPT memory type is ((MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT) When there're noncoherent DMA devices, EPT memory type needs to honor guest CR0.CD and MTRR settings. So, if noncoherent DMA count transitions between 0 and 1, EPT leaf entries need to be zapped to clear stale memory type. This issue might be hidden when the device is statically assigned with VFIO adding/removing MMIO regions of the noncoherent DMA devices for several times during guest boot, and current KVM MMU will call kvm_mmu_zap_all_fast() on the memslot removal. But if the device is hot-plugged, or if the guest has mmio_always_on for the device, the MMIO regions of it may only be added for once, then there's no path to do the EPT entries zapping to clear stale memory type. Therefore do the EPT zapping when noncoherent assignment starts/stops to ensure stale entries cleaned away. Signed-off-by: Yan Zhao --- arch/x86/kvm/x86.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6693daeb5686..ac9548efa76f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13164,15 +13164,31 @@ bool noinstr kvm_arch_has_assigned_device(struct kvm *kvm) } EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); +static void kvm_noncoherent_dma_assignment_start_or_stop(struct kvm *kvm) +{ + /* + * Non-coherent DMA assignement and de-assignment will affect + * whether KVM honors guest MTRRs and cause changes in memtypes + * in TDP. + * So, specify the second parameter as true here to indicate + * non-coherent DMAs are/were involved and TDP zap might be + * necessary. + */ + if (__kvm_mmu_honors_guest_mtrrs(kvm, true)) + kvm_zap_gfn_range(kvm, gpa_to_gfn(0), gpa_to_gfn(~0ULL)); +} + void kvm_arch_register_noncoherent_dma(struct kvm *kvm) { - atomic_inc(&kvm->arch.noncoherent_dma_count); + if (atomic_inc_return(&kvm->arch.noncoherent_dma_count) == 1) + kvm_noncoherent_dma_assignment_start_or_stop(kvm); } EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) { - atomic_dec(&kvm->arch.noncoherent_dma_count); + if (!atomic_dec_return(&kvm->arch.noncoherent_dma_count)) + kvm_noncoherent_dma_assignment_start_or_stop(kvm); } EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);