Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp257470pxb; Mon, 8 Feb 2021 22:31:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxd8IIncYfexT0giRNuQU7LHjtTLKFn/xxemildBKGg2IsAqPcUuFZvr19cXwBO7Ggma77a X-Received: by 2002:a17:906:3e42:: with SMTP id t2mr20999385eji.439.1612852315910; Mon, 08 Feb 2021 22:31:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612852315; cv=none; d=google.com; s=arc-20160816; b=ub588sYtPF8N2mE7cShwb1QqrMS/qdDor2HQvdLO6QntJciZ4/3tn45WvrXtAQS+pw ygXYhQNhpEQLfIvffy3LKl6+QQP3+E6b5MICC+NPLiw6vFncCb9+vexwdx8jku/qGQIs 8E/lM1e5mbH0blbeynXxE9rfAJsplz/DzqxAAWMc+vCmSGsmCYZFZpwL3O6CGUSNwkX8 EUP6UIRxdwc7P7fa6oNVJEHxXJQEpaQQGCbb0OjMZTzkEKdpyH+7rP7OUo6BVsb1dl4q ZcxO3vtQqE8Nvv2j2M55s1RqXCbJrUp/FWd2pLDScAuTMjWFc/QmnnTciUNeOrcHaDu6 QJfg== 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=KUcKCdqxUtmE3c3/sCLCFX58+aEPLItGYu5wwEcivTc=; b=ITAFBhQ7R+4VnbDTRnjnDIOsZ8nsjc1Qlgm0TPsF64EHF138Xe/p6Q+92IYbgirH4j Oa25ZHE0s0tmlsp36F27JJs/v2rUkE076zfq2cBBmYWtXU7OmW7v1q0fKXGDt+HLhPDT /50kfBzAUjqfnhwL10Lm1ZvAss8zKx0uxlb7jhzHk9/dDLQwXAc4DtADgDY4yWyUO/WM 8/mds13hUMmcX9Bqh+zeVz7jGsCkK5hPdz7qkgiw59/Naev7Ti0YezCCWu4iYoAgjlej 20m8LZyFe4K+Gj3dq4OT2iZqCQJ6tv2rTgqOfbbP0oJYkYgf2SOofWIcu70u5jLLvRmC s30A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NhEm9T2A; 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 5si6555083ejv.339.2021.02.08.22.31.32; Mon, 08 Feb 2021 22:31:55 -0800 (PST) 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=NhEm9T2A; 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 S230167AbhBIG2o (ORCPT + 99 others); Tue, 9 Feb 2021 01:28:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230102AbhBIGX5 (ORCPT ); Tue, 9 Feb 2021 01:23:57 -0500 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 AEA70C06178A for ; Mon, 8 Feb 2021 22:22:40 -0800 (PST) Received: by mail-pj1-x1029.google.com with SMTP id cv23so972160pjb.5 for ; Mon, 08 Feb 2021 22:22:40 -0800 (PST) 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=KUcKCdqxUtmE3c3/sCLCFX58+aEPLItGYu5wwEcivTc=; b=NhEm9T2AwADa9VsvefRZO3hJf81eglORlqEc/M/a8rR0kuSIY5UrlWGbaksqIVirGr J6QDJ3CyNkeplzaOrje+vpzgt129yWbwHTzlb+beGZFWZQ8LQ0tH754TJy6h0q3jS8jo cGxvFhbRKlBHTIBko/408oiJovV3JTKwJW8QU= 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=KUcKCdqxUtmE3c3/sCLCFX58+aEPLItGYu5wwEcivTc=; b=gaAuqdADbjDoMeeSmzfMBCF30dmCQXNoTcO8Ft5rtYaE9gbF9FOQLYqPeDhyd2A4oh in10i7J3wpXG1meASmB+GG2zYyLo33qM53khZLgEOe0Unv2Pm88TnYSXD+8GGCZDcXNc DWpjI29A70fAQ9kCoauu7pq990isqQ1kr4hYuTlSjzN3xxJLG+tsUy3vIiLdUC+xgIpc j6/iewT4k6CrVrsXcX5dNlBoG39M4x+dOmexEKZt1oNtHiSvzidaf3LPOPJ6rV0h+oh7 TUvLtBE7UKpFjpoetbZffOsh7F7zuU2Y7B88FZSIX7z+duzH2u4o7P3qVOBBfmzmSMns IJ1w== X-Gm-Message-State: AOAM532By1Y9+8iSSOlHiDjwWELbyCp5DDlkG8GpNnvLVDIp0gbQVT6l peL+Efpmw0D55+Enw7ciELvigg== X-Received: by 2002:a17:90a:49c4:: with SMTP id l4mr2647464pjm.33.1612851760260; Mon, 08 Feb 2021 22:22:40 -0800 (PST) Received: from localhost ([2401:fa00:1:10:a106:46e1:a999:81df]) by smtp.gmail.com with UTF8SMTPSA id x14sm20837364pfj.15.2021.02.08.22.22.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Feb 2021 22:22:39 -0800 (PST) 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 , Claire Chang Subject: [PATCH v4 08/14] swiotlb: Use restricted DMA pool if available Date: Tue, 9 Feb 2021 14:21:25 +0800 Message-Id: <20210209062131.2300005-9-tientzu@chromium.org> X-Mailer: git-send-email 2.30.0.478.g8a0d178c01-goog In-Reply-To: <20210209062131.2300005-1-tientzu@chromium.org> References: <20210209062131.2300005-1-tientzu@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 13 +++++++++++++ kernel/dma/direct.h | 2 +- kernel/dma/swiotlb.c | 20 +++++++++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index f13a52a97382..76f86c684524 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -71,6 +71,15 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t phys, #ifdef CONFIG_SWIOTLB extern enum swiotlb_force swiotlb_force; +#ifdef CONFIG_DMA_RESTRICTED_POOL +bool is_swiotlb_force(struct device *dev); +#else +static inline bool is_swiotlb_force(struct device *dev) +{ + return unlikely(swiotlb_force == SWIOTLB_FORCE); +} +#endif /* CONFIG_DMA_RESTRICTED_POOL */ + bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr); void __init swiotlb_exit(void); unsigned int swiotlb_max_segment(void); @@ -80,6 +89,10 @@ phys_addr_t get_swiotlb_start(struct device *dev); void __init swiotlb_adjust_size(unsigned long new_size); #else #define swiotlb_force SWIOTLB_NO_FORCE +static inline bool is_swiotlb_force(struct device *dev) +{ + return false; +} static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) { return false; diff --git a/kernel/dma/direct.h b/kernel/dma/direct.h index 7b83b1595989..b011db1b625d 100644 --- a/kernel/dma/direct.h +++ b/kernel/dma/direct.h @@ -87,7 +87,7 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev, phys_addr_t phys = page_to_phys(page) + offset; dma_addr_t dma_addr = phys_to_dma(dev, phys); - if (unlikely(swiotlb_force == SWIOTLB_FORCE)) + if (is_swiotlb_force(dev)) return swiotlb_map(dev, phys, size, dir, attrs); if (unlikely(!dma_capable(dev, dma_addr, size, true))) { diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c index e22e7ae75f1c..6fdebde8fb1f 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -40,6 +40,7 @@ #include #endif #ifdef CONFIG_DMA_RESTRICTED_POOL +#include #include #include #include @@ -109,6 +110,10 @@ static struct swiotlb default_swiotlb; static inline struct swiotlb *get_swiotlb(struct device *dev) { +#ifdef CONFIG_DMA_RESTRICTED_POOL + if (dev && dev->dev_swiotlb) + return dev->dev_swiotlb; +#endif return &default_swiotlb; } @@ -508,7 +513,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr, size_t mapping_size, size_t alloc_size, enum dma_data_direction dir, unsigned long attrs) { - struct swiotlb *swiotlb = &default_swiotlb; + struct swiotlb *swiotlb = get_swiotlb(hwdev); dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, swiotlb->start); unsigned long flags; phys_addr_t tlb_addr; @@ -519,7 +524,11 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr, unsigned long max_slots; unsigned long tmp_io_tlb_used; +#ifdef CONFIG_DMA_RESTRICTED_POOL + if (no_iotlb_memory && !hwdev->dev_swiotlb) +#else if (no_iotlb_memory) +#endif panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); if (mem_encrypt_active()) @@ -641,7 +650,7 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, size_t mapping_size, size_t alloc_size, enum dma_data_direction dir, unsigned long attrs) { - struct swiotlb *swiotlb = &default_swiotlb; + struct swiotlb *swiotlb = get_swiotlb(hwdev); unsigned long flags; int i, count, nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; int index = (tlb_addr - swiotlb->start) >> IO_TLB_SHIFT; @@ -689,7 +698,7 @@ void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr, size_t size, enum dma_data_direction dir, enum dma_sync_target target) { - struct swiotlb *swiotlb = &default_swiotlb; + struct swiotlb *swiotlb = get_swiotlb(hwdev); int index = (tlb_addr - swiotlb->start) >> IO_TLB_SHIFT; phys_addr_t orig_addr = swiotlb->orig_addr[index]; @@ -801,6 +810,11 @@ late_initcall(swiotlb_create_default_debugfs); #endif #ifdef CONFIG_DMA_RESTRICTED_POOL +bool is_swiotlb_force(struct device *dev) +{ + return unlikely(swiotlb_force == SWIOTLB_FORCE) || dev->dev_swiotlb; +} + static int rmem_swiotlb_device_init(struct reserved_mem *rmem, struct device *dev) { -- 2.30.0.478.g8a0d178c01-goog