Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp168855pxj; Wed, 16 Jun 2021 23:29:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAx/9GO03rBJr0yUetzrnp1VelZdnecDIO9UIXPWrrltjLv9UIge68lInylzL5MR/VopCM X-Received: by 2002:a05:6e02:54e:: with SMTP id i14mr2450378ils.129.1623911364640; Wed, 16 Jun 2021 23:29:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623911364; cv=none; d=google.com; s=arc-20160816; b=BX8Tl8AF1tVtJtoJDEu5NZFAO36KY+2CGyHqIn85kg9Gq9RJ5qOTocGkIsRfKUF6Sw xuG8t1YDyGt/IoFk8jdJVTNHBxacjBXbuPZlt25AOGKaikR+iVTMBmUaCphGTgrUZYqt d46oACHNM+kkhjoEXgH6PwKJA39dTjeQk+1iM3MG9397c/f2jkwfiKf1BuvqN+2hIvf6 uOR8zG4PDhVbA4UdmImq6CPj2CXGGxBJq04fUCUGSggrnfIToaHk3WRbSi74+mdf4IEw /ba0sLpZzvq3489ccuKxDJEhNLDBB/rsca4jvdQjMn1tRBLTmsL5Afq8WWuRY9HuiZSb 3+Vg== 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=YcJPdW3IJKdu8Vg2GAKBiOP1MfG4JF6ejtcxzo1Zojw=; b=vuaQkEWKQIgp/dkAOM/LPzl1X3t96ENdkIf9aNVCyXxT/qa2KFP38tQ0I7vNQhhT2G 0zL4rHcF7X9sJcNqTEs55sAeJFIPFoez21T2tcss1femEWc9vd3evGhj4IOWLZCtVLtR lwS9+YVHJlrpOnBlcarcPWDXpfwa81S2oB3aamoilQJiW5uvhsJQrXzPk3wbZdTOFym0 vHkqmP6NA3aag6JFkLkhq7oaUJNGkHaym89yYjXzpsM3CNeHFdfb3FN4rivgh3Uznhby h6UbKbIJxsR9kkXCSM5OGyU5gbXcp05qFslADeq/4y4TOg1Os7Ov7FEt9CtDlk0/Xuuc bcYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=WLRzcZL+; 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 d70si5941995iog.4.2021.06.16.23.29.12; Wed, 16 Jun 2021 23:29:24 -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=WLRzcZL+; 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 S230083AbhFQGaX (ORCPT + 99 others); Thu, 17 Jun 2021 02:30:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230380AbhFQGaU (ORCPT ); Thu, 17 Jun 2021 02:30:20 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD16DC061760 for ; Wed, 16 Jun 2021 23:28:13 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id m15-20020a17090a5a4fb029016f385ffad0so410335pji.0 for ; Wed, 16 Jun 2021 23:28:13 -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=YcJPdW3IJKdu8Vg2GAKBiOP1MfG4JF6ejtcxzo1Zojw=; b=WLRzcZL+Sp/o8JxRFJy9NQzEr/G3yFoLjJpxsgkVdU9mIalPFmQgD7zkZHyjNgav1G t4ZHSy7CENJJjqq74/Nq6GDw0sr3iB9qBLUdGrpnfiVyc0+bDCIwUCBGA/hTAmTh6xBM onFKTV0nEVzcKHlXT23XylR9k1E7YfCcKx5qg= 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=YcJPdW3IJKdu8Vg2GAKBiOP1MfG4JF6ejtcxzo1Zojw=; b=hy+/JExP4r478xAu8okwwhI1DQkGU6auKjXuAP+fdDITBkf3YoRJRRGC8TZWKgBj5i 9zSUJdz/pldnm9qwO5PpLeNhyoGkzBCKAzUWgfqYZaxckCb3vaUNigWZ4S2cX1qM0miI noH4nc27aXzmxpziorhD/bfsOM68Rc6I+syuV50moT+L8Djag4vFoIrzwwiyj5+eT+vN c7mIyiATU8tl0R6h+kvHDcn2+Wt+Z4mDfXWJtmygHFXWbnjEaaZUSApNyP/g94PCPulu lg2X8v6qmOse+y8RvRpsnAf1nxRei5ahjC4+UJx+iewKV/jANG+WilAsWFtY4M9eYbpp wkcA== X-Gm-Message-State: AOAM533OiJU+wL9aOWh8Njor08v2QzdjF5H4liQTqaBMsJHdAm55F3+p 4AoJ+mo/vRuHeyEmb4tXhETbkg== X-Received: by 2002:a17:90a:7e0a:: with SMTP id i10mr3834074pjl.133.1623911293360; Wed, 16 Jun 2021 23:28:13 -0700 (PDT) Received: from localhost ([2401:fa00:95:205:e349:a6ae:d3d0:1621]) by smtp.gmail.com with UTF8SMTPSA id w15sm3701122pjg.32.2021.06.16.23.28.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Jun 2021 23:28:12 -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 v13 10/12] swiotlb: Add restricted DMA pool initialization Date: Thu, 17 Jun 2021 14:26:33 +0800 Message-Id: <20210617062635.1660944-11-tientzu@chromium.org> X-Mailer: git-send-email 2.32.0.288.g62a8d224e6-goog In-Reply-To: <20210617062635.1660944-1-tientzu@chromium.org> References: <20210617062635.1660944-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 Reviewed-by: Christoph Hellwig Tested-by: Stefano Stabellini Tested-by: Will Deacon --- include/linux/swiotlb.h | 3 +- kernel/dma/Kconfig | 14 ++++++++ kernel/dma/swiotlb.c | 76 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index a73fad460162..175b6c113ed8 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 6499cfbfe95f..d4099f03b2f0 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 @@ -736,4 +743,73 @@ bool swiotlb_free(struct device *dev, struct page *page, size_t size) return true; } +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_bounce = true; + mem->for_alloc = 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.288.g62a8d224e6-goog