* [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4
@ 2026-12-04 16:37 Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled Claudio Imbrenda
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: Claudio Imbrenda @ 2026-12-04 16:37 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, gra, seiden
Builds on top of my previous series "KVM: s390: More fixes"
When the guest is using CMMA, only allocate the pte tables, do not
fault-in the memory at the first fault in the segment (1M block).
This fix was postponed from the previous series because it was not
ready.
Fix a few other small but important issues, including some races and
stray bits in page tables that were not ignored as they should have.
I am afraid this might not even be the last series of fixes for 7.4.
Claudio Imbrenda (5):
KVM: s390: Only allocate page table when cmma is enabled
KVM: s390: Fix race in handle_essa()
KVM: s390: Fix race in _do_shadow_crste()
KVM: s390: Fix _crste_fc1()
KVM: s390: Fix dat_split_crste()
arch/s390/kvm/gmap/dat.c | 1 +
arch/s390/kvm/gmap/dat.h | 3 ++-
arch/s390/kvm/gmap/faultin.c | 21 +++++++++++++++++++--
arch/s390/kvm/s390/gaccess.c | 2 ++
arch/s390/kvm/s390/priv.c | 34 ++++++++++++++++++----------------
arch/s390/kvm/s390/s390.c | 1 +
6 files changed, 43 insertions(+), 19 deletions(-)
--
2.56.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled
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
2026-12-04 17:06 ` sashiko-bot
2026-12-04 16:37 ` [PATCH v1 2/5] KVM: s390: Fix race in handle_essa() Claudio Imbrenda
` (3 subsequent siblings)
4 siblings, 1 reply; 7+ messages in thread
From: Claudio Imbrenda @ 2026-12-04 16:37 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, gra, seiden
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
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v1 2/5] KVM: s390: Fix race in handle_essa()
2026-12-04 16:37 [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4 Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled Claudio Imbrenda
@ 2026-12-04 16:37 ` Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 3/5] KVM: s390: Fix race in _do_shadow_crste() Claudio Imbrenda
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Claudio Imbrenda @ 2026-12-04 16:37 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, gra, seiden
The check for migration mode was performed without locks. This can
obviously lead to races.
Fix the races by checking for migration mode again after taking the
appropriate lock.
Fixes: 6cfd47f91f6a ("KVM: s390: Fix cmma dirty tracking")
Cc: stable@vger.kernel.org # depends on "KVM: s390: Small miscellaneous CMMA fixes"
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/s390/priv.c | 34 ++++++++++++++++++----------------
1 file changed, 18 insertions(+), 16 deletions(-)
diff --git a/arch/s390/kvm/s390/priv.c b/arch/s390/kvm/s390/priv.c
index 4b590c09240c..4bde27555a87 100644
--- a/arch/s390/kvm/s390/priv.c
+++ b/arch/s390/kvm/s390/priv.c
@@ -1240,31 +1240,33 @@ static int handle_essa(struct kvm_vcpu *vcpu)
: ESSA_SET_STABLE_IF_RESIDENT))
return kvm_s390_inject_program_int(vcpu, PGM_SPECIFICATION);
+ if (!uses_cmm(vcpu->arch.gmap))
+ set_bit(GMAP_FLAG_USES_CMM, &vcpu->arch.gmap->flags);
+
+ /* Racy best-effort check, needs to be checked again properly with the lock */
if (!READ_ONCE(vcpu->kvm->arch.migration_mode)) {
/*
* CMMA is enabled in the KVM settings, but is disabled in
- * the SIE block and in the gmap, and we are not doing a
- * migration. Enable CMMA in the gmap.
- */
- set_bit(GMAP_FLAG_USES_CMM, &vcpu->arch.gmap->flags);
- /*
- * If we are here, we are supposed to have CMMA enabled in
- * the SIE block. Enabling CMMA works on a per-CPU basis,
- * while the GMAP_FLAG_USES_CMM flag is per gmap.
- * It's possible that the gmap flag is enabled and the
- * SIE flag is not, so we set the flag always; if it was
- * already set, nothing changes, otherwise we enable it
- * on this CPU too.
+ * the SIE block. Enable CMMA in the SIE block if not in
+ * migration mode. In case of races, the ECB2_CMMA bit will
+ * be cleared again when KVM_REQ_START_MIGRATION is
+ * processed, before entering SIE again.
*/
- vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
+ if (!(vcpu->arch.sie_block->ecb2 & ECB2_CMMA))
+ vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
+
/* Retry the ESSA instruction */
kvm_s390_retry_instr(vcpu);
} else {
- set_bit(GMAP_FLAG_USES_CMM, &vcpu->arch.gmap->flags);
-
- scoped_guard(mutex, &vcpu->kvm->slots_arch_lock)
+ scoped_guard(mutex, &vcpu->kvm->slots_arch_lock) {
+ if (!vcpu->kvm->arch.migration_mode) {
+ /* Retry the ESSA instruction */
+ kvm_s390_retry_instr(vcpu);
+ return 0;
+ }
scoped_guard(read_lock, &vcpu->kvm->mmu_lock)
i = __do_essa(vcpu, orc);
+ }
if (i < 0)
return i;
/* Account for the possible extra cbrl entry */
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v1 3/5] KVM: s390: Fix race in _do_shadow_crste()
2026-12-04 16:37 [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4 Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 2/5] KVM: s390: Fix race in handle_essa() Claudio Imbrenda
@ 2026-12-04 16:37 ` 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
4 siblings, 0 replies; 7+ messages in thread
From: Claudio Imbrenda @ 2026-12-04 16:37 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, gra, seiden
The compare-and-swap loop in _do_shadow_crste() does not check whether
the large page being shadowed is still large. This can lead to a stale
entry being installed in the host gmap.
Fix by checking if the entry is large, return -EAGAIN otherwise.
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/s390/gaccess.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/arch/s390/kvm/s390/gaccess.c b/arch/s390/kvm/s390/gaccess.c
index 158af3415e2d..505390a80786 100644
--- a/arch/s390/kvm/s390/gaccess.c
+++ b/arch/s390/kvm/s390/gaccess.c
@@ -1481,6 +1481,8 @@ static int _do_shadow_crste(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gpa_
if (sg->invalidated)
return -EAGAIN;
oldcrste = READ_ONCE(*host);
+ if (!crste_leaf(oldcrste))
+ return -EAGAIN;
newcrste = _crste_fc1(f->pfn, oldcrste.h.tt, f->writable, !p);
newcrste.s.fc1.d |= oldcrste.s.fc1.d;
newcrste.s.fc1.sd |= oldcrste.s.fc1.sd;
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v1 4/5] KVM: s390: Fix _crste_fc1()
2026-12-04 16:37 [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4 Claudio Imbrenda
` (2 preceding siblings ...)
2026-12-04 16:37 ` [PATCH v1 3/5] KVM: s390: Fix race in _do_shadow_crste() Claudio Imbrenda
@ 2026-12-04 16:37 ` Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 5/5] KVM: s390: Fix dat_split_crste() Claudio Imbrenda
4 siblings, 0 replies; 7+ messages in thread
From: Claudio Imbrenda @ 2026-12-04 16:37 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, gra, seiden
Use the appropriate mask when creating a large region-3 entry (pud).
This avoids having stray bits in the lower part of the address, which
can be picked up by other functions and cause wrong addresses to be
mapped.
Fixes: 5a74e3d93417 ("KVM: s390: KVM-specific bitfields and helper functions")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/dat.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/s390/kvm/gmap/dat.h b/arch/s390/kvm/gmap/dat.h
index d273956b3eb7..488fa6bbbcaf 100644
--- a/arch/s390/kvm/gmap/dat.h
+++ b/arch/s390/kvm/gmap/dat.h
@@ -456,7 +456,7 @@ static inline union crste _crste_ucas(kvm_pfn_t pfn)
*/
static inline union crste _crste_fc1(kvm_pfn_t pfn, int tt, bool writable, bool dirty)
{
- union crste res = { .val = PFN_PHYS(pfn) & _SEGMENT_MASK };
+ union crste res = { .val = PFN_PHYS(pfn) & (tt ? _REGION3_MASK : _SEGMENT_MASK) };
res.h.tt = tt;
res.h.p = !dirty;
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v1 5/5] KVM: s390: Fix dat_split_crste()
2026-12-04 16:37 [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4 Claudio Imbrenda
` (3 preceding siblings ...)
2026-12-04 16:37 ` [PATCH v1 4/5] KVM: s390: Fix _crste_fc1() Claudio Imbrenda
@ 2026-12-04 16:37 ` Claudio Imbrenda
4 siblings, 0 replies; 7+ messages in thread
From: Claudio Imbrenda @ 2026-12-04 16:37 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, gra, seiden
A large region-3 entry (pud) might have stray bits where the segment
index is located, which is not a problem by itself, since hardware
ignores them.
The problem arises in dat_split_crste(), which assumes that there are
no stray bits. The stray bits were not cleared and could have ended up
overlapping the actual segment index of the split table.
Fix by clearing the segment index.
Fixes: 2db149a0a6c5 ("KVM: s390: KVM page table management functions: walks")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/dat.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/s390/kvm/gmap/dat.c b/arch/s390/kvm/gmap/dat.c
index aee22867309c..02bc874d004f 100644
--- a/arch/s390/kvm/gmap/dat.c
+++ b/arch/s390/kvm/gmap/dat.c
@@ -302,6 +302,7 @@ static int dat_split_crste(struct kvm_s390_mmu_cache *mc, union crste *crstep,
init = old;
init.h.tt--;
if (old.h.fc) {
+ init.val &= ~_SEGMENT_INDEX;
for (i = 0; i < _CRST_ENTRIES; i++)
table->crstes[i].val = init.val | i * HPAGE_SIZE;
} else {
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled
2026-12-04 16:37 ` [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled Claudio Imbrenda
@ 2026-12-04 17:06 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-04 17:06 UTC (permalink / raw)
To: Claudio Imbrenda, kvm
Cc: linux-s390, Heiko Carstens, Vasily Gorbik, Alexander Gordeev,
Christian Borntraeger
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: s390: missing MMU cache topup on -ENOMEM in
kvm_s390_faultin_gfn()
--
commit 0d1f3b314bae87061643363aac78552599afc310
Author: Claudio Imbrenda <imbrenda@linux.ibm.com>
KVM: s390: Only allocate page table when cmma is enabled
When a guest-originated fault hits an unallocated segment with CMMA
enabled, this commit allocates only the page table instead of also
faulting in the backing guest memory.
> diff --git a/arch/s390/kvm/gmap/faultin.c b/arch/s390/kvm/gmap/faultin.c
> index 2e93bd563d5c3..8d0e86943b23c 100644
> --- a/arch/s390/kvm/gmap/faultin.c
> +++ b/arch/s390/kvm/gmap/faultin.c
[ ... ]
> @@ -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;
[Severity: Medium]
Should this handle -ENOMEM from dat_entry_walk() by calling
kvm_s390_mmu_cache_topup(mc) outside mmu_lock and retrying?
vcpu->arch.mc only caches 2 page tables and 6 CRST tables, so an ESSA
sweep quickly drains it and leaves dat_entry_walk() falling back to
GFP_ATOMIC allocations (including order-2 CRST tables).
When an atomic allocation fails under memory pressure or fragmentation,
falling through to the main loop calls __kvm_faultin_pfn() and faults in
the guest backing page (or full huge page) for an ESSA fault before
gmap_link() hits -ENOMEM and finally tops up mc.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204163735.237528-1-imbrenda@linux.ibm.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-04 17:06 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:37 [PATCH v1 0/5] KVM: s390: Even more fixes for 7.4 Claudio Imbrenda
2026-12-04 16:37 ` [PATCH v1 1/5] KVM: s390: Only allocate page table when cmma is enabled Claudio Imbrenda
2026-12-04 17:06 ` 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox