Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: linux-s390@vger.kernel.org, borntraeger@de.ibm.com,
	frankja@linux.ibm.com, david@kernel.org, nrb@linux.ibm.com,
	gra@linux.ibm.com, seiden@linux.ibm.com
Subject: [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled
Date: Fri,  4 Dec 2026 17:37:31 +0100	[thread overview]
Message-ID: <20261204163735.237528-2-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20261204163735.237528-1-imbrenda@linux.ibm.com>

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 <imbrenda@linux.ibm.com>
---
 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


  reply	other threads:[~2026-12-04 16:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:37 [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4 Claudio Imbrenda
2026-12-04 16:37 ` Claudio Imbrenda [this message]
2026-12-04 17:06   ` [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled sashiko-bot
2026-12-04 16:37 ` [PATCH v1 2/5] KVM: s390: Fix race in handle_essa() Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 3/5] KVM: s390: Fix race in _do_shadow_crste() Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 4/5] KVM: s390: Fix _crste_fc1() Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 5/5] KVM: s390: Fix dat_split_crste() Claudio Imbrenda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204163735.237528-2-imbrenda@linux.ibm.com \
    --to=imbrenda@linux.ibm.com \
    --cc=borntraeger@de.ibm.com \
    --cc=david@kernel.org \
    --cc=frankja@linux.ibm.com \
    --cc=gra@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=nrb@linux.ibm.com \
    --cc=seiden@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox