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 <[email protected]>
---
 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


Reply via email to