Skip to contentSkip to conversation

iommu/amd: Do not call sleep while holding spinlock

PATCH Applied Suravee Suthikulpanit 2 participants 2 messages Mar 14, 2022 – Apr 28, 2022

Subsystems (2)
lore
Share & tools
lore

Suravee Suthikulpanit

author reviewer
#0 lore raw

PATCH 1/1 iommu/amd: Do not call sleep while holding spinlock

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.

Fixes dc6a709e5123 ("iommu/amd: Improve amd_iommu_v2_exit()")
---
 drivers/iommu/amd/iommu_v2.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu_v2.c +11 -1
index 490da41c3c71..5a6e4f87d875 100644
@@ -947,6 +947,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;
@@ -966,11 +967,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);
 }
 
Signature · 1 line
2.17.1
maintainer Applied
#1 lore raw
Quoted 7 lines · Suravee Suthikulpanit

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.

Quoted 6 lines
Fixes dc6a709e5123 ("iommu/amd: Improve amd_iommu_v2_exit()")
---
 drivers/iommu/amd/iommu_v2.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

Applied, thanks Suravee.