Skip to contentSkip to conversation

KVM: s390: Fix gmap_helper_zap_one_page() again

PATCH In review Claudio Imbrenda 2 participants 2 messages Nov 6, 2025 – Nov 10, 2025

Subsystems (4)
lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

PATCH 1/1 KVM: s390: Fix gmap_helper_zap_one_page() again

A few checks were missing in gmap_helper_zap_one_page(), which can lead to memory corruption in the guest under specific circumstances.

Add the missing checks.

Fixes 5deafa27d9ae ("KVM: s390: Fix to clear PTE when discarding a swapped page")
---
 arch/s390/mm/gmap_helpers.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/s390/mm/gmap_helpers.c +7 -2
index d4c3c36855e2..38a2d82cd88a 100644
@@ -47,6 +47,7 @@ static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
 void gmap_helper_zap_one_page(struct mm_struct *mm, unsigned long vmaddr)
 {
 	struct vm_area_struct *vma;
+	unsigned long pgstev;
 	spinlock_t *ptl;
 	pgste_t pgste;
 	pte_t *ptep;
@@ -65,9 +66,13 @@ void gmap_helper_zap_one_page(struct mm_struct *mm, unsigned long vmaddr)
 	if (pte_swap(*ptep)) {
 		preempt_disable();
 		pgste = pgste_get_lock(ptep);
+		pgstev = pgste_val(pgste);
 
-		ptep_zap_swap_entry(mm, pte_to_swp_entry(*ptep));
-		pte_clear(mm, vmaddr, ptep);
+		if ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
+		    (pgstev & _PGSTE_GPS_ZERO)) {
+			ptep_zap_swap_entry(mm, pte_to_swp_entry(*ptep));
+			pte_clear(mm, vmaddr, ptep);
+		}
 
 		pgste_set_unlock(ptep, pgste);
 		preempt_enable();
Signature · 1 line
2.51.1
Tested-by on v1 1/1
#1 lore raw
Quoted 42 lines · Claudio Imbrenda

A few checks were missing in gmap_helper_zap_one_page(), which can lead to memory corruption in the guest under specific circumstances.

Add the missing checks.

Fixes 5deafa27d9ae ("KVM: s390: Fix to clear PTE when discarding a swapped page")
---
 arch/s390/mm/gmap_helpers.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/s390/mm/gmap_helpers.c +7 -2
index d4c3c36855e2..38a2d82cd88a 100644
@@ -47,6 +47,7 @@ static void ptep_zap_swap_entry(struct mm_struct *mm, swp_entry_t entry)
 void gmap_helper_zap_one_page(struct mm_struct *mm, unsigned long vmaddr)
 {
 	struct vm_area_struct *vma;
+	unsigned long pgstev;
 	spinlock_t *ptl;
 	pgste_t pgste;
 	pte_t *ptep;
@@ -65,9 +66,13 @@ void gmap_helper_zap_one_page(struct mm_struct *mm, unsigned long vmaddr)
 	if (pte_swap(*ptep)) {
 		preempt_disable();
 		pgste = pgste_get_lock(ptep);
+		pgstev = pgste_val(pgste);
 
-		ptep_zap_swap_entry(mm, pte_to_swp_entry(*ptep));
-		pte_clear(mm, vmaddr, ptep);
+		if ((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED ||
+		    (pgstev & _PGSTE_GPS_ZERO)) {
+			ptep_zap_swap_entry(mm, pte_to_swp_entry(*ptep));
+			pte_clear(mm, vmaddr, ptep);
+		}
 
 		pgste_set_unlock(ptep, pgste);
 		preempt_enable();
Signature · 1 line
2.51.1

Thanks for the fix.