iommu/amd: Various fixes to interrupt handler code path
Subsystems (2)
Vasant Hegde
maintainercover iommu/amd: Various fixes to interrupt handler code path
Two of the handlers race their teardown. amd_iommu_report_page_fault() reads dev_data->domain with no lock, while detach_device() clears and frees that domain under dev_data->mutex. The GA log notifier is loaded twice outside an RCU read-side section, so synchronize_rcu() on unregister does not cover the call.
The I/O page-fault case also returns before the event slot is zeroed, so the entry is decoded again. Print the raw event before decoding it, so fields the decoder does not know about stay visible.
Full series including other cleanup is available at :
https://github.com/AMDESE/linux-iommu/tree/iommu_code_audit_7.4_v1
Vasant Hegde (4):
iommu/amd: Clear the event log entry after reporting an I/O page fault iommu/amd: Hold the device mutex across a page-fault report iommu/amd: Call the GA log notifier inside an RCU read section iommu/amd: Log raw event log
drivers/iommu/amd/iommu.c | 56 ++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 19 deletions(-)
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 1/4 iommu/amd: Clear the event log entry after reporting an I/O page fault
The I/O page fault path returned early after reporting the fault, skipping the code that zeroes the slot. Make sure slot is cleared before returning.
--- drivers/iommu/amd/iommu.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +2 -1
@@ -927,7 +927,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt)if (type ==EVENT_TYPE_IO_FAULT ) {amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); -return ; + goto out; }switch (type) {
@@ -981,6 +981,7 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) event[0], event[1], event[2], event[3]); } +out:/* * To detect the hardware errata 732 we need to clear the * entry back to zero. This issue does not exist on SNP
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 2/4 iommu/amd: Hold the device mutex across a page-fault report
In reply to Vasant Hegdeamd_iommu_report_page_fault() read dev_data->domain outside any lock and then dereferenced it in report_iommu_fault(). detach_device() clears that pointer under dev_data->mutex and the domain is freed only after detach returns. Nothing prevented a concurrent detach from freeing the domain between the read and the report, leading to a potential use-after-free.
The fault runs in the threaded IRQ handler, which can sleep, so the same mutex can cover both the lookup and the report. Hold dev_data->mutex across reading dev_data->domain and calling report_iommu_fault(), so the domain cannot be detached and freed while it is in use.
--- drivers/iommu/amd/iommu.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/iommu/amd/iommu.c +15 -6
@@ -831,8 +831,21 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu,* prevent logging it. */ if (IS_IOMMU_MEM_TRANSACTION (flags)) { +struct protection_domain *domain; +int handled = -ENODEV ; + +mutex_lock (&dev_data->mutex); + domain = dev_data->domain; +if (domain) + handled =report_iommu_fault (&domain->domain, + &pdev->dev, address, +IS_WRITE_REQUEST (flags) ? +IOMMU_FAULT_WRITE : +IOMMU_FAULT_READ ); +mutex_unlock (&dev_data->mutex); +/* Device not attached to domain properly */ -if (dev_data->domain ==NULL ) { +if (!domain) {pr_err_ratelimited ("Event logged [Device not attached to domain properly]\n" ); pr_err_ratelimited(" device=%04x:%02x:%02x.%x domain=0x%04x\n", iommu->pci_seg->id, PCI_BUS_NUM(devid), PCI_SLOT(devid),
@@ -840,11 +853,7 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu, goto out; } -if (!report_iommu_fault (&dev_data->domain->domain, - &pdev->dev, address, -IS_WRITE_REQUEST (flags) ? -IOMMU_FAULT_WRITE : -IOMMU_FAULT_READ )) +if (!handled) goto out; }
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 3/4 iommu/amd: Call the GA log notifier inside an RCU read section
In reply to Vasant HegdeOn unregister, amd_iommu_register_ga_log_notifier() calls synchronize_rcu() so the module owning the notifier can be safely unloaded once in-flight callers finish. But the GA log handler never entered an RCU read side section and loaded the pointer twice (once to test for NULL, once to call it). The grace period therefore did not cover the call: a concurrent unregister could make the second load see NULL, or free the notifier's module while it was still executing.
Mark iommu_ga_log_notifier as __rcu, publish it with rcu_assign_pointer(), and in the handler snapshot it once under rcu_read_lock() with rcu_dereference(), testing and calling that single snapshot. synchronize_rcu() now correctly waits for in-flight calls.
--- drivers/iommu/amd/iommu.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-)
drivers/iommu/amd/iommu.c +17 -12
@@ -1019,11 +1019,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 );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
@@ -1059,17 +1059,22 @@ 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)) !=0 ) +pr_err ("GA log notifier failed.\n" ); + } +rcu_read_unlock ();break ; + }default :break ; }
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 4/4 iommu/amd: Log raw event log
In reply to Vasant HegdeLog the raw event entry unconditionally before processing/decoding the event. This is useful for debugging especially when decoded log doesn't decode all the fields like newly introduced fields.
--- drivers/iommu/amd/iommu.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/iommu/amd/iommu.c +3 -0
@@ -934,6 +934,9 @@ static void iommu_print_event(struct amd_iommu *iommu, void *__evt) goto retry; } +dev_err_ratelimited (dev,"Raw event: evt[0]=0x%08x, evt[1]=0x%08x, evt[2]=0x%08x, evt[3]=0x%08x\n" , + event[0 ], event[1 ], event[2 ], event[3 ]); +if (type ==EVENT_TYPE_IO_FAULT ) {amd_iommu_report_page_fault (iommu, devid, pasid, address, flags); goto out;
Signature · 1 line
2.31.1