Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH v2 00/12] KVM: s390: More fixes
@ 2026-12-02 18:50 Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
                   ` (11 more replies)
  0 siblings, 12 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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.

v1->v2:
* Inject interrupts in the wrong order instead of dropping them
  altogether if the queue is full
* Remove the handling of cmma page faults; will be dealt with in the
  next series
* Do not drop the zero bit from PGSTEs when testing for soft-dirty
* Fix some typos
* Destroy shadow gmaps when removing memslots (thanks Christian)

Christian Borntraeger (1):
  KVM: s390: vsie: Drop shadow gmaps when a memslot is deleted or moved


Christian Borntraeger (1):
  KVM: s390: vsie: Drop shadow gmaps when a memslot is deleted or moved

Claudio Imbrenda (11):
  KVM: s390: ucontrol: Do not mask -ENOMEM when translating
  KVM: s390: Fix potential leak in kvm_s390_get_io_int()
  KVM: s390: Small miscellaneous CMMA fixes
  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      | 207 ++++++++++++++++++++++++++-------
 arch/s390/kvm/gmap/gmap.h      |   1 +
 arch/s390/kvm/gmap/kvm_mmu.c   |  10 +-
 arch/s390/kvm/s390/gaccess.c   |   3 +-
 arch/s390/kvm/s390/interrupt.c |  17 ++-
 arch/s390/kvm/s390/priv.c      |   8 +-
 arch/s390/kvm/s390/s390.c      |  18 ++-
 8 files changed, 210 insertions(+), 56 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v2 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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 9900a1a1c148..4ef7ca960b46 100644
--- a/arch/s390/kvm/s390/s390.c
+++ b/arch/s390/kvm/s390/s390.c
@@ -4309,8 +4309,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);
@@ -4612,8 +4613,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] 14+ messages in thread

* [PATCH v2 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int()
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 03/12] KVM: s390: Small miscellaneous CMMA fixes Claudio Imbrenda
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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
in case of failure, inject the lower priority interrupt and keep the
higher priority GISA interrupt pending.
While this is technically a violation of the architecture, it's a
smaller violation than dropping an interrupt completely.

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 | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/arch/s390/kvm/s390/interrupt.c b/arch/s390/kvm/s390/interrupt.c
index 31a6074feffc..8612d2e96205 100644
--- a/arch/s390/kvm/s390/interrupt.c
+++ b/arch/s390/kvm/s390/interrupt.c
@@ -1775,8 +1775,21 @@ 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 && kvm_s390_reinject_io_int(kvm, inti)) {
+			/*
+			 * The maximum number of pending interrupts is so
+			 * high that this is impossible in practice during
+			 * normal guest operation.
+			 * If this happens anyway, deliver the lower
+			 * priority interrupt, instead of discarding it. It
+			 * is still technically a violation of the
+			 * architecture, but it's not as bad as dropping an
+			 * interrupt entirely.
+			 */
+			kfree(tmp_inti);
+			gisa_set_ipm_gisc(gi->origin, isc);
+			goto out;
+		}
 		inti = tmp_inti;
 	} else
 		gisa_set_ipm_gisc(gi->origin, isc);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v2 03/12] KVM: s390: Small miscellaneous CMMA fixes
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 04/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 UTC (permalink / raw)
  To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
	seiden

Set the CMMA flag also when the first ESSA instruction was performed in
migration mode.

Set the CMMA flag in kvm_s390_set_cmma_bits() before starting to set
the CMMA values, instead of afterwards.

Fix the stale comment in handle_essa().

Fence UCONTROL VMs from using the KVM_S390_SET_CMMA_BITS ioctl,
returning -EINVAL.

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 | 8 +++++---
 arch/s390/kvm/s390/s390.c | 8 ++++++--
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/arch/s390/kvm/s390/priv.c b/arch/s390/kvm/s390/priv.c
index f22612a127c9..55fb1e3a8755 100644
--- a/arch/s390/kvm/s390/priv.c
+++ b/arch/s390/kvm/s390/priv.c
@@ -1243,9 +1243,9 @@ 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.
@@ -1264,6 +1264,8 @@ static int handle_essa(struct kvm_vcpu *vcpu)
 		/* 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(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 4ef7ca960b46..09c43770296d 100644
--- a/arch/s390/kvm/s390/s390.c
+++ b/arch/s390/kvm/s390/s390.c
@@ -2349,6 +2349,8 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,
 	if (r)
 		return -EFAULT;
 
+	set_bit(GMAP_FLAG_USES_CMM, &kvm->arch.gmap->flags);
+
 	do {
 		r = kvm_s390_mmu_cache_topup(mc);
 		if (r)
@@ -2359,8 +2361,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 +2991,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] 14+ messages in thread

* [PATCH v2 04/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (2 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 03/12] KVM: s390: Small miscellaneous CMMA fixes Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 05/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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 7a25e41a1423..aee22867309c 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] 14+ messages in thread

* [PATCH v2 05/12] KVM: s390: Fix soft-dirty handling in _gmap_link()
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (3 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 04/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 06/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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>
Reviewed-by: Janosch Frank <frankja@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 ef1243ad4e54..790f44a4c803 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] 14+ messages in thread

* [PATCH v2 06/12] KVM: s390: Fix race in gmap_clear_young_pte()
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (4 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 05/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 07/12] KVM: s390: Properly adjust page size before calling _gmap_link() Claudio Imbrenda
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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>
Reviewed-by: Janosch Frank <frankja@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 790f44a4c803..5e6db36b6b0d 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] 14+ messages in thread

* [PATCH v2 07/12] KVM: s390: Properly adjust page size before calling _gmap_link()
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (5 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 06/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 08/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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>
Acked-by: Janosch Frank <frankja@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 5e6db36b6b0d..ca196a474f83 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] 14+ messages in thread

* [PATCH v2 08/12] KVM: s390: Fix *_test_and_clear_softdirty() functions
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (6 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 07/12] KVM: s390: Properly adjust page size before calling _gmap_link() Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 19:11   ` sashiko-bot
  2026-12-02 18:50 ` [PATCH v2 09/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
                   ` (3 subsequent siblings)
  11 siblings, 1 reply; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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.

