Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1548330pxj; Wed, 19 May 2021 08:22:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzyAq9XQ41s3Ko3dqN2sOxAad9GoQGCIvGo+jFpUfCpIkg0uPPwLfj5D8nvsevj5wG6aR9Y X-Received: by 2002:a17:906:c448:: with SMTP id ck8mr13208023ejb.497.1621437770422; Wed, 19 May 2021 08:22:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621437770; cv=none; d=google.com; s=arc-20160816; b=cbSjmLWtKR9IiZYdKfPeK+GTTFRYZKax2hxC1tFS2TYomG+/EirdtlUDT4kkR16ea4 hDmySB3EvtqHKiEfJikxzVwMoI6dSLBozwDaJmvNhfokdIeGb8Yv5rqbaOFb0NZ1zMVg g1rBDAUkLezA0QBUPP8yXs4jZYO0yNFXRBWoZYJa4l3jrJPRDXYtYEzc33EzdNjJ4q5z wR3ghL18jUJHs9R3ledy7HuVAH+p93A7VrRhjvfoXxlzf9Pc5Of1iiIfifgK2ei4WWPH 3kFte0PEQCtqqFn31b+8Sbe3ZpcN2MA2UBLUru6qt6adgOr5IV1gKjR18FZxQE/jjH3u kEdA== 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=JT5d6YAbqSvmA2KlfQD5hf+kbiYfdJYrN1IkT+aNsag=; b=pnpd179x51KVxxPgd0+L54Zl+3tnAVxCkKm6BgdqYSfmEOL0DBzGo8JE01yP1KFCvF HW0R9xH03hZX6eU7+r8dOReed0XB4Iiyn40i3ZmNxZx/iTdF2CU+AM2lLBA/qHVXu0wu PWbp++Ys3LzLcLmfzRUecEYv7ZRLIn4mOoHkBEKwEk4IghDlhwSz4s7nXnUFxRriR0p8 MmNR8TLWtxsDH+D1Vpxd99s27K1tFJrcq65S9rkLYRhlS2MnfXPcei+XCHDtYKKWmzz5 f6BVM/XFIBBHCdbbwkxFtO5s7JEOvRTj6VtPL+4vACsBOmgOfCsCCmOnx1f4M2QVwscE Jvhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=ZD9htTky; 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 q2si26276180ejx.574.2021.05.19.08.22.04; Wed, 19 May 2021 08:22:50 -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=ZD9htTky; 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 S236547AbhERGpA (ORCPT + 99 others); Tue, 18 May 2021 02:45:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240259AbhERGo7 (ORCPT ); Tue, 18 May 2021 02:44:59 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A14DC06175F for ; Mon, 17 May 2021 23:43:42 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id s4so2972790plg.12 for ; Mon, 17 May 2021 23:43: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=JT5d6YAbqSvmA2KlfQD5hf+kbiYfdJYrN1IkT+aNsag=; b=ZD9htTkyvkMMEUEkLQy/gcEpM7a0MIQiTSmGWFCNXxq+cS4fwyD1/1tr56UCMPrm90 EVzumZJoS3llTlb5ISKK/cVcn3v918VUbLmvCTKfyykZEE8GPe8Bv+qSbFp6wqzg5TUP ctly0anMGiSEZrCnObhPGgfqy4/ugiUoIEdJo= 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=JT5d6YAbqSvmA2KlfQD5hf+kbiYfdJYrN1IkT+aNsag=; b=GXW8es3ddQYyJhDmz031j5CnOPqMinKwQ4/gaVbTCbaBWo1hodhCYTNhdFMATcr3DA mzK3V9iocFyRAvv42zU+g1rXpNcHibPVsQqKBCyY+ZUYEaAmvtRHi8Z3t4DfZG8HNK0T 5+/145hb4Vcm4Ssdn1TdovzaXR7/sOKl8Fa/NAjElzG4o3BatwccYGJpeVDlCGC3T8wH cTbg/0g0EP3954En17OywsWGA4DvweOpB6neVBNmnkurvM7q3wblHsMbQ+h5nsgFl7Vc A+iiVmxDB9nCY554GPJhY/MDruvBIuhd/e26r215Mkm2bGX5Sq1INM5au3ef0B66vOXA yd9w== X-Gm-Message-State: AOAM530MdKniKD74Qooxl2XoYThfE6DX5eNd46pMn2Q4WzTRxN2PIblU i1dR/52NvOZw7DaMzcJZyJHfTw== X-Received: by 2002:a17:90a:bc4a:: with SMTP id t10mr3648458pjv.4.1621320221637; Mon, 17 May 2021 23:43:41 -0700 (PDT) Received: from localhost ([2401:fa00:95:205:f284:b819:54ca:c198]) by smtp.gmail.com with UTF8SMTPSA id c16sm11590759pgl.79.2021.05.17.23.43.34 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 17 May 2021 23:43: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 v7 08/15] swiotlb: Bounce data from/to restricted DMA pool if available Date: Tue, 18 May 2021 14:42:08 +0800 Message-Id: <20210518064215.2856977-9-tientzu@chromium.org> X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog In-Reply-To: <20210518064215.2856977-1-tientzu@chromium.org> References: <20210518064215.2856977-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. Note that is_dev_swiotlb_force doesn't check if swiotlb_force == SWIOTLB_FORCE. Otherwise the memory allocation behavior with default swiotlb will be changed by the following patche ("dma-direct: Allocate memory from restricted DMA pool if available"). Signed-off-by: Claire Chang --- include/linux/swiotlb.h | 13 +++++++++++++ kernel/dma/direct.c | 3 ++- kernel/dma/direct.h | 3 ++- kernel/dma/swiotlb.c | 8 ++++---- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/linux/swiotlb.h b/include/linux/swiotlb.h index c530c976d18b..0c5a18d9cf89 100644 --- a/include/linux/swiotlb.h +++ b/include/linux/swiotlb.h @@ -120,6 +120,15 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) return mem && paddr >= mem->start && paddr < mem->end; } +static inline bool is_dev_swiotlb_force(struct device *dev) +{ +#ifdef CONFIG_DMA_RESTRICTED_POOL + if (dev->dma_io_tlb_mem) + return true; +#endif /* CONFIG_DMA_RESTRICTED_POOL */ + return false; +} + void __init swiotlb_exit(void); unsigned int swiotlb_max_segment(void); size_t swiotlb_max_mapping_size(struct device *dev); @@ -131,6 +140,10 @@ static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) { return false; } +static inline bool is_dev_swiotlb_force(struct device *dev) +{ + return false; +} static inline void swiotlb_exit(void) { } diff --git a/kernel/dma/direct.c b/kernel/dma/direct.c index 7a88c34d0867..078f7087e466 100644 --- a/kernel/dma/direct.c +++ b/kernel/dma/direct.c @@ -496,7 +496,8 @@ size_t dma_direct_max_mapping_size(struct device *dev) { /* If SWIOTLB is active, use its maximum mapping size */ if (is_swiotlb_active(dev) && - (dma_addressing_limited(dev) || swiotlb_force == SWIOTLB_FORCE)) + (dma_addressing_limited(dev) || swiotlb_force == SWIOTLB_FORCE || + is_dev_swiotlb_force(dev))) return swiotlb_max_mapping_size(dev); return SIZE_MAX; } diff --git a/kernel/dma/direct.h b/kernel/dma/direct.h index 13e9e7158d94..f94813674e23 100644 --- a/kernel/dma/direct.h +++ b/kernel/dma/direct.h @@ -87,7 +87,8 @@ 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 (unlikely(swiotlb_force == SWIOTLB_FORCE) || + is_dev_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 68e7633f11fe..95f482c4408c 100644 --- a/kernel/dma/swiotlb.c +++ b/kernel/dma/swiotlb.c @@ -347,7 +347,7 @@ void __init swiotlb_exit(void) static void swiotlb_bounce(struct device *dev, phys_addr_t tlb_addr, size_t size, enum dma_data_direction dir) { - struct io_tlb_mem *mem = io_tlb_default_mem; + struct io_tlb_mem *mem = get_io_tlb_mem(dev); int index = (tlb_addr - mem->start) >> IO_TLB_SHIFT; phys_addr_t orig_addr = mem->slots[index].orig_addr; size_t alloc_size = mem->slots[index].alloc_size; @@ -429,7 +429,7 @@ static unsigned int wrap_index(struct io_tlb_mem *mem, unsigned int index) static int find_slots(struct device *dev, phys_addr_t orig_addr, size_t alloc_size) { - struct io_tlb_mem *mem = io_tlb_default_mem; + struct io_tlb_mem *mem = get_io_tlb_mem(dev); unsigned long boundary_mask = dma_get_seg_boundary(dev); dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(dev, mem->start) & boundary_mask; @@ -506,7 +506,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr, size_t mapping_size, size_t alloc_size, enum dma_data_direction dir, unsigned long attrs) { - struct io_tlb_mem *mem = io_tlb_default_mem; + struct io_tlb_mem *mem = get_io_tlb_mem(dev); unsigned int offset = swiotlb_align_offset(dev, orig_addr); unsigned int i; int index; @@ -557,7 +557,7 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, size_t mapping_size, enum dma_data_direction dir, unsigned long attrs) { - struct io_tlb_mem *mem = io_tlb_default_mem; + struct io_tlb_mem *mem = get_io_tlb_mem(hwdev); unsigned long flags; unsigned int offset = swiotlb_align_offset(hwdev, tlb_addr); int index = (tlb_addr - offset - mem->start) >> IO_TLB_SHIFT; -- 2.31.1.751.gd2f1c929bd-goog