Skip to contentSkip to conversation

xen/passthrough: Support a single iommu_domain per xen domain per SMMU

PATCH Superseded Robbie VanVossen 5 participants 10 messages Mar 23, 2015 – Mar 30, 2015

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/passthrough: Support a single iommu_domain per xen domain per SMMU

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

---
Changed since v1:
* Fixed coding style for comments
* Move increment/decrement outside of attach/detach functions
* Expanded xen_domain->lock to protect more of the assign/deassign functions
* Removed iommu_domain add/remove_device functions

---
 xen/drivers/passthrough/arm/smmu.c |   98 +++++++++++++++++++++++++++---------
 1 file changed, 74 insertions(+), 24 deletions(-)
xen/drivers/passthrough/arm/smmu.c +74 -24
index a7a7da9..1a3de00 100644
@@ -223,6 +223,7 @@ struct iommu_domain
 	/* Runtime SMMU configuration for this iommu_domain */
 	struct arm_smmu_domain		*priv;
 
+	atomic_t ref;
 	/* Used to link iommu_domain contexts for a same domain.
 	 * There is at least one per-SMMU to used by the domain.
 	 * */
@@ -2569,7 +2570,9 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
 {
 	struct iommu_domain *domain;
 	struct arm_smmu_xen_domain *xen_domain;
-	int ret;
+	struct arm_smmu_device *smmu;
+	int ret = 0;
+	int existing_ctxt_fnd = 0;
 
 	xen_domain = domain_hvm_iommu(d)->arch.priv;
 
@@ -2585,36 +2588,77 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
 			return ret;
 	}
 
+	spin_lock(&xen_domain->lock);
+
 	/*
-	 * TODO: Share the context bank (i.e iommu_domain) when the device is
-	 * under the same SMMU as another device assigned to this domain.
-	 * Would it useful for PCI
+	 * Check to see if a context bank (iommu_domain) already exists for
+	 * this xen domain under the same SMMU
 	 */
-	domain = xzalloc(struct iommu_domain);
-	if (!domain)
-		return -ENOMEM;
+	if (!list_empty(&xen_domain->contexts)) {
+		smmu = find_smmu_for_device(dev);
+		if (!smmu) {
+			dev_err(dev, "cannot find SMMU\n");
+			ret = -ENXIO;
+			goto out;
+		}
 
-	ret = arm_smmu_domain_init(domain);
-	if (ret)
-		goto err_dom_init;
+		/*
+		 * Loop through the &xen_domain->contexts to locate a context
+		 * assigned to this SMMU
+		 */
+		list_for_each_entry(domain, &xen_domain->contexts, list) {
+			if(domain->priv->smmu == smmu)
+			{
+				/*
+				 * We have found a context already associated with the
+				 * same xen domain and SMMU
+				 */
+				ret = arm_smmu_attach_dev(domain, dev);
+				if (ret) {
+					dev_err(dev,
+						"cannot attach device to already existing iommu_domain\n");
+					goto out;
+				}
+				atomic_inc(&domain->ref);
+
+				existing_ctxt_fnd = 1;
+				break;
+			}
+		}
+	}
 
-	domain->priv->cfg.domain = d;
+	if(!existing_ctxt_fnd){
 
-	ret = arm_smmu_attach_dev(domain, dev);
-	if (ret)
-		goto err_attach_dev;
+		domain = xzalloc(struct iommu_domain);
+		if (!domain) {
+			ret = -ENOMEM;
+			goto out;
+		}
 
-	spin_lock(&xen_domain->lock);
-	/* Chain the new context to the domain */
-	list_add(&domain->list, &xen_domain->contexts);
-	spin_unlock(&xen_domain->lock);
+		ret = arm_smmu_domain_init(domain);
+		if (ret)
+			goto err_dom_init;
 
-	return 0;
+		domain->priv->cfg.domain = d;
+
+		ret = arm_smmu_attach_dev(domain, dev);
+		if (ret)
+			goto err_attach_dev;
+		atomic_inc(&domain->ref);
+
+		/* Chain the new context to the domain */
+		list_add(&domain->list, &xen_domain->contexts);
+		
+	}
+
+	goto out;
 
 err_attach_dev:
 	arm_smmu_domain_destroy(domain);
 err_dom_init:
 	xfree(domain);
