* [PATCH v1 00/12] KVM: s390: More fixes
@ 2026-11-27 15:03 Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
` (11 more replies)
0 siblings, 12 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:03 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
Fix a bunch of gmap bugs. Several patches concern proper handling of
the soft-dirty bit for migration.
Claudio Imbrenda (12):
KVM: s390: ucontrol: Do not mask -ENOMEM when translating
KVM: s390: Fix potential leak in kvm_s390_get_io_int()
KVM: s390: Introduce gmap_enable_cmm()
KVM: s390: Allocate page tables when CMMA is used
KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE
KVM: s390: Fix soft-dirty handling in _gmap_link()
KVM: s390: Fix race in gmap_clear_young_pte()
KVM: s390: Properly adjust page size before calling _gmap_link()
KVM: s390: Fix *_test_and_clear_softdirty() functions
KVM: s390: Do not unnecessarily drop the soft-dirty bit
KVM: s390: Disallow 2G pages for guests up to 2G
KVM: s390: vsie: Fix improper shadowing of non-present host ptes
arch/s390/kvm/gmap/dat.c | 2 +-
arch/s390/kvm/gmap/gmap.c | 228 +++++++++++++++++++++++++++------
arch/s390/kvm/gmap/gmap.h | 14 ++
arch/s390/kvm/s390/gaccess.c | 3 +-
arch/s390/kvm/s390/interrupt.c | 13 +-
arch/s390/kvm/s390/priv.c | 11 +-
arch/s390/kvm/s390/s390.c | 19 ++-
7 files changed, 235 insertions(+), 55 deletions(-)
--
2.56.1
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v1 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
` (10 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
Do not mask -ENOMEM or any other error when vcpu_ucontrol_translate()
is called. In case of failure, return the error without masking it to
-EREMOTE.
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/gmap.c | 5 ++++-
arch/s390/kvm/s390/s390.c | 10 ++++++----
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index b9d92bef5459..ef1243ad4e54 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -937,7 +937,10 @@ static int gmap_ucas_translate_simple(struct gmap *gmap, gpa_t *gaddr, union crs
* This allows ucontrol VMs to use the normal fault resolution path, like
* normal VMs.
*
- * Return: %0 in case of success, otherwise %-EREMOTE.
+ * Return:
+ * * %0 in case of success,
+ * * %-ENOMEM if running out of memory,
+ * * otherwise %-EREMOTE.
*/
int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t *gaddr)
{
diff --git a/arch/s390/kvm/s390/s390.c b/arch/s390/kvm/s390/s390.c
index 18fb200f854a..cec1278f2ceb 100644
--- a/arch/s390/kvm/s390/s390.c
+++ b/arch/s390/kvm/s390/s390.c
@@ -4316,8 +4316,9 @@ static int kvm_s390_fixup_prefix(struct kvm_vcpu *vcpu)
gfn_t gfn;
int rc;
- if (vcpu_ucontrol_translate(vcpu, &gaddr))
- return -EREMOTE;
+ rc = vcpu_ucontrol_translate(vcpu, &gaddr);
+ if (rc)
+ return rc;
gfn = gpa_to_gfn(gaddr);
rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gfn, true);
@@ -4619,8 +4620,9 @@ static int vcpu_dat_fault_handler(struct kvm_vcpu *vcpu, gpa_t gaddr, bool wr)
};
int rc;
- if (vcpu_ucontrol_translate(vcpu, &gaddr))
- return -EREMOTE;
+ rc = vcpu_ucontrol_translate(vcpu, &gaddr);
+ if (rc)
+ return rc;
f.gfn = gpa_to_gfn(gaddr);
rc = kvm_s390_faultin_gfn(vcpu, NULL, &f);
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int()
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 03/12] KVM: s390: Introduce gmap_enable_cmm() Claudio Imbrenda
` (9 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
In case all of the following apply at the same time:
* a higher priority GISA interrupt is pending
* a lower priority classic interrupt is also pending
* the pending interrupt queue is full
then kvm_s390_reinject_io_int() will fail, the classic interrupt is
dropped and its memory is leaked.
Fix by checking whether the reinjection of the interrupt succeeded, and
free it if not. The interrupt is still dropped, but no leak occurs.
This is not expected to happen during normal guest operation, due to
the large maximum size of the pending interrupt queue.
Fixes: 4b35f65e67ee ("KVM: s390: make kvm_s390_get_io_int() aware of GISA")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/s390/interrupt.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/arch/s390/kvm/s390/interrupt.c b/arch/s390/kvm/s390/interrupt.c
index 31a6074feffc..33672f093322 100644
--- a/arch/s390/kvm/s390/interrupt.c
+++ b/arch/s390/kvm/s390/interrupt.c
@@ -1775,8 +1775,17 @@ struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
if (tmp_inti) {
tmp_inti->type = KVM_S390_INT_IO(1, 0, 0, 0);
tmp_inti->io.io_int_word = isc_to_int_word(isc);
- if (inti)
- kvm_s390_reinject_io_int(kvm, inti);
+ if (inti) {
+ /*
+ * The maximum number of pending interrupts is so
+ * high that this is impossible in practice during
+ * normal guest operation.
+ * Drop the interrupt if the limit was somehow
+ * reached anyway, and free it, to avoid a leak.
+ */
+ if (kvm_s390_reinject_io_int(kvm, inti))
+ kfree(inti);
+ }
inti = tmp_inti;
} else
gisa_set_ipm_gisc(gi->origin, isc);
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 03/12] KVM: s390: Introduce gmap_enable_cmm()
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used Claudio Imbrenda
` (8 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
Introduce gmap_enable_cmm_already_locked() and its locking wrapper
gmap_enable_cmm() 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.
Cc: stable@vger.kernel.org # 7.0+: prerequisite for the next patch
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 48 +++++++++++++++++++++++++++++++++++++++
arch/s390/kvm/gmap/gmap.h | 14 ++++++++++++
2 files changed, 62 insertions(+)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index ef1243ad4e54..c7af1ad9f7c1 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -1527,3 +1527,51 @@ struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare
}
return new;
}
+
+#if KVM_S390_MANAGES_S390_GUEST
+/* Bail out for any allocation failure. This is only a best effort to allocate all page tables. */
+void gmap_enable_cmm_already_locked(struct kvm_s390_mmu_cache *mc, struct gmap *gmap)
+{
+ struct kvm_memory_slot *ms;
+ struct kvm_memslots *slots;
+ gfn_t gfn, start, end;
+ union crste *crstep;
+ union pte *ptep;
+ int flags, bkt;
+
+ lockdep_assert_held(&gmap->kvm->srcu);
+ lockdep_assert_held(&gmap->kvm->slots_arch_lock);
+
+ if (uses_cmm(gmap))
+ return;
+
+ set_bit(GMAP_FLAG_USES_CMM, &gmap->flags);
+
+ slots = kvm_memslots(gmap->kvm);
+ if (!slots || kvm_memslots_empty(slots))
+ return;
+
+ if (WARN_ON_ONCE(is_ucontrol(gmap)))
+ return;
+
+ flags = DAT_WALK_ALLOC | DAT_WALK_SPLIT;
+
+ 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))
+ return;
+
+ guard(write_lock)(&gmap->kvm->mmu_lock);
+
+ if (uses_skeys(gmap))
+ flags |= DAT_WALK_USES_SKEYS;
+
+ dat_entry_walk(mc, gfn, gmap->asce, flags, TABLE_TYPE_PAGE_TABLE,
+ &crstep, &ptep);
+ }
+ }
+}
+#endif /* KVM_S390_MANAGES_S390_GUEST */
diff --git a/arch/s390/kvm/gmap/gmap.h b/arch/s390/kvm/gmap/gmap.h
index 8f47f29c89fd..fe96268b6de7 100644
--- a/arch/s390/kvm/gmap/gmap.h
+++ b/arch/s390/kvm/gmap/gmap.h
@@ -102,6 +102,7 @@ void gmap_ucas_unmap(struct gmap *gmap, gfn_t c_gfn, unsigned long count);
#if KVM_S390_MANAGES_S390_GUEST
int gmap_enable_skeys(struct gmap *gmap);
+void gmap_enable_cmm_already_locked(struct kvm_s390_mmu_cache *mc, struct gmap *gmap);
#endif /* KVM_S390_MANAGES_S390_GUEST */
int gmap_pv_destroy_range(struct gmap *gmap, gfn_t start, gfn_t end, bool interruptible);
@@ -345,4 +346,17 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed
return sg->guest_asce.val == asce.val && sg->edat_level == edat_level;
}
+#if KVM_S390_MANAGES_S390_GUEST
+static inline void gmap_enable_cmm(struct kvm_s390_mmu_cache *mc, struct gmap *gmap)
+{
+ /* CMMA already enabled, nothing to do */
+ if (uses_cmm(gmap))
+ return;
+
+ guard(srcu)(&gmap->kvm->srcu);
+ guard(mutex)(&gmap->kvm->slots_arch_lock);
+
+ gmap_enable_cmm_already_locked(mc, gmap);
+}
+#endif /* KVM_S390_MANAGES_S390_GUEST */
#endif /* ARCH_KVM_GMAP_GMAP_H */
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (2 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 03/12] KVM: s390: Introduce gmap_enable_cmm() Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:34 ` sashiko-bot
2026-11-27 15:04 ` [PATCH v1 05/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
` (7 subsequent siblings)
11 siblings, 1 reply; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
When ESSA is executed in the guest for the first time, allocate all
page tables and split all existing large page mappings, regardless of
the hpage parameter. This also applies if the first ESSA instruction
was performed in migration mode. Also fence UCONTROL VMs from using
the KVM_S390_SET_CMMA_BITS ioctl, returning -EINVAL.
With the page tables present (even if all the entries within are not
present), ESSA will not cause page faults.
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 startup the whole guest memory
is faulted in, since the guest will mark all of its memory at boot.
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/s390/priv.c | 11 +++++++----
arch/s390/kvm/s390/s390.c | 9 +++++++--
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/arch/s390/kvm/s390/priv.c b/arch/s390/kvm/s390/priv.c
index 856939acf012..f5d4a82e1078 100644
--- a/arch/s390/kvm/s390/priv.c
+++ b/arch/s390/kvm/s390/priv.c
@@ -1241,14 +1241,15 @@ static int handle_essa(struct kvm_vcpu *vcpu)
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 mm_context, and we are not doing
- * a migration. Enable CMMA in the mm_context.
- * Since we need to take a write lock to write to the context
+ * the SIE block and in the gmap, and we are not doing a
+ * migration. Enable CMMA in the gmap.
+ * Since we need to take a mutex to write to the gmap
* to avoid races with storage keys handling, we check if the
* value really needs to be written to; if the value is
* already correct, we do nothing and avoid the lock.
*/
- set_bit(GMAP_FLAG_USES_CMM, &vcpu->arch.gmap->flags);
+ if (!uses_cmm(vcpu->arch.gmap))
+ gmap_enable_cmm(vcpu->arch.mc, vcpu->arch.gmap);
/*
* If we are here, we are supposed to have CMMA enabled in
* the SIE block. Enabling CMMA works on a per-CPU basis,
@@ -1262,6 +1263,8 @@ static int handle_essa(struct kvm_vcpu *vcpu)
/* Retry the ESSA instruction */
kvm_s390_retry_instr(vcpu);
} else {
+ if (!uses_cmm(vcpu->arch.gmap))
+ gmap_enable_cmm(vcpu->arch.mc, vcpu->arch.gmap);
scoped_guard(mutex, &vcpu->kvm->slots_arch_lock)
scoped_guard(read_lock, &vcpu->kvm->mmu_lock)
i = __do_essa(vcpu, orc);
diff --git a/arch/s390/kvm/s390/s390.c b/arch/s390/kvm/s390/s390.c
index cec1278f2ceb..f96d3cff2971 100644
--- a/arch/s390/kvm/s390/s390.c
+++ b/arch/s390/kvm/s390/s390.c
@@ -2349,6 +2349,9 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,
if (r)
return -EFAULT;
+ scoped_guard(srcu, &kvm->srcu)
+ gmap_enable_cmm_already_locked(mc, kvm->arch.gmap);
+
do {
r = kvm_s390_mmu_cache_topup(mc);
if (r)
@@ -2359,8 +2362,6 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,
}
} while (r == -ENOMEM);
- set_bit(GMAP_FLAG_USES_CMM, &kvm->arch.gmap->flags);
-
return r <= 0 ? r : -EFAULT;
}
@@ -2991,6 +2992,10 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
r = -EFAULT;
if (copy_from_user(&args, argp, sizeof(args)))
break;
+ if (kvm_is_ucontrol(kvm)) {
+ r = -EINVAL;
+ break;
+ }
mutex_lock(&kvm->slots_arch_lock);
r = kvm_s390_set_cmma_bits(kvm, &args);
mutex_unlock(&kvm->slots_arch_lock);
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 05/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (3 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 06/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
` (6 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
When dat_entry_walk() is called specifying the DAT_WALK_CONTINUE flag,
the page table walk continues past the specified level, towards ptes.
If TABLE_TYPE_REGION3 (pud) was specified together with
DAT_WALK_CONTINUE, but the segment table entry (pmd) is invalid or a
large entry, the current code will return a failure, instead of the pmd.
Fix by overriding walk_level with TABLE_TYPE_SEGMENT instead of
TABLE_TYPE_PAGE_TABLE. The code handling segment table entries (pmd)
will correctly handle the situation and continue to ptes if needed.
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 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/s390/kvm/gmap/dat.c b/arch/s390/kvm/gmap/dat.c
index b9507daaf327..fc3e1420a4bb 100644
--- a/arch/s390/kvm/gmap/dat.c
+++ b/arch/s390/kvm/gmap/dat.c
@@ -425,7 +425,7 @@ int dat_entry_walk(struct kvm_s390_mmu_cache *mc, gfn_t gfn, union asce asce, in
return entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT;
if (walk_level == TABLE_TYPE_REGION3 &&
continue_anyway && !entry.pud.h.fc && !entry.h.i) {
- walk_level = TABLE_TYPE_PAGE_TABLE;
+ walk_level = TABLE_TYPE_SEGMENT;
allocate = false;
}
if (walk_level == TABLE_TYPE_REGION3 || ((leaf || any) && entry.pud.h.fc))
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 06/12] KVM: s390: Fix soft-dirty handling in _gmap_link()
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (4 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 05/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 07/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
` (5 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
When resolving a fault in the slow path, via _gmap_link(), the
soft-dirty bit of the old entry is used for the new entry, overwriting
whatever value it had.
If the new entry would have set the soft-dirty bit, it is overwritten
with the old value, potentially losing the soft-dirty status.
Fix by ORing the soft-dirty bit in the new entries with the old one. This
way a soft-dirty entry stays soft-dirty, but a previously clean page
can become soft-dirty.
Also fix improper check of the old crste. Use crste_none() instead of
the improper open coded comparison to _CRSTE_EMPTY(oldval.h.tt).
Fixes: 94fd9b16cc67 ("KVM: s390: KVM page table management functions: lifecycle management")
Fixes: 45921d0212d4 ("KVM: s390: Fix gmap_link()")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index c7af1ad9f7c1..31669a5c51cc 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -825,7 +825,7 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int leve
pgste = pgste_get_lock(f->ptep);
oldpte = *f->ptep;
newpte = _pte(f->pfn, f->writable, f->write_attempt | oldpte.s.d, !f->page);
- newpte.s.sd = oldpte.s.sd;
+ newpte.s.sd |= oldpte.s.sd;
oldpte.s.sd = 0;
if (oldpte.val == _PTE_EMPTY.val || oldpte.h.pfra == f->pfn) {
pgste = gmap_ptep_xchg(gmap, f->ptep, newpte, pgste, f->gfn);
@@ -841,8 +841,8 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int leve
newval = _crste_fc1(f->pfn, oldval.h.tt, f->writable,
f->write_attempt | oldval.s.fc1.d);
newval.s.fc1.s = !f->page;
- newval.s.fc1.sd = oldval.s.fc1.sd;
- if (oldval.val != _CRSTE_EMPTY(oldval.h.tt).val &&
+ newval.s.fc1.sd |= oldval.s.fc1.sd;
+ if (!crste_none(oldval) &&
crste_origin_large(oldval) != crste_origin_large(newval))
return -EAGAIN;
f->crste_region3 = is_pud(newval);
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 07/12] KVM: s390: Fix race in gmap_clear_young_pte()
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (5 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 06/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 08/12] KVM: s390: Properly adjust page size before calling _gmap_link() Claudio Imbrenda
` (4 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
In gmap_clear_young_pte(), pte is being READ_ONCE() outside of the lock
for an opportunistic check, and then the same value is reused inside
the lock without checking again.
If the actual value of the pte has changed between the opportunistic
check and the lock, a stale value for pte is used and written to the
pte.
Fix by reading the value of the pte again inside the lock, and
performing the check again.
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 28 ++++++++++++++++------------
1 file changed, 16 insertions(+), 12 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index 31669a5c51cc..837815a7dffa 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -302,19 +302,23 @@ static long gmap_clear_young_pte(union pte *ptep, gfn_t gfn, gfn_t end, struct d
return 0;
pgste = pgste_get_lock(ptep);
- if (!pgste.prefix_notif || gmap_mkold_prefix(p->gmap, gfn, end)) {
- new = pte;
- new.h.i = 1;
- new.s.y = 0;
- if ((new.s.d || !new.h.p) && !new.s.s)
- folio_set_dirty(pfn_folio(pte.h.pfra));
- new.s.d = 0;
- new.h.p = 1;
-
- pgste.prefix_notif = 0;
- pgste = __dat_ptep_xchg(ptep, pgste, new, gfn, walk->asce, uses_skeys(p->gmap));
+ pte = READ_ONCE(*ptep);
+ if (pte.s.pr && (pte.s.y || !pte.h.i)) {
+ if (!pgste.prefix_notif || gmap_mkold_prefix(p->gmap, gfn, end)) {
+ new = pte;
+ new.h.i = 1;
+ new.s.y = 0;
+ if ((new.s.d || !new.h.p) && !new.s.s)
+ folio_set_dirty(pfn_folio(pte.h.pfra));
+ new.s.d = 0;
+ new.h.p = 1;
+
+ pgste.prefix_notif = 0;
+ pgste = __dat_ptep_xchg(ptep, pgste, new, gfn, walk->asce,
+ uses_skeys(p->gmap));
+ }
+ p->young = 1;
}
- p->young = 1;
pgste_set_unlock(ptep, pgste);
return 0;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 08/12] KVM: s390: Properly adjust page size before calling _gmap_link()
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (6 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 07/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
` (3 subsequent siblings)
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
Checking the size of the folio alone is not enough to determine the
maximum allowed page size for a mapping. Multiple vmas with different
permissions could be sharing a single large folio.
Fix by checking the actual size of the userspace mapping if the folio
was large enough, and adjust the size accordingly before attempting to
use large pmds or puds.
This reflects the behaviour on other architectures.
Fixes: 4204067f9982 ("KVM: s390: Add alignment checks for hugepages")
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 88 +++++++++++++++++++++++++++++++++++----
1 file changed, 79 insertions(+), 9 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index 837815a7dffa..5a40f411bcc1 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -858,22 +858,92 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int leve
return rc;
}
+/**
+ * gmap_get_userspace_page_size() - Check the actual page size of the mapping
+ * @kvm: the guest
+ * @gfn: the guest address whose page size is to be checked
+ * @slot: the KVM memslot containing @gfn
+ *
+ * Manually walk the userspace page tables to find out the page size.
+ *
+ * This is safe because kvm->mmu_lock is being held; being a (r/w) spinlock,
+ * preemption is disabled while it is being held.
+ * Due to RCU-deferred freeing of page tables, that is enough to guarantee
+ * that the userspace tables won't disappear while being walked.
+ *
+ * Context: kvm->mmu_lock must be held.
+ * Return:
+ * * TABLE_TYPE_REGION3 if userspace is mapping with a large pud,
+ * * TABLE_TYPE_SEGMENT if userspace is mapping with a large pmd,
+ * * TABLE_TYPE_PAGE_TABLE otherwise, including missing mappings.
+ */
+static int gmap_get_userspace_page_size(struct kvm *kvm, gfn_t gfn, struct kvm_memory_slot *slot)
+{
+ hva_t hva;
+ pgd_t pgd;
+ p4d_t p4d;
+ pud_t pud;
+ pmd_t pmd;
+
+ lockdep_assert_held(&kvm->mmu_lock);
+
+ hva = __gfn_to_hva_memslot(slot, gfn);
+
+ pgd = READ_ONCE(*pgd_offset(kvm->mm, hva));
+ if (pgd_none(pgd))
+ return TABLE_TYPE_PAGE_TABLE;
+
+ p4d = READ_ONCE(*p4d_offset(&pgd, hva));
+ if (p4d_none(p4d) || !p4d_present(p4d))
+ return TABLE_TYPE_PAGE_TABLE;
+
+ pud = READ_ONCE(*pud_offset(&p4d, hva));
+ if (pud_none(pud) || !pud_present(pud))
+ return TABLE_TYPE_PAGE_TABLE;
+ if (pud_leaf(pud))
+ return TABLE_TYPE_REGION3;
+
+ pmd = READ_ONCE(*pmd_offset(&pud, hva));
+ if (pmd_none(pmd) || !pmd_present(pmd))
+ return TABLE_TYPE_PAGE_TABLE;
+ if (pmd_leaf(pmd))
+ return TABLE_TYPE_SEGMENT;
+
+ return TABLE_TYPE_PAGE_TABLE;
+}
+
+static int gmap_determine_fault_page_size(struct gmap *gmap, struct guest_fault *f,
+ struct kvm_memory_slot *slot)
+{
+ unsigned int order;
+ int level;
+
+ if (!f->page)
+ return TABLE_TYPE_PAGE_TABLE;
+
+ order = folio_order(page_folio(f->page));
+ level = TABLE_TYPE_PAGE_TABLE;
+
+ if (order >= get_order(_REGION3_SIZE) && gmap_2g_allowed(gmap, f, slot))
+ level = TABLE_TYPE_REGION3;
+ else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f, slot))
+ level = TABLE_TYPE_SEGMENT;
+
+ if (level == TABLE_TYPE_PAGE_TABLE)
+ return level;
+
+ return min(level, gmap_get_userspace_page_size(gmap->kvm, f->gfn, slot));
+}
+
int gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, struct guest_fault *f,
struct kvm_memory_slot *slot)
{
- unsigned int order;
int level;
lockdep_assert_held(&gmap->kvm->mmu_lock);
- level = TABLE_TYPE_PAGE_TABLE;
- if (f->page) {
- order = folio_order(page_folio(f->page));
- if (order >= get_order(_REGION3_SIZE) && gmap_2g_allowed(gmap, f, slot))
- level = TABLE_TYPE_REGION3;
- else if (order >= get_order(_SEGMENT_SIZE) && gmap_1m_allowed(gmap, f, slot))
- level = TABLE_TYPE_SEGMENT;
- }
+ level = gmap_determine_fault_page_size(gmap, f, slot);
+
return _gmap_link(mc, gmap, level, f);
}
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (7 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 08/12] KVM: s390: Properly adjust page size before calling _gmap_link() Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:30 ` sashiko-bot
2026-11-27 15:04 ` [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
` (2 subsequent siblings)
11 siblings, 1 reply; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
Do not skip non-present entries, as they can be soft-dirty, and need to
be accounted as such.
Check for the invalid bit together with the protected bit when
determining whether to skip an entry.
Do not set the protected bit if the invalid bit is still set; the
invalid bit is enough.
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index 5a40f411bcc1..ad2d815cf9c4 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -585,7 +585,7 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p
{
union pte pte = READ_ONCE(*ptep);
- if (!pte.s.pr || (pte.h.p && !pte.s.sd))
+ if ((pte.h.p || pte.h.i) && !pte.s.sd)
return pgste;
/*
@@ -593,7 +593,8 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p
* running, do not reset the protection, leave it marked as dirty.
*/
if (!pgste.prefix_notif || gmap_mkold_prefix(gmap, gfn, gfn + 1)) {
- pte.h.p = 1;
+ if (!pte.h.i)
+ pte.h.p = 1;
pte.s.sd = 0;
pgste = gmap_ptep_xchg(gmap, ptep, pte, pgste, gfn);
}
@@ -625,9 +626,9 @@ static long _crste_test_and_clear_softdirty(union crste *table, gfn_t gfn, gfn_t
return 1;
do {
crste = READ_ONCE(*table);
- if (!crste.h.fc)
+ if (!crste.h.fc && !crste.h.i)
return 0;
- if (crste.h.p && !crste.s.fc1.sd)
+ if ((crste.h.p || crste.h.i) && !crste.s.fc1.sd)
return 0;
/*
@@ -635,10 +636,11 @@ static long _crste_test_and_clear_softdirty(union crste *table, gfn_t gfn, gfn_t
* currently running, do not reset the protection, leave it marked as
* dirty.
*/
- if (crste.s.fc1.prefix_notif && !gmap_mkold_prefix(gmap, gfn, end))
+ if (crste.h.fc && crste.s.fc1.prefix_notif && !gmap_mkold_prefix(gmap, gfn, end))
break;
new = crste;
- new.h.p = 1;
+ if (!crste.h.i)
+ new.h.p = 1;
new.s.fc1.sd = 0;
} while (!gmap_crstep_xchg_atomic(gmap, table, crste, new, gfn));
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (8 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:27 ` sashiko-bot
2026-11-27 15:04 ` [PATCH v1 11/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 12/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
11 siblings, 1 reply; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
Properly preserve the soft-dirty bit when performing unmaps or other
similar operations.
The soft-dirty bit is only cleared by gmap_sync_dirty_log(), which also
properly marks the dirty pages as such in the dirty bitmap.
The previous behaviour could lose the soft-dirty bit in some
circumstances.
Fixes: d56cd0c0d4df ("KVM: s390: Improve unmapping of large ranges")
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 36 +++++++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 9 deletions(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index ad2d815cf9c4..410226cf4dd0 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -389,6 +389,7 @@ static long _gmap_unmap_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_w
struct folio *folio = NULL;
unsigned long vmaddr;
union pgste pgste;
+ union pte newpte;
pgste = pgste_get_lock(ptep);
if (ptep->s.pr && pgste.usage == PGSTE_GPS_USAGE_UNUSED) {
@@ -397,7 +398,9 @@ static long _gmap_unmap_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_w
}
if (ptep->s.pr && test_bit(GMAP_FLAG_EXPORT_ON_UNMAP, &priv->gmap->flags))
folio = pfn_folio(ptep->h.pfra);
- pgste = gmap_ptep_xchg(priv->gmap, ptep, _PTE_EMPTY, pgste, gfn);
+ newpte = _PTE_EMPTY;
+ newpte.s.sd = ptep->s.sd;
+ pgste = gmap_ptep_xchg(priv->gmap, ptep, newpte, pgste, gfn);
pgste_set_unlock(ptep, pgste);
if (folio)
uv_convert_from_secure_folio(folio);
@@ -408,8 +411,8 @@ static long _gmap_unmap_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_w
static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *walk)
{
struct gmap_unmap_priv *priv = walk->priv;
+ union crste newcrste, old = *crstep;
struct folio *folio = NULL;
- union crste old = *crstep;
bool ok;
if (!old.h.fc)
@@ -417,6 +420,9 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct
if (old.s.fc1.pr && test_bit(GMAP_FLAG_EXPORT_ON_UNMAP, &priv->gmap->flags))
folio = phys_to_folio(crste_origin_large(old));
+
+ newcrste = _CRSTE_EMPTY(old.h.tt);
+ newcrste.s.fc1.sd = old.s.fc1.sd;
/*
* No races should happen because kvm->mmu_lock is held in write mode,
* but the unmap operation could have triggered an unshadow, which
@@ -425,11 +431,11 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct
* had the vsie_notif bit set. A second failure is not allowed, for
* the reasons above.
*/
- ok = gmap_crstep_xchg_atomic(priv->gmap, crstep, old, _CRSTE_EMPTY(old.h.tt), gfn);
+ ok = gmap_crstep_xchg_atomic(priv->gmap, crstep, old, newcrste, gfn);
if (!ok) {
KVM_BUG_ON(!old.s.fc1.vsie_notif, priv->gmap->kvm);
old.s.fc1.vsie_notif = 0;
- ok = gmap_crstep_xchg_atomic(priv->gmap, crstep, old, _CRSTE_EMPTY(old.h.tt), gfn);
+ ok = gmap_crstep_xchg_atomic(priv->gmap, crstep, old, newcrste, gfn);
KVM_BUG_ON(!ok, priv->gmap->kvm);
}
if (folio)
@@ -459,7 +465,7 @@ static bool gmap_unmap_gfn_range_slow(struct gmap *gmap, struct kvm_memory_slot
static long _gmap_unmap_pud_fast(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *w)
{
- union crste old = *crstep;
+ union crste newcrste, old = *crstep;
/* We only care about large puds */
if (!old.h.fc)
@@ -480,14 +486,17 @@ static long _gmap_unmap_pud_fast(union crste *crstep, gfn_t gfn, gfn_t next, str
idte_crste(crstep, gfn, IDTE_GUEST_ASCE, w->asce, 0);
else
idte_crste(crstep, gfn, 0, w->asce, 0);
- *crstep = _CRSTE_EMPTY(old.h.tt);
+
+ newcrste = _CRSTE_EMPTY(old.h.tt);
+ newcrste.s.fc1.sd = old.s.fc1.sd;
+ *crstep = newcrste;
return 0;
}
static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, struct dat_walk *w)
{
- union crste old = *crstep;
+ union crste newcrste, old = *crstep;
struct page_table *pt;
gfn_t first, last;
unsigned int i;
@@ -500,7 +509,10 @@ static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, str
idte_crste(crstep, gfn, IDTE_GUEST_ASCE, w->asce, 0);
else
idte_crste(crstep, gfn, 0, w->asce, 0);
- WRITE_ONCE(*crstep, _CRSTE_EMPTY(old.h.tt));
+
+ newcrste = _CRSTE_EMPTY(old.h.tt);
+ newcrste.s.fc1.sd = old.s.fc1.sd;
+ *crstep = newcrste;
return 0;
}
@@ -523,11 +535,15 @@ static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, str
idte_crste(crstep, gfn, 0, w->asce, 0);
for (i = first; i <= last; i++) {
+ union pte newpte;
+
if (pte_hole(pt->ptes[i]))
continue;
if (!pt->ptes[i].h.i && uses_skeys(w->priv))
pt->pgstes[i] = dat_save_storage_key_into_pgste(pt->ptes[i], pt->pgstes[i]);
- pt->ptes[i] = _PTE_EMPTY;
+ newpte = _PTE_EMPTY;
+ newpte.s.sd = pt->ptes[i].s.sd;
+ pt->ptes[i] = newpte;
pt->pgstes[i].vsie_notif = 0;
pt->pgstes[i].vsie_gmem = 0;
pt->pgstes[i].zero = 0;
@@ -1115,6 +1131,7 @@ static long _gmap_split_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct
gmap_unmap_prefix(gmap, gfn, next);
if (crste.s.fc1.vsie_notif)
gmap_handle_vsie_unshadow_event(gmap, gfn);
+ newcrste.s.fc1.sd = crste.s.fc1.sd;
if (dat_crstep_xchg_atomic(crstep, crste, newcrste, gfn, walk->asce))
break;
crste = READ_ONCE(*crstep);
@@ -1290,6 +1307,7 @@ int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gf
return -EAGAIN;
pte = ptep->s.pr ? *ptep : _pte(pfn, wr, false, false);
pte.h.p = 1;
+ pte.s.sd |= ptep->s.sd;
pgste = _gmap_ptep_xchg(sg->parent, ptep, pte, pgste, p_gfn, false);
pgste.vsie_notif = 1;
pgste_set_unlock(ptep, pgste);
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 11/12] KVM: s390: Disallow 2G pages for guests up to 2G
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (9 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 12/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
If a guest of size exactly 2G is being backed with a 2G page by
userspace, gmap will try to use a 2G page (large pud).
But due to the address space being <= 2G, a segment-type ASCE is used,
which means that there are no puds. This causes some checks to fail and
ultimately kill the VM.
Fix by disallowing 2G large pages for guests with a segment-type ASCE.
Fixes: 46756c086310 ("KVM: s390: Allow for 2G hugepages")
Cc: stable@vger.kernel.org
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
arch/s390/kvm/gmap/gmap.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index 410226cf4dd0..6a6376a840fe 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -798,7 +798,8 @@ static inline bool gmap_2g_allowed(struct gmap *gmap, struct guest_fault *f,
return test_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &gmap->flags) &&
!((f->gfn ^ f->pfn) & ~_REGION3_FR_MASK) &&
slot->base_gfn <= ALIGN_DOWN(f->gfn, _PAGES_PER_REGION3) &&
- slot->base_gfn + slot->npages >= ALIGN(f->gfn + 1, _PAGES_PER_REGION3);
+ slot->base_gfn + slot->npages >= ALIGN(f->gfn + 1, _PAGES_PER_REGION3) &&
+ gmap->asce.dt > ASCE_TYPE_SEGMENT;
}
/**
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v1 12/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
` (10 preceding siblings ...)
2026-11-27 15:04 ` [PATCH v1 11/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
@ 2026-11-27 15:04 ` Claudio Imbrenda
11 siblings, 0 replies; 16+ messages in thread
From: Claudio Imbrenda @ 2026-11-27 15:04 UTC (permalink / raw)
To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
seiden
If a host pte that is being shadowed was empty in the old mapping, the
new mapping might end up not write-protected, even if it should have
been, since empty ptes do not have the p bit set.
Fix by only and-ing the old host p bit if the old entry was present.
Fixes: fd7bc612cf27 ("KVM: s390: vsie: Fix nested guest memory shadowing")
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 | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/arch/s390/kvm/s390/gaccess.c b/arch/s390/kvm/s390/gaccess.c
index 2d27dcb68002..158af3415e2d 100644
--- a/arch/s390/kvm/s390/gaccess.c
+++ b/arch/s390/kvm/s390/gaccess.c
@@ -1434,7 +1434,8 @@ static int _do_shadow_pte(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gpa_t
newpte = _pte(f->pfn, f->writable, !p, ptep_h->s.s);
newpte.s.d |= ptep_h->s.d;
newpte.s.sd |= ptep_h->s.sd;
- newpte.h.p &= ptep_h->h.p;
+ if (ptep_h->s.pr)
+ newpte.h.p &= ptep_h->h.p;
if (!newpte.h.p && !f->writable) {
rc = -EOPNOTSUPP;
} else {
--
2.56.1
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit
2026-11-27 15:04 ` [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
@ 2026-11-27 15:27 ` sashiko-bot
0 siblings, 0 replies; 16+ messages in thread
From: sashiko-bot @ 2026-11-27 15:27 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:
- [Low] KVM: s390: unresolvable commit SHA d56cd0c0d4df in Fixes trailer
--
commit c65dbb8fcb17b43130bab25f810a7051e6e82044
Author: Claudio Imbrenda <imbrenda@linux.ibm.com>
> KVM: s390: Do not unnecessarily drop the soft-dirty bit
>
> Properly preserve the soft-dirty bit when performing unmaps or other
> similar operations.
>
> The soft-dirty bit is only cleared by gmap_sync_dirty_log(), which also
> properly marks the dirty pages as such in the dirty bitmap.
>
> The previous behaviour could lose the soft-dirty bit in some
> circumstances.
>
> Fixes: d56cd0c0d4df ("KVM: s390: Improve unmapping of large ranges")
> Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
> Cc: stable@vger.kernel.org
> Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
[Severity: Low]
Commit sha d56cd0c0d4df in the first Fixes: trailer cannot be resolved in
the repository. In git history, the commit with that title that introduced
_gmap_unmap_pud_fast() and _gmap_unmap_pmd_fast() is d71e7918b459 ("KVM:
s390: Improve unmapping of large ranges"). Should the trailer be updated
to:
Fixes: d71e7918b459 ("KVM: s390: Improve unmapping of large ranges")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127150411.221072-1-imbrenda@linux.ibm.com?part=10
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions
2026-11-27 15:04 ` [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
@ 2026-11-27 15:30 ` sashiko-bot
0 siblings, 0 replies; 16+ messages in thread
From: sashiko-bot @ 2026-11-27 15:30 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:
- [High] KVM: s390: __pte_test_and_clear_softdirty() clears pgste.zero
on invalid PTEs
--
commit d9b61cfa531bfdb9496d9935489a88705ec5abb5
Author: Claudio Imbrenda <imbrenda@linux.ibm.com>
KVM: s390: Fix *_test_and_clear_softdirty() functions
This commit updates __pte_test_and_clear_softdirty() and
_crste_test_and_clear_softdirty() to process non-present soft-dirty
entries, check the invalid bit alongside the protected bit when skipping
clean entries, and avoid setting the protected bit on invalid entries.
> diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
> index 5a40f411bcc15..ad2d815cf9c47 100644
> --- a/arch/s390/kvm/gmap/gmap.c
> +++ b/arch/s390/kvm/gmap/gmap.c
> @@ -585,7 +585,7 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p
> {
> union pte pte = READ_ONCE(*ptep);
>
> - if (!pte.s.pr || (pte.h.p && !pte.s.sd))
> + if ((pte.h.p || pte.h.i) && !pte.s.sd)
> return pgste;
>
> /*
> @@ -593,7 +593,8 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p
> * running, do not reset the protection, leave it marked as dirty.
> */
> if (!pgste.prefix_notif || gmap_mkold_prefix(gmap, gfn, gfn + 1)) {
> - pte.h.p = 1;
> + if (!pte.h.i)
> + pte.h.p = 1;
> pte.s.sd = 0;
> pgste = gmap_ptep_xchg(gmap, ptep, pte, pgste, gfn);
> }
[Severity: High]
Does calling gmap_ptep_xchg() on an invalid or non-present PTE
(pte.h.i == 1) clear pgste.zero?
arch/s390/kvm/gmap/gmap.h:_gmap_ptep_xchg() {
...
pgste.zero = 0;
return __dat_ptep_xchg(ptep, pgste, newpte, gfn, gmap->asce, uses_skeys(gmap));
}
When a dirty log sync clears pte.s.sd on an invalid PTE that was
discarded via ESSA (pgste.zero == 1), zeroing pgste.zero while pte.h.i
remains 1 causes subsequent ESSA operations to report the page content
state as preserved (2) instead of logically zero (3), leading to guest
memory corruption if the guest relies on ESSA state to detect discarded
volatile pages.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127150411.221072-1-imbrenda@linux.ibm.com?part=9
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used
2026-11-27 15:04 ` [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used Claudio Imbrenda
@ 2026-11-27 15:34 ` sashiko-bot
0 siblings, 0 replies; 16+ messages in thread
From: sashiko-bot @ 2026-11-27 15:34 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: premature GMAP_FLAG_USES_CMM publication in
gmap_enable_cmm()
--
commit 4eafe3dbf444099ded4397c5a1073f2aff0effc2
Author: Claudio Imbrenda <imbrenda@linux.ibm.com>
KVM: s390: Allocate page tables when CMMA is used
This commit calls gmap_enable_cmm() on the first ESSA intercept or
KVM_S390_SET_CMMA_BITS ioctl to preallocate page tables and split large
page mappings, and rejects KVM_S390_SET_CMMA_BITS on ucontrol VMs.
> diff --git a/arch/s390/kvm/s390/priv.c b/arch/s390/kvm/s390/priv.c
> index f22612a127c96..e66fec89ac788 100644
> --- a/arch/s390/kvm/s390/priv.c
> +++ b/arch/s390/kvm/s390/priv.c
> @@ -1243,14 +1243,15 @@ static int handle_essa(struct kvm_vcpu *vcpu)
> 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 mm_context, and we are not doing
> - * a migration. Enable CMMA in the mm_context.
> - * Since we need to take a write lock to write to the context
> + * the SIE block and in the gmap, and we are not doing a
> + * migration. Enable CMMA in the gmap.
> + * Since we need to take a mutex to write to the gmap
> * to avoid races with storage keys handling, we check if the
> * value really needs to be written to; if the value is
> * already correct, we do nothing and avoid the lock.
> */
> - set_bit(GMAP_FLAG_USES_CMM, &vcpu->arch.gmap->flags);
> + if (!uses_cmm(vcpu->arch.gmap))
> + gmap_enable_cmm(vcpu->arch.mc, vcpu->arch.gmap);
> /*
> * If we are here, we are supposed to have CMMA enabled in
> * the SIE block. Enabling CMMA works on a per-CPU basis,
[Severity: Medium]
Can a second vCPU observe uses_cmm(vcpu->arch.gmap) == true before
gmap_enable_cmm() has finished allocating page tables and splitting large
pages?
gmap_enable_cmm_already_locked() sets GMAP_FLAG_USES_CMM before walking
the memslots:
arch/s390/kvm/gmap/gmap.c:gmap_enable_cmm_already_locked() {
if (uses_cmm(gmap))
return;
set_bit(GMAP_FLAG_USES_CMM, &gmap->flags);
...
kvm_for_each_memslot(ms, bkt, slots) {
...
for (gfn = start; gfn < end; gfn += _PAGE_ENTRIES) {
if (kvm_s390_mmu_cache_topup(mc))
return;
guard(write_lock)(&gmap->kvm->mmu_lock);
...
dat_entry_walk(mc, gfn, gmap->asce, flags, ...);
}
}
}
Because both handle_essa() and gmap_enable_cmm() check uses_cmm()
locklessly before taking slots_arch_lock, another vCPU intercepting on
ESSA while the memslot walk is in progress can skip gmap_enable_cmm() and
re-enter SIE with ECB2_CMMA enabled:
CPU0 CPU1
handle_essa()
gmap_enable_cmm()
mutex_lock(&slots_arch_lock)
gmap_enable_cmm_already_locked()
set_bit(GMAP_FLAG_USES_CMM, ...)
handle_essa()
if (!uses_cmm(...)) -> false
sie_block->ecb2 |= ECB2_CMMA
kvm_s390_retry_instr()
<re-enters SIE and runs ESSA
on unvisited segments>
kvm_for_each_memslot(...)
dat_entry_walk(...)
CPU1 then executes ESSA in hardware on segments that still lack page
tables or have unsplit large pages, triggering segment translation faults
and faulting in backing hugepages.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127150411.221072-1-imbrenda@linux.ibm.com?part=4
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2026-11-27 15:34 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 15:03 [PATCH v1 00/12] KVM: s390: More fixes Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 03/12] KVM: s390: Introduce gmap_enable_cmm() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used Claudio Imbrenda
2026-11-27 15:34 ` sashiko-bot
2026-11-27 15:04 ` [PATCH v1 05/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 06/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 07/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 08/12] KVM: s390: Properly adjust page size before calling _gmap_link() Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
2026-11-27 15:30 ` sashiko-bot
2026-11-27 15:04 ` [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
2026-11-27 15:27 ` sashiko-bot
2026-11-27 15:04 ` [PATCH v1 11/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
2026-11-27 15:04 ` [PATCH v1 12/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox