Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2088606pxj; Sat, 19 Jun 2021 02:34:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXNGxkk7IF3a757KqHB9vNiFCanL8/KhSyRrnnge1tiL3c/rugFRiWZhJH85E95o2rtLma X-Received: by 2002:aa7:c782:: with SMTP id n2mr9890147eds.77.1624095253713; Sat, 19 Jun 2021 02:34:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624095253; cv=none; d=google.com; s=arc-20160816; b=TvbTaJMjju62Y3VzbYue6l+a/P7qQnEsKa7lK2QIhP0nGDG7dp5wy0FDr8jFN6eMUG YsoKfh+qo+RpO2WgxJAQvMuvkQ8/fY/DAxllbS7taWFJhweGER/RW2mW0Xk7Pmr7gVcl OuSe6BI+ktnsAhbKsePGeWyoxE5/yYXMa/XkZEOOsBhvkVvGBqiAZxGpWqQFRDW4R/vN j3Ezug68ofh4N5xiIwt9iAx/UylZHl0nUL73i7rP2hqI6Ro+/TyPvRJyZJBLMgT5Fbs1 uatK3pfDJcOyDqiZZOYbevxCrihhv2ulK5yTlXT8MJhh/fXgcWuBkd1eKtmcmMFWI5ce 3o4Q== 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=siEy8G7PeUIP5SwwgMvapyAkxanEyejlu7ONUZ9yGrs=; b=LM+522FyFUgykxS6iBGzV+bLy6xGQ1kFC0l3mCk8eC4r39lQ6tn3pXyxO8cy4g9km2 COGdFXrCpwm3F9Q/mQTu1qVNdVp4OzCRH0NlHuuGDmnGRsnuz43DQU+o+heA0x9KL0Hn tOsZOfG5ey4OgMknsHugrpSvl2DL5gNOyrd3+ACjIK1o/eUNDXz5TvGQSdxmz5ic+5JN 8I2Dw0Js6Go+CZKZAmUVV+gnS68NdCnguF3RgrnkdcKSfgwp2h3WHkynWLhATJL+V8Q7 I3NoKtSkUIdmtysIzZM6R5Y9bA8vKX9/s9sqx/b/63nLLzbvsLgxJSjoBo5vIEYSZ+j5 Esjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=PVzE47oP; 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 u14si11411746edx.165.2021.06.19.02.33.51; Sat, 19 Jun 2021 02:34:13 -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=PVzE47oP; 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 S235882AbhFSDpB (ORCPT + 99 others); Fri, 18 Jun 2021 23:45:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235662AbhFSDoe (ORCPT ); Fri, 18 Jun 2021 23:44:34 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A9F1C061760 for ; Fri, 18 Jun 2021 20:42:24 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id k6so9195983pfk.12 for ; Fri, 18 Jun 2021 20:42:24 -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=siEy8G7PeUIP5SwwgMvapyAkxanEyejlu7ONUZ9yGrs=; b=PVzE47oPcF725/coDqsJRzac3T/wkFYs0l0OUgKwwlqVYtfZ1VcPE+apLpO3PiE39E hroZdkWyJ1FY8+MMxcIXfLg8mSzUQOWKW1HghPEs/CnHdT3B8XoNEo0T/Ku1bIExGIif gFdxf+gYkQKTSymCtV+BwSVCd70PKzuU3VIss= 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=siEy8G7PeUIP5SwwgMvapyAkxanEyejlu7ONUZ9yGrs=; b=k0na3TT6Xu6lByTE++7xb1d0gzLUD8yPWYcUNsoSXlyVHe9N7mElnG0D5NuA8pQC9R aEEfJY9gB9Kx9VwW6liVjG+bhDcwJdkDvPsJFZA8Ep/TsxLv8gZ21imUJk4nshaWdjp5 RbZ8r3+nAn9P9jhsXNZDN0hC3hLqveFHw2/c4KHyd86j0SpvlJRbQSFLd0NxiaaJFauG mmDEJRLcLxui3WzJzBpf5z2/IwLu8allk8/0f0vhmXMh6XOPw6co6PtPGubb+Ioccqg0 gLigRiusQT2LjozE5tZdzyMjnWALGkHwjfpIi96lluUvMf40ka7C9TmzpmakdNnVi0KG WNjQ== X-Gm-Message-State: AOAM532J39pBpHDiw9PhrysP6u8FKvOQrouuFa+npMh0XQGku+AwHoRq SqPwLVGz2NEEgOM038EgzFLfgw== X-Received: by 2002:a63:d305:: with SMTP id b5mr13411892pgg.67.1624074143723; Fri, 18 Jun 2021 20:42:23 -0700 (PDT) Received: from localhost ([2401:fa00:95:205:4a46:e208:29e8:e076]) by smtp.gmail.com with UTF8SMTPSA id o72sm3823389pfg.102.2021.06.18.20.42.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 18 Jun 2021 20:42:23 -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, thomas.lendacky@amd.com Subject: [PATCH v14 10/12] swiotlb: Add restricted DMA pool initialization Date: Sat, 19 Jun 2021 11:40:41 +0800 Message-Id: <20210619034043.199220-11-tientzu@chromium.org> X-Mailer: git-send-email 2.32.0.288.g62a8d224e6-goog In-Reply-To: <20210619034043.199220-1-tientzu@chromium.org> References: <20210619034043.199220-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 273b21090ee8..1aef294c82b5 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; + + set_memory_decrypted((unsigned long)phys_to_virt(rmem->base), + rmem->size >> PAGE_SHIFT); + swiotlb_init_io_tlb_mem(mem, rmem->base, nslabs, false); + mem->force_bounce = true; + mem->for_alloc = true; + + 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