+out:
+	spin_unlock(&xen_domain->lock);
 
 	return ret;
 }
@@ -2631,14 +2675,20 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
 		return -ESRCH;
 	}
 
+	spin_lock(&xen_domain->lock);
+
 	arm_smmu_detach_dev(domain, dev);
+	atomic_dec(&domain->ref);
 
-	spin_lock(&xen_domain->lock);
-	list_del(&domain->list);
-	spin_unlock(&xen_domain->lock);
+	if (domain->ref.counter == 0)
+	{
+		list_del(&domain->list);
 
-	arm_smmu_domain_destroy(domain);
-	xfree(domain);
+		arm_smmu_domain_destroy(domain);
+		xfree(domain);
+	}
+
+	spin_unlock(&xen_domain->lock);
 
 	return 0;
 }
Signature · 1 line
1.7.9.5
#1 lore raw

Hello Robert,

Quoted 46 lines · Robbie VanVossen

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

---
Changed since v1:
* Fixed coding style for comments
* Move increment/decrement outside of attach/detach functions
* Expanded xen_domain->lock to protect more of the assign/deassign functions
* Removed iommu_domain add/remove_device functions

---
  xen/drivers/passthrough/arm/smmu.c |   98 +++++++++++++++++++++++++++---------
  1 file changed, 74 insertions(+), 24 deletions(-)
xen/drivers/passthrough/arm/smmu.c +1 -0
index a7a7da9..1a3de00 100644
@@ -223,6 +223,7 @@ struct iommu_domain
  	/* Runtime SMMU configuration for this iommu_domain */
  	struct arm_smmu_domain		*priv;
 
+	atomic_t ref;
  	/* Used to link iommu_domain contexts for a same domain.
  	 * There is at least one per-SMMU to used by the domain.
  	 * */

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2575

Quoted 7 lines
xen/drivers/passthrough/arm/smmu.c +3 -1
@@ -2569,7 +2570,9 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
  {
  	struct iommu_domain *domain;
  	struct arm_smmu_xen_domain *xen_domain;
-	int ret;
+	struct arm_smmu_device *smmu;
+	int ret = 0;
+	int existing_ctxt_fnd = 0;

You can use a bool_t here.

Quoted 27 lines
xen/drivers/passthrough/arm/smmu.c +11 -6
  	xen_domain = domain_hvm_iommu(d)->arch.priv;
 
@@ -2585,36 +2588,77 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
  			return ret;
  	}
 
+	spin_lock(&xen_domain->lock);
+
  	/*
-	 * TODO: Share the context bank (i.e iommu_domain) when the device is
-	 * under the same SMMU as another device assigned to this domain.
-	 * Would it useful for PCI
+	 * Check to see if a context bank (iommu_domain) already exists for
+	 * this xen domain under the same SMMU
  	 */
