iommu/amd: Lock the device table by DeviceID
Subsystems (2)
Vasant Hegde
maintainercover iommu/amd: Lock the device table by DeviceID
The device table is indexed by DeviceID, but the lock that protects a write lives in iommu_dev_data. A DTE can therefore be updated with no lock when no dev_data exists. set_dte_irq_entry() skips the lock when search_dev_data() returns NULL, and clone_alias() allocates a dev_data for an alias DeviceID only so that it has a lock to take. That alias object is never attached to a domain and never freed.
Move dte_lock to the PCI segment so every writer takes the same lock. Address each slot by DeviceID instead of by a dev_data pointer, and drop the alias allocation. A DTE update is not a fast path, so one lock per segment is enough.
Full series including other cleanup is available at:
https://github.com/AMDESE/linux-iommu/tree/iommu_code_audit_7.4_v1
Vasant Hegde (3):
iommu/amd: Serialize device-table writes with one per-segment lock iommu/amd: Identify a DTE entry by its DeviceID iommu/amd: Drop the iommu_dev_data allocation for alias devices
drivers/iommu/amd/amd_iommu_types.h | 2 +- drivers/iommu/amd/init.c | 1 + drivers/iommu/amd/iommu.c | 76 ++++++++++------------------- 3 files changed, 29 insertions(+), 50 deletions(-)
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 1/3 iommu/amd: Serialize device-table writes with one per-segment lock
The DTE update lock (dte_lock) currently lives in iommu_dev_data structure. Because of this, dte[devid] can only be serialized when a corresponding dev_data object exists. This leads to inconsistent locking:
- The interrupt remapping path (set_dte_irq_entry()) skips the lock entirely when search_dev_data() returns NULL, leaving the DTE write unprotected.
- An alias ID has no device object of its own, so clone_alias() has to create a dev_data for alias devices purely to have a lock to hold before updating the DTE.
Move dte_lock to per PCI segment (amd_iommu_pci_seg). This guarantees that every code path holds the lock before updating DTE. A subsequent patch builds on this by removing the need to create dev_data objects solely for alias devices.
A DTE update is not a fast-path operation, so using a single per-PCI- segment lock instead of a per-device lock does not affect performance.
--- drivers/iommu/amd/amd_iommu_types.h | 2 +- drivers/iommu/amd/init.c | 1 + drivers/iommu/amd/iommu.c | 22 ++++++++++------------ 3 files changed, 12 insertions(+), 13 deletions(-)
drivers/iommu/amd/amd_iommu_types.h +1 -1
@@ -566,6 +566,7 @@ struct amd_iommu_pci_seg {/* List of all available dev_data structures */ struct llist_head dev_data_list; +spinlock_t dte_lock;/* Device-table update lock */ /* PCI segment number */ u16 id;
@@ -806,7 +807,6 @@ struct devid_map {struct iommu_dev_data {/*Protect against attach/detach races */ struct mutex mutex; -spinlock_t dte_lock;/* DTE lock for 256-bit access */ struct list_head list;/* For domain->dev_list */ struct llist_node dev_data_list;/* For global dev_data_list */
drivers/iommu/amd/init.c +1 -0
@@ -1743,6 +1743,7 @@ static struct amd_iommu_pci_seg *__init alloc_pci_segment(u16 id, pci_seg->id = id;init_llist_head (&pci_seg->dev_data_list); +spin_lock_init (&pci_seg->dte_lock);INIT_LIST_HEAD (&pci_seg->unity_map);list_add_tail (&pci_seg->list, &amd_iommu_pci_seg_list);
drivers/iommu/amd/iommu.c +10 -12
@@ -120,7 +120,7 @@ static void write_dte_upper128(struct dev_table_entry *ptr, struct dev_table_ent/* * Preserve DTE_DATA2_INTR_MASK. This needs to be * done here since it requires to be inside -* spin_lock(&dev_data->dte_lock) context. +* pci_seg->dte_lock context. */ new->data[2 ] &= ~DTE_DATA2_INTR_MASK ; new->data[2 ] |= old.data[2 ] &DTE_DATA2_INTR_MASK ;
@@ -151,7 +151,7 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_dastruct dev_table_entry *dev_table =get_dev_table (iommu);struct dev_table_entry *ptr = &dev_table[dev_data->devid]; -spin_lock_irqsave (&dev_data->dte_lock, flags); +spin_lock_irqsave (&iommu->pci_seg->dte_lock, flags);if (!(ptr->data[0 ] &DTE_FLAG_V )) {/* Existing DTE is not valid. */
@@ -207,7 +207,7 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_dawrite_dte_lower128 (ptr, new); } -spin_unlock_irqrestore (&dev_data->dte_lock, flags); +spin_unlock_irqrestore (&iommu->pci_seg->dte_lock, flags); } void amd_iommu_update_dte(struct amd_iommu *iommu,
@@ -229,10 +229,10 @@ static void get_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_data, ptr = &dev_table[dev_data->devid]; -spin_lock_irqsave (&dev_data->dte_lock, flags); +spin_lock_irqsave (&iommu->pci_seg->dte_lock, flags); dte->data128[0 ] = ptr->data128[0 ]; dte->data128[1 ] = ptr->data128[1 ]; -spin_unlock_irqrestore (&dev_data->dte_lock, flags); +spin_unlock_irqrestore (&iommu->pci_seg->dte_lock, flags); }static inline bool pdom_is_v2_pgtbl_mode (struct protection_domain *pdom)
@@ -382,7 +382,6 @@ static struct iommu_dev_data *alloc_dev_data(struct amd_iommu *iommu, u16 devid)return NULL ;mutex_init (&dev_data->mutex); -spin_lock_init (&dev_data->dte_lock); dev_data->devid = devid;ratelimit_default_init (&dev_data->rs);
@@ -3285,13 +3284,13 @@ static int amd_iommu_set_dirty_tracking(struct iommu_domain *domain, }list_for_each_entry (dev_data, &pdomain->dev_list, list) { -spin_lock (&dev_data->dte_lock); iommu =get_amd_iommu_from_dev_data (dev_data); +spin_lock (&iommu->pci_seg->dte_lock); dte = &get_dev_table (iommu)[dev_data->devid]; new = dte->data[0 ]; new = (enable ? new |DTE_FLAG_HAD : new & ~DTE_FLAG_HAD ); dte->data[0 ] = new; -spin_unlock (&dev_data->dte_lock); +spin_unlock (&iommu->pci_seg->dte_lock);/* Flush device DTE */ device_flush_dte (dev_data);
@@ -3517,11 +3516,11 @@ static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid, struct irq_remap_table *table) {u64 new; +unsigned long flags;struct dev_table_entry *dte = &get_dev_table (iommu)[devid];struct iommu_dev_data *dev_data =search_dev_data (iommu, devid); -if (dev_data) -spin_lock (&dev_data->dte_lock); +spin_lock_irqsave (&iommu->pci_seg->dte_lock, flags); new =READ_ONCE (dte->data[2 ]); new &= ~DTE_IRQ_PHYS_ADDR_MASK ;
@@ -3531,8 +3530,7 @@ static void set_dte_irq_entry(struct amd_iommu *iommu, u16 devid, new |=DTE_IRQ_REMAP_ENABLE ;WRITE_ONCE (dte->data[2 ], new); -if (dev_data) -spin_unlock (&dev_data->dte_lock); +spin_unlock_irqrestore (&iommu->pci_seg->dte_lock, flags); }static struct irq_remap_table *get_irq_table (struct amd_iommu *iommu,u16 devid)
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 2/3 iommu/amd: Identify a DTE entry by its DeviceID
In reply to Vasant HegdeThe DTE accessors update_dte256() and get_dte256() take a struct iommu_dev_data pointer purely to read ->devid when indexing into the device table. Now that the DTE lock lives in the per-PCI-segment structure rather than in iommu_dev_data, nothing in these helpers needs the dev_data object itself.
A DTE entry is fundamentally identified by its DeviceID, so pass the DeviceID directly instead of an iommu_dev_data. This removes the artificial requirement for callers to construct or look up a dev_data object just to program or read a DTE slot. ex, dump_dte_entry() no longer has to call find_dev_data() before dumping an entry.
No functional change intended.
--- drivers/iommu/amd/iommu.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-)
drivers/iommu/amd/iommu.c +15 -16
@@ -144,12 +144,12 @@ static void write_dte_lower128(struct dev_table_entry *ptr, struct dev_table_ent* This function is used only by code, which updates DMA translation part of the DTE. * So, only consider control bits related to DMA when updating the entry. */ -static void update_dte256 (struct amd_iommu *iommu,struct iommu_dev_data *dev_data, +static void update_dte256 (struct amd_iommu *iommu,u16 devid,struct dev_table_entry *new) {unsigned long flags;struct dev_table_entry *dev_table =get_dev_table (iommu); -struct dev_table_entry *ptr = &dev_table[dev_data->devid]; +struct dev_table_entry *ptr = &dev_table[devid];spin_lock_irqsave (&iommu->pci_seg->dte_lock, flags);
@@ -157,12 +157,12 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da/* Existing DTE is not valid. */ write_dte_upper128 (ptr, new);write_dte_lower128 (ptr, new); -iommu_flush_dte_sync (iommu, dev_data->devid); +iommu_flush_dte_sync (iommu, devid); }else if (!(new->data[0 ] &DTE_FLAG_V )) {/* Existing DTE is valid. New DTE is not valid. */ write_dte_lower128 (ptr, new);write_dte_upper128 (ptr, new); -iommu_flush_dte_sync (iommu, dev_data->devid); +iommu_flush_dte_sync (iommu, devid); }else if (!FIELD_GET (DTE_FLAG_GV , ptr->data[0 ])) {/* * Both DTEs are valid.
@@ -170,7 +170,7 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da*/ write_dte_upper128 (ptr, new);write_dte_lower128 (ptr, new); -iommu_flush_dte_sync (iommu, dev_data->devid); +iommu_flush_dte_sync (iommu, devid); }else if (!FIELD_GET (DTE_FLAG_GV , new->data[0 ])) {/* * Both DTEs are valid.
@@ -179,7 +179,7 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da*/ write_dte_lower128 (ptr, new);write_dte_upper128 (ptr, new); -iommu_flush_dte_sync (iommu, dev_data->devid); +iommu_flush_dte_sync (iommu, devid); }else if (FIELD_GET (DTE_GPT_LEVEL_MASK , ptr->data[2 ]) !=FIELD_GET (DTE_GPT_LEVEL_MASK , new->data[2 ])) {/*
@@ -192,12 +192,12 @@ static void update_dte256(struct amd_iommu *iommu, struct iommu_dev_data *dev_da/* First disable DTE */ write_dte_lower128 (ptr, &clear); -iommu_flush_dte_sync (iommu, dev_data->devid); +iommu_flush_dte_sync (iommu, devid);/* Then update DTE */ write_dte_upper128 (ptr, new);write_dte_lower128 (ptr, new); -iommu_flush_dte_sync (iommu, dev_data->devid); +iommu_flush_dte_sync (iommu, devid); }else {/* * Both DTEs are valid and have guest page table,
@@ -214,20 +214,20 @@ void amd_iommu_update_dte(struct amd_iommu *iommu,struct iommu_dev_data *dev_data, struct dev_table_entry *new) { -update_dte256 (iommu ,dev_data ,new ); + update_dte256(iommu, dev_data->devid, new);clone_aliases (iommu, dev_data->dev);device_flush_dte (dev_data);iommu_completion_wait (iommu); } -static void get_dte256 (struct amd_iommu *iommu,struct iommu_dev_data *dev_data, -struct dev_table_entry *dte) +static void get_dte256 (struct amd_iommu *iommu,u16 devid, +struct dev_table_entry *dte) {unsigned long flags;struct dev_table_entry *ptr;struct dev_table_entry *dev_table =get_dev_table (iommu); - ptr = &dev_table[dev_data->devid]; + ptr = &dev_table[devid];spin_lock_irqsave (&iommu->pci_seg->dte_lock, flags); dte->data128[0 ] = ptr->data128[0 ];
@@ -430,7 +430,7 @@ static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data) ret = -EINVAL ; goto out; } -get_dte256 (iommu, dev_data, &new); +get_dte256 (iommu, dev_data->devid, &new);/* Setup alias */ alias_data =find_dev_data (iommu, alias);
@@ -439,7 +439,7 @@ static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data) ret = -EINVAL ; goto out; } -update_dte256 (iommu, alias_data, &new); +update_dte256 (iommu, alias_data->devid, &new);amd_iommu_set_rlookup_table (iommu, alias); out:
@@ -744,9 +744,8 @@ static void dump_dte_entry(struct amd_iommu *iommu, u16 devid) {int i;struct dev_table_entry dte; -struct iommu_dev_data *dev_data =find_dev_data (iommu, devid); -get_dte256 (iommu, dev_data, &dte); +get_dte256 (iommu, devid, &dte);for (i =0 ; i <4 ; ++i)pr_err ("DTE[%d]: %016llx\n" , i, dte.data[i]);
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 3/3 iommu/amd: Drop the iommu_dev_data allocation for alias devices
In reply to Vasant HegdeA DMA alias is simply another DeviceID for a device the driver already tracks. The alias dev_data object is never attached to a domain and is never freed, so it serves no purpose beyond carrying a deviceID.
Now that the DTE helpers address a slot directly by DeviceID, get_dte256()/update_dte256() can read the source entry and write the alias entry using their DeviceIDs alone. Drop the dev_data lookup and the alias allocation.
No functional change intended.
--- drivers/iommu/amd/iommu.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-)
drivers/iommu/amd/iommu.c +4 -23
@@ -81,7 +81,6 @@ static void amd_iommu_change_top(struct pt_iommu *iommu_table,static void iommu_flush_dte_sync (struct amd_iommu *iommu,u16 devid); -static struct iommu_dev_data *find_dev_data (struct amd_iommu *iommu,u16 devid);static bool amd_iommu_enforce_cache_coherency (struct iommu_domain *domain);static int amd_iommu_set_dirty_tracking (struct iommu_domain *domain,bool enable);
@@ -411,10 +410,8 @@ static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data) {struct dev_table_entry new;struct amd_iommu *iommu; -struct iommu_dev_data *dev_data, *alias_data;struct pci_dev *pdev = data;u16 devid =pci_dev_id (pdev); -int ret =0 ;if (devid == alias)return 0 ;
@@ -423,27 +420,11 @@ static int clone_alias(struct pci_dev *pdev_origin, u16 alias, void *data)if (!iommu)return 0 ; -/* Copy the data from pdev */ - dev_data =dev_iommu_priv_get (&pdev->dev); -if (!dev_data) { -pr_err ("%s : Failed to get dev_data for 0x%x\n" , __func__, devid); - ret = -EINVAL ; - goto out; - } -get_dte256 (iommu, dev_data->devid, &new); - -/* Setup alias */ - alias_data =find_dev_data (iommu, alias); -if (!alias_data) { -pr_err ("%s : Failed to get alias dev_data for 0x%x\n" , __func__, alias); - ret = -EINVAL ; - goto out; - } -update_dte256 (iommu, alias_data->devid, &new); - +get_dte256 (iommu, devid, &new); +update_dte256 (iommu, alias, &new);amd_iommu_set_rlookup_table (iommu, alias); -out: -return ret; + +return 0 ; }static void clone_aliases (struct amd_iommu *iommu,struct device *dev)
Signature · 1 line
2.31.1