Linux s390 Architecture 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 07/12] KVM: s390: Fix race in gmap_clear_young_pte()
Date: Fri, 27 Nov 2026 16:04:06 +0100	[thread overview]
Message-ID: <20261127150411.221072-8-imbrenda@linux.ibm.com> (raw)
In-Reply-To: <20261127150411.221072-1-imbrenda@linux.ibm.com>

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


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

Thread overview: 18+ 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:47   ` sashiko-bot
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 ` Claudio Imbrenda [this message]
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
2026-11-27 15:47 ` [PATCH v1 00/12] KVM: s390: More fixes sashiko-bot

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-8-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