-	domain = xzalloc(struct iommu_domain);
-	if (!domain)
-		return -ENOMEM;
+	if (!list_empty(&xen_domain->contexts)) {
+		smmu = find_smmu_for_device(dev);
+		if (!smmu) {
+			dev_err(dev, "cannot find SMMU\n");
+			ret = -ENXIO;
+			goto out;
+		}
 

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2611

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +7 -3
-	ret = arm_smmu_domain_init(domain);
-	if (ret)
-		goto err_dom_init;
+		/*
+		 * Loop through the &xen_domain->contexts to locate a context
+		 * assigned to this SMMU
+		 */
+		list_for_each_entry(domain, &xen_domain->contexts, list) {
+			if(domain->priv->smmu == smmu)
+			{

Coding style. This should be
if (domain->priv->smmu == smmu) {

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2619

Quoted 8 lines
xen/drivers/passthrough/arm/smmu.c +8 -0
+				/*
+				 * We have found a context already associated with the
+				 * same xen domain and SMMU
+				 */
+				ret = arm_smmu_attach_dev(domain, dev);
+				if (ret) {
+					dev_err(dev,
+						"cannot attach device to already existing iommu_domain\n");

We don't print an error message for new domain, I don't think this one is useful.

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2622

Quoted 3 lines
xen/drivers/passthrough/arm/smmu.c +3 -0
+					goto out;
+				}
+				atomic_inc(&domain->ref);

Introducing an helper to get the iommu_domain would simply the workflow.

That would give smth like:

domain = arm_smmu_get_domain(d, dev);
if (!domain)
{
allocate domain
Add to list dev
}

arm_smmu_attach_dev(domain, dev);
atomic_inc(&domain->ref);

out:
if (failed && atomic == 0)
destroy iommu_domain.

The destroy iommu_domain could be shared with the one in deassign_dev.

Quoted 55 lines
xen/drivers/passthrough/arm/smmu.c +30 -9
+
+				existing_ctxt_fnd = 1;
+				break;
+			}
+		}
+	}
 
-	domain->priv->cfg.domain = d;
+	if(!existing_ctxt_fnd){
 
-	ret = arm_smmu_attach_dev(domain, dev);
-	if (ret)
-		goto err_attach_dev;
+		domain = xzalloc(struct iommu_domain);
+		if (!domain) {
+			ret = -ENOMEM;
+			goto out;
+		}
 
-	spin_lock(&xen_domain->lock);
-	/* Chain the new context to the domain */
-	list_add(&domain->list, &xen_domain->contexts);
-	spin_unlock(&xen_domain->lock);
+		ret = arm_smmu_domain_init(domain);
+		if (ret)
+			goto err_dom_init;
 
-	return 0;
+		domain->priv->cfg.domain = d;
+
+		ret = arm_smmu_attach_dev(domain, dev);
+		if (ret)
+			goto err_attach_dev;
+		atomic_inc(&domain->ref);
+
+		/* Chain the new context to the domain */
+		list_add(&domain->list, &xen_domain->contexts);
+		
+	}
+
+	goto out;
 
  err_attach_dev:
  	arm_smmu_domain_destroy(domain);
  err_dom_init:
  	xfree(domain);
+out:
+	spin_unlock(&xen_domain->lock);
 
  	return ret;
  }
@@ -2631,14 +2675,20 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
  		return -ESRCH;
  	}
 

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_deassign_dev(struct domain *d, struct device *dev) · line 2684

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +5 -3
+	spin_lock(&xen_domain->lock);
+
  	arm_smmu_detach_dev(domain, dev);
+	atomic_dec(&domain->ref);
 
-	spin_lock(&xen_domain->lock);
-	list_del(&domain->list);
-	spin_unlock(&xen_domain->lock);
+	if (domain->ref.counter == 0)
+	{

Coding style:

if (...) {

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_deassign_dev(struct domain *d, struct device *dev) · line 2694

Quoted 13 lines
xen/drivers/passthrough/arm/smmu.c +6 -2
+		list_del(&domain->list);
 
-	arm_smmu_domain_destroy(domain);
-	xfree(domain);
+		arm_smmu_domain_destroy(domain);
+		xfree(domain);
+	}
+
+	spin_unlock(&xen_domain->lock);
 
  	return 0;
  }

Regards,

Signature · 1 line
Julien Grall
#2 lore raw

Hello Julien,

Quoted 14 lines · Julien Grall

Hello Robert,

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2575

