Skip to contentSkip to conversation

Add PASID support to SMMUv3 unmanaged domains

PATCH Superseded Michael Shavit 5 participants 18 messages May 10, 2023 – May 23, 2023

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Add PASID support to SMMUv3 unmanaged domains

Hi all,

This patch series refactors the arm-smmu-v3 driver and implements the set_dev_pasid functionality for DMA and UNMANAGED iommu domains. As part of this effort, we also refactor the arm-smmu-v3 driver such that each iommu domain represent a single address space. In particular, stage 1 domains hold a single ContextDescriptor instead of the entire STE entry.

The refactor is arguably valuable independently from the set_dev_pasid feature since an iommu_domain is conceptually closer to a single address space than an entire STE. In addition this unlocks some nice clean-up of the arm SVA implementation which today piggybacks SVA domains on the "primary" domain.

This patch series makes some changes to SVA and PCIe, but I haven't tested those features. The cd table allocations could also be further optimized for masters that don't support multiple context. However, the SMMUv3 Nested translation patch series has me worried about this change. At a glance, it seems like this refactor conflicts with its proposed uAPI. Is this refactor no longer an appropriate clean-up or path forward for set_dev_pasid support? Or could a uAPI that only exposes a single CD instead of the entire STE be an appropriate fit for the nesting use cases?

Thanks,
Michael Shavit

Michael Shavit (5):
iommu/arm-smmu-v3: Move cdtable to arm_smmu_master iommu/arm-smmu-v3: Add has_stage1 field iommu/arm-smmu-v3: Simplify arm_smmu_enable_ats iommu/arm-smmu-v3: Keep track of attached ssids iommu/arm-smmu-v3: Implement set_dev_pasid

 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |  46 +-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 432 ++++++++++++------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  44 +-
 3 files changed, 344 insertions(+), 178 deletions(-)
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
#1 lore raw

PATCH 1/5 iommu/arm-smmu-v3: Move cdtable to arm_smmu_master

In the arm-smmu-v3 driver, stage 1 domains represent the entire STE. Iommu domains are conceptually closer to a single address space (with some exceptions), which are better represented by a single ContextDescriptor for stage 1 domains. With this change, the CD table is now owned by the arm_smmu_master instead of the attached domain. Each stage 1 domain now represents a single context.

For now, SVA still piggy-backs on the primary arm_smmu_domain for other state management (such as the devices list).

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   |  35 +++-
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 185 ++++++++----------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   |  29 +--
 3 files changed, 127 insertions(+), 122 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +26 -9
index a5a63b1c947eb..a721461b355c6 100644
@@ -35,6 +35,9 @@ struct arm_smmu_bond {
 #define sva_to_bond(handle) \
 	container_of(handle, struct arm_smmu_bond, sva)
 
+#define cd_to_domain(cd) \
+	container_of(cd, struct arm_smmu_domain, cd)
+
 static DEFINE_MUTEX(sva_lock);
 
 /*
@@ -45,10 +48,12 @@ static struct arm_smmu_ctx_desc *
 arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
 {
 	int ret;
+	unsigned long flags;
 	u32 new_asid;
 	struct arm_smmu_ctx_desc *cd;
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_domain *smmu_domain;
+	struct arm_smmu_master *master;
 
 	cd = xa_load(&arm_smmu_asid_xa, asid);
 	if (!cd)
@@ -62,7 +67,7 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
 		return cd;
 	}
 
-	smmu_domain = container_of(cd, struct arm_smmu_domain, s1_cfg.cd);
+	smmu_domain = cd_to_domain(cd);
 	smmu = smmu_domain->smmu;
 
 	ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd,
@@ -80,7 +85,11 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
 	 * be some overlap between use of both ASIDs, until we invalidate the
 	 * TLB.
 	 */
-	arm_smmu_write_ctx_desc(smmu_domain, 0, cd);
+	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
+	list_for_each_entry(master, &smmu_domain->devices, domain_head) {
+		arm_smmu_write_ctx_desc(master, 0, cd);
+	}
+	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
 
 	/* Invalidate TLB entries previously associated with that context */
 	arm_smmu_tlb_inv_asid(smmu, asid);
@@ -211,6 +220,8 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 {
 	struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn);
 	struct arm_smmu_domain *smmu_domain = smmu_mn->domain;
+	struct arm_smmu_master *master;
+	unsigned long flags;
 
 	mutex_lock(&sva_lock);
 	if (smmu_mn->cleared) {
@@ -222,7 +233,11 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 	 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events,
 	 * but disable translation.
 	 */
-	arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, &quiet_cd);
+	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
+	list_for_each_entry(master, &smmu_domain->devices, domain_head) {
+		arm_smmu_write_ctx_desc(master, mm->pasid, &quiet_cd);
+	}
+	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
 
 	arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid);
 	arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0);
@@ -244,7 +259,8 @@ static const struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = {
 
 /* Allocate or get existing MMU notifier for this {domain, mm} pair */
 static struct arm_smmu_mmu_notifier *
-arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain,
+arm_smmu_mmu_notifier_get(struct arm_smmu_master *master,
+			  struct arm_smmu_domain *smmu_domain,
 			  struct mm_struct *mm)
 {
 	int ret;
@@ -279,7 +295,7 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain,
 		goto err_free_cd;
 	}
 
-	ret = arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, cd);
+	ret = arm_smmu_write_ctx_desc(master, mm->pasid, cd);
 	if (ret)
 		goto err_put_notifier;
 
@@ -294,7 +310,8 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain,
 	return ERR_PTR(ret);
 }
 
-static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn)
+static void arm_smmu_mmu_notifier_put(struct arm_smmu_master *master,
+				      struct arm_smmu_mmu_notifier *smmu_mn)
 {
 	struct mm_struct *mm = smmu_mn->mn.mm;
 	struct arm_smmu_ctx_desc *cd = smmu_mn->cd;
@@ -304,7 +321,7 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn)
 		return;
 
 	list_del(&smmu_mn->list);
