mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] KVM: PPC: Book3S: Protect TCE IOMMU table walks with RCU
@ 2026-11-26  8:43 Narayana Murty N
  2026-11-26  8:50 ` [PATCH v2 1/3] KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls Narayana Murty N
  0 siblings, 1 reply; 4+ messages in thread
From: Narayana Murty N @ 2026-11-26  8:43 UTC (permalink / raw)
  To: Ritesh Harjani, Madhavan Srinivasan, linuxppc-dev, kvm
  Cc: Nicholas Piggin, Michael Ellerman, Christophe Leroy,
	Shrikanth Hegde, Alexey Kardashevskiy, Narayana Murty N,
	linux-kernel, sbhat, harshpb

The sPAPR TCE hypercalls run in preemptible virtual mode and may race
with concurrent TCE table fd release or VFIO IOMMU group detachment.

This series addresses the parent TCE table lifetime, synchronizes IOMMU
table access with detach, and ensures safe child-table reclamation.

Changes log:
v1:
  - Revisited the Fixes tag for IOMMU table traversal and 
    used 121f80ba68f1.(Ritesh Harjani) 
  - Added parent TCE table reference counting and SRCU
    synchronization with fd release.
  - Added SRCU-protected child-table traversal and synchronization
    with VFIO detach.
  - Added SRCU-aware reclamation to prevent child-table
    use-after-free.
  - The partial IOMMU mapping rollback issue will be addressed
    in a separate follow-up patch.(Sashiko Review)

Narayana Murty N (3):
  KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls
  KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach
  KVM: PPC: Book3S: Free child IOMMU tables after an SRCU grace period

 arch/powerpc/include/asm/kvm_host.h |   2 +
 arch/powerpc/kvm/book3s_64_vio.c    | 179 +++++++++++++++++++++-------
 2 files changed, 141 insertions(+), 40 deletions(-)

-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 1/3] KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls
  2026-11-26  8:43 [PATCH v2 0/3] KVM: PPC: Book3S: Protect TCE IOMMU table walks with RCU Narayana Murty N
@ 2026-11-26  8:50 ` Narayana Murty N
  2026-11-26  8:50   ` [PATCH v2 2/3] KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach Narayana Murty N
  2026-11-26  8:50   ` [PATCH v2 3/3] KVM: PPC: Book3S: Free child IOMMU tables after an SRCU grace period Narayana Murty N
  0 siblings, 2 replies; 4+ messages in thread
From: Narayana Murty N @ 2026-11-26  8:50 UTC (permalink / raw)
  To: Ritesh Harjani, Madhavan Srinivasan, linuxppc-dev, kvm
  Cc: Nicholas Piggin, Michael Ellerman, Christophe Leroy,
	Shrikanth Hegde, Alexey Kardashevskiy, Narayana Murty N,
	linux-kernel, sbhat, harshpb

