* [PATCH 01/10] iommu: Remove useless group refcounting
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-20 6:11 ` Baolu Lu
2023-07-21 7:10 ` Tian, Kevin
2023-07-18 19:05 ` [PATCH 02/10] iommu: Add a lockdep assertion for remaining dev->iommu_group reads Jason Gunthorpe
` (8 subsequent siblings)
9 siblings, 2 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Several functions obtain the group reference and then release it before
returning. This gives the impression that the refcount is protecting
something for the duration of the function.
In truth all of these functions are called in places that know a device
driver is probed to the device and our locking rules already require
that dev->iommu_group cannot change while a driver is attached to the
struct device.
If this was not the case then this code is already at risk of triggering
UAF as it is racy if the dev->iommu_group is concurrently going to
NULL/free. refcount debugging will throw a WARN if kobject_get() is
called on a 0 refcount object to highlight the bug.
Remove the confusing refcounting and leave behind a comment about the
restriction.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommu.c | 57 ++++++++++++++++---------------------------
1 file changed, 21 insertions(+), 36 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 4352a149a935e8..2f6eb781dfc317 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2014,10 +2014,10 @@ static int __iommu_attach_device(struct iommu_domain *domain,
*/
int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
{
- struct iommu_group *group;
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
int ret;
- group = iommu_group_get(dev);
if (!group)
return -ENODEV;
@@ -2034,8 +2034,6 @@ int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
out_unlock:
mutex_unlock(&group->mutex);
- iommu_group_put(group);
-
return ret;
}
EXPORT_SYMBOL_GPL(iommu_attach_device);
@@ -2050,9 +2048,9 @@ int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
{
- struct iommu_group *group;
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
- group = iommu_group_get(dev);
if (!group)
return;
@@ -2064,24 +2062,18 @@ void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
out_unlock:
mutex_unlock(&group->mutex);
- iommu_group_put(group);
}
EXPORT_SYMBOL_GPL(iommu_detach_device);
struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
{
- struct iommu_domain *domain;
- struct iommu_group *group;
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
- group = iommu_group_get(dev);
if (!group)
return NULL;
- domain = group->domain;
-
- iommu_group_put(group);
-
- return domain;
+ return group->domain;
}
EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
@@ -3044,7 +3036,8 @@ static bool iommu_is_default_domain(struct iommu_group *group)
*/
int iommu_device_use_default_domain(struct device *dev)
{
- struct iommu_group *group = iommu_group_get(dev);
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
int ret = 0;
if (!group)
@@ -3063,8 +3056,6 @@ int iommu_device_use_default_domain(struct device *dev)
unlock_out:
mutex_unlock(&group->mutex);
- iommu_group_put(group);
-
return ret;
}
@@ -3078,7 +3069,8 @@ int iommu_device_use_default_domain(struct device *dev)
*/
void iommu_device_unuse_default_domain(struct device *dev)
{
- struct iommu_group *group = iommu_group_get(dev);
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
if (!group)
return;
@@ -3088,7 +3080,6 @@ void iommu_device_unuse_default_domain(struct device *dev)
group->owner_cnt--;
mutex_unlock(&group->mutex);
- iommu_group_put(group);
}
static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
@@ -3175,13 +3166,13 @@ EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
*/
int iommu_device_claim_dma_owner(struct device *dev, void *owner)
{
- struct iommu_group *group;
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
int ret = 0;
if (WARN_ON(!owner))
return -EINVAL;
- group = iommu_group_get(dev);
if (!group)
return -ENODEV;
@@ -3198,8 +3189,6 @@ int iommu_device_claim_dma_owner(struct device *dev, void *owner)
ret = __iommu_take_dma_ownership(group, owner);
unlock_out:
mutex_unlock(&group->mutex);
- iommu_group_put(group);
-
return ret;
}
EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
@@ -3237,7 +3226,8 @@ EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
*/
void iommu_device_release_dma_owner(struct device *dev)
{
- struct iommu_group *group = iommu_group_get(dev);
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
mutex_lock(&group->mutex);
if (group->owner_cnt > 1)
@@ -3245,7 +3235,6 @@ void iommu_device_release_dma_owner(struct device *dev)
else
__iommu_release_dma_ownership(group);
mutex_unlock(&group->mutex);
- iommu_group_put(group);
}
EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
@@ -3306,14 +3295,14 @@ static void __iommu_remove_group_pasid(struct iommu_group *group,
int iommu_attach_device_pasid(struct iommu_domain *domain,
struct device *dev, ioasid_t pasid)
{
- struct iommu_group *group;
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
void *curr;
int ret;
if (!domain->ops->set_dev_pasid)
return -EOPNOTSUPP;
- group = iommu_group_get(dev);
if (!group)
return -ENODEV;
@@ -3331,8 +3320,6 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
}
out_unlock:
mutex_unlock(&group->mutex);
- iommu_group_put(group);
-
return ret;
}
EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
@@ -3349,14 +3336,13 @@ EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
ioasid_t pasid)
{
- struct iommu_group *group = iommu_group_get(dev);
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
mutex_lock(&group->mutex);
__iommu_remove_group_pasid(group, pasid);
WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
mutex_unlock(&group->mutex);
-
- iommu_group_put(group);
}
EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
@@ -3378,10 +3364,10 @@ struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
ioasid_t pasid,
unsigned int type)
{
+ /* Caller must be a probed driver on dev */
+ struct iommu_group *group = dev->iommu_group;
struct iommu_domain *domain;
- struct iommu_group *group;
- group = iommu_group_get(dev);
if (!group)
return NULL;
@@ -3390,7 +3376,6 @@ struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
if (type && domain && domain->type != type)
domain = ERR_PTR(-EBUSY);
xa_unlock(&group->pasid_array);
- iommu_group_put(group);
return domain;
}
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 01/10] iommu: Remove useless group refcounting
2023-07-18 19:05 ` [PATCH 01/10] iommu: Remove useless group refcounting Jason Gunthorpe
@ 2023-07-20 6:11 ` Baolu Lu
2023-07-21 7:10 ` Tian, Kevin
1 sibling, 0 replies; 26+ messages in thread
From: Baolu Lu @ 2023-07-20 6:11 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, David Woodhouse, Heiko Stuebner,
iommu, Jernej Skrabec, Joerg Roedel, linux-arm-kernel,
linux-rockchip, linux-sunxi, Orson Zhai, Robin Murphy,
Samuel Holland, Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: baolu.lu, Alex Williamson
On 2023/7/19 3:05, Jason Gunthorpe wrote:
> Several functions obtain the group reference and then release it before
> returning. This gives the impression that the refcount is protecting
> something for the duration of the function.
>
> In truth all of these functions are called in places that know a device
> driver is probed to the device and our locking rules already require
> that dev->iommu_group cannot change while a driver is attached to the
> struct device.
>
> If this was not the case then this code is already at risk of triggering
> UAF as it is racy if the dev->iommu_group is concurrently going to
> NULL/free. refcount debugging will throw a WARN if kobject_get() is
> called on a 0 refcount object to highlight the bug.
>
> Remove the confusing refcounting and leave behind a comment about the
> restriction.
>
> Signed-off-by: Jason Gunthorpe<jgg@nvidia.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* RE: [PATCH 01/10] iommu: Remove useless group refcounting
2023-07-18 19:05 ` [PATCH 01/10] iommu: Remove useless group refcounting Jason Gunthorpe
2023-07-20 6:11 ` Baolu Lu
@ 2023-07-21 7:10 ` Tian, Kevin
2023-07-21 12:01 ` Jason Gunthorpe
1 sibling, 1 reply; 26+ messages in thread
From: Tian, Kevin @ 2023-07-21 7:10 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, Lu Baolu, David Woodhouse,
Heiko Stuebner, iommu@lists.linux.dev, Jernej Skrabec,
Joerg Roedel, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev,
Orson Zhai, Robin Murphy, Samuel Holland, Chen-Yu Tsai,
Will Deacon, Chunyan Zhang
Cc: Alex Williamson
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, July 19, 2023 3:06 AM
>
> int iommu_device_use_default_domain(struct device *dev)
> {
> - struct iommu_group *group = iommu_group_get(dev);
> + /* Caller must be a probed driver on dev */
> + struct iommu_group *group = dev->iommu_group;
> int ret = 0;
this is called in the probing path by .dma_configure().
the driver hasn't been probed yet hence the rationale to not
refcount is due to device_lock() instead?
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [PATCH 01/10] iommu: Remove useless group refcounting
2023-07-21 7:10 ` Tian, Kevin
@ 2023-07-21 12:01 ` Jason Gunthorpe
0 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-21 12:01 UTC (permalink / raw)
To: Tian, Kevin
Cc: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner,
iommu@lists.linux.dev, Jernej Skrabec, Joerg Roedel,
linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev,
Orson Zhai, Robin Murphy, Samuel Holland, Chen-Yu Tsai,
Will Deacon, Chunyan Zhang, Alex Williamson
On Fri, Jul 21, 2023 at 07:10:57AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Wednesday, July 19, 2023 3:06 AM
> >
> > int iommu_device_use_default_domain(struct device *dev)
> > {
> > - struct iommu_group *group = iommu_group_get(dev);
> > + /* Caller must be a probed driver on dev */
> > + struct iommu_group *group = dev->iommu_group;
> > int ret = 0;
>
> this is called in the probing path by .dma_configure().
>
> the driver hasn't been probed yet hence the rationale to not
> refcount is due to device_lock() instead?
At this point the driver core has partially attached/detached a driver
so I'm considering it part of the probed driver explanation.
Really during anything protected by the driver core probe path is
assured that the group cannot change.
Jason
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 02/10] iommu: Add a lockdep assertion for remaining dev->iommu_group reads
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 01/10] iommu: Remove useless group refcounting Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-20 6:33 ` Baolu Lu
2023-07-18 19:05 ` [PATCH 03/10] iommu: Add generic_single_device_group() Jason Gunthorpe
` (7 subsequent siblings)
9 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
The remaining reads are all in functions called under ops->device_group.
Broadly these functions are walking around the device tree (eg going up
the PCI bus tree) and are trying to de-duplicate group allocations
according to their logic.
Since these functions don't hold any particular per-device locks their
reads to dev->iommu_group are being locked by the caller's
iommu_probe_device_lock, and this explains why iommu_probe_device_lock
needs to be a global lock.
Rename iommu_probe_device_lock to dev_iommu_group_lock, make it local to
the module and annotate all the device_group helpers with
iommu_group_get_locked() that includes a lockdep to indicate that they are
special.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommu.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 2f6eb781dfc317..9e41ad4e3219b6 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -44,6 +44,8 @@ static unsigned int iommu_def_domain_type __read_mostly;
static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
static u32 iommu_cmd_line __read_mostly;
+static DEFINE_MUTEX(dev_iommu_group_lock);
+
struct iommu_group {
struct kobject kobj;
struct kobject *devices_kobj;
@@ -438,7 +440,6 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
{
const struct iommu_ops *ops = dev->bus->iommu_ops;
struct iommu_group *group;
- static DEFINE_MUTEX(iommu_probe_device_lock);
struct group_device *gdev;
int ret;
@@ -451,7 +452,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
* probably be able to use device_lock() here to minimise the scope,
* but for now enforcing a simple global ordering is fine.
*/
- mutex_lock(&iommu_probe_device_lock);
+ mutex_lock(&dev_iommu_group_lock);
/* Device is probed already if in a group */
if (dev->iommu_group) {
@@ -497,7 +498,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
list_add_tail(&group->entry, group_list);
}
mutex_unlock(&group->mutex);
- mutex_unlock(&iommu_probe_device_lock);
+ mutex_unlock(&dev_iommu_group_lock);
if (dev_is_pci(dev))
iommu_dma_set_pci_32bit_workaround(dev);
@@ -512,7 +513,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
mutex_unlock(&group->mutex);
iommu_group_put(group);
out_unlock:
- mutex_unlock(&iommu_probe_device_lock);
+ mutex_unlock(&dev_iommu_group_lock);
return ret;
}
@@ -1219,6 +1220,12 @@ struct iommu_group *iommu_group_get(struct device *dev)
}
EXPORT_SYMBOL_GPL(iommu_group_get);
+static struct iommu_group *iommu_group_get_locked(struct device *dev)
+{
+ lockdep_assert_held(&dev_iommu_group_lock);
+ return iommu_group_get(dev);
+}
+
/**
* iommu_group_ref_get - Increment reference on a group
* @group: the group to use, must not be NULL
@@ -1532,7 +1539,7 @@ static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
if (test_and_set_bit(pdev->devfn & 0xff, devfns))
return NULL;
- group = iommu_group_get(&pdev->dev);
+ group = iommu_group_get_locked(&pdev->dev);
if (group)
return group;
@@ -1573,7 +1580,7 @@ static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
struct group_for_pci_data *data = opaque;
data->pdev = pdev;
- data->group = iommu_group_get(&pdev->dev);
+ data->group = iommu_group_get_locked(&pdev->dev);
return data->group != NULL;
}
@@ -1629,7 +1636,7 @@ struct iommu_group *pci_device_group(struct device *dev)
pdev = bus->self;
- group = iommu_group_get(&pdev->dev);
+ group = iommu_group_get_locked(&pdev->dev);
if (group)
return group;
}
@@ -1662,7 +1669,7 @@ struct iommu_group *fsl_mc_device_group(struct device *dev)
struct device *cont_dev = fsl_mc_cont_dev(dev);
struct iommu_group *group;
- group = iommu_group_get(cont_dev);
+ group = iommu_group_get_locked(cont_dev);
if (!group)
group = iommu_group_alloc();
return group;
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 02/10] iommu: Add a lockdep assertion for remaining dev->iommu_group reads
2023-07-18 19:05 ` [PATCH 02/10] iommu: Add a lockdep assertion for remaining dev->iommu_group reads Jason Gunthorpe
@ 2023-07-20 6:33 ` Baolu Lu
0 siblings, 0 replies; 26+ messages in thread
From: Baolu Lu @ 2023-07-20 6:33 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, David Woodhouse, Heiko Stuebner,
iommu, Jernej Skrabec, Joerg Roedel, linux-arm-kernel,
linux-rockchip, linux-sunxi, Orson Zhai, Robin Murphy,
Samuel Holland, Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: baolu.lu, Alex Williamson
On 2023/7/19 3:05, Jason Gunthorpe wrote:
> The remaining reads are all in functions called under ops->device_group.
>
> Broadly these functions are walking around the device tree (eg going up
> the PCI bus tree) and are trying to de-duplicate group allocations
> according to their logic.
>
> Since these functions don't hold any particular per-device locks their
> reads to dev->iommu_group are being locked by the caller's
> iommu_probe_device_lock, and this explains why iommu_probe_device_lock
> needs to be a global lock.
>
> Rename iommu_probe_device_lock to dev_iommu_group_lock, make it local to
> the module and annotate all the device_group helpers with
> iommu_group_get_locked() that includes a lockdep to indicate that they are
> special.
>
> Signed-off-by: Jason Gunthorpe<jgg@nvidia.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 01/10] iommu: Remove useless group refcounting Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 02/10] iommu: Add a lockdep assertion for remaining dev->iommu_group reads Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-20 7:39 ` Baolu Lu
` (2 more replies)
2023-07-18 19:05 ` [PATCH 04/10] iommu/sun50i: Convert to generic_single_device_group() Jason Gunthorpe
` (6 subsequent siblings)
9 siblings, 3 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
This implements the common pattern seen in drivers of a single
iommu_group for the entire iommu driver. Implement this in core code
so the drivers that want this can select it from their ops.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommu.c | 25 +++++++++++++++++++++++++
include/linux/iommu.h | 3 +++
2 files changed, 28 insertions(+)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 9e41ad4e3219b6..1e0c5d9a0370fb 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu)
spin_lock(&iommu_device_lock);
list_del(&iommu->list);
spin_unlock(&iommu_device_lock);
+
+ /* Pairs with the alloc in generic_single_device_group() */
+ iommu_group_put(iommu->singleton_group);
}
EXPORT_SYMBOL_GPL(iommu_device_unregister);
@@ -1595,6 +1598,28 @@ struct iommu_group *generic_device_group(struct device *dev)
}
EXPORT_SYMBOL_GPL(generic_device_group);
+/*
+ * Generic device_group call-back function. It just allocates one
+ * iommu-group per iommu driver.
+ */
+struct iommu_group *generic_single_device_group(struct device *dev)
+{
+ struct iommu_device *iommu = dev->iommu->iommu_dev;
+
+ lockdep_assert_held(&dev_iommu_group_lock);
+
+ if (!iommu->singleton_group) {
+ struct iommu_group *group;
+
+ group = iommu_group_alloc();
+ if (IS_ERR(group))
+ return group;
+ iommu->singleton_group = group;
+ }
+ return iommu_group_ref_get(iommu->singleton_group);
+}
+EXPORT_SYMBOL_GPL(generic_single_device_group);
+
/*
* Use standard PCI bus topology, isolation features, and DMA alias quirks
* to find or create an IOMMU group for a device.
diff --git a/include/linux/iommu.h b/include/linux/iommu.h
index b1dcb1b9b17040..f1e18e81fca78b 100644
--- a/include/linux/iommu.h
+++ b/include/linux/iommu.h
@@ -361,6 +361,7 @@ struct iommu_domain_ops {
* @list: Used by the iommu-core to keep a list of registered iommus
* @ops: iommu-ops for talking to this iommu
* @dev: struct device for sysfs handling
+ * @singleton_group: Used internally for drivers that have only one group
* @max_pasids: number of supported PASIDs
*/
struct iommu_device {
@@ -368,6 +369,7 @@ struct iommu_device {
const struct iommu_ops *ops;
struct fwnode_handle *fwnode;
struct device *dev;
+ struct iommu_group *singleton_group;
u32 max_pasids;
};
@@ -640,6 +642,7 @@ extern struct iommu_group *pci_device_group(struct device *dev);
extern struct iommu_group *generic_device_group(struct device *dev);
/* FSL-MC device grouping function */
struct iommu_group *fsl_mc_device_group(struct device *dev);
+extern struct iommu_group *generic_single_device_group(struct device *dev);
/**
* struct iommu_fwspec - per-device IOMMU instance data
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-18 19:05 ` [PATCH 03/10] iommu: Add generic_single_device_group() Jason Gunthorpe
@ 2023-07-20 7:39 ` Baolu Lu
2023-07-20 12:04 ` Jason Gunthorpe
2023-07-21 7:17 ` Tian, Kevin
2023-07-22 14:02 ` Baolu Lu
2 siblings, 1 reply; 26+ messages in thread
From: Baolu Lu @ 2023-07-20 7:39 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, David Woodhouse, Heiko Stuebner,
iommu, Jernej Skrabec, Joerg Roedel, linux-arm-kernel,
linux-rockchip, linux-sunxi, Orson Zhai, Robin Murphy,
Samuel Holland, Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: baolu.lu, Alex Williamson
On 2023/7/19 3:05, Jason Gunthorpe wrote:
> This implements the common pattern seen in drivers of a single
> iommu_group for the entire iommu driver. Implement this in core code
> so the drivers that want this can select it from their ops.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
> drivers/iommu/iommu.c | 25 +++++++++++++++++++++++++
> include/linux/iommu.h | 3 +++
> 2 files changed, 28 insertions(+)
>
> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> index 9e41ad4e3219b6..1e0c5d9a0370fb 100644
> --- a/drivers/iommu/iommu.c
> +++ b/drivers/iommu/iommu.c
> @@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu)
> spin_lock(&iommu_device_lock);
> list_del(&iommu->list);
> spin_unlock(&iommu_device_lock);
> +
> + /* Pairs with the alloc in generic_single_device_group() */
> + iommu_group_put(iommu->singleton_group);
> }
> EXPORT_SYMBOL_GPL(iommu_device_unregister);
>
> @@ -1595,6 +1598,28 @@ struct iommu_group *generic_device_group(struct device *dev)
> }
> EXPORT_SYMBOL_GPL(generic_device_group);
>
> +/*
> + * Generic device_group call-back function. It just allocates one
> + * iommu-group per iommu driver.
> + */
> +struct iommu_group *generic_single_device_group(struct device *dev)
> +{
> + struct iommu_device *iommu = dev->iommu->iommu_dev;
> +
> + lockdep_assert_held(&dev_iommu_group_lock);
> +
> + if (!iommu->singleton_group) {
> + struct iommu_group *group;
> +
> + group = iommu_group_alloc();
> + if (IS_ERR(group))
> + return group;
> + iommu->singleton_group = group;
> + }
> + return iommu_group_ref_get(iommu->singleton_group);
> +}
> +EXPORT_SYMBOL_GPL(generic_single_device_group);
When allocating the singleton group for the first time, the group's
refcount is taken twice. This can cause memory leaks even after calling
iommu_device_unregister(). Perhaps it can be adjusted as follows?
struct iommu_group *generic_single_device_group(struct device *dev)
{
struct iommu_device *iommu = dev->iommu->iommu_dev;
struct iommu_group *group;
lockdep_assert_held(&dev_iommu_group_lock);
if (iommu->singleton_group)
return iommu_group_ref_get(iommu->singleton_group);
group = iommu_group_alloc();
if (!IS_ERR(group))
iommu->singleton_group = group;
return group;
}
> +
> /*
> * Use standard PCI bus topology, isolation features, and DMA alias quirks
> * to find or create an IOMMU group for a device.
> diff --git a/include/linux/iommu.h b/include/linux/iommu.h
> index b1dcb1b9b17040..f1e18e81fca78b 100644
> --- a/include/linux/iommu.h
> +++ b/include/linux/iommu.h
> @@ -361,6 +361,7 @@ struct iommu_domain_ops {
> * @list: Used by the iommu-core to keep a list of registered iommus
> * @ops: iommu-ops for talking to this iommu
> * @dev: struct device for sysfs handling
> + * @singleton_group: Used internally for drivers that have only one group
> * @max_pasids: number of supported PASIDs
> */
> struct iommu_device {
> @@ -368,6 +369,7 @@ struct iommu_device {
> const struct iommu_ops *ops;
> struct fwnode_handle *fwnode;
> struct device *dev;
> + struct iommu_group *singleton_group;
> u32 max_pasids;
> };
>
> @@ -640,6 +642,7 @@ extern struct iommu_group *pci_device_group(struct device *dev);
> extern struct iommu_group *generic_device_group(struct device *dev);
> /* FSL-MC device grouping function */
> struct iommu_group *fsl_mc_device_group(struct device *dev);
> +extern struct iommu_group *generic_single_device_group(struct device *dev);
"extern" is not necessary.
>
> /**
> * struct iommu_fwspec - per-device IOMMU instance data
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-20 7:39 ` Baolu Lu
@ 2023-07-20 12:04 ` Jason Gunthorpe
2023-07-20 14:01 ` Baolu Lu
0 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-20 12:04 UTC (permalink / raw)
To: Baolu Lu
Cc: Baolin Wang, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang, Alex Williamson
On Thu, Jul 20, 2023 at 03:39:27PM +0800, Baolu Lu wrote:
> On 2023/7/19 3:05, Jason Gunthorpe wrote:
> > This implements the common pattern seen in drivers of a single
> > iommu_group for the entire iommu driver. Implement this in core code
> > so the drivers that want this can select it from their ops.
> >
> > Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> > ---
> > drivers/iommu/iommu.c | 25 +++++++++++++++++++++++++
> > include/linux/iommu.h | 3 +++
> > 2 files changed, 28 insertions(+)
> >
> > diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
> > index 9e41ad4e3219b6..1e0c5d9a0370fb 100644
> > --- a/drivers/iommu/iommu.c
> > +++ b/drivers/iommu/iommu.c
> > @@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu)
> > spin_lock(&iommu_device_lock);
> > list_del(&iommu->list);
> > spin_unlock(&iommu_device_lock);
> > +
> > + /* Pairs with the alloc in generic_single_device_group() */
> > + iommu_group_put(iommu->singleton_group);
> > }
> > EXPORT_SYMBOL_GPL(iommu_device_unregister);
> > @@ -1595,6 +1598,28 @@ struct iommu_group *generic_device_group(struct device *dev)
> > }
> > EXPORT_SYMBOL_GPL(generic_device_group);
> > +/*
> > + * Generic device_group call-back function. It just allocates one
> > + * iommu-group per iommu driver.
> > + */
> > +struct iommu_group *generic_single_device_group(struct device *dev)
> > +{
> > + struct iommu_device *iommu = dev->iommu->iommu_dev;
> > +
> > + lockdep_assert_held(&dev_iommu_group_lock);
> > +
> > + if (!iommu->singleton_group) {
> > + struct iommu_group *group;
> > +
> > + group = iommu_group_alloc();
> > + if (IS_ERR(group))
> > + return group;
> > + iommu->singleton_group = group;
> > + }
> > + return iommu_group_ref_get(iommu->singleton_group);
> > +}
> > +EXPORT_SYMBOL_GPL(generic_single_device_group);
>
> When allocating the singleton group for the first time, the group's
> refcount is taken twice.
Yes, that is correct.
The refcount from alloc belongs to iommu->singleton_group and the
pair'd put is here:
@@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu)
spin_lock(&iommu_device_lock);
list_del(&iommu->list);
spin_unlock(&iommu_device_lock);
+
+ /* Pairs with the alloc in generic_single_device_group() */
+ iommu_group_put(iommu->singleton_group);
}
The refcount from iommu_group_ref_get() belongs to the caller and the
caller must have a paired put.
> struct iommu_group *generic_single_device_group(struct device *dev)
> {
> struct iommu_device *iommu = dev->iommu->iommu_dev;
> struct iommu_group *group;
>
> lockdep_assert_held(&dev_iommu_group_lock);
>
> if (iommu->singleton_group)
> return iommu_group_ref_get(iommu->singleton_group);
>
> group = iommu_group_alloc();
> if (!IS_ERR(group))
> iommu->singleton_group = group;
>
> return group;
This will UAF the iommu->singleton_group, consider a caller that does:
iommu_group_put(generic_single_device_group(dev))
Jason
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-20 12:04 ` Jason Gunthorpe
@ 2023-07-20 14:01 ` Baolu Lu
2023-07-21 17:19 ` Jason Gunthorpe
0 siblings, 1 reply; 26+ messages in thread
From: Baolu Lu @ 2023-07-20 14:01 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: baolu.lu, Baolin Wang, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang, Alex Williamson
On 2023/7/20 20:04, Jason Gunthorpe wrote:
> On Thu, Jul 20, 2023 at 03:39:27PM +0800, Baolu Lu wrote:
>> On 2023/7/19 3:05, Jason Gunthorpe wrote:
>>> This implements the common pattern seen in drivers of a single
>>> iommu_group for the entire iommu driver. Implement this in core code
>>> so the drivers that want this can select it from their ops.
>>>
>>> Signed-off-by: Jason Gunthorpe<jgg@nvidia.com>
>>> ---
>>> drivers/iommu/iommu.c | 25 +++++++++++++++++++++++++
>>> include/linux/iommu.h | 3 +++
>>> 2 files changed, 28 insertions(+)
>>>
>>> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
>>> index 9e41ad4e3219b6..1e0c5d9a0370fb 100644
>>> --- a/drivers/iommu/iommu.c
>>> +++ b/drivers/iommu/iommu.c
>>> @@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu)
>>> spin_lock(&iommu_device_lock);
>>> list_del(&iommu->list);
>>> spin_unlock(&iommu_device_lock);
>>> +
>>> + /* Pairs with the alloc in generic_single_device_group() */
>>> + iommu_group_put(iommu->singleton_group);
>>> }
>>> EXPORT_SYMBOL_GPL(iommu_device_unregister);
>>> @@ -1595,6 +1598,28 @@ struct iommu_group *generic_device_group(struct device *dev)
>>> }
>>> EXPORT_SYMBOL_GPL(generic_device_group);
>>> +/*
>>> + * Generic device_group call-back function. It just allocates one
>>> + * iommu-group per iommu driver.
>>> + */
>>> +struct iommu_group *generic_single_device_group(struct device *dev)
>>> +{
>>> + struct iommu_device *iommu = dev->iommu->iommu_dev;
>>> +
>>> + lockdep_assert_held(&dev_iommu_group_lock);
>>> +
>>> + if (!iommu->singleton_group) {
>>> + struct iommu_group *group;
>>> +
>>> + group = iommu_group_alloc();
>>> + if (IS_ERR(group))
>>> + return group;
>>> + iommu->singleton_group = group;
>>> + }
>>> + return iommu_group_ref_get(iommu->singleton_group);
>>> +}
>>> +EXPORT_SYMBOL_GPL(generic_single_device_group);
>> When allocating the singleton group for the first time, the group's
>> refcount is taken twice.
> Yes, that is correct.
>
> The refcount from alloc belongs to iommu->singleton_group and the
> pair'd put is here:
>
> @@ -289,6 +289,9 @@ void iommu_device_unregister(struct iommu_device *iommu)
> spin_lock(&iommu_device_lock);
> list_del(&iommu->list);
> spin_unlock(&iommu_device_lock);
> +
> + /* Pairs with the alloc in generic_single_device_group() */
> + iommu_group_put(iommu->singleton_group);
> }
>
> The refcount from iommu_group_ref_get() belongs to the caller and the
> caller must have a paired put.
Oh, yes! The extra reference counter is paired with above put.
Thanks for the explanation.
Then, another small comment:
iommu->singleton_group will be freed with above put, right? Do you need
to set iommu->singleton_group to NULL? Given that iommu_device is not
freed here.
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-20 14:01 ` Baolu Lu
@ 2023-07-21 17:19 ` Jason Gunthorpe
2023-07-22 14:01 ` Baolu Lu
0 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-21 17:19 UTC (permalink / raw)
To: Baolu Lu
Cc: Baolin Wang, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang, Alex Williamson
On Thu, Jul 20, 2023 at 10:01:54PM +0800, Baolu Lu wrote:
> Then, another small comment:
>
> iommu->singleton_group will be freed with above put, right? Do you need
> to set iommu->singleton_group to NULL? Given that iommu_device is not
> freed here.
Well, I think the general API is we expect the caller to free the
iommu_device after calling unregister so this would be like all the
other places we free or put something then go on to release the
memory.
At the very least if the caller thinks it should re-use the
iommu_device then it needs to zero it.
Notice this also doesn't hold the lock while putting it, we require no
concurrent calls to probe with unregister.
Jason
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-21 17:19 ` Jason Gunthorpe
@ 2023-07-22 14:01 ` Baolu Lu
0 siblings, 0 replies; 26+ messages in thread
From: Baolu Lu @ 2023-07-22 14:01 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: baolu.lu, Baolin Wang, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang, Alex Williamson
On 2023/7/22 1:19, Jason Gunthorpe wrote:
> On Thu, Jul 20, 2023 at 10:01:54PM +0800, Baolu Lu wrote:
>
>> Then, another small comment:
>>
>> iommu->singleton_group will be freed with above put, right? Do you need
>> to set iommu->singleton_group to NULL? Given that iommu_device is not
>> freed here.
> Well, I think the general API is we expect the caller to free the
> iommu_device after calling unregister so this would be like all the
> other places we free or put something then go on to release the
> memory.
>
> At the very least if the caller thinks it should re-use the
> iommu_device then it needs to zero it.
>
> Notice this also doesn't hold the lock while putting it, we require no
> concurrent calls to probe with unregister.
Fair enough. Thanks for the explanation.
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* RE: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-18 19:05 ` [PATCH 03/10] iommu: Add generic_single_device_group() Jason Gunthorpe
2023-07-20 7:39 ` Baolu Lu
@ 2023-07-21 7:17 ` Tian, Kevin
2023-07-22 14:02 ` Baolu Lu
2 siblings, 0 replies; 26+ messages in thread
From: Tian, Kevin @ 2023-07-21 7:17 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, Lu Baolu, David Woodhouse,
Heiko Stuebner, iommu@lists.linux.dev, Jernej Skrabec,
Joerg Roedel, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev,
Orson Zhai, Robin Murphy, Samuel Holland, Chen-Yu Tsai,
Will Deacon, Chunyan Zhang
Cc: Alex Williamson
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, July 19, 2023 3:06 AM
>
> This implements the common pattern seen in drivers of a single
> iommu_group for the entire iommu driver. Implement this in core code
> so the drivers that want this can select it from their ops.
strictly speaking it's per-iommu-instance group. 😊
> +/*
> + * Generic device_group call-back function. It just allocates one
> + * iommu-group per iommu driver.
> + */
> +struct iommu_group *generic_single_device_group(struct device *dev)
> +{
> + struct iommu_device *iommu = dev->iommu->iommu_dev;
> +
> + lockdep_assert_held(&dev_iommu_group_lock);
> +
> + if (!iommu->singleton_group) {
> + struct iommu_group *group;
> +
> + group = iommu_group_alloc();
> + if (IS_ERR(group))
> + return group;
> + iommu->singleton_group = group;
> + }
> + return iommu_group_ref_get(iommu->singleton_group);
'singleton' is a bit confusing whether it means a single
group per instance or a group having only a single device.
I don't know a better name, but probably just using
iommu->group is not worse than the 'singleton_' prefix...
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [PATCH 03/10] iommu: Add generic_single_device_group()
2023-07-18 19:05 ` [PATCH 03/10] iommu: Add generic_single_device_group() Jason Gunthorpe
2023-07-20 7:39 ` Baolu Lu
2023-07-21 7:17 ` Tian, Kevin
@ 2023-07-22 14:02 ` Baolu Lu
2 siblings, 0 replies; 26+ messages in thread
From: Baolu Lu @ 2023-07-22 14:02 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, David Woodhouse, Heiko Stuebner,
iommu, Jernej Skrabec, Joerg Roedel, linux-arm-kernel,
linux-rockchip, linux-sunxi, Orson Zhai, Robin Murphy,
Samuel Holland, Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: baolu.lu, Alex Williamson
On 2023/7/19 3:05, Jason Gunthorpe wrote:
> This implements the common pattern seen in drivers of a single
> iommu_group for the entire iommu driver. Implement this in core code
> so the drivers that want this can select it from their ops.
>
> Signed-off-by: Jason Gunthorpe<jgg@nvidia.com>
> ---
> drivers/iommu/iommu.c | 25 +++++++++++++++++++++++++
> include/linux/iommu.h | 3 +++
> 2 files changed, 28 insertions(+)
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 04/10] iommu/sun50i: Convert to generic_single_device_group()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (2 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 03/10] iommu: Add generic_single_device_group() Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 05/10] iommu/sprd: " Jason Gunthorpe
` (5 subsequent siblings)
9 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Use the new helper.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/sun50i-iommu.c | 29 ++++++-----------------------
1 file changed, 6 insertions(+), 23 deletions(-)
diff --git a/drivers/iommu/sun50i-iommu.c b/drivers/iommu/sun50i-iommu.c
index 74c5cb93e90027..b8df655185ab2a 100644
--- a/drivers/iommu/sun50i-iommu.c
+++ b/drivers/iommu/sun50i-iommu.c
@@ -107,7 +107,6 @@ struct sun50i_iommu {
struct clk *clk;
struct iommu_domain *domain;
- struct iommu_group *group;
struct kmem_cache *pt_pool;
};
@@ -808,13 +807,6 @@ static struct iommu_device *sun50i_iommu_probe_device(struct device *dev)
return &iommu->iommu;
}
-static struct iommu_group *sun50i_iommu_device_group(struct device *dev)
-{
- struct sun50i_iommu *iommu = sun50i_iommu_from_dev(dev);
-
- return iommu_group_ref_get(iommu->group);
-}
-
static int sun50i_iommu_of_xlate(struct device *dev,
struct of_phandle_args *args)
{
@@ -828,7 +820,7 @@ static int sun50i_iommu_of_xlate(struct device *dev,
static const struct iommu_ops sun50i_iommu_ops = {
.pgsize_bitmap = SZ_4K,
- .device_group = sun50i_iommu_device_group,
+ .device_group = generic_single_device_group,
.domain_alloc = sun50i_iommu_domain_alloc,
.of_xlate = sun50i_iommu_of_xlate,
.probe_device = sun50i_iommu_probe_device,
@@ -995,42 +987,36 @@ static int sun50i_iommu_probe(struct platform_device *pdev)
if (!iommu->pt_pool)
return -ENOMEM;
- iommu->group = iommu_group_alloc();
- if (IS_ERR(iommu->group)) {
- ret = PTR_ERR(iommu->group);
- goto err_free_cache;
- }
-
iommu->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(iommu->base)) {
ret = PTR_ERR(iommu->base);
- goto err_free_group;
+ goto err_free_cache;
}
irq = platform_get_irq(pdev, 0);
if (irq < 0) {
ret = irq;
- goto err_free_group;
+ goto err_free_cache;
}
iommu->clk = devm_clk_get(&pdev->dev, NULL);
if (IS_ERR(iommu->clk)) {
dev_err(&pdev->dev, "Couldn't get our clock.\n");
ret = PTR_ERR(iommu->clk);
- goto err_free_group;
+ goto err_free_cache;
}
iommu->reset = devm_reset_control_get(&pdev->dev, NULL);
if (IS_ERR(iommu->reset)) {
dev_err(&pdev->dev, "Couldn't get our reset line.\n");
ret = PTR_ERR(iommu->reset);
- goto err_free_group;
+ goto err_free_cache;
}
ret = iommu_device_sysfs_add(&iommu->iommu, &pdev->dev,
NULL, dev_name(&pdev->dev));
if (ret)
- goto err_free_group;
+ goto err_free_cache;
ret = iommu_device_register(&iommu->iommu, &sun50i_iommu_ops, &pdev->dev);
if (ret)
@@ -1049,9 +1035,6 @@ static int sun50i_iommu_probe(struct platform_device *pdev)
err_remove_sysfs:
iommu_device_sysfs_remove(&iommu->iommu);
-err_free_group:
- iommu_group_put(iommu->group);
-
err_free_cache:
kmem_cache_destroy(iommu->pt_pool);
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* [PATCH 05/10] iommu/sprd: Convert to generic_single_device_group()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (3 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 04/10] iommu/sun50i: Convert to generic_single_device_group() Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 06/10] iommu/rockchip: " Jason Gunthorpe
` (4 subsequent siblings)
9 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Use the new helper.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/sprd-iommu.c | 24 ++----------------------
1 file changed, 2 insertions(+), 22 deletions(-)
diff --git a/drivers/iommu/sprd-iommu.c b/drivers/iommu/sprd-iommu.c
index 39e34fdeccda78..9fc98fd144e445 100644
--- a/drivers/iommu/sprd-iommu.c
+++ b/drivers/iommu/sprd-iommu.c
@@ -69,7 +69,6 @@ struct sprd_iommu_device {
void __iomem *base;
struct device *dev;
struct iommu_device iommu;
- struct iommu_group *group;
struct clk *eb;
};
@@ -397,13 +396,6 @@ static struct iommu_device *sprd_iommu_probe_device(struct device *dev)
return &sdev->iommu;
}
-static struct iommu_group *sprd_iommu_device_group(struct device *dev)
-{
- struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev);
-
- return iommu_group_ref_get(sdev->group);
-}
-
static int sprd_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
{
struct platform_device *pdev;
@@ -421,7 +413,7 @@ static int sprd_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
static const struct iommu_ops sprd_iommu_ops = {
.domain_alloc = sprd_iommu_domain_alloc,
.probe_device = sprd_iommu_probe_device,
- .device_group = sprd_iommu_device_group,
+ .device_group = generic_single_device_group,
.of_xlate = sprd_iommu_of_xlate,
.pgsize_bitmap = SPRD_IOMMU_PAGE_SIZE,
.owner = THIS_MODULE,
@@ -494,16 +486,9 @@ static int sprd_iommu_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, sdev);
sdev->dev = dev;
- /* All the client devices are in the same iommu-group */
- sdev->group = iommu_group_alloc();
- if (IS_ERR(sdev->group)) {
- ret = PTR_ERR(sdev->group);
- goto free_page;
- }
-
ret = iommu_device_sysfs_add(&sdev->iommu, dev, NULL, dev_name(dev));
if (ret)
- goto put_group;
+ goto free_page;
ret = iommu_device_register(&sdev->iommu, &sprd_iommu_ops, dev);
if (ret)
@@ -528,8 +513,6 @@ static int sprd_iommu_probe(struct platform_device *pdev)
iommu_device_unregister(&sdev->iommu);
remove_sysfs:
iommu_device_sysfs_remove(&sdev->iommu);
-put_group:
- iommu_group_put(sdev->group);
free_page:
dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa);
return ret;
@@ -541,9 +524,6 @@ static void sprd_iommu_remove(struct platform_device *pdev)
dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa);
- iommu_group_put(sdev->group);
- sdev->group = NULL;
-
platform_set_drvdata(pdev, NULL);
iommu_device_sysfs_remove(&sdev->iommu);
iommu_device_unregister(&sdev->iommu);
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* [PATCH 06/10] iommu/rockchip: Convert to generic_single_device_group()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (4 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 05/10] iommu/sprd: " Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 07/10] iommu/ipmmu-vmsa: " Jason Gunthorpe
` (3 subsequent siblings)
9 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Use the new helper.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/rockchip-iommu.c | 22 ++--------------------
1 file changed, 2 insertions(+), 20 deletions(-)
diff --git a/drivers/iommu/rockchip-iommu.c b/drivers/iommu/rockchip-iommu.c
index 8ff69fbf9f65db..91f13cc9411548 100644
--- a/drivers/iommu/rockchip-iommu.c
+++ b/drivers/iommu/rockchip-iommu.c
@@ -113,7 +113,6 @@ struct rk_iommu {
struct iommu_device iommu;
struct list_head node; /* entry in rk_iommu_domain.iommus */
struct iommu_domain *domain; /* domain to which iommu is attached */
- struct iommu_group *group;
};
struct rk_iommudata {
@@ -1155,15 +1154,6 @@ static void rk_iommu_release_device(struct device *dev)
device_link_del(data->link);
}
-static struct iommu_group *rk_iommu_device_group(struct device *dev)
-{
- struct rk_iommu *iommu;
-
- iommu = rk_iommu_from_dev(dev);
-
- return iommu_group_ref_get(iommu->group);
-}
-
static int rk_iommu_of_xlate(struct device *dev,
struct of_phandle_args *args)
{
@@ -1189,7 +1179,7 @@ static const struct iommu_ops rk_iommu_ops = {
.domain_alloc = rk_iommu_domain_alloc,
.probe_device = rk_iommu_probe_device,
.release_device = rk_iommu_release_device,
- .device_group = rk_iommu_device_group,
+ .device_group = generic_single_device_group,
#ifdef CONFIG_ARM
.set_platform_dma_ops = rk_iommu_set_platform_dma,
#endif
@@ -1280,15 +1270,9 @@ static int rk_iommu_probe(struct platform_device *pdev)
if (err)
return err;
- iommu->group = iommu_group_alloc();
- if (IS_ERR(iommu->group)) {
- err = PTR_ERR(iommu->group);
- goto err_unprepare_clocks;
- }
-
err = iommu_device_sysfs_add(&iommu->iommu, dev, NULL, dev_name(dev));
if (err)
- goto err_put_group;
+ goto err_unprepare_clocks;
err = iommu_device_register(&iommu->iommu, &rk_iommu_ops, dev);
if (err)
@@ -1325,8 +1309,6 @@ static int rk_iommu_probe(struct platform_device *pdev)
pm_runtime_disable(dev);
err_remove_sysfs:
iommu_device_sysfs_remove(&iommu->iommu);
-err_put_group:
- iommu_group_put(iommu->group);
err_unprepare_clocks:
clk_bulk_unprepare(iommu->num_clocks, iommu->clocks);
return err;
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* [PATCH 07/10] iommu/ipmmu-vmsa: Convert to generic_single_device_group()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (5 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 06/10] iommu/rockchip: " Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-21 7:20 ` Tian, Kevin
2023-07-18 19:05 ` [PATCH 08/10] iommu/omap: " Jason Gunthorpe
` (2 subsequent siblings)
9 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Use the new helper.
This driver is kind of weird since in ARM mode it pretends it has
per-device groups, but ARM64 mode does not.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/ipmmu-vmsa.c | 22 +++++-----------------
1 file changed, 5 insertions(+), 17 deletions(-)
diff --git a/drivers/iommu/ipmmu-vmsa.c b/drivers/iommu/ipmmu-vmsa.c
index 9f64c5c9f5b90a..55b9b29221461c 100644
--- a/drivers/iommu/ipmmu-vmsa.c
+++ b/drivers/iommu/ipmmu-vmsa.c
@@ -63,7 +63,6 @@ struct ipmmu_vmsa_device {
struct ipmmu_vmsa_domain *domains[IPMMU_CTX_MAX];
s8 utlb_ctx[IPMMU_UTLB_MAX];
- struct iommu_group *group;
struct dma_iommu_mapping *mapping;
};
@@ -832,28 +831,17 @@ static void ipmmu_release_device(struct device *dev)
arm_iommu_release_mapping(mmu->mapping);
}
-static struct iommu_group *ipmmu_find_group(struct device *dev)
-{
- struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
- struct iommu_group *group;
-
- if (mmu->group)
- return iommu_group_ref_get(mmu->group);
-
- group = iommu_group_alloc();
- if (!IS_ERR(group))
- mmu->group = group;
-
- return group;
-}
-
static const struct iommu_ops ipmmu_ops = {
.domain_alloc = ipmmu_domain_alloc,
.probe_device = ipmmu_probe_device,
.release_device = ipmmu_release_device,
.probe_finalize = ipmmu_probe_finalize,
+ /*
+ * FIXME: The device grouping is a fixed property of the hardware's
+ * ability to isolate and control DMA, it should not depend on kconfig.
+ */
.device_group = IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_IOMMU_DMA)
- ? generic_device_group : ipmmu_find_group,
+ ? generic_device_group : generic_single_device_group,
.pgsize_bitmap = SZ_1G | SZ_2M | SZ_4K,
.of_xlate = ipmmu_of_xlate,
.default_domain_ops = &(const struct iommu_domain_ops) {
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* RE: [PATCH 07/10] iommu/ipmmu-vmsa: Convert to generic_single_device_group()
2023-07-18 19:05 ` [PATCH 07/10] iommu/ipmmu-vmsa: " Jason Gunthorpe
@ 2023-07-21 7:20 ` Tian, Kevin
2023-07-21 12:04 ` Jason Gunthorpe
0 siblings, 1 reply; 26+ messages in thread
From: Tian, Kevin @ 2023-07-21 7:20 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, Lu Baolu, David Woodhouse,
Heiko Stuebner, iommu@lists.linux.dev, Jernej Skrabec,
Joerg Roedel, linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev,
Orson Zhai, Robin Murphy, Samuel Holland, Chen-Yu Tsai,
Will Deacon, Chunyan Zhang
Cc: Alex Williamson
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Wednesday, July 19, 2023 3:06 AM
>
> -static struct iommu_group *ipmmu_find_group(struct device *dev)
> -{
> - struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
> - struct iommu_group *group;
> -
> - if (mmu->group)
> - return iommu_group_ref_get(mmu->group);
> -
> - group = iommu_group_alloc();
> - if (!IS_ERR(group))
> - mmu->group = group;
> -
> - return group;
> -}
Looks existing code has a leak w/o freeing the group in ipmmu_remove().
While at it could you add a patch to fix it allowing backport and then this
patch to convert to the generic helper?
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [PATCH 07/10] iommu/ipmmu-vmsa: Convert to generic_single_device_group()
2023-07-21 7:20 ` Tian, Kevin
@ 2023-07-21 12:04 ` Jason Gunthorpe
0 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-21 12:04 UTC (permalink / raw)
To: Tian, Kevin
Cc: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner,
iommu@lists.linux.dev, Jernej Skrabec, Joerg Roedel,
linux-arm-kernel@lists.infradead.org,
linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev,
Orson Zhai, Robin Murphy, Samuel Holland, Chen-Yu Tsai,
Will Deacon, Chunyan Zhang, Alex Williamson
On Fri, Jul 21, 2023 at 07:20:21AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Wednesday, July 19, 2023 3:06 AM
> >
> > -static struct iommu_group *ipmmu_find_group(struct device *dev)
> > -{
> > - struct ipmmu_vmsa_device *mmu = to_ipmmu(dev);
> > - struct iommu_group *group;
> > -
> > - if (mmu->group)
> > - return iommu_group_ref_get(mmu->group);
> > -
> > - group = iommu_group_alloc();
> > - if (!IS_ERR(group))
> > - mmu->group = group;
> > -
> > - return group;
> > -}
>
> Looks existing code has a leak w/o freeing the group in ipmmu_remove().
IIRC many of the drivers had this, or other bugs here.
> While at it could you add a patch to fix it allowing backport and then this
> patch to convert to the generic helper?
It isn't worth it, these drivers are never removed, so it is a bug
that nobody will ever hit.
Jason
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 08/10] iommu/omap: Convert to generic_single_device_group()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (6 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 07/10] iommu/ipmmu-vmsa: " Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 09/10] iommu: Complete the locking for dev->iommu_group Jason Gunthorpe
2023-07-18 19:05 ` [PATCH 10/10] iommu/intel: Fix missing locking for show_device_domain_translation() Jason Gunthorpe
9 siblings, 0 replies; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Use the new helper.
For some reason omap will probe its driver even if it doesn't load an
iommu driver. Keep this working by keeping a bool to track if the iommu
driver was started.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/omap-iommu.c | 30 ++++--------------------------
drivers/iommu/omap-iommu.h | 2 +-
2 files changed, 5 insertions(+), 27 deletions(-)
diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c
index 537e402f9bba97..97c45f50bf4332 100644
--- a/drivers/iommu/omap-iommu.c
+++ b/drivers/iommu/omap-iommu.c
@@ -1225,18 +1225,15 @@ static int omap_iommu_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, obj);
if (omap_iommu_can_register(pdev)) {
- obj->group = iommu_group_alloc();
- if (IS_ERR(obj->group))
- return PTR_ERR(obj->group);
-
err = iommu_device_sysfs_add(&obj->iommu, obj->dev, NULL,
obj->name);
if (err)
- goto out_group;
+ return err;
err = iommu_device_register(&obj->iommu, &omap_iommu_ops, &pdev->dev);
if (err)
goto out_sysfs;
+ obj->has_iommu_driver = true;
}
pm_runtime_enable(obj->dev);
@@ -1252,8 +1249,6 @@ static int omap_iommu_probe(struct platform_device *pdev)
out_sysfs:
iommu_device_sysfs_remove(&obj->iommu);
-out_group:
- iommu_group_put(obj->group);
return err;
}
@@ -1261,10 +1256,7 @@ static void omap_iommu_remove(struct platform_device *pdev)
{
struct omap_iommu *obj = platform_get_drvdata(pdev);
- if (obj->group) {
- iommu_group_put(obj->group);
- obj->group = NULL;
-
+ if (obj->has_iommu_driver) {
iommu_device_sysfs_remove(&obj->iommu);
iommu_device_unregister(&obj->iommu);
}
@@ -1717,25 +1709,11 @@ static void omap_iommu_release_device(struct device *dev)
}
-static struct iommu_group *omap_iommu_device_group(struct device *dev)
-{
- struct omap_iommu_arch_data *arch_data = dev_iommu_priv_get(dev);
- struct iommu_group *group = ERR_PTR(-EINVAL);
-
- if (!arch_data)
- return ERR_PTR(-ENODEV);
-
- if (arch_data->iommu_dev)
- group = iommu_group_ref_get(arch_data->iommu_dev->group);
-
- return group;
-}
-
static const struct iommu_ops omap_iommu_ops = {
.domain_alloc = omap_iommu_domain_alloc,
.probe_device = omap_iommu_probe_device,
.release_device = omap_iommu_release_device,
- .device_group = omap_iommu_device_group,
+ .device_group = generic_single_device_group,
.set_platform_dma_ops = omap_iommu_set_platform_dma,
.pgsize_bitmap = OMAP_IOMMU_PGSIZES,
.default_domain_ops = &(const struct iommu_domain_ops) {
diff --git a/drivers/iommu/omap-iommu.h b/drivers/iommu/omap-iommu.h
index 18ee713ede784d..27697109ec79a5 100644
--- a/drivers/iommu/omap-iommu.h
+++ b/drivers/iommu/omap-iommu.h
@@ -80,7 +80,7 @@ struct omap_iommu {
u32 id;
struct iommu_device iommu;
- struct iommu_group *group;
+ bool has_iommu_driver;
u8 pwrst;
};
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* [PATCH 09/10] iommu: Complete the locking for dev->iommu_group
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (7 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 08/10] iommu/omap: " Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-20 9:55 ` Baolu Lu
2023-07-18 19:05 ` [PATCH 10/10] iommu/intel: Fix missing locking for show_device_domain_translation() Jason Gunthorpe
9 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
Revise the locking for dev->iommu_group so that it has three safe ways to
access it:
- It is read by a probe'd device driver. So long as a device driver is
probed the dev->iommu_group will be guaranteed stable without further
locking.
- Read under the device_lock(), this primarily protects against
parallel probe of the same device, and parallel probe/remove
- Read/Write under the global dev_iommu_group_lock. This is used during
probe time discovery of groups. Device drivers will scan unlocked
portions of the device tree to locate an already existing group. These
scans can access the dev->iommu_group under the global lock to single
thread determining and installing the group. This ensures that groups
are reliably formed.
Narrow the scope of the global dev_iommu_group_lock to be only during the
dev->iommu_group setup, and not for the entire probing.
Prior patches removed the various races inherent to the probe process by
consolidating all the work under the group->mutex. In this configuration
it is fine if two devices race to the group_device step of a new
iommu_group, the group->mutex locking will ensure the group_device and
domain setup part remains properly ordered.
Add the missing locking on the remove paths. For iommu_deinit_device() it
is necessary to hold the dev_iommu_group_lock due to possible races during
probe error unwind.
Fully lock the iommu_group_add/remove_device() path so we can use lockdep
assertions. Other than lockdep this is redundant, VFIO no-iommu doesn't
use group clustering.
For iommu_release_device() it is redundant, as we expect no external
references to the struct device by this point, but it is harmless so
add the missing lock to allow lockdep assertions to work.
This resolves the remarks of the comment in __iommu_probe_device().
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommu.c | 54 +++++++++++++++++++++++++++++--------------
1 file changed, 37 insertions(+), 17 deletions(-)
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 1e0c5d9a0370fb..0f75428be79d4d 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -369,14 +369,17 @@ static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
if (ret)
goto err_release;
+ mutex_lock(&dev_iommu_group_lock);
group = ops->device_group(dev);
if (WARN_ON_ONCE(group == NULL))
group = ERR_PTR(-EINVAL);
if (IS_ERR(group)) {
+ mutex_unlock(&dev_iommu_group_lock);
ret = PTR_ERR(group);
goto err_unlink;
}
dev->iommu_group = group;
+ mutex_unlock(&dev_iommu_group_lock);
dev->iommu->iommu_dev = iommu_dev;
dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
@@ -434,7 +437,9 @@ static void iommu_deinit_device(struct device *dev)
}
/* Caller must put iommu_group */
+ mutex_lock(&dev_iommu_group_lock);
dev->iommu_group = NULL;
+ mutex_unlock(&dev_iommu_group_lock);
module_put(ops->owner);
dev_iommu_free(dev);
}
@@ -449,13 +454,11 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
if (!ops)
return -ENODEV;
/*
- * Serialise to avoid races between IOMMU drivers registering in
- * parallel and/or the "replay" calls from ACPI/OF code via client
- * driver probe. Once the latter have been cleaned up we should
- * probably be able to use device_lock() here to minimise the scope,
- * but for now enforcing a simple global ordering is fine.
+ * Allow __iommu_probe_device() to be safely called in parallel,
+ * both dev->iommu_group and the initial setup of dev->iommu are
+ * protected this way.
*/
- mutex_lock(&dev_iommu_group_lock);
+ device_lock(dev);
/* Device is probed already if in a group */
if (dev->iommu_group) {
@@ -501,7 +504,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
list_add_tail(&group->entry, group_list);
}
mutex_unlock(&group->mutex);
- mutex_unlock(&dev_iommu_group_lock);
+ device_unlock(dev);
if (dev_is_pci(dev))
iommu_dma_set_pci_32bit_workaround(dev);
@@ -516,8 +519,7 @@ static int __iommu_probe_device(struct device *dev, struct list_head *group_list
mutex_unlock(&group->mutex);
iommu_group_put(group);
out_unlock:
- mutex_unlock(&dev_iommu_group_lock);
-
+ device_unlock(dev);
return ret;
}
@@ -566,6 +568,7 @@ static void __iommu_group_remove_device(struct device *dev)
struct iommu_group *group = dev->iommu_group;
struct group_device *device;
+ device_lock_assert(dev);
mutex_lock(&group->mutex);
for_each_group_device(group, device) {
if (device->dev != dev)
@@ -590,14 +593,23 @@ static void __iommu_group_remove_device(struct device *dev)
static void iommu_release_device(struct device *dev)
{
- struct iommu_group *group = dev->iommu_group;
+ struct iommu_group *group;
+ /*
+ * This locking for dev->iommu_group is overkill when this is called
+ * from the BUS_NOTIFY_REMOVED_DEVICE, as we don't expect any other
+ * threads to have a reference to the device at that point. Keep it
+ * because this isn't a performance path and helps lockdep analysis.
+ */
+ device_lock(dev);
+ group = dev->iommu_group;
if (group)
__iommu_group_remove_device(dev);
/* Free any fwspec if no iommu_driver was ever attached */
if (dev->iommu)
dev_iommu_free(dev);
+ device_unlock(dev);
}
static int __init iommu_set_def_domain_type(char *str)
@@ -1080,6 +1092,8 @@ static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
int ret, i = 0;
struct group_device *device;
+ device_lock_assert(dev);
+
device = kzalloc(sizeof(*device), GFP_KERNEL);
if (!device)
return ERR_PTR(-ENOMEM);
@@ -1141,9 +1155,12 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
{
struct group_device *gdev;
+ device_lock(dev);
gdev = iommu_group_alloc_device(group, dev);
- if (IS_ERR(gdev))
+ if (IS_ERR(gdev)) {
+ device_unlock(dev);
return PTR_ERR(gdev);
+ }
iommu_group_ref_get(group);
dev->iommu_group = group;
@@ -1151,6 +1168,7 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
mutex_lock(&group->mutex);
list_add_tail(&gdev->list, &group->devices);
mutex_unlock(&group->mutex);
+ device_unlock(dev);
return 0;
}
EXPORT_SYMBOL_GPL(iommu_group_add_device);
@@ -1164,14 +1182,16 @@ EXPORT_SYMBOL_GPL(iommu_group_add_device);
*/
void iommu_group_remove_device(struct device *dev)
{
- struct iommu_group *group = dev->iommu_group;
+ struct iommu_group *group;
- if (!group)
- return;
+ device_lock(dev);
+ group = dev->iommu_group;
+ if (group) {
+ dev_info(dev, "Removing from iommu group %d\n", group->id);
+ __iommu_group_remove_device(dev);
+ }
+ device_unlock(dev);
- dev_info(dev, "Removing from iommu group %d\n", group->id);
-
- __iommu_group_remove_device(dev);
}
EXPORT_SYMBOL_GPL(iommu_group_remove_device);
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 09/10] iommu: Complete the locking for dev->iommu_group
2023-07-18 19:05 ` [PATCH 09/10] iommu: Complete the locking for dev->iommu_group Jason Gunthorpe
@ 2023-07-20 9:55 ` Baolu Lu
0 siblings, 0 replies; 26+ messages in thread
From: Baolu Lu @ 2023-07-20 9:55 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, David Woodhouse, Heiko Stuebner,
iommu, Jernej Skrabec, Joerg Roedel, linux-arm-kernel,
linux-rockchip, linux-sunxi, Orson Zhai, Robin Murphy,
Samuel Holland, Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: baolu.lu, Alex Williamson
On 2023/7/19 3:05, Jason Gunthorpe wrote:
> Revise the locking for dev->iommu_group so that it has three safe ways to
> access it:
>
> - It is read by a probe'd device driver. So long as a device driver is
> probed the dev->iommu_group will be guaranteed stable without further
> locking.
>
> - Read under the device_lock(), this primarily protects against
> parallel probe of the same device, and parallel probe/remove
>
> - Read/Write under the global dev_iommu_group_lock. This is used during
> probe time discovery of groups. Device drivers will scan unlocked
> portions of the device tree to locate an already existing group. These
> scans can access the dev->iommu_group under the global lock to single
> thread determining and installing the group. This ensures that groups
> are reliably formed.
>
> Narrow the scope of the global dev_iommu_group_lock to be only during the
> dev->iommu_group setup, and not for the entire probing.
>
> Prior patches removed the various races inherent to the probe process by
> consolidating all the work under the group->mutex. In this configuration
> it is fine if two devices race to the group_device step of a new
> iommu_group, the group->mutex locking will ensure the group_device and
> domain setup part remains properly ordered.
>
> Add the missing locking on the remove paths. For iommu_deinit_device() it
> is necessary to hold the dev_iommu_group_lock due to possible races during
> probe error unwind.
>
> Fully lock the iommu_group_add/remove_device() path so we can use lockdep
> assertions. Other than lockdep this is redundant, VFIO no-iommu doesn't
> use group clustering.
>
> For iommu_release_device() it is redundant, as we expect no external
> references to the struct device by this point, but it is harmless so
> add the missing lock to allow lockdep assertions to work.
>
> This resolves the remarks of the comment in __iommu_probe_device().
>
> Signed-off-by: Jason Gunthorpe<jgg@nvidia.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [PATCH 10/10] iommu/intel: Fix missing locking for show_device_domain_translation()
2023-07-18 19:05 [PATCH 00/10] Refine the locking for dev->iommu_group Jason Gunthorpe
` (8 preceding siblings ...)
2023-07-18 19:05 ` [PATCH 09/10] iommu: Complete the locking for dev->iommu_group Jason Gunthorpe
@ 2023-07-18 19:05 ` Jason Gunthorpe
2023-07-20 9:56 ` Baolu Lu
9 siblings, 1 reply; 26+ messages in thread
From: Jason Gunthorpe @ 2023-07-18 19:05 UTC (permalink / raw)
To: Baolin Wang, Lu Baolu, David Woodhouse, Heiko Stuebner, iommu,
Jernej Skrabec, Joerg Roedel, linux-arm-kernel, linux-rockchip,
linux-sunxi, Orson Zhai, Robin Murphy, Samuel Holland,
Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: Alex Williamson
This is called from bus_for_each_dev() and must lock the device before
calling iommu_group_get().
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/intel/debugfs.c | 34 ++++++++++++++++++----------------
1 file changed, 18 insertions(+), 16 deletions(-)
diff --git a/drivers/iommu/intel/debugfs.c b/drivers/iommu/intel/debugfs.c
index 1f925285104eee..0255c4a2326931 100644
--- a/drivers/iommu/intel/debugfs.c
+++ b/drivers/iommu/intel/debugfs.c
@@ -365,23 +365,25 @@ static int show_device_domain_translation(struct device *dev, void *data)
{
struct iommu_group *group;
+ device_lock(dev);
group = iommu_group_get(dev);
- if (group) {
- /*
- * The group->mutex is held across the callback, which will
- * block calls to iommu_attach/detach_group/device. Hence,
- * the domain of the device will not change during traversal.
- *
- * All devices in an iommu group share a single domain, hence
- * we only dump the domain of the first device. Even though,
- * this code still possibly races with the iommu_unmap()
- * interface. This could be solved by RCU-freeing the page
- * table pages in the iommu_unmap() path.
- */
- iommu_group_for_each_dev(group, data,
- __show_device_domain_translation);
- iommu_group_put(group);
- }
+ device_unlock(dev);
+ if (!group)
+ return 0;
+
+ /*
+ * The group->mutex is held across the callback, which will
+ * block calls to iommu_attach/detach_group/device. Hence,
+ * the domain of the device will not change during traversal.
+ *
+ * All devices in an iommu group share a single domain, hence
+ * we only dump the domain of the first device. Even though,
+ * this code still possibly races with the iommu_unmap()
+ * interface. This could be solved by RCU-freeing the page
+ * table pages in the iommu_unmap() path.
+ */
+ iommu_group_for_each_dev(group, data, __show_device_domain_translation);
+ iommu_group_put(group);
return 0;
}
--
2.41.0
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply related [flat|nested] 26+ messages in thread* Re: [PATCH 10/10] iommu/intel: Fix missing locking for show_device_domain_translation()
2023-07-18 19:05 ` [PATCH 10/10] iommu/intel: Fix missing locking for show_device_domain_translation() Jason Gunthorpe
@ 2023-07-20 9:56 ` Baolu Lu
0 siblings, 0 replies; 26+ messages in thread
From: Baolu Lu @ 2023-07-20 9:56 UTC (permalink / raw)
To: Jason Gunthorpe, Baolin Wang, David Woodhouse, Heiko Stuebner,
iommu, Jernej Skrabec, Joerg Roedel, linux-arm-kernel,
linux-rockchip, linux-sunxi, Orson Zhai, Robin Murphy,
Samuel Holland, Chen-Yu Tsai, Will Deacon, Chunyan Zhang
Cc: baolu.lu, Alex Williamson
On 2023/7/19 3:05, Jason Gunthorpe wrote:
> This is called from bus_for_each_dev() and must lock the device before
> calling iommu_group_get().
>
> Signed-off-by: Jason Gunthorpe<jgg@nvidia.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Best regards,
baolu
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 26+ messages in thread