public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/3] KVM: s390: some vSIE and UCONTROL fixes
@ 2026-05-06 14:11 Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 1/3] KVM: s390: vsie: Fix memory leak when unshadowing Claudio Imbrenda
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Claudio Imbrenda @ 2026-05-06 14:11 UTC (permalink / raw)
  To: linux-kernel
  Cc: kvm, linux-s390, borntraeger, frankja, david, seiden, nrb,
	schlameuss, gra

Fix some memory leaks and some hangs in vSIE.

This is still a fallout from the gmap rewrite.

Claudio Imbrenda (3):
  KVM: s390: vsie: Fix memory leak when unshadowing
  KVM: s390: Fix memory leak in UCONTROL path
  KVM: s390: vsie: Fix unshadowing logic

 arch/s390/kvm/dat.h  |  3 ++-
 arch/s390/kvm/gmap.c |  9 ++++++---
 arch/s390/kvm/gmap.h | 12 ++++++++++--
 3 files changed, 18 insertions(+), 6 deletions(-)

-- 
2.54.0


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

* [PATCH v1 1/3] KVM: s390: vsie: Fix memory leak when unshadowing
  2026-05-06 14:11 [PATCH v1 0/3] KVM: s390: some vSIE and UCONTROL fixes Claudio Imbrenda
@ 2026-05-06 14:11 ` Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 2/3] KVM: s390: Fix memory leak in UCONTROL path Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 3/3] KVM: s390: vsie: Fix unshadowing logic Claudio Imbrenda
  2 siblings, 0 replies; 4+ messages in thread
From: Claudio Imbrenda @ 2026-05-06 14:11 UTC (permalink / raw)
  To: linux-kernel
  Cc: kvm, linux-s390, borntraeger, frankja, david, seiden, nrb,
	schlameuss, gra

When performing a partial unshadowing, the rmap was being leaked.

Add the missing kfree().

Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
---
 arch/s390/kvm/gmap.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/s390/kvm/gmap.c b/arch/s390/kvm/gmap.c
index 3c26e35af0ef..fd1927761980 100644
--- a/arch/s390/kvm/gmap.c
+++ b/arch/s390/kvm/gmap.c
@@ -1143,8 +1143,10 @@ void _gmap_handle_vsie_unshadow_event(struct gmap *parent, gfn_t gfn)
 		}
 		scoped_guard(spinlock, &sg->host_to_rmap_lock)
 			head = radix_tree_delete(&sg->host_to_rmap, gfn);
-		gmap_for_each_rmap_safe(rmap, rnext, head)
+		gmap_for_each_rmap_safe(rmap, rnext, head) {
 			gmap_unshadow_level(sg, rmap->r_gfn, rmap->level);
+			kfree(rmap);
+		}
 	}
 }
 
-- 
2.54.0


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

* [PATCH v1 2/3] KVM: s390: Fix memory leak in UCONTROL path
  2026-05-06 14:11 [PATCH v1 0/3] KVM: s390: some vSIE and UCONTROL fixes Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 1/3] KVM: s390: vsie: Fix memory leak when unshadowing Claudio Imbrenda
@ 2026-05-06 14:11 ` Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 3/3] KVM: s390: vsie: Fix unshadowing logic Claudio Imbrenda
  2 siblings, 0 replies; 4+ messages in thread
From: Claudio Imbrenda @ 2026-05-06 14:11 UTC (permalink / raw)
  To: linux-kernel
  Cc: kvm, linux-s390, borntraeger, frankja, david, seiden, nrb,
	schlameuss, gra

Fix a memory leak that can happen if gmap_ucas_map_one() or
kvm_s390_mmu_cache_topup() return error values.

Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
Reported-by: Jiaxin Fan <jiaxin.fan@ibm.com>
---
 arch/s390/kvm/gmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/s390/kvm/gmap.c b/arch/s390/kvm/gmap.c