Commit cad32d9d42e8 ("KVM: PPC: Book3s: Retire H_PUT_TCE/etc real
mode handlers") moved TCE hypercalls to preemptible virtual
mode. However, kvmppc_find_table() returns a table pointer without
holding a reference, allowing concurrent fd release to free it
while a hypercall is still using it.

Add a kref to the sPAPR TCE table and acquire it under RCU during
lookup. Release the reference when the hypercall completes.

Use kvm->srcu to synchronize active table accesses with fd release
before dropping the locked-memory accounting.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 arch/powerpc/include/asm/kvm_host.h |   1 +
 arch/powerpc/kvm/book3s_64_vio.c    | 136 ++++++++++++++++++++--------
 2 files changed, 101 insertions(+), 36 deletions(-)

diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h
index 708a32f89776..13e8c5b12f40 100644
--- a/arch/powerpc/include/asm/kvm_host.h
+++ b/arch/powerpc/include/asm/kvm_host.h
@@ -198,6 +198,7 @@ struct kvmppc_spapr_tce_table {
 	struct kvm *kvm;
 	u64 liobn;
 	struct rcu_head rcu;
+	struct kref kref;
 	u32 page_shift;
 	u64 offset;		/* in pages */
 	u64 size;		/* window size in pages */
diff --git a/arch/powerpc/kvm/book3s_64_vio.c b/arch/powerpc/kvm/book3s_64_vio.c
index c5f3f5b29d89..e9370b0e9afe 100644
--- a/arch/powerpc/kvm/book3s_64_vio.c
+++ b/arch/powerpc/kvm/book3s_64_vio.c
@@ -38,9 +38,17 @@ static struct kvmppc_spapr_tce_table *kvmppc_find_table(struct kvm *kvm,
 {
 	struct kvmppc_spapr_tce_table *stt;
 
-	list_for_each_entry_lockless(stt, &kvm->arch.spapr_tce_tables, list)
-		if (stt->liobn == liobn)
+	rcu_read_lock();
+	list_for_each_entry_rcu(stt, &kvm->arch.spapr_tce_tables, list) {
+		if (stt->liobn != liobn)
+			continue;
+
+		if (kref_get_unless_zero(&stt->kref)) {
+			rcu_read_unlock();
 			return stt;
+		}
+	}
+	rcu_read_unlock();
 
 	return NULL;
 }
@@ -205,6 +213,19 @@ static void release_spapr_tce_table(struct rcu_head *head)
 	kfree(stt);
 }
 
+static void kvm_spapr_tce_table_release(struct kref *kref)
+{
+	struct kvmppc_spapr_tce_table *stt = container_of(kref,
+			struct kvmppc_spapr_tce_table, kref);
+
+	call_rcu(&stt->rcu, release_spapr_tce_table);
+}
+
+static void kvmppc_spapr_tce_table_put(struct kvmppc_spapr_tce_table *stt)
+{
+	kref_put(&stt->kref, kvm_spapr_tce_table_release);
+}
+
 static struct page *kvm_spapr_get_tce_page(struct kvmppc_spapr_tce_table *stt,
 		unsigned long sttpage)
 {
@@ -271,12 +292,20 @@ static int kvm_spapr_tce_release(struct inode *inode, struct file *filp)
 		}
 	}
 
+	/*
+	 * The table is already off kvm->arch.spapr_tce_tables, so hcalls
+	 * that start after this point cannot find it. Wait for hcalls that
+	 * already located it, which run under kvm->srcu, before removing the
+	 * accounting for its pages.
+	 */
+	synchronize_srcu(&kvm->srcu);
+
 	account_locked_vm(kvm->mm,
 		kvmppc_stt_pages(kvmppc_tce_pages(stt->size)), false);
 
-	kvm_put_kvm(stt->kvm);
+	kvmppc_spapr_tce_table_put(stt);
 
-	call_rcu(&stt->rcu, release_spapr_tce_table);
+	kvm_put_kvm(kvm);
 
 	return 0;
 }
@@ -314,6 +343,7 @@ int kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 	stt->offset = args->offset;
 	stt->size = args->size;
 	stt->kvm = kvm;
+	kref_init(&stt->kref);
 	mutex_init(&stt->alloc_lock);
 	INIT_LIST_HEAD_RCU(&stt->iommu_tables);
 
@@ -579,25 +609,27 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
 	/* udbg_printf("H_PUT_TCE(): liobn=0x%lx ioba=0x%lx, tce=0x%lx\n", */
 	/* 	    liobn, ioba, tce); */
 
+	idx = srcu_read_lock(&vcpu->kvm->srcu);
+
 	stt = kvmppc_find_table(vcpu->kvm, liobn);
-	if (!stt)
+	if (!stt) {
+		srcu_read_unlock(&vcpu->kvm->srcu, idx);
 		return H_TOO_HARD;
+	}
 
 	ret = kvmppc_ioba_validate(stt, ioba, 1);
 	if (ret != H_SUCCESS)
-		return ret;
-
-	idx = srcu_read_lock(&vcpu->kvm->srcu);
+		goto put_exit;
 
 	ret = kvmppc_tce_validate(stt, tce);
 	if (ret != H_SUCCESS)
-		goto unlock_exit;
+		goto put_exit;
 
 	dir = iommu_tce_direction(tce);
 
 	if ((dir != DMA_NONE) && kvmppc_tce_to_ua(vcpu->kvm, tce, &ua)) {
 		ret = H_PARAMETER;
-		goto unlock_exit;
+		goto put_exit;
 	}
 
 	entry = ioba >> stt->page_shift;
@@ -613,15 +645,17 @@ long kvmppc_h_put_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
 
 		if (ret != H_SUCCESS) {
 			kvmppc_clear_tce(vcpu->kvm->mm, stt, stit->tbl, entry);
-			goto unlock_exit;
+			goto put_exit;
 		}
 	}
 
 	kvmppc_tce_put(stt, entry, tce);
 