Quoted 8 lines · Robbie VanVossen
xen/drivers/passthrough/arm/smmu.c +3 -1
@@ -2569,7 +2570,9 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
  {
  	struct iommu_domain *domain;
  	struct arm_smmu_xen_domain *xen_domain;
-	int ret;
+	struct arm_smmu_device *smmu;
+	int ret = 0;
+	int existing_ctxt_fnd = 0;

You can use a bool_t here.

Ok.

Quoted 27 lines
Quoted 27 lines
xen/drivers/passthrough/arm/smmu.c +11 -6
  	xen_domain = domain_hvm_iommu(d)->arch.priv;
 
@@ -2585,36 +2588,77 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
  			return ret;
  	}
 
+	spin_lock(&xen_domain->lock);
+
  	/*
-	 * TODO: Share the context bank (i.e iommu_domain) when the device is
-	 * under the same SMMU as another device assigned to this domain.
-	 * Would it useful for PCI
+	 * Check to see if a context bank (iommu_domain) already exists for
+	 * this xen domain under the same SMMU
  	 */
-	domain = xzalloc(struct iommu_domain);
-	if (!domain)
-		return -ENOMEM;
+	if (!list_empty(&xen_domain->contexts)) {
+		smmu = find_smmu_for_device(dev);
+		if (!smmu) {
+			dev_err(dev, "cannot find SMMU\n");
+			ret = -ENXIO;
+			goto out;
+		}
 
Quoted 14 lines

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2611

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +7 -3
-	ret = arm_smmu_domain_init(domain);
-	if (ret)
-		goto err_dom_init;
+		/*
+		 * Loop through the &xen_domain->contexts to locate a context
+		 * assigned to this SMMU
+		 */
+		list_for_each_entry(domain, &xen_domain->contexts, list) {
+			if(domain->priv->smmu == smmu)
+			{

Coding style. This should be
if (domain->priv->smmu == smmu) {

Will fix.

Quoted 12 lines

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2619

Quoted 8 lines
xen/drivers/passthrough/arm/smmu.c +8 -0
+				/*
+				 * We have found a context already associated with the
+				 * same xen domain and SMMU
+				 */
+				ret = arm_smmu_attach_dev(domain, dev);
+				if (ret) {
+					dev_err(dev,
+						"cannot attach device to already existing iommu_domain\n");

We don't print an error message for new domain, I don't think this one is useful.

Will remove dev_err.

Quoted 15 lines

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_assign_dev(struct domain *d, u8 devfn, · line 2622

Quoted 3 lines
xen/drivers/passthrough/arm/smmu.c +3 -0
+					goto out;
+				}
+				atomic_inc(&domain->ref);

Introducing an helper to get the iommu_domain would simply the workflow.

That would give smth like:

domain = arm_smmu_get_domain(d, dev);
if (!domain)
{
allocate domain
Add to list dev
}

Quoted 9 lines

arm_smmu_attach_dev(domain, dev);
atomic_inc(&domain->ref);

out:

    if (failed && atomic == 0)
      destroy iommu_domain.

The destroy iommu_domain could be shared with the one in deassign_dev.

But don't we need to handle the failure of attach_dev differently based on which fails? If the attach_dev fails on a new iommu_domain, then the iommu_domain should be destroyed. If it fails on a preexisting iommu_domain that is already attached to another device, then you don't want to destroy the iommu_domain.

Quoted 68 lines
Quoted 55 lines
xen/drivers/passthrough/arm/smmu.c +30 -9
+
+				existing_ctxt_fnd = 1;
+				break;
+			}
+		}
+	}
 
-	domain->priv->cfg.domain = d;
+	if(!existing_ctxt_fnd){
 
-	ret = arm_smmu_attach_dev(domain, dev);
-	if (ret)
-		goto err_attach_dev;
+		domain = xzalloc(struct iommu_domain);
+		if (!domain) {
+			ret = -ENOMEM;
+			goto out;
+		}
 
-	spin_lock(&xen_domain->lock);
-	/* Chain the new context to the domain */
-	list_add(&domain->list, &xen_domain->contexts);
-	spin_unlock(&xen_domain->lock);
+		ret = arm_smmu_domain_init(domain);
+		if (ret)
+			goto err_dom_init;
 
-	return 0;
+		domain->priv->cfg.domain = d;
+
+		ret = arm_smmu_attach_dev(domain, dev);
+		if (ret)
+			goto err_attach_dev;
+		atomic_inc(&domain->ref);
+
+		/* Chain the new context to the domain */
+		list_add(&domain->list, &xen_domain->contexts);
+		
+	}
+
+	goto out;
 
  err_attach_dev:
  	arm_smmu_domain_destroy(domain);
  err_dom_init:
  	xfree(domain);
+out:
+	spin_unlock(&xen_domain->lock);
 
  	return ret;
  }
@@ -2631,14 +2675,20 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
  		return -ESRCH;
  	}
 

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_deassign_dev(struct domain *d, struct device *dev) · line 2684

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +5 -3
+	spin_lock(&xen_domain->lock);
+
  	arm_smmu_detach_dev(domain, dev);