-	arm_smmu_write_ctx_desc(smmu_domain, mm->pasid, NULL);
+	arm_smmu_write_ctx_desc(master, mm->pasid, NULL);
 
 	/*
 	 * If we went through clear(), we've already invalidated, and no
@@ -348,7 +365,7 @@ __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm)
 	bond->sva.dev = dev;
 	refcount_set(&bond->refs, 1);
 
-	bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm);
+	bond->smmu_mn = arm_smmu_mmu_notifier_get(master, smmu_domain, mm);
 	if (IS_ERR(bond->smmu_mn)) {
 		ret = PTR_ERR(bond->smmu_mn);
 		goto err_free_bond;
@@ -527,7 +544,7 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain,
 
 	if (!WARN_ON(!bond) && refcount_dec_and_test(&bond->refs)) {
 		list_del(&bond->list);
-		arm_smmu_mmu_notifier_put(bond->smmu_mn);
+		arm_smmu_mmu_notifier_put(master, bond->smmu_mn);
 		kfree(bond);
 	}
 	mutex_unlock(&sva_lock);
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +86 -99

415 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +15 -14
index b574c58a34876..0b87c74bdf46e 100644
@@ -593,7 +593,6 @@ struct arm_smmu_ctx_desc_cfg {
 
 struct arm_smmu_s1_cfg {
 	struct arm_smmu_ctx_desc_cfg	cdcfg;
-	struct arm_smmu_ctx_desc	cd;
 	u8				s1fmt;
 	u8				s1cdmax;
 };
@@ -689,6 +688,8 @@ struct arm_smmu_master {
 	struct arm_smmu_domain		*domain;
 	struct list_head		domain_head;
 	struct arm_smmu_stream		*streams;
+	struct arm_smmu_s1_cfg		s1_cfg;
+	struct arm_smmu_s2_cfg		*s2_cfg;
 	unsigned int			num_streams;
 	bool				ats_enabled;
 	bool				stall_enabled;
@@ -707,25 +708,25 @@ enum arm_smmu_domain_stage {
 };
 
 struct arm_smmu_domain {
-	struct arm_smmu_device		*smmu;
-	struct mutex			init_mutex; /* Protects smmu pointer */
+	struct arm_smmu_device			*smmu;
+	 /* Protects smmu pointer */
+	struct mutex				init_mutex;
 
-	struct io_pgtable_ops		*pgtbl_ops;
-	bool				stall_enabled;
-	atomic_t			nr_ats_masters;
+	struct io_pgtable_ops			*pgtbl_ops;
+	atomic_t				nr_ats_masters;
 
-	enum arm_smmu_domain_stage	stage;
+	enum arm_smmu_domain_stage		stage;
 	union {
-		struct arm_smmu_s1_cfg	s1_cfg;
-		struct arm_smmu_s2_cfg	s2_cfg;
+		struct arm_smmu_ctx_desc	cd;
+		struct arm_smmu_s2_cfg		s2_cfg;
 	};
 
-	struct iommu_domain		domain;
+	struct iommu_domain			domain;
 
-	struct list_head		devices;
-	spinlock_t			devices_lock;
+	struct list_head			devices;
+	spinlock_t				devices_lock;
 
-	struct list_head		mmu_notifiers;
+	struct list_head			mmu_notifiers;
 };
 
 static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom)
@@ -737,7 +738,7 @@ extern struct xarray arm_smmu_asid_xa;
 extern struct mutex arm_smmu_asid_lock;
 extern struct arm_smmu_ctx_desc quiet_cd;
 
-int arm_smmu_write_ctx_desc(struct arm_smmu_domain *smmu_domain, int ssid,
+int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid,
 			    struct arm_smmu_ctx_desc *cd);
 void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid);
 void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
#4 lore raw

PATCH 2/5 iommu/arm-smmu-v3: Add has_stage1 field

In reply to Michael Shavit

Inferring the state of the STE based on attached domains becomes tricker when multiple domains can be attached to a master on different PASIDs. The new field allows the smmu driver to directly query the state of the STE (S1 present, S2 present, neither) instead of inferring it from the attached domain.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 22 +++++++--------------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |  1 +
 2 files changed, 8 insertions(+), 15 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +7 -15
index cee3efff3c9fa..7b4399b5ba68b 100644
@@ -980,9 +980,9 @@ static void arm_smmu_sync_cd(struct arm_smmu_master *master,
 	};
 
 	/*
-	 * There's nothing to sync if the STE isn't valid yet.
+	 * There's nothing to sync if stage 1 hasn't been installed yet.
 	 */
-	if (!master->domain)
+	if (!master->has_stage1)
 		return;
 
 	cmds.num = 0;
@@ -1278,20 +1278,11 @@ static void arm_smmu_write_strtab_ent(struct arm_smmu_master *master, u32 sid,
 			.sid	= sid,
 		},
 	};
-	struct iommu_domain *domain = NULL;
-
 	if (master) {
 		smmu = master->smmu;
-		if (master->domain)
-			domain = &master->domain->domain;
-	}
-	if (domain) {
-		if (domain->type != IOMMU_DOMAIN_IDENTITY) {
-			if (master->s2_cfg)
-				s2_cfg = master->s2_cfg;
-			else
-				s1_cfg = &master->s1_cfg;
-		}
+		if (master->has_stage1)
+			s1_cfg = &master->s1_cfg;
+		s2_cfg = master->s2_cfg;
 	}
 
 	if (val & STRTAB_STE_0_V) {
@@ -2367,9 +2358,9 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master)
 	list_del(&master->domain_head);
 	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
 
-	master->domain = NULL;
 	master->ats_enabled = false;
 	master->s2_cfg = NULL;
+	master->has_stage1 = false;
 	/*
 	 * Note that this will end up calling arm_smmu_sync_cd() even though
 	 * we're about to destroy the entire STE anyways. This is ok because
@@ -2426,6 +2417,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 	 * This isn't an issue because the STE hasn't been installed yet.
 	 */
 	if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) {
+		master->has_stage1 = true;
 		ret = arm_smmu_write_ctx_desc(master, 0, &smmu_domain->cd);
 		if (ret)
 			goto out_unlock;
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -0
index 0b87c74bdf46e..d715794572b13 100644
@@ -689,6 +689,7 @@ struct arm_smmu_master {
 	struct list_head		domain_head;
 	struct arm_smmu_stream		*streams;
 	struct arm_smmu_s1_cfg		s1_cfg;
+	bool                            has_stage1;
 	struct arm_smmu_s2_cfg		*s2_cfg;
 	unsigned int			num_streams;
 	bool				ats_enabled;
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
#5 lore raw

PATCH 3/5 iommu/arm-smmu-v3: Simplify arm_smmu_enable_ats

In reply to Michael Shavit

arm_smmu_enable_ats's call to inv_domain would trigger an invalidation for all masters that a domain is attached to everytime it's attached to another ATS-enabled master. It doesn't seem like those invalidations are necessary, and it's easier to reason about arm_smmu_enable_ats if it only issues invalidation commands for the current master.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +1 -1
index 7b4399b5ba68b..47dda287a4736 100644
@@ -2275,7 +2275,7 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master)
 	pdev = to_pci_dev(master->dev);
 
 	atomic_inc(&smmu_domain->nr_ats_masters);
-	arm_smmu_atc_inv_domain(smmu_domain, 0, 0, 0);
+	arm_smmu_atc_inv_master(master);
 	if (pci_enable_ats(pdev, stu))
 		dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu);
 }
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
#6 lore raw

