iommu/amd: Fix GCR3 lifetime and PPR completion
Subsystems (3)
Vasant Hegde
maintainercover iommu/amd: Fix GCR3 lifetime and PPR completion
This series fixes a number of issues in the SVA/PRI path. It does not address all of them: a follow-up series will move PASID/PRI enablement to the probe/release path and fix the remaining PRI issues.
Note that this series also does not fully address the issue during
the mmu_notifier release path, which was discussed in an earlier thread [1]. We are still running some experiments internally, and I intend to address it in v2.
[1] https://lore.kernel.org/linux-iommu/[email protected]/
Summary of changes:
-------------------
SVA teardown and page-request completion have several ordering bugs. The GCR3 table can be freed while PASIDs are still attached, and a PPR response can complete the wrong request or run after its attach handle is gone.
- amd_iommu_complete_ppr() takes the GN bit from the request. Page-table mode does not cover passthrough, which also uses PASID.
- The GCR3 table stays allocated while PASIDs are attached, so the blocked-domain attach during PCI reset does not free the root out from under a later clear. A PASID-capable device allocates the table at probe and frees it at release. A non-PASID v2 domain still allocates it on attach and frees it on detach. The mm release notifier only disables GCR3 entries and leaves the PASID attachment for the core to remove. After reset, a blocking old domain is programmed again as a fresh SVA attach. A failed tracking allocation returns -ENOMEM.
- An aborted multi-page group is completed only on its last page. Overflow discards partial groups whose last page never reached the log. Detach drains the PPR log before the SVA attach handle is freed, and before a device is removed from the IOPF queue. The DTE is cleared first so the IOMMU stops logging new requests, and PCI capabilities are disabled after those requests have been answered.
Full series including other cleanup is available at:
https://github.com/AMDESE/linux-iommu/tree/iommu_code_audit_7.4_v1
Santosh Shukla (1):
iommu/amd: Restore an SVA PASID after PCI reset
Vasant Hegde (11):
iommu/amd: Disable PRI before ATS and PASID
iommu/amd: Derive the PPR completion GN bit from the request iommu/amd: Complete a PPR from a device the IOMMU did not probe iommu/amd: Keep the GCR3 table while PASIDs are attached iommu/amd: Disable GCR3 entries from the SVA mm release notifier iommu/amd: Complete an aborted PPR group only on its last page iommu/amd: Discard partial page requests when the PPR log overflows iommu/amd: Allocate the GCR3 table from the device iommu/amd: Return -ENOMEM when SVA PASID allocation fails iommu/amd: Drain the PPR log before detaching an SVA PASID iommu/amd: Drain the PPR log before removing a device from the IOPF
queue
drivers/iommu/amd/amd_iommu.h | 5 +- drivers/iommu/amd/amd_iommu_types.h | 2 + drivers/iommu/amd/init.c | 1 + drivers/iommu/amd/iommu.c | 187 +++++++++++++--------------- drivers/iommu/amd/pasid.c | 32 +++-- drivers/iommu/amd/ppr.c | 92 ++++++++++++-- 6 files changed, 203 insertions(+), 116 deletions(-)
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 1/12 iommu/amd: Disable PRI before ATS and PASID
pdev_enable_caps() enables the device capabilities in the order PASID, ATS, then PRI, but pdev_disable_caps() tore them down as ATS, PASID, then PRI -- not the reverse of enabling.
PRI sits on top of ATS: page requests are issued for failed ATS translations, and with PASID enabled both the requests and (when pri_tlp is set) the responses carry a PASID prefix. Disabling ATS and PASID first left PRI enabled on a function that could no longer issue or accept such transactions, while page requests might still be outstanding.
Disable the capabilities in the reverse order of enabling: PRI, then ATS, then PASID. This also keeps ATS disabled before PASID, as required by the PCIe specification.
--- drivers/iommu/amd/iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +1 -1
@@ -651,9 +651,9 @@ static void pdev_enable_caps(struct pci_dev *pdev)static void pdev_disable_caps (struct pci_dev *pdev) { +pdev_disable_cap_pri (pdev);pdev_disable_cap_ats (pdev);pdev_disable_cap_pasid (pdev); -pdev_disable_cap_pri (pdev); }/*
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 2/12 iommu/amd: Derive the PPR completion GN bit from the request
In reply to Vasant HegdeCurrent amd_iommu_complete_ppr() derived GN bit from page table mode. It also missed IOMMU passthrough mode which also supports PASID.
Pass GN in from the caller instead: amd_iommu_report_ppr_err() uses EVENT_FLAG_PPR_GN, the abort path copies PPR_FLAG_GN from the log entry, and amd_iommu_page_response() reads it back from IOMMU_FAULT_PAGE_REQUEST_PASID_VALID recorded when the fault was reported. Fold __amd_iommu_complete_ppr() into amd_iommu_complete_ppr() now that every caller supplies gn.
--- drivers/iommu/amd/amd_iommu.h | 3 ++- drivers/iommu/amd/iommu.c | 19 +++---------------- drivers/iommu/amd/ppr.c | 14 ++++++++++---- 3 files changed, 15 insertions(+), 21 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -1
@@ -83,7 +83,8 @@ int __init amd_iommu_alloc_ppr_log(struct amd_iommu *iommu);void __init amd_iommu_free_ppr_log (struct amd_iommu *iommu);void amd_iommu_enable_ppr_log (struct amd_iommu *iommu);void amd_iommu_poll_ppr_log (struct amd_iommu *iommu); -int amd_iommu_complete_ppr (struct device *dev,u32 pasid,int status,int tag); +int amd_iommu_complete_ppr (struct device *dev,u32 pasid,int status,int tag, +bool gn);/* * This function flushes all internal caches of
drivers/iommu/amd/iommu.c +3 -16
@@ -89,9 +89,6 @@ static void clone_aliases(struct amd_iommu *iommu, struct device *dev);static int iommu_completion_wait (struct amd_iommu *iommu); -static int __amd_iommu_complete_ppr (struct device *dev,u32 pasid, -int status,int tag,bool gn); -/**************************************************************************** * * Helper functions
@@ -902,7 +899,7 @@ static void amd_iommu_report_ppr_err(struct amd_iommu *iommu, volatile u32 *even gn = (flags &EVENT_FLAG_PPR_GN ); -__amd_iommu_complete_ppr (&pdev->dev, pasid,IOMMU_PAGE_RESP_FAILURE , tag, gn); +amd_iommu_complete_ppr (&pdev->dev, pasid,IOMMU_PAGE_RESP_FAILURE , tag, gn);pci_dev_put (pdev); }
@@ -1853,8 +1850,8 @@ static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,amd_iommu_dev_flush_pasid_pages (dev_data, pasid,0 ,U64_MAX ); } -static int __amd_iommu_complete_ppr (struct device *dev,u32 pasid, -int status,int tag,bool gn) +int amd_iommu_complete_ppr (struct device *dev,u32 pasid,int status, +int tag,bool gn) {struct iommu_dev_data *dev_data;struct amd_iommu *iommu;
@@ -1868,16 +1865,6 @@ static int __amd_iommu_complete_ppr(struct device *dev, u32 pasid,return iommu_queue_command (iommu, &cmd); } -int amd_iommu_complete_ppr (struct device *dev,u32 pasid,int status,int tag) -{ -struct iommu_dev_data *dev_data =dev_iommu_priv_get (dev); -bool gn; - - gn =pdom_is_v2_pgtbl_mode (dev_data->domain); - -return __amd_iommu_complete_ppr (dev, pasid, status, tag, gn); -} -/**************************************************************************** * * The next functions belong to the domain allocation. A domain is
drivers/iommu/amd/ppr.c +10 -4
@@ -144,8 +144,11 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw) goto out; } - event.fault.prm.flags |=IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID ; - event.fault.prm.flags |=IOMMU_FAULT_PAGE_REQUEST_PASID_VALID ; +/* PPR_FLAG_GN means this request carries a valid PASID. */ +if (PPR_FLAGS (raw[0 ]) &PPR_FLAG_GN ) { + event.fault.prm.flags |=IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID ; + event.fault.prm.flags |=IOMMU_FAULT_PAGE_REQUEST_PASID_VALID ; + }if (PPR_TAG_LAST_PAGE (raw[0 ])) event.fault.prm.flags |=IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE ;
@@ -157,7 +160,8 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw) out:/* Nobody cared, abort */ amd_iommu_complete_ppr (&pdev->dev,PPR_PASID (raw[0 ]), -IOMMU_PAGE_RESP_FAILURE ,PPR_TAG (raw[0 ])); +IOMMU_PAGE_RESP_FAILURE ,PPR_TAG (raw[0 ]), +PPR_FLAGS (raw[0 ]) &PPR_FLAG_GN );pci_dev_put (pdev); }
@@ -241,7 +245,9 @@ void amd_iommu_iopf_uninit(struct amd_iommu *iommu)void amd_iommu_page_response (struct device *dev,struct iopf_fault *evt,struct iommu_page_response *resp) { -amd_iommu_complete_ppr (dev, resp->pasid, resp->code, resp->grpid); +bool gn = evt->fault.prm.flags &IOMMU_FAULT_PAGE_REQUEST_PASID_VALID ; + +amd_iommu_complete_ppr (dev, resp->pasid, resp->code, resp->grpid, gn); } int amd_iommu_iopf_add_device(struct amd_iommu *iommu,
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 3/12 iommu/amd: Complete a PPR from a device the IOMMU did not probe
In reply to Vasant Hegdeiommu_call_iopf_notifier() reads dev_data->max_pasids after looking up the PCI device. dev_iommu_priv_get() is NULL when probe failed, and the abort path then calls amd_iommu_complete_ppr(), which reads dev_data->devid and the device's IOMMU. A kdump kernel reuses the previous device table, so a device that failed probe can still have PPR set. The invalid-PPR event path has the same dependency.
COMPLETE_PPR needs the IOMMU that owns the log and the DeviceID from the entry. amd_iommu_complete_ppr() takes those directly. Report the fault only when the device was probed. Otherwise complete the last page of the group from that DeviceID. The invalid-PPR event path completes the same way and no longer looks the device up.
--- drivers/iommu/amd/amd_iommu.h | 4 ++-- drivers/iommu/amd/iommu.c | 27 +++++---------------------- drivers/iommu/amd/ppr.c | 27 +++++++++++++++++++++++---- 3 files changed, 30 insertions(+), 28 deletions(-)
drivers/iommu/amd/amd_iommu.h +2 -2
@@ -83,8 +83,8 @@ int __init amd_iommu_alloc_ppr_log(struct amd_iommu *iommu);void __init amd_iommu_free_ppr_log (struct amd_iommu *iommu);void amd_iommu_enable_ppr_log (struct amd_iommu *iommu);void amd_iommu_poll_ppr_log (struct amd_iommu *iommu); -int amd_iommu_complete_ppr (struct device *dev,u32 pasid,int status,int tag, -bool gn); +int amd_iommu_complete_ppr (struct amd_iommu *iommu,u16 devid,u32 pasid, +int status,int tag,bool gn);/* * This function flushes all internal caches of
drivers/iommu/amd/iommu.c +5 -22
@@ -872,7 +872,6 @@ static void amd_iommu_report_page_fault(struct amd_iommu *iommu,static void amd_iommu_report_ppr_err (struct amd_iommu *iommu,volatile u32 *event,u16 devid,u64 address,int flags) { -struct pci_dev *pdev;struct device *dev = iommu->iommu.dev;u32 pasid =PPR_PASID (*((u64 *)event));int tag = event[1 ] &0x03FF ;
@@ -887,20 +886,9 @@ static void amd_iommu_report_ppr_err(struct amd_iommu *iommu, volatile u32 *evenif (flags &EVENT_FLAG_PPR_RX )return ; - pdev =pci_get_domain_bus_and_slot (iommu->pci_seg->id,PCI_BUS_NUM (devid), - devid &0xff ); -if (!pdev) -return ; - -if (!dev_iommu_priv_get (&pdev->dev)) { -pci_dev_put (pdev); -return ; - } - gn = (flags &EVENT_FLAG_PPR_GN ); - -amd_iommu_complete_ppr (&pdev->dev, pasid,IOMMU_PAGE_RESP_FAILURE , tag, gn); -pci_dev_put (pdev); +amd_iommu_complete_ppr (iommu, devid, pasid,IOMMU_PAGE_RESP_FAILURE , + tag, gn); }static void iommu_print_event (struct amd_iommu *iommu,void *__evt)
@@ -1850,17 +1838,12 @@ static void dev_flush_pasid_all(struct iommu_dev_data *dev_data,amd_iommu_dev_flush_pasid_pages (dev_data, pasid,0 ,U64_MAX ); } -int amd_iommu_complete_ppr (struct device *dev,u32 pasid,int status, -int tag,bool gn) +int amd_iommu_complete_ppr (struct amd_iommu *iommu,u16 devid,u32 pasid, +int status,int tag,bool gn) { -struct iommu_dev_data *dev_data; -struct amd_iommu *iommu;struct iommu_cmd cmd; - dev_data =dev_iommu_priv_get (dev); - iommu =get_amd_iommu_from_dev (dev); - -build_complete_ppr (&cmd, dev_data->devid, pasid, status, tag, gn); +build_complete_ppr (&cmd, devid, pasid, status, tag, gn);return iommu_queue_command (iommu, &cmd); }
drivers/iommu/amd/ppr.c +23 -4
@@ -116,10 +116,27 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw)return ; } +/* +* Probe never attached this device, or it is no longer on the +* bus. A kdump kernel can reuse a DTE that still has PPR set. +* The log entry carries the DeviceID, so complete the group +* here and do not touch dev_data. +*/ pdev =pci_get_domain_bus_and_slot (iommu->pci_seg->id,PCI_BUS_NUM (devid), devid &0xff ); -if (!pdev) + dev_data = pdev ?dev_iommu_priv_get (&pdev->dev) :NULL ; +if (!dev_data) { +pr_info_ratelimited ("PPR for unprobed device: 0x%x\n" , devid); + +if (PPR_TAG_LAST_PAGE (raw[0 ])) { +amd_iommu_complete_ppr (iommu, devid,PPR_PASID (raw[0 ]), +IOMMU_PAGE_RESP_FAILURE , +PPR_TAG (raw[0 ]), +PPR_FLAGS (raw[0 ]) &PPR_FLAG_GN ); + } +pci_dev_put (pdev);return ; + }if (!ppr_is_valid (iommu, raw)) goto out;
@@ -136,7 +153,6 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw)* PASID zero is used for requests from the I/O device without * a PASID */ - dev_data =dev_iommu_priv_get (&pdev->dev);if (event.fault.prm.pasid ==0 || event.fault.prm.pasid >= dev_data->max_pasids) { pr_info_ratelimited("Invalid PASID : 0x%x, device : 0x%x\n",
@@ -159,7 +175,7 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw) out:/* Nobody cared, abort */ -amd_iommu_complete_ppr (&pdev->dev,PPR_PASID (raw[0 ]), +amd_iommu_complete_ppr (iommu, devid,PPR_PASID (raw[0 ]),IOMMU_PAGE_RESP_FAILURE ,PPR_TAG (raw[0 ]),PPR_FLAGS (raw[0 ]) &PPR_FLAG_GN );pci_dev_put (pdev);
@@ -245,9 +261,12 @@ void amd_iommu_iopf_uninit(struct amd_iommu *iommu)void amd_iommu_page_response (struct device *dev,struct iopf_fault *evt,struct iommu_page_response *resp) { +struct iommu_dev_data *dev_data =dev_iommu_priv_get (dev);bool gn = evt->fault.prm.flags &IOMMU_FAULT_PAGE_REQUEST_PASID_VALID ; -amd_iommu_complete_ppr (dev, resp->pasid, resp->code, resp->grpid, gn); +amd_iommu_complete_ppr (get_amd_iommu_from_dev_data (dev_data), + dev_data->devid, resp->pasid, resp->code, + resp->grpid, gn); } int amd_iommu_iopf_add_device(struct amd_iommu *iommu,
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 4/12 iommu/amd: Keep the GCR3 table while PASIDs are attached
In reply to Vasant Hegdepci_dev_reset_iommu_prepare() attaches the blocked domain before clearing the device's PASIDs. detach_device() frees the GCR3 table during that transition, so a later amd_iommu_clear_gcr3() dereferences a NULL root.
Do not free the GCR3 table while it still has attached PASIDs. The RID GCR3[0] entry is still cleared before detach, and setup_gcr3_table() reuses the retained table on re-attach. Free it from amd_iommu_release_device() instead, after the blocked-domain attach has cleared the DTE.
--- drivers/iommu/amd/iommu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/iommu/amd/iommu.c +5 -4
@@ -1949,12 +1949,12 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info,int nid = iommu ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ;int domid; +if (gcr3_info->gcr3_tbl) +return 0 ; +if (levels > amd_iommu_max_glx_val)return -EINVAL ; -if (gcr3_info->gcr3_tbl) -return -EBUSY ; -/* Allocate per device domain ID */ domid =amd_iommu_pdom_id_alloc ();if (domid <=0 )
@@ -2247,7 +2247,7 @@ static void destroy_gcr3_table(struct iommu_dev_data *dev_data,if (pdom_is_v2_pgtbl_mode (pdom))update_gcr3 (dev_data,0 ,0 , false); -if (gcr3_info->gcr3_tbl ==NULL ) +if (gcr3_info->gcr3_tbl ==NULL || gcr3_info->pasid_cnt)return ;free_gcr3_table (gcr3_info);
@@ -2722,6 +2722,7 @@ static void amd_iommu_release_device(struct device *dev) }WARN_ON (dev_data->domain); +destroy_gcr3_table (dev_data,NULL );/* * We keep dev_data around for unplugged devices and reuse it when the
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 5/12 iommu/amd: Restore an SVA PASID after PCI reset
In reply to Vasant HegdeFrom: Santosh Shukla <[email protected]>
pci_dev_reset_iommu_done() re-attaches every PASID with old set to the group's blocking domain. iommu_sva_set_dev_pasid() rejects any old domain, so the core warns and the PASID stays unprogrammed until the device is unbound.
prepare() has already cleared the GCR3 entry and dropped the SVA tracking record. A blocking old domain is therefore a fresh attach. Replacing one attached domain with another still returns -EOPNOTSUPP.
--- drivers/iommu/amd/pasid.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/iommu/amd/pasid.c +6 -1
@@ -109,7 +109,12 @@ static int iommu_sva_set_dev_pasid(struct iommu_domain *domain,unsigned long flags;int ret = -EINVAL ; -if (old) +/* +* pci_dev_reset_iommu_done() passes the blocking domain. prepare() +* has already cleared this PASID, so program it again. Replacing +* one attached domain with another is not supported. +*/ +if (old && old->type !=IOMMU_DOMAIN_BLOCKED )return -EOPNOTSUPP ;/* PASID zero is used for requests from the I/O device without PASID */
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 6/12 iommu/amd: Disable GCR3 entries from the SVA mm release notifier
In reply to Vasant Hegdesva_mn_release() runs when the mm page tables are about to be freed. It calls remove_dev_pasid(), which invokes amd_iommu_clear_gcr3() without the group mutex and triggers its lockdep assertion. It also removes the PASID attachment and driver metadata behind the IOMMU core's back.
Split disabling a GCR3 entry from the normal PASID detach operation. The notifier now disables and flushes each entry while holding the SVA domain lock, but leaves pasid_cnt and pdom_dev_data intact. The normal core-driven detach still takes the group mutex, clears the entry again, and removes the attachment.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/iommu.c | 12 +++++++++++- drivers/iommu/amd/pasid.c | 13 +++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -77,6 +77,7 @@ void amd_iommu_iopf_remove_device(struct amd_iommu *iommu,int amd_iommu_set_gcr3 (struct iommu_dev_data *dev_data,ioasid_t pasid,unsigned long gcr3);int amd_iommu_clear_gcr3 (struct iommu_dev_data *dev_data,ioasid_t pasid); +int amd_iommu_disable_gcr3 (struct iommu_dev_data *dev_data,ioasid_t pasid);/* PPR */ int __init amd_iommu_alloc_ppr_log (struct amd_iommu *iommu);
drivers/iommu/amd/iommu.c +11 -1
@@ -2044,6 +2044,16 @@ int amd_iommu_set_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid,return ret; } +/* +* Clear a GCR3 entry and flush its cached translations without dropping +* pasid_cnt. amd_iommu_clear_gcr3() drops the count when the PASID is +* detached. +*/ +int amd_iommu_disable_gcr3 (struct iommu_dev_data *dev_data,ioasid_t pasid) +{ +return update_gcr3 (dev_data, pasid,0 , false); +} +int amd_iommu_clear_gcr3 (struct iommu_dev_data *dev_data,ioasid_t pasid) {struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info;
@@ -2051,7 +2061,7 @@ int amd_iommu_clear_gcr3(struct iommu_dev_data *dev_data, ioasid_t pasid)iommu_group_mutex_assert (dev_data->dev); - ret =update_gcr3 (dev_data, pasid,0 , false); + ret =amd_iommu_disable_gcr3 (dev_data, pasid);if (ret)return ret;
drivers/iommu/amd/pasid.c +9 -4
@@ -79,7 +79,7 @@ static void sva_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn,static void sva_mn_release (struct mmu_notifier *mn,struct mm_struct *mm) { -struct pdom_dev_data *pdom_dev_data, *next; +struct pdom_dev_data *pdom_dev_data;struct protection_domain *sva_pdom;unsigned long flags;
@@ -87,9 +87,14 @@ static void sva_mn_release(struct mmu_notifier *mn, struct mm_struct *mm)spin_lock_irqsave (&sva_pdom->lock, flags); -/* Assume dev_data_list contains same PASID with different devices */ -for_each_pdom_dev_data_safe (pdom_dev_data, next, sva_pdom) -remove_dev_pasid (pdom_dev_data); +/* +* Disable the GCR3 entry before the mm page tables are freed. Leave +* the PASID attachment and pdom_dev_data for the IOMMU core to +* remove through the normal detach path. +*/ +for_each_pdom_dev_data (pdom_dev_data, sva_pdom) +amd_iommu_disable_gcr3 (pdom_dev_data->dev_data, + pdom_dev_data->pasid);spin_unlock_irqrestore (&sva_pdom->lock, flags); }
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 7/12 iommu/amd: Complete an aborted PPR group only on its last page
In reply to Vasant Hegdeiommu_call_iopf_notifier() sends COMPLETE_PPR for every rejected request. A group of several pages then gets several responses for one group index. A response for an earlier page can complete a newer group that reused the index.
Respond only when the request is the last page of its group. A request that is not the last page needs no acknowledgement.
--- drivers/iommu/amd/ppr.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/iommu/amd/ppr.c +11 -4
@@ -174,10 +174,17 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw)return ; out: -/* Nobody cared, abort */ -amd_iommu_complete_ppr (iommu, devid,PPR_PASID (raw[0 ]), -IOMMU_PAGE_RESP_FAILURE ,PPR_TAG (raw[0 ]), -PPR_FLAGS (raw[0 ]) &PPR_FLAG_GN ); +/* +* A page-request group is completed once. Responding to a page +* that is not the last one sends another completion for the same +* group index, which can complete a newer group that reused it. +*/ +if (PPR_TAG_LAST_PAGE (raw[0 ])) { +amd_iommu_complete_ppr (iommu, devid,PPR_PASID (raw[0 ]), +IOMMU_PAGE_RESP_FAILURE , +PPR_TAG (raw[0 ]), +PPR_FLAGS (raw[0 ]) &PPR_FLAG_GN ); + }pci_dev_put (pdev); }
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 8/12 iommu/amd: Discard partial page requests when the PPR log overflows
In reply to Vasant HegdeAn overflow drops requests that were not written into the log. The IRQ thread consumes what is still there and then restarts the log. A group whose last page was dropped stays on the device partial list. The next group that reuses the index is merged with those requests, and the partials remain until the device leaves the IOPF queue.
Discard the partials after the log has stopped and before it is enabled again. The handler has already polled every entry that survived.
--- drivers/iommu/amd/ppr.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/iommu/amd/ppr.c +15 -0
@@ -59,6 +59,21 @@ void __init amd_iommu_free_ppr_log(struct amd_iommu *iommu)*/ void amd_iommu_restart_ppr_log (struct amd_iommu *iommu) { +u32 status; + +/* +* Overflow stops the log. The IRQ thread has already consumed +* the entries that were written. A group whose last page was +* dropped is still on the partial list; discard it before the +* log restarts and another group reuses the index. +*/ + status =readl (iommu->mmio_base +MMIO_STATUS_OFFSET ); +if (status &MMIO_STATUS_PPR_RUN_MASK ) +return ; + +if (iommu->iopf_queue) +iopf_queue_discard_partial (iommu->iopf_queue); +amd_iommu_restart_log (iommu,"PPR" ,CONTROL_PPRINT_EN ,CONTROL_PPRLOG_EN ,MMIO_STATUS_PPR_RUN_MASK ,MMIO_STATUS_PPR_OVERFLOW_MASK );
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 9/12 iommu/amd: Allocate the GCR3 table from the device
In reply to Vasant Hegdesetup_gcr3_table() conflated allocating the per-device GCR3 table with programming the v2 RID GCR3[0] entry. Because the table can outlive the RID domain, split the two and change where the table is allocated based on the device's PASID capability:
- PASID-capable device: the GCR3 table is allocated once in amd_iommu_probe_device() and kept for the lifetime of the device. It is freed only in amd_iommu_release_device(), after the DTE has been cleared.
- Non-PASID device with a v2 page-table domain: the table is allocated on demand in attach_device() and freed in detach_device() when the domain is detached.
With allocation handled per the above, attach_device() only programs GCR3[0] (via set_default_gcr3_entry()) for a v2 domain. detach_device() clears GCR3[0] (via clear_default_gcr3_entry()) for a v2 domain and, for a non-PASID device, frees the table there. set_dte_entry() installs the GCR3 table in the DTE only for an SVA-capable domain.
Rename the helpers accordingly: setup_gcr3_table() becomes alloc_gcr3_table(), and init_gcr3_table()/destroy_gcr3_table() are replaced by the narrower set_default_gcr3_entry()/clear_default_gcr3_entry().
--- drivers/iommu/amd/iommu.c | 108 ++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 50 deletions(-)
drivers/iommu/amd/iommu.c +58 -50
@@ -1942,11 +1942,14 @@ static int get_gcr3_levels(int pasids)return levels ? (DIV_ROUND_UP (levels,9 ) -1 ) : levels; } -static int setup_gcr3_table (struct gcr3_tbl_info *gcr3_info, -struct amd_iommu *iommu,int pasids) +static int alloc_gcr3_table (struct iommu_dev_data *dev_data, +struct amd_iommu *iommu) { +struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; +int pasids = dev_data->max_pasids >0 ? dev_data->max_pasids :1 ;int levels =get_gcr3_levels (pasids); -int nid = iommu ?dev_to_node (&iommu->dev->dev) :NUMA_NO_NODE ; +int nid = iommu && iommu->dev ?dev_to_node (&iommu->dev->dev) : +NUMA_NO_NODE ;int domid;if (gcr3_info->gcr3_tbl)
@@ -1964,6 +1967,7 @@ static int setup_gcr3_table(struct gcr3_tbl_info *gcr3_info, gcr3_info->gcr3_tbl =iommu_alloc_pages_node_sz (nid,GFP_ATOMIC ,SZ_4K );if (gcr3_info->gcr3_tbl ==NULL ) {amd_iommu_pdom_id_free (domid); + gcr3_info->domid =0 ;return -ENOMEM ; }
@@ -2165,7 +2169,11 @@ static void set_dte_entry(struct amd_iommu *iommu,amd_iommu_make_clear_dte (dev_data, &new); old_domid =READ_ONCE (dte->data[1 ]) &DTE_DOMID_MASK ; -if (gcr3_info->gcr3_tbl) +/* +* Update GCR3 related info if device is attached to PASID +* capable domain +*/ +if (pdom_is_sva_capable (domain) && gcr3_info->gcr3_tbl)set_dte_gcr3_table (dev_data, &new);else if (domain->domain.type ==IOMMU_DOMAIN_IDENTITY )set_dte_passthrough (dev_data, domain, &new);
@@ -2209,58 +2217,27 @@ static void dev_update_dte(struct iommu_dev_data *dev_data, bool set)clear_dte_entry (iommu, dev_data); } -/* -* If domain is SVA capable then initialize GCR3 table. Also if domain is -* in v2 page table mode then update GCR3[0]. -*/ -static int init_gcr3_table (struct iommu_dev_data *dev_data, -struct protection_domain *pdom) +/* Program GCR3[0], the RID page table. Only a v2 domain has one. */ +static int set_default_gcr3_entry (struct iommu_dev_data *dev_data, +struct protection_domain *pdom) { -struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data); -int max_pasids = dev_data->max_pasids;struct pt_iommu_x86_64_hw_info pt_info; -int ret =0 ; - -/* -* If domain is in pt mode then setup GCR3 table only if device -* is PASID capable -*/ -if (pdom_is_in_pt_mode (pdom) && !pdev_pasid_supported (dev_data)) -return ret; -/* -* By default, setup GCR3 table to support MAX PASIDs -* supported by the device/IOMMU. -*/ - ret =setup_gcr3_table (&dev_data->gcr3_info, iommu, - max_pasids >0 ? max_pasids :1 ); -if (ret) -return ret; - -/* Setup GCR3[0] only if domain is setup with v2 page table mode */ if (!pdom_is_v2_pgtbl_mode (pdom)) -return ret; +return 0 ;pt_iommu_x86_64_hw_info (&pdom->amdv2, &pt_info); - ret =update_gcr3 (dev_data,0 ,__sme_set (pt_info.gcr3_pt), true); -if (ret) -free_gcr3_table (&dev_data->gcr3_info); - -return ret; +return update_gcr3 (dev_data,0 ,__sme_set (pt_info.gcr3_pt), true); } -static void destroy_gcr3_table (struct iommu_dev_data *dev_data, -struct protection_domain *pdom) +static void clear_default_gcr3_entry (struct iommu_dev_data *dev_data) {struct gcr3_tbl_info *gcr3_info = &dev_data->gcr3_info; -if (pdom_is_v2_pgtbl_mode (pdom)) -update_gcr3 (dev_data,0 ,0 , false); - -if (gcr3_info->gcr3_tbl ==NULL || gcr3_info->pasid_cnt) +if (gcr3_info->gcr3_tbl ==NULL )return ; -free_gcr3_table (gcr3_info); +update_gcr3 (dev_data,0 ,0 , false); } static int pdom_attach_iommu(struct amd_iommu *iommu,
@@ -2351,9 +2328,18 @@ static int attach_device(struct device *dev,if (dev_data->perfopt) goto skip_caps; -/* Setup GCR3 table */ -if (pdom_is_sva_capable (domain)) { - ret =init_gcr3_table (dev_data, domain); +/* +* A PASID-capable device already owns a table from probe. A paging +* domain with v2 page table without PASID allocates one here. +*/ +if (pdom_is_v2_pgtbl_mode (domain)) { + ret =alloc_gcr3_table (dev_data, iommu); +if (ret) { +pdom_detach_iommu (iommu, domain); + goto out; + } + + ret =set_default_gcr3_entry (dev_data, domain);if (ret) {pdom_detach_iommu (iommu, domain); goto out;
@@ -2433,9 +2419,17 @@ static void detach_device(struct device *dev)list_del (&dev_data->list);spin_unlock_irqrestore (&domain->lock, flags); -/* Clear GCR3 table */ -if (pdom_is_sva_capable (domain)) -destroy_gcr3_table (dev_data, domain); +/* +* GCR3[0] belongs to the paging domain w/ v2 page table. +* A device without PASID has no other entries, so the +* table is freed with that domain. +*/ +if (pdom_is_v2_pgtbl_mode (domain)) { +clear_default_gcr3_entry (dev_data); + +if (!pdev_pasid_supported (dev_data)) +free_gcr3_table (&dev_data->gcr3_info); + }/* Update data structures */ dev_data->domain =NULL ;
@@ -2716,6 +2710,17 @@ static struct iommu_device *amd_iommu_probe_device(struct device *dev)return ERR_PTR (-ENODEV ); } +/* +* PASID-capable devices own a GCR3 table for the life of the device. +* A paging domain with v2 page table without PASID allocates one +* later, from attach_device(). +*/ +if (pdev_pasid_supported (dev_data)) { + ret =alloc_gcr3_table (dev_data, iommu); +if (ret) +return ERR_PTR (ret); + } +return iommu_dev; }
@@ -2732,7 +2737,10 @@ static void amd_iommu_release_device(struct device *dev) }WARN_ON (dev_data->domain); -destroy_gcr3_table (dev_data,NULL ); +WARN_ON (dev_data->gcr3_info.pasid_cnt); + +if (dev_data->gcr3_info.gcr3_tbl) +free_gcr3_table (&dev_data->gcr3_info);/* * We keep dev_data around for unplugged devices and reuse it when the
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 10/12 iommu/amd: Return -ENOMEM when SVA PASID allocation fails
In reply to Vasant Hegdeiommu_sva_set_dev_pasid() returns -EINVAL when the pdom_dev_data allocation fails. An invalid PASID or a disabled PASID is an invalid argument. Running out of memory is not, so return -ENOMEM and let the bind caller tell the two failures apart.
--- drivers/iommu/amd/pasid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd/pasid.c +1 -1
@@ -133,7 +133,7 @@ static int iommu_sva_set_dev_pasid(struct iommu_domain *domain,/* Add PASID to protection domain pasid list */ pdom_dev_data =kzalloc_obj (*pdom_dev_data);if (pdom_dev_data ==NULL ) -return ret; +return -ENOMEM ; pdom_dev_data->pasid = pasid; pdom_dev_data->dev_data = dev_data;
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 11/12 iommu/amd: Drain the PPR log before detaching an SVA PASID
In reply to Vasant Hegdeamd_iommu_remove_dev_pasid() can return while a page request for that PASID is still sitting in the hardware PPR log. The PPR thread may later enqueue that request after the core has already freed the PASID attach handle, and the IOPF worker then dereferences the freed handle -- a use-after-free.
The device driver issues a PASID stop before unbind, so no new request is generated for this PASID, but requests already posted remain in the log. Process those entries and then flush the IOPF workqueue so that queued work completes before the handle is freed.
Serialize PPR log consumption with a new per-IOMMU mutex (ppr_lock):
factor the existing poll into __amd_iommu_poll_ppr_log() under the lock, and have both the PPR thread and the drain path take it.
--- drivers/iommu/amd/amd_iommu.h | 1 + drivers/iommu/amd/amd_iommu_types.h | 2 ++ drivers/iommu/amd/init.c | 1 + drivers/iommu/amd/pasid.c | 10 +++++++++- drivers/iommu/amd/ppr.c | 29 ++++++++++++++++++++++++++++- 5 files changed, 41 insertions(+), 2 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
@@ -84,6 +84,7 @@ int __init amd_iommu_alloc_ppr_log(struct amd_iommu *iommu);void __init amd_iommu_free_ppr_log (struct amd_iommu *iommu);void amd_iommu_enable_ppr_log (struct amd_iommu *iommu);void amd_iommu_poll_ppr_log (struct amd_iommu *iommu); +void amd_iommu_drain_ppr_log (struct iommu_dev_data *dev_data);int amd_iommu_complete_ppr (struct amd_iommu *iommu,u16 devid,u32 pasid,int status,int tag,bool gn);
drivers/iommu/amd/amd_iommu_types.h +2 -0
@@ -691,6 +691,8 @@ struct amd_iommu {/* Base of the PPR log, if present */ u8 *ppr_log; +/* Serializes PPR log consumption */ +struct mutex ppr_lock;/* Name for PPR log interrupt */ unsigned char ppr_irq_name[16 ];
drivers/iommu/amd/init.c +1 -0
@@ -1914,6 +1914,7 @@ static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h, iommu->pci_seg = pci_seg;raw_spin_lock_init (&iommu->lock); +mutex_init (&iommu->ppr_lock); iommu->cmd_sem_val =0 ;/* Add IOMMU to internal data structures */
drivers/iommu/amd/pasid.c +9 -1
@@ -159,9 +159,10 @@ void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid, struct iommu_domain *domain) {struct protection_domain *sva_pdom; +struct iommu_dev_data *dev_data =dev_iommu_priv_get (dev);unsigned long flags; -if (!is_pasid_valid (dev_iommu_priv_get (dev), pasid)) +if (!is_pasid_valid (dev_data, pasid))return ; sva_pdom =to_pdomain (domain);
@@ -172,6 +173,13 @@ void amd_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid,remove_pdom_dev_pasid (sva_pdom, dev, pasid);spin_unlock_irqrestore (&sva_pdom->lock, flags); + +/* +* Queued faults reference the attach handle, which the caller +* frees once this returns. Consume requests already in the log +* and flush the work they queued. +*/ +amd_iommu_drain_ppr_log (dev_data); }static void iommu_sva_domain_free (struct iommu_domain *domain)
drivers/iommu/amd/ppr.c +28 -1
@@ -203,10 +203,12 @@ static void iommu_call_iopf_notifier(struct amd_iommu *iommu, u64 *raw)pci_dev_put (pdev); } -void amd_iommu_poll_ppr_log (struct amd_iommu *iommu) +static void __amd_iommu_poll_ppr_log (struct amd_iommu *iommu) {u32 head, tail; +lockdep_assert_held (&iommu->ppr_lock); + head =readl (iommu->mmio_base +MMIO_PPR_HEAD_OFFSET ); tail =readl (iommu->mmio_base +MMIO_PPR_TAIL_OFFSET );
@@ -250,6 +252,31 @@ void amd_iommu_poll_ppr_log(struct amd_iommu *iommu) } } +void amd_iommu_poll_ppr_log (struct amd_iommu *iommu) +{ +mutex_lock (&iommu->ppr_lock); +__amd_iommu_poll_ppr_log (iommu); +mutex_unlock (&iommu->ppr_lock); +} + +/* +* Consume PPR requests already logged for this device, then flush the +* IOPF work they queued. The caller is releasing a PASID attach handle. +*/ +void amd_iommu_drain_ppr_log (struct iommu_dev_data *dev_data) +{ +struct amd_iommu *iommu =get_amd_iommu_from_dev_data (dev_data); + +if (!iommu->ppr_log || !dev_data->ppr) +return ; + +mutex_lock (&iommu->ppr_lock); +__amd_iommu_poll_ppr_log (iommu); +mutex_unlock (&iommu->ppr_lock); + +iopf_queue_flush_dev (dev_data->dev); +} +/************************************************************** * * IOPF handling stuff
Signature · 1 line
2.31.1
Vasant Hegde
maintainerPATCH 12/12 iommu/amd: Drain the PPR log before removing a device from the IOPF queue
In reply to Vasant Hegdedetach_device() flushes the software fault queue and removes the device from the IOPF queue before it clears the DTE. The DTE is what enables PPR, so the IOMMU can still log a page request after fault_param is gone. iommu_report_device_fault() warns when it finds a handler and no fault parameter.
Clear and invalidate the DTE first. A device on the IOPF queue then drains the PPR log and is removed from the queue. Disable the PCI capabilities after that, for every device, so PRI stays enabled until the logged requests have been answered.
--- drivers/iommu/amd/iommu.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
drivers/iommu/amd/iommu.c +12 -12
@@ -2400,25 +2400,25 @@ static void detach_device(struct device *dev)if (WARN_ON (!dev_data->domain)) goto out; -/* Remove IOPF handler */ +/* Clear DTE and flush the entry */ +spin_lock_irqsave (&domain->lock, flags); +dev_update_dte (dev_data, false); +amd_iommu_domain_flush_all (domain); +list_del (&dev_data->list); +spin_unlock_irqrestore (&domain->lock, flags); + +/* +* Stop new page requests, then finish the ones already logged. +* Disable the PCI capabilities only after that, for every device. +*/ if (dev_data->ppr) { -iopf_queue_flush_dev (dev); +amd_iommu_drain_ppr_log (dev_data);amd_iommu_iopf_remove_device (iommu, dev_data); }if (dev_is_pci (dev))pdev_disable_caps (to_pci_dev (dev)); -spin_lock_irqsave (&domain->lock, flags); - -/* Clear DTE and flush the entry */ -dev_update_dte (dev_data, false); - -/* Flush IOTLB and wait for the flushes to finish */ -amd_iommu_domain_flush_all (domain); -list_del (&dev_data->list); -spin_unlock_irqrestore (&domain->lock, flags); -/* * GCR3[0] belongs to the paging domain w/ v2 page table. * A device without PASID has no other entries, so the
Signature · 1 line
2.31.1