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


Reply via email to