[PATCH v2 2/3] KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach
From: Narayana Murty N
Date: Thu Nov 26 2026 - 03:51:32 EST
TCE hypercalls traverse stt->iommu_tables while VFIO detach may
remove attachments and return hardware window ownership before
in-flight TCE updates complete.
Use the existing kvm->srcu read-side protection for IOMMU table
traversal and synchronize SRCU readers during detach.
This ensures that TCE hardware updates complete before the
detached window is returned to VFIO.
The following patch addresses safe reclamation of child entries
accessed by these SRCU readers.
Signed-off-by: Narayana Murty N <nnmlinux@xxxxxxxxxxxxx>
---
arch/powerpc/kvm/book3s_64_vio.c | 16 +++++++++++++---
1 file changed, 13 insertions(+), 3 deletions(-)
diff --git a/arch/powerpc/kvm/book3s_64_vio.c b/arch/powerpc/kvm/book3s_64_vio.c
index e9370b0e9afe..afa7ce57f5ae 100644
--- a/arch/powerpc/kvm/book3s_64_vio.c
+++ b/arch/powerpc/kvm/book3s_64_vio.c
@@ -112,6 +112,13 @@ void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
cond_resched_rcu();
}
rcu_read_unlock();
+
+ /*
+ * Wait for hcalls that may still be programming the detached
+ * window. They run under kvm->srcu, so once this returns, no
+ * TCE update can touch the hardware after ownership is released.
+ */
+ synchronize_srcu(&kvm->srcu);
}
long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
@@ -634,7 +641,8 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
entry = ioba >> stt->page_shift;
- list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
+ list_for_each_entry_srcu(stit, &stt->iommu_tables, next,
+ srcu_read_lock_held(&vcpu->kvm->srcu)) {
if (dir == DMA_NONE)
ret = kvmppc_tce_iommu_unmap(vcpu->kvm, stt,
stit->tbl, entry);
@@ -738,7 +746,8 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
goto put_exit;
}
- list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
+ list_for_each_entry_srcu(stit, &stt->iommu_tables, next,
+ srcu_read_lock_held(&vcpu->kvm->srcu)) {
ret = kvmppc_tce_iommu_map(vcpu->kvm, stt,
stit->tbl, entry + i, ua,
iommu_tce_direction(tce));
@@ -788,7 +797,8 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
goto put_exit;
}
- list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
+ list_for_each_entry_srcu(stit, &stt->iommu_tables, next,
+ srcu_read_lock_held(&vcpu->kvm->srcu)) {
unsigned long entry = ioba >> stt->page_shift;
for (i = 0; i < npages; ++i) {
--
2.54.0