From patchwork Fri Jan 5 09:15:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 185322 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp6118643dyb; Fri, 5 Jan 2024 01:45:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHOYvAmUlkrCguFpK8iwUpBxaW3yf+5z36GhDqfuri1qgr3MooZVSxP/BxKue3tB7DtoVSc X-Received: by 2002:a25:a543:0:b0:db7:dad0:76c3 with SMTP id h61-20020a25a543000000b00db7dad076c3mr1776112ybi.95.1704447928320; Fri, 05 Jan 2024 01:45:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704447928; cv=none; d=google.com; s=arc-20160816; b=y4YEyv/gtEJIftwfJ0eRLRAU+zEmK9yDEmM+Efnl0CDRJNWsY6671m1ZjnzRIgtVXw ZpnMo1Di04AMdASfH15pevlZAblcnddRM8VxOfRs0InHfMeSbaR0dOaXEQF+35fANZ93 1YOBXRMndOg0240AuNUL2E+rZs5qyoyrJj9UDmXTILoQmgV0lWuswqPQ2dzYTiRvSKCv 5lhlTsFUn7BY0YaZffghAX+meEzfMvmC7mZeqO0czDJ01fgnuGbMaioyg3KHBcSl+pxt dwZc+j8r8aC6u1TzXsYXbJY+AeOzUuN83rv3nkhsQ1rugy6eB2LM5Br0/9XoSMA7qscQ fiqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:from:dkim-signature; bh=QbAUgoPW97JEnRx+QnmrZtHmf8g+iI6uxO/AlaIYCf8=; fh=YdyUGy0kKu0DsyGpr0tP9PGkupWicnl2+pb1wvQMDbw=; b=IXceenib3YmX2+FJa0dyVbOYhYMd6OP7kvUpEm3oXxYm09hysnbY7Rq2vEmjPAqP8Z TNC2QXMkVxx9iQlfR+RJTwkrIvhqCQK/7k4+JUvaWMWYXYFmNQxGFtYPPKLnXoO3B1so v6NvMbeWLT3m8FfGsXRu+FzV1ziewm9UHraXM9ObCBsTWn5Gi6WChAKNi8UD1TyWHQXW 3LT8CH6T1HXusaKeL7yAWS7GUUV1iAflTMZktPBAoPOit+QqkN4Myme+xOLudbYFdpRl 6rQU9MtWxZOexoYTe33kSflAfkWPoVxBCQXdDqCPLOPtjGzqWtVLqXP7zGhjtc+smG3h QRng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HAs4YsIO; spf=pass (google.com: domain of linux-kernel+bounces-17679-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17679-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f3-20020a0cf3c3000000b0067f1a03a510si1397444qvm.364.2024.01.05.01.45.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 01:45:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17679-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HAs4YsIO; spf=pass (google.com: domain of linux-kernel+bounces-17679-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17679-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 219241C20F7E for ; Fri, 5 Jan 2024 09:45:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B6DA5250FF; Fri, 5 Jan 2024 09:44:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HAs4YsIO" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) (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 B3B6424213; Fri, 5 Jan 2024 09:44:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704447896; x=1735983896; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=dRCZ1S0CO2msFURA90luHjWFusP5YsJ6PzRmy9EYQoE=; b=HAs4YsIOHW+Pid1jcca5wKwo6ctPSfAA65outbCZccaszkdPYOsrdm76 JGPi3y3xfPR5ZMT0D2ybPTPzA8os3obluvbRs38R26EHiclsnV0AUw16D PCdmqiMhTPk86PBHIyEYhEsE7gAlURIa/SuclNem7XRCBtKzh+8OJB+w/ f/cbM8dU4HGnNzXPVQhbHXdIVLOVT2ts76277VWBgYHOy2VBOlbyncKsV 9ajEIQ3iUuTlLpFS1cTbCaxeAob5hXgiT531WiS21gVukJ2pfvccLdgiB JHRK9rqT9iPIw0hukA260h4M/3/XJeB+vnuybrPw867bJpW3UCFU7E0MP g==; X-IronPort-AV: E=McAfee;i="6600,9927,10943"; a="10959399" X-IronPort-AV: E=Sophos;i="6.04,333,1695711600"; d="scan'208";a="10959399" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jan 2024 01:44:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10943"; a="784158177" X-IronPort-AV: E=Sophos;i="6.04,333,1695711600"; d="scan'208";a="784158177" Received: from yzhao56-desk.sh.intel.com ([10.239.159.62]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Jan 2024 01:44:47 -0800 From: Yan Zhao To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Cc: pbonzini@redhat.com, seanjc@google.com, olvaffe@gmail.com, kevin.tian@intel.com, zhiyuan.lv@intel.com, zhenyu.z.wang@intel.com, yongwei.ma@intel.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, gurchetansingh@chromium.org, kraxel@redhat.com, zzyiwei@google.com, ankita@nvidia.com, jgg@nvidia.com, alex.williamson@redhat.com, maz@kernel.org, oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, Yan Zhao , Zhenyu Wang Subject: [PATCH 3/4] KVM: VMX: Honor guest PATs for memslots of flag KVM_MEM_NON_COHERENT_DMA Date: Fri, 5 Jan 2024 17:15:35 +0800 Message-Id: <20240105091535.24760-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240105091237.24577-1-yan.y.zhao@intel.com> References: <20240105091237.24577-1-yan.y.zhao@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787243190683117766 X-GMAIL-MSGID: 1787243190683117766 Honor guest PATs in the range of memslots of flag KVM_MEM_NON_COHERENT_DMA set no matter the value of noncoherent dma count. Just honoring guest PATs (without honoring guest MTRRs) for memslots of flag KVM_MEM_NON_COHERENT_DMA is because - guest OS will ensure no page aliasing issue in guest side by honoring guest MTRRs in guest page table. Combinations like guest MTRR=WC or UC, guest PAT = WB is not allowed. (at least in Linux, see pat_x_mtrr_type()). - guest device driver programs device hardware according to guest PATs in modern platforms. Besides, we don't break down an EPT huge page if guest MTRRs in its range are not consistent, because - guest should have chosen correct guest PATs according to guest MTRRs. - in normal platforms, small guest pages with different PATs must correspond to different TLBs though they are mapped in a huge page in EPT. However, one condition may not be supported well by honoring guest PAT alone -- when guest MTRR=WC, guest PAT=UC-. By honoring guest MTRRs+PATs, the effective memory type is WC; while by honoring guest PATs alone, the effective memory type is UC. But it's arguable to support such a usage. Suggested-by: Sean Christopherson Cc: Kevin Tian Cc: Zhenyu Wang Tested-by: Yongwei Ma Signed-off-by: Yan Zhao --- arch/x86/kvm/vmx/vmx.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 85a23765e506..99f22589fa6d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7600,6 +7600,9 @@ static u8 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio, if (is_mmio) return MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT; + if (slot->flags & KVM_MEM_NON_COHERENT_DMA) + return MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT; + if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) return (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT) | VMX_EPT_IPAT_BIT;