From nobody Sat Nov 30 06:49:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1613313329; cv=none; d=zohomail.com; s=zohoarc; b=kp72iN/Q1TcTSstZA6Trardvx0Oru003FNOzsyv/auyGnyaN3EYg8QhheGDBZv3YHYYb4BIkjb1CvLuiolGSQa13AklDmMPWbb2C6gbbFyw3maPQTx/1wGqGDT2LbQeAYfoID0vX7KCZYTpecsv91TXDLvqmIrsNGlUffa3mSx4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613313329; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=m/LlNL0TamjvVVErh/pPovK2MwMDVTxbRIY3ju8BM/k=; b=Xmc25akNHj57ujchiQhI4MHYe5aopa5OgjVwzrgGi+CTxHGu4aF8aSJJi+ELjaWpc3mjnJ2/NlwOCbP09xYzFRGbAxUaoSvHyNKHLcJLUHlGms10OkG3WS1OdrawRwYn8UOhoI43EgmimUIyo9PQ+TVncI/97sdbr8m24ocBtqM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1613313326355153.70914557484684; Sun, 14 Feb 2021 06:35:26 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.84920.159186 (Exim 4.92) (envelope-from ) id 1lBIUQ-0007Br-Db; Sun, 14 Feb 2021 14:35:10 +0000 Received: by outflank-mailman (output) from mailman id 84920.159186; Sun, 14 Feb 2021 14:35:10 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lBIUQ-0007Bk-AU; Sun, 14 Feb 2021 14:35:10 +0000 Received: by outflank-mailman (input) for mailman id 84920; Sun, 14 Feb 2021 14:35:08 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lBIUO-0007Be-Q4 for xen-devel@lists.xenproject.org; Sun, 14 Feb 2021 14:35:08 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lBIUO-0004DK-9b; Sun, 14 Feb 2021 14:35:08 +0000 Received: from 54-240-197-235.amazon.com ([54.240.197.235] helo=ufe34d9ed68d054.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lBIUN-00057D-WE; Sun, 14 Feb 2021 14:35:08 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Message-Id:Date:Subject:Cc:To:From; bh=m/LlNL0TamjvVVErh/pPovK2MwMDVTxbRIY3ju8BM/k=; b=4DbgDOufF2y1YcJUTFqmEFkZkM APBxrcLN5LfRljx07JdtnDU0WjYWeC11Cr2DMH+Xha6Ru0FegqY3c6RJHHCJW7LUqamltDQ9YgAso rigAa87oBUkROeFUb4djd9/CcFPFLVa2ZMqCCrdmrMdBrpEXkY3JT/BjKasggs6Iu+AI=; From: Julien Grall To: xen-devel@lists.xenproject.org Cc: julien@xen.org, Julien Grall , Stefano Stabellini , Volodymyr Babchuk , Rahul Singh Subject: [PATCH] xen/iommu: arm: Don't insert an IOMMU mapping when the grantee and granter... Date: Sun, 14 Feb 2021 14:35:04 +0000 Message-Id: <20210214143504.23099-1-julien@xen.org> X-Mailer: git-send-email 2.17.1 X-ZohoMail-DKIM: pass (identity @xen.org) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Julien Grall ... are the same. When the IOMMU is enabled and the domain is direct mapped (e.g. Dom0), Xen will insert a 1:1 mapping for each grant mapping in the P2M to allow DMA. This works quite well when the grantee and granter and not the same because the GFN in the P2M should not be mapped. However, if they are the same, we will overwrite the mapping. Worse, it will be completely removed when the grant is unmapped. As the domain is direct mapped, a 1:1 mapping should always present in the P2M. This is not 100% guaranteed if the domain decides to mess with the P2M. However, such domain would already end up in trouble as the page would be soon be freed (when the last reference dropped). Add an additional check in arm_iommu_{,un}map_page() to check whether the page belongs to the domain. If it is belongs to it, then ignore the request. Note that we don't need to hold an extra reference on the page because the grant code will already do it for us. Reported-by: Rahul Singh Fixes: 552710b38863 ("xen/arm: grant: Add another entry to map MFN 1:1 in d= om0 p2m") Signed-off-by: Julien Grall Reviewed-by: Rahul Singh Tested-by: Rahul Singh --- This is a candidate for 4.15. Without the patch, it would not be possible to get the frontend and backend in the same domain (useful when trying to map the guest block device in dom0). --- xen/drivers/passthrough/arm/iommu_helpers.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/xen/drivers/passthrough/arm/iommu_helpers.c b/xen/drivers/pass= through/arm/iommu_helpers.c index a36e2b8e6c42..35a813308b8c 100644 --- a/xen/drivers/passthrough/arm/iommu_helpers.c +++ b/xen/drivers/passthrough/arm/iommu_helpers.c @@ -53,6 +53,17 @@ int __must_check arm_iommu_map_page(struct domain *d, df= n_t dfn, mfn_t mfn, =20 t =3D (flags & IOMMUF_writable) ? p2m_iommu_map_rw : p2m_iommu_map_ro; =20 + /* + * The granter and grantee can be the same domain. In normal + * condition, the 1:1 mapping should already present in the P2M so + * we want to avoid overwriting it. + * + * Note that there is no guarantee the 1:1 mapping will be present + * if the domain decides to mess with the P2M. + */ + if ( page_get_owner(mfn_to_page(mfn)) =3D=3D d ) + return 0; + /* * The function guest_physmap_add_entry replaces the current mapping * if there is already one... @@ -71,6 +82,13 @@ int __must_check arm_iommu_unmap_page(struct domain *d, = dfn_t dfn, if ( !is_domain_direct_mapped(d) ) return -EINVAL; =20 + /* + * When the granter and grantee are the same, we didn't insert a + * mapping. So ignore the unmap request. + */ + if ( page_get_owner(mfn_to_page(_mfn(dfn_x(dfn)))) =3D=3D d ) + return 0; + return guest_physmap_remove_page(d, _gfn(dfn_x(dfn)), _mfn(dfn_x(dfn))= , 0); } =20 --=20 2.17.1