PATCH 4/5 iommu/arm-smmu-v3: Keep track of attached ssids

In reply to Michael Shavit

The arm-smmu-v3 driver keeps track of all masters that a domain is attached so that it can re-write their STEs when the domain's ASID is upated by SVA. This tracking is also used to invalidate ATCs on all masters that a domain is attached.

This change introduces a new data structures to track all the CD entries that a domain is attached to. This change is a pre-requisite to allow domain attachment on non 0 SSIDs.

---
 .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c   | 31 ++++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c   | 90 ++++++++++++-------
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h   | 17 ++--
 3 files changed, 89 insertions(+), 49 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +18 -13
index a721461b355c6..2eb066c0f3f99 100644
@@ -53,7 +53,7 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
 	struct arm_smmu_ctx_desc *cd;
 	struct arm_smmu_device *smmu;
 	struct arm_smmu_domain *smmu_domain;
-	struct arm_smmu_master *master;
+	struct arm_smmu_attached_domain *attached_domain;
 
 	cd = xa_load(&arm_smmu_asid_xa, asid);
 	if (!cd)
@@ -85,11 +85,11 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid)
 	 * be some overlap between use of both ASIDs, until we invalidate the
 	 * TLB.
 	 */
-	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
-	list_for_each_entry(master, &smmu_domain->devices, domain_head) {
-		arm_smmu_write_ctx_desc(master, 0, cd);
+	spin_lock_irqsave(&smmu_domain->attached_domains_lock, flags);
+	list_for_each_entry(attached_domain, &smmu_domain->attached_domains, domain_head) {
+		arm_smmu_write_ctx_desc(attached_domain->master, attached_domain->ssid, cd);
 	}
-	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+	spin_unlock_irqrestore(&smmu_domain->attached_domains_lock, flags);
 
 	/* Invalidate TLB entries previously associated with that context */
 	arm_smmu_tlb_inv_asid(smmu, asid);
@@ -213,14 +213,14 @@ static void arm_smmu_mm_invalidate_range(struct mmu_notifier *mn,
 	if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM))
 		arm_smmu_tlb_inv_range_asid(start, size, smmu_mn->cd->asid,
 					    PAGE_SIZE, false, smmu_domain);
-	arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, start, size);
+	arm_smmu_atc_inv_domain_ssid(smmu_domain, mm->pasid, start, size);
 }
 
 static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 {
 	struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn);
 	struct arm_smmu_domain *smmu_domain = smmu_mn->domain;
-	struct arm_smmu_master *master;
+	struct arm_smmu_attached_domain *attached_domain;
 	unsigned long flags;
 
 	mutex_lock(&sva_lock);
@@ -233,14 +233,19 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm)
 	 * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events,
 	 * but disable translation.
 	 */
-	spin_lock_irqsave(&smmu_domain->devices_lock, flags);
-	list_for_each_entry(master, &smmu_domain->devices, domain_head) {
-		arm_smmu_write_ctx_desc(master, mm->pasid, &quiet_cd);
+	spin_lock_irqsave(&smmu_domain->attached_domains_lock, flags);
+	list_for_each_entry(attached_domain, &smmu_domain->attached_domains, domain_head) {
+		/*
+		 * SVA domains piggyback on the attached_domain with SSID 0.
+		 */
+		if (attached_domain->ssid == 0)
+			arm_smmu_write_ctx_desc(attached_domain->master,
+						mm->pasid, &quiet_cd);
 	}
-	spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+	spin_unlock_irqrestore(&smmu_domain->attached_domains_lock, flags);
 
 	arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid);
-	arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0);
+	arm_smmu_atc_inv_domain_ssid(smmu_domain, mm->pasid, 0, 0);
 
 	smmu_mn->cleared = true;
 	mutex_unlock(&sva_lock);
@@ -329,7 +334,7 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_master *master,
 	 */
 	if (!smmu_mn->cleared) {
 		arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid);
-		arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0);
+		arm_smmu_atc_inv_domain_ssid(smmu_domain, mm->pasid, 0, 0);
 	}
 
 	/* Frees smmu_mn */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +59 -31

198 lines not on this page · raw message

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +12 -5
index d715794572b13..35700534a0b4a 100644
@@ -681,11 +681,18 @@ struct arm_smmu_stream {
 	struct rb_node			node;
 };
 
+struct arm_smmu_attached_domain {
+	struct list_head domain_head;
+	struct arm_smmu_master *master;
+	int ssid;
+	struct arm_smmu_domain *domain;
+};
+
 /* SMMU private data for each master */
 struct arm_smmu_master {
 	struct arm_smmu_device		*smmu;
 	struct device			*dev;
-	struct arm_smmu_domain		*domain;
+	struct arm_smmu_attached_domain	attached_domain;
 	struct list_head		domain_head;
 	struct arm_smmu_stream		*streams;
 	struct arm_smmu_s1_cfg		s1_cfg;
@@ -724,8 +731,8 @@ struct arm_smmu_domain {
 
 	struct iommu_domain			domain;
 
-	struct list_head			devices;
-	spinlock_t				devices_lock;
+	struct list_head			attached_domains;
+	spinlock_t				attached_domains_lock;
 
 	struct list_head			mmu_notifiers;
 };
@@ -746,8 +753,8 @@ void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,
 				 size_t granule, bool leaf,
 				 struct arm_smmu_domain *smmu_domain);
 bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd);
-int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid,
-			    unsigned long iova, size_t size);
+int arm_smmu_atc_inv_domain_ssid(struct arm_smmu_domain *smmu_domain, int ssid,
+				 unsigned long iova, size_t size);
 
 #ifdef CONFIG_ARM_SMMU_V3_SVA
 bool arm_smmu_sva_supported(struct arm_smmu_device *smmu);
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
#12 lore raw

PATCH 5/5 iommu/arm-smmu-v3: Implement set_dev_pasid

In reply to Michael Shavit

This change enables the use of the iommu_attach_dev_pasid API for UNMANAGED domains. The primary use-case is to allow in-kernel users of the iommu API to manage domains with PASID. This change also allows for future support of pasid in the DMA api.

---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 165 +++++++++++++++++---
 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h |   1 +
 2 files changed, 147 insertions(+), 19 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +146 -19
index 81f49a86c1266..468a7a30ffe7b 100644
@@ -2353,6 +2353,11 @@ static int arm_smmu_enable_pasid(struct arm_smmu_master *master)
 	return 0;
 }
 
+static bool arm_smmu_master_has_pasid_domains(struct arm_smmu_master *master)
+{
+	return master->nr_attached_pasid_domains > 0;
+}
+
 static void arm_smmu_disable_pasid(struct arm_smmu_master *master)
 {
 	struct pci_dev *pdev;
@@ -2396,6 +2401,33 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master)
 	arm_smmu_install_ste_for_dev(master);
 }
 
