When unmapping guest ranges, each individual leaf DAT entry is invalidated. When unmapping large ranges, this results in a very large amount of invalidations, which is time consuming. Moreover, since the unmap callback is called by KVM while holding a spinlock, this also leads to stall warnings. Improve the situation by invalidating DAT entries higher in the tree, instead of each leaf individually. For small invalidations (up to 1 segment = 1M of guest memory), and special cases (like protected VMs) regardless of the size, the old and slow invalidation is performed; all leaf entries are invalidated one by one. For large invalidations (larger than 1M of guest memory), the new strategy is used: - Invalidate prefixes on the whole range, extending start and end to 1M granularity - Iterate through the page table pointed to by each pmd and unshadow entries as needed - Detach (invalidate) the pmd - Save storage keys as needed - Invalidate each (now detached) pte by simply overwriting it - Reattach the old pmd to point to the updated page table Fixes: a2c17f9270cc ("KVM: s390: New gmap code") Signed-off-by: Claudio Imbrenda Cc: stable@vger.kernel.org # 7.0+ --- arch/s390/kvm/gmap/gmap.c | 136 +++++++++++++++++++++++++++++++++++--- 1 file changed, 125 insertions(+), 11 deletions(-) diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c index 225fdae3d57c..04f3ab03a041 100644 --- a/arch/s390/kvm/gmap/gmap.c +++ b/arch/s390/kvm/gmap/gmap.c @@ -434,17 +434,8 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct return 0; } -/** - * gmap_unmap_gfn_range() - Unmap a range of guest addresses. - * @gmap: The gmap to act on. - * @slot: The memslot in which the range is located. - * @start: The first gfn to unmap. - * @end: The gfn after the last one to unmap. - * - * Context: Called with the kvm mmu write lock held. - * Return: false - */ -bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +static bool gmap_unmap_gfn_range_slow(struct gmap *gmap, struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) { const struct dat_walk_ops ops = { .pte_entry = _gmap_unmap_pte, @@ -462,6 +453,129 @@ bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t return false; } +static long _gmap_unmap_pud_fast(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *w) +{ + union crste old = *crstep; + + /* We only care about large puds */ + if (!old.h.fc) + return 0; + /* + * The caller did not unmap prefixes outside the original range, which + * does not necessarily cover the whole pud. + * Since all prefixes are below 2G, the first pud covers them all; + * make sure all prefixes are unmapped whenever the first pud is being + * unmapped. + */ + if (!gfn) + gmap_unmap_prefix(w->priv, 0, next); + if (old.s.fc1.vsie_notif) + gmap_handle_vsie_unshadow_event(w->priv, gfn); + + if (machine_has_tlb_guest()) + idte_crste(crstep, gfn, IDTE_GUEST_ASCE, w->asce, 0); + else + idte_crste(crstep, gfn, 0, w->asce, 0); + *crstep = _CRSTE_EMPTY(old.h.tt); + + return 0; +} + +static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *w) +{ + union crste old = *crstep; + struct page_table *pt; + gfn_t first, last; + unsigned int i; + + if (old.h.fc) { + if (old.s.fc1.vsie_notif) + gmap_handle_vsie_unshadow_event(w->priv, gfn); + + if (machine_has_tlb_guest()) + idte_crste(crstep, gfn, IDTE_GUEST_ASCE, w->asce, 0); + else + idte_crste(crstep, gfn, 0, w->asce, 0); + WRITE_ONCE(*crstep, _CRSTE_EMPTY(old.h.tt)); + + return 0; + } + + if (old.h.i) + return 0; + + pt = dereference_pmd(old.pmd); + first = max(gfn, w->start) & (PTRS_PER_PTE - 1); + last = (min(next, w->end) - 1) & (PTRS_PER_PTE - 1); + + for (i = first; i <= last; i++) + if (pte_needs_unshadow(pt->ptes[i], _PTE_EMPTY, pt->pgstes[i])) + gmap_handle_vsie_unshadow_event(w->priv, gfn + i); + + /* Detach the page table */ + if (machine_has_tlb_guest()) + idte_crste(crstep, gfn, IDTE_GUEST_ASCE, w->asce, 0); + else + idte_crste(crstep, gfn, 0, w->asce, 0); + + for (i = first; i <= last; i++) { + if (pte_hole(pt->ptes[i])) + continue; + if (!pt->ptes[i].h.i && uses_skeys(w->priv)) + pt->pgstes[i] = dat_save_storage_key_into_pgste(pt->ptes[i], pt->pgstes[i]); + pt->ptes[i] = _PTE_EMPTY; + pt->pgstes[i].vsie_notif = 0; + pt->pgstes[i].vsie_gmem = 0; + pt->pgstes[i].zero = 0; + pt->pgstes[i].prefix_notif = 0; + } + + barrier(); + /* Reattach the modified page table */ + WRITE_ONCE(*crstep, old); + + return 0; +} + +static bool gmap_unmap_gfn_range_fast(struct gmap *gmap, gfn_t start, gfn_t end) +{ + const struct dat_walk_ops ops = { + .pud_entry = _gmap_unmap_pud_fast, + .pmd_entry = _gmap_unmap_pmd_fast, + }; + gfn_t aligned_start = ALIGN_DOWN(start, _PAGE_ENTRIES); + gfn_t aligned_end = ALIGN(end, _PAGE_ENTRIES); + bool failed; + + lockdep_assert_held_write(&gmap->kvm->mmu_lock); + + /* Prefixes are below 2G; if the start is above 2G, nothing to do */ + if (aligned_start < _REGION3_SIZE) + gmap_unmap_prefix(gmap, aligned_start, aligned_end); + + failed = _dat_walk_gfn_range(start, end, gmap->asce, &ops, DAT_WALK_IGN_HOLES, gmap); + KVM_BUG_ON(failed, gmap->kvm); + return false; +} + +/** + * gmap_unmap_gfn_range() - Unmap a range of guest addresses. + * @gmap: The gmap to act on. + * @slot: The memslot in which the range is located. + * @start: The first gfn to unmap. + * @end: The gfn after the last one to unmap. + * + * Context: Called with the kvm mmu write lock held. + * Return: false + */ +bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + if ((end - start <= _PAGES_PER_SEGMENT) || + test_bit(GMAP_FLAG_EXPORT_ON_UNMAP, &gmap->flags) || is_ucontrol(gmap)) + return gmap_unmap_gfn_range_slow(gmap, slot, start, end); + return gmap_unmap_gfn_range_fast(gmap, start, end); +} + static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste pgste, gfn_t gfn, struct gmap *gmap) { -- 2.56.0