For UCONTROL VMs, when a vCPU segment entry is mapped but unallocated, the tl field was set to 1. This collides with the present bit, which means that other parts of gmap would mistake the entry for a present one. Refactor the code to use a different combination of flags to indicate a mapped but unallocated segment. The new flags combination will result in several functions returning -EFAULT when used on UCONTROL guests. This is fine, since those interfaces should not be used for UCONTROL guests to begin with. Proper fencing of the interfaces that are not allowed for UCONTROL guests is outside the scope of this patch. Fixes: a2c17f9270cc ("KVM: s390: New gmap code") Signed-off-by: Claudio Imbrenda Cc: stable@vger.kernel.org # 7.0+ --- arch/s390/kvm/gmap/dat.h | 16 +++++++++++- arch/s390/kvm/gmap/gmap.c | 54 +++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/arch/s390/kvm/gmap/dat.h b/arch/s390/kvm/gmap/dat.h index 90389d47ba4e..4665c1112bb9 100644 --- a/arch/s390/kvm/gmap/dat.h +++ b/arch/s390/kvm/gmap/dat.h @@ -109,6 +109,8 @@ union pte { #define _REGION3_FR_MASK (_REGION3_MASK >> PAGE_SHIFT) #define _PAGES_PER_SEGMENT _PAGE_ENTRIES #define _PAGES_PER_REGION3 (_PAGES_PER_SEGMENT * _CRST_ENTRIES) +#define _UCAS_ENTRY_LENGTH 0 +#define _UCAS_ENTRY_OFFSET 3 /* Soft dirty, needed as macro for atomic operations on ptes */ #define _PAGE_SD 0x002 @@ -421,6 +423,17 @@ static inline union crste _crste_fc0(kvm_pfn_t pfn, int tt) return res; } +static inline union crste _crste_ucas(kvm_pfn_t pfn) +{ + union crste res = { .val = PFN_PHYS(pfn) }; + + res.h.i = 1; + res.h.tt = TABLE_TYPE_SEGMENT; + res.h.fc0.tl = _UCAS_ENTRY_LENGTH; + res.h.fc0.tf = _UCAS_ENTRY_OFFSET; + return res; +} + /** * _crste() - Useful constructor for union crste with FC=1 * @pfn: the pfn this pte should point to. @@ -985,7 +998,8 @@ static inline int dat_create_slot(struct kvm_s390_mmu_cache *mc, union asce asce static inline bool crste_is_ucas(union crste crste) { - return is_pmd(crste) && crste.h.i && crste.h.fc0.tl == 1 && crste.h.fc == 0; + return is_pmd(crste) && crste.h.i && !crste.h.fc && + crste.h.fc0.tl == _UCAS_ENTRY_LENGTH && crste.h.fc0.tf == _UCAS_ENTRY_OFFSET; } #endif /* ARCH_KVM_GMAP_DAT_H */ diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c index bf91e3d5013b..225fdae3d57c 100644 --- a/arch/s390/kvm/gmap/gmap.c +++ b/arch/s390/kvm/gmap/gmap.c @@ -777,16 +777,14 @@ static int gmap_ucas_map_one(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, if (rc) return rc; if (!ptep) { - newcrste = _crste_fc0(p_gfn, TABLE_TYPE_SEGMENT); - newcrste.h.i = 1; - newcrste.h.fc0.tl = 1; + newcrste = _crste_ucas(p_gfn); } else { pt = pte_table_start(ptep); dat_set_ptval(pt, PTVAL_VMADDR, p_gfn >> (_SEGMENT_SHIFT - PAGE_SHIFT)); newcrste = _crste_fc0(virt_to_pfn(pt), TABLE_TYPE_SEGMENT); } - rc = dat_entry_walk(mc, c_gfn, gmap->asce, DAT_WALK_ALLOC, TABLE_TYPE_SEGMENT, - &crstep, &ptep); + rc = dat_entry_walk(mc, c_gfn, gmap->asce, DAT_WALK_ALLOC | DAT_WALK_IGN_HOLES, + TABLE_TYPE_SEGMENT, &crstep, &ptep); if (rc) return rc; do { @@ -802,8 +800,9 @@ static int gmap_ucas_translate_simple(struct gmap *gmap, gpa_t *gaddr, union crs union pte *ptep; int rc; - rc = dat_entry_walk(NULL, gpa_to_gfn(*gaddr), gmap->asce, DAT_WALK_CONTINUE, - TABLE_TYPE_SEGMENT, crstepp, &ptep); + rc = dat_entry_walk(NULL, gpa_to_gfn(*gaddr), gmap->asce, + DAT_WALK_IGN_HOLES | DAT_WALK_ANY, + TABLE_TYPE_PAGE_TABLE, crstepp, &ptep); if (rc || (!ptep && !crste_is_ucas(**crstepp))) return -EREMOTE; if (!ptep) @@ -840,26 +839,24 @@ int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t if (rc <= 0) return rc; } - do { - scoped_guard(write_lock, &gmap->kvm->mmu_lock) { - rc = gmap_ucas_translate_simple(gmap, gaddr, &crstep); - if (rc <= 0) - return rc; - translated_address = (*gaddr & ~_SEGMENT_MASK) | - (crstep->val & _SEGMENT_MASK); - rc = gmap_ucas_map_one(mc, gmap, gpa_to_gfn(translated_address), gfn, true); - } - if (!rc) { - *gaddr = translated_address; - return 0; - } - if (rc != -ENOMEM) - return -EREMOTE; - rc = kvm_s390_mmu_cache_topup(mc); - if (rc) + rc = kvm_s390_mmu_cache_topup(mc); + if (rc) + return rc; + + scoped_guard(write_lock, &gmap->kvm->mmu_lock) { + rc = gmap_ucas_translate_simple(gmap, gaddr, &crstep); + if (rc <= 0) return rc; - } while (1); - return 0; + translated_address = (*gaddr & ~_SEGMENT_MASK) | + (crstep->val & _SEGMENT_MASK); + rc = gmap_ucas_map_one(mc, gmap, gpa_to_gfn(translated_address), gfn, true); + } + + if (!rc) + *gaddr = translated_address; + if (!rc || rc == -ENOMEM) + return rc; + return -EREMOTE; } int gmap_ucas_map(struct gmap *gmap, gfn_t p_gfn, gfn_t c_gfn, unsigned long count) @@ -896,8 +893,9 @@ static void gmap_ucas_unmap_one(struct gmap *gmap, gfn_t c_gfn) union pte *ptep; int rc; - rc = dat_entry_walk(NULL, c_gfn, gmap->asce, 0, TABLE_TYPE_SEGMENT, &crstep, &ptep); - if (rc) + rc = dat_entry_walk(NULL, c_gfn, gmap->asce, DAT_WALK_IGN_HOLES, + TABLE_TYPE_SEGMENT, &crstep, &ptep); + if (rc || !is_pmd(*crstep)) return; while (!dat_crstep_xchg_atomic(crstep, READ_ONCE(*crstep), _PMD_EMPTY, c_gfn, gmap->asce)) ; -- 2.56.0