When a guest-originated page fault happens, if CMMA is enabled, and if the fault looks like it is the first fault in the segment, allocate only the page table, and do not attempt to also actually fault-in the memory. Whether the fault is the first one is determined by checking that the gmap tables have neither page tables nor large pages for the faulting address. For a CMMA-enabled guest, the first fault in a segment is almost surely caused by the guest running ESSA. Once the page table is in place, even if all the entries within are not present, ESSA will not cause any further page faults. If the origin of the fault was not ESSA, a new DAT fault will occur immediately, which will then go through the normal path (since the ptes are now reachable) and actually fault-in the memory. Without this fix, ESSA will cause a fault on the first page of each segment (1M block). If userspace is backing the guest with huge pages (hugetlbfs or THP), the page fault will be resolved by faulting in the whole huge page. The result is that, at guest startup, the whole guest memory is faulted in, since the guest will usually mark all of its memory at boot using ESSA. With this fix, no extra guest memory is allocated. This will also prevent the first page in each segment from being faulted in, regardless of hugepages. Fixes: e38c884df921 ("KVM: s390: Switch to new gmap") Cc: stable@vger.kernel.org Signed-off-by: Claudio Imbrenda --- arch/s390/kvm/gmap/dat.h | 1 + arch/s390/kvm/gmap/faultin.c | 21 +++++++++++++++++++-- arch/s390/kvm/s390/s390.c | 1 + 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/s390/kvm/gmap/dat.h b/arch/s390/kvm/gmap/dat.h index 79efc56710b7..d273956b3eb7 100644 --- a/arch/s390/kvm/gmap/dat.h +++ b/arch/s390/kvm/gmap/dat.h @@ -522,6 +522,7 @@ struct guest_fault { bool attempt_pfault; /* Attempt a pfault first */ bool valid; /* This entry contains valid data */ bool crste_region3; /* Whether crstep refers to a region3 entry */ + bool guest_originated; /* Fault event actually generated by guest operation */ void (*callback)(struct guest_fault *f); void *priv; }; diff --git a/arch/s390/kvm/gmap/faultin.c b/arch/s390/kvm/gmap/faultin.c index 2e93bd563d5c..8d0e86943b23 100644 --- a/arch/s390/kvm/gmap/faultin.c +++ b/arch/s390/kvm/gmap/faultin.c @@ -42,8 +42,8 @@ int kvm_s390_faultin_gfn(struct kvm_vcpu *vcpu, struct kvm *kvm, struct guest_fa struct kvm_s390_mmu_cache *mc = NULL; struct kvm_memory_slot *slot; unsigned long inv_seq; - int rc = -EAGAIN; - int foll; + bool cmm_first_fault; + int foll, rc; foll = f->write_attempt ? FOLL_WRITE : 0; foll |= f->attempt_pfault ? FOLL_NOWAIT : 0; @@ -58,6 +58,8 @@ int kvm_s390_faultin_gfn(struct kvm_vcpu *vcpu, struct kvm *kvm, struct guest_fa scoped_guard(read_lock, &kvm->mmu_lock) { if (gmap_try_fixup_minor(kvm->arch.gmap, f) == 0) return 0; + cmm_first_fault = uses_cmm(kvm->arch.gmap) && f->guest_originated && !f->ptep && + f->crstep && crste_none(READ_ONCE(*f->crstep)); } if (!mc) { @@ -67,6 +69,21 @@ int kvm_s390_faultin_gfn(struct kvm_vcpu *vcpu, struct kvm *kvm, struct guest_fa mc = local_mc; } + /* + * For guest-originated faults when CMMA is active, if no leaf + * tables were present, allocate the page table only, do not also + * fault-in the memory. + */ + if (cmm_first_fault) { + scoped_guard(read_lock, &kvm->mmu_lock) { + rc = dat_entry_walk(mc, f->gfn, kvm->arch.gmap->asce, DAT_WALK_ALLOC, + TABLE_TYPE_PAGE_TABLE, &f->crstep, &f->ptep); + if (!rc) + return 0; + } + } + + rc = -EAGAIN; while (rc == -EAGAIN) { f->valid = false; inv_seq = kvm->mmu_invalidate_seq; diff --git a/arch/s390/kvm/s390/s390.c b/arch/s390/kvm/s390/s390.c index 09c43770296d..dcb4446301a2 100644 --- a/arch/s390/kvm/s390/s390.c +++ b/arch/s390/kvm/s390/s390.c @@ -4614,6 +4614,7 @@ static int vcpu_dat_fault_handler(struct kvm_vcpu *vcpu, gpa_t gaddr, bool wr) struct guest_fault f = { .write_attempt = wr, .attempt_pfault = pfault_enabled(vcpu->arch.gmap), + .guest_originated = true, }; int rc; -- 2.56.1