-unlock_exit:
+put_exit:
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
+	kvmppc_spapr_tce_table_put(stt);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(kvmppc_h_put_tce);
@@ -637,42 +671,49 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
 	u64 tce;
 	struct kvmppc_spapr_tce_iommu_table *stit;
 
+	idx = srcu_read_lock(&vcpu->kvm->srcu);
+
 	stt = kvmppc_find_table(vcpu->kvm, liobn);
-	if (!stt)
+	if (!stt) {
+		srcu_read_unlock(&vcpu->kvm->srcu, idx);
 		return H_TOO_HARD;
+	}
 
 	entry = ioba >> stt->page_shift;
 	/*
 	 * SPAPR spec says that the maximum size of the list is 512 TCEs
 	 * so the whole table fits in 4K page
 	 */
-	if (npages > 512)
-		return H_PARAMETER;
+	if (npages > 512) {
+		ret = H_PARAMETER;
+		goto put_exit;
+	}
 
-	if (tce_list & (SZ_4K - 1))
-		return H_PARAMETER;
+	if (tce_list & (SZ_4K - 1)) {
+		ret = H_PARAMETER;
+		goto put_exit;
+	}
 
 	ret = kvmppc_ioba_validate(stt, ioba, npages);
 	if (ret != H_SUCCESS)
-		return ret;
+		goto put_exit;
 
-	idx = srcu_read_lock(&vcpu->kvm->srcu);
 	if (kvmppc_tce_to_ua(vcpu->kvm, tce_list, &ua)) {
 		ret = H_TOO_HARD;
-		goto unlock_exit;
+		goto put_exit;
 	}
 	tces = (u64 __user *) ua;
 
 	for (i = 0; i < npages; ++i) {
 		if (get_user(tce, tces + i)) {
 			ret = H_TOO_HARD;
-			goto unlock_exit;
+			goto put_exit;
 		}
 		tce = be64_to_cpu(tce);
 
 		ret = kvmppc_tce_validate(stt, tce);
 		if (ret != H_SUCCESS)
-			goto unlock_exit;
+			goto put_exit;
 	}
 
 	for (i = 0; i < npages; ++i) {
@@ -688,13 +729,13 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
 		 */
 		if (get_user(tce, tces + i)) {
 			ret = H_TOO_HARD;
-			goto unlock_exit;
+			goto put_exit;
 		}
 		tce = be64_to_cpu(tce);
 
 		if (kvmppc_tce_to_ua(vcpu->kvm, tce, &ua)) {
 			ret = H_PARAMETER;
-			goto unlock_exit;
+			goto put_exit;
 		}
 
 		list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
@@ -705,16 +746,18 @@ long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
 			if (ret != H_SUCCESS) {
 				kvmppc_clear_tce(vcpu->kvm->mm, stt, stit->tbl,
 						 entry + i);
-				goto unlock_exit;
+				goto put_exit;
 			}
 		}
 
 		kvmppc_tce_put(stt, entry + i, tce);
 	}
 
-unlock_exit:
+put_exit:
 	srcu_read_unlock(&vcpu->kvm->srcu, idx);
 