+	atomic_dec(&domain->ref);
 
-	spin_lock(&xen_domain->lock);
-	list_del(&domain->list);
-	spin_unlock(&xen_domain->lock);
+	if (domain->ref.counter == 0)
+	{

Coding style:

Quoted 2 lines

if (...) {

Will Fix

Thanks,
Robbie VanVossen

#3 lore raw
Quoted 1 line · Robert VanVossen

Hello Julien,

Hello Robert,

Quoted 5 lines · Julien Grall

But don't we need to handle the failure of attach_dev differently based on which fails? If the attach_dev fails on a new iommu_domain, then the iommu_domain should be destroyed. If it fails on a preexisting iommu_domain that is already attached to another device, then you don't want to destroy the iommu_domain.

It should be covered by

if (failed && atomic == 0)
destroy iommu_domain.

If you prefer to keep the 2 possible case separate, I would still like to have a separate helper to get the domain.

Regards,

Signature · 1 line
Julien Grall
#4 lore raw
In reply to Robbie VanVossen
Quoted 14 lines · Robbie VanVossen

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

Quoted 7 lines

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

Hi,
Are you adding a PCI passthrough support to Xen ?. I am in process of sending smmu driver patches based on juliens latest code.

Quoted 156 lines

---
Changed since v1:
* Fixed coding style for comments
* Move increment/decrement outside of attach/detach functions
* Expanded xen_domain->lock to protect more of the assign/deassign functions
* Removed iommu_domain add/remove_device functions

---
  xen/drivers/passthrough/arm/smmu.c |   98 +++++++++++++++++++++++++++---------
  1 file changed, 74 insertions(+), 24 deletions(-)
xen/drivers/passthrough/arm/smmu.c +74 -24
index a7a7da9..1a3de00 100644
@@ -223,6 +223,7 @@ struct iommu_domain
  	/* Runtime SMMU configuration for this iommu_domain */
  	struct arm_smmu_domain		*priv;
 
+	atomic_t ref;
  	/* Used to link iommu_domain contexts for a same domain.
  	 * There is at least one per-SMMU to used by the domain.
  	 * */
@@ -2569,7 +2570,9 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
  {
  	struct iommu_domain *domain;
  	struct arm_smmu_xen_domain *xen_domain;
-	int ret;
+	struct arm_smmu_device *smmu;
+	int ret = 0;
+	int existing_ctxt_fnd = 0;
 
  	xen_domain = domain_hvm_iommu(d)->arch.priv;
 
@@ -2585,36 +2588,77 @@ static int arm_smmu_assign_dev(struct domain *d, u8 devfn,
  			return ret;
  	}
 
+	spin_lock(&xen_domain->lock);
+
  	/*
-	 * TODO: Share the context bank (i.e iommu_domain) when the device is
-	 * under the same SMMU as another device assigned to this domain.
-	 * Would it useful for PCI
+	 * Check to see if a context bank (iommu_domain) already exists for
+	 * this xen domain under the same SMMU
  	 */
-	domain = xzalloc(struct iommu_domain);
-	if (!domain)
-		return -ENOMEM;
+	if (!list_empty(&xen_domain->contexts)) {
+		smmu = find_smmu_for_device(dev);
+		if (!smmu) {
+			dev_err(dev, "cannot find SMMU\n");
+			ret = -ENXIO;
+			goto out;
+		}
 
-	ret = arm_smmu_domain_init(domain);
-	if (ret)
-		goto err_dom_init;
+		/*
+		 * Loop through the &xen_domain->contexts to locate a context
+		 * assigned to this SMMU
+		 */
+		list_for_each_entry(domain, &xen_domain->contexts, list) {
+			if(domain->priv->smmu == smmu)
+			{
+				/*
+				 * We have found a context already associated with the
+				 * same xen domain and SMMU
+				 */
+				ret = arm_smmu_attach_dev(domain, dev);
+				if (ret) {
+					dev_err(dev,
+						"cannot attach device to already existing iommu_domain\n");
+					goto out;
+				}
+				atomic_inc(&domain->ref);
+
+				existing_ctxt_fnd = 1;
+				break;
+			}
+		}
+	}
 
-	domain->priv->cfg.domain = d;
+	if(!existing_ctxt_fnd){
 
-	ret = arm_smmu_attach_dev(domain, dev);
-	if (ret)
-		goto err_attach_dev;
+		domain = xzalloc(struct iommu_domain);
+		if (!domain) {
+			ret = -ENOMEM;
+			goto out;
+		}
 
-	spin_lock(&xen_domain->lock);
-	/* Chain the new context to the domain */
-	list_add(&domain->list, &xen_domain->contexts);
-	spin_unlock(&xen_domain->lock);
+		ret = arm_smmu_domain_init(domain);
+		if (ret)
+			goto err_dom_init;
 
-	return 0;
+		domain->priv->cfg.domain = d;
+
+		ret = arm_smmu_attach_dev(domain, dev);
+		if (ret)
+			goto err_attach_dev;
+		atomic_inc(&domain->ref);
+
+		/* Chain the new context to the domain */
+		list_add(&domain->list, &xen_domain->contexts);
+		
+	}
+
+	goto out;
 
  err_attach_dev:
  	arm_smmu_domain_destroy(domain);
  err_dom_init:
  	xfree(domain);
+out:
+	spin_unlock(&xen_domain->lock);
 
  	return ret;
  }
@@ -2631,14 +2675,20 @@ static int arm_smmu_deassign_dev(struct domain *d, struct device *dev)
  		return -ESRCH;
  	}
 
+	spin_lock(&xen_domain->lock);
+
  	arm_smmu_detach_dev(domain, dev);
+	atomic_dec(&domain->ref);
 
-	spin_lock(&xen_domain->lock);
-	list_del(&domain->list);
-	spin_unlock(&xen_domain->lock);
+	if (domain->ref.counter == 0)
+	{
+		list_del(&domain->list);
 
-	arm_smmu_domain_destroy(domain);
-	xfree(domain);
+		arm_smmu_domain_destroy(domain);
+		xfree(domain);
+	}
+
+	spin_unlock(&xen_domain->lock);
 
  	return 0;
  }