index fd1927761980..c1140da0689d 100644
--- a/arch/s390/kvm/gmap.c
+++ b/arch/s390/kvm/gmap.c
@@ -822,8 +822,8 @@ int gmap_ucas_translate(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, gpa_t
 
 int gmap_ucas_map(struct gmap *gmap, gfn_t p_gfn, gfn_t c_gfn, unsigned long count)
 {
-	struct kvm_s390_mmu_cache *mc;
-	int rc;
+	struct kvm_s390_mmu_cache *mc __free(kvm_s390_mmu_cache) = NULL;
+	int rc = 0;
 
 	mc = kvm_s390_new_mmu_cache();
 	if (!mc)
-- 
2.54.0


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

* [PATCH v1 3/3] KVM: s390: vsie: Fix unshadowing logic
  2026-05-06 14:11 [PATCH v1 0/3] KVM: s390: some vSIE and UCONTROL fixes Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 1/3] KVM: s390: vsie: Fix memory leak when unshadowing Claudio Imbrenda
  2026-05-06 14:11 ` [PATCH v1 2/3] KVM: s390: Fix memory leak in UCONTROL path Claudio Imbrenda
@ 2026-05-06 14:11 ` Claudio Imbrenda
  2 siblings, 0 replies; 4+ messages in thread
From: Claudio Imbrenda @ 2026-05-06 14:11 UTC (permalink / raw)
  To: linux-kernel
  Cc: kvm, linux-s390, borntraeger, frankja, david, seiden, nrb,
	schlameuss, gra

In some cases (i.e. under extreme memory pressure on the host),
attempting to shadow memory will result in the same memory being
unshadowed, causing a loop.

Add a PGSTE bit to distinguish between shadowed memory and shadowed DAT
tables, fix the unshadowing logic in _gmap_ptep_xchg() to prevent
unnecessary unshadowing and perform better checks.

Also fix the unshadowing logic in _gmap_crstep_xchg_atomic() which did
not unshadow properly when the large page would become unprotected.

Signed-off-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Fixes: a2c17f9270cc ("KVM: s390: New gmap code")
---
 arch/s390/kvm/dat.h  |  3 ++-
 arch/s390/kvm/gmap.c |  1 +
 arch/s390/kvm/gmap.h | 12 ++++++++++--
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/arch/s390/kvm/dat.h b/arch/s390/kvm/dat.h
index 8f8278c44879..873e13ac5a27 100644
--- a/arch/s390/kvm/dat.h
+++ b/arch/s390/kvm/dat.h
@@ -145,7 +145,8 @@ union pgste {
 		unsigned long cmma_d       : 1; /* Dirty flag for CMMA bits */
 		unsigned long prefix_notif : 1; /* Guest prefix invalidation notification */
 		unsigned long vsie_notif   : 1; /* Referenced in a shadow table */
-		unsigned long              : 5;
+		unsigned long vsie_gmem    : 1; /* Contains nested guest memory */
+		unsigned long              : 4;
 		unsigned long              : 8;
 	};
 	struct {
diff --git a/arch/s390/kvm/gmap.c b/arch/s390/kvm/gmap.c
index c1140da0689d..08fb806c9c36 100644
--- a/arch/s390/kvm/gmap.c
+++ b/arch/s390/kvm/gmap.c
@@ -1053,6 +1053,7 @@ int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gf
 	pte.h.p = 1;
 	pgste = _gmap_ptep_xchg(sg->parent, ptep, pte, pgste, p_gfn, false);
 	pgste.vsie_notif = 1;
+	pgste.vsie_gmem |= level == TABLE_TYPE_PAGE_TABLE;
 	pgste_set_unlock(ptep, pgste);
 
 	return 0;
diff --git a/arch/s390/kvm/gmap.h b/arch/s390/kvm/gmap.h
index 96ee1395a592..f4caa3f42c0e 100644
--- a/arch/s390/kvm/gmap.h
+++ b/arch/s390/kvm/gmap.h
@@ -167,6 +167,13 @@ static inline bool gmap_unmap_prefix(struct gmap *gmap, gfn_t gfn, gfn_t end)
 	return _gmap_unmap_prefix(gmap, gfn, end, false);
 }
 
+static inline bool needs_unshadow(union pte oldpte, union pte newpte, union pgste pgste)
+{
+	if (pgste.vsie_gmem)
+		return (oldpte.h.p != newpte.h.p) || newpte.h.i;
+	return (oldpte.h.p && !newpte.h.p) || !newpte.s.pr;
+}
+
 static inline union pgste _gmap_ptep_xchg(struct gmap *gmap, union pte *ptep, union pte newpte,
 					  union pgste pgste, gfn_t gfn, bool needs_lock)
 {
@@ -180,8 +187,9 @@ static inline union pgste _gmap_ptep_xchg(struct gmap *gmap, union pte *ptep, un
 		pgste.prefix_notif = 0;
 		gmap_unmap_prefix(gmap, gfn, gfn + 1);
 	}
-	if (pgste.vsie_notif && (ptep->h.p != newpte.h.p || newpte.h.i)) {
+	if (pgste.vsie_notif && needs_unshadow(*ptep, newpte, pgste)) {
 		pgste.vsie_notif = 0;
+		pgste.vsie_gmem = 0;
 		if (needs_lock)
 			gmap_handle_vsie_unshadow_event(gmap, gfn);
 		else
@@ -217,7 +225,7 @@ static inline bool __must_check _gmap_crstep_xchg_atomic(struct gmap *gmap, unio
 		gmap_unmap_prefix(gmap, gfn, gfn + align);
 	}
 	if (crste_leaf(oldcrste) && oldcrste.s.fc1.vsie_notif &&
-	    (newcrste.h.p || newcrste.h.i || !newcrste.s.fc1.vsie_notif)) {
+	    ((newcrste.h.p != oldcrste.h.p) || newcrste.h.i || !newcrste.s.fc1.vsie_notif)) {
 		newcrste.s.fc1.vsie_notif = 0;
 		if (needs_lock)
 			gmap_handle_vsie_unshadow_event(gmap, gfn);
-- 
2.54.0


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

end of thread, other threads:[~2026-05-06 14:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-06 14:11 [PATCH v1 0/3] KVM: s390: some vSIE and UCONTROL fixes Claudio Imbrenda
2026-05-06 14:11 ` [PATCH v1 1/3] KVM: s390: vsie: Fix memory leak when unshadowing Claudio Imbrenda
2026-05-06 14:11 ` [PATCH v1 2/3] KVM: s390: Fix memory leak in UCONTROL path Claudio Imbrenda
2026-05-06 14:11 ` [PATCH v1 3/3] KVM: s390: vsie: Fix unshadowing logic Claudio Imbrenda

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