Do not mark dirty a page that has the zero bit set; it can safely be
discarded when migrating.

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 | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index ca196a474f83..2ba3d8e4e23d 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -584,8 +584,9 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p
 						  struct gmap *gmap)
 {
 	union pte pte = READ_ONCE(*ptep);
+	bool zero = pgste.zero;
 
-	if (!pte.s.pr || (pte.h.p && !pte.s.sd))
+	if ((pte.h.p || pte.h.i) && !pte.s.sd)
 		return pgste;
 
 	/*
@@ -593,12 +594,16 @@ 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);
+		pgste.zero = zero;
 	}
 
-	mark_page_dirty(gmap->kvm, gfn);
+	/* dirty but unused, does not need to be migrated */
+	if (!zero)
+		mark_page_dirty(gmap->kvm, gfn);
 
 	return pgste;
 }
@@ -625,9 +630,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 +640,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] 14+ messages in thread

* [PATCH v2 09/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (7 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 08/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 10/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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: d71e7918b459 ("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 2ba3d8e4e23d..e54f911aa841 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;
@@ -1119,6 +1135,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);
@@ -1294,6 +1311,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] 14+ messages in thread

* [PATCH v2 10/12] KVM: s390: Disallow 2G pages for guests up to 2G
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (8 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 09/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 11/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 12/12] KVM: s390: vsie: Drop shadow gmaps when a memslot is deleted or moved Claudio Imbrenda
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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 e54f911aa841..437abb32d2c7 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -802,7 +802,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] 14+ messages in thread

* [PATCH v2 11/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (9 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 10/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  2026-12-02 18:50 ` [PATCH v2 12/12] KVM: s390: vsie: Drop shadow gmaps when a memslot is deleted or moved Claudio Imbrenda
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 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, because 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] 14+ messages in thread

* [PATCH v2 12/12] KVM: s390: vsie: Drop shadow gmaps when a memslot is deleted or moved
  2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
                   ` (10 preceding siblings ...)
  2026-12-02 18:50 ` [PATCH v2 11/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
@ 2026-12-02 18:50 ` Claudio Imbrenda
  11 siblings, 0 replies; 14+ messages in thread
From: Claudio Imbrenda @ 2026-12-02 18:50 UTC (permalink / raw)
  To: kvm; +Cc: linux-s390, borntraeger, frankja, david, nrb, schlameuss, gra,
	seiden

From: Christian Borntraeger <borntraeger@linux.ibm.com>

Shadow gmaps map host frames directly and do not hold references to
them; they rely on vsie unshadow events from the parent gmap to stay
consistent. When a memslot is deleted or moved, dat_delete_slot()
removes the parent entries with the raw exchange helpers, which do not
raise unshadow events, and kvm_arch_flush_shadow_memslot() is empty.

Once userspace unmaps the memory of the old slot, the mmu notifier finds
no memslot and does nothing, the host frames are freed, and existing
shadow gmaps still map them. A nested guest (guest 3) can then read and
write freed host memory.

Remove all shadow gmaps right after the parent entries of the old slot
have been removed, still under kvm->mmu_lock and before the ioctl
returns, so before userspace can unmap the memory. Memslot deletion is
rare; vSIE recreates shadows on demand.

Fixes: e38c884df921 ("KVM: s390: Switch to new gmap")
Cc: stable@vger.kernel.org
Assisted-by: LLM
Signed-off-by: Christian Borntraeger <borntraeger@linux.ibm.com>
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
 arch/s390/kvm/gmap/gmap.c    | 21 +++++++++++++++++++++
 arch/s390/kvm/gmap/gmap.h    |  1 +
 arch/s390/kvm/gmap/kvm_mmu.c | 10 +++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
index 437abb32d2c7..f66e23d6302b 100644
--- a/arch/s390/kvm/gmap/gmap.c
+++ b/arch/s390/kvm/gmap/gmap.c
@@ -1414,6 +1414,27 @@ static void gmap_unshadow(struct gmap *sg)
 	gmap_put(sg);
 }
 
+/**
+ * gmap_unshadow_all() - Remove all shadow gmaps of a parent gmap.
+ * @parent: The parent gmap.
+ *
+ * Shadow gmaps map host frames directly and do not hold references to them.
+ * Use this when parent gmap entries are removed without raising vsie
+ * unshadow events, e.g. when a memslot is deleted or moved.
+ *
+ * Context: Takes parent->children_lock; kvm->mmu_lock may be held.
+ */
+void gmap_unshadow_all(struct gmap *parent)
+{
+	struct gmap *sg, *next;
+
+	guard(spinlock)(&parent->children_lock);
+	list_for_each_entry_safe(sg, next, &parent->children, list) {
+		if (is_shadow(sg))
+			gmap_unshadow(sg);
+	}
+}
+
 void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn)
 {
 	struct vsie_rmap *rmap, *rnext, *head;
diff --git a/arch/s390/kvm/gmap/gmap.h b/arch/s390/kvm/gmap/gmap.h
index 8f47f29c89fd..eeb57493a649 100644
--- a/arch/s390/kvm/gmap/gmap.h
+++ b/arch/s390/kvm/gmap/gmap.h
@@ -110,6 +110,7 @@ int gmap_insert_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn
 int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfn, gfn_t r_gfn,
 		      kvm_pfn_t pfn, int level, bool wr);
 void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn);
+void gmap_unshadow_all(struct gmap *parent);
 struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *gmap,
 				union asce asce, int edat_level);
 void gmap_split_huge_pages(struct gmap *gmap);
diff --git a/arch/s390/kvm/gmap/kvm_mmu.c b/arch/s390/kvm/gmap/kvm_mmu.c
index 4e2da60b9859..cea38de7dcb1 100644
--- a/arch/s390/kvm/gmap/kvm_mmu.c
+++ b/arch/s390/kvm/gmap/kvm_mmu.c
@@ -105,8 +105,16 @@ int s390_kvm_mmu_prepare_memory_region(struct kvm *kvm,
 	}
 	scoped_guard(write_lock, &kvm->mmu_lock) {
 		kvm_s390_update_cmma_dirty(kvm, old);
-		if (change == KVM_MR_DELETE || change == KVM_MR_MOVE)
+		if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
 			rc = dat_delete_slot(kvm->arch.gmap->asce, old->base_gfn, old->npages);
+			/*
+			 * The parent entries were removed without vsie unshadow
+			 * events, and shadow gmaps do not hold references to
+			 * the host frames they map. Drop all shadows before
+			 * userspace can unmap the memory of the old slot.
+			 */
+			gmap_unshadow_all(kvm->arch.gmap);
+		}
 		if (!rc && (change == KVM_MR_MOVE || change == KVM_MR_CREATE))
 			rc = dat_create_slot(mc, kvm->arch.gmap->asce, new->base_gfn, new->npages);
 	}
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH v2 08/12] KVM: s390: Fix *_test_and_clear_softdirty() functions
  2026-12-02 18:50 ` [PATCH v2 08/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
@ 2026-12-02 19:11   ` sashiko-bot
  0 siblings, 0 replies; 14+ messages in thread
