Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Claudio Imbrenda <imbrenda@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: linux-s390@vger.kernel.org, borntraeger@de.ibm.com,
	frankja@linux.ibm.com, david@kernel.org, nrb@linux.ibm.com,
	schlameuss@linux.ibm.com, gra@linux.ibm.com,
	seiden@linux.ibm.com
Subject: [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit
Date: Fri, 27 Nov 2026 16:04:09 +0100	[thread overview]
Message-ID: <20261127150411.221072-11-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20261127150411.221072-1-imbrenda@linux.ibm.com>

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


  parent reply	other threads:[~2026-11-27 15:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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: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:04 ` Claudio Imbrenda [this message]
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

Reply instructions:

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

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

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

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

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

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

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