Skip to content

Commit 9f968fc

Browse files
ssuthiku-amdjoergroedel
authored andcommitted
iommu/amd: Improve amd_iommu_v2_exit()
During module exit, the current logic loops through all possible 16-bit device ID space to search for existing devices and clean up device state structures. This can be simplified by looping through the device state list. Also, refactor various clean up logic into free_device_state() for better reusability. Signed-off-by: Vasant Hegde <vasant.hegde@amd.com> Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> Link: https://lore.kernel.org/r/20220301085626.87680-6-vasant.hegde@amd.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent c1d5b57 commit 9f968fc

1 file changed

Lines changed: 17 additions & 17 deletions

File tree

drivers/iommu/amd/iommu_v2.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
MODULE_LICENSE("GPL v2");
2525
MODULE_AUTHOR("Joerg Roedel <jroedel@suse.de>");
2626

27-
#define MAX_DEVICES 0x10000
2827
#define PRI_QUEUE_SIZE 512
2928

3029
struct pri_queue {
@@ -124,6 +123,15 @@ static void free_device_state(struct device_state *dev_state)
124123
{
125124
struct iommu_group *group;
126125

126+
/* Get rid of any remaining pasid states */
127+
free_pasid_states(dev_state);
128+
129+
/*
130+
* Wait until the last reference is dropped before freeing
131+
* the device state.
132+
*/
133+
wait_event(dev_state->wq, !atomic_read(&dev_state->count));
134+
127135
/*
128136
* First detach device from domain - No more PRI requests will arrive
129137
* from that device after it is unbound from the IOMMUv2 domain.
@@ -849,15 +857,7 @@ void amd_iommu_free_device(struct pci_dev *pdev)
849857

850858
spin_unlock_irqrestore(&state_lock, flags);
851859

852-
/* Get rid of any remaining pasid states */
853-
free_pasid_states(dev_state);
854-
855860
put_device_state(dev_state);
856-
/*
857-
* Wait until the last reference is dropped before freeing
858-
* the device state.
859-
*/
860-
wait_event(dev_state->wq, !atomic_read(&dev_state->count));
861861
free_device_state(dev_state);
862862
}
863863
EXPORT_SYMBOL(amd_iommu_free_device);
@@ -954,8 +954,8 @@ static int __init amd_iommu_v2_init(void)
954954

955955
static void __exit amd_iommu_v2_exit(void)
956956
{
957-
struct device_state *dev_state;
958-
int i;
957+
struct device_state *dev_state, *next;
958+
unsigned long flags;
959959

960960
if (!amd_iommu_v2_supported())
961961
return;
@@ -968,18 +968,18 @@ static void __exit amd_iommu_v2_exit(void)
968968
* The loop below might call flush_workqueue(), so call
969969
* destroy_workqueue() after it
970970
*/
971-
for (i = 0; i < MAX_DEVICES; ++i) {
972-
dev_state = get_device_state(i);
973-
974-
if (dev_state == NULL)
975-
continue;
971+
spin_lock_irqsave(&state_lock, flags);
976972

973+
list_for_each_entry_safe(dev_state, next, &state_list, list) {
977974
WARN_ON_ONCE(1);
978975

979976
put_device_state(dev_state);
980-
amd_iommu_free_device(dev_state->pdev);
977+
list_del(&dev_state->list);
978+
free_device_state(dev_state);
981979
}
982980

981+
spin_unlock_irqrestore(&state_lock, flags);
982+
983983
destroy_workqueue(iommu_wq);
984984
}
985985

0 commit comments

Comments
 (0)