Skip to contentSkip to conversation

KVM: PPC: Book3S: Protect TCE IOMMU table walks with RCU

PATCH New Narayana Murty N 2 participants 6 messages Nov 26

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover KVM: PPC: Book3S: Protect TCE IOMMU table walks with RCU

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(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/3 KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls

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.

---
 arch/powerpc/include/asm/kvm_host.h |   1 +
 arch/powerpc/kvm/book3s_64_vio.c    | 136 ++++++++++++++++++++--------
 2 files changed, 101 insertions(+), 36 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
index 708a32f89776..13e8c5b12f40 100644
@@ -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 */
arch/powerpc/kvm/book3s_64_vio.c +100 -36

305 lines not on this page · raw message

Signature · 1 line
2.54.0
#4 lore raw

PATCH v2 3/3 KVM: PPC: Book3S: Free child IOMMU tables after an SRCU grace period

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.

---
 arch/powerpc/include/asm/kvm_host.h |  1 +
 arch/powerpc/kvm/book3s_64_vio.c    | 27 ++++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
index 13e8c5b12f40..28442cadcabd 100644
@@ -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;
 };
 
arch/powerpc/kvm/book3s_64_vio.c +26 -1
index afa7ce57f5ae..71835c58d25d 100644
@@ -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);
Signature · 1 line
2.54.0
#2 lore raw

PATCH v2 2/3 KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach

In reply to Narayana Murty N

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.

---
 arch/powerpc/kvm/book3s_64_vio.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
arch/powerpc/kvm/book3s_64_vio.c +13 -3
index e9370b0e9afe..afa7ce57f5ae 100644
@@ -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) {
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH v2 2/3] KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: PPC: Book3S: missing Fixes trailer for TCE hcall detach race fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: PPC: Book3S: missing Fixes trailer for TCE hcall detach race fix
--

commit bff0a1814a06b4153499fc640facd0cd8bd3623e
Author: Narayana Murty N <[email protected]>

Quoted 9 lines

KVM: PPC: Book3S: Synchronize TCE hcalls with IOMMU table detach

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.

Quoted 7 lines

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.

[Severity: Low]
This fixes a race between in-flight TCE hypercalls and IOMMU group detach introduced in 121f80ba68f1 ("KVM: PPC: VFIO: Add in-kernel acceleration for VFIO"). Should this include:

Fixes 121f80ba68f1 ("KVM: PPC: VFIO: Add in-kernel acceleration for VFIO")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH v2 1/3] KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls

In reply to Narayana Murty N
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: PPC: Book3S: missing Fixes trailer for commit cad32d9d42e8 --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: PPC: Book3S: missing Fixes trailer for commit cad32d9d42e8 --

commit e9c775c6ce258aa52807c78d9ed640f4c2596501
Author: Narayana Murty N <[email protected]>

Quoted 8 lines

KVM: PPC: Book3S: Hold a reference to sPAPR TCE tables during hcalls

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.

Quoted 7 lines

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.

[Severity: Low]
This fixes a use-after-free introduced when TCE hypercalls were moved to preemptible virtual mode. Should this include:

Fixes cad32d9d42e8 ("KVM: PPC: Book3s: Retire H_PUT_TCE/etc real mode handlers")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1