diff mbox series

[RFC,v2,1/9] iommu/arm-smmu-v3: group attached devices by smmu

Message ID 20230822185632.RFC.v2.1.Ib87a2696f25414e0fc39cc22dc74e31a4415c2a1@changeid (mailing list archive)
State New, archived
Headers show
Series Install domain onto multiple smmus | expand

Commit Message

Michael Shavit Aug. 22, 2023, 10:56 a.m. UTC
Always insert a new master in the devices_list besides other masters
that belong to the same smmu.
This allows code to batch commands by SMMU when iterating over masters
that a domain is attached to.

Signed-off-by: Michael Shavit <mshavit@google.com>
---

Changes in v2:
- New commit

 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 22 ++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

Comments

Jason Gunthorpe Aug. 22, 2023, 12:49 p.m. UTC | #1
On Tue, Aug 22, 2023 at 06:56:57PM +0800, Michael Shavit wrote:
> Always insert a new master in the devices_list besides other masters
> that belong to the same smmu.
> This allows code to batch commands by SMMU when iterating over masters
> that a domain is attached to.
> 
> Signed-off-by: Michael Shavit <mshavit@google.com>
> ---
> 
> Changes in v2:
> - New commit
> 
>  drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 22 ++++++++++++++++++---
>  1 file changed, 19 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> index f17704c35858d..37b9223c145ba 100644
> --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
> @@ -2382,6 +2382,24 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master)
>  		arm_smmu_write_ctx_desc(master, 0, NULL);
>  }
>  
> +static void arm_smmu_domain_device_list_add(struct arm_smmu_domain *smmu_domain,
> +					   struct arm_smmu_master *master)
> +{
> +	struct arm_smmu_master *iter;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
> +	if (list_empty(&smmu_domain->devices))
> +		list_add(&master->domain_head, &smmu_domain->devices);
> +	else {
> +		list_for_each_entry(iter, &smmu_domain->devices, domain_head)
> +			if (iter->smmu == master->smmu)
> +				break;
> +		list_add(&master->domain_head, &iter->domain_head);
> +	}

IIRC you are not supposed to touch iter after the list_for_each. Like this:

 list_for_each_entry(iter, &smmu_domain->devices, domain_head) {
      if (iter->smmu == master->smmu) {
          list_add(&master->domain_head, iter->domain_head);
 	  goto out;
      }
 }
 list_add(&master->domain_head, &smmu_domain->devices);
out:
 spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);

Jason
diff mbox series

Patch

diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index f17704c35858d..37b9223c145ba 100644
--- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
+++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
@@ -2382,6 +2382,24 @@  static void arm_smmu_detach_dev(struct arm_smmu_master *master)
 		arm_smmu_write_ctx_desc(master, 0, NULL);
 }
 
+static void arm_smmu_domain_device_list_add(struct arm_smmu_domain *smmu_domain,
+					   struct arm_smmu_master *master)
+{
+	struct arm_smmu_master *iter;
+	unsigned long flags;
+
+	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
+	if (list_empty(&smmu_domain->devices))
+		list_add(&master->domain_head, &smmu_domain->devices);
+	else {
+		list_for_each_entry(iter, &smmu_domain->devices, domain_head)
+			if (iter->smmu == master->smmu)
+				break;
+		list_add(&master->domain_head, &iter->domain_head);
+	}
+	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+}
+
 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 {
 	int ret = 0;
@@ -2435,9 +2453,7 @@  static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 	if (smmu_domain->stage != ARM_SMMU_DOMAIN_BYPASS)
 		master->ats_enabled = arm_smmu_ats_supported(master);
 
-	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
-	list_add(&master->domain_head, &smmu_domain->devices);
-	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+	arm_smmu_domain_device_list_add(smmu_domain, master);
 
 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
 		if (!master->cd_table.cdtab) {