+/*
+ * Once attached for the first time, a domain can no longer be attached to any
+ * master with a distinct upstream SMMU.
+ */
+static int arm_smmu_prepare_domain_for_smmu(struct device *dev,
+					    struct arm_smmu_device *smmu,
+					    struct arm_smmu_domain *smmu_domain)
+{
+	int ret = 0;
+
+	mutex_lock(&smmu_domain->init_mutex);
+	if (!smmu_domain->smmu) {
+		smmu_domain->smmu = smmu;
+		ret = arm_smmu_domain_finalise(&smmu_domain->domain);
+		if (ret) {
+			smmu_domain->smmu = NULL;
+			goto out_unlock;
+		}
+	} else if (smmu_domain->smmu != smmu) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+out_unlock:
+	mutex_unlock(&smmu_domain->init_mutex);
+	return ret;
+}
+
 static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 {
 	int ret = 0;
@@ -2411,6 +2443,10 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 	master = dev_iommu_priv_get(dev);
 	smmu = master->smmu;
 
+	ret = arm_smmu_prepare_domain_for_smmu(dev, smmu, smmu_domain);
+	if (ret)
+		return ret;
+
 	/*
 	 * Checking that SVA is disabled ensures that this device isn't bound to
 	 * any mm, and can be safely detached from its old domain. Bonds cannot
@@ -2421,22 +2457,18 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 		return -EBUSY;
 	}
 
-	arm_smmu_detach_dev(master);
-
-	mutex_lock(&smmu_domain->init_mutex);
-
-	if (!smmu_domain->smmu) {
-		smmu_domain->smmu = smmu;
-		ret = arm_smmu_domain_finalise(domain);
-		if (ret) {
-			smmu_domain->smmu = NULL;
-			goto out_unlock;
-		}
-	} else if (smmu_domain->smmu != smmu) {
-		ret = -EINVAL;
-		goto out_unlock;
+	/*
+	 * Attaching a bypass or stage 2 domain would break any domains attached
+	 * with pasid. Attaching an S1 domain should be feasible but requires
+	 * more complicated logic to handle.
+	 */
+	if (arm_smmu_master_has_pasid_domains(master)) {
+		dev_err(dev, "cannot attach - domain attached with pasid\n");
+		return -EBUSY;
 	}
 
+	arm_smmu_detach_dev(master);
+
 	/*
 	 * Note that this will end up calling arm_smmu_sync_cd() before
 	 * the master has been added to the devices list for this domain.
@@ -2446,7 +2478,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 		master->has_stage1 = true;
 		ret = arm_smmu_write_ctx_desc(master, 0, &smmu_domain->cd);
 		if (ret)
-			goto out_unlock;
+			return ret;
 	} else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 ||
 		   smmu_domain->stage == ARM_SMMU_DOMAIN_NESTED) {
 		master->s2_cfg = &smmu_domain->s2_cfg;
@@ -2473,11 +2505,74 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev)
 
 	arm_smmu_enable_ats(master, smmu_domain);
 
-out_unlock:
-	mutex_unlock(&smmu_domain->init_mutex);
 	return ret;
 }
 
+static int arm_smmu_set_dev_pasid(struct iommu_domain *domain,
+				  struct device *dev, ioasid_t pasid)
+{
+	int ret = 0;
+	unsigned long flags;
+	struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
+	struct arm_smmu_device *smmu;
+	struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain);
+	struct arm_smmu_attached_domain *attached_domain;
+	struct arm_smmu_master *master;
+
+	if (!fwspec)
+		return -ENOENT;
+
+	master = dev_iommu_priv_get(dev);
+	smmu = master->smmu;
+
+	ret = arm_smmu_prepare_domain_for_smmu(dev, smmu, smmu_domain);
+	if (ret)
+		return ret;
+
+	if (pasid == 0) {
+		dev_err(dev, "pasid 0 is reserved for the device's primary domain\n");
+		return -ENODEV;
+	}
+
+	if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) {
+		dev_err(dev, "set_dev_pasid only supports stage 1 domains\n");
+		return -EINVAL;
+	}
+
+	if (!master->has_stage1 || master->s2_cfg)
+		return -EBUSY;
+
+	attached_domain = kzalloc(sizeof(*attached_domain), GFP_KERNEL);
+	if (!attached_domain)
+		return -ENOMEM;
+
+	attached_domain->master = master;
+	attached_domain->domain = smmu_domain;
+	attached_domain->ssid = pasid;
+
+	master->nr_attached_pasid_domains += 1;
+	/*
+	 * arm_smmu_share_asid may update the cd's asid value and write the
+	 * ctx_desc for every attached_domains in the list. There's a potential
+	 * race here regardless of whether we first the write the ctx_desc or
+	 * first insert into the domain's list. Grabbing the asic_lock prevents
+	 * SVA from changing the cd's ASID while the cd is being attached.
+	 */
+	mutex_lock(&arm_smmu_asid_lock);
+	ret = arm_smmu_write_ctx_desc(master, pasid, &smmu_domain->cd);
+	if (ret) {
+		mutex_unlock(&arm_smmu_asid_lock);
+		kfree(attached_domain);
+	}
+
+	spin_lock_irqsave(&smmu_domain->attached_domains_lock, flags);
+	list_add(&attached_domain->domain_head, &smmu_domain->attached_domains);
+	spin_unlock_irqrestore(&smmu_domain->attached_domains_lock, flags);
+	mutex_unlock(&arm_smmu_asid_lock);
+
+	return 0;
+}
+
 static int arm_smmu_map_pages(struct iommu_domain *domain, unsigned long iova,
 			      phys_addr_t paddr, size_t pgsize, size_t pgcount,
 			      int prot, gfp_t gfp, size_t *mapped)
@@ -2723,6 +2818,15 @@ static void arm_smmu_release_device(struct device *dev)
 
 	if (WARN_ON(arm_smmu_master_sva_enabled(master)))
 		iopf_queue_remove_device(master->smmu->evtq.iopf, dev);
+	if (WARN_ON(master->nr_attached_pasid_domains != 0)) {
+		/*
+		 * TODO: Do we need to handle this case?
+		 * This requires a mechanism to obtain all the pasid domains
+		 * that this master is attached to so that we can clean up the
+		 * domain's attached_domain list.
+		 */
+	}
+
 	arm_smmu_detach_dev(master);
 	arm_smmu_free_cd_tables(master);
 	arm_smmu_disable_pasid(master);
