lkml.org 
[lkml]   [2020]   [Jan]   [1]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[RFC PATCH 3/4] iommu: Preallocate iommu group when probing devices
Date
This splits iommu group allocation from adding devices. This makes
it possible to determine the default domain type for each group as
all devices belonging to the group have been determined.

Signed-off-by: Lu Baolu <baolu.lu@linux.intel.com>
---
drivers/iommu/iommu.c | 92 +++++++++++++++++++++++++++++++------------
1 file changed, 66 insertions(+), 26 deletions(-)

diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index fdd40756dbc1..716326a2ee5b 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -49,6 +49,7 @@ struct group_device {
struct list_head list;
struct device *dev;
char *name;
+ bool added;
};

struct iommu_group_attribute {
@@ -176,7 +177,6 @@ int iommu_probe_device(struct device *dev)
const struct iommu_ops *ops = dev->bus->iommu_ops;
int ret;

- WARN_ON(dev->iommu_group);
if (!ops)
return -EINVAL;

@@ -686,13 +686,28 @@ static int iommu_group_create_direct_mappings(struct iommu_group *group,
int iommu_group_add_device(struct iommu_group *group, struct device *dev)
{
int ret, i = 0;
- struct group_device *device;
+ struct group_device *device = NULL;

- device = kzalloc(sizeof(*device), GFP_KERNEL);
- if (!device)
- return -ENOMEM;
+ mutex_lock(&group->mutex);
+ list_for_each_entry(device, &group->devices, list) {
+ if (device->dev == dev)
+ break;
+ }
+ mutex_unlock(&group->mutex);

- device->dev = dev;
+ if (!device || device->dev != dev) {
+ device = kzalloc(sizeof(*device), GFP_KERNEL);
+ if (!device)
+ return -ENOMEM;
+
+ device->dev = dev;
+ mutex_lock(&group->mutex);
+ list_add_tail(&device->list, &group->devices);
+ mutex_unlock(&group->mutex);
+ } else if (device->added) {
+ kobject_get(group->devices_kobj);
+ return 0;
+ }

ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
if (ret)
@@ -728,13 +743,14 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
iommu_group_create_direct_mappings(group, dev);

mutex_lock(&group->mutex);
- list_add_tail(&device->list, &group->devices);
if (group->domain)
ret = __iommu_attach_device(group->domain, dev);
mutex_unlock(&group->mutex);
if (ret)
goto err_put_group;

+ device->added = true;
+
/* Notify any listeners about change to group. */
blocking_notifier_call_chain(&group->notifier,
IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
@@ -746,16 +762,16 @@ int iommu_group_add_device(struct iommu_group *group, struct device *dev)
return 0;

err_put_group:
- mutex_lock(&group->mutex);
- list_del(&device->list);
- mutex_unlock(&group->mutex);
- dev->iommu_group = NULL;
kobject_put(group->devices_kobj);
err_free_name:
kfree(device->name);
err_remove_link:
sysfs_remove_link(&dev->kobj, "iommu_group");
err_free_device:
+ mutex_lock(&group->mutex);
+ list_del(&device->list);
+ mutex_unlock(&group->mutex);
+ dev->iommu_group = NULL;
kfree(device);
dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
return ret;
@@ -1339,6 +1355,34 @@ struct iommu_group *fsl_mc_device_group(struct device *dev)
return group;
}

+static int alloc_iommu_group(struct device *dev, void *data)
+{
+ const struct iommu_ops *ops = dev->bus->iommu_ops;
+ struct group_device *device;
+ struct iommu_group *group;
+
+ if (!ops || WARN_ON(dev->iommu_group))
+ return -EINVAL;
+
+ device = kzalloc(sizeof(*device), GFP_KERNEL);
+ if (!device)
+ return -ENOMEM;
+
+ group = ops->device_group(dev);
+ if (WARN_ON_ONCE(IS_ERR_OR_NULL(group))) {
+ kfree(device);
+ return -EINVAL;
+ }
+
+ device->dev = dev;
+ dev->iommu_group = group;
+ mutex_lock(&group->mutex);
+ list_add_tail(&device->list, &group->devices);
+ mutex_unlock(&group->mutex);
+
+ return 0;
+}
+
/**
* iommu_group_get_for_dev - Find or create the IOMMU group for a device
* @dev: target device
@@ -1351,23 +1395,15 @@ struct iommu_group *fsl_mc_device_group(struct device *dev)
*/
struct iommu_group *iommu_group_get_for_dev(struct device *dev)
{
- const struct iommu_ops *ops = dev->bus->iommu_ops;
- struct iommu_group *group;
+ struct iommu_group *group = dev->iommu_group;
int ret;

- group = iommu_group_get(dev);
- if (group)
- return group;
-
- if (!ops)
- return ERR_PTR(-EINVAL);
-
- group = ops->device_group(dev);
- if (WARN_ON_ONCE(group == NULL))
- return ERR_PTR(-EINVAL);
-
- if (IS_ERR(group))
- return group;
+ if (!group) {
+ ret = alloc_iommu_group(dev, NULL);
+ if (ret)
+ return ERR_PTR(ret);
+ group = dev->iommu_group;
+ }

/*
* Try to allocate a default domain - needs support from the
@@ -1501,6 +1537,10 @@ static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
if (err)
goto out_free;

+ err = bus_for_each_dev(bus, NULL, NULL, alloc_iommu_group);
+ if (err)
+ goto out_err;
+
err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
if (err)
goto out_err;
--
2.17.1
\
 
 \ /
  Last update: 2020-01-01 06:29    [W:0.118 / U:6.696 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site