* [PATCH v3 0/3] iommu: Clean up cookie and sw_msi in struct iommu_domain
@ 2025-03-03 20:52 Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
` (2 more replies)
0 siblings, 3 replies; 16+ messages in thread
From: Nicolin Chen @ 2025-03-03 20:52 UTC (permalink / raw)
To: jgg, kevin.tian, robin.murphy, joro, will; +Cc: iommu, linux-kernel
A few things in iommu_domain structure (such as iova_cookie, iommufd_hwpt,
fault handler, and etc) are exclusive to certain types of iommu_domains.
So, all of them can be seen as domain cookies in different cookie types.
Furthermore, iova_cookie is shared with the msi_cookie use case. It could
be cleaner by decoupling these too.
Define an enum iommu_domain_cookie_type to fit all those cookies in.
Since there are only two sw_msi implementations, check domain->cookie_type
in iommu_dma_prepare_msi() to call the corresponding sw_msi function.
This is a clean-up series for the previous sw_msi Part-1 core series. It's
on github:
https://github.com/nicolinc/iommufd/commits/iommufd_msi_cleanup-v3
Changelog
v3
* Integrate Robin's patch
https://lore.kernel.org/linux-iommu/4e68d5820be06adc1b34fc0d1c9399481151daee.1740742271.git.robin.murphy@arm.com/
replacing private_data_owner with the new cookie_type
* Move iommu_sw_msi stub from the public header to iommu-priv.h
v2
https://lore.kernel.org/all/cover.1740705776.git.nicolinc@nvidia.com/
* Drop sw_msi function pointer
* Add a new private_data_owner tag in iommu_domain
* Let iommu core call the two sw_msi implementations directly
v1
https://lore.kernel.org/all/cover.1740600272.git.nicolinc@nvidia.com/
Thanks
Nicolin
Nicolin Chen (2):
iommufd: Move iommufd_sw_msi and related functions to driver.c
iommu: Drop sw_msi from iommu_domain
Robin Murphy (1):
iommu: Sort out domain user data
drivers/iommu/dma-iommu.h | 14 ++
drivers/iommu/iommu-priv.h | 13 ++
drivers/iommu/iommufd/iommufd_private.h | 7 +-
include/linux/iommu.h | 35 ++--
drivers/iommu/dma-iommu.c | 207 ++++++++++++------------
drivers/iommu/iommu-sva.c | 1 +
drivers/iommu/iommu.c | 34 +++-
drivers/iommu/iommufd/device.c | 127 +--------------
drivers/iommu/iommufd/driver.c | 125 ++++++++++++++
drivers/iommu/iommufd/hw_pagetable.c | 4 +-
10 files changed, 313 insertions(+), 254 deletions(-)
base-commit: 5e9f822c9c683ae884fa5e71df41d1647b2876c6
--
2.43.0
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-03 20:52 [PATCH v3 0/3] iommu: Clean up cookie and sw_msi in struct iommu_domain Nicolin Chen
@ 2025-03-03 20:52 ` Nicolin Chen
2025-03-05 15:08 ` kernel test robot
` (4 more replies)
2025-03-03 20:52 ` [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain Nicolin Chen
2 siblings, 5 replies; 16+ messages in thread
From: Nicolin Chen @ 2025-03-03 20:52 UTC (permalink / raw)
To: jgg, kevin.tian, robin.murphy, joro, will; +Cc: iommu, linux-kernel
From: Robin Murphy <robin.murphy@arm.com>
When DMA/MSI cookies were made first-class citizens back in commit
46983fcd67ac ("iommu: Pull IOVA cookie management into the core"), there
was no real need to further expose the two different cookie types.
However, now that IOMMUFD wants to add a third type of MSI-mapping
cookie, we do have a nicely compelling reason to properly dismabiguate
things at the domain level beyond just vaguely guessing from the domain
type.
Meanwhile, we also effectively have another "cookie" in the form of the
anonymous union for other user data, which isn't much better in terms of
being vague and unenforced. The fact is that all these cookie types are
mutually exclusive, in the sense that combining them makes zero sense
and/or would be catastrophic (iommu_set_fault_handler() on an SVA
domain, anyone?) - the only combination which *might* be reasonable is
perhaps a fault handler and an MSI cookie, but nobody's doing that at
the moment, so let's rule it out as well for the sake of being clear and
robust. To that end, we pull DMA and MSI cookies apart a little more,
mostly to clear up the ambiguity at domain teardown, then for clarity
(and to save a little space), move them into the union, whose ownership
we can then properly describe and enforce entirely unambiguously.
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
[nicolinc: rebase on latest tree; use prefix IOMMU_COOKIE_; merge unions
in iommu_domain; add IOMMU_COOKIE_IOMMUFD for iommufd_hwpt]
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/dma-iommu.h | 5 +
include/linux/iommu.h | 20 ++-
drivers/iommu/dma-iommu.c | 193 ++++++++++++++-------------
drivers/iommu/iommu-sva.c | 1 +
drivers/iommu/iommu.c | 17 ++-
drivers/iommu/iommufd/hw_pagetable.c | 1 +
6 files changed, 139 insertions(+), 98 deletions(-)
diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h
index c12d63457c76..9cca11806e5d 100644
--- a/drivers/iommu/dma-iommu.h
+++ b/drivers/iommu/dma-iommu.h
@@ -13,6 +13,7 @@ void iommu_setup_dma_ops(struct device *dev);
int iommu_get_dma_cookie(struct iommu_domain *domain);
void iommu_put_dma_cookie(struct iommu_domain *domain);
+void iommu_put_msi_cookie(struct iommu_domain *domain);
int iommu_dma_init_fq(struct iommu_domain *domain);
@@ -40,6 +41,10 @@ static inline void iommu_put_dma_cookie(struct iommu_domain *domain)
{
}
+static inline void iommu_put_msi_cookie(struct iommu_domain *domain)
+{
+}
+
static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
{
}
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index e93d2e918599..06cc14e9993d 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -41,6 +41,7 @@ struct iommu_dirty_ops;
struct notifier_block;
struct iommu_sva;
struct iommu_dma_cookie;
+struct iommu_dma_msi_cookie;
struct iommu_fault_param;
struct iommufd_ctx;
struct iommufd_viommu;
@@ -165,6 +166,15 @@ struct iommu_domain_geometry {
bool force_aperture; /* DMA only allowed in mappable range? */
};
+enum iommu_domain_cookie_type {
+ IOMMU_COOKIE_NONE,
+ IOMMU_COOKIE_DMA_IOVA,
+ IOMMU_COOKIE_DMA_MSI,
+ IOMMU_COOKIE_FAULT_HANDLER,
+ IOMMU_COOKIE_SVA,
+ IOMMU_COOKIE_IOMMUFD,
+};
+
/* Domain feature flags */
#define __IOMMU_DOMAIN_PAGING (1U << 0) /* Support for iommu_map/unmap */
#define __IOMMU_DOMAIN_DMA_API (1U << 1) /* Domain for use in DMA-API
@@ -211,12 +221,12 @@ struct iommu_domain_geometry {
struct iommu_domain {
unsigned type;
+ enum iommu_domain_cookie_type cookie_type;
const struct iommu_domain_ops *ops;
const struct iommu_dirty_ops *dirty_ops;
const struct iommu_ops *owner; /* Whose domain_alloc we came from */
unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */
struct iommu_domain_geometry geometry;
- struct iommu_dma_cookie *iova_cookie;
int (*iopf_handler)(struct iopf_group *group);
#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
@@ -224,10 +234,10 @@ struct iommu_domain {
phys_addr_t msi_addr);
#endif
- union { /* Pointer usable by owner of the domain */
- struct iommufd_hw_pagetable *iommufd_hwpt; /* iommufd */
- };
- union { /* Fault handler */
+ union { /* cookie */
+ struct iommu_dma_cookie *iova_cookie;
+ struct iommu_dma_msi_cookie *msi_cookie;
+ struct iommufd_hw_pagetable *iommufd_hwpt;
struct {
iommu_fault_handler_t handler;
void *handler_token;
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index 94263ed2c564..bc9bb9cb70c8 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -42,11 +42,6 @@ struct iommu_dma_msi_page {
phys_addr_t phys;
};
-enum iommu_dma_cookie_type {
- IOMMU_DMA_IOVA_COOKIE,
- IOMMU_DMA_MSI_COOKIE,
-};
-
enum iommu_dma_queue_type {
IOMMU_DMA_OPTS_PER_CPU_QUEUE,
IOMMU_DMA_OPTS_SINGLE_QUEUE,
@@ -59,35 +54,31 @@ struct iommu_dma_options {
};
struct iommu_dma_cookie {
- enum iommu_dma_cookie_type type;
+ struct iova_domain iovad;
+ struct list_head msi_page_list;
+ /* Flush queue */
union {
- /* Full allocator for IOMMU_DMA_IOVA_COOKIE */
- struct {
- struct iova_domain iovad;
- /* Flush queue */
- union {
- struct iova_fq *single_fq;
- struct iova_fq __percpu *percpu_fq;
- };
- /* Number of TLB flushes that have been started */
- atomic64_t fq_flush_start_cnt;
- /* Number of TLB flushes that have been finished */
- atomic64_t fq_flush_finish_cnt;
- /* Timer to regularily empty the flush queues */
- struct timer_list fq_timer;
- /* 1 when timer is active, 0 when not */
- atomic_t fq_timer_on;
- };
- /* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
- dma_addr_t msi_iova;
+ struct iova_fq *single_fq;
+ struct iova_fq __percpu *percpu_fq;
};
- struct list_head msi_page_list;
-
+ /* Number of TLB flushes that have been started */
+ atomic64_t fq_flush_start_cnt;
+ /* Number of TLB flushes that have been finished */
+ atomic64_t fq_flush_finish_cnt;
+ /* Timer to regularily empty the flush queues */
+ struct timer_list fq_timer;
+ /* 1 when timer is active, 0 when not */
+ atomic_t fq_timer_on;
/* Domain for flush queue callback; NULL if flush queue not in use */
- struct iommu_domain *fq_domain;
+ struct iommu_domain *fq_domain;
/* Options for dma-iommu use */
- struct iommu_dma_options options;
- struct mutex mutex;
+ struct iommu_dma_options options;
+ struct mutex mutex;
+};
+
+struct iommu_dma_msi_cookie {
+ dma_addr_t msi_iova;
+ struct list_head msi_page_list;
};
static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled);
@@ -369,40 +360,26 @@ int iommu_dma_init_fq(struct iommu_domain *domain)
return 0;
}
-static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie)
-{
- if (cookie->type == IOMMU_DMA_IOVA_COOKIE)
- return cookie->iovad.granule;
- return PAGE_SIZE;
-}
-
-static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
-{
- struct iommu_dma_cookie *cookie;
-
- cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
- if (cookie) {
- INIT_LIST_HEAD(&cookie->msi_page_list);
- cookie->type = type;
- }
- return cookie;
-}
-
/**
* iommu_get_dma_cookie - Acquire DMA-API resources for a domain
* @domain: IOMMU domain to prepare for DMA-API usage
*/
int iommu_get_dma_cookie(struct iommu_domain *domain)
{
- if (domain->iova_cookie)
+ struct iommu_dma_cookie *cookie;
+
+ if (domain->cookie_type != IOMMU_COOKIE_NONE)
return -EEXIST;
- domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE);
- if (!domain->iova_cookie)
+ cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
+ if (!cookie)
return -ENOMEM;
- mutex_init(&domain->iova_cookie->mutex);
+ mutex_init(&cookie->mutex);
+ INIT_LIST_HEAD(&cookie->msi_page_list);
iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+ domain->cookie_type = IOMMU_COOKIE_DMA_IOVA;
+ domain->iova_cookie = cookie;
return 0;
}
@@ -420,29 +397,30 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
*/
int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
{
- struct iommu_dma_cookie *cookie;
+ struct iommu_dma_msi_cookie *cookie;
if (domain->type != IOMMU_DOMAIN_UNMANAGED)
return -EINVAL;
- if (domain->iova_cookie)
+ if (domain->cookie_type != IOMMU_COOKIE_NONE)
return -EEXIST;
- cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE);
+ cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
if (!cookie)
return -ENOMEM;
cookie->msi_iova = base;
- domain->iova_cookie = cookie;
+ INIT_LIST_HEAD(&cookie->msi_page_list);
iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
+ domain->cookie_type = IOMMU_COOKIE_DMA_MSI;
+ domain->msi_cookie = cookie;
return 0;
}
EXPORT_SYMBOL(iommu_get_msi_cookie);
/**
* iommu_put_dma_cookie - Release a domain's DMA mapping resources
- * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or
- * iommu_get_msi_cookie()
+ * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
*/
void iommu_put_dma_cookie(struct iommu_domain *domain)
{
@@ -454,20 +432,27 @@ void iommu_put_dma_cookie(struct iommu_domain *domain)
return;
#endif
- if (!cookie)
- return;
-
- if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule) {
+ if (cookie->iovad.granule) {
iommu_dma_free_fq(cookie);
put_iova_domain(&cookie->iovad);
}
+ list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list)
+ kfree(msi);
+ kfree(cookie);
+}
- list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
- list_del(&msi->list);
+/**
+ * iommu_put_msi_cookie - Release a domain's MSI mapping resources
+ * @domain: IOMMU domain previously prepared by iommu_get_msi_cookie()
+ */
+void iommu_put_msi_cookie(struct iommu_domain *domain)
+{
+ struct iommu_dma_msi_cookie *cookie = domain->msi_cookie;
+ struct iommu_dma_msi_page *msi, *tmp;
+
+ list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list)
kfree(msi);
- }
kfree(cookie);
- domain->iova_cookie = NULL;
}
/**
@@ -687,7 +672,7 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev
struct iova_domain *iovad;
int ret;
- if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE)
+ if (!cookie || domain->cookie_type != IOMMU_COOKIE_DMA_IOVA)
return -EINVAL;
iovad = &cookie->iovad;
@@ -777,9 +762,9 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
struct iova_domain *iovad = &cookie->iovad;
unsigned long shift, iova_len, iova;
- if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
- cookie->msi_iova += size;
- return cookie->msi_iova - size;
+ if (domain->cookie_type == IOMMU_COOKIE_DMA_MSI) {
+ domain->msi_cookie->msi_iova += size;
+ return domain->msi_cookie->msi_iova - size;
}
shift = iova_shift(iovad);
@@ -816,16 +801,16 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
return (dma_addr_t)iova << shift;
}
-static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
- dma_addr_t iova, size_t size, struct iommu_iotlb_gather *gather)
+static void iommu_dma_free_iova(struct iommu_domain *domain, dma_addr_t iova,
+ size_t size, struct iommu_iotlb_gather *gather)
{
- struct iova_domain *iovad = &cookie->iovad;
+ struct iova_domain *iovad = &domain->iova_cookie->iovad;
/* The MSI case is only ever cleaning up its most recent allocation */
- if (cookie->type == IOMMU_DMA_MSI_COOKIE)
- cookie->msi_iova -= size;
+ if (domain->cookie_type == IOMMU_COOKIE_DMA_MSI)
+ domain->msi_cookie->msi_iova -= size;
else if (gather && gather->queued)
- queue_iova(cookie, iova_pfn(iovad, iova),
+ queue_iova(domain->iova_cookie, iova_pfn(iovad, iova),
size >> iova_shift(iovad),
&gather->freelist);
else
@@ -853,7 +838,7 @@ static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr,
if (!iotlb_gather.queued)
iommu_iotlb_sync(domain, &iotlb_gather);
- iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather);
+ iommu_dma_free_iova(domain, dma_addr, size, &iotlb_gather);
}
static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
@@ -881,7 +866,7 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
return DMA_MAPPING_ERROR;
if (iommu_map(domain, iova, phys - iova_off, size, prot, GFP_ATOMIC)) {
- iommu_dma_free_iova(cookie, iova, size, NULL);
+ iommu_dma_free_iova(domain, iova, size, NULL);
return DMA_MAPPING_ERROR;
}
return iova + iova_off;
@@ -1018,7 +1003,7 @@ static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
out_free_sg:
sg_free_table(sgt);
out_free_iova:
- iommu_dma_free_iova(cookie, iova, size, NULL);
+ iommu_dma_free_iova(domain, iova, size, NULL);
out_free_pages:
__iommu_dma_free_pages(pages, count);
return NULL;
@@ -1495,7 +1480,7 @@ int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
return __finalise_sg(dev, sg, nents, iova);
out_free_iova:
- iommu_dma_free_iova(cookie, iova, iova_len, NULL);
+ iommu_dma_free_iova(domain, iova, iova_len, NULL);
out_restore_sg:
__invalidate_sg(sg, nents);
out:
@@ -1773,17 +1758,46 @@ void iommu_setup_dma_ops(struct device *dev)
dev->dma_iommu = false;
}
+static bool has_msi_cookie(const struct iommu_domain *domain)
+{
+ return domain && (domain->cookie_type == IOMMU_COOKIE_DMA_IOVA ||
+ domain->cookie_type == IOMMU_COOKIE_DMA_MSI);
+}
+
+static size_t cookie_msi_granule(const struct iommu_domain *domain)
+{
+ switch (domain->cookie_type) {
+ case IOMMU_COOKIE_DMA_IOVA:
+ return domain->iova_cookie->iovad.granule;
+ case IOMMU_COOKIE_DMA_MSI:
+ return PAGE_SIZE;
+ default:
+ unreachable();
+ };
+}
+
+static struct list_head *cookie_msi_pages(const struct iommu_domain *domain)
+{
+ switch (domain->cookie_type) {
+ case IOMMU_COOKIE_DMA_IOVA:
+ return &domain->iova_cookie->msi_page_list;
+ case IOMMU_COOKIE_DMA_MSI:
+ return &domain->msi_cookie->msi_page_list;
+ default:
+ unreachable();
+ };
+}
+
static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
phys_addr_t msi_addr, struct iommu_domain *domain)
{
- struct iommu_dma_cookie *cookie = domain->iova_cookie;
struct iommu_dma_msi_page *msi_page;
dma_addr_t iova;
int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
- size_t size = cookie_msi_granule(cookie);
+ size_t size = cookie_msi_granule(domain);
msi_addr &= ~(phys_addr_t)(size - 1);
- list_for_each_entry(msi_page, &cookie->msi_page_list, list)
+ list_for_each_entry(msi_page, cookie_msi_pages(domain), list)
if (msi_page->phys == msi_addr)
return msi_page;
@@ -1801,11 +1815,11 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
INIT_LIST_HEAD(&msi_page->list);
msi_page->phys = msi_addr;
msi_page->iova = iova;
- list_add(&msi_page->list, &cookie->msi_page_list);
+ list_add(&msi_page->list, cookie_msi_pages(domain));
return msi_page;
out_free_iova:
- iommu_dma_free_iova(cookie, iova, size, NULL);
+ iommu_dma_free_iova(domain, iova, size, NULL);
out_free_page:
kfree(msi_page);
return NULL;
@@ -1817,7 +1831,7 @@ static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
struct device *dev = msi_desc_to_dev(desc);
const struct iommu_dma_msi_page *msi_page;
- if (!domain->iova_cookie) {
+ if (!has_msi_cookie(domain)) {
msi_desc_set_iommu_msi_iova(desc, 0, 0);
return 0;
}
@@ -1827,9 +1841,8 @@ static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
if (!msi_page)
return -ENOMEM;
- msi_desc_set_iommu_msi_iova(
- desc, msi_page->iova,
- ilog2(cookie_msi_granule(domain->iova_cookie)));
+ msi_desc_set_iommu_msi_iova(desc, msi_page->iova,
+ ilog2(cookie_msi_granule(domain)));
return 0;
}
diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c
index 503c5d23c1ea..ab18bc494eef 100644
--- a/drivers/iommu/iommu-sva.c
+++ b/drivers/iommu/iommu-sva.c
@@ -310,6 +310,7 @@ static struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
}
domain->type = IOMMU_DOMAIN_SVA;
+ domain->cookie_type = IOMMU_COOKIE_SVA;
mmgrab(mm);
domain->mm = mm;
domain->owner = ops;
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 0ee17893810f..8b9423340221 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -1953,8 +1953,10 @@ void iommu_set_fault_handler(struct iommu_domain *domain,
iommu_fault_handler_t handler,
void *token)
{
- BUG_ON(!domain);
+ if (WARN_ON(!domain || domain->cookie_type != IOMMU_COOKIE_NONE))
+ return;
+ domain->cookie_type = IOMMU_COOKIE_FAULT_HANDLER;
domain->handler = handler;
domain->handler_token = token;
}
@@ -2024,9 +2026,18 @@ EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags);
void iommu_domain_free(struct iommu_domain *domain)
{
- if (domain->type == IOMMU_DOMAIN_SVA)
+ switch (domain->cookie_type) {
+ case IOMMU_COOKIE_DMA_IOVA:
+ iommu_put_dma_cookie(domain);
+ break;
+ case IOMMU_COOKIE_DMA_MSI:
+ iommu_put_msi_cookie(domain);
+ break;
+ case IOMMU_COOKIE_SVA:
mmdrop(domain->mm);
- iommu_put_dma_cookie(domain);
+ break;
+ default:
+ }
if (domain->ops->free)
domain->ops->free(domain);
}
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index 7de6e914232e..227514030655 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -413,6 +413,7 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
iommufd_put_object(ucmd->ictx, &fault->obj);
}
hwpt->domain->iommufd_hwpt = hwpt;
+ hwpt->domain->cookie_type = IOMMU_COOKIE_IOMMUFD;
cmd->out_hwpt_id = hwpt->obj.id;
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
--
2.43.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c
2025-03-03 20:52 [PATCH v3 0/3] iommu: Clean up cookie and sw_msi in struct iommu_domain Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
@ 2025-03-03 20:52 ` Nicolin Chen
2025-03-05 17:53 ` Jason Gunthorpe
2025-03-03 20:52 ` [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain Nicolin Chen
2 siblings, 1 reply; 16+ messages in thread
From: Nicolin Chen @ 2025-03-03 20:52 UTC (permalink / raw)
To: jgg, kevin.tian, robin.murphy, joro, will; +Cc: iommu, linux-kernel
To provide the iommufd_sw_msi() to the iommu core that is under a different
Kconfig, move it and its related functions to driver.c. Then, stub it into
the iommu-priv header. The iommufd_sw_msi_install() continues to be used by
iommufd internal, so put it in the private header.
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommu-priv.h | 13 +++
drivers/iommu/iommufd/iommufd_private.h | 7 +-
drivers/iommu/iommufd/device.c | 127 +-----------------------
drivers/iommu/iommufd/driver.c | 125 +++++++++++++++++++++++
4 files changed, 148 insertions(+), 124 deletions(-)
diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h
index b4508423e13b..c74fff25be78 100644
--- a/drivers/iommu/iommu-priv.h
+++ b/drivers/iommu/iommu-priv.h
@@ -5,6 +5,7 @@
#define __LINUX_IOMMU_PRIV_H
#include <linux/iommu.h>
+#include <linux/msi.h>
static inline const struct iommu_ops *dev_iommu_ops(struct device *dev)
{
@@ -43,4 +44,16 @@ void iommu_detach_group_handle(struct iommu_domain *domain,
int iommu_replace_group_handle(struct iommu_group *group,
struct iommu_domain *new_domain,
struct iommu_attach_handle *handle);
+
+#if IS_ENABLED(CONFIG_IOMMUFD_DRIVER_CORE) && IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
+int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ phys_addr_t msi_addr);
+#else /* !CONFIG_IOMMUFD_DRIVER_CORE || !CONFIG_IRQ_MSI_IOMMU */
+static inline int iommufd_sw_msi(struct iommu_domain *domain,
+ struct msi_desc *desc, phys_addr_t msi_addr)
+{
+ return -EOPNOTSUPP;
+}
+#endif /* CONFIG_IOMMUFD_DRIVER_CORE && CONFIG_IRQ_MSI_IOMMU */
+
#endif /* __LINUX_IOMMU_PRIV_H */
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 246297452a44..b6897a9991ee 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -32,8 +32,11 @@ struct iommufd_sw_msi_maps {
DECLARE_BITMAP(bitmap, 64);
};
-int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr);
+#ifdef CONFIG_IRQ_MSI_IOMMU
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *msi_map);
+#endif
struct iommufd_ctx {
struct file *file;
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 4e107f69f951..ca17c2b7f455 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -294,129 +294,7 @@ u32 iommufd_device_to_id(struct iommufd_device *idev)
}
EXPORT_SYMBOL_NS_GPL(iommufd_device_to_id, "IOMMUFD");
-/*
- * Get a iommufd_sw_msi_map for the msi physical address requested by the irq
- * layer. The mapping to IOVA is global to the iommufd file descriptor, every
- * domain that is attached to a device using the same MSI parameters will use
- * the same IOVA.
- */
-static __maybe_unused struct iommufd_sw_msi_map *
-iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
- phys_addr_t sw_msi_start)
-{
- struct iommufd_sw_msi_map *cur;
- unsigned int max_pgoff = 0;
-
- lockdep_assert_held(&ictx->sw_msi_lock);
-
- list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
- if (cur->sw_msi_start != sw_msi_start)
- continue;
- max_pgoff = max(max_pgoff, cur->pgoff + 1);
- if (cur->msi_addr == msi_addr)
- return cur;
- }
-
- if (ictx->sw_msi_id >=
- BITS_PER_BYTE * sizeof_field(struct iommufd_sw_msi_maps, bitmap))
- return ERR_PTR(-EOVERFLOW);
-
- cur = kzalloc(sizeof(*cur), GFP_KERNEL);
- if (!cur)
- return ERR_PTR(-ENOMEM);
-
- cur->sw_msi_start = sw_msi_start;
- cur->msi_addr = msi_addr;
- cur->pgoff = max_pgoff;
- cur->id = ictx->sw_msi_id++;
- list_add_tail(&cur->sw_msi_item, &ictx->sw_msi_list);
- return cur;
-}
-
-static int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
- struct iommufd_hwpt_paging *hwpt_paging,
- struct iommufd_sw_msi_map *msi_map)
-{
- unsigned long iova;
-
- lockdep_assert_held(&ictx->sw_msi_lock);
-
- iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
- if (!test_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap)) {
- int rc;
-
- rc = iommu_map(hwpt_paging->common.domain, iova,
- msi_map->msi_addr, PAGE_SIZE,
- IOMMU_WRITE | IOMMU_READ | IOMMU_MMIO,
- GFP_KERNEL_ACCOUNT);
- if (rc)
- return rc;
- __set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
- }
- return 0;
-}
-
-/*
- * Called by the irq code if the platform translates the MSI address through the
- * IOMMU. msi_addr is the physical address of the MSI page. iommufd will
- * allocate a fd global iova for the physical page that is the same on all
- * domains and devices.
- */
#ifdef CONFIG_IRQ_MSI_IOMMU
-int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr)
-{
- struct device *dev = msi_desc_to_dev(desc);
- struct iommufd_hwpt_paging *hwpt_paging;
- struct iommu_attach_handle *raw_handle;
- struct iommufd_attach_handle *handle;
- struct iommufd_sw_msi_map *msi_map;
- struct iommufd_ctx *ictx;
- unsigned long iova;
- int rc;
-
- /*
- * It is safe to call iommu_attach_handle_get() here because the iommu
- * core code invokes this under the group mutex which also prevents any
- * change of the attach handle for the duration of this function.
- */
- iommu_group_mutex_assert(dev);
-
- raw_handle =
- iommu_attach_handle_get(dev->iommu_group, IOMMU_NO_PASID, 0);
- if (IS_ERR(raw_handle))
- return 0;
- hwpt_paging = find_hwpt_paging(domain->iommufd_hwpt);
-
- handle = to_iommufd_handle(raw_handle);
- /* No IOMMU_RESV_SW_MSI means no change to the msi_msg */
- if (handle->idev->igroup->sw_msi_start == PHYS_ADDR_MAX)
- return 0;
-
- ictx = handle->idev->ictx;
- guard(mutex)(&ictx->sw_msi_lock);
- /*
- * The input msi_addr is the exact byte offset of the MSI doorbell, we
- * assume the caller has checked that it is contained with a MMIO region
- * that is secure to map at PAGE_SIZE.
- */
- msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
- msi_addr & PAGE_MASK,
- handle->idev->igroup->sw_msi_start);
- if (IS_ERR(msi_map))
- return PTR_ERR(msi_map);
-
- rc = iommufd_sw_msi_install(ictx, hwpt_paging, msi_map);
- if (rc)
- return rc;
- __set_bit(msi_map->id, handle->idev->igroup->required_sw_msi.bitmap);
-
- iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
- msi_desc_set_iommu_msi_iova(desc, iova, PAGE_SHIFT);
- return 0;
-}
-#endif
-
static int iommufd_group_setup_msi(struct iommufd_group *igroup,
struct iommufd_hwpt_paging *hwpt_paging)
{
@@ -443,6 +321,7 @@ static int iommufd_group_setup_msi(struct iommufd_group *igroup,
}
return 0;
}
+#endif
static int
iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
@@ -458,6 +337,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
if (rc)
return rc;
+#ifdef CONFIG_IRQ_MSI_IOMMU
if (list_empty(&idev->igroup->device_list)) {
rc = iommufd_group_setup_msi(idev->igroup, hwpt_paging);
if (rc) {
@@ -466,6 +346,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
return rc;
}
}
+#endif
return 0;
}
@@ -682,9 +563,11 @@ iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
}
}
+#ifdef CONFIG_IRQ_MSI_IOMMU
rc = iommufd_group_setup_msi(igroup, hwpt_paging);
if (rc)
goto err_unresv;
+#endif
return 0;
err_unresv:
diff --git a/drivers/iommu/iommufd/driver.c b/drivers/iommu/iommufd/driver.c
index 2d98b04ff1cb..386953c510b3 100644
--- a/drivers/iommu/iommufd/driver.c
+++ b/drivers/iommu/iommufd/driver.c
@@ -49,5 +49,130 @@ struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,
}
EXPORT_SYMBOL_NS_GPL(iommufd_viommu_find_dev, "IOMMUFD");
+#ifdef CONFIG_IRQ_MSI_IOMMU
+/*
+ * Get a iommufd_sw_msi_map for the msi physical address requested by the irq
+ * layer. The mapping to IOVA is global to the iommufd file descriptor, every
+ * domain that is attached to a device using the same MSI parameters will use
+ * the same IOVA.
+ */
+static struct iommufd_sw_msi_map *
+iommufd_sw_msi_get_map(struct iommufd_ctx *ictx, phys_addr_t msi_addr,
+ phys_addr_t sw_msi_start)
+{
+ struct iommufd_sw_msi_map *cur;
+ unsigned int max_pgoff = 0;
+
+ lockdep_assert_held(&ictx->sw_msi_lock);
+
+ list_for_each_entry(cur, &ictx->sw_msi_list, sw_msi_item) {
+ if (cur->sw_msi_start != sw_msi_start)
+ continue;
+ max_pgoff = max(max_pgoff, cur->pgoff + 1);
+ if (cur->msi_addr == msi_addr)
+ return cur;
+ }
+
+ if (ictx->sw_msi_id >=
+ BITS_PER_BYTE * sizeof_field(struct iommufd_sw_msi_maps, bitmap))
+ return ERR_PTR(-EOVERFLOW);
+
+ cur = kzalloc(sizeof(*cur), GFP_KERNEL);
+ if (!cur)
+ return ERR_PTR(-ENOMEM);
+
+ cur->sw_msi_start = sw_msi_start;
+ cur->msi_addr = msi_addr;
+ cur->pgoff = max_pgoff;
+ cur->id = ictx->sw_msi_id++;
+ list_add_tail(&cur->sw_msi_item, &ictx->sw_msi_list);
+ return cur;
+}
+
+int iommufd_sw_msi_install(struct iommufd_ctx *ictx,
+ struct iommufd_hwpt_paging *hwpt_paging,
+ struct iommufd_sw_msi_map *msi_map)
+{
+ unsigned long iova;
+
+ lockdep_assert_held(&ictx->sw_msi_lock);
+
+ iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
+ if (!test_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap)) {
+ int rc;
+
+ rc = iommu_map(hwpt_paging->common.domain, iova,
+ msi_map->msi_addr, PAGE_SIZE,
+ IOMMU_WRITE | IOMMU_READ | IOMMU_MMIO,
+ GFP_KERNEL_ACCOUNT);
+ if (rc)
+ return rc;
+ __set_bit(msi_map->id, hwpt_paging->present_sw_msi.bitmap);
+ }
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi_install, "IOMMUFD");
+
+/*
+ * Called by the irq code if the platform translates the MSI address through the
+ * IOMMU. msi_addr is the physical address of the MSI page. iommufd will
+ * allocate a fd global iova for the physical page that is the same on all
+ * domains and devices.
+ */
+int iommufd_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ phys_addr_t msi_addr)
+{
+ struct device *dev = msi_desc_to_dev(desc);
+ struct iommufd_hwpt_paging *hwpt_paging;
+ struct iommu_attach_handle *raw_handle;
+ struct iommufd_attach_handle *handle;
+ struct iommufd_sw_msi_map *msi_map;
+ struct iommufd_ctx *ictx;
+ unsigned long iova;
+ int rc;
+
+ /*
+ * It is safe to call iommu_attach_handle_get() here because the iommu
+ * core code invokes this under the group mutex which also prevents any
+ * change of the attach handle for the duration of this function.
+ */
+ iommu_group_mutex_assert(dev);
+
+ raw_handle =
+ iommu_attach_handle_get(dev->iommu_group, IOMMU_NO_PASID, 0);
+ if (IS_ERR(raw_handle))
+ return 0;
+ hwpt_paging = find_hwpt_paging(domain->iommufd_hwpt);
+
+ handle = to_iommufd_handle(raw_handle);
+ /* No IOMMU_RESV_SW_MSI means no change to the msi_msg */
+ if (handle->idev->igroup->sw_msi_start == PHYS_ADDR_MAX)
+ return 0;
+
+ ictx = handle->idev->ictx;
+ guard(mutex)(&ictx->sw_msi_lock);
+ /*
+ * The input msi_addr is the exact byte offset of the MSI doorbell, we
+ * assume the caller has checked that it is contained with a MMIO region
+ * that is secure to map at PAGE_SIZE.
+ */
+ msi_map = iommufd_sw_msi_get_map(handle->idev->ictx,
+ msi_addr & PAGE_MASK,
+ handle->idev->igroup->sw_msi_start);
+ if (IS_ERR(msi_map))
+ return PTR_ERR(msi_map);
+
+ rc = iommufd_sw_msi_install(ictx, hwpt_paging, msi_map);
+ if (rc)
+ return rc;
+ __set_bit(msi_map->id, handle->idev->igroup->required_sw_msi.bitmap);
+
+ iova = msi_map->sw_msi_start + msi_map->pgoff * PAGE_SIZE;
+ msi_desc_set_iommu_msi_iova(desc, iova, PAGE_SHIFT);
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_sw_msi, "IOMMUFD");
+#endif
+
MODULE_DESCRIPTION("iommufd code shared with builtin modules");
MODULE_LICENSE("GPL");
--
2.43.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain
2025-03-03 20:52 [PATCH v3 0/3] iommu: Clean up cookie and sw_msi in struct iommu_domain Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c Nicolin Chen
@ 2025-03-03 20:52 ` Nicolin Chen
2025-03-04 14:50 ` Robin Murphy
2025-03-06 6:01 ` Tian, Kevin
2 siblings, 2 replies; 16+ messages in thread
From: Nicolin Chen @ 2025-03-03 20:52 UTC (permalink / raw)
To: jgg, kevin.tian, robin.murphy, joro, will; +Cc: iommu, linux-kernel
There are only two sw_msi implementations in the entire system, thus it's
not very necessary to have an sw_msi pointer.
Instead, check domain->private_data_owner to call the two implementations
directly from the core code.
Suggested-by: Robin Murphy <robin.murphy@arm.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/dma-iommu.h | 9 +++++++++
include/linux/iommu.h | 15 ---------------
drivers/iommu/dma-iommu.c | 14 ++------------
drivers/iommu/iommu.c | 17 +++++++++++++++--
drivers/iommu/iommufd/hw_pagetable.c | 3 ---
5 files changed, 26 insertions(+), 32 deletions(-)
diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h
index 9cca11806e5d..eca201c1f963 100644
--- a/drivers/iommu/dma-iommu.h
+++ b/drivers/iommu/dma-iommu.h
@@ -19,6 +19,9 @@ int iommu_dma_init_fq(struct iommu_domain *domain);
void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
+int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ phys_addr_t msi_addr);
+
extern bool iommu_dma_forcedac;
#else /* CONFIG_IOMMU_DMA */
@@ -49,5 +52,11 @@ static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_he
{
}
+static inline int iommu_dma_sw_msi(struct iommu_domain *domain,
+ struct msi_desc *desc, phys_addr_t msi_addr)
+{
+ return -ENODEV;
+}
+
#endif /* CONFIG_IOMMU_DMA */
#endif /* __DMA_IOMMU_H */
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index 06cc14e9993d..e01c855ae8a7 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -229,11 +229,6 @@ struct iommu_domain {
struct iommu_domain_geometry geometry;
int (*iopf_handler)(struct iopf_group *group);
-#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
- int (*sw_msi)(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr);
-#endif
-
union { /* cookie */
struct iommu_dma_cookie *iova_cookie;
struct iommu_dma_msi_cookie *msi_cookie;
@@ -254,16 +249,6 @@ struct iommu_domain {
};
};
-static inline void iommu_domain_set_sw_msi(
- struct iommu_domain *domain,
- int (*sw_msi)(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr))
-{
-#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
- domain->sw_msi = sw_msi;
-#endif
-}
-
static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
{
return domain->type & __IOMMU_DOMAIN_DMA_API;
diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
index bc9bb9cb70c8..96e9f8d0a4f6 100644
--- a/drivers/iommu/dma-iommu.c
+++ b/drivers/iommu/dma-iommu.c
@@ -94,9 +94,6 @@ static int __init iommu_dma_forcedac_setup(char *str)
}
early_param("iommu.forcedac", iommu_dma_forcedac_setup);
-static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr);
-
/* Number of entries per flush queue */
#define IOVA_DEFAULT_FQ_SIZE 256
#define IOVA_SINGLE_FQ_SIZE 32768
@@ -377,7 +374,6 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
mutex_init(&cookie->mutex);
INIT_LIST_HEAD(&cookie->msi_page_list);
- iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
domain->cookie_type = IOMMU_COOKIE_DMA_IOVA;
domain->iova_cookie = cookie;
return 0;
@@ -411,7 +407,6 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
cookie->msi_iova = base;
INIT_LIST_HEAD(&cookie->msi_page_list);
- iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
domain->cookie_type = IOMMU_COOKIE_DMA_MSI;
domain->msi_cookie = cookie;
return 0;
@@ -427,11 +422,6 @@ void iommu_put_dma_cookie(struct iommu_domain *domain)
struct iommu_dma_cookie *cookie = domain->iova_cookie;
struct iommu_dma_msi_page *msi, *tmp;
-#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
- if (domain->sw_msi != iommu_dma_sw_msi)
- return;
-#endif
-
if (cookie->iovad.granule) {
iommu_dma_free_fq(cookie);
put_iova_domain(&cookie->iovad);
@@ -1825,8 +1815,8 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
return NULL;
}
-static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
- phys_addr_t msi_addr)
+int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
+ phys_addr_t msi_addr)
{
struct device *dev = msi_desc_to_dev(desc);
const struct iommu_dma_msi_page *msi_page;
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 8b9423340221..c1c0656f41ef 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -18,6 +18,7 @@
#include <linux/errno.h>
#include <linux/host1x_context_bus.h>
#include <linux/iommu.h>
+#include <linux/iommufd.h>
#include <linux/idr.h>
#include <linux/err.h>
#include <linux/pci.h>
@@ -3649,8 +3650,20 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
return 0;
mutex_lock(&group->mutex);
- if (group->domain && group->domain->sw_msi)
- ret = group->domain->sw_msi(group->domain, desc, msi_addr);
+ if (group->domain) {
+ switch (group->domain->cookie_type) {
+ case IOMMU_COOKIE_DMA_MSI:
+ case IOMMU_COOKIE_DMA_IOVA:
+ ret = iommu_dma_sw_msi(group->domain, desc, msi_addr);
+ break;
+ case IOMMU_COOKIE_IOMMUFD:
+ ret = iommufd_sw_msi(group->domain, desc, msi_addr);
+ break;
+ default:
+ ret = -EOPNOTSUPP;
+ break;
+ }
+ }
mutex_unlock(&group->mutex);
return ret;
}
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index 227514030655..98aecf904902 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -156,7 +156,6 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
goto out_abort;
}
}
- iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
/*
* Set the coherency mode before we do iopt_table_add_domain() as some
@@ -252,7 +251,6 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
goto out_abort;
}
hwpt->domain->owner = ops;
- iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
rc = -EINVAL;
@@ -309,7 +307,6 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
goto out_abort;
}
hwpt->domain->owner = viommu->iommu_dev->ops;
- iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
rc = -EINVAL;
--
2.43.0
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain
2025-03-03 20:52 ` [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain Nicolin Chen
@ 2025-03-04 14:50 ` Robin Murphy
2025-03-06 6:01 ` Tian, Kevin
1 sibling, 0 replies; 16+ messages in thread
From: Robin Murphy @ 2025-03-04 14:50 UTC (permalink / raw)
To: Nicolin Chen, jgg, kevin.tian, joro, will; +Cc: iommu, linux-kernel
On 03/03/2025 8:52 pm, Nicolin Chen wrote:
> There are only two sw_msi implementations in the entire system, thus it's
> not very necessary to have an sw_msi pointer.
>
> Instead, check domain->private_data_owner to call the two implementations
> directly from the core code.
FWIW I still wish this was squashed into the pending patches so we're
not adding stuff we immediately remove again, but oh well, what's done
is done.
Reviewed-by: Robin Murphy <robin.murphy@arm.com>
(assuming this isn't also queued already given that once again I've had
the audacity to not look at my inbox until after 2PM the day after it
was posted...)
> Suggested-by: Robin Murphy <robin.murphy@arm.com>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> ---
> drivers/iommu/dma-iommu.h | 9 +++++++++
> include/linux/iommu.h | 15 ---------------
> drivers/iommu/dma-iommu.c | 14 ++------------
> drivers/iommu/iommu.c | 17 +++++++++++++++--
> drivers/iommu/iommufd/hw_pagetable.c | 3 ---
> 5 files changed, 26 insertions(+), 32 deletions(-)
>
> diff --git a/drivers/iommu/dma-iommu.h b/drivers/iommu/dma-iommu.h
> index 9cca11806e5d..eca201c1f963 100644
> --- a/drivers/iommu/dma-iommu.h
> +++ b/drivers/iommu/dma-iommu.h
> @@ -19,6 +19,9 @@ int iommu_dma_init_fq(struct iommu_domain *domain);
>
> void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list);
>
> +int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
> + phys_addr_t msi_addr);
> +
> extern bool iommu_dma_forcedac;
>
> #else /* CONFIG_IOMMU_DMA */
> @@ -49,5 +52,11 @@ static inline void iommu_dma_get_resv_regions(struct device *dev, struct list_he
> {
> }
>
> +static inline int iommu_dma_sw_msi(struct iommu_domain *domain,
> + struct msi_desc *desc, phys_addr_t msi_addr)
> +{
> + return -ENODEV;
> +}
> +
> #endif /* CONFIG_IOMMU_DMA */
> #endif /* __DMA_IOMMU_H */
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index 06cc14e9993d..e01c855ae8a7 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -229,11 +229,6 @@ struct iommu_domain {
> struct iommu_domain_geometry geometry;
> int (*iopf_handler)(struct iopf_group *group);
>
> -#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
> - int (*sw_msi)(struct iommu_domain *domain, struct msi_desc *desc,
> - phys_addr_t msi_addr);
> -#endif
> -
> union { /* cookie */
> struct iommu_dma_cookie *iova_cookie;
> struct iommu_dma_msi_cookie *msi_cookie;
> @@ -254,16 +249,6 @@ struct iommu_domain {
> };
> };
>
> -static inline void iommu_domain_set_sw_msi(
> - struct iommu_domain *domain,
> - int (*sw_msi)(struct iommu_domain *domain, struct msi_desc *desc,
> - phys_addr_t msi_addr))
> -{
> -#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
> - domain->sw_msi = sw_msi;
> -#endif
> -}
> -
> static inline bool iommu_is_dma_domain(struct iommu_domain *domain)
> {
> return domain->type & __IOMMU_DOMAIN_DMA_API;
> diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c
> index bc9bb9cb70c8..96e9f8d0a4f6 100644
> --- a/drivers/iommu/dma-iommu.c
> +++ b/drivers/iommu/dma-iommu.c
> @@ -94,9 +94,6 @@ static int __init iommu_dma_forcedac_setup(char *str)
> }
> early_param("iommu.forcedac", iommu_dma_forcedac_setup);
>
> -static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
> - phys_addr_t msi_addr);
> -
> /* Number of entries per flush queue */
> #define IOVA_DEFAULT_FQ_SIZE 256
> #define IOVA_SINGLE_FQ_SIZE 32768
> @@ -377,7 +374,6 @@ int iommu_get_dma_cookie(struct iommu_domain *domain)
>
> mutex_init(&cookie->mutex);
> INIT_LIST_HEAD(&cookie->msi_page_list);
> - iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
> domain->cookie_type = IOMMU_COOKIE_DMA_IOVA;
> domain->iova_cookie = cookie;
> return 0;
> @@ -411,7 +407,6 @@ int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
>
> cookie->msi_iova = base;
> INIT_LIST_HEAD(&cookie->msi_page_list);
> - iommu_domain_set_sw_msi(domain, iommu_dma_sw_msi);
> domain->cookie_type = IOMMU_COOKIE_DMA_MSI;
> domain->msi_cookie = cookie;
> return 0;
> @@ -427,11 +422,6 @@ void iommu_put_dma_cookie(struct iommu_domain *domain)
> struct iommu_dma_cookie *cookie = domain->iova_cookie;
> struct iommu_dma_msi_page *msi, *tmp;
>
> -#if IS_ENABLED(CONFIG_IRQ_MSI_IOMMU)
> - if (domain->sw_msi != iommu_dma_sw_msi)
> - return;
> -#endif
> -
> if (cookie->iovad.granule) {
> iommu_dma_free_fq(cookie);
> put_iova_domain(&cookie->iovad);
> @@ -1825,8 +1815,8 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
> return NULL;
> }
>
> -static int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
> - phys_addr_t msi_addr)
> +int iommu_dma_sw_msi(struct iommu_domain *domain, struct msi_desc *desc,
> + phys_addr_t msi_addr)
> {
> struct device *dev = msi_desc_to_dev(desc);
> const struct iommu_dma_msi_page *msi_page;
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 8b9423340221..c1c0656f41ef 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -18,6 +18,7 @@
> #include <linux/errno.h>
> #include <linux/host1x_context_bus.h>
> #include <linux/iommu.h>
> +#include <linux/iommufd.h>
> #include <linux/idr.h>
> #include <linux/err.h>
> #include <linux/pci.h>
> @@ -3649,8 +3650,20 @@ int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
> return 0;
>
> mutex_lock(&group->mutex);
> - if (group->domain && group->domain->sw_msi)
> - ret = group->domain->sw_msi(group->domain, desc, msi_addr);
> + if (group->domain) {
> + switch (group->domain->cookie_type) {
> + case IOMMU_COOKIE_DMA_MSI:
> + case IOMMU_COOKIE_DMA_IOVA:
> + ret = iommu_dma_sw_msi(group->domain, desc, msi_addr);
> + break;
> + case IOMMU_COOKIE_IOMMUFD:
> + ret = iommufd_sw_msi(group->domain, desc, msi_addr);
> + break;
> + default:
> + ret = -EOPNOTSUPP;
> + break;
> + }
> + }
> mutex_unlock(&group->mutex);
> return ret;
> }
> diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
> index 227514030655..98aecf904902 100644
> --- a/drivers/iommu/iommufd/hw_pagetable.c
> +++ b/drivers/iommu/iommufd/hw_pagetable.c
> @@ -156,7 +156,6 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
> goto out_abort;
> }
> }
> - iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
>
> /*
> * Set the coherency mode before we do iopt_table_add_domain() as some
> @@ -252,7 +251,6 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx,
> goto out_abort;
> }
> hwpt->domain->owner = ops;
> - iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
>
> if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
> rc = -EINVAL;
> @@ -309,7 +307,6 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags,
> goto out_abort;
> }
> hwpt->domain->owner = viommu->iommu_dev->ops;
> - iommu_domain_set_sw_msi(hwpt->domain, iommufd_sw_msi);
>
> if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) {
> rc = -EINVAL;
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
@ 2025-03-05 15:08 ` kernel test robot
2025-03-05 15:18 ` kernel test robot
` (3 subsequent siblings)
4 siblings, 0 replies; 16+ messages in thread
From: kernel test robot @ 2025-03-05 15:08 UTC (permalink / raw)
To: Nicolin Chen, jgg, kevin.tian, robin.murphy, joro, will
Cc: llvm, oe-kbuild-all, iommu, linux-kernel
Hi Nicolin,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 5e9f822c9c683ae884fa5e71df41d1647b2876c6]
url: https://github.com/intel-lab-lkp/linux/commits/Nicolin-Chen/iommu-Sort-out-domain-user-data/20250304-045529
base: 5e9f822c9c683ae884fa5e71df41d1647b2876c6
patch link: https://lore.kernel.org/r/74a3bf91b52563faaf7ef7de065931e4df47e63f.1741034886.git.nicolinc%40nvidia.com
patch subject: [PATCH v3 1/3] iommu: Sort out domain user data
config: hexagon-randconfig-002-20250305 (https://download.01.org/0day-ci/archive/20250305/202503052232.H3gZRJRp-lkp@intel.com/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250305/202503052232.H3gZRJRp-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503052232.H3gZRJRp-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> drivers/iommu/iommu.c:2040:2: warning: label at end of compound statement is a C23 extension [-Wc23-extensions]
2040 | }
| ^
1 warning generated.
vim +2040 drivers/iommu/iommu.c
2026
2027 void iommu_domain_free(struct iommu_domain *domain)
2028 {
2029 switch (domain->cookie_type) {
2030 case IOMMU_COOKIE_DMA_IOVA:
2031 iommu_put_dma_cookie(domain);
2032 break;
2033 case IOMMU_COOKIE_DMA_MSI:
2034 iommu_put_msi_cookie(domain);
2035 break;
2036 case IOMMU_COOKIE_SVA:
2037 mmdrop(domain->mm);
2038 break;
2039 default:
> 2040 }
2041 if (domain->ops->free)
2042 domain->ops->free(domain);
2043 }
2044 EXPORT_SYMBOL_GPL(iommu_domain_free);
2045
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
2025-03-05 15:08 ` kernel test robot
@ 2025-03-05 15:18 ` kernel test robot
2025-03-05 15:45 ` Robin Murphy
2025-03-05 16:51 ` kernel test robot
` (2 subsequent siblings)
4 siblings, 1 reply; 16+ messages in thread
From: kernel test robot @ 2025-03-05 15:18 UTC (permalink / raw)
To: Nicolin Chen, jgg, kevin.tian, robin.murphy, joro, will
Cc: oe-kbuild-all, iommu, linux-kernel
Hi Nicolin,
kernel test robot noticed the following build errors:
[auto build test ERROR on 5e9f822c9c683ae884fa5e71df41d1647b2876c6]
url: https://github.com/intel-lab-lkp/linux/commits/Nicolin-Chen/iommu-Sort-out-domain-user-data/20250304-045529
base: 5e9f822c9c683ae884fa5e71df41d1647b2876c6
patch link: https://lore.kernel.org/r/74a3bf91b52563faaf7ef7de065931e4df47e63f.1741034886.git.nicolinc%40nvidia.com
patch subject: [PATCH v3 1/3] iommu: Sort out domain user data
config: mips-randconfig-r071-20250305 (https://download.01.org/0day-ci/archive/20250305/202503052255.t1N6KrAn-lkp@intel.com/config)
compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250305/202503052255.t1N6KrAn-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503052255.t1N6KrAn-lkp@intel.com/
All errors (new ones prefixed by >>):
>> drivers/iommu/iommu.c:2039:10: error: label at end of compound statement: expected statement
default:
^
;
1 error generated.
vim +2039 drivers/iommu/iommu.c
2026
2027 void iommu_domain_free(struct iommu_domain *domain)
2028 {
2029 switch (domain->cookie_type) {
2030 case IOMMU_COOKIE_DMA_IOVA:
2031 iommu_put_dma_cookie(domain);
2032 break;
2033 case IOMMU_COOKIE_DMA_MSI:
2034 iommu_put_msi_cookie(domain);
2035 break;
2036 case IOMMU_COOKIE_SVA:
2037 mmdrop(domain->mm);
2038 break;
> 2039 default:
2040 }
2041 if (domain->ops->free)
2042 domain->ops->free(domain);
2043 }
2044 EXPORT_SYMBOL_GPL(iommu_domain_free);
2045
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-05 15:18 ` kernel test robot
@ 2025-03-05 15:45 ` Robin Murphy
2025-03-05 17:48 ` Nicolin Chen
0 siblings, 1 reply; 16+ messages in thread
From: Robin Murphy @ 2025-03-05 15:45 UTC (permalink / raw)
To: kernel test robot, Nicolin Chen, jgg, kevin.tian, joro, will
Cc: oe-kbuild-all, iommu, linux-kernel
On 05/03/2025 3:18 pm, kernel test robot wrote:
> Hi Nicolin,
>
> kernel test robot noticed the following build errors:
>
> [auto build test ERROR on 5e9f822c9c683ae884fa5e71df41d1647b2876c6]
>
> url: https://github.com/intel-lab-lkp/linux/commits/Nicolin-Chen/iommu-Sort-out-domain-user-data/20250304-045529
> base: 5e9f822c9c683ae884fa5e71df41d1647b2876c6
> patch link: https://lore.kernel.org/r/74a3bf91b52563faaf7ef7de065931e4df47e63f.1741034886.git.nicolinc%40nvidia.com
> patch subject: [PATCH v3 1/3] iommu: Sort out domain user data
> config: mips-randconfig-r071-20250305 (https://download.01.org/0day-ci/archive/20250305/202503052255.t1N6KrAn-lkp@intel.com/config)
> compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250305/202503052255.t1N6KrAn-lkp@intel.com/reproduce)
>
> If you fix the issue in a separate patch/commit (i.e. not just a new version of
> the same patch/commit), kindly add following tags
> | Reported-by: kernel test robot <lkp@intel.com>
> | Closes: https://lore.kernel.org/oe-kbuild-all/202503052255.t1N6KrAn-lkp@intel.com/
>
> All errors (new ones prefixed by >>):
>
>>> drivers/iommu/iommu.c:2039:10: error: label at end of compound statement: expected statement
> default:
> ^
> ;
> 1 error generated.
Oops, my bad... omitting an extra break here was a semi-conscious
brainfart. Weird that my AArch64 GCC doesn't complain about this
construct though, even with "-ansi -Wall" - I have to go all the way to
-Wpedantic before it notices :/
Thanks,
Robin.
>
>
> vim +2039 drivers/iommu/iommu.c
>
> 2026
> 2027 void iommu_domain_free(struct iommu_domain *domain)
> 2028 {
> 2029 switch (domain->cookie_type) {
> 2030 case IOMMU_COOKIE_DMA_IOVA:
> 2031 iommu_put_dma_cookie(domain);
> 2032 break;
> 2033 case IOMMU_COOKIE_DMA_MSI:
> 2034 iommu_put_msi_cookie(domain);
> 2035 break;
> 2036 case IOMMU_COOKIE_SVA:
> 2037 mmdrop(domain->mm);
> 2038 break;
>> 2039 default:
> 2040 }
> 2041 if (domain->ops->free)
> 2042 domain->ops->free(domain);
> 2043 }
> 2044 EXPORT_SYMBOL_GPL(iommu_domain_free);
> 2045
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
2025-03-05 15:08 ` kernel test robot
2025-03-05 15:18 ` kernel test robot
@ 2025-03-05 16:51 ` kernel test robot
2025-03-05 17:44 ` kernel test robot
2025-03-06 5:59 ` Tian, Kevin
4 siblings, 0 replies; 16+ messages in thread
From: kernel test robot @ 2025-03-05 16:51 UTC (permalink / raw)
To: Nicolin Chen, jgg, kevin.tian, robin.murphy, joro, will
Cc: llvm, oe-kbuild-all, iommu, linux-kernel
Hi Nicolin,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 5e9f822c9c683ae884fa5e71df41d1647b2876c6]
url: https://github.com/intel-lab-lkp/linux/commits/Nicolin-Chen/iommu-Sort-out-domain-user-data/20250304-045529
base: 5e9f822c9c683ae884fa5e71df41d1647b2876c6
patch link: https://lore.kernel.org/r/74a3bf91b52563faaf7ef7de065931e4df47e63f.1741034886.git.nicolinc%40nvidia.com
patch subject: [PATCH v3 1/3] iommu: Sort out domain user data
config: powerpc-randconfig-001-20250305 (https://download.01.org/0day-ci/archive/20250306/202503060014.ddw19ThY-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250306/202503060014.ddw19ThY-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503060014.ddw19ThY-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> drivers/iommu/iommu.c:2040:2: warning: label at end of compound statement is a C2x extension [-Wc2x-extensions]
2040 | }
| ^
1 warning generated.
vim +2040 drivers/iommu/iommu.c
2026
2027 void iommu_domain_free(struct iommu_domain *domain)
2028 {
2029 switch (domain->cookie_type) {
2030 case IOMMU_COOKIE_DMA_IOVA:
2031 iommu_put_dma_cookie(domain);
2032 break;
2033 case IOMMU_COOKIE_DMA_MSI:
2034 iommu_put_msi_cookie(domain);
2035 break;
2036 case IOMMU_COOKIE_SVA:
2037 mmdrop(domain->mm);
2038 break;
2039 default:
> 2040 }
2041 if (domain->ops->free)
2042 domain->ops->free(domain);
2043 }
2044 EXPORT_SYMBOL_GPL(iommu_domain_free);
2045
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
` (2 preceding siblings ...)
2025-03-05 16:51 ` kernel test robot
@ 2025-03-05 17:44 ` kernel test robot
2025-03-06 5:59 ` Tian, Kevin
4 siblings, 0 replies; 16+ messages in thread
From: kernel test robot @ 2025-03-05 17:44 UTC (permalink / raw)
To: Nicolin Chen, jgg, kevin.tian, robin.murphy, joro, will
Cc: oe-kbuild-all, iommu, linux-kernel
Hi Nicolin,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 5e9f822c9c683ae884fa5e71df41d1647b2876c6]
url: https://github.com/intel-lab-lkp/linux/commits/Nicolin-Chen/iommu-Sort-out-domain-user-data/20250304-045529
base: 5e9f822c9c683ae884fa5e71df41d1647b2876c6
patch link: https://lore.kernel.org/r/74a3bf91b52563faaf7ef7de065931e4df47e63f.1741034886.git.nicolinc%40nvidia.com
patch subject: [PATCH v3 1/3] iommu: Sort out domain user data
config: csky-randconfig-r111-20250305 (https://download.01.org/0day-ci/archive/20250306/202503060130.fkBIRmys-lkp@intel.com/config)
compiler: csky-linux-gcc (GCC) 14.2.0
reproduce: (https://download.01.org/0day-ci/archive/20250306/202503060130.fkBIRmys-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202503060130.fkBIRmys-lkp@intel.com/
sparse warnings: (new ones prefixed by >>)
>> drivers/iommu/iommu.c:2040:9: sparse: sparse: statement expected after case label
drivers/iommu/iommu.c: note: in included file (through include/linux/mutex.h, include/linux/notifier.h, include/linux/clk.h, ...):
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
include/linux/list.h:83:21: sparse: sparse: self-comparison always evaluates to true
vim +2040 drivers/iommu/iommu.c
2026
2027 void iommu_domain_free(struct iommu_domain *domain)
2028 {
2029 switch (domain->cookie_type) {
2030 case IOMMU_COOKIE_DMA_IOVA:
2031 iommu_put_dma_cookie(domain);
2032 break;
2033 case IOMMU_COOKIE_DMA_MSI:
2034 iommu_put_msi_cookie(domain);
2035 break;
2036 case IOMMU_COOKIE_SVA:
2037 mmdrop(domain->mm);
2038 break;
2039 default:
> 2040 }
2041 if (domain->ops->free)
2042 domain->ops->free(domain);
2043 }
2044 EXPORT_SYMBOL_GPL(iommu_domain_free);
2045
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-05 15:45 ` Robin Murphy
@ 2025-03-05 17:48 ` Nicolin Chen
0 siblings, 0 replies; 16+ messages in thread
From: Nicolin Chen @ 2025-03-05 17:48 UTC (permalink / raw)
To: Robin Murphy
Cc: kernel test robot, jgg, kevin.tian, joro, will, oe-kbuild-all,
iommu, linux-kernel
On Wed, Mar 05, 2025 at 03:45:21PM +0000, Robin Murphy wrote:
> > All errors (new ones prefixed by >>):
> >
> > > > drivers/iommu/iommu.c:2039:10: error: label at end of compound statement: expected statement
> > default:
> > ^
> > ;
> > 1 error generated.
>
> Oops, my bad... omitting an extra break here was a semi-conscious brainfart.
> Weird that my AArch64 GCC doesn't complain about this construct though, even
> with "-ansi -Wall" - I have to go all the way to -Wpedantic before it
> notices :/
Mine doesn't complain about this either.
I will add a break and respin.
Thanks
Nicolin
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c
2025-03-03 20:52 ` [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c Nicolin Chen
@ 2025-03-05 17:53 ` Jason Gunthorpe
2025-03-05 18:53 ` Nicolin Chen
0 siblings, 1 reply; 16+ messages in thread
From: Jason Gunthorpe @ 2025-03-05 17:53 UTC (permalink / raw)
To: Nicolin Chen; +Cc: kevin.tian, robin.murphy, joro, will, iommu, linux-kernel
On Mon, Mar 03, 2025 at 12:52:51PM -0800, Nicolin Chen wrote:
> @@ -458,6 +337,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
> if (rc)
> return rc;
>
> +#ifdef CONFIG_IRQ_MSI_IOMMU
> if (list_empty(&idev->igroup->device_list)) {
> rc = iommufd_group_setup_msi(idev->igroup, hwpt_paging);
> if (rc) {
> @@ -466,6 +346,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
> return rc;
> }
> }
> +#endif
> return 0;
> }
>
> @@ -682,9 +563,11 @@ iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
> }
> }
>
> +#ifdef CONFIG_IRQ_MSI_IOMMU
> rc = iommufd_group_setup_msi(igroup, hwpt_paging);
> if (rc)
> goto err_unresv;
> +#endif
> return 0;
Lets try to avoid these ifdefs, maybe a empty inline, or maybe just
need to put a if (!IS_ENABLED(CONFIG_IRQ_MSI_IOMMU) in the right
spots..
I'd also include the size measurement that you did in the commit
message
Jason
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c
2025-03-05 17:53 ` Jason Gunthorpe
@ 2025-03-05 18:53 ` Nicolin Chen
0 siblings, 0 replies; 16+ messages in thread
From: Nicolin Chen @ 2025-03-05 18:53 UTC (permalink / raw)
To: Jason Gunthorpe; +Cc: kevin.tian, robin.murphy, joro, will, iommu, linux-kernel
On Wed, Mar 05, 2025 at 01:53:15PM -0400, Jason Gunthorpe wrote:
> On Mon, Mar 03, 2025 at 12:52:51PM -0800, Nicolin Chen wrote:
> > @@ -458,6 +337,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
> > if (rc)
> > return rc;
> >
> > +#ifdef CONFIG_IRQ_MSI_IOMMU
> > if (list_empty(&idev->igroup->device_list)) {
> > rc = iommufd_group_setup_msi(idev->igroup, hwpt_paging);
> > if (rc) {
> > @@ -466,6 +346,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
> > return rc;
> > }
> > }
> > +#endif
> > return 0;
> > }
> >
> > @@ -682,9 +563,11 @@ iommufd_group_do_replace_reserved_iova(struct iommufd_group *igroup,
> > }
> > }
> >
> > +#ifdef CONFIG_IRQ_MSI_IOMMU
> > rc = iommufd_group_setup_msi(igroup, hwpt_paging);
> > if (rc)
> > goto err_unresv;
> > +#endif
> > return 0;
>
> Lets try to avoid these ifdefs, maybe a empty inline, or maybe just
> need to put a if (!IS_ENABLED(CONFIG_IRQ_MSI_IOMMU) in the right
> spots..
>
> I'd also include the size measurement that you did in the commit
> message
Ack. Will rework.
Thanks
Nicolin
^ permalink raw reply [flat|nested] 16+ messages in thread
* RE: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
` (3 preceding siblings ...)
2025-03-05 17:44 ` kernel test robot
@ 2025-03-06 5:59 ` Tian, Kevin
2025-03-06 20:10 ` Nicolin Chen
4 siblings, 1 reply; 16+ messages in thread
From: Tian, Kevin @ 2025-03-06 5:59 UTC (permalink / raw)
To: Nicolin Chen, jgg@nvidia.com, robin.murphy@arm.com,
joro@8bytes.org, will@kernel.org
Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org
> From: Nicolin Chen <nicolinc@nvidia.com>
> Sent: Tuesday, March 4, 2025 4:53 AM
>
> From: Robin Murphy <robin.murphy@arm.com>
>
> When DMA/MSI cookies were made first-class citizens back in commit
> 46983fcd67ac ("iommu: Pull IOVA cookie management into the core"), there
> was no real need to further expose the two different cookie types.
> However, now that IOMMUFD wants to add a third type of MSI-mapping
> cookie, we do have a nicely compelling reason to properly dismabiguate
> things at the domain level beyond just vaguely guessing from the domain
> type.
>
> Meanwhile, we also effectively have another "cookie" in the form of the
> anonymous union for other user data, which isn't much better in terms of
> being vague and unenforced. The fact is that all these cookie types are
> mutually exclusive, in the sense that combining them makes zero sense
> and/or would be catastrophic (iommu_set_fault_handler() on an SVA
> domain, anyone?) - the only combination which *might* be reasonable is
> perhaps a fault handler and an MSI cookie, but nobody's doing that at
> the moment, so let's rule it out as well for the sake of being clear and
> robust. To that end, we pull DMA and MSI cookies apart a little more,
> mostly to clear up the ambiguity at domain teardown, then for clarity
> (and to save a little space), move them into the union, whose ownership
> we can then properly describe and enforce entirely unambiguously.
>
> Signed-off-by: Robin Murphy <robin.murphy@arm.com>
> [nicolinc: rebase on latest tree; use prefix IOMMU_COOKIE_; merge unions
> in iommu_domain; add IOMMU_COOKIE_IOMMUFD for
> iommufd_hwpt]
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
with a nit:
> msi_addr &= ~(phys_addr_t)(size - 1);
> - list_for_each_entry(msi_page, &cookie->msi_page_list, list)
> + list_for_each_entry(msi_page, cookie_msi_pages(domain), list)
> if (msi_page->phys == msi_addr)
> return msi_page;
>
Above checks cookie type in every iteration. Better save the list
pointer to a local variable.
^ permalink raw reply [flat|nested] 16+ messages in thread
* RE: [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain
2025-03-03 20:52 ` [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain Nicolin Chen
2025-03-04 14:50 ` Robin Murphy
@ 2025-03-06 6:01 ` Tian, Kevin
1 sibling, 0 replies; 16+ messages in thread
From: Tian, Kevin @ 2025-03-06 6:01 UTC (permalink / raw)
To: Nicolin Chen, jgg@nvidia.com, robin.murphy@arm.com,
joro@8bytes.org, will@kernel.org
Cc: iommu@lists.linux.dev, linux-kernel@vger.kernel.org
> From: Nicolin Chen <nicolinc@nvidia.com>
> Sent: Tuesday, March 4, 2025 4:53 AM
>
> There are only two sw_msi implementations in the entire system, thus it's
> not very necessary to have an sw_msi pointer.
>
> Instead, check domain->private_data_owner to call the two implementations
> directly from the core code.
s/private_data_owner/cookie_type/
>
> Suggested-by: Robin Murphy <robin.murphy@arm.com>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v3 1/3] iommu: Sort out domain user data
2025-03-06 5:59 ` Tian, Kevin
@ 2025-03-06 20:10 ` Nicolin Chen
0 siblings, 0 replies; 16+ messages in thread
From: Nicolin Chen @ 2025-03-06 20:10 UTC (permalink / raw)
To: Tian, Kevin
Cc: jgg@nvidia.com, robin.murphy@arm.com, joro@8bytes.org,
will@kernel.org, iommu@lists.linux.dev,
linux-kernel@vger.kernel.org
On Thu, Mar 06, 2025 at 05:59:03AM +0000, Tian, Kevin wrote:
> > From: Nicolin Chen <nicolinc@nvidia.com>
> > Sent: Tuesday, March 4, 2025 4:53 AM
> >
> > From: Robin Murphy <robin.murphy@arm.com>
> >
> > When DMA/MSI cookies were made first-class citizens back in commit
> > 46983fcd67ac ("iommu: Pull IOVA cookie management into the core"), there
> > was no real need to further expose the two different cookie types.
> > However, now that IOMMUFD wants to add a third type of MSI-mapping
> > cookie, we do have a nicely compelling reason to properly dismabiguate
> > things at the domain level beyond just vaguely guessing from the domain
> > type.
> >
> > Meanwhile, we also effectively have another "cookie" in the form of the
> > anonymous union for other user data, which isn't much better in terms of
> > being vague and unenforced. The fact is that all these cookie types are
> > mutually exclusive, in the sense that combining them makes zero sense
> > and/or would be catastrophic (iommu_set_fault_handler() on an SVA
> > domain, anyone?) - the only combination which *might* be reasonable is
> > perhaps a fault handler and an MSI cookie, but nobody's doing that at
> > the moment, so let's rule it out as well for the sake of being clear and
> > robust. To that end, we pull DMA and MSI cookies apart a little more,
> > mostly to clear up the ambiguity at domain teardown, then for clarity
> > (and to save a little space), move them into the union, whose ownership
> > we can then properly describe and enforce entirely unambiguously.
> >
> > Signed-off-by: Robin Murphy <robin.murphy@arm.com>
> > [nicolinc: rebase on latest tree; use prefix IOMMU_COOKIE_; merge unions
> > in iommu_domain; add IOMMU_COOKIE_IOMMUFD for
> > iommufd_hwpt]
> > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
>
> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
>
> with a nit:
>
> > msi_addr &= ~(phys_addr_t)(size - 1);
> > - list_for_each_entry(msi_page, &cookie->msi_page_list, list)
> > + list_for_each_entry(msi_page, cookie_msi_pages(domain), list)
> > if (msi_page->phys == msi_addr)
> > return msi_page;
> >
>
> Above checks cookie type in every iteration. Better save the list
> pointer to a local variable.
Ack.
--------------------------------------------------------------
@@ -1793,2 +1793,3 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
{
+ struct list_head *msi_page_list = cookie_msi_pages(domain);
struct iommu_dma_msi_page *msi_page;
@@ -1799,3 +1800,3 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
msi_addr &= ~(phys_addr_t)(size - 1);
- list_for_each_entry(msi_page, cookie_msi_pages(domain), list)
+ list_for_each_entry(msi_page, msi_page_list, list)
if (msi_page->phys == msi_addr)
@@ -1817,3 +1818,3 @@ static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
msi_page->iova = iova;
- list_add(&msi_page->list, cookie_msi_pages(domain));
+ list_add(&msi_page->list, msi_page_list);
return msi_page;
--------------------------------------------------------------
Thanks
Nicolin
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2025-03-06 20:10 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-03 20:52 [PATCH v3 0/3] iommu: Clean up cookie and sw_msi in struct iommu_domain Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 1/3] iommu: Sort out domain user data Nicolin Chen
2025-03-05 15:08 ` kernel test robot
2025-03-05 15:18 ` kernel test robot
2025-03-05 15:45 ` Robin Murphy
2025-03-05 17:48 ` Nicolin Chen
2025-03-05 16:51 ` kernel test robot
2025-03-05 17:44 ` kernel test robot
2025-03-06 5:59 ` Tian, Kevin
2025-03-06 20:10 ` Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 2/3] iommufd: Move iommufd_sw_msi and related functions to driver.c Nicolin Chen
2025-03-05 17:53 ` Jason Gunthorpe
2025-03-05 18:53 ` Nicolin Chen
2025-03-03 20:52 ` [PATCH v3 3/3] iommu: Drop sw_msi from iommu_domain Nicolin Chen
2025-03-04 14:50 ` Robin Murphy
2025-03-06 6:01 ` Tian, Kevin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox