Introduce gmap_allocate_all_page_tables_best_effort() to attempt to allocate all page tables of a running guest. This means that all hugepages will be mapped with 4k pages regardless of the hpage parameter. Existing mappings with large pages will be split. If any memory allocation fails, ignore the error and give up. This is only a best effort. Signed-off-by: Claudio Imbrenda Cc: stable@vger.kernel.org # 7.0+: prerequisite for the next patch --- arch/s390/kvm/gmap/gmap.c | 37 +++++++++++++++++++++++++++++++++++++ arch/s390/kvm/gmap/gmap.h | 1 + 2 files changed, 38 insertions(+) diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c index 04f3ab03a041..3ff08232cbb3 100644 --- a/arch/s390/kvm/gmap/gmap.c +++ b/arch/s390/kvm/gmap/gmap.c @@ -1524,3 +1524,40 @@ struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare } return new; } + +/* Bail out for any allocation failure. This is only a best effort to allocate all page tables. */ +void gmap_allocate_all_page_tables_best_effort(struct gmap *gmap) +{ + struct kvm_s390_mmu_cache *mc __free(kvm_s390_mmu_cache) = NULL; + struct kvm_memory_slot *ms; + struct kvm_memslots *slots; + gfn_t gfn, start, end; + union crste *crstep; + union pte *ptep; + int bkt; + + mc = kvm_s390_new_mmu_cache(); + if (!mc) + return; + + guard(srcu)(&gmap->kvm->srcu); + + slots = kvm_memslots(gmap->kvm); + if (!slots || kvm_memslots_empty(slots)) + return; + + kvm_for_each_memslot(ms, bkt, slots) { + start = ALIGN(ms->base_gfn, _PAGE_ENTRIES); + end = ALIGN_DOWN(ms->base_gfn + ms->npages, _PAGE_ENTRIES); + + for (gfn = start; gfn < end; gfn += _PAGE_ENTRIES) { + if (kvm_s390_mmu_cache_topup(mc)) + break; + + guard(write_lock)(&gmap->kvm->mmu_lock); + + dat_entry_walk(mc, gfn, gmap->asce, DAT_WALK_ALLOC | DAT_WALK_SPLIT, + TABLE_TYPE_PAGE_TABLE, &crstep, &ptep); + } + } +} diff --git a/arch/s390/kvm/gmap/gmap.h b/arch/s390/kvm/gmap/gmap.h index 8f47f29c89fd..9b5d394e9146 100644 --- a/arch/s390/kvm/gmap/gmap.h +++ b/arch/s390/kvm/gmap/gmap.h @@ -113,6 +113,7 @@ void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn); 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); +void gmap_allocate_all_page_tables_best_effort(struct gmap *gmap); static inline bool uses_skeys(struct gmap *gmap) { -- 2.56.0