@@ -2858,12 +2962,34 @@ static int arm_smmu_def_domain_type(struct device *dev)
 static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid)
 {
 	struct iommu_domain *domain;
+	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
+	struct arm_smmu_domain *smmu_domain;
+	struct arm_smmu_attached_domain *attached_domain;
+	unsigned long flags;
 
-	domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA);
+	if (!master || pasid == 0)
+		return;
+
+	domain = iommu_get_domain_for_dev_pasid(dev, pasid, 0);
 	if (WARN_ON(IS_ERR(domain)) || !domain)
 		return;
+	if (domain->type == IOMMU_DOMAIN_SVA)
+		return arm_smmu_sva_remove_dev_pasid(domain, dev, pasid);
 
-	arm_smmu_sva_remove_dev_pasid(domain, dev, pasid);
+	smmu_domain = to_smmu_domain(domain);
+	mutex_lock(&arm_smmu_asid_lock);
+	spin_lock_irqsave(&smmu_domain->attached_domains_lock, flags);
+	list_for_each_entry(attached_domain, &smmu_domain->attached_domains, domain_head) {
+		if (attached_domain->master != master ||
+		    attached_domain->ssid != pasid)
+			continue;
+		list_del(&attached_domain->domain_head);
+		break;
+	}
+	spin_unlock_irqrestore(&smmu_domain->attached_domains_lock, flags);
+	arm_smmu_write_ctx_desc(master, pasid, NULL);
+	master->nr_attached_pasid_domains -= 1;
+	mutex_unlock(&arm_smmu_asid_lock);
 }
 
 static struct iommu_ops arm_smmu_ops = {
@@ -2883,6 +3009,7 @@ static struct iommu_ops arm_smmu_ops = {
 	.owner			= THIS_MODULE,
 	.default_domain_ops = &(const struct iommu_domain_ops) {
 		.attach_dev		= arm_smmu_attach_dev,
+		.set_dev_pasid		= arm_smmu_set_dev_pasid,
 		.map_pages		= arm_smmu_map_pages,
 		.unmap_pages		= arm_smmu_unmap_pages,
 		.flush_iotlb_all	= arm_smmu_flush_iotlb_all,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +1 -0
index 35700534a0b4a..9ef63ea381f4b 100644
@@ -705,6 +705,7 @@ struct arm_smmu_master {
 	bool				iopf_enabled;
 	struct list_head		bonds;
 	unsigned int			ssid_bits;
+	unsigned int			nr_attached_pasid_domains;
 };
 
 /* SMMU private data for an IOMMU domain */
Signature · 1 line
2.40.1.521.gf1e218fcd8-goog
#13 lore raw
In reply to Michael Shavit
Quoted 7 lines · Michael Shavit

Hi all,

This patch series refactors the arm-smmu-v3 driver and implements the set_dev_pasid functionality for DMA and UNMANAGED iommu domains. As part of this effort, we also refactor the arm-smmu-v3 driver such that each iommu domain represent a single address space. In particular, stage 1 domains hold a single ContextDescriptor instead of the entire STE entry.

I'm not sure what you mean "holds" a ContextDescriptor?

Ideally the iommu_domain should only hold a pointer to some table top. Depending on the domain type this would be a S1 IOPTE table, S2 IOPTE table or a CD table. Plus the non-table domains like IDENTITY and blocked.

Logically when an iommu_domain is attached to a device or a PASID a STE or CD is generated from the iommu_domain's configuration but the iommu_domain doesn't "hold" it

When a kernel-owned CD table is used it should be held someplace else, certianly not in the iommu_domain. Logically as a per-device structure, but maybe with optimizations for sharing.

Quoted 5 lines

The refactor is arguably valuable independently from the set_dev_pasid feature since an iommu_domain is conceptually closer to a single address space than an entire STE. In addition this unlocks some nice clean-up of the arm SVA implementation which today piggybacks SVA domains on the "primary" domain.

I always thought of this as sort of a pre-calculation of the STE, that gets cached in the iommu_domain? Not sure the pre-calculation is that valuable though..

Quoted 3 lines

path forward for set_dev_pasid support? Or could a uAPI that only exposes a single CD instead of the entire STE be an appropriate fit for the nesting use cases?

The uAPI is to create an iommu_domain that holds a CD Table Top located in user memory, it cannot deviate from this. These kinds of iommu_domain's can only be pointed at by STEs.

Again it doesnt really "hold" the STE, but we can compute a STE that points to the SD Table that it does hold.

Other than this, it is good to take this project on, getting PASID support aligned with the new API is something that needs to be done here!

Thanks,
Jason

#2 lore raw

Re: [PATCH v1 1/5] iommu/arm-smmu-v3: Move cdtable to arm_smmu_master

In reply to Michael Shavit
Quoted 6 lines · Michael Shavit

In the arm-smmu-v3 driver, stage 1 domains represent the entire STE. Iommu domains are conceptually closer to a single address space (with some exceptions), which are better represented by a single ContextDescriptor for stage 1 domains. With this change, the CD table is now owned by the arm_smmu_master instead of the attached domain. Each stage 1 domain now represents a single context.

As per my remark on the cover letter this seems a bit confusing description, but the idea that the CD Table for kernel owned PASID list is placed in the 'struct arm_smmu_master' does make sense to me

This patch seems very big, is there any way to break it into smaller steps?

Jason

#7 lore raw

Re: [PATCH v1 4/5] iommu/arm-smmu-v3: Keep track of attached ssids

In reply to Michael Shavit

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c static void arm_smmu_mm_invalidate_range(struct mmu_notifier *mn, · line 217

Quoted 7 lines · Michael Shavit
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +1 -1
@@ -213,14 +213,14 @@ static void arm_smmu_mm_invalidate_range(struct mmu_notifier *mn,
 	if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM))
 		arm_smmu_tlb_inv_range_asid(start, size, smmu_mn->cd->asid,
 					    PAGE_SIZE, false, smmu_domain);
-	arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, start, size);
+	arm_smmu_atc_inv_domain_ssid(smmu_domain, mm->pasid, start,
 	size);

You should be getting rid of mm->pasid in this series as well.

When each domain keeps track of what STE/CD entries that point to it then *ALL* invalidation should iterate over the list of pointing entires and generate the correct invalidation for that pointer.

Eg we learn the PASID from the fact that a CD at PASID xyz is pointing at this domain and generate an invalidation for that PASID.

mm->pasid is logically incorrect in all of this code with our multi-attach model, it was here because this code wasn't tracking at what as pointing at the iommu_domain.

Jason

#11 lore raw

Re: [PATCH v1 4/5] iommu/arm-smmu-v3: Keep track of attached ssids

In reply to Michael Shavit
kernel test robot noticed the following build warnings:

Hi Michael,

kernel test robot noticed the following build warnings:

[auto build test WARNING on v6.4-rc1]
[also build test WARNING on linus/master next-20230510] [cannot apply to joro-iommu/next arm-perf/for-next/perf]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Michael-Shavit/iommu-arm-smmu-v3-Move-cdtable-to-arm_smmu_master/20230511-045514
base:   ac9a78681b921877518763ba0e89202254349d1b
patch link:    https://lore.kernel.org/r/20230510205054.2667898-5-mshavit%40google.com
patch subject: [PATCH v1 4/5] iommu/arm-smmu-v3: Keep track of attached ssids
config: arm64-allyesconfig (https://download.01.org/0day-ci/archive/20230511/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 12.1.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/2172fed7450d1bb8518b86b2b7113a1e42b4d456
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Michael-Shavit/iommu-arm-smmu-v3-Move-cdtable-to-arm_smmu_master/20230511-045514 git checkout 2172fed7450d1bb8518b86b2b7113a1e42b4d456
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash drivers/iommu/arm/arm-smmu-v3/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c:1857:5: warning: no previous prototype for 'arm_smmu_atc_inv_domain' [-Wmissing-prototypes]

1857 | int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,
| ^~~~~~~~~~~~~~~~~~~~~~~

vim +/arm_smmu_atc_inv_domain +1857 drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c

1856

> 1857	int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,

1858 unsigned long iova, size_t size)
1859 {
1860 return arm_smmu_atc_inv_domain_ssid(smmu_domain, 0, iova, size);
1861 }
1862

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#14 lore raw
In reply to Jason Gunthorpe
Quoted 4 lines

Logically when an iommu_domain is attached to a device or a PASID a STE or CD is generated from the iommu_domain's configuration but the iommu_domain doesn't "hold" it

Ah yes, I was using iommu domain and arm_smmu_domain interchangeably here since there's a 1:1 mapping between the two. In the current smmuv3 implementation, arm_smmu_domain holds the s1cfg structure which represents the s1 portion of an STE.

Quoted 3 lines

I always thought of this as sort of a pre-calculation of the STE, that gets cached in the iommu_domain? Not sure the pre-calculation is that valuable though..

I think we can consider the case where an iommu domain is attached to multiple masters as a form of pre-calculation and resource sharing. When attaching an iommu domain that has been attached before, its arm_smmu_domain contains an already finalized STE configuration that can be immediately written to the smmu. I don't think there's anything specific to SVA about this behavior however, SVA will do the same amount of work whether the cd table is owned by some special iommu domain or by the arm_smmu_master (since we require that special iommu domain be attached to the master and disallow detaching it).

Quoted 1 line

Ideally the iommu_domain should only hold a pointer to some table

top. Depending on the domain type this would be a S1 IOPTE table, S2 IOPTE table or a CD table. Plus the non-table domains like IDENTITY and blocked.

Gotcha. So this patch series should be less aggressive, but is probably still workable with the nested domain patch series:
1. For (stage 1) unmanaged/dma and sva domains, arm_smmu_domains should hold a single CD. For the nested domain series, arm_smmu_domain can alternatively hold an entire s1cfg.
2. arm_smmu_master should own an s1cfg (which holds a cdtable) that is used by unmanaged/dma and sva domains attached to this master.
3. arm_smmu_master also holds a pointer to the live s1cfg, which may either points to its owned s1cfg, or the nested domain's s1cfg, or null (bypass or stage2)

Quoted 17 lines
Quoted 3 lines

path forward for set_dev_pasid support? Or could a uAPI that only exposes a single CD instead of the entire STE be an appropriate fit for the nesting use cases?

The uAPI is to create an iommu_domain that holds a CD Table Top located in user memory, it cannot deviate from this. These kinds of iommu_domain's can only be pointed at by STEs.

Again it doesnt really "hold" the STE, but we can compute a STE that points to the SD Table that it does hold.

Other than this, it is good to take this project on, getting PASID support aligned with the new API is something that needs to be done here!

Thanks,
Jason

#15 lore raw
Quoted 9 lines · Michael Shavit
Quoted 4 lines

Logically when an iommu_domain is attached to a device or a PASID a STE or CD is generated from the iommu_domain's configuration but the iommu_domain doesn't "hold" it

Ah yes, I was using iommu domain and arm_smmu_domain interchangeably here since there's a 1:1 mapping between the two. In the current smmuv3 implementation, arm_smmu_domain holds the s1cfg structure which represents the s1 portion of an STE.

I mean to include the arm_smmu_domain too..

Generally this sort of seemed OK in the code, I just wouldn't use the word 'hold' - the iommu_domain may cache a computed STE or CD value but that the actual steering or context tables are held else where

ie you insert an iommu_domain into a steering or context table, it does not 'hold' a table entry.

Quoted 4 lines

specific to SVA about this behavior however, SVA will do the same amount of work whether the cd table is owned by some special iommu domain or by the arm_smmu_master (since we require that special iommu domain be attached to the master and disallow detaching it).

The CD table for SVA definately should not be part of an iommu_domain, moving it to the master seems reasonable.

Quoted 5 lines

Gotcha. So this patch series should be less aggressive, but is probably still workable with the nested domain patch series:
1. For (stage 1) unmanaged/dma and sva domains, arm_smmu_domains should hold a single CD. For the nested domain series, arm_smmu_domain can alternatively hold an entire s1cfg.

These are just pre computed values the can help when inserting the iommu_domain into a steering or CD table

Quoted 2 lines

2. arm_smmu_master should own an s1cfg (which holds a cdtable) that is used by unmanaged/dma and sva domains attached to this master.

The arm_smmu_master's cd table can be inserted into a steering table

Quoted 3 lines

3. arm_smmu_master also holds a pointer to the live s1cfg, which may either points to its owned s1cfg, or the nested domain's s1cfg, or null (bypass or stage2)

The steering table either points to the CD table owned by the arm_smmu_master, a S1 domain held by an iommu_domain, or a S1 & CD table owned by userspace represented by a special nested iommu_domain and its internal parent.

If a kernel owned S2 it attached then the S1 points at the CD table owned by the arm_smmu_master and the CD table points to the S2, same as if there was PASID (IIRC, from memory I don't have the spec here right now)

Think about it in terms of what object owns the table and what other object(s) are inserted into the the table. Nothing "holds" a table entry.

Jason

reviewer
#16 lore raw
Quoted 28 lines · Jason Gunthorpe
Quoted 9 lines · Michael Shavit
Quoted 4 lines

Logically when an iommu_domain is attached to a device or a PASID a STE or CD is generated from the iommu_domain's configuration but the iommu_domain doesn't "hold" it

Ah yes, I was using iommu domain and arm_smmu_domain interchangeably here since there's a 1:1 mapping between the two. In the current smmuv3 implementation, arm_smmu_domain holds the s1cfg structure which represents the s1 portion of an STE.

I mean to include the arm_smmu_domain too..

Generally this sort of seemed OK in the code, I just wouldn't use the word 'hold' - the iommu_domain may cache a computed STE or CD value but that the actual steering or context tables are held else where

ie you insert an iommu_domain into a steering or context table, it does not 'hold' a table entry.

Quoted 4 lines

specific to SVA about this behavior however, SVA will do the same amount of work whether the cd table is owned by some special iommu domain or by the arm_smmu_master (since we require that special iommu domain be attached to the master and disallow detaching it).

The CD table for SVA definately should not be part of an iommu_domain, moving it to the master seems reasonable.

Quoted 8 lines
Quoted 5 lines

Gotcha. So this patch series should be less aggressive, but is probably still workable with the nested domain patch series:
1. For (stage 1) unmanaged/dma and sva domains, arm_smmu_domains should hold a single CD. For the nested domain series, arm_smmu_domain can alternatively hold an entire s1cfg.

These are just pre computed values the can help when inserting the iommu_domain into a steering or CD table

Right, a stage 1 domain still logically owns the *contents* of a corresponding CD, however in this design it can no longer own a physical CD structure, because now every device attached to the same domain must maintain its own distinct copy.

Quoted 4 lines
Quoted 2 lines

2. arm_smmu_master should own an s1cfg (which holds a cdtable) that is used by unmanaged/dma and sva domains attached to this master.

The arm_smmu_master's cd table can be inserted into a steering table

Not sure what you mean there... STE.S1ContextPtr is essentially just a pointer to an array of CD structures (which only contains 1 element when PASIDs aren't enabled), so every master must own its own CD table directly. There is no viable indirection if you want the abstraction to bear any relation to reality.

Quoted 9 lines
Quoted 3 lines

3. arm_smmu_master also holds a pointer to the live s1cfg, which may either points to its owned s1cfg, or the nested domain's s1cfg, or null (bypass or stage2)

The steering table either points to the CD table owned by the arm_smmu_master, a S1 domain held by an iommu_domain, or a S1 & CD table owned by userspace represented by a special nested iommu_domain and its internal parent.

Quoted 4 lines

If a kernel owned S2 it attached then the S1 points at the CD table owned by the arm_smmu_master and the CD table points to the S2, same as if there was PASID (IIRC, from memory I don't have the spec here right now)

Nope, CDs *only* represent stage 1 domains, stage 2 configuration is a property of the STE, i.e. is directly connected to the arm_smmu_master.

Thanks,
Robin.

#8 lore raw

Re: [PATCH v1 4/5] iommu/arm-smmu-v3: Keep track of attached ssids

In reply to Jason Gunthorpe
Quoted 6 lines

You should be getting rid of mm->pasid in this series as well.

When each domain keeps track of what STE/CD entries that point to it then *ALL* invalidation should iterate over the list of pointing entires and generate the correct invalidation for that pointer.

Completely agree. The arm_smmu_atc_inv_domain_ssid function introduced by this patch is a stopgap to decompose this patch from the SVA refactor that's required to stop using ssid in these calls. I also agree that such a refactoring probably belongs in the same patch series. @Jean-Philippe Brucker and others: is there any way I can about testing or at least exercising the SVA flow without physical hardware that supports SVA?

#17 lore raw
In reply to Robin Murphy
Quoted 10 lines · Robin Murphy
Quoted 4 lines
Quoted 2 lines

2. arm_smmu_master should own an s1cfg (which holds a cdtable) that is used by unmanaged/dma and sva domains attached to this master.

The arm_smmu_master's cd table can be inserted into a steering table

Not sure what you mean there... STE.S1ContextPtr is essentially just a pointer to an array of CD structures (which only contains 1 element when PASIDs aren't enabled), so every master must own its own CD table directly. There is no viable indirection if you want the abstraction to bear any relation to reality.

Yes, this is what I mean. Whenever we need a kernel owned CD table it comes from the smmu master and is inserted into the steering table owned by the arm_smmu_device.

"Insert" is just the usual verb we tend to use when talking about these kinds of structures. Ie a PTE is inserted into a page table and points at a page - a page table doesn't hold a PTE owned by the page.

So we have, basically, three kinds of tables, Steering/CD/IOPTE, they are owned by their respective objects arm_smmu_device/arm_smmu_master/arm_smmu_domain

And we insert pointers from Steering -> CD -> IOPTE as appropriate.

The only case a CD table is not in the arm_smmu_master is for nesting, but we can still say that the nesting domain is inserted into the steering table.

Jason

#3 lore raw

Re: [PATCH v1 1/5] iommu/arm-smmu-v3: Move cdtable to arm_smmu_master

In reply to Jason Gunthorpe
Quoted 3 lines · Jason Gunthorpe

As per my remark on the cover letter this seems a bit confusing description, but the idea that the CD Table for kernel owned PASID list is placed in the 'struct arm_smmu_master' does make sense to me

Ack; glad to hear the idea is well received. I'll rephrase commit messages for v2.

Quoted 2 lines

This patch seems very big, is there any way to break it into smaller steps?

I can add a preparatory patch that defines and allocates the arm_smmu_master's cd table before this patch starts writing to that cd table instead of the smmu_domain owned cd table. This doesn't cut down the patch's size by a significant amount but it's a start. I'll try to think about it some more but a lot of the other changes in this patch are pretty tightly coupled.

#9 lore raw

Re: [PATCH v1 4/5] iommu/arm-smmu-v3: Keep track of attached ssids

In reply to Michael Shavit
Quoted 7 lines · Michael Shavit
Quoted 6 lines

You should be getting rid of mm->pasid in this series as well.

When each domain keeps track of what STE/CD entries that point to it then *ALL* invalidation should iterate over the list of pointing entires and generate the correct invalidation for that pointer.

Quoted 7 lines

Completely agree. The arm_smmu_atc_inv_domain_ssid function introduced by this patch is a stopgap to decompose this patch from the SVA refactor that's required to stop using ssid in these calls. I also agree that such a refactoring probably belongs in the same patch series. @Jean-Philippe Brucker and others: is there any way I can about testing or at least exercising the SVA flow without physical hardware that supports SVA?

Yes, there is a model with a simple test device that supports PASID and I/O page faults. It's not completely straightforward to setup and the driver needs to be rewritten from scratch, but it's the best we have at the moment. I'd like to do something equally useful for QEMU, so we can have proper regression tests, but that requires a lot of preliminary work to add PASID+PRI to PCI, virtio and IOMMUs.

You'll need a kernel with the driver and a rootfs with the smmute tool [1]; the RevC model [2] and a boot-wrapper [3].

  $ ${BOOTWRAPPER}/configure --host=aarch64-linux-gnu
     --with-dtb=${KERNEL}/arch/arm64/boot/dts/arm/fvp-base-revc.dts
     --with-kernel-dir=${KERNEL}
     --with-initrd=${BUILDROOT}/images/rootfs.cpio
     --with-cmdline="console=ttyAMA0"
     --enable-psci --enable-gicv3
  $ make	# produces linux-system.axf

Run the model:

  $ FVP_Base_RevC-2xAEMvA 
     -C bp.secure_memory=0
     -C 'pctl.startup=0.*.*.*'
     -C bp.refcounter.non_arch_start_at_default=1
     -C cache_state_modelled=0
     -C bp.vis.disable_visualisation=1
     -C bp.virtio_net.enabled=1
     -C bp.virtio_net.hostbridge.userNetPorts=8022=22
     -C bp.virtio_net.hostbridge.userNetworking=1
     -C pci.pci_smmuv3.mmu.SMMU_IDR0=135100351
     -C pci.pci_smmuv3.mmu.SMMU_IDR3=4116
     -C pci.pci_smmuv3.mmu.SMMU_IDR5=8389749
     -C cluster0.NUM_CORES=4
     -C cluster1.NUM_CORES=4
     -a 'cluster*.cpu*=linux-system.axf'

Then run a job using the tool. The process allocates two buffers and passes their VA to the device (via the kernel driver). The device memcpies one buffer to the other:

   $ smmute -u mmap
   ... Success

With smmu and iommu trace events enabled, a trace should contain smmu_mm_invalidate and dev_fault/dev_page_response events.

It's not entirely representative of SVA flow, where an assignable device interface is mapped into the process and the process launches jobs directly without going through the kernel (that would now use drivers/misc/uacce), but it does exercise IOMMU SVA: sva_bind(), device accessing the process address space with PASID and some IOPFs, which I think is what you're looking for. However this model doesn't have a PCI test device so you won't be able to test ATC invalidations with PASID.

Other useful tests would be enabling lockdep (some intricate locking between IOPF, the driver and mm), killing bound processes (-k), triggering invalid accesses to verify TLB invalidations (-f tlb, I think). There is a lot more to test, like thp and oom, but I don't have those in this branch.

Thanks,
Jean

[1] https://jpbrucker.net/git/linux/log/?h=sva/smmute-revc
[2] https://developer.arm.com/downloads/-/arm-ecosystem-models
[3] https://git.kernel.org/pub/scm/linux/kernel/git/mark/boot-wrapper-aarch64.git/

#10 lore raw

Re: [PATCH v1 4/5] iommu/arm-smmu-v3: Keep track of attached ssids

Oh nice, this is exactly what I was looking for (minus the missing ATC inv, but that's somewhat easier to reason from code)! Thanks for the detailed guide Jean!
I finally got around to trying it out and was able to see the page fault followed by invalidations on this patch-series as it is. This will be super useful to start refactoring SVA for a v2 of this patch.

Quoted 81 lines · Jean-Philippe Brucker
Quoted 7 lines · Michael Shavit
Quoted 6 lines

You should be getting rid of mm->pasid in this series as well.

When each domain keeps track of what STE/CD entries that point to it then *ALL* invalidation should iterate over the list of pointing entires and generate the correct invalidation for that pointer.

Quoted 7 lines

Completely agree. The arm_smmu_atc_inv_domain_ssid function introduced by this patch is a stopgap to decompose this patch from the SVA refactor that's required to stop using ssid in these calls. I also agree that such a refactoring probably belongs in the same patch series. @Jean-Philippe Brucker and others: is there any way I can about testing or at least exercising the SVA flow without physical hardware that supports SVA?

Yes, there is a model with a simple test device that supports PASID and I/O page faults. It's not completely straightforward to setup and the driver needs to be rewritten from scratch, but it's the best we have at the moment. I'd like to do something equally useful for QEMU, so we can have proper regression tests, but that requires a lot of preliminary work to add PASID+PRI to PCI, virtio and IOMMUs.

You'll need a kernel with the driver and a rootfs with the smmute tool [1]; the RevC model [2] and a boot-wrapper [3].

  $ ${BOOTWRAPPER}/configure --host=aarch64-linux-gnu
     --with-dtb=${KERNEL}/arch/arm64/boot/dts/arm/fvp-base-revc.dts
     --with-kernel-dir=${KERNEL}
     --with-initrd=${BUILDROOT}/images/rootfs.cpio
     --with-cmdline="console=ttyAMA0"
     --enable-psci --enable-gicv3
  $ make        # produces linux-system.axf

Run the model:

  $ FVP_Base_RevC-2xAEMvA
     -C bp.secure_memory=0
     -C 'pctl.startup=0.*.*.*'
     -C bp.refcounter.non_arch_start_at_default=1
     -C cache_state_modelled=0
     -C bp.vis.disable_visualisation=1
     -C bp.virtio_net.enabled=1
     -C bp.virtio_net.hostbridge.userNetPorts=8022=22
     -C bp.virtio_net.hostbridge.userNetworking=1
     -C pci.pci_smmuv3.mmu.SMMU_IDR0=135100351
     -C pci.pci_smmuv3.mmu.SMMU_IDR3=4116
     -C pci.pci_smmuv3.mmu.SMMU_IDR5=8389749
     -C cluster0.NUM_CORES=4
     -C cluster1.NUM_CORES=4
     -a 'cluster*.cpu*=linux-system.axf'

Then run a job using the tool. The process allocates two buffers and passes their VA to the device (via the kernel driver). The device memcpies one buffer to the other:

   $ smmute -u mmap
   ... Success

With smmu and iommu trace events enabled, a trace should contain smmu_mm_invalidate and dev_fault/dev_page_response events.

It's not entirely representative of SVA flow, where an assignable device interface is mapped into the process and the process launches jobs directly without going through the kernel (that would now use drivers/misc/uacce), but it does exercise IOMMU SVA: sva_bind(), device accessing the process address space with PASID and some IOPFs, which I think is what you're looking for. However this model doesn't have a PCI test device so you won't be able to test ATC invalidations with PASID.

Other useful tests would be enabling lockdep (some intricate locking between IOPF, the driver and mm), killing bound processes (-k), triggering invalid accesses to verify TLB invalidations (-f tlb, I think). There is a lot more to test, like thp and oom, but I don't have those in this branch.

Thanks,
Jean

[1] https://jpbrucker.net/git/linux/log/?h=sva/smmute-revc
[2] https://developer.arm.com/downloads/-/arm-ecosystem-models
[3] https://git.kernel.org/pub/scm/linux/kernel/git/mark/boot-wrapper-aarch64.git/