From: sashiko-bot @ 2026-12-02 19:11 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: lost dirty page tracking in
  __pte_test_and_clear_softdirty()
--

commit 45534fc2296382ab64a7218f46f3ed17915a0fe0
Author: Claudio Imbrenda <imbrenda@linux.ibm.com>

KVM: s390: Fix *_test_and_clear_softdirty() functions

This commit adjusts soft-dirty checking and clearing in
__pte_test_and_clear_softdirty() and _crste_test_and_clear_softdirty() to
handle non-present and invalid entries and skip marking zero-flagged pages
as dirty.

> diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c
> index ca196a474f833..2ba3d8e4e23dd 100644
> --- a/arch/s390/kvm/gmap/gmap.c
> +++ b/arch/s390/kvm/gmap/gmap.c
> @@ -584,8 +584,9 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p
>  						  struct gmap *gmap)
>  {
>  	union pte pte = READ_ONCE(*ptep);
> +	bool zero = pgste.zero;
>  
> -	if (!pte.s.pr || (pte.h.p && !pte.s.sd))
> +	if ((pte.h.p || pte.h.i) && !pte.s.sd)
>  		return pgste;
>  
>  	/*
> @@ -593,12 +594,16 @@ 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);
> +		pgste.zero = zero;
>  	}
>  
> -	mark_page_dirty(gmap->kvm, gfn);
> +	/* dirty but unused, does not need to be migrated */
> +	if (!zero)
> +		mark_page_dirty(gmap->kvm, gfn);
>  
>  	return pgste;
>  }

[Severity: High]
Can clearing pte.s.sd while skipping mark_page_dirty() when pgste.zero is
set lose dirty page notifications across live migration?

When a dirty page (pte.s.sd == 1) is aged by gmap_clear_young_pte(),
pte.h.i is set to 1 while pte.s.pr remains 1 and the host folio keeps the
modified contents.  If the guest then issues ESSA (for example,
ESSA_SET_VOLATILE or ESSA_SET_UNUSED), dat_perform_essa() sees ptep->h.i
and sets pgste.zero = 1, while _essa_clear_cbrl() does not zap the page
because ptep->s.pr is still 1.

When __pte_test_and_clear_softdirty() runs next, it clears pte.s.sd = 0 and
skips mark_page_dirty().  A subsequent guest read fault resolves in
_gmap_handle_minor_pte_fault() by clearing newpte.h.i = 0 and pgste.zero = 0
without setting newpte.s.sd = 1, leaving the modified resident page omitted
from the dirty log.  In addition, because __dat_get_cmma_pte() only exports
pgste.usage and pgste.nodat (not pgste.zero), skipping mark_page_dirty()
for a page already transferred in an earlier migration round leaves stale
data on the destination host.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202185020.241132-1-imbrenda@linux.ibm.com?part=8

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-12-02 19:11 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 18:50 [PATCH v2 00/12] KVM: s390: More fixes Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 01/12] KVM: s390: ucontrol: Do not mask -ENOMEM when translating Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 02/12] KVM: s390: Fix potential leak in kvm_s390_get_io_int() Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 03/12] KVM: s390: Small miscellaneous CMMA fixes Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 04/12] KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 05/12] KVM: s390: Fix soft-dirty handling in _gmap_link() Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 06/12] KVM: s390: Fix race in gmap_clear_young_pte() Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 07/12] KVM: s390: Properly adjust page size before calling _gmap_link() Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 08/12] KVM: s390: Fix *_test_and_clear_softdirty() functions Claudio Imbrenda
2026-12-02 19:11   ` sashiko-bot
2026-12-02 18:50 ` [PATCH v2 09/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 10/12] KVM: s390: Disallow 2G pages for guests up to 2G Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 11/12] KVM: s390: vsie: Fix improper shadowing of non-present host ptes Claudio Imbrenda
2026-12-02 18:50 ` [PATCH v2 12/12] KVM: s390: vsie: Drop shadow gmaps when a memslot is deleted or moved Claudio Imbrenda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox