iommu/amd: Do not call sleep while holding spinlock
authorSuravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Mon, 14 Mar 2022 02:43:21 +0000 (21:43 -0500)
committerJoerg Roedel <jroedel@suse.de>
Wed, 4 May 2022 08:32:11 +0000 (10:32 +0200)
Smatch static checker warns:
drivers/iommu/amd/iommu_v2.c:133 free_device_state()
warn: sleeping in atomic context

Fixes by storing the list of struct device_state in a temporary
list, and then free the memory after releasing the spinlock.

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Fixes: 9f968fc70d85 ("iommu/amd: Improve amd_iommu_v2_exit()")
Signed-off-by: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Link: https://lore.kernel.org/r/20220314024321.37411-1-suravee.suthikulpanit@amd.com
Signed-off-by: Joerg Roedel <jroedel@suse.de>
drivers/iommu/amd/iommu_v2.c

index e56b137ceabd142dd36217f5062fe64933e317a7..afb3efd565b78ed9f84ff5a5b1ffc9904b5c2f48 100644 (file)
@@ -956,6 +956,7 @@ static void __exit amd_iommu_v2_exit(void)
 {
        struct device_state *dev_state, *next;
        unsigned long flags;
+       LIST_HEAD(freelist);
 
        if (!amd_iommu_v2_supported())
                return;
@@ -975,11 +976,20 @@ static void __exit amd_iommu_v2_exit(void)
 
                put_device_state(dev_state);
                list_del(&dev_state->list);
-               free_device_state(dev_state);
+               list_add_tail(&dev_state->list, &freelist);
        }
 
        spin_unlock_irqrestore(&state_lock, flags);
 
+       /*
+        * Since free_device_state waits on the count to be zero,
+        * we need to free dev_state outside the spinlock.
+        */
+       list_for_each_entry_safe(dev_state, next, &freelist, list) {
+               list_del(&dev_state->list);
+               free_device_state(dev_state);
+       }
+
        destroy_workqueue(iommu_wq);
 }