From patchwork Tue Jul 2 09:09:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13719170 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 80DD0C30658 for ; Tue, 2 Jul 2024 09:10:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 947C26B00A1; Tue, 2 Jul 2024 05:10:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F5076B00A2; Tue, 2 Jul 2024 05:10:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 795A36B00A3; Tue, 2 Jul 2024 05:10:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 589C46B00A1 for ; Tue, 2 Jul 2024 05:10:24 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 092EA121FA5 for ; Tue, 2 Jul 2024 09:10:24 +0000 (UTC) X-FDA: 82294241568.26.62D7E5C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf18.hostedemail.com (Postfix) with ESMTP id 572F71C000F for ; Tue, 2 Jul 2024 09:10:22 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=B0P6xSie; spf=pass (imf18.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719911399; 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=rbchBOHSC9nt77VwqMfCPdOcuY7N/xjcpgjKjtqVxUQ=; b=KZtq2Xl86fNlTMzj9ajrS6OFqbYGjkqHkwuo5uo5oMwOXOm6htI1FtG6hDWLbsw+k2JyLn 6+LBL5wAmyrSqET/POb2kzm328MyRr/pMJBz+7/TSXd6g6oVljfkWkz0/4/953BCt7ZGga xemvYYlEjAkbu6cx1w6QsFQeFaVFxi8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719911399; a=rsa-sha256; cv=none; b=r596BU4TDNOjWnblOjrcxejrSfzwn/VuQKQewTBkpUiqf/iYoflfFiU3qb4PE1Q0F2SXrn oXnuwIWNsqKW1S+AqjPccfw7Fo3EueB9Te3m//wca5KwR0/6tigEqZk4FvgZrQ8STnw4Uj bQMA91ncycq32H8ILdG4HxI3QSxCgnY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=B0P6xSie; spf=pass (imf18.hostedemail.com: domain of leon@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=leon@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 6611C61A2A; Tue, 2 Jul 2024 09:10:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26C0AC4AF0D; Tue, 2 Jul 2024 09:10:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1719911420; bh=SqRJkswAXpWAsD53KsFAbDwitxXp1HHK8jZehp+jNVQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B0P6xSien3K1c31vVmNKC6qS649bWpjI5mYxXWH8emcmDGg2eigH8FA2DdzWikUeV OTGCsKzhgCxVn0yFExmtnvDeYvSdw03wWMH3YCkE0Is5H/+FrihVahL3vShxIXpt5H ekIE/hI3wHawgVD7zRlm3769EJWxwN6cjJq9LjWDY8CwX07zGC8TIt4ox3kCloi44h 8tByePaihSYoAZbPDlvArG+KbAuvnYoS6wLwWd3uqYtGr/qXHBBtoAFke7v4CagF12 +Ba7rdX8nQKBDA4nkUOmts95lMSEr2/fvOG5MxU0r8Xs/bA45+3GA81F9bXChbQUkP XrFGs/s9NsLMw== 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 PATCH v1 06/18] dma-mapping: provide callbacks to link/unlink HMM PFNs to specific IOVA Date: Tue, 2 Jul 2024 12:09:36 +0300 Message-ID: X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: qxe78ycsy9yt1x3nu4bdqotna9pfzxxj X-Rspamd-Queue-Id: 572F71C000F X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1719911422-92444 X-HE-Meta: U2FsdGVkX198PbgmRAh/hL+CiJGPekIJbYTKxOjlrmuY/pQfJ50v9IFJ/D60V96vt1xmLraSE1l/eOx74K2K0j8xf7JOyws67uVJdPKJybRjyLrPYjmP84XKmaw51XAUlLAAUM9embkAm1umtDO72lGzzDWdIhdc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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 Introduce new DMA link/unlink API to provide a way for HMM users to link pages to already preallocated IOVA. Signed-off-by: Leon Romanovsky --- include/linux/dma-mapping.h | 15 +++++ kernel/dma/mapping.c | 108 ++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index c530095ff232..2578b6615a2f 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -135,6 +135,10 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) int dma_alloc_iova(struct dma_iova_attrs *iova); void dma_free_iova(struct dma_iova_attrs *iova); +dma_addr_t dma_hmm_link_page(unsigned long *pfn, struct dma_iova_attrs *iova, + dma_addr_t dma_offset); +void dma_hmm_unlink_page(unsigned long *pfn, struct dma_iova_attrs *iova, + dma_addr_t dma_offset); dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, size_t offset, size_t size, enum dma_data_direction dir, @@ -199,6 +203,17 @@ static inline int dma_alloc_iova(struct dma_iova_attrs *iova) static inline void dma_free_iova(struct dma_iova_attrs *iova) { } +static inline dma_addr_t dma_hmm_link_page(unsigned long *pfn, + struct dma_iova_attrs *iova, + dma_addr_t dma_offset) +{ + return DMA_MAPPING_ERROR; +} +static inline void dma_hmm_unlink_page(unsigned long *pfn, + struct dma_iova_attrs *iova, + dma_addr_t dma_offset) +{ +} static inline dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page, size_t offset, size_t size, enum dma_data_direction dir, unsigned long attrs) diff --git a/kernel/dma/mapping.c b/kernel/dma/mapping.c index 089b4a977bab..69c431bd89e6 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "debug.h" #include "direct.h" @@ -1076,3 +1077,110 @@ void dma_unlink_range(struct dma_iova_state *state) ops->unlink_range(state, state->iova->addr, state->range_size); } EXPORT_SYMBOL_GPL(dma_unlink_range); + +/** + * dma_hmm_link_page - Link a physical HMM page to DMA address + * @pfn: HMM PFN + * @iova: Preallocated IOVA space + * @dma_offset: DMA offset form which this page needs to be linked + * + * dma_alloc_iova() allocates IOVA based on the size specified by their use in + * iova->size. Call this function after IOVA allocation to link whole @page + * to get the DMA address. Note that very first call to this function + * will have @dma_offset set to 0 in the IOVA space allocated from + * dma_alloc_iova(). For subsequent calls to this function on same @iova, + * @dma_offset needs to be advanced by the caller with the size of previous + * page that was linked + DMA address returned for the previous page that was + * linked by this function. + */ +dma_addr_t dma_hmm_link_page(unsigned long *pfn, struct dma_iova_attrs *iova, + dma_addr_t dma_offset) +{ + struct device *dev = iova->dev; + struct page *page = hmm_pfn_to_page(*pfn); + phys_addr_t phys = page_to_phys(page); + bool coherent = dev_is_dma_coherent(dev); + struct dma_memory_type type = {}; + struct dma_iova_state state = {}; + dma_addr_t addr; + int ret; + + if (*pfn & HMM_PFN_DMA_MAPPED) + /* + * We are in this flow when there is a need to resync flags, + * for example when page was already linked in prefetch call + * with READ flag and now we need to add WRITE flag + * + * This page was already programmed to HW and we don't want/need + * to unlink and link it again just to resync flags. + * + * The DMA address calculation below is based on the fact that + * HMM doesn't work with swiotlb. + */ + return (iova->addr) ? iova->addr + dma_offset : + phys_to_dma(dev, phys); + + dma_get_memory_type(page, &type); + + state.iova = iova; + state.type = &type; + state.range_size = dma_offset; + + if (!dma_can_use_iova(&state, PAGE_SIZE)) { + if (!coherent && !(iova->attrs & DMA_ATTR_SKIP_CPU_SYNC)) + arch_sync_dma_for_device(phys, PAGE_SIZE, iova->dir); + + addr = phys_to_dma(dev, phys); + goto done; + } + + ret = dma_start_range(&state); + if (ret) + return DMA_MAPPING_ERROR; + + ret = dma_link_range(&state, page_to_phys(page), PAGE_SIZE); + dma_end_range(&state); + if (ret) + return DMA_MAPPING_ERROR; + + addr = iova->addr + dma_offset; +done: + kmsan_handle_dma(page, 0, PAGE_SIZE, iova->dir); + *pfn |= HMM_PFN_DMA_MAPPED; + return addr; +} +EXPORT_SYMBOL_GPL(dma_hmm_link_page); + +/** + * dma_hmm_unlink_page - Unlink a physical HMM page from DMA address + * @pfn: HMM PFN + * @iova: Preallocated IOVA space + * @dma_offset: DMA offset form which this page needs to be unlinked + * from the IOVA space + */ +void dma_hmm_unlink_page(unsigned long *pfn, struct dma_iova_attrs *iova, + dma_addr_t dma_offset) +{ + struct device *dev = iova->dev; + struct page *page = hmm_pfn_to_page(*pfn); + struct dma_memory_type type = {}; + struct dma_iova_state state = {}; + const struct dma_map_ops *ops = get_dma_ops(dev); + + dma_get_memory_type(page, &type); + + state.iova = iova; + state.type = &type; + + *pfn &= ~HMM_PFN_DMA_MAPPED; + + if (!dma_can_use_iova(&state, PAGE_SIZE)) { + if (!(iova->attrs & DMA_ATTR_SKIP_CPU_SYNC)) + dma_direct_sync_single_for_cpu(dev, dma_offset, + PAGE_SIZE, iova->dir); + return; + } + + ops->unlink_range(&state, state.iova->addr + dma_offset, PAGE_SIZE); +} +EXPORT_SYMBOL_GPL(dma_hmm_unlink_page);