From patchwork Thu Jun 2 19:23:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Oleksandr Tyshchenko X-Patchwork-Id: 12868081 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 47F83C433EF for ; Thu, 2 Jun 2022 19:25:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=y6E+Ntt0Vsvtu99aqEdNMJiXRvIzwPR7+yItX9BYCtk=; b=CrhAwwMK/Ve59E Umb0cEfkB0mpMdmIXbinxrk1vkkMEurRxFMhVHwrhRyqO93KhWZ7REUS5vajt1pRxkW6eRqTPpVwO JKo1JI4A/qw3vQW9FUDu9OHT1mllcs9Wo9o42oOWOkwzCpuzTdacmmTP5geR2DDJlb9mKSMqGFgOY y8g05ONdcvP3LZRiciZGxM/iRU6Br4PJsqqRHCnZel1CGWfjr7WpSud6bvUtfryN16Q/uRNlWqYmm X+FRaftT44SuHcnWUZ4tZGhsEEiQYSfAtzQvB+7VjzBAZUrCH5WmzyxifSkPysGU6BQZPOU6Ply+4 sV+8gF4pQG8DFSQkyYrw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nwqQi-00444S-Rd; Thu, 02 Jun 2022 19:24:25 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nwqQe-00440V-8Z for linux-arm-kernel@lists.infradead.org; Thu, 02 Jun 2022 19:24:22 +0000 Received: by mail-ej1-x636.google.com with SMTP id s6so262277eja.0 for ; Thu, 02 Jun 2022 12:24:17 -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:mime-version :content-transfer-encoding; bh=uZWf8OfW06wjRVZAgl1HK+YLzR5URPKYW1hO+2XycMY=; b=WXCbAGOdwthNz7JOaFwSAUnxA/ls8r/957G0EZDOoet9Ob1zjdRpDX2mg5f7nRnQMe i7STT3KF5AT8zIFgesLqvyTUBaBAlR5fiYvzZ+Fws0kLwrNbREbLOhCQbg7kloyNOGhN RnenAJBMA6moK+K4pnXCG4gdMKwFnwfLhiEwFjpWRXcdTIqWwNglLc40m8Yn6zAu1pLu tpLnrRaBP3GCVuBAREF1faftktovinZR7ZIR2gDTIwlx3VvIt5dvw7EUscc+xzBE6NXC Y8wPNXXUy/gecJ9bHOIdAFdYMOAS5N76qAxkz8MPPGJlGmLdVZF+VeBdGiJFhM6vWQgz GKlw== 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:mime-version :content-transfer-encoding; bh=uZWf8OfW06wjRVZAgl1HK+YLzR5URPKYW1hO+2XycMY=; b=jDFAgtk2j0XCY4wZ9Zl4jd5LMcRbcQi4ThKXKcmnnMDcK1/RrKi2eF9djjLjsSauB7 lDdS40AW6Slxy+UIeC7mpnSYNo97b5ZZ/3zcWcR+ArtINEBC9P3PzPA5N2kUMqnyPfAp y0BqIpqv5eHf89sJWScPRiz3c/SEDb4SRTS2B754zNq6FEDksa6TJF/c7S/Brg6tnKMO apx8DWzrxBzRut0U253Sood+TY6VP7fZqzT9zpD5pZbV7uiASuYzhFuna0WlPE452xUc CxEnDYlD7B5zazxzYDgVeU6D9j4EPLlqilLo48EOA2YXteuy3fGmaFxjJuo1yZd44Dzm MjXQ== X-Gm-Message-State: AOAM53227FeisNRvBZcCCwdsIiJZcEGR+6oBUklpsmtNdKp7B9SDB2Yx 7YJpF2FASf/360RTme51yeM= X-Google-Smtp-Source: ABdhPJzGG+rmpMMdlJHu1m2b41/LSpc6IQX1oGnbgOADMIvgbQTpOYfgkFHqvKd7AUU946nanljHwg== X-Received: by 2002:a17:907:7f0d:b0:6ff:b84:a4aa with SMTP id qf13-20020a1709077f0d00b006ff0b84a4aamr5807189ejc.595.1654197855998; Thu, 02 Jun 2022 12:24:15 -0700 (PDT) Received: from otyshchenko.router ([212.22.223.21]) by smtp.gmail.com with ESMTPSA id eg13-20020a056402288d00b0042dce73168csm2938301edb.13.2022.06.02.12.24.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 Jun 2022 12:24:15 -0700 (PDT) From: Oleksandr Tyshchenko To: xen-devel@lists.xenproject.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, virtualization@lists.linux-foundation.org, x86@kernel.org Cc: Oleksandr Tyshchenko , "Michael S. Tsirkin" , Christoph Hellwig , Stefano Stabellini , Boris Ostrovsky , Juergen Gross , Julien Grall , Bertrand Marquis , Wei Chen , Henry Wang , Kaly Xin , Jiamei Xie , =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH V4 0/8] virtio: Solution to restrict memory access under Xen using xen-grant DMA-mapping layer Date: Thu, 2 Jun 2022 22:23:45 +0300 Message-Id: <1654197833-25362-1-git-send-email-olekstysh@gmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220602_122420_333568_6B2B884F X-CRM114-Status: GOOD ( 24.51 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Oleksandr Tyshchenko Hello all. The purpose of this patch series is to add support for restricting memory access under Xen using specific grant table [1] based DMA-mapping layer. Patch series is based on Juergen Gross’ initial work [2] which implies using grant references instead of raw guest physical addresses (GPA) for the virtio communications (some kind of the software IOMMU). You can find RFC-V3 patch series (and previous discussions) at [3]. !!! Please note, the only diff between V3 and V4 is in commit #5, also I have collected the acks (commits ##4-7). The high level idea is to create new Xen’s grant table based DMA-mapping layer for the guest Linux whose main purpose is to provide a special 64-bit DMA address which is formed by using the grant reference (for a page to be shared with the backend) with offset and setting the highest address bit (this is for the backend to be able to distinguish grant ref based DMA address from normal GPA). For this to work we need the ability to allocate contiguous (consecutive) grant references for multi-page allocations. And the backend then needs to offer VIRTIO_F_ACCESS_PLATFORM and VIRTIO_F_VERSION_1 feature bits (it must support virtio-mmio modern transport for 64-bit addresses in the virtqueue). Xen's grant mapping mechanism is the secure and safe solution to share pages between domains which proven to work and works for years (in the context of traditional Xen PV drivers for example). So far, the foreign mapping is used for the virtio backend to map and access guest memory. With the foreign mapping, the backend is able to map arbitrary pages from the guest memory (or even from Dom0 memory). And as the result, the malicious backend which runs in a non-trusted domain can take advantage of this. Instead, with the grant mapping the backend is only allowed to map pages which were explicitly granted by the guest before and nothing else. According to the discussions in various mainline threads this solution would likely be welcome because it perfectly fits in the security model Xen provides. What is more, the grant table based solution requires zero changes to the Xen hypervisor itself at least with virtio-mmio and DT (in comparison, for example, with "foreign mapping + virtio-iommu" solution which would require the whole new complex emulator in hypervisor in addition to new functionality/hypercall to pass IOVA from the virtio backend running elsewhere to the hypervisor and translate it to the GPA before mapping into P2M or denying the foreign mapping request if no corresponding IOVA-GPA mapping present in the IOMMU page table for that particular device). We only need to update toolstack to insert "xen,grant-dma" IOMMU node (to be referred by the virtio-mmio device using "iommus" property) when creating a guest device-tree (this is an indicator for the guest to use Xen grant mappings scheme for that device with the endpoint ID being used as ID of Xen domain where the corresponding backend is running, the backend domid is used as an argument to the grant mapping APIs). It worth mentioning that toolstack patch is based on non upstreamed yet “Virtio support for toolstack on Arm” series which is on review now [4]. Please note the following: - Patch series only covers Arm and virtio-mmio (device-tree) for now. To enable the restricted memory access feature on Arm the following option should be set: CONFIG_XEN_VIRTIO=y - Patch series is based on "kernel: add new infrastructure for platform_has() support" patch series which is on review now [5] - Xen should be built with the following options: CONFIG_IOREQ_SERVER=y CONFIG_EXPERT=y Patch series is rebased on "for-linus-5.19" branch [1] with "platform_has()" series applied and tested on Renesas Salvator-X board + H3 ES3.0 SoC (Arm64) with standalone userspace (non-Qemu) virtio-mmio based virtio-disk backend running in Driver domain and Linux guest running on existing virtio-blk driver (frontend). No issues were observed. Guest domain 'reboot/destroy' use-cases work properly. I have also tested other use-cases such as assigning several virtio block devices or a mix of virtio and Xen PV block devices to the guest. Patch series was build-tested on Arm32 and x86. 1. Xen changes located at (last patch): https://github.com/otyshchenko1/xen/commits/libxl_virtio_next2_1 2. Linux changes located at (last 8 patches): https://github.com/otyshchenko1/linux/commits/virtio_grant9 3. virtio-disk changes located at: https://github.com/otyshchenko1/virtio-disk/commits/virtio_grant Any feedback/help would be highly appreciated. [1] https://xenbits.xenproject.org/docs/4.16-testing/misc/grant-tables.txt [2] https://www.youtube.com/watch?v=IrlEdaIUDPk [3] https://lore.kernel.org/xen-devel/1649963973-22879-1-git-send-email-olekstysh@gmail.com/ https://lore.kernel.org/xen-devel/1650646263-22047-1-git-send-email-olekstysh@gmail.com/ https://lore.kernel.org/xen-devel/1651947548-4055-1-git-send-email-olekstysh@gmail.com/ https://lore.kernel.org/xen-devel/1653944417-17168-1-git-send-email-olekstysh@gmail.com/ [4] https://lore.kernel.org/xen-devel/1654106261-28044-1-git-send-email-olekstysh@gmail.com/ https://lore.kernel.org/xen-devel/1653944813-17970-1-git-send-email-olekstysh@gmail.com/ [5] https://lore.kernel.org/xen-devel/20220504155703.13336-1-jgross@suse.com/ [6] https://git.kernel.org/pub/scm/linux/kernel/git/xen/tip.git/log/?h=for-linus-5.19 Juergen Gross (3): xen/grants: support allocating consecutive grants xen/grant-dma-ops: Add option to restrict memory access under Xen xen/virtio: Enable restricted memory access using Xen grant mappings Oleksandr Tyshchenko (5): arm/xen: Introduce xen_setup_dma_ops() dt-bindings: Add xen,grant-dma IOMMU description for xen-grant DMA ops xen/grant-dma-iommu: Introduce stub IOMMU driver xen/grant-dma-ops: Retrieve the ID of backend's domain for DT devices arm/xen: Assign xen-grant DMA ops for xen-grant DMA devices .../devicetree/bindings/iommu/xen,grant-dma.yaml | 39 +++ arch/arm/include/asm/xen/xen-ops.h | 2 + arch/arm/mm/dma-mapping.c | 7 +- arch/arm/xen/enlighten.c | 2 + arch/arm64/include/asm/xen/xen-ops.h | 2 + arch/arm64/mm/dma-mapping.c | 7 +- arch/x86/xen/enlighten_hvm.c | 2 + arch/x86/xen/enlighten_pv.c | 2 + drivers/xen/Kconfig | 20 ++ drivers/xen/Makefile | 2 + drivers/xen/grant-dma-iommu.c | 78 +++++ drivers/xen/grant-dma-ops.c | 345 +++++++++++++++++++++ drivers/xen/grant-table.c | 251 ++++++++++++--- include/xen/arm/xen-ops.h | 18 ++ include/xen/grant_table.h | 4 + include/xen/xen-ops.h | 13 + include/xen/xen.h | 8 + 17 files changed, 756 insertions(+), 46 deletions(-) create mode 100644 Documentation/devicetree/bindings/iommu/xen,grant-dma.yaml 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 drivers/xen/grant-dma-iommu.c create mode 100644 drivers/xen/grant-dma-ops.c create mode 100644 include/xen/arm/xen-ops.h