#5 lore raw
Quoted 22 lines · Manish Jaggi
Quoted 20 lines · Robbie VanVossen

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

Quoted 5 lines
Quoted 1 line

Hi,
Are you adding a PCI passthrough support to Xen ?. I am in process of sending smmu driver patches based on juliens latest code.

Nope, I am just working on what this patch describes

Thanks,
Robbie VanVossen
DornerWorks

#6 lore raw
Quoted 23 lines · Robert VanVossen
Quoted 21 lines · Manish Jaggi
Quoted 20 lines · Robbie VanVossen

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Hi,
Are you adding a PCI passthrough support to Xen ?. I am in process of sending smmu driver patches based on juliens latest code.

Nope, I am just working on what this patch describes

Your patch is doing similar thing what am working on. As per the Xen 4.6 release I took smmu for pci-passthorugh. I didnt send smmu patches so far as there are other dependency patches I need to send first like pci_host_bridge register hypercall patch.

Julien/Ian could you please hold the merge of this patch.

Quoted 4 lines

Thanks,
Robbie VanVossen
DornerWorks

#7 lore raw
Quoted 29 lines · Manish Jaggi
Quoted 28 lines · Robert VanVossen
Quoted 26 lines · Manish Jaggi
Quoted 14 lines · Robbie VanVossen

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

