Skip to contentSkip to conversation

iommu/amd: RCU-protect iommu_ga_log_notifier

PATCH Applied lirongqing 2 participants 2 messages Nov 17 – Nov 23

Subsystems (2)
lore
Share & tools
lore

lirongqing

author
#0 lore raw

PATCH v2 1/1 iommu/amd: RCU-protect iommu_ga_log_notifier

From: Li RongQing <[email protected]>

iommu_poll_ga_log() accesses iommu_ga_log_notifier without an RCU read-side critical section, so synchronize_rcu() in the unregister path does not wait for an in-flight notifier callback.

Use rcu_assign_pointer() when updating the notifier and rcu_dereference() under rcu_read_lock() when invoking it. Load the pointer once so the NULL check and the call observe the same value.

---
Diff with v1:
Changes since v1:
- Replace scoped_guard(rcu) with rcu_read_lock()/rcu_read_unlock(). There is a single call site and no early return, so the scoped guard is unnecessary.
- Keep pr_debug() and pr_err() inside the RCU read-side section. printk() does not sleep, so this is legal, and the notifier pointer is checked only once.

 drivers/iommu/amd/iommu.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
drivers/iommu/amd/iommu.c +16 -12
index 1df0807..bdf2ed0 100644
@@ -1035,11 +1035,11 @@ static void iommu_poll_events(struct amd_iommu *iommu)
 }
 
 #ifdef CONFIG_IRQ_REMAP
-static int (*iommu_ga_log_notifier)(u32);
+static int (__rcu *iommu_ga_log_notifier)(u32 ga_tag);
 
 int amd_iommu_register_ga_log_notifier(int (*notifier)(u32))
 {
-	iommu_ga_log_notifier = notifier;
+	rcu_assign_pointer(iommu_ga_log_notifier, notifier);
 
 	/*
 	 * Ensure all in-flight IRQ handlers run to completion before returning
@@ -1075,17 +1075,21 @@ static void iommu_poll_ga_log(struct amd_iommu *iommu)
 
 		/* Handle GA entry */
 		switch (GA_REQ_TYPE(log_entry)) {
-		case GA_GUEST_NR:
-			if (!iommu_ga_log_notifier)
-				break;
-
-			pr_debug("%s: devid=%#x, ga_tag=%#x\n",
-				 __func__, GA_DEVID(log_entry),
-				 GA_TAG(log_entry));
-
-			if (iommu_ga_log_notifier(GA_TAG(log_entry)) != 0)
-				pr_err("GA log notifier failed.\n");
+		case GA_GUEST_NR: {
+			int (*notifier)(u32 ga_tag);
+
+			rcu_read_lock();
+			notifier = rcu_dereference(iommu_ga_log_notifier);
+			if (notifier) {
+				pr_debug("%s: devid=%#x, ga_tag=%#x\n",
+					 __func__, GA_DEVID(log_entry),
+					 GA_TAG(log_entry));
+				if (notifier(GA_TAG(log_entry)))
+					pr_err("GA log notifier failed.\n");
+			}
+			rcu_read_unlock();
 			break;
+		}
 		default:
 			break;
 		}
Signature · 1 line
2.9.4
maintainer Applied
#1 lore raw
Quoted 9 lines · lirongqing

From: Li RongQing <[email protected]>

iommu_poll_ga_log() accesses iommu_ga_log_notifier without an RCU read-side critical section, so synchronize_rcu() in the unregister path does not wait for an in-flight notifier callback.

Use rcu_assign_pointer() when updating the notifier and rcu_dereference() under rcu_read_lock() when invoking it. Load the pointer once so the NULL check and the call observe the same value.

Applied, thanks!

-Vasant