+	kvmppc_spapr_tce_table_put(stt);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(kvmppc_h_put_tce_indirect);
@@ -724,20 +767,26 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
 		unsigned long tce_value, unsigned long npages)
 {
 	struct kvmppc_spapr_tce_table *stt;
-	long i, ret;
+	long i, ret, idx;
 	struct kvmppc_spapr_tce_iommu_table *stit;
 
+	idx = srcu_read_lock(&vcpu->kvm->srcu);
+
 	stt = kvmppc_find_table(vcpu->kvm, liobn);
-	if (!stt)
+	if (!stt) {
+		srcu_read_unlock(&vcpu->kvm->srcu, idx);
 		return H_TOO_HARD;
+	}
 
 	ret = kvmppc_ioba_validate(stt, ioba, npages);
 	if (ret != H_SUCCESS)
-		return ret;
+		goto put_exit;
 
 	/* Check permission bits only to allow userspace poison TCE for debug */
-	if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ))
-		return H_PARAMETER;
+	if (tce_value & (TCE_PCI_WRITE | TCE_PCI_READ)) {
+		ret = H_PARAMETER;
+		goto put_exit;
+	}
 
 	list_for_each_entry_lockless(stit, &stt->iommu_tables, next) {
 		unsigned long entry = ioba >> stt->page_shift;
@@ -750,7 +799,7 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
 				continue;
 
 			if (ret == H_TOO_HARD)
-				return ret;
+				goto put_exit;
 
 			WARN_ON_ONCE(1);
 			kvmppc_clear_tce(vcpu->kvm->mm, stt, stit->tbl, entry + i);
@@ -760,6 +809,11 @@ long kvmppc_h_stuff_tce(struct kvm_vcpu *vcpu,
 	for (i = 0; i < npages; ++i, ioba += (1ULL << stt->page_shift))
 		kvmppc_tce_put(stt, ioba >> stt->page_shift, tce_value);
 
+put_exit:
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+
+	kvmppc_spapr_tce_table_put(stt);
+
 	return ret;
 }
 EXPORT_SYMBOL_GPL(kvmppc_h_stuff_tce);
@@ -769,28 +823,38 @@ long kvmppc_h_get_tce(struct kvm_vcpu *vcpu, unsigned long liobn,
 {
 	struct kvmppc_spapr_tce_table *stt;
 	long ret;
+	int srcu;
 	unsigned long idx;
 	struct page *page;
 	u64 *tbl;
 
+	srcu = srcu_read_lock(&vcpu->kvm->srcu);
+
 	stt = kvmppc_find_table(vcpu->kvm, liobn);
-	if (!stt)
+	if (!stt) {
+		srcu_read_unlock(&vcpu->kvm->srcu, srcu);
 		return H_TOO_HARD;
+	}
 
 	ret = kvmppc_ioba_validate(stt, ioba, 1);
 	if (ret != H_SUCCESS)
-		return ret;
+		goto put_exit;
 
 	idx = (ioba >> stt->page_shift) - stt->offset;
 	page = stt->pages[idx / TCES_PER_PAGE];
 	if (!page) {
 		kvmppc_set_gpr(vcpu, 4, 0);
-		return H_SUCCESS;
+		goto put_exit;
 	}
 	tbl = (u64 *)page_address(page);
 
 	kvmppc_set_gpr(vcpu, 4, tbl[idx % TCES_PER_PAGE]);
 
-	return H_SUCCESS;
+put_exit:
+	srcu_read_unlock(&vcpu->kvm->srcu, srcu);
+
+	kvmppc_spapr_tce_table_put(stt);
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(kvmppc_h_get_tce);
-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 2/3] KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach
  2026-11-26  8:50 ` [PATCH v2 1/3] KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls Narayana Murty N
@ 2026-11-26  8:50   ` Narayana Murty N
  2026-11-26  8:50   ` [PATCH v2 3/3] KVM: PPC: Book3S: Free child IOMMU tables after an SRCU grace period Narayana Murty N
  1 sibling, 0 replies; 4+ messages in thread
From: Narayana Murty N @ 2026-11-26  8:50 UTC (permalink / raw)
  To: Ritesh Harjani, Madhavan Srinivasan, linuxppc-dev, kvm
  Cc: Nicholas Piggin, Michael Ellerman, Christophe Leroy,
	Shrikanth Hegde, Alexey Kardashevskiy, Narayana Murty N,
	linux-kernel, sbhat, harshpb

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@linux.ibm.com>
---
 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2 3/3] KVM: PPC: Book3S: Free child IOMMU tables after an SRCU grace period
  2026-11-26  8:50 ` [PATCH v2 1/3] KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls Narayana Murty N
  2026-11-26  8:50   ` [PATCH v2 2/3] KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach Narayana Murty N
@ 2026-11-26  8:50   ` Narayana Murty N
  1 sibling, 0 replies; 4+ messages in thread
From: Narayana Murty N @ 2026-11-26  8:50 UTC (permalink / raw)
  To: Ritesh Harjani, Madhavan Srinivasan, linuxppc-dev, kvm
  Cc: Nicholas Piggin, Michael Ellerman, Christophe Leroy,
	Shrikanth Hegde, Alexey Kardashevskiy, Narayana Murty N,
	linux-kernel, sbhat, harshpb

TCE hypercalls access child IOMMU tables under kvm->srcu, but
the entries are currently reclaimed using call_rcu(). A classic
RCU grace period does not wait for SRCU readers, allowing an
attachment to be freed while a hypercall still uses it.

Defer reclamation through call_srcu(), followed by the existing
call_rcu() callback, to protect both SRCU and classic RCU readers.

Keep the KVM pointer in each attachment to identify its SRCU
domain during deferred reclamation.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 arch/powerpc/include/asm/kvm_host.h |  1 +
 arch/powerpc/kvm/book3s_64_vio.c    | 27 ++++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/arch/powerpc/include/asm/kvm_host.h b/arch/powerpc/include/asm/kvm_host.h
index 13e8c5b12f40..28442cadcabd 100644
--- a/arch/powerpc/include/asm/kvm_host.h
+++ b/arch/powerpc/include/asm/kvm_host.h
@@ -188,6 +188,7 @@ struct kvmppc_spapr_tce_iommu_table {
 	struct rcu_head rcu;
 	struct list_head next;
 	struct iommu_table *tbl;
+	struct kvm *kvm;	/* SRCU domain of the hcalls using @tbl */
 	struct kref kref;
 };
 
diff --git a/arch/powerpc/kvm/book3s_64_vio.c b/arch/powerpc/kvm/book3s_64_vio.c
index afa7ce57f5ae..71835c58d25d 100644
--- a/arch/powerpc/kvm/book3s_64_vio.c
+++ b/arch/powerpc/kvm/book3s_64_vio.c
@@ -76,6 +76,25 @@ static void kvm_spapr_tce_iommu_table_free(struct rcu_head *head)
 	kfree(stit);
 }
 
+/*
+ * The TCE hcalls walk stt->iommu_tables and program stit->tbl under
+ * kvm->srcu, while kvmppc_tce_validate() and the attach/detach paths use
+ * classic RCU. call_rcu() alone does not wait for SRCU readers, so wait
+ * for kvm->srcu first and then for a classic RCU grace period.
+ */
+static void kvm_spapr_tce_iommu_table_srcu_free(struct rcu_head *head)
+{
+	struct kvmppc_spapr_tce_iommu_table *stit = container_of(head,
+			struct kvmppc_spapr_tce_iommu_table, rcu);
+
+	call_rcu(&stit->rcu, kvm_spapr_tce_iommu_table_free);
+}
+
+/*
+ * May run under rcu_read_lock(), so it must not sleep. kvm->srcu stays
+ * valid until the callback has run: every caller holds a reference on the
+ * VM, and kvm_destroy_vm() calls srcu_barrier() before cleanup_srcu_struct().
+ */
 static void kvm_spapr_tce_liobn_put(struct kref *kref)
 {
 	struct kvmppc_spapr_tce_iommu_table *stit = container_of(kref,
@@ -83,7 +102,8 @@ static void kvm_spapr_tce_liobn_put(struct kref *kref)
 
 	list_del_rcu(&stit->next);
 
-	call_rcu(&stit->rcu, kvm_spapr_tce_iommu_table_free);
+	call_srcu(&stit->kvm->srcu, &stit->rcu,
+		  kvm_spapr_tce_iommu_table_srcu_free);
 }
 
 void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
@@ -117,6 +137,10 @@ void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
 	 * 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.
+	 *
+	 * This must be unconditional: the attachment may already have been
+	 * unlinked by a concurrent close of the TCE table fd, which waits for
+	 * kvm->srcu on its own but may not have finished yet.
 	 */
 	synchronize_srcu(&kvm->srcu);
 }
@@ -200,6 +224,7 @@ long kvm_spapr_tce_attach_iommu_group(struct kvm *kvm, int tablefd,
 	}
 
 	stit->tbl = tbl;
+	stit->kvm = kvm;
 	kref_init(&stit->kref);
 
 	list_add_rcu(&stit->next, &stt->iommu_tables);
-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-26  8:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  8:43 [PATCH v2 0/3] KVM: PPC: Book3S: Protect TCE IOMMU table walks with RCU Narayana Murty N
2026-11-26  8:50 ` [PATCH v2 1/3] KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls Narayana Murty N
2026-11-26  8:50   ` [PATCH v2 2/3] KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach Narayana Murty N
2026-11-26  8:50   ` [PATCH v2 3/3] KVM: PPC: Book3S: Free child IOMMU tables after an SRCU grace period Narayana Murty N

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®