Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3683517pxj; Tue, 15 Jun 2021 06:31:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzj1BDuCblUp0r5GO0b01rQ/1Ourqh4PRyeWlDiYGTJ5EImEaP0eyTHz6yCbF7uUFOXvkL X-Received: by 2002:a5d:6849:: with SMTP id o9mr24144788wrw.44.1623763861217; Tue, 15 Jun 2021 06:31:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623763861; cv=none; d=google.com; s=arc-20160816; b=yEexpe/6m21tU6QrFexHNa5aJ/oc8TIna2V+d0U1C31nUq2MzjCKAkXI1mkTZGsQNS wIY/VotzscWS+ANSpkQqvWKJOAjL2Pgd0V0xASVSftsaX6dtVndytllxSJ/3+gO3ptWG 06y0QAfFE9G30N4JL0KKjJoR8KO2N0G4f1SezRXX1tRojDZZlAbsDfYAbjWGkWJWZaXq qRF6w2kjMlbIuxns3AqnsUEbdmcdQJiVDnzbe8CBY5dE23CuhHjZcuyCunc+LESBXghC /OanS8c10RZxCFsxmROiOv3xRfeAGBiNMyOkYQVCqjG08kVQ8cx9wYns3qexCM+T3ldt H/lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=r+B+WmWHIHMs91nx15AVM2itHN+i1YzQ775bO9+kHdU=; b=P5iFinQAW/xHnJnH0N0Hr7sSZWmSBp1YfBDsWHluWVDYZLrRiWNCiLHICuU0/g1nBK wZgavMwuaiQ6ROtswRWdnleJdlQhhZTzQE9Tk1eCgb8Nbd43ND5z3H9nc+AeVTnY7ETm 0s1AgAn/C3hLYlvgqyZ8hugZjXg59T/cHn/hgO0PC40higYk3ybnI5T6aGqSkQNuvWaz t0hdy9hLA7uLKqy3iZLKKvHDy0y6JNPL0+k68uIqFq57do9/9DwYaEvzf6WuR3SYZDhx B7qGNAIsm8aDNyKgxgamm/58B5qpYLVI6ayVLiPuVWELLbqLfuSs0rbNm58uWC9ztHE9 hF2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WzvexR7E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 20si6744574ejj.661.2021.06.15.06.30.37; Tue, 15 Jun 2021 06:31:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WzvexR7E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231452AbhFONat (ORCPT + 99 others); Tue, 15 Jun 2021 09:30:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231484AbhFONar (ORCPT ); Tue, 15 Jun 2021 09:30:47 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F657C06175F for ; Tue, 15 Jun 2021 06:28:42 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id m13-20020a17090b068db02901656cc93a75so1797784pjz.3 for ; Tue, 15 Jun 2021 06:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=r+B+WmWHIHMs91nx15AVM2itHN+i1YzQ775bO9+kHdU=; b=WzvexR7E+ALC8UHpMEnA+Cx3vlr1A1Cr5s3TW1YH4PposTtGKLhFpA/R/qQszzGPqE ordIiKc6mP07Rj60smVk6CJmnBRs8OEvLe0owSLDWKCjVANuxKyE3xs4HLfFTCYWWVik a6KT5EvKiBmg9I5cDjlQBmab8/PKoLLvYg/3Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r+B+WmWHIHMs91nx15AVM2itHN+i1YzQ775bO9+kHdU=; b=t7SOXefBuhinQ0ckB3HSnqlReWLHvrhy8pPY7WWTQIy8JVjwY9HPSoV3D3Lv5Hdu2p 3wHUan2CRaHl3k/kThGyudLQLZOGB6p65sSAqxe7JHGzXofODb8qLL+pnA004H0X0E/Z 0UdqYY+qaE09O5xixpAQZR0QSpCrHekP7cA4u9kBgf0ETmFBEkHC4YbZyv81YSfNg/n+ T/bwl4Ms7Jq1KElAaKz0kQbH4QuWWd9+TvUer4p3fCqgmcV8935PtY1qmX7TBhKDS8LR 7zPlfMPdrUO2OiX5JUPLSu5A+vdyaeBRM6ceTLc3Cuioh8z4WWqcYqQJggcUCG+lPGnO QZGA== X-Gm-Message-State: AOAM5321T4vsXwn57pD+wuyfjm9mjNzV16Bn8MrTZFpKtvEPlcgufyFn eXIV/gPlk2aBh1pnDPOqh2eolA== X-Received: by 2002:a17:90b:19cd:: with SMTP id nm13mr5085576pjb.226.1623763722158; Tue, 15 Jun 2021 06:28:42 -0700 (PDT) Received: from localhost ([2401:fa00:95:205:1846:5274:e444:139e]) by smtp.gmail.com with UTF8SMTPSA id d8sm16002774pfq.198.2021.06.15.06.28.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 15 Jun 2021 06:28:41 -0700 (PDT) From: Claire Chang To: Rob Herring , mpe@ellerman.id.au, Joerg Roedel , Will Deacon , Frank Rowand , Konrad Rzeszutek Wilk , boris.ostrovsky@oracle.com, jgross@suse.com, Christoph Hellwig , Marek Szyprowski Cc: benh@kernel.crashing.org, paulus@samba.org, "list@263.net:IOMMU DRIVERS" , sstabellini@kernel.org, Robin Murphy , grant.likely@arm.com, xypron.glpk@gmx.de, Thierry Reding , mingo@kernel.org, bauerman@linux.ibm.com, peterz@infradead.org, Greg KH , Saravana Kannan , "Rafael J . Wysocki" , heikki.krogerus@linux.intel.com, Andy Shevchenko , Randy Dunlap , Dan Williams , Bartosz Golaszewski , linux-devicetree , lkml , linuxppc-dev@lists.ozlabs.org, xen-devel@lists.xenproject.org, Nicolas Boichat , Jim Quinlan , tfiga@chromium.org, bskeggs@redhat.com, bhelgaas@google.com, chris@chris-wilson.co.uk, tientzu@chromium.org, daniel@ffwll.ch, airlied@linux.ie, dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, jani.nikula@linux.intel.com, jxgao@google.com, joonas.lahtinen@linux.intel.com, linux-pci@vger.kernel.org, maarten.lankhorst@linux.intel.com, matthew.auld@intel.com, rodrigo.vivi@intel.com, thomas.hellstrom@linux.intel.com Subject: [PATCH v10 09/12] swiotlb: Add restricted DMA pool initialization Date: Tue, 15 Jun 2021 21:27:08 +0800 Message-Id: <20210615132711.553451-10-tientzu@chromium.org> X-Mailer: git-send-email 2.32.0.272.g935e593368-goog In-Reply-To: <20210615132711.553451-1-tientzu@chromium.org> References: <20210615132711.553451-1-tientzu@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add the initialization function to create restricted DMA pools from matching reserved-memory nodes. Regardless of swiotlb setting, the restricted DMA pool is preferred if available. The restricted DMA pools provide a basic level of protection against the DMA overwriting buffer contents at unexpected times. However, to protect against general data leakage and system memory corruption, the system needs to provide a way to lock down the memory access, e.g., MPU. Signed-off-by: Claire Chang --- include/linux/swiotlb.h | 3 +- kernel/dma/Kconfig | 14 ++++++++ kernel/dma/swiotlb.c | 78 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index efcd56e3a16c..e76ac46ffff9 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -73,7 +73,8 @@ extern enum swiotlb_force swiotlb_force; * range check to see if the memory was in fact allocated by this * API. * @nslabs: The number of IO TLB blocks (in groups of 64) between @start and - * @end. This is command line adjustable via setup_io_tlb_npages. + * @end. For default swiotlb, this is command line adjustable via + * setup_io_tlb_npages. * @used: The number of used IO TLB block. * @list: The free list describing the number of free entries available * from each index. diff --git a/kernel/dma/Kconfig b/kernel/dma/Kconfig index 77b405508743..3e961dc39634 100644 --- a/kernel/dma/Kconfig +++ b/kernel/dma/Kconfig @@ -80,6 +80,20 @@ config SWIOTLB bool select NEED_DMA_MAP_STATE +config DMA_RESTRICTED_POOL + bool "DMA Restricted Pool" + depends on OF && OF_RESERVED_MEM + select SWIOTLB + help + This enables support for restricted DMA pools which provide a level of + DMA memory protection on systems with limited hardware protection + capabilities, such as those lacking an IOMMU. + + For more information see + + and . + If unsure, say "n". + # # Should be selected if we can mmap non-coherent mappings to userspace. # The only thing that is really required is a way to set an uncached bit diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index 3c01162c400b..ef1ccd63534d 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -39,6 +39,13 @@ #ifdef CONFIG_DEBUG_FS #include #endif +#ifdef CONFIG_DMA_RESTRICTED_POOL +#include +#include +#include +#include +#include +#endif #include #include @@ -693,3 +700,74 @@ static int __init swiotlb_create_default_debugfs(void) late_initcall(swiotlb_create_default_debugfs); #endif + +#ifdef CONFIG_DMA_RESTRICTED_POOL +static int rmem_swiotlb_device_init(struct reserved_mem *rmem, + struct device *dev) +{ + struct io_tlb_mem *mem = rmem->priv; + unsigned long nslabs = rmem->size >> IO_TLB_SHIFT; + + /* + * Since multiple devices can share the same pool, the private data, + * io_tlb_mem struct, will be initialized by the first device attached + * to it. + */ + if (!mem) { + mem = kzalloc(struct_size(mem, slots, nslabs), GFP_KERNEL); + if (!mem) + return -ENOMEM; + + swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, false); + mem->force = true; + set_memory_decrypted((unsigned long)phys_to_virt(rmem->base), + rmem->size >> PAGE_SHIFT); + + rmem->priv = mem; + + if (IS_ENABLED(CONFIG_DEBUG_FS)) { + mem->debugfs = + debugfs_create_dir(rmem->name, debugfs_dir); + swiotlb_create_debugfs_files(mem); + } + } + + dev->dma_io_tlb_mem = mem; + + return 0; +} + +static void rmem_swiotlb_device_release(struct reserved_mem *rmem, + struct device *dev) +{ + dev->dma_io_tlb_mem = io_tlb_default_mem; +} + +static const struct reserved_mem_ops rmem_swiotlb_ops = { + .device_init = rmem_swiotlb_device_init, + .device_release = rmem_swiotlb_device_release, +}; + +static int __init rmem_swiotlb_setup(struct reserved_mem *rmem) +{ + unsigned long node = rmem->fdt_node; + + if (of_get_flat_dt_prop(node, "reusable", NULL) || + of_get_flat_dt_prop(node, "linux,cma-default", NULL) || + of_get_flat_dt_prop(node, "linux,dma-default", NULL) || + of_get_flat_dt_prop(node, "no-map", NULL)) + return -EINVAL; + + if (PageHighMem(pfn_to_page(PHYS_PFN(rmem->base)))) { + pr_err("Restricted DMA pool must be accessible within the linear mapping."); + return -EINVAL; + } + + rmem->ops = &rmem_swiotlb_ops; + pr_info("Reserved memory: created restricted DMA pool at %pa, size %ld MiB\n", + &rmem->base, (unsigned long)rmem->size / SZ_1M); + return 0; +} + +RESERVEDMEM_OF_DECLARE(dma, "restricted-dma-pool", rmem_swiotlb_setup); +#endif /* CONFIG_DMA_RESTRICTED_POOL */ -- 2.32.0.272.g935e593368-goog