From patchwork Thu Sep 12 11:15:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13801928 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AE1E6EEB594 for ; Thu, 12 Sep 2024 11:16:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 418DE6B0095; Thu, 12 Sep 2024 07:16:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C8D06B0096; Thu, 12 Sep 2024 07:16:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F4086B0098; Thu, 12 Sep 2024 07:16:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F0E226B0095 for ; Thu, 12 Sep 2024 07:16:27 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9B6B6A1F5D for ; Thu, 12 Sep 2024 11:16:27 +0000 (UTC) X-FDA: 82555832814.08.39CD472 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf19.hostedemail.com (Postfix) with ESMTP id 182BC1A0010 for ; Thu, 12 Sep 2024 11:16:25 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aLB45VxS; spf=pass (imf19.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726139732; a=rsa-sha256; cv=none; b=nYV/D69TGfjOCQTSfSHO5X1vGxubMcL4pUIYONWpazTahJckvDvVb23LoTmMTMZAaA8II0 iDNMVSK4pb30VPXyBZp5Smn4Gkgs92tXTo6ksUALyY3qEpNmW85b6Oe5YkafBeug8u43mY f9BSgqNSZSX1OT4JdzT/VWDgeNEh90s= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=aLB45VxS; spf=pass (imf19.hostedemail.com: domain of leon@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726139732; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2JnuJBl7580UzNRE/N3KmM4LmreoZWhWzDO4jU/MMb0=; b=7JVXnYV4lYuA0P/iz5RQ/NjvoMTfaayRTZl0d3fV81WrjMVrUtERDh8CZpUgQqf2FhrKqR eZ95x9VmQsQlK3qdisNLWCErYv2uV8/U0UwFD5SLRZ/SrKKB2CHAdAoY5X7NqFEfGPFwf3 sKK86aE9PZdU9wM3Jolk6au/nI5GA+Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id C5354A45257; Thu, 12 Sep 2024 11:16:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 499D0C4CECF; Thu, 12 Sep 2024 11:16:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726139785; bh=VvpomHjn0PXFDu7e/jW7/gfUbs21DN2RjkYJAyY9I0w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aLB45VxSAnSX1Omnd4tNbgxYwGvlO0vlHzxS5XRXs3Jf+H6Sv4g2apXm3/BjzYZcU jJat8T1BsFMxKIbvmMGDI3d2Wav68reJ0E3LN7sVO+ms1PwvbS5VCjz/Zp+6dn0u38 Hjyfj6EN+qrGYfmm8UXj6bzSoYbXvDIEBCuKATHLVp+eKVznmpDYlgEn6mEfIV25ac CluAye/X57cQXU1+3AFoTAvO2BnVfHu3mTsIsvPd7Y8II6FIRfLqDJj5jqYb3qQCuB HoFkBLlicit7oilmLRe8VOlKBnHex2vD837rA3akUkz25QskU2jzMySkDTK/0QW30m CBd4EkuZoW2Aw== From: Leon Romanovsky To: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Keith Busch , Christoph Hellwig , "Zeng, Oak" , Chaitanya Kulkarni Cc: Leon Romanovsky , Sagi Grimberg , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?utf-8?b?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Subject: [RFC v2 06/21] dma-mapping: set and query DMA IOVA state Date: Thu, 12 Sep 2024 14:15:41 +0300 Message-ID: <818f2fbdb80f07297ca2abe5d04443d3b665f445.1726138681.git.leon@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: 8x53833rafy9jfcxxozgdjx59jj9zhkc X-Rspamd-Queue-Id: 182BC1A0010 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1726139785-809130 X-HE-Meta: U2FsdGVkX1+hI+lNK9SnxwG4dr012Cm/36+xUk2nzezeRX8Nsz87fryd7LGTYlb7+fjzUu1IOZP9UwT8AgFZI1pFSDr2/c/HD/yUbzdTjS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2inf9QBPkHqPc4Em+mstCPwloaBzeVdeOiDFJQzVBRknwz4j3w== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Leon Romanovsky Provide an option to query and set if IOMMU path can be taken. Callers who supply range of pages can perform it only once as the whole range is supposed to have same memory type. Signed-off-by: Leon Romanovsky --- include/linux/dma-mapping.h | 12 ++++++++++++ kernel/dma/mapping.c | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 6a51d8e96a9d..2c74e68b0567 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -81,6 +81,7 @@ struct dma_iova_state { dma_addr_t addr; size_t size; enum dma_data_direction dir; + u8 use_iova : 1; }; static inline void dma_init_iova_state(struct dma_iova_state *state, @@ -169,6 +170,9 @@ void *dma_vmap_noncontiguous(struct device *dev, size_t size, void dma_vunmap_noncontiguous(struct device *dev, void *vaddr); int dma_mmap_noncontiguous(struct device *dev, struct vm_area_struct *vma, size_t size, struct sg_table *sgt); +void dma_set_iova_state(struct dma_iova_state *state, struct page *page, + size_t size); +bool dma_can_use_iova(struct dma_iova_state *state); #else /* CONFIG_HAS_DMA */ static inline int dma_alloc_iova_unaligned(struct dma_iova_state *state, phys_addr_t phys, size_t size) @@ -307,6 +311,14 @@ static inline int dma_mmap_noncontiguous(struct device *dev, { return -EINVAL; } +static inline void dma_set_iova_state(struct dma_iova_state *state, + struct page *page, size_t size) +{ +} +static inline bool dma_can_use_iova(struct dma_iova_state *state) +{ + return false; +} #endif /* CONFIG_HAS_DMA */ #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 4cd910f27dee..16cb03d5d87d 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -6,6 +6,7 @@ * Copyright (c) 2006 Tejun Heo */ #include /* for max_pfn */ +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include #include "debug.h" #include "direct.h" @@ -986,3 +988,39 @@ void dma_free_iova(struct dma_iova_state *state) iommu_dma_free_iova(state); } EXPORT_SYMBOL_GPL(dma_free_iova); + +/** + * dma_set_iova_state - Set the IOVA state for the given page and size + * @state: IOVA state + * @page: page to check + * @size: size of the page + * + * Set the IOVA state for the given page and size. The IOVA state is set + * based on the device and the page. + */ +void dma_set_iova_state(struct dma_iova_state *state, struct page *page, + size_t size) +{ + if (!use_dma_iommu(state->dev)) + return; + + state->use_iova = iommu_can_use_iova(state->dev, page, size, state->dir); +} +EXPORT_SYMBOL_GPL(dma_set_iova_state); + +/** + * dma_can_use_iova - check if the device type is valid + * and won't take SWIOTLB path + * @state: IOVA state + * + * Return %true if the device should use swiotlb for the given buffer, else + * %false. + */ +bool dma_can_use_iova(struct dma_iova_state *state) +{ + if (!use_dma_iommu(state->dev)) + return false; + + return state->use_iova; +} +EXPORT_SYMBOL_GPL(dma_can_use_iova);