From: Christian Borntraeger Shadow gmaps map host frames directly and do not hold references to them; they rely on vsie unshadow events from the parent gmap to stay consistent. When a memslot is deleted or moved, dat_delete_slot() removes the parent entries with the raw exchange helpers, which do not raise unshadow events, and kvm_arch_flush_shadow_memslot() is empty. Once userspace unmaps the memory of the old slot, the mmu notifier finds no memslot and does nothing, the host frames are freed, and existing shadow gmaps still map them. A nested guest (guest 3) can then read and write freed host memory. Remove all shadow gmaps right after the parent entries of the old slot have been removed, still under kvm->mmu_lock and before the ioctl returns, so before userspace can unmap the memory. Memslot deletion is rare; vSIE recreates shadows on demand. Fixes: e38c884df921 ("KVM: s390: Switch to new gmap") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Christian Borntraeger Signed-off-by: Claudio Imbrenda --- arch/s390/kvm/gmap/gmap.c | 21 +++++++++++++++++++++ arch/s390/kvm/gmap/gmap.h | 1 + arch/s390/kvm/gmap/kvm_mmu.c | 10 +++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c index 79eae7e4dac6..505684ebebb3 100644 --- a/arch/s390/kvm/gmap/gmap.c +++ b/arch/s390/kvm/gmap/gmap.c @@ -1412,6 +1412,27 @@ static void gmap_unshadow(struct gmap *sg) gmap_put(sg); } +/** + * gmap_unshadow_all() - Remove all shadow gmaps of a parent gmap. + * @parent: The parent gmap. + * + * Shadow gmaps map host frames directly and do not hold references to them. + * Use this when parent gmap entries are removed without raising vsie + * unshadow events, e.g. when a memslot is deleted or moved. + * + * Context: Takes parent->children_lock; kvm->mmu_lock may be held. + */ +void gmap_unshadow_all(struct gmap *parent) +{ + struct gmap *sg, *next; + + guard(spinlock)(&parent->children_lock); + list_for_each_entry_safe(sg, next, &parent->children, list) { + if (is_shadow(sg)) + gmap_unshadow(sg); + } +} + void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn) { struct vsie_rmap *rmap, *rnext, *head; diff --git a/arch/s390/kvm/gmap/gmap.h b/arch/s390/kvm/gmap/gmap.h index 8f47f29c89fd..eeb57493a649 100644 --- a/arch/s390/kvm/gmap/gmap.h +++ b/arch/s390/kvm/gmap/gmap.h @@ -110,6 +110,7 @@ int gmap_insert_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn, gfn_t r_gfn, kvm_pfn_t pfn, int level, bool wr); void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn); +void gmap_unshadow_all(struct gmap *parent); struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, union asce asce, int edat_level); void gmap_split_huge_pages(struct gmap *gmap); diff --git a/arch/s390/kvm/gmap/kvm_mmu.c b/arch/s390/kvm/gmap/kvm_mmu.c index 4e2da60b9859..cea38de7dcb1 100644 --- a/arch/s390/kvm/gmap/kvm_mmu.c +++ b/arch/s390/kvm/gmap/kvm_mmu.c @@ -105,8 +105,16 @@ int s390_kvm_mmu_prepare_memory_region(struct kvm *kvm, } scoped_guard(write_lock, &kvm->mmu_lock) { kvm_s390_update_cmma_dirty(kvm, old); - if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) + if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { rc = dat_delete_slot(kvm->arch.gmap->asce, old->base_gfn, old->npages); + /* + * The parent entries were removed without vsie unshadow + * events, and shadow gmaps do not hold references to + * the host frames they map. Drop all shadows before + * userspace can unmap the memory of the old slot. + */ + gmap_unshadow_all(kvm->arch.gmap); + } if (!rc && (change == KVM_MR_MOVE || change == KVM_MR_CREATE)) rc = dat_create_slot(mc, kvm->arch.gmap->asce, new->base_gfn, new->npages); } -- 2.56.1