From: Christoph Hellwig <hch@lst.de>
To: Leon Romanovsky <leon@kernel.org>
Cc: Christoph Hellwig <hch@lst.de>,
Robin Murphy <robin.murphy@arm.com>,
Joerg Roedel <joro@8bytes.org>, Will Deacon <will@kernel.org>,
Marek Szyprowski <m.szyprowski@samsung.com>,
Leon Romanovsky <leonro@nvidia.com>,
Easwar Hariharan <eahariha@linux.microsoft.com>,
linux-kernel@vger.kernel.org, iommu@lists.linux.dev,
Jason Gunthorpe <jgg@nvidia.com>
Subject: Re: [PATCH v2 2/2] dma: add IOMMU static calls with clear default ops
Date: Thu, 18 Jul 2024 05:48:54 +0200 [thread overview]
Message-ID: <20240718034854.GA31912@lst.de> (raw)
In-Reply-To: <dd34bf3ecef252b4910d70aa21ff5273b5e8b19b.1721219730.git.leon@kernel.org>
On Wed, Jul 17, 2024 at 03:37:11PM +0300, Leon Romanovsky wrote:
> From: Leon Romanovsky <leonro@nvidia.com>
>
> Most of the arch DMA ops (which often, but not always, involve
> some sort of IOMMU) are using the same DMA operations. These DMA
> operations are default ones implemented in drivers/iomem/dma-iommu.c.
I'm not sure I agree with the exact wording. There's tons of arch DMA
ops not in any way tied to dma-iommu (or the iommu subsystem, but not
dma-iommu like the arm32 ones), but for all modern platforms dma-iommu
is what actually matters.
> Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
> Signed-off-by: Leon Romanovsky <leon@kernel.org>
A double-signoff for the same person is a bit weird, isn't it?
> struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
> @@ -1756,10 +1733,10 @@ void iommu_setup_dma_ops(struct device *dev)
> if (iommu_is_dma_domain(domain)) {
> if (iommu_dma_init_domain(domain, dev))
> goto out_err;
> + dev->dma_iommu = true;
> + } else if (dev->dma_iommu) {
> /* Clean up if we've switched *from* a DMA domain */
> + dev->dma_iommu = false;
> }
Strictly speaking we can no remove the if part of the else if above.
Or reword this a bit to:
dev->dma_iommu = iommu_is_dma_domain(domain);
if (dev->dma_iommu && iommu_dma_init_domain(domain, dev))
goto out_err;
> diff --git a/include/linux/device.h b/include/linux/device.h
> index ace039151cb8..7fa1e40b617a 100644
> --- a/include/linux/device.h
> +++ b/include/linux/device.h
> @@ -822,6 +822,9 @@ struct device {
> #ifdef CONFIG_DMA_NEED_SYNC
> bool dma_skip_sync:1;
> #endif
> +#ifdef CONFIG_IOMMU_DMA
> + bool dma_iommu : 1;
> +#endif
The kerneldoc above should be updated to describe this field.
Please also add the maintainers of this file to the next round.
> +static inline bool dma_is_default_iommu(struct device *dev)
> +{
> +#ifdef CONFIG_IOMMU_DMA
> + return dev->dma_iommu;
> +#else
> + return false;
> +#endif
> +}
The normal style would be to move the ifdefs outside the helper
function. Also maybe name this use_dma_iommu?
> static bool dma_go_direct(struct device *dev, dma_addr_t mask,
> const struct dma_map_ops *ops)
> {
> + WARN_ON_ONCE(ops && dma_is_default_iommu(dev));
I'd prefer to keep this out of the fast path and only do it in
say dma_set_mask. And fail the call while we're at it.
> + if (likely(!ops) && !dma_is_default_iommu(dev))
The likely should cover both conditions.
> return true;
> +
> #ifdef CONFIG_DMA_OPS_BYPASS
> + WARN_ON_ONCE(dev->dma_ops_bypass && dma_is_default_iommu(dev));
> if (dev->dma_ops_bypass)
Let's skip this and think about it if we ever use the bypass for
something that is not powerpc with it's own dma ops.
> @@ -323,8 +346,12 @@ void dma_unmap_resource(struct device *dev, dma_addr_t addr, size_t size,
> const struct dma_map_ops *ops = get_dma_ops(dev);
>
> BUG_ON(!valid_dma_direction(dir));
> - if (!dma_map_direct(dev, ops) && ops->unmap_resource)
> - ops->unmap_resource(dev, addr, size, dir, attrs);
> + if (!dma_map_direct(dev, ops)) {
> + if (dma_is_default_iommu(dev))
> + iommu_dma_unmap_resource(dev, addr, size, dir, attrs);
> + else if (ops->unmap_resource)
> + ops->unmap_resource(dev, addr, size, dir, attrs);
> + }
I'd prefer to order this as:
if (dma_map_direct(dev, ops))
; /* nothing to do: uncached and no swiotlb */
else if (use_dma_iommu(dev))
iommu_dma_unmap_resource(dev, addr, size, dir, attrs);
else if (ops->unmap_resource)
ops->unmap_resource(dev, addr, size, dir, attrs);
> + return (!ops || dma_is_default_iommu(dev));
dma_is_default_iommu implies !ops, so the second condition is
redundant.
> EXPORT_SYMBOL_GPL(dma_opt_mapping_size);
> @@ -888,7 +943,12 @@ unsigned long dma_get_merge_boundary(struct device *dev)
> {
> const struct dma_map_ops *ops = get_dma_ops(dev);
>
> + if (!ops)
> + return 0; /* can't merge */
> + if (dma_is_default_iommu(dev))
> + return iommu_dma_get_merge_boundary(dev);
The second check needs to move up here.
next prev parent reply other threads:[~2024-07-18 3:49 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-17 12:37 [PATCH v2 0/2] DMA IOMMU static calls Leon Romanovsky
2024-07-17 12:37 ` [PATCH v2 1/2] dma: call unconditionally to unmap_page and unmap_sg callbacks Leon Romanovsky
2024-07-18 3:49 ` Christoph Hellwig
2024-07-18 6:33 ` Leon Romanovsky
2024-07-17 12:37 ` [PATCH v2 2/2] dma: add IOMMU static calls with clear default ops Leon Romanovsky
2024-07-18 3:48 ` Christoph Hellwig [this message]
2024-07-18 7:04 ` Leon Romanovsky
2024-07-18 12:30 ` Christoph Hellwig
2024-07-18 13:26 ` Leon Romanovsky
2024-07-29 12:41 ` Mostafa Saleh
2024-07-29 14:07 ` Leon Romanovsky
2024-07-29 17:15 ` Christoph Hellwig
2024-07-30 8:16 ` Leon Romanovsky
2024-07-30 15:26 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240718034854.GA31912@lst.de \
--to=hch@lst.de \
--cc=eahariha@linux.microsoft.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=leon@kernel.org \
--cc=leonro@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=m.szyprowski@samsung.com \
--cc=robin.murphy@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.