Quoted 7 lines

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

Hi,
Are you adding a PCI passthrough support to Xen ?. I am in process of sending smmu driver patches based on juliens latest code.

Quoted 7 lines
Quoted 1 line

Nope, I am just working on what this patch describes

Your patch is doing similar thing what am working on. As per the Xen 4.6 release I took smmu for pci-passthorugh. I didnt send smmu patches so far as there are other dependency patches I need to send first like pci_host_bridge register hypercall patch.

Julien/Ian could you please hold the merge of this patch.

Sorry Manish, but the Xen community works on a first come first served
basis: if/when this patch is considered in good condition and ready to be merged, is likely to be merged.
Unless you have a specific concern about the code of course.

#8 lore raw
Quoted 35 lines · Stefano Stabellini
Quoted 34 lines · Manish Jaggi
Quoted 22 lines · Robert VanVossen
Quoted 21 lines · Manish Jaggi
Quoted 20 lines · Robbie VanVossen

If multiple devices are being passed through to the same domain and they share a single SMMU, then they only require a single iommu_domain.

In arm_smmu_assign_dev, before a new iommu_domain is created, the xen_domain->contexts is checked for any iommu_domains that are already assigned to device that uses the same SMMU as the current device. If one is found, attach the device to that iommu_domain. If a new one isn't found, create a new iommu_domain just like before.

The arm_smmu_deassign_dev function assumes that there is a single device per iommu_domain. This meant that when the first device was deassigned, the iommu_domain was freed and when another device was deassigned a crash occured in xen.

To fix this, a reference counter was added to the iommu_domain struct. When an arm_smmu_xen_device references an iommu_domain, the iommu_domains ref is incremented. When that reference is removed, the iommu_domains ref is decremented. The iommu_domain will only be freed when the ref is 0.

Quoted 6 lines
Quoted 5 lines
Quoted 1 line

Hi,
Are you adding a PCI passthrough support to Xen ?. I am in process of sending smmu driver patches based on juliens latest code.

Nope, I am just working on what this patch describes

Your patch is doing similar thing what am working on. As per the Xen 4.6 release I took smmu for pci-passthorugh. I didnt send smmu patches so far as there are other dependency patches I need to send first like pci_host_bridge register hypercall patch.

Quoted 5 lines
Quoted 1 line

Julien/Ian could you please hold the merge of this patch.

Sorry Manish, but the Xen community works on a first come first served
basis: if/when this patch is considered in good condition and ready to be merged, is likely to be merged.
Unless you have a specific concern about the code of course.

Ok. I will incorporate robert's patch in my patch set.

Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Hello,

Quoted 6 lines · Manish Jaggi
Quoted 5 lines
Quoted 1 line

Julien/Ian could you please hold the merge of this patch.

Sorry Manish, but the Xen community works on a first come first served
basis: if/when this patch is considered in good condition and ready to be merged, is likely to be merged.
Unless you have a specific concern about the code of course.

Ok. I will incorporate robert's patch in my patch set.

Why we should wait you to sent your PCI SMMU changes? It will likely take another couple of months before your series will be merged.

Robert's patch is a standalone patch which is useful for non-PCI case (for instance 2 networks cards under the same SMMU).

He sent the v4 last week and the patch is ready to go in Xen tree.

Regards,

Signature · 1 line
Julien Grall