From nobody Sat May 4 07:14:27 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1651947603; cv=none; d=zohomail.com; s=zohoarc; b=CPo+Tm5xlIZwLUJ9FNhjSG7w5rSQFfVxCxSRGPwlXWEvzO5xuJuQpx9uUE3dMfIjq7+TdedYobCnv80qJ+ZGTQutTGVribItZHo5n5ex4v+GIkFeZUoApzazWXqbMZ3jOClYbYg1ckUui9ZVqk6n4L4NIactR8Ivzeuj9eTXzAw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651947603; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=JdehnkGLqTKFFqJmno6u7ZfWHrdJGNPcR0+Jk9IE3ng=; b=XIEVYxPUtesXZfT5vuCzz6pt2jzgGbTU8w/iJscQ2fQySGFAEC0dYj9pIJNT4yV1y2VpeCY+RpsPyv3e4aXW55yx2Bmkik38G98fiteAR80DYN+AJEZ4eEGtEB4w5WBzQ100OQGi+mhd8bkzu9w7S5kdp9gVy3c9cACprO9O9m8= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1651947603234978.0008334223215; Sat, 7 May 2022 11:20:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.323755.545574 (Exim 4.92) (envelope-from ) id 1nnP1X-0006jT-T4; Sat, 07 May 2022 18:19:23 +0000 Received: by outflank-mailman (output) from mailman id 323755.545574; Sat, 07 May 2022 18:19:23 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1X-0006jM-Pn; Sat, 07 May 2022 18:19:23 +0000 Received: by outflank-mailman (input) for mailman id 323755; Sat, 07 May 2022 18:19:21 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1V-0006Tm-Ow for xen-devel@lists.xenproject.org; Sat, 07 May 2022 18:19:21 +0000 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [2a00:1450:4864:20::32f]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 36f9c426-ce32-11ec-a406-831a346695d4; Sat, 07 May 2022 20:19:21 +0200 (CEST) Received: by mail-wm1-x32f.google.com with SMTP id m2-20020a1ca302000000b003943bc63f98so6129843wme.4 for ; Sat, 07 May 2022 11:19:20 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:19 -0700 (PDT) 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" X-Inumbo-ID: 36f9c426-ce32-11ec-a406-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JdehnkGLqTKFFqJmno6u7ZfWHrdJGNPcR0+Jk9IE3ng=; b=W2CM1/+K9Zk1Kt7dirJgx2bl5Gzq0zuZnPN5BVRpPATox1liMddFAgkXT9YhMEJKJW 3O660mwRMAf8TC005txxxBgmgrS9oiMeZwotoJPeew+vIvCdOm765wKV5c9x0BQ00/qM lOgnr6KN7alyGkPQQDDUnMJeacpkc2gh1QJmK01mhUdDzdgbsOjUoKte+pU3cJeipEuf YSvs0HkqOPU6wQ5cIxg7hJrG1hxCK2aJ4ZjSYAp3T1krrkp7p1CUCOkmJSjmRJV2FUhp N7Fr3JhClK19P4klrT/48MqsDShlu0CI9z8qoywZr6bhKFckl2kixdga3Z5rYAdM4lNL NeOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JdehnkGLqTKFFqJmno6u7ZfWHrdJGNPcR0+Jk9IE3ng=; b=KJaxT8T12HeSmZK9j2tBWHIlTGJsmkd9MS4knL28sOpMmBfHi+265HQCtpvfS6CSx+ jo3d1+W8kMiAeD4EUgxU3bUBrtsprPpDeVESpjMgKvI3FOWIVZ8mlRSEGh6C/6JYyxCx +It6wyniH31NSKtmgy5+XU48+B/gtpCYclL82nKMSS4bjwKxDPOxYngZ2hhiApZutMu1 Adi3rpdxtRK5Q5Emr8zJbPDbI6E0rCemPH+RNVYG7vvHo1k3PpnTN1bOlMLim6otYj+0 tbERcAJWLEDlW7lZVJO/6ZyKNPph3F6bsxU1XwdraeuYBTDU2KUsv5hgZJyWzCH7lDxs Txrg== X-Gm-Message-State: AOAM531rKDnJgUwpnhw2iqbzm4QhteuDaLcLwm1yhqFhqstFUAfM9Fr9 MsSFBsAIsnygeWlGihgxCu5JqxsA248= X-Google-Smtp-Source: ABdhPJwcoJACAJIOBQsEykFr9K/bH+BLZEo0MgY60F8IfPkFfRkCVpyNg4ZkPrg0s2ewaX0SV0R9DQ== X-Received: by 2002:a7b:c446:0:b0:394:3293:a88f with SMTP id l6-20020a7bc446000000b003943293a88fmr8943853wmi.22.1651947559848; Sat, 07 May 2022 11:19:19 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Oleksandr Tyshchenko , Stefano Stabellini , Russell King , Catalin Marinas , Will Deacon , Boris Ostrovsky , Juergen Gross , Julien Grall , "Michael S. Tsirkin" , Christoph Hellwig Subject: [PATCH V2 1/7] arm/xen: Introduce xen_setup_dma_ops() Date: Sat, 7 May 2022 21:19:02 +0300 Message-Id: <1651947548-4055-2-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1651947605277100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko This patch introduces new helper and places it in new header. The helper's purpose is to assign any Xen specific DMA ops in a single place. For now, we deal with xen-swiotlb DMA ops only. The one of the subsequent commits in current series will add xen-grant DMA ops case. Also re-use the xen_swiotlb_detect() check on Arm32. Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini Acked-by: Catalin Marinas --- Changes RFC -> V1: - update commit description - move commit to the beginning of the series - move #ifdef CONFIG_XEN from dma-mapping.c to xen-ops.h Changes V1 -> V2: - add Stefano's R-b - add missing SPDX-License-Identifier to xen-ops.h --- arch/arm/include/asm/xen/xen-ops.h | 2 ++ arch/arm/mm/dma-mapping.c | 7 ++----- arch/arm64/include/asm/xen/xen-ops.h | 2 ++ arch/arm64/mm/dma-mapping.c | 7 ++----- include/xen/arm/xen-ops.h | 15 +++++++++++++++ 5 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 arch/arm/include/asm/xen/xen-ops.h create mode 100644 arch/arm64/include/asm/xen/xen-ops.h create mode 100644 include/xen/arm/xen-ops.h diff --git a/arch/arm/include/asm/xen/xen-ops.h b/arch/arm/include/asm/xen/= xen-ops.h new file mode 100644 index 00000000..7ebb7eb --- /dev/null +++ b/arch/arm/include/asm/xen/xen-ops.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 82ffac6..059cce0 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -33,7 +33,7 @@ #include #include #include -#include +#include =20 #include "dma.h" #include "mm.h" @@ -2287,10 +2287,7 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_= base, u64 size, =20 set_dma_ops(dev, dma_ops); =20 -#ifdef CONFIG_XEN - if (xen_initial_domain()) - dev->dma_ops =3D &xen_swiotlb_dma_ops; -#endif + xen_setup_dma_ops(dev); dev->archdata.dma_ops_setup =3D true; } =20 diff --git a/arch/arm64/include/asm/xen/xen-ops.h b/arch/arm64/include/asm/= xen/xen-ops.h new file mode 100644 index 00000000..7ebb7eb --- /dev/null +++ b/arch/arm64/include/asm/xen/xen-ops.h @@ -0,0 +1,2 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c index 6719f9e..6099c81 100644 --- a/arch/arm64/mm/dma-mapping.c +++ b/arch/arm64/mm/dma-mapping.c @@ -9,9 +9,9 @@ #include #include #include -#include =20 #include +#include =20 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, enum dma_data_direction dir) @@ -52,8 +52,5 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base,= u64 size, if (iommu) iommu_setup_dma_ops(dev, dma_base, dma_base + size - 1); =20 -#ifdef CONFIG_XEN - if (xen_swiotlb_detect()) - dev->dma_ops =3D &xen_swiotlb_dma_ops; -#endif + xen_setup_dma_ops(dev); } diff --git a/include/xen/arm/xen-ops.h b/include/xen/arm/xen-ops.h new file mode 100644 index 00000000..288deb1 --- /dev/null +++ b/include/xen/arm/xen-ops.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ARM_XEN_OPS_H +#define _ASM_ARM_XEN_OPS_H + +#include + +static inline void xen_setup_dma_ops(struct device *dev) +{ +#ifdef CONFIG_XEN + if (xen_swiotlb_detect()) + dev->dma_ops =3D &xen_swiotlb_dma_ops; +#endif +} + +#endif /* _ASM_ARM_XEN_OPS_H */ --=20 2.7.4 From nobody Sat May 4 07:14:27 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1651947597; cv=none; d=zohomail.com; s=zohoarc; b=T4dN8760TDdhIiP5H2y8UCHpjPw6TB5YGRRVr2zDAhRebgZZyMF6eGaXgo2r6HpLiLr1KJUGESgRX2aNJzYXO/1AS4a5c+ADNdCl9HcCE13CecBHEPvNBvAwTCc/RnwzNFtV/E2ps/kYSbsKY2cuKoOZzK8O938MCfUyxh+oTFk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651947597; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=OrgzCi2vWreU0D3K+vX8BJWItQ5HoVmyJ6Y4kPIhynE=; b=UT7SjRk7YBFfc7xXmHacx0MC7Sc2xHC8+sqgGFwob+Yu8L4afuFy9h1fS4Bz9rrfRPwZKthVgAC6fLEXk2KNYQZ/abfRddeb8BD3DavO6eDV50lXK4HCZM/DyUnCMFp2+3AJL7Fm9HNsKL8zLNTk8d2t92NS3GFmPIZXONK+LCM= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1651947597025201.3205808690658; Sat, 7 May 2022 11:19:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.323756.545585 (Exim 4.92) (envelope-from ) id 1nnP1Z-0006yx-4w; Sat, 07 May 2022 18:19:25 +0000 Received: by outflank-mailman (output) from mailman id 323756.545585; Sat, 07 May 2022 18:19:25 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1Z-0006yq-1S; Sat, 07 May 2022 18:19:25 +0000 Received: by outflank-mailman (input) for mailman id 323756; Sat, 07 May 2022 18:19:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1X-0006Tm-RS for xen-devel@lists.xenproject.org; Sat, 07 May 2022 18:19:24 +0000 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [2a00:1450:4864:20::32c]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 38019e83-ce32-11ec-a406-831a346695d4; Sat, 07 May 2022 20:19:22 +0200 (CEST) Received: by mail-wm1-x32c.google.com with SMTP id p189so6244075wmp.3 for ; Sat, 07 May 2022 11:19:22 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:20 -0700 (PDT) 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" X-Inumbo-ID: 38019e83-ce32-11ec-a406-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=OrgzCi2vWreU0D3K+vX8BJWItQ5HoVmyJ6Y4kPIhynE=; b=OUAhv9vXITWVMU9TeHHBFXF+xNxB5VE882DW65aTxQO9VaGcSIkn1h0II2gUa8W+cg oIwLH5eetSj3mlQfVgEc/AWmjip/UKoCGQj8eTrn9uMsIUmcrTiCXaswFFUmP79k2cO+ U8LmQDpnGRWYUo1x2xy1Ne9W1L4r6LEYYwHF0Hl7RHpwtAhFph/ixx9SvqDTDJjEI/et O+F5fnrB+Z/JhzefM4sN3B25lzgA2/A/aGxdRmbwB54vv29wzxEEJPCLpLgtTsKwVeQ3 ZiVdRo8kt+20WXxug38HrZkNGZSPuISLsBBvMU7gHkv81+FWRTzAFw0t+EbSkCNsLzmL RtZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=OrgzCi2vWreU0D3K+vX8BJWItQ5HoVmyJ6Y4kPIhynE=; b=Q+WXYwFDsGo3X5TwGwHBJ86wU8xLDpM2aDDzUzeiLypzCWm5jtiBZiJ0y5aUluEBQD GJMgq2EySM94CbYsvCZhdu5u6asKk6mC/dAETKevFPB6Zr1PGdJFTWgaFnMz9YGO3EpW 3pE3Ng0wxDvpb1V81pftzxB0fH5ySzNQ3FvF6963m8GHiXaXxQIf8OHoyCqSjB7hcrc3 qbdVA/c6g/v0l9q/RTwqk45Af/vCuX7GWtukEw4NNMQ6C12i9HRcxZUcPOlQ2XkWrKbW j5+shWQpqzn0XwXBfHdnq0o7sgqSXpp/kFiScfdUcAh2YI0IC3adBylsrnRYU/SP9TbV 8vTQ== X-Gm-Message-State: AOAM531eXIEFl1IYbYGO4Y7zjqEcDB5bnJDSa8IXKOL9ZYIlMqGGN7eR 5g85TKOQhbU0juEEiyXfDDdLCVQXKng= X-Google-Smtp-Source: ABdhPJwbvqsd8GXKWxFUVsXuDTyUOhkUCHUMiidTybWZFxP/+3NjFKIjeYM0vnYNwa7eE5HX0o+fYg== X-Received: by 2002:a7b:c04d:0:b0:394:61ea:4fa2 with SMTP id u13-20020a7bc04d000000b0039461ea4fa2mr8926671wmc.40.1651947561575; Sat, 07 May 2022 11:19:21 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , Julien Grall , Oleksandr Tyshchenko , "Michael S. Tsirkin" , Christoph Hellwig Subject: [PATCH V2 2/7] xen/grants: support allocating consecutive grants Date: Sat, 7 May 2022 21:19:03 +0300 Message-Id: <1651947548-4055-3-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1651947597825100003 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Juergen Gross For support of virtio via grant mappings in rare cases larger mappings using consecutive grants are needed. Support those by adding a bitmap of free grants. As consecutive grants will be needed only in very rare cases (e.g. when configuring a virtio device with a multi-page ring), optimize for the normal case of non-consecutive allocations. Signed-off-by: Juergen Gross Reviewed-by: Boris Ostrovsky --- Changes RFC -> V1: - no changes =20 Changes V1 -> V2: - no changes --- drivers/xen/grant-table.c | 238 +++++++++++++++++++++++++++++++++++++++---= ---- include/xen/grant_table.h | 4 + 2 files changed, 210 insertions(+), 32 deletions(-) diff --git a/drivers/xen/grant-table.c b/drivers/xen/grant-table.c index 8ccccac..1b458c0 100644 --- a/drivers/xen/grant-table.c +++ b/drivers/xen/grant-table.c @@ -33,6 +33,7 @@ =20 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt =20 +#include #include #include #include @@ -72,9 +73,32 @@ =20 static grant_ref_t **gnttab_list; static unsigned int nr_grant_frames; + +/* + * Handling of free grants: + * + * Free grants are in a simple list anchored in gnttab_free_head. They are + * linked by grant ref, the last element contains GNTTAB_LIST_END. The num= ber + * of free entries is stored in gnttab_free_count. + * Additionally there is a bitmap of free entries anchored in + * gnttab_free_bitmap. This is being used for simplifying allocation of + * multiple consecutive grants, which is needed e.g. for support of virtio. + * gnttab_last_free is used to add free entries of new frames at the end of + * the free list. + * gnttab_free_tail_ptr specifies the variable which references the start + * of consecutive free grants ending with gnttab_last_free. This pointer is + * updated in a rather defensive way, in order to avoid performance hits in + * hot paths. + * All those variables are protected by gnttab_list_lock. + */ static int gnttab_free_count; -static grant_ref_t gnttab_free_head; +static unsigned int gnttab_size; +static grant_ref_t gnttab_free_head =3D GNTTAB_LIST_END; +static grant_ref_t gnttab_last_free =3D GNTTAB_LIST_END; +static grant_ref_t *gnttab_free_tail_ptr; +static unsigned long *gnttab_free_bitmap; static DEFINE_SPINLOCK(gnttab_list_lock); + struct grant_frames xen_auto_xlat_grant_frames; static unsigned int xen_gnttab_version; module_param_named(version, xen_gnttab_version, uint, 0); @@ -170,16 +194,111 @@ static int get_free_entries(unsigned count) =20 ref =3D head =3D gnttab_free_head; gnttab_free_count -=3D count; - while (count-- > 1) - head =3D gnttab_entry(head); + while (count--) { + bitmap_clear(gnttab_free_bitmap, head, 1); + if (gnttab_free_tail_ptr =3D=3D __gnttab_entry(head)) + gnttab_free_tail_ptr =3D &gnttab_free_head; + if (count) + head =3D gnttab_entry(head); + } gnttab_free_head =3D gnttab_entry(head); gnttab_entry(head) =3D GNTTAB_LIST_END; =20 + if (!gnttab_free_count) { + gnttab_last_free =3D GNTTAB_LIST_END; + gnttab_free_tail_ptr =3D NULL; + } + spin_unlock_irqrestore(&gnttab_list_lock, flags); =20 return ref; } =20 +static int get_seq_entry_count(void) +{ + if (gnttab_last_free =3D=3D GNTTAB_LIST_END || !gnttab_free_tail_ptr || + *gnttab_free_tail_ptr =3D=3D GNTTAB_LIST_END) + return 0; + + return gnttab_last_free - *gnttab_free_tail_ptr + 1; +} + +/* Rebuilds the free grant list and tries to find count consecutive entrie= s. */ +static int get_free_seq(unsigned int count) +{ + int ret =3D -ENOSPC; + unsigned int from, to; + grant_ref_t *last; + + gnttab_free_tail_ptr =3D &gnttab_free_head; + last =3D &gnttab_free_head; + + for (from =3D find_first_bit(gnttab_free_bitmap, gnttab_size); + from < gnttab_size; + from =3D find_next_bit(gnttab_free_bitmap, gnttab_size, to + 1)) { + to =3D find_next_zero_bit(gnttab_free_bitmap, gnttab_size, + from + 1); + if (ret < 0 && to - from >=3D count) { + ret =3D from; + bitmap_clear(gnttab_free_bitmap, ret, count); + from +=3D count; + gnttab_free_count -=3D count; + if (from =3D=3D to) + continue; + } + + while (from < to) { + *last =3D from; + last =3D __gnttab_entry(from); + gnttab_last_free =3D from; + from++; + } + if (to < gnttab_size) + gnttab_free_tail_ptr =3D __gnttab_entry(to - 1); + } + + *last =3D GNTTAB_LIST_END; + if (gnttab_last_free !=3D gnttab_size - 1) + gnttab_free_tail_ptr =3D NULL; + + return ret; +} + +static int get_free_entries_seq(unsigned int count) +{ + unsigned long flags; + int ret =3D 0; + + spin_lock_irqsave(&gnttab_list_lock, flags); + + if (gnttab_free_count < count) { + ret =3D gnttab_expand(count - gnttab_free_count); + if (ret < 0) + goto out; + } + + if (get_seq_entry_count() < count) { + ret =3D get_free_seq(count); + if (ret >=3D 0) + goto out; + ret =3D gnttab_expand(count - get_seq_entry_count()); + if (ret < 0) + goto out; + } + + ret =3D *gnttab_free_tail_ptr; + *gnttab_free_tail_ptr =3D gnttab_entry(ret + count - 1); + gnttab_free_count -=3D count; + if (!gnttab_free_count) + gnttab_free_tail_ptr =3D NULL; + bitmap_clear(gnttab_free_bitmap, ret, count); + + out: + spin_unlock_irqrestore(&gnttab_list_lock, flags); + + return ret; +} + static void do_free_callbacks(void) { struct gnttab_free_callback *callback, *next; @@ -206,17 +325,48 @@ static inline void check_free_callbacks(void) do_free_callbacks(); } =20 -static void put_free_entry(grant_ref_t ref) +static void put_free_entry_locked(grant_ref_t ref) { - unsigned long flags; - spin_lock_irqsave(&gnttab_list_lock, flags); gnttab_entry(ref) =3D gnttab_free_head; gnttab_free_head =3D ref; + if (!gnttab_free_count) + gnttab_last_free =3D ref; + if (gnttab_free_tail_ptr =3D=3D &gnttab_free_head) + gnttab_free_tail_ptr =3D __gnttab_entry(ref); gnttab_free_count++; + bitmap_set(gnttab_free_bitmap, ref, 1); +} + +static void put_free_entry(grant_ref_t ref) +{ + unsigned long flags; + + spin_lock_irqsave(&gnttab_list_lock, flags); + put_free_entry_locked(ref); check_free_callbacks(); spin_unlock_irqrestore(&gnttab_list_lock, flags); } =20 +static void gnttab_set_free(unsigned int start, unsigned int n) +{ + unsigned int i; + + for (i =3D start; i < start + n - 1; i++) + gnttab_entry(i) =3D i + 1; + + gnttab_entry(i) =3D GNTTAB_LIST_END; + if (!gnttab_free_count) { + gnttab_free_head =3D start; + gnttab_free_tail_ptr =3D &gnttab_free_head; + } else { + gnttab_entry(gnttab_last_free) =3D start; + } + gnttab_free_count +=3D n; + gnttab_last_free =3D i; + + bitmap_set(gnttab_free_bitmap, start, n); +} + /* * Following applies to gnttab_update_entry_v1 and gnttab_update_entry_v2. * Introducing a valid entry into the grant table: @@ -448,23 +598,31 @@ void gnttab_free_grant_references(grant_ref_t head) { grant_ref_t ref; unsigned long flags; - int count =3D 1; - if (head =3D=3D GNTTAB_LIST_END) - return; + spin_lock_irqsave(&gnttab_list_lock, flags); - ref =3D head; - while (gnttab_entry(ref) !=3D GNTTAB_LIST_END) { - ref =3D gnttab_entry(ref); - count++; + while (head !=3D GNTTAB_LIST_END) { + ref =3D gnttab_entry(head); + put_free_entry_locked(head); + head =3D ref; } - gnttab_entry(ref) =3D gnttab_free_head; - gnttab_free_head =3D head; - gnttab_free_count +=3D count; check_free_callbacks(); spin_unlock_irqrestore(&gnttab_list_lock, flags); } EXPORT_SYMBOL_GPL(gnttab_free_grant_references); =20 +void gnttab_free_grant_reference_seq(grant_ref_t head, unsigned int count) +{ + unsigned long flags; + unsigned int i; + + spin_lock_irqsave(&gnttab_list_lock, flags); + for (i =3D count; i > 0; i--) + put_free_entry_locked(head + i - 1); + check_free_callbacks(); + spin_unlock_irqrestore(&gnttab_list_lock, flags); +} +EXPORT_SYMBOL_GPL(gnttab_free_grant_reference_seq); + int gnttab_alloc_grant_references(u16 count, grant_ref_t *head) { int h =3D get_free_entries(count); @@ -478,6 +636,24 @@ int gnttab_alloc_grant_references(u16 count, grant_ref= _t *head) } EXPORT_SYMBOL_GPL(gnttab_alloc_grant_references); =20 +int gnttab_alloc_grant_reference_seq(unsigned int count, grant_ref_t *firs= t) +{ + int h; + + if (count =3D=3D 1) + h =3D get_free_entries(1); + else + h =3D get_free_entries_seq(count); + + if (h < 0) + return -ENOSPC; + + *first =3D h; + + return 0; +} +EXPORT_SYMBOL_GPL(gnttab_alloc_grant_reference_seq); + int gnttab_empty_grant_references(const grant_ref_t *private_head) { return (*private_head =3D=3D GNTTAB_LIST_END); @@ -570,16 +746,13 @@ static int grow_gnttab_list(unsigned int more_frames) goto grow_nomem; } =20 + gnttab_set_free(gnttab_size, extra_entries); =20 - for (i =3D grefs_per_frame * nr_grant_frames; - i < grefs_per_frame * new_nr_grant_frames - 1; i++) - gnttab_entry(i) =3D i + 1; - - gnttab_entry(i) =3D gnttab_free_head; - gnttab_free_head =3D grefs_per_frame * nr_grant_frames; - gnttab_free_count +=3D extra_entries; + if (!gnttab_free_tail_ptr) + gnttab_free_tail_ptr =3D __gnttab_entry(gnttab_size); =20 nr_grant_frames =3D new_nr_grant_frames; + gnttab_size +=3D extra_entries; =20 check_free_callbacks(); =20 @@ -1424,7 +1597,6 @@ int gnttab_init(void) int i; unsigned long max_nr_grant_frames; unsigned int max_nr_glist_frames, nr_glist_frames; - unsigned int nr_init_grefs; int ret; =20 gnttab_request_version(); @@ -1452,6 +1624,13 @@ int gnttab_init(void) } } =20 + i =3D gnttab_interface->grefs_per_grant_frame * max_nr_grant_frames; + gnttab_free_bitmap =3D bitmap_zalloc(i, GFP_KERNEL); + if (!gnttab_free_bitmap) { + ret =3D -ENOMEM; + goto ini_nomem; + } + ret =3D arch_gnttab_init(max_nr_grant_frames, nr_status_frames(max_nr_grant_frames)); if (ret < 0) @@ -1462,15 +1641,9 @@ int gnttab_init(void) goto ini_nomem; } =20 - nr_init_grefs =3D nr_grant_frames * - gnttab_interface->grefs_per_grant_frame; - - for (i =3D NR_RESERVED_ENTRIES; i < nr_init_grefs - 1; i++) - gnttab_entry(i) =3D i + 1; + gnttab_size =3D nr_grant_frames * gnttab_interface->grefs_per_grant_frame; =20 - gnttab_entry(nr_init_grefs - 1) =3D GNTTAB_LIST_END; - gnttab_free_count =3D nr_init_grefs - NR_RESERVED_ENTRIES; - gnttab_free_head =3D NR_RESERVED_ENTRIES; + gnttab_set_free(NR_RESERVED_ENTRIES, gnttab_size - NR_RESERVED_ENTRIES); =20 printk("Grant table initialized\n"); return 0; @@ -1479,6 +1652,7 @@ int gnttab_init(void) for (i--; i >=3D 0; i--) free_page((unsigned long)gnttab_list[i]); kfree(gnttab_list); + bitmap_free(gnttab_free_bitmap); return ret; } EXPORT_SYMBOL_GPL(gnttab_init); diff --git a/include/xen/grant_table.h b/include/xen/grant_table.h index dfd5bf3..d815e1d 100644 --- a/include/xen/grant_table.h +++ b/include/xen/grant_table.h @@ -129,10 +129,14 @@ int gnttab_try_end_foreign_access(grant_ref_t ref); */ int gnttab_alloc_grant_references(u16 count, grant_ref_t *pprivate_head); =20 +int gnttab_alloc_grant_reference_seq(unsigned int count, grant_ref_t *firs= t); + void gnttab_free_grant_reference(grant_ref_t ref); =20 void gnttab_free_grant_references(grant_ref_t head); =20 +void gnttab_free_grant_reference_seq(grant_ref_t head, unsigned int count); + int gnttab_empty_grant_references(const grant_ref_t *pprivate_head); =20 int gnttab_claim_grant_reference(grant_ref_t *pprivate_head); --=20 2.7.4 From nobody Sat May 4 07:14:27 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1651947595; cv=none; d=zohomail.com; s=zohoarc; b=B0UtfTdU2Ogna6pVAxTDPnA/U1rykBmX//CXfZ1Uylliq7nzFnPGwiNOWGPAGk039EXbqbozd+xebRPDRvg0zoJhOptodTc5NufkLokr5wNg+Z+r4qfG/eomGu7tDCWKaJCkY5SOGzBZUrWY0IpOQZHSZ1hsFWtFoAJZMkZM4UQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651947595; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/tE24Epa/B9t6hINhti+0qrrH2Ayc1ZVkfo5g4wLeJM=; b=jjZYUqMayrAZ4FVPjYRmBuTd4f96Cc5bCoNlKg7p0Ds87Ua4Jt/p2J3AvL6u5x32U+dA9hoUnDybs0Y5MpBJIjyk9jJSl/85/gPHmY2W/1O+oZTuicAtblP3Fr1sy9j2EMcN+CqAuj7RvKvLKNSsKMyZbQ3003hbk+hwHxEaelQ= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1651947595245827.754566932949; Sat, 7 May 2022 11:19:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.323757.545596 (Exim 4.92) (envelope-from ) id 1nnP1c-0007IE-IO; Sat, 07 May 2022 18:19:28 +0000 Received: by outflank-mailman (output) from mailman id 323757.545596; Sat, 07 May 2022 18:19:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1c-0007I4-Ef; Sat, 07 May 2022 18:19:28 +0000 Received: by outflank-mailman (input) for mailman id 323757; Sat, 07 May 2022 18:19:27 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1b-00079i-4b for xen-devel@lists.xenproject.org; Sat, 07 May 2022 18:19:27 +0000 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [2a00:1450:4864:20::32e]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 392a1dbf-ce32-11ec-8fc4-03012f2f19d4; Sat, 07 May 2022 20:19:24 +0200 (CEST) Received: by mail-wm1-x32e.google.com with SMTP id k126so6254940wme.2 for ; Sat, 07 May 2022 11:19:24 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.21 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:22 -0700 (PDT) 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" X-Inumbo-ID: 392a1dbf-ce32-11ec-8fc4-03012f2f19d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/tE24Epa/B9t6hINhti+0qrrH2Ayc1ZVkfo5g4wLeJM=; b=pHunhm5LcGaWGqSqpnCaJLx1HKwwGm+QcAnyHXni4G05/DDzLu2dBCIMlTSs7Kro1r Imn+rsvPoXSJMbnO9sqeeo6fJ0w/25pXPesstI2pdMmYSHfUc4SHlTQw98HSRt1LNzk8 x153rz6Sd6vfTRIX0+v3B5mC4ntIR/NWR2YUs8BMSHF5A/agx379p/zUe5ACZoNhwHDZ NPCdJK9Inl/jdm8S1FpBqyILR9jorjdbdrJR+jR/4DRbHzewrO/YMAp8eRwi1fA3FfD+ aABoI/isygKUQxu1kQNlDPhC/e19Jz2I8BoUYqpdFbYShAwaVq8FSMb30nCHea6JeW0L hjwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/tE24Epa/B9t6hINhti+0qrrH2Ayc1ZVkfo5g4wLeJM=; b=0Fd341ACJEUR+ydJjft/Y0EkDX/yBnOdjfY+RE6JN4NWcgpNuoi4ZFt04AjruGLSjc nZEHY6lA4i8pwfkjxLWWrggbEl9654TybtswIohmaS9ZcZnA68Ynond6HzfZWLmTbObG R8DtShOLJ949PykrmkLrF1FkQfG6OZU/lbbXPjUTonkuXCHQCVp0Y/6AgHlledjvR9i9 0xwHGB6a++8tshDxM61WrXepaB0ONoF3encygbDwroiE8Uez3HFAHIPBXDrOcEmZHtMR YFWyackjIsBw7oy20GF//l3JkTqeggll5NkOdLy2/T7nuTWYiwfeEJwYd9IAyi5ujXGA KV8A== X-Gm-Message-State: AOAM530KP1DIEPKIcYvirtsMP2HMYmDUix1lrEAYSZTOjGWT5SQTms22 qlQmbHApouTHOVQNTSoce6UtxuoTFss= X-Google-Smtp-Source: ABdhPJxBOOgVAgBRCVt+whQtaQ1bMotolbL617ZTixMBeupKujHPOUz5W2K18bpkn4k6f+wihFjJ5g== X-Received: by 2002:a05:600c:1909:b0:394:47d2:eca9 with SMTP id j9-20020a05600c190900b0039447d2eca9mr15802949wmq.82.1651947563308; Sat, 07 May 2022 11:19:23 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , Julien Grall , Oleksandr Tyshchenko , "Michael S. Tsirkin" , Christoph Hellwig Subject: [PATCH V2 3/7] xen/grant-dma-ops: Add option to restrict memory access under Xen Date: Sat, 7 May 2022 21:19:04 +0300 Message-Id: <1651947548-4055-4-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1651947597860100005 From: Juergen Gross Introduce Xen grant DMA-mapping layer which contains special DMA-mapping routines for providing grant references as DMA addresses to be used by frontends (e.g. virtio) in Xen guests. Add the needed functionality by providing a special set of DMA ops handling the needed grant operations for the I/O pages. The subsequent commit will introduce the use case for xen-grant DMA ops layer to enable using virtio devices in Xen guests in a safe manner. Signed-off-by: Juergen Gross Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini --- Changes RFC -> V1: - squash with almost all changes from commit (except handling "xen,dev-d= omid" property): "[PATCH 4/6] virtio: Various updates to xen-virtio DMA ops layer" - update commit subject/description and comments in code - leave only single Kconfig option XEN_VIRTIO and remove architectural dependencies - introduce common xen_has_restricted_virtio_memory_access() in xen.h and update arch_has_restricted_virtio_memory_access() for both Arm and x86 to call new helper - use (1ULL << 63) instead of 0x8000000000000000ULL for XEN_GRANT_ADDR_O= FF - implement xen_virtio_dma_map(unmap)_sg() using example in swiotlb-xen.c - optimize padding by moving "broken" field in struct xen_virtio_data - remove unneeded per-device spinlock - remove the inclusion of virtio_config.h - remane everything according to the new naming scheme: s/virtio/grant_dma - add new hidden config option XEN_GRANT_DMA_OPS Changes V1 -> V2: - fix checkpatch.pl warnings - remove the inclusion of linux/pci.h - rework to use xarray for data context - remove EXPORT_SYMBOL_GPL(xen_grant_setup_dma_ops); - remove the line of * after SPDX-License-Identifier - split changes into grant-dma-ops.c and arch_has_restricted_virtio_memo= ry_access() and update commit subject/description accordingly - remove "default n" for config XEN_VIRTIO - implement xen_grant_dma_alloc(free)_pages() --- drivers/xen/Kconfig | 4 + drivers/xen/Makefile | 1 + drivers/xen/grant-dma-ops.c | 314 ++++++++++++++++++++++++++++++++++++++++= ++++ include/xen/xen-ops.h | 8 ++ 4 files changed, 327 insertions(+) create mode 100644 drivers/xen/grant-dma-ops.c diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig index 120d32f..313a9127 100644 --- a/drivers/xen/Kconfig +++ b/drivers/xen/Kconfig @@ -335,4 +335,8 @@ config XEN_UNPOPULATED_ALLOC having to balloon out RAM regions in order to obtain physical memory space to create such mappings. =20 +config XEN_GRANT_DMA_OPS + bool + select DMA_OPS + endmenu diff --git a/drivers/xen/Makefile b/drivers/xen/Makefile index 5aae66e..1a23cb0 100644 --- a/drivers/xen/Makefile +++ b/drivers/xen/Makefile @@ -39,3 +39,4 @@ xen-gntalloc-y :=3D gntalloc.o xen-privcmd-y :=3D privcmd.o privcmd-buf.o obj-$(CONFIG_XEN_FRONT_PGDIR_SHBUF) +=3D xen-front-pgdir-shbuf.o obj-$(CONFIG_XEN_UNPOPULATED_ALLOC) +=3D unpopulated-alloc.o +obj-$(CONFIG_XEN_GRANT_DMA_OPS) +=3D grant-dma-ops.o diff --git a/drivers/xen/grant-dma-ops.c b/drivers/xen/grant-dma-ops.c new file mode 100644 index 00000000..29ad7bf --- /dev/null +++ b/drivers/xen/grant-dma-ops.c @@ -0,0 +1,314 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Xen grant DMA-mapping layer - contains special DMA-mapping routines + * for providing grant references as DMA addresses to be used by frontends + * (e.g. virtio) in Xen guests + * + * Copyright (c) 2021, Juergen Gross + */ + +#include +#include +#include +#include +#include +#include +#include + +struct xen_grant_dma_data { + /* The ID of backend domain */ + domid_t dev_domid; + /* Is device behaving sane? */ + bool broken; +}; + +static DEFINE_XARRAY(xen_grant_dma_devices); + +#define XEN_GRANT_DMA_ADDR_OFF (1ULL << 63) + +static inline dma_addr_t grant_to_dma(grant_ref_t grant) +{ + return XEN_GRANT_DMA_ADDR_OFF | ((dma_addr_t)grant << PAGE_SHIFT); +} + +static inline grant_ref_t dma_to_grant(dma_addr_t dma) +{ + return (grant_ref_t)((dma & ~XEN_GRANT_DMA_ADDR_OFF) >> PAGE_SHIFT); +} + +static struct xen_grant_dma_data *find_xen_grant_dma_data(struct device *d= ev) +{ + struct xen_grant_dma_data *data; + + xa_lock(&xen_grant_dma_devices); + data =3D xa_load(&xen_grant_dma_devices, (unsigned long)dev); + xa_unlock(&xen_grant_dma_devices); + + return data; +} + +/* + * DMA ops for Xen frontends (e.g. virtio). + * + * Used to act as a kind of software IOMMU for Xen guests by using grants = as + * DMA addresses. + * Such a DMA address is formed by using the grant reference as a frame + * number and setting the highest address bit (this bit is for the backend + * to be able to distinguish it from e.g. a mmio address). + * + * Note that for now we hard wire dom0 to be the backend domain. In order + * to support any domain as backend we'd need to add a way to communicate + * the domid of this backend, e.g. via Xenstore, via the PCI-device's + * config space or DT/ACPI. + */ +static void *xen_grant_dma_alloc(struct device *dev, size_t size, + dma_addr_t *dma_handle, gfp_t gfp, + unsigned long attrs) +{ + struct xen_grant_dma_data *data; + unsigned int i, n_pages =3D PFN_UP(size); + unsigned long pfn; + grant_ref_t grant; + void *ret; + + data =3D find_xen_grant_dma_data(dev); + if (!data) + return NULL; + + if (unlikely(data->broken)) + return NULL; + + ret =3D alloc_pages_exact(n_pages * PAGE_SIZE, gfp); + if (!ret) + return NULL; + + pfn =3D virt_to_pfn(ret); + + if (gnttab_alloc_grant_reference_seq(n_pages, &grant)) { + free_pages_exact(ret, n_pages * PAGE_SIZE); + return NULL; + } + + for (i =3D 0; i < n_pages; i++) { + gnttab_grant_foreign_access_ref(grant + i, data->dev_domid, + pfn_to_gfn(pfn + i), 0); + } + + *dma_handle =3D grant_to_dma(grant); + + return ret; +} + +static void xen_grant_dma_free(struct device *dev, size_t size, void *vadd= r, + dma_addr_t dma_handle, unsigned long attrs) +{ + struct xen_grant_dma_data *data; + unsigned int i, n_pages =3D PFN_UP(size); + grant_ref_t grant; + + data =3D find_xen_grant_dma_data(dev); + if (!data) + return; + + if (unlikely(data->broken)) + return; + + grant =3D dma_to_grant(dma_handle); + + for (i =3D 0; i < n_pages; i++) { + if (unlikely(!gnttab_end_foreign_access_ref(grant + i))) { + dev_alert(dev, "Grant still in use by backend domain, disabled for furt= her use\n"); + data->broken =3D true; + return; + } + } + + gnttab_free_grant_reference_seq(grant, n_pages); + + free_pages_exact(vaddr, n_pages * PAGE_SIZE); +} + +static struct page *xen_grant_dma_alloc_pages(struct device *dev, size_t s= ize, + dma_addr_t *dma_handle, + enum dma_data_direction dir, + gfp_t gfp) +{ + void *vaddr; + + vaddr =3D xen_grant_dma_alloc(dev, size, dma_handle, gfp, 0); + if (!vaddr) + return NULL; + + return virt_to_page(vaddr); +} + +static void xen_grant_dma_free_pages(struct device *dev, size_t size, + struct page *vaddr, dma_addr_t dma_handle, + enum dma_data_direction dir) +{ + xen_grant_dma_free(dev, size, page_to_virt(vaddr), dma_handle, 0); +} + +static dma_addr_t xen_grant_dma_map_page(struct device *dev, struct page *= page, + unsigned long offset, size_t size, + enum dma_data_direction dir, + unsigned long attrs) +{ + struct xen_grant_dma_data *data; + unsigned int i, n_pages =3D PFN_UP(size); + grant_ref_t grant; + dma_addr_t dma_handle; + + if (WARN_ON(dir =3D=3D DMA_NONE)) + return DMA_MAPPING_ERROR; + + data =3D find_xen_grant_dma_data(dev); + if (!data) + return DMA_MAPPING_ERROR; + + if (unlikely(data->broken)) + return DMA_MAPPING_ERROR; + + if (gnttab_alloc_grant_reference_seq(n_pages, &grant)) + return DMA_MAPPING_ERROR; + + for (i =3D 0; i < n_pages; i++) { + gnttab_grant_foreign_access_ref(grant + i, data->dev_domid, + xen_page_to_gfn(page) + i, dir =3D=3D DMA_TO_DEVICE); + } + + dma_handle =3D grant_to_dma(grant) + offset; + + return dma_handle; +} + +static void xen_grant_dma_unmap_page(struct device *dev, dma_addr_t dma_ha= ndle, + size_t size, enum dma_data_direction dir, + unsigned long attrs) +{ + struct xen_grant_dma_data *data; + unsigned int i, n_pages =3D PFN_UP(size); + grant_ref_t grant; + + if (WARN_ON(dir =3D=3D DMA_NONE)) + return; + + data =3D find_xen_grant_dma_data(dev); + if (!data) + return; + + if (unlikely(data->broken)) + return; + + grant =3D dma_to_grant(dma_handle); + + for (i =3D 0; i < n_pages; i++) { + if (unlikely(!gnttab_end_foreign_access_ref(grant + i))) { + dev_alert(dev, "Grant still in use by backend domain, disabled for furt= her use\n"); + data->broken =3D true; + return; + } + } + + gnttab_free_grant_reference_seq(grant, n_pages); +} + +static void xen_grant_dma_unmap_sg(struct device *dev, struct scatterlist = *sg, + int nents, enum dma_data_direction dir, + unsigned long attrs) +{ + struct scatterlist *s; + unsigned int i; + + if (WARN_ON(dir =3D=3D DMA_NONE)) + return; + + for_each_sg(sg, s, nents, i) + xen_grant_dma_unmap_page(dev, s->dma_address, sg_dma_len(s), dir, + attrs); +} + +static int xen_grant_dma_map_sg(struct device *dev, struct scatterlist *sg, + int nents, enum dma_data_direction dir, + unsigned long attrs) +{ + struct scatterlist *s; + unsigned int i; + + if (WARN_ON(dir =3D=3D DMA_NONE)) + return -EINVAL; + + for_each_sg(sg, s, nents, i) { + s->dma_address =3D xen_grant_dma_map_page(dev, sg_page(s), s->offset, + s->length, dir, attrs); + if (s->dma_address =3D=3D DMA_MAPPING_ERROR) + goto out; + + sg_dma_len(s) =3D s->length; + } + + return nents; + +out: + xen_grant_dma_unmap_sg(dev, sg, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC); + sg_dma_len(sg) =3D 0; + + return -EIO; +} + +static int xen_grant_dma_supported(struct device *dev, u64 mask) +{ + return mask =3D=3D DMA_BIT_MASK(64); +} + +static const struct dma_map_ops xen_grant_dma_ops =3D { + .alloc =3D xen_grant_dma_alloc, + .free =3D xen_grant_dma_free, + .alloc_pages =3D xen_grant_dma_alloc_pages, + .free_pages =3D xen_grant_dma_free_pages, + .mmap =3D dma_common_mmap, + .get_sgtable =3D dma_common_get_sgtable, + .map_page =3D xen_grant_dma_map_page, + .unmap_page =3D xen_grant_dma_unmap_page, + .map_sg =3D xen_grant_dma_map_sg, + .unmap_sg =3D xen_grant_dma_unmap_sg, + .dma_supported =3D xen_grant_dma_supported, +}; + +void xen_grant_setup_dma_ops(struct device *dev) +{ + struct xen_grant_dma_data *data; + uint32_t dev_domid; + + data =3D find_xen_grant_dma_data(dev); + if (data) { + dev_err(dev, "Xen grant DMA data is already created\n"); + return; + } + + /* XXX The dom0 is hardcoded as the backend domain for now */ + dev_domid =3D 0; + + data =3D devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); + if (!data) + goto err; + + data->dev_domid =3D dev_domid; + + if (xa_err(xa_store(&xen_grant_dma_devices, (unsigned long)dev, data, + GFP_KERNEL))) { + dev_err(dev, "Cannot store Xen grant DMA data\n"); + goto err; + } + + dev->dma_ops =3D &xen_grant_dma_ops; + + return; + +err: + dev_err(dev, "=D0=A1annot set up Xen grant DMA ops, retain platform DMA o= ps\n"); +} + +MODULE_DESCRIPTION("Xen grant DMA-mapping layer"); +MODULE_AUTHOR("Juergen Gross "); +MODULE_LICENSE("GPL"); diff --git a/include/xen/xen-ops.h b/include/xen/xen-ops.h index a3584a3..4f9fad5 100644 --- a/include/xen/xen-ops.h +++ b/include/xen/xen-ops.h @@ -221,4 +221,12 @@ static inline void xen_preemptible_hcall_end(void) { } =20 #endif /* CONFIG_XEN_PV && !CONFIG_PREEMPTION */ =20 +#ifdef CONFIG_XEN_GRANT_DMA_OPS +void xen_grant_setup_dma_ops(struct device *dev); +#else +static inline void xen_grant_setup_dma_ops(struct device *dev) +{ +} +#endif /* CONFIG_XEN_GRANT_DMA_OPS */ + #endif /* INCLUDE_XEN_OPS_H */ --=20 2.7.4 From nobody Sat May 4 07:14:27 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1651947598; cv=none; d=zohomail.com; s=zohoarc; b=Wtxiyd35u81pLGxvvmN2qbdfOkCauN55vxNCJCrFvy58OgzAHv2FD83jRdM07eN/C9us7qqCT7k51AibuvZUweQutuAGnevERNlyBJjFTQcqyS65Ruge/GKpAzDMY7HeIOVjT7hKjSiSTF08nuuj7pHD8K/KuCeXGWjvnlPfpPs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651947598; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=7ptoks8wk67ER3ivOLNGqLnlrp2ieijG6XsCJH8U+xs=; b=Z3vbG0Q87eQvhOZ5GlxSMTGu1ponSdqBSXcFMSU8uyxZlHKGPNKQlx1mcpjKQZgtiIY4NjkBMR9FL+/bW73M0vq+OMLYQKooNgXapHqf5w6MnMkGxWvrra1WpsE+kQDRxIdLv7ejorAm2Gsy0xnI7Vu149PEBtOy8nDXflGFujM= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1651947598422282.1418210807135; Sat, 7 May 2022 11:19:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.323758.545602 (Exim 4.92) (envelope-from ) id 1nnP1d-0007Lv-0L; Sat, 07 May 2022 18:19:29 +0000 Received: by outflank-mailman (output) from mailman id 323758.545602; Sat, 07 May 2022 18:19:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1c-0007Kh-O9; Sat, 07 May 2022 18:19:28 +0000 Received: by outflank-mailman (input) for mailman id 323758; Sat, 07 May 2022 18:19:27 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1b-00079i-PL for xen-devel@lists.xenproject.org; Sat, 07 May 2022 18:19:27 +0000 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [2a00:1450:4864:20::330]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 3a2ff787-ce32-11ec-8fc4-03012f2f19d4; Sat, 07 May 2022 20:19:26 +0200 (CEST) Received: by mail-wm1-x330.google.com with SMTP id 125-20020a1c1983000000b003941f354c62so6139733wmz.0 for ; Sat, 07 May 2022 11:19:26 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.23 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:24 -0700 (PDT) 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" X-Inumbo-ID: 3a2ff787-ce32-11ec-8fc4-03012f2f19d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7ptoks8wk67ER3ivOLNGqLnlrp2ieijG6XsCJH8U+xs=; b=d0kYK2iQ+DUSxY8TIV5wRNyHPklomH/v8OiDA4avtHKXjmkp4Zp1juytfZVUe5R9jw 6iI1z703N161/p94YTEgZzBvV9tNZtwPoy1WZ99vf+cAGadEz6RqANGY9FVDwr2m9bcl Vq7ONdNVKafzqy9lZM8s8n4nvRB9ora+yUZoAt2GW5V39j3rxHySpA1Bl9hCr6EfO8wr wcE7f5iYJhMtLTBFXVXiEc4fKvgbgVfEgQsPHaAzdWDsIo3wWtkzNdw3r6VK/JIMDzna jKGEcTMofqI63n2NHbaC2hXBtSEP29EI9mF7SGOUoo5t1apStnAP1DkfYc+vLXvc6o9J 31OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7ptoks8wk67ER3ivOLNGqLnlrp2ieijG6XsCJH8U+xs=; b=qc4hcwkvrlIVkWPqgkZGhfV32e8c/kay79SVXhB3SZF+G+yk8CP+uaR7pAwNzRHIDK Jm5WwBFUSSMPM/ojNbwQW7kShxwJI/qKW57IjgZ0pAn0l2EBGfGb+vk09bTZtlXdgOIE W5qSXu6YYuM+7xsTtOusKTsMiDp3OKlGnxIydqt2zHaBYU0VJmWr7+H4nyuJ/H5xq7fZ cbPUAdgEyL2Wx2vqsOjCoh7WcIb+7SrHhdoi+nJ/NGBZcsaHcRfP5eKu0n+yHcjUsOWT icD9+Xz67EpDT9H5pY407EIPEbf3JDVDm7/UZpW3/MP2LNwQOnZSayGMY3wqAw3akBla mtCA== X-Gm-Message-State: AOAM532D0WV+uDK4SbYssZ/6XhaDBM/k5NtLXzEVXqVxPKC6s8D1QxhI eeWSVZXjYY55qD+i0HUqyxHHeR0m3tI= X-Google-Smtp-Source: ABdhPJzE5bIacIcwrc1M6w7qJQB9mFgFaaKn933CxgAcdM1oZCHeWaRYPU3HEcjcxuQeVC7j1yiIqw== X-Received: by 2002:a05:600c:3393:b0:394:160a:18aa with SMTP id o19-20020a05600c339300b00394160a18aamr15602115wmp.58.1651947565193; Sat, 07 May 2022 11:19:25 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Oleksandr Tyshchenko , Stefano Stabellini , Russell King , Boris Ostrovsky , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Julien Grall , "Michael S. Tsirkin" , Christoph Hellwig Subject: [PATCH V2 4/7] xen/virtio: Enable restricted memory access using Xen grant mappings Date: Sat, 7 May 2022 21:19:05 +0300 Message-Id: <1651947548-4055-5-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1651947599260100010 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Juergen Gross In order to support virtio in Xen guests add a config option XEN_VIRTIO enabling the user to specify whether in all Xen guests virtio should be able to access memory via Xen grant mappings only on the host side. Also set PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS feature from the guest initialization code on Arm and x86 if CONFIG_XEN_VIRTIO is enabled. Signed-off-by: Juergen Gross Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini --- Changes V1 -> V2: - new patch, split required changes from commit: "[PATCH V1 3/6] xen/virtio: Add option to restrict memory access under = Xen" - rework according to new platform_has() infrastructure --- arch/arm/xen/enlighten.c | 2 ++ arch/x86/xen/enlighten_hvm.c | 2 ++ arch/x86/xen/enlighten_pv.c | 2 ++ drivers/xen/Kconfig | 11 +++++++++++ include/xen/xen.h | 8 ++++++++ 5 files changed, 25 insertions(+) diff --git a/arch/arm/xen/enlighten.c b/arch/arm/xen/enlighten.c index 07eb69f..1f9c3ba 100644 --- a/arch/arm/xen/enlighten.c +++ b/arch/arm/xen/enlighten.c @@ -443,6 +443,8 @@ static int __init xen_guest_init(void) if (!xen_domain()) return 0; =20 + xen_set_restricted_virtio_memory_access(); + if (!acpi_disabled) xen_acpi_guest_init(); else diff --git a/arch/x86/xen/enlighten_hvm.c b/arch/x86/xen/enlighten_hvm.c index 517a9d8..8b71b1d 100644 --- a/arch/x86/xen/enlighten_hvm.c +++ b/arch/x86/xen/enlighten_hvm.c @@ -195,6 +195,8 @@ static void __init xen_hvm_guest_init(void) if (xen_pv_domain()) return; =20 + xen_set_restricted_virtio_memory_access(); + init_hvm_pv_info(); =20 reserve_shared_info(); diff --git a/arch/x86/xen/enlighten_pv.c b/arch/x86/xen/enlighten_pv.c index 5038edb..fcd5d5d 100644 --- a/arch/x86/xen/enlighten_pv.c +++ b/arch/x86/xen/enlighten_pv.c @@ -109,6 +109,8 @@ static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc= ); =20 static void __init xen_pv_init_platform(void) { + xen_set_restricted_virtio_memory_access(); + populate_extra_pte(fix_to_virt(FIX_PARAVIRT_BOOTMAP)); =20 set_fixmap(FIX_PARAVIRT_BOOTMAP, xen_start_info->shared_info); diff --git a/drivers/xen/Kconfig b/drivers/xen/Kconfig index 313a9127..a7bd8ce 100644 --- a/drivers/xen/Kconfig +++ b/drivers/xen/Kconfig @@ -339,4 +339,15 @@ config XEN_GRANT_DMA_OPS bool select DMA_OPS =20 +config XEN_VIRTIO + bool "Xen virtio support" + depends on VIRTIO + select XEN_GRANT_DMA_OPS + help + Enable virtio support for running as Xen guest. Depending on the + guest type this will require special support on the backend side + (qemu or kernel, depending on the virtio device types used). + + If in doubt, say n. + endmenu diff --git a/include/xen/xen.h b/include/xen/xen.h index a99bab8..0780a81 100644 --- a/include/xen/xen.h +++ b/include/xen/xen.h @@ -52,6 +52,14 @@ bool xen_biovec_phys_mergeable(const struct bio_vec *vec= 1, extern u64 xen_saved_max_mem_size; #endif =20 +#include + +static inline void xen_set_restricted_virtio_memory_access(void) +{ + if (IS_ENABLED(CONFIG_XEN_VIRTIO) && xen_domain()) + platform_set(PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS); +} + #ifdef CONFIG_XEN_UNPOPULATED_ALLOC int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages= ); void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages= ); --=20 2.7.4 From nobody Sat May 4 07:14:27 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1651947596; cv=none; d=zohomail.com; s=zohoarc; b=Kh0S9z8sCvDTbUMCPLiXcIILWxR84vtJvdRi/a1CdmWqPZOEh7UQHqsY+5ebJa27An0v1xtTEer+SsAQ0Y6Pn6g7NMmOdg4T2r+zAKmV9reF5/Q7IPP9LnMZHGAKyym7lD5MQHMUemN1JXiG7BAVcwPArJ+9XcC+K6jFJhQZ5c0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1651947596; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=f9eTxQwBpqmyE5YQv8y/mSGc6BFKrmx/9RtbXRM94yk=; b=TGNqGzrNkH3JIQSToE6MEkczC+tCwLn2pZZb2t603vXZdR6N8IfR+CGYeDaMxIFdND4aj4C2eOEkwp5Jj8xIucOx47Kym0myqSbJF4GN4IMN1L52g0BkaN3zenhbW1cUAD9fqB39MZ9aEVI+a9OKNEhvgh8H4wmbSR8bbWBiWP0= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 165194759694439.944908704450086; Sat, 7 May 2022 11:19:56 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.323759.545618 (Exim 4.92) (envelope-from ) id 1nnP1f-0007sH-AU; Sat, 07 May 2022 18:19:31 +0000 Received: by outflank-mailman (output) from mailman id 323759.545618; Sat, 07 May 2022 18:19:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1f-0007s4-48; Sat, 07 May 2022 18:19:31 +0000 Received: by outflank-mailman (input) for mailman id 323759; Sat, 07 May 2022 18:19:29 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nnP1c-0006Tm-SQ for xen-devel@lists.xenproject.org; Sat, 07 May 2022 18:19:28 +0000 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [2a00:1450:4864:20::32e]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3b384a03-ce32-11ec-a406-831a346695d4; Sat, 07 May 2022 20:19:28 +0200 (CEST) Received: by mail-wm1-x32e.google.com with SMTP id k126-20020a1ca184000000b003943fd07180so6133137wme.3 for ; Sat, 07 May 2022 11:19:28 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:26 -0700 (PDT) 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" X-Inumbo-ID: 3b384a03-ce32-11ec-a406-831a346695d4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=f9eTxQwBpqmyE5YQv8y/mSGc6BFKrmx/9RtbXRM94yk=; b=q0QvHmJEV199SvJxS8MMwMhMnT/+xhVpVqbOLeHgkt06Xhs2dBC0jVTbeKOwr91pvR ndKaasEP0xJG1RLgJyqL7ROM0gZzZQdJc/q0MNhHsXPKClgmJU88UwkBbKhnyLTjrxeH akHWUnH51O7ewtusDhyChb/efNbilF+VAy0f52Ter98LqUPNVkvzci9eD41asZ+qZHI8 cxd6Fw0R6dR9XKl9hBhot2Ml9a+SI7h0b6qTcC3h1dkFbCIGTfD57A7X/PwLBhWUviLJ RMrUlN4pHsO1OCXg4EHqgP/3qgV4jfWL/K1p0l8arjpAUs+e6MkOmajqxI/Xjre5TUMj SXUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=f9eTxQwBpqmyE5YQv8y/mSGc6BFKrmx/9RtbXRM94yk=; b=LYgk0x+raOODGwiaT56Bckp+VpV5v1BOi1404LdMTQ/ChmyNXG42wSudv1HzGWKID3 la1nsWxioDaBF3M6MbGXVVe+mcmlDfKaJmwKbbRfxwXWDns1aVVnHArVnMG4IQVJ16cs gE+cD371rgL7Kh5IrIHUdWmOsf9y2U/gICyCS+K0d5YqcsxZ4xovUHg/xWIB1KWTut+b faqOzAhFP9sKNigRBTwqrq+VOfPnPVIApQgD5fUs22wVt1C0/YWghNMZiym7+XABdvtE SH+XwUs8Fw1X1MTeMSEiTkVRjNjIhZqAu5jV8jm/kibShwLSQCgYXNwDy/yd6HG+SrXT s1OQ== X-Gm-Message-State: AOAM532XRnqaesVbMTR1nMmglkUxpNn/OCIFE7FQXgVDuyB3rPKo6DZU F0PoxK1ZfTwfR8EOgAJ8ys7lO7iQ/1s= X-Google-Smtp-Source: ABdhPJzhvjQBg6LQrWKFmAkWoYNYrT/dAdUxxfWwxE0Fo8ocoWuRQNu1extz5NXA4crK+Rzl3m2uBw== X-Received: by 2002:a05:600c:4f03:b0:394:6499:21c7 with SMTP id l3-20020a05600c4f0300b00394649921c7mr8976554wmq.103.1651947566943; Sat, 07 May 2022 11:19:26 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, virtualization@lists.linux-foundation.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Oleksandr Tyshchenko , Jason Wang , Rob Herring , Krzysztof Kozlowski , Julien Grall , Juergen Gross , Stefano Stabellini , "Michael S. Tsirkin" , Christoph Hellwig , Jean-Philippe Brucker Subject: [PATCH V2 5/7] dt-bindings: Add xen,dev-domid property description for xen-grant DMA ops Date: Sat, 7 May 2022 21:19:06 +0300 Message-Id: <1651947548-4055-6-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1651947597778100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Introduce Xen specific binding for the virtualized device (e.g. virtio) to be used by Xen grant DMA-mapping layer in the subsequent commit. This binding indicates that Xen grant mappings scheme needs to be enabled for the device which DT node contains that property and specifies the ID of Xen domain where the corresponding backend resides. The ID (domid) is used as an argument to the grant mapping APIs. This is needed for the option to restrict memory access using Xen grant mappings to work which primary goal is to enable using virtio devices in Xen guests. Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini --- Changes RFC -> V1: - update commit subject/description and text in description - move to devicetree/bindings/arm/ Changes V1 -> V2: - update text in description - change the maintainer of the binding - fix validation issue - reference xen,dev-domid.yaml schema from virtio/mmio.yaml --- .../devicetree/bindings/arm/xen,dev-domid.yaml | 37 ++++++++++++++++++= ++++ Documentation/devicetree/bindings/virtio/mmio.yaml | 7 ++++ 2 files changed, 44 insertions(+) create mode 100644 Documentation/devicetree/bindings/arm/xen,dev-domid.yaml diff --git a/Documentation/devicetree/bindings/arm/xen,dev-domid.yaml b/Doc= umentation/devicetree/bindings/arm/xen,dev-domid.yaml new file mode 100644 index 00000000..750e89e --- /dev/null +++ b/Documentation/devicetree/bindings/arm/xen,dev-domid.yaml @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: (GPL-2.0-only or BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/arm/xen,dev-domid.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Xen specific binding for virtualized devices (e.g. virtio) + +maintainers: + - Stefano Stabellini + +select: true + +description: + This binding indicates that Xen grant mappings need to be enabled for + the device, and it specifies the ID of the domain where the corresponding + device (backend) resides. The property is required to restrict memory + access using Xen grant mappings. + +properties: + xen,dev-domid: + $ref: /schemas/types.yaml#/definitions/uint32 + description: + The domid (domain ID) of the domain where the device (backend) is ru= nning. + +additionalProperties: true + +examples: + - | + virtio@3000 { + compatible =3D "virtio,mmio"; + reg =3D <0x3000 0x100>; + interrupts =3D <41>; + + /* The device is located in Xen domain with ID 1 */ + xen,dev-domid =3D <1>; + }; diff --git a/Documentation/devicetree/bindings/virtio/mmio.yaml b/Documenta= tion/devicetree/bindings/virtio/mmio.yaml index 10c22b5..29a0932 100644 --- a/Documentation/devicetree/bindings/virtio/mmio.yaml +++ b/Documentation/devicetree/bindings/virtio/mmio.yaml @@ -13,6 +13,9 @@ description: See https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=3Dvirtio= for more details. =20 +allOf: + - $ref: /schemas/arm/xen,dev-domid.yaml# + properties: compatible: const: virtio,mmio @@ -33,6 +36,10 @@ properties: description: Required for devices making accesses thru an IOMMU. maxItems: 1 =20 + xen,dev-domid: + description: Required when Xen grant mappings need to be enabled for d= evice. + $ref: /schemas/types.yaml#/definitions/uint32 + required: - compatible - reg --=20 2.7.4 From nobody Sat May 4 07:14:27 2024 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 57478C433F5 for ; Sat, 7 May 2022 18:20:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446853AbiEGSXg (ORCPT ); Sat, 7 May 2022 14:23:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446805AbiEGSXR (ORCPT ); Sat, 7 May 2022 14:23:17 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FF123054E for ; Sat, 7 May 2022 11:19:30 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id k126-20020a1ca184000000b003943fd07180so6133150wme.3 for ; Sat, 07 May 2022 11:19:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=i20ZoV0Dy7aIhximSqwDLxqvQZkpduCzRqaqQhDs+Gw=; b=kaeAxv8eQCkCyM/YsKefnviDP9GsVHb7/51955hWaPMeqjaXkbK/uyVclInXeCIUdE X8IXEo2iLyaRQragIBliJUttM3m9nzwxKVQiWuRmtv46zoTAxr+TSDhNPCDeGLBzpq1X 49JcoOjn13VevVWcxYzJrKiK6peJgDk6R4HwUT1NR3m/lvWnFhUk8bsPpDQ1f9I7QBD2 33LCxLFmevZNmxrucdGWpD9GNkaUtqTcfGH95is2ZEuijHlLJi2xWq6+Rv3EOoL7eK2c Wfthv4gIYltKPDkuCDjm9ROaxIFniVwE1HfbHBVyoPo58rw1q35dbgiELmS1saNmOgEZ oWqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=i20ZoV0Dy7aIhximSqwDLxqvQZkpduCzRqaqQhDs+Gw=; b=YjcRH+Shy5zWfrZf1cEz9JOPZVDUobPrbxTRkC6O0w0nmIPsgU5uIQSGSHSXaqkSrj BMLxbbIlvZtR1IvM+KEpVLm1lqlAVEl88xLZH2+R+Dq+wSFuvVB2ElSQZxbejGua1Bdf ZosAlIfZwbGLXM+Jd+b1ZCSFdkGYRbBVauSltkqaayhL5f2nHlbWqODMcUk/J+J3VIwO jTudt3v1DdQYHKf/YqQaw9UPVwUT0k0t6q7uKpNadlUTDIX4SBHJNml4VyuAmw85YNlT FdDAv1kQer5G6G0Lpi4WUxN1cfBgCAW5jD5sVvj7TWe09Zp4YHW4MJoZZnEXilmoVnqY jKJg== X-Gm-Message-State: AOAM532uUCjUEyRI192imb72+EdxK4QC+OPDFa7ic+ylaTdJRK85kP2w R6UQyArAMEEWTieQH9foz+A= X-Google-Smtp-Source: ABdhPJwPA0kb7aWDicLsLDhgAaPwApIyoCaRdYZ+eHdlC/fExsCO5IbDdRUI+wRMk84GSOATJZsaLQ== X-Received: by 2002:a7b:cbc2:0:b0:388:faec:2036 with SMTP id n2-20020a7bcbc2000000b00388faec2036mr9020942wmi.190.1651947568572; Sat, 07 May 2022 11:19:28 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:27 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Oleksandr Tyshchenko , Stefano Stabellini , Boris Ostrovsky , Juergen Gross , Julien Grall , "Michael S. Tsirkin" , Christoph Hellwig Subject: [PATCH V2 6/7] xen/grant-dma-ops: Retrieve the ID of backend's domain for DT devices Date: Sat, 7 May 2022 21:19:07 +0300 Message-Id: <1651947548-4055-7-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko Use the presence of recently introduced "xen,dev-domid" property in the device node as a clear indicator of enabling Xen grant mappings scheme for that device and read the ID of Xen domain where the corresponding backend resides. The ID (domid) is used as an argument to the Xen grant mapping APIs. Also introduce xen_is_grant_dma_device() to check whether xen-grant DMA ops need to be set for a passed device. Remove the hardcoded domid 0 in xen_grant_setup_dma_ops(). Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini --- Changes RFC -> V1: - new patch, split required changes from commit: "[PATCH 4/6] virtio: Various updates to xen-virtio DMA ops layer" - update checks in xen_virtio_setup_dma_ops() to only support DT devices for now - remove the "virtio,mmio" check from xen_is_virtio_device() - remane everything according to the new naming scheme: s/virtio/grant_dma Changes V1 -> V2: - remove dev_is_pci() check in xen_grant_setup_dma_ops() - remove EXPORT_SYMBOL_GPL(xen_is_grant_dma_device); --- drivers/xen/grant-dma-ops.c | 24 +++++++++++++++++------- include/xen/xen-ops.h | 5 +++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/drivers/xen/grant-dma-ops.c b/drivers/xen/grant-dma-ops.c index 29ad7bf..8924178 100644 --- a/drivers/xen/grant-dma-ops.c +++ b/drivers/xen/grant-dma-ops.c @@ -55,11 +55,6 @@ static struct xen_grant_dma_data *find_xen_grant_dma_dat= a(struct device *dev) * Such a DMA address is formed by using the grant reference as a frame * number and setting the highest address bit (this bit is for the backend * to be able to distinguish it from e.g. a mmio address). - * - * Note that for now we hard wire dom0 to be the backend domain. In order - * to support any domain as backend we'd need to add a way to communicate - * the domid of this backend, e.g. via Xenstore, via the PCI-device's - * config space or DT/ACPI. */ static void *xen_grant_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, @@ -275,6 +270,15 @@ static const struct dma_map_ops xen_grant_dma_ops =3D { .dma_supported =3D xen_grant_dma_supported, }; =20 +bool xen_is_grant_dma_device(struct device *dev) +{ + /* XXX Handle only DT devices for now */ + if (!dev->of_node) + return false; + + return of_property_read_bool(dev->of_node, "xen,dev-domid"); +} + void xen_grant_setup_dma_ops(struct device *dev) { struct xen_grant_dma_data *data; @@ -286,8 +290,14 @@ void xen_grant_setup_dma_ops(struct device *dev) return; } =20 - /* XXX The dom0 is hardcoded as the backend domain for now */ - dev_domid =3D 0; + /* XXX ACPI device unsupported for now */ + if (!dev->of_node) + goto err; + + if (of_property_read_u32(dev->of_node, "xen,dev-domid", &dev_domid)) { + dev_err(dev, "xen,dev-domid property is not present\n"); + goto err; + } =20 data =3D devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); if (!data) diff --git a/include/xen/xen-ops.h b/include/xen/xen-ops.h index 4f9fad5..62be9dc 100644 --- a/include/xen/xen-ops.h +++ b/include/xen/xen-ops.h @@ -223,10 +223,15 @@ static inline void xen_preemptible_hcall_end(void) { } =20 #ifdef CONFIG_XEN_GRANT_DMA_OPS void xen_grant_setup_dma_ops(struct device *dev); +bool xen_is_grant_dma_device(struct device *dev); #else static inline void xen_grant_setup_dma_ops(struct device *dev) { } +static inline bool xen_is_grant_dma_device(struct device *dev) +{ + return false; +} #endif /* CONFIG_XEN_GRANT_DMA_OPS */ =20 #endif /* INCLUDE_XEN_OPS_H */ --=20 2.7.4 From nobody Sat May 4 07:14:27 2024 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 1E23DC433EF for ; Sat, 7 May 2022 18:21:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1446885AbiEGSXk (ORCPT ); Sat, 7 May 2022 14:23:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1446819AbiEGSXW (ORCPT ); Sat, 7 May 2022 14:23:22 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE97630F43 for ; Sat, 7 May 2022 11:19:31 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id 125-20020a1c1983000000b003941f354c62so6139776wmz.0 for ; Sat, 07 May 2022 11:19:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=JQ4/wPcUQ3XIBwyiOH+85aEeFtvLk17ZXfaTMabSQhQ=; b=M0nL9AqbUiqgoGeBXuGJocL7BzhM9dpf6lZVm6z1MjjHA7Ksx3fAI6nsxT0Nt1TL05 V9n3RuTkUTnoTUTcWzbDZzYyXq+nDqhodBqF5QWBpLz3KG7WkLCJw+dMLaWXpmXERYF1 Z/ciVWktZyebFps/o8q2Oq5Yq41Pu3hxh+stWpNbBkwcaCkgIOQ5WYEwn7lV+jJsEdSh +HhxY/jdkwpoWo5uCgqY6+fdKmpb5rQY+LpZDKBI6NpE+HUIsQOlFS0+S/WN3zq9WuIL TiiJg4uvRYrYvsi0W51dYzG/nuX5J3xAnuGzn1LhIo6s+ydhszeik8BNC8Hg3Kloj1TO zwOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=JQ4/wPcUQ3XIBwyiOH+85aEeFtvLk17ZXfaTMabSQhQ=; b=HlmFV/R/Ea6819rQM6MBm5LVA6y4OrYdNPvYMTbNLS9IWZikBTUaCI9IOasquBKtJp WOl/ye8oGXVxPBMVCI/7sZ/UUH348+EUqL2lJtbmF7uOSGWcbsuVyW0e2NfP/huzHzOE g0MCuzmSg0BwyY/iBYWEZNIO6avmabOZV+deWPPuaV5yVQGy+/+PUrZP+pa6zimluFUi 5QJ8ipjHPRznWjyrbIks0z3G7Or7u69pHPZvJmy6okMF/X2Tykg6MBoArMSqCYsgQrDN Gr3wTAL4t9unqAw1NSaweffEk4+U3qeO302Vp57Nak03WPeWraCVxl/xpRMqSCdVuDQZ 8Jgw== X-Gm-Message-State: AOAM531XAxmS7V+hkqxDT4BWT6APNv5oz6dsVF8OrQgzGzcMCcBS1k2D LyiTuEAVAlt/sT++BUGaKLY= X-Google-Smtp-Source: ABdhPJx4KKTzPkd6Pb55J8tu0qaETBwCwPrhXqxi1bGV5Mve7UPIQCUHgzJRC6duNfUV/2m203EIIQ== X-Received: by 2002:a1c:3b54:0:b0:394:3910:3c7b with SMTP id i81-20020a1c3b54000000b0039439103c7bmr8718493wma.3.1651947570262; Sat, 07 May 2022 11:19:30 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id b10-20020a5d4d8a000000b0020c5253d8f3sm8198457wru.63.2022.05.07.11.19.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 07 May 2022 11:19:29 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: Oleksandr Tyshchenko , Boris Ostrovsky , Juergen Gross , Stefano Stabellini , Julien Grall , "Michael S. Tsirkin" , Christoph Hellwig Subject: [PATCH V2 7/7] arm/xen: Assign xen-grant DMA ops for xen-grant DMA devices Date: Sat, 7 May 2022 21:19:08 +0300 Message-Id: <1651947548-4055-8-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> References: <1651947548-4055-1-git-send-email-olekstysh@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Oleksandr Tyshchenko By assigning xen-grant DMA ops we will restrict memory access for passed device using Xen grant mappings. This is needed for using any virtualized device (e.g. virtio) in Xen guests in a safe manner. Please note, for the virtio devices the XEN_VIRTIO config should be enabled (it forces ARCH_HAS_RESTRICTED_VIRTIO_MEMORY_ACCESS). Signed-off-by: Oleksandr Tyshchenko Reviewed-by: Stefano Stabellini --- Changes RFC -> V1: - update commit subject/description - remove #ifdef CONFIG_XEN_VIRTIO - re-organize the check taking into the account that swiotlb and virtio cases are mutually exclusive - update according to the new naming scheme: s/virtio/grant_dma Changes V1 -> V2: - add Stefano's R-b - remove arch_has_restricted_virtio_memory_access() check - update commit description - remove the inclusion of virtio_config.h --- include/xen/arm/xen-ops.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/xen/arm/xen-ops.h b/include/xen/arm/xen-ops.h index 288deb1..b0766a6 100644 --- a/include/xen/arm/xen-ops.h +++ b/include/xen/arm/xen-ops.h @@ -3,11 +3,14 @@ #define _ASM_ARM_XEN_OPS_H =20 #include +#include =20 static inline void xen_setup_dma_ops(struct device *dev) { #ifdef CONFIG_XEN - if (xen_swiotlb_detect()) + if (xen_is_grant_dma_device(dev)) + xen_grant_setup_dma_ops(dev); + else if (xen_swiotlb_detect()) dev->dma_ops =3D &xen_swiotlb_dma_ops; #endif } --=20 2.7.4