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