From mboxrd@z Thu Jan 1 00:00:00 1970 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 4BCE2D13588 for ; Mon, 28 Oct 2024 04:25:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60BA26B008A; Mon, 28 Oct 2024 00:25:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BBEA6B008C; Mon, 28 Oct 2024 00:25:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45BD96B0092; Mon, 28 Oct 2024 00:25:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 288DE6B008A for ; Mon, 28 Oct 2024 00:25:05 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4A5EC81D52 for ; Mon, 28 Oct 2024 04:24:45 +0000 (UTC) X-FDA: 82721720004.27.4BA5E90 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 154F24000E for ; Mon, 28 Oct 2024 04:24:30 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ThdcANL/"; spf=pass (imf07.hostedemail.com: domain of dev.srinivasulu@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=dev.srinivasulu@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730089293; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=m3x7sfjCk6an/jee3PqRBielnEix2rcYMcqqI8sdlR0=; b=MW0YZqug9nA5dqH0l6mNgOk1TPfHv3/c0uAuGm1h82M3ih871abFsH1lFLun8CDnuuwspJ uzyWxTbyCG2pJ+jfBE6fd3mqzXRhg7ktKZkH4hjtvaAR4dsCwS79B6jG+WEe9MkmQMmWKA 7UTV7R37aRbNMgfhTbtn9dHe9o+STq8= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="ThdcANL/"; spf=pass (imf07.hostedemail.com: domain of dev.srinivasulu@gmail.com designates 209.85.214.172 as permitted sender) smtp.mailfrom=dev.srinivasulu@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730089293; a=rsa-sha256; cv=none; b=uOzfzkYMf1xZKpkHo4zXlaLAkU7GWI8hrNMywYRi+HGqASj37VE/rKv6xaq7FOmGZqgomO +4ofmp7bjSfCnPPBRwCz2huAtTM27Neb0grO2mV8ai58A8EG7ToaruAluFUCPzzzCf++rU /lY7I6tGeq07lqdtJDbij+4/s4emSq8= Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-20e576dbc42so38304415ad.0 for ; Sun, 27 Oct 2024 21:25:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730089501; x=1730694301; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=m3x7sfjCk6an/jee3PqRBielnEix2rcYMcqqI8sdlR0=; b=ThdcANL/QslhWxNUEmvMjt19n82hEzPuSWOSVaby8L/Ov9PS6s9SG7JMcK3JVzMlhY aFFoKB3zpT3iS3h+Plxf+9p6D+9khRhJBoH6KJmfNYivSEtzRk4dvGnSXNUIjim6+NFR xGZGXRmubv/bn/ngRJASn6ISj5T8wn8URJ04FIZS11/dZRBsXV6PyshXV583NLzQGjn/ RTvhbqviRHo6vaY87V2DCXVEQ19x7rFH0iVS7bVKNyKSxhr6B8kUXIzIMj4oXGXPwlXF mxkoA609f7fw1pDOhg/Sjp1bMcqJwmbKCcdapF95ZkWXtSsNb1sGaMPR71Rwh53pSAeT aZoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730089501; x=1730694301; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=m3x7sfjCk6an/jee3PqRBielnEix2rcYMcqqI8sdlR0=; b=ei/piW0br9Skd2aH8AKQ6v7cFZy5Qoj/m5DCQhPZdodpAsJgo+bokIfRc5MHUqwvBE 3QypsSkG8UM8B0sU8ardrcfLyleON6GEZ1my9t7/CN3lxMQqVNZ8hIVzcnkhrKOL3a/3 NtTbdPBIp/snKK7v2S1fLF3pxh/ywoP75E/PYIai6vHCNwAKqzXAxdyelEpLLoH7dsSG 2BujFauyiO3PquamO7Dh7ZcZRhi2XrHQ0tgNLPq0gIJo1FASM81PAfPrKFEWM4pjI+Vk iqwVWzyx97NZxSQOe3M6XJelyAmQqTtm/cm3k9jd66qlTxVC2FMf8lpcMpbgTGIa57CT HqTQ== X-Forwarded-Encrypted: i=1; AJvYcCXjNDhJrnGSbJ1dn+ownJMaa1/9Bq5Pz/i03uBo4ai5e5SEBJiwpsAeNvVKxS4RnqgzQiXDArwYng==@kvack.org X-Gm-Message-State: AOJu0YzTJeBfVgs8MLl36dMJiqDXFw/IxkpZ4WgyK/pe3zwiyDByzMVV px8lLq90zl9Ll4nWj6VN7oVu58OfVaXf/n2sKRX5ruLfxhovWAodLiYttPXs2mNsoWEtwYfDqgQ D0Cr0uj+Kj9L+IPNy9QnQur9BFa0= X-Google-Smtp-Source: AGHT+IF/XbpZuxphQQOGSTKqE4rMFhTi6L+p1iG+0wHvbb3b0Zmpl46tdIWRMT6RjNj1jqQyIDmtIwt2bzj/qL3GX/U= X-Received: by 2002:a17:902:db02:b0:20c:b700:6e10 with SMTP id d9443c01a7336-210c6c8856cmr116967905ad.34.1730089501376; Sun, 27 Oct 2024 21:25:01 -0700 (PDT) MIME-Version: 1.0 References: <844f3dcf9c341b8178bfbc90909ef13d11dd2193.1730037276.git.leon@kernel.org> In-Reply-To: <844f3dcf9c341b8178bfbc90909ef13d11dd2193.1730037276.git.leon@kernel.org> From: Srinivasulu Thanneeru Date: Mon, 28 Oct 2024 09:54:49 +0530 Message-ID: Subject: Re: [PATCH 05/18] dma: Provide an interface to allow allocate IOVA To: Leon Romanovsky Cc: Jens Axboe , Jason Gunthorpe , Robin Murphy , Joerg Roedel , Will Deacon , Christoph Hellwig , Sagi Grimberg , Leon Romanovsky , Keith Busch , Bjorn Helgaas , Logan Gunthorpe , Yishai Hadas , Shameer Kolothum , Kevin Tian , Alex Williamson , Marek Szyprowski , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Andrew Morton , Jonathan Corbet , 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, linux-pci@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 154F24000E X-Stat-Signature: 4jcm9ypsdd9gm1w6gmx8bnihn5dam94h X-Rspam-User: X-HE-Tag: 1730089470-737151 X-HE-Meta: U2FsdGVkX18uJ5yjPwmJxN9K4AP7a+YpPAhKIyDTlDOFUbBWYFYn7bCS6TRN18/RCSdwucbkWa1vor+uyLsYfAX4tvSPVN1UvvjTdzg1pBFevqDssUKgNvdtHNrb9LxDFS8MgReaOLXN+Od8CA0UW0IFPrrst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AlnqxVgCYySBfeXWRvjia8PUI57qMui4tO4mRF46UklVBw6GJ/STGAZ0qnx+2Y+VvIXeAKdCP1ZPOUVANPmkbzYqLusKsMpgkXq2uP7L7maeoI4PCA6oXfpRvgUpuzHb5REPf8LiPS4CH42NTNF2mOJeclsh8H1JVgTZQ== 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: On Sun, Oct 27, 2024 at 10:23=E2=80=AFPM Leon Romanovsky = wrote: > > From: Leon Romanovsky > > The existing .map_page() callback provides both allocating of IOVA > and linking DMA pages. That combination works great for most of the > callers who use it in control paths, but is less effective in fast > paths where there may be multiple calls to map_page(). Can you please share perf stats with this patch in fast path, if available? > 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. > > The API is exported from dma-iommu as it is the only implementation > supported, the namespace is clearly different from iommu_* functions > which are not allowed to be used. This code layout allows us to save > function call per API call used in datapath as well as a lot of boilerpla= te > code. > > Signed-off-by: Leon Romanovsky > --- > drivers/iommu/dma-iommu.c | 79 +++++++++++++++++++++++++++++++++++++ > include/linux/dma-mapping.h | 15 +++++++ > 2 files changed, 94 insertions(+) > > diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c > index c422e36c0d66..0644152c5aad 100644 > --- a/drivers/iommu/dma-iommu.c > +++ b/drivers/iommu/dma-iommu.c > @@ -1745,6 +1745,85 @@ size_t iommu_dma_max_mapping_size(struct device *d= ev) > return SIZE_MAX; > } > > +static bool iommu_dma_iova_alloc(struct device *dev, > + struct dma_iova_state *state, phys_addr_t phys, size_t si= ze) > +{ > + struct iommu_domain *domain =3D iommu_get_dma_domain(dev); > + struct iommu_dma_cookie *cookie =3D domain->iova_cookie; > + struct iova_domain *iovad =3D &cookie->iovad; > + size_t iova_off =3D iova_offset(iovad, phys); > + dma_addr_t addr; > + > + if (WARN_ON_ONCE(!size)) > + return false; > + if (WARN_ON_ONCE(size & DMA_IOVA_USE_SWIOTLB)) > + return false; > + > + addr =3D iommu_dma_alloc_iova(domain, > + iova_align(iovad, size + iova_off), > + dma_get_mask(dev), dev); > + if (!addr) > + return false; > + > + state->addr =3D addr + iova_off; > + state->__size =3D size; > + return true; > +} > + > +/** > + * dma_iova_try_alloc - Try to allocate an IOVA space > + * @dev: Device to allocate the IOVA space for > + * @state: IOVA state > + * @phys: physical address > + * @size: IOVA size > + * > + * Check if @dev supports the IOVA-based DMA API, and if yes allocate IO= VA space > + * for the given base address and size. > + * > + * Note: @phys is only used to calculate the IOVA alignment. Callers tha= t always > + * do PAGE_SIZE aligned transfers can safely pass 0 here. > + * > + * Returns %true if the IOVA-based DMA API can be used and IOVA space ha= s been > + * allocated, or %false if the regular DMA API should be used. > + */ > +bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state= , > + phys_addr_t phys, size_t size) > +{ > + memset(state, 0, sizeof(*state)); > + if (!use_dma_iommu(dev)) > + return false; > + if (static_branch_unlikely(&iommu_deferred_attach_enabled) && > + iommu_deferred_attach(dev, iommu_get_domain_for_dev(dev))) > + return false; > + return iommu_dma_iova_alloc(dev, state, phys, size); > +} > +EXPORT_SYMBOL_GPL(dma_iova_try_alloc); > + > +/** > + * dma_iova_free - Free an IOVA space > + * @dev: Device to free the IOVA space for > + * @state: IOVA state > + * > + * Undoes a successful dma_try_iova_alloc(). > + * > + * Note that all dma_iova_link() calls need to be undone first. For cal= lers > + * that never call dma_iova_unlink(), dma_iova_destroy() can be used ins= tead > + * which unlinks all ranges and frees the IOVA space in a single efficie= nt > + * operation. > + */ > +void dma_iova_free(struct device *dev, struct dma_iova_state *state) > +{ > + struct iommu_domain *domain =3D iommu_get_dma_domain(dev); > + struct iommu_dma_cookie *cookie =3D domain->iova_cookie; > + struct iova_domain *iovad =3D &cookie->iovad; > + size_t iova_start_pad =3D iova_offset(iovad, state->addr); > + size_t size =3D dma_iova_size(state); > + > + iommu_dma_free_iova(cookie, state->addr - iova_start_pad, > + iova_align(iovad, size + iova_start_pad), NULL); > +} > +EXPORT_SYMBOL_GPL(dma_iova_free); > + > void iommu_setup_dma_ops(struct device *dev) > { > struct iommu_domain *domain =3D iommu_get_domain_for_dev(dev); > diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h > index 6075e0708deb..817f11bce7bc 100644 > --- a/include/linux/dma-mapping.h > +++ b/include/linux/dma-mapping.h > @@ -11,6 +11,7 @@ > #include > #include > #include > +#include > > /** > * List of possible attributes associated with a DMA mapping. The semant= ics > @@ -77,6 +78,7 @@ > #define DMA_BIT_MASK(n) (((n) =3D=3D 64) ? ~0ULL : ((1ULL<<(n))-1= )) > > struct dma_iova_state { > + dma_addr_t addr; > size_t __size; > }; > > @@ -307,11 +309,24 @@ static inline bool dma_use_iova(struct dma_iova_sta= te *state) > { > return state->__size !=3D 0; > } > + > +bool dma_iova_try_alloc(struct device *dev, struct dma_iova_state *state= , > + phys_addr_t phys, size_t size); > +void dma_iova_free(struct device *dev, struct dma_iova_state *state); > #else /* CONFIG_IOMMU_DMA */ > static inline bool dma_use_iova(struct dma_iova_state *state) > { > return false; > } > +static inline bool dma_iova_try_alloc(struct device *dev, > + struct dma_iova_state *state, phys_addr_t phys, size_t si= ze) > +{ > + return false; > +} > +static inline void dma_iova_free(struct device *dev, > + struct dma_iova_state *state) > +{ > +} > #endif /* CONFIG_IOMMU_DMA */ > > #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) > -- > 2.46.2 > >