From patchwork Thu Aug 29 08:49:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 11120663 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A041A1398 for ; Thu, 29 Aug 2019 08:51:41 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7CBA12189D for ; Thu, 29 Aug 2019 08:51:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=citrix.com header.i=@citrix.com header.b="EnPlAi5e" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7CBA12189D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=citrix.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i3G7m-0003Ea-U5; Thu, 29 Aug 2019 08:49:46 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1i3G7l-0003EV-CD for xen-devel@lists.xenproject.org; Thu, 29 Aug 2019 08:49:45 +0000 X-Inumbo-ID: f21b9824-ca39-11e9-8980-bc764e2007e4 Received: from esa4.hc3370-68.iphmx.com (unknown [216.71.155.144]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f21b9824-ca39-11e9-8980-bc764e2007e4; Thu, 29 Aug 2019 08:49:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1567068583; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=sKcC0gdvsWkZJPlBBQsuSypnUnY1Dt1H043GO1Ab1d0=; b=EnPlAi5eTQ/SwcQlC8GHR41W9enxEjHtk0XyJ7NVuL6Jpq8Bm2UTcJpI FFnTQU8iGeftMHB+BJaoX/QCB8xf6wkkSPhV+i9dMFOPjll37Ao6ZRAb4 6qIpBk2QTea2hWVKSAxsacUTSy6wpTB/W1EjK6mKXHxCiUQ/qZWPw5zYq 4=; Authentication-Results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=roger.pau@citrix.com; spf=Pass smtp.mailfrom=roger.pau@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of roger.pau@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa4.hc3370-68.iphmx.com: domain of roger.pau@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="roger.pau@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa4.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa4.hc3370-68.iphmx.com; envelope-from="roger.pau@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: O7WbXWYFO8sCxbJ43toiIh3ttS695yB3aXUJ2Yno56DsZnvErS0/ggKFUwzAOVWjh/l0j8JHYx oRffJz8WuxDtV36VungRzMFmjfxiZon9uiP8wK0nqG1MtrMeNU8D2vHFH532iJx+R6r8tVpoo+ gMgFXsW6yvT67QOdP2VjPmPaGLKeQlkLWdb3YT0jyV98/w/MmLvUWt8pbZHnwFY+ZXu5TYMgtL eCq27zzRLRHKZusXOJ/M6xbFfaFpJY7GCAoSfkFXDNxMKVpxYpJgcl6NdaNdtZZ2b67HSO4tsv X2E= X-SBRS: 2.7 X-MesageID: 5134594 X-Ironport-Server: esa4.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.64,442,1559534400"; d="scan'208";a="5134594" From: Roger Pau Monne To: Date: Thu, 29 Aug 2019 10:49:18 +0200 Message-ID: <20190829084918.89608-1-roger.pau@citrix.com> X-Mailer: git-send-email 2.22.0 MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2] Partially revert "x86/mm: Clean IOMMU flags from p2m-pt code" X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Wei Liu , George Dunlap , Andrew Cooper , Jan Beulich , Alexandru Stefan ISAILA , Roger Pau Monne Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" This partially reverts commit 854a49a7486a02edae5b3e53617bace526e9c1b1 by re-adding the logic that propagates changes to the domain physmap done by p2m_pt_set_entry into the iommu page tables. Without this logic changes to the guest physmap are not propagated to the iommu, leaving stale iommu entries that can leak data, or failing to add new entries. Note that this commit doesn't re-introduce iommu flags to the cpu page table entries, since the logic to add/remove entries to the iommu page tables is based on the p2m type and the mfn. Fixes: 854a49a7486a02 ('x86/mm: Clean IOMMU flags from p2m-pt code') Signed-off-by: Roger Pau Monné Reviewed-by: Jan Beulich Reviewed-by: Alexandru Isaila Acked-by: George Dunlap --- Cc: Alexandru Stefan ISAILA --- Changes since v1: - Remove the share-pt branch, there's no sharing on AMD hardware. --- xen/arch/x86/mm/p2m-pt.c | 42 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/mm/p2m-pt.c b/xen/arch/x86/mm/p2m-pt.c index 3a0a500d66..8483dc8142 100644 --- a/xen/arch/x86/mm/p2m-pt.c +++ b/xen/arch/x86/mm/p2m-pt.c @@ -508,7 +508,18 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, l2_pgentry_t l2e_content; l3_pgentry_t l3e_content; int rc; - unsigned int flags; + unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt, mfn); + /* + * old_mfn and iommu_old_flags control possible flush/update needs on the + * IOMMU: We need to flush when MFN or flags (i.e. permissions) change. + * iommu_old_flags being initialized to zero covers the case of the entry + * getting replaced being a non-present (leaf or intermediate) one. For + * present leaf entries the real value will get calculated below, while + * for present intermediate entries ~0 (guaranteed != iommu_pte_flags) + * will be used (to cover all cases of what the leaf entries underneath + * the intermediate one might be). + */ + unsigned int flags, iommu_old_flags = 0; unsigned long old_mfn = mfn_x(INVALID_MFN); if ( !sve ) @@ -556,9 +567,17 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, if ( flags & _PAGE_PRESENT ) { if ( flags & _PAGE_PSE ) + { old_mfn = l1e_get_pfn(*p2m_entry); + iommu_old_flags = + p2m_get_iommu_flags(p2m_flags_to_type(flags), + _mfn(old_mfn)); + } else + { + iommu_old_flags = ~0; intermediate_entry = *p2m_entry; + } } check_entry(mfn, p2mt, p2m_flags_to_type(flags), page_order); @@ -594,6 +613,9 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, 0, L1_PAGETABLE_ENTRIES); ASSERT(p2m_entry); old_mfn = l1e_get_pfn(*p2m_entry); + iommu_old_flags = + p2m_get_iommu_flags(p2m_flags_to_type(l1e_get_flags(*p2m_entry)), + _mfn(old_mfn)); if ( mfn_valid(mfn) || p2m_allows_invalid_mfn(p2mt) ) entry_content = p2m_l1e_from_pfn(mfn_x(mfn), @@ -617,9 +639,17 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, if ( flags & _PAGE_PRESENT ) { if ( flags & _PAGE_PSE ) + { old_mfn = l1e_get_pfn(*p2m_entry); + iommu_old_flags = + p2m_get_iommu_flags(p2m_flags_to_type(flags), + _mfn(old_mfn)); + } else + { + iommu_old_flags = ~0; intermediate_entry = *p2m_entry; + } } check_entry(mfn, p2mt, p2m_flags_to_type(flags), page_order); @@ -640,9 +670,17 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, && (gfn + (1UL << page_order) - 1 > p2m->max_mapped_pfn) ) p2m->max_mapped_pfn = gfn + (1UL << page_order) - 1; + if ( need_iommu_pt_sync(p2m->domain) && + (iommu_old_flags != iommu_pte_flags || old_mfn != mfn_x(mfn)) ) + rc = iommu_pte_flags ? + iommu_legacy_map(d, _dfn(gfn), mfn, page_order, + iommu_pte_flags) : + iommu_legacy_unmap(d, _dfn(gfn), page_order); + /* * Free old intermediate tables if necessary. This has to be the - * last thing we do so as to avoid a potential use-after-free. + * last thing we do, after removal from the IOMMU tables, so as to + * avoid a potential use-after-free. */ if ( l1e_get_flags(intermediate_entry) & _PAGE_PRESENT ) p2m_free_entry(p2m, &intermediate_entry, page_order);