From patchwork Tue Mar 5 11:18:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leon Romanovsky X-Patchwork-Id: 13582152 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 7D9BAC54798 for ; Tue, 5 Mar 2024 11:19:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CAE0940018; Tue, 5 Mar 2024 06:19:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 75285940007; Tue, 5 Mar 2024 06:19:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 557B3940018; Tue, 5 Mar 2024 06:19:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3F1AD940007 for ; Tue, 5 Mar 2024 06:19:10 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C9A2180DC3 for ; Tue, 5 Mar 2024 11:19:09 +0000 (UTC) X-FDA: 81862738818.05.587138E Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf14.hostedemail.com (Postfix) with ESMTP id 9FC8C100003 for ; Tue, 5 Mar 2024 11:19:07 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ExrnqGWc; spf=pass (imf14.hostedemail.com: domain of leon@kernel.org designates 145.40.73.55 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=1709637548; 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=ZV22DEkctZnBDhVO2mpwxlurhUyOLjlkYP+TkKjvMOA=; b=6GQ+T26F9K0PIiFCFkYIJpD+b/yU3DjlmbNlw/SbK7ssII1FqzircDccP6FzYgybbtQmyw tuZqd9VmLyL8BNYCZ839uqwCXgy8QRyigMcvjg3y4MP98fYCRFYTBFRriob005q6uePD8W 0znw8S8QTr0zJP9BvEjIUPAs+hqwSMg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709637548; a=rsa-sha256; cv=none; b=6ryhxoj/ZkJzk01JtKSXaj57i2XN4rM4dUuDwIWvscT55Vrd1rVhnrenRDkiBf3O92d3c9 CctqMKvWMLkS67AuJ7pcWBEzCJ9+zl+3/vc1Xui+wMe79eeoZFMPPdG287aFhPrt/CHf2E gcMEqOIR7J1UTpyQk+wuUiqMAaaEupU= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ExrnqGWc; spf=pass (imf14.hostedemail.com: domain of leon@kernel.org designates 145.40.73.55 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 sin.source.kernel.org (Postfix) with ESMTP id 95634CE1870; Tue, 5 Mar 2024 11:19:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 337BFC43399; Tue, 5 Mar 2024 11:19:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1709637542; bh=XGhek5Egl9EZN5GDA4K/P4lgi/cIEFOzTc1LzkqAZ+k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ExrnqGWc0DJr7OpJKj54LQl6f6CrjIMS2nso33ODtmHYOG4xoeRiikz9mKhHtlevS OW18Yww55g/DlhzTuNXVBVpBGGcPumW3WdmaZ+BSIXs71LW5gBPDeb7bGZAQYLyvPv eofh7gi2/b9TS7bAWiucQd7AmGomodjGllpcXaLcXsgET3VyyxDkx+LqTWXrUBpjFt XZlLtS7lCvQofgbhGV6A2fJWURQN6Cq8QMD8H14fX9kTepqWmYZdq08iM8zY8rsSnt iAcZ0j9KR8OSybyHpCjS9/R4re/Fny883NywNVBcPJBPDkiabjHWIsmcoJpY3J4vjr YJS7GIjPXBQ2A== From: Leon Romanovsky To: Christoph Hellwig , Robin Murphy , Marek Szyprowski , Joerg Roedel , Will Deacon , Jason Gunthorpe , Chaitanya Kulkarni Cc: Leon Romanovsky , Jonathan Corbet , Jens Axboe , Keith Busch , Sagi Grimberg , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , =?utf-8?b?SsOpcsO0bWUgR2xp?= =?utf-8?b?c3Nl?= , Andrew Morton , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-rdma@vger.kernel.org, iommu@lists.linux.dev, linux-nvme@lists.infradead.org, kvm@vger.kernel.org, linux-mm@kvack.org, Bart Van Assche , Damien Le Moal , Amir Goldstein , "josef@toxicpanda.com" , "Martin K. Petersen" , "daniel@iogearbox.net" , Dan Williams , "jack@suse.com" , Zhu Yanjun Subject: [RFC RESEND 02/16] dma-mapping: provide an interface to allocate IOVA Date: Tue, 5 Mar 2024 13:18:33 +0200 Message-ID: <54a3554639bfb963c9919c5d7c1f449021bebdb3.1709635535.git.leon@kernel.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 X-Stat-Signature: niz5aea4afkgtz3mhzjhfid3xxunwczp X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 9FC8C100003 X-Rspam-User: X-HE-Tag: 1709637547-949344 X-HE-Meta: U2FsdGVkX1+XJv0ge43qaVcoTrzSLuUroxhmF4BKYE7IjbekeS7otG19UeDtBwy9iJjxMCoMltW3s2hjQ4kRxcPtdGLbQhKQy+nG6eC2L8FSISgCZnHdO8SaCr0okYYtPmSIU+eVmXEa5yTYoMGx1844fqYqFNbMunNccWWbD7JzIa1HpJpXTe6fvFeNTSYTR4FiKMlaU+v4lfG5hDR95NDhq8s2Hhod+vQ5qXQxnaOXPM4MFCIC4PytyQyaHBewq0sHRVsIDluLazcmh7BpLblfPDT66yxzs7xp83v7EWqx5THXE/hq4YVLLDCZ4gYtF5YxKXLWGDEU+Uc3phs7Jn6B43QyOPNxIt4YLT2pgog5t1neqgk1nhooc8zCluJyfhGZc8aRGghyaQCKUuCOf1bwnlBfkaIA+oEhonofa+lDOUYwKih6l8VSAF9Wck/mgt4B+vpaCdWCpjn2jU5lcp6t5c7/AY+m+Qs39XbREO+PKcR3mzGv7HUHeydcxZwb024Drzn+tx0Hiu7htIwP4BYbG9KiRkULsy5EWWvYgNoRNUY9LQBTZp4nx3KFbIcuuwcgdMBTb5ycOxYYYIPCDHRznE7Up0AJEJXeCrGrSY6xQhAkluK6dFulyZLQZud7k/ICL41Z+E1rhq6JfP1aO77vZe6oXCbHNjLdcW9Lm0cmQnleMKy1+ryHdaiR/D7K2ZzYcQXEBICECrU8371euDGTvNcJNtBzIl/Jk6H4GzrBcq/I/j5u3fprxh7uvHwAJRs0HAludRS6ZG89alCbTSKcpQSS4bsM6MzeDP8rRCz345wNJmk/82FGEcIT0r10Im872zhme/4H2Ppwkvb89DHcaI27e4jRaHVEFTsVkCmQjXCt0bJeUNPsHdUcS5NbfU+GveCSWCIRqMj4wUmvi0pOy7s4WJUmkjqcemgPgjSlmkodFaB45N1KJ1uHobov78vTJ7U8WZLSzTOdEmj bzg== 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 Existing .map_page() callback provides two things at the same time: allocates IOVA and links DMA pages. That combination works great for most of the callers who use it in control paths, but less effective in fast paths. These advanced callers already manage their data in some sort of database and can perform IOVA allocation in advance, leaving range linkage operation to be in fast path. Provide an interface to allocate/deallocate IOVA and next patch link/unlink DMA ranges to that specific IOVA. Signed-off-by: Leon Romanovsky --- include/linux/dma-map-ops.h | 3 +++ include/linux/dma-mapping.h | 20 ++++++++++++++++++++ kernel/dma/mapping.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/include/linux/dma-map-ops.h b/include/linux/dma-map-ops.h index 4abc60f04209..bd605b44bb57 100644 --- a/include/linux/dma-map-ops.h +++ b/include/linux/dma-map-ops.h @@ -83,6 +83,9 @@ struct dma_map_ops { size_t (*max_mapping_size)(struct device *dev); size_t (*opt_mapping_size)(void); unsigned long (*get_merge_boundary)(struct device *dev); + + dma_addr_t (*alloc_iova)(struct device *dev, size_t size); + void (*free_iova)(struct device *dev, dma_addr_t dma_addr, size_t size); }; #ifdef CONFIG_DMA_OPS diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index 4a658de44ee9..176fb8a86d63 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -91,6 +91,16 @@ static inline void debug_dma_map_single(struct device *dev, const void *addr, } #endif /* CONFIG_DMA_API_DEBUG */ +struct dma_iova_attrs { + /* OUT field */ + dma_addr_t addr; + /* IN fields */ + struct device *dev; + size_t size; + enum dma_data_direction dir; + unsigned long attrs; +}; + #ifdef CONFIG_HAS_DMA static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) { @@ -101,6 +111,9 @@ static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr) return 0; } +int dma_alloc_iova(struct dma_iova_attrs *iova); +void dma_free_iova(struct dma_iova_attrs *iova); + 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); @@ -159,6 +172,13 @@ 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); #else /* CONFIG_HAS_DMA */ +static inline int dma_alloc_iova(struct dma_iova_attrs *iova) +{ + return -EOPNOTSUPP; +} +static inline void dma_free_iova(struct dma_iova_attrs *iova) +{ +} 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 58db8fd70471..b6b27bab90f3 100644 --- a/kernel/dma/mapping.c +++ b/kernel/dma/mapping.c @@ -183,6 +183,36 @@ void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size, } EXPORT_SYMBOL(dma_unmap_page_attrs); +int dma_alloc_iova(struct dma_iova_attrs *iova) +{ + struct device *dev = iova->dev; + const struct dma_map_ops *ops = get_dma_ops(dev); + + if (dma_map_direct(dev, ops) || !ops->alloc_iova) { + iova->addr = 0; + return 0; + } + + iova->addr = ops->alloc_iova(dev, iova->size); + if (dma_mapping_error(dev, iova->addr)) + return -ENOMEM; + + return 0; +} +EXPORT_SYMBOL(dma_alloc_iova); + +void dma_free_iova(struct dma_iova_attrs *iova) +{ + struct device *dev = iova->dev; + const struct dma_map_ops *ops = get_dma_ops(dev); + + if (dma_map_direct(dev, ops) || !ops->free_iova) + return; + + ops->free_iova(dev, iova->addr, iova->size); +} +EXPORT_SYMBOL(dma_free_iova); + static int __dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir, unsigned long attrs) {