Skip to contentSkip to conversation

KVM: s390: fix a newly introduced bug

PATCH Superseded Claudio Imbrenda 2 participants 6 messages Mar 4, 2025 – Mar 7, 2025

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

v4 cover KVM: s390: fix a newly introduced bug

Fix race when making a page secure (hold pte lock again)

This should fix the issues I have seen, which I think/hope are also the same issues that David found.

v3->v4:
* move and rename s390_wiggle_split_folio() to fix a compile issue when KVM is not selected
* removed obsolete reference to __() from comments

v2->v3:
* added check for pte_write() in make_hva_secure() [thanks David]

v1->v2:
* major refactoring
* walk the page tables only once
* when importing, manually fault in pages if needed

Claudio Imbrenda (1):
KVM: s390: pv: fix race when making a page secure

 arch/s390/include/asm/gmap.h |   1 -
 arch/s390/include/asm/uv.h   |   3 +-
 arch/s390/kernel/uv.c        | 135 +++++++++++++++++++++++++++++++++--
 arch/s390/kvm/gmap.c         | 101 ++------------------------
 arch/s390/kvm/kvm-s390.c     |  25 ++++---
 arch/s390/mm/gmap.c          |  28 --------
 6 files changed, 153 insertions(+), 140 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH v4 1/1 KVM: s390: pv: fix race when making a page secure

Holding the pte lock for the page that is being converted to secure is needed to avoid races. A previous commit removed the locking, which caused issues. Fix by locking the pte again.

Fixes 5cbe24350b7d ("KVM: s390: move pv gmap functions into kvm")
---
 arch/s390/include/asm/gmap.h |   1 -
 arch/s390/include/asm/uv.h   |   3 +-
 arch/s390/kernel/uv.c        | 135 +++++++++++++++++++++++++++++++++--
 arch/s390/kvm/gmap.c         | 101 ++------------------------
 arch/s390/kvm/kvm-s390.c     |  25 ++++---
 arch/s390/mm/gmap.c          |  28 --------
 6 files changed, 153 insertions(+), 140 deletions(-)
arch/s390/include/asm/gmap.h +0 -1
index 4e73ef46d4b2..9f2814d0e1e9 100644
@@ -139,7 +139,6 @@ int s390_replace_asce(struct gmap *gmap);
 void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns);
 int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
 			    unsigned long end, bool interruptible);
-int kvm_s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split);
 unsigned long *gmap_table_walk(struct gmap *gmap, unsigned long gaddr, int level);
 
 /**
arch/s390/include/asm/uv.h +2 -1
index b11f5b6d0bd1..fa33a6ff2fab 100644
@@ -631,9 +631,10 @@ int uv_pin_shared(unsigned long paddr);
 int uv_destroy_folio(struct folio *folio);
 int uv_destroy_pte(pte_t pte);
 int uv_convert_from_secure_pte(pte_t pte);
-int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb);
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb);
 int uv_convert_from_secure(unsigned long paddr);
 int uv_convert_from_secure_folio(struct folio *folio);
+int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split);
 
 void setup_uv(void);
 
arch/s390/kernel/uv.c +129 -6

163 lines not on this page · raw message

arch/s390/kvm/gmap.c +7 -94

121 lines not on this page · raw message

arch/s390/kvm/kvm-s390.c +15 -10
index ebecb96bacce..020502af7dc9 100644
@@ -4952,6 +4952,7 @@ static int vcpu_post_run_handle_fault(struct kvm_vcpu *vcpu)
 {
 	unsigned int flags = 0;
 	unsigned long gaddr;
+	int rc;
 
 	gaddr = current->thread.gmap_teid.addr * PAGE_SIZE;
 	if (kvm_s390_cur_gmap_fault_is_write())
@@ -4961,16 +4962,6 @@ static int vcpu_post_run_handle_fault(struct kvm_vcpu *vcpu)
 	case 0:
 		vcpu->stat.exit_null++;
 		break;
-	case PGM_NON_SECURE_STORAGE_ACCESS:
-		kvm_s390_assert_primary_as(vcpu);
-		/*
-		 * This is normal operation; a page belonging to a protected
-		 * guest has not been imported yet. Try to import the page into
-		 * the protected guest.
-		 */
-		if (gmap_convert_to_secure(vcpu->arch.gmap, gaddr) == -EINVAL)
-			send_sig(SIGSEGV, current, 0);
-		break;
 	case PGM_SECURE_STORAGE_ACCESS:
 	case PGM_SECURE_STORAGE_VIOLATION:
 		kvm_s390_assert_primary_as(vcpu);
@@ -4995,6 +4986,20 @@ static int vcpu_post_run_handle_fault(struct kvm_vcpu *vcpu)
 			send_sig(SIGSEGV, current, 0);
 		}
 		break;
+	case PGM_NON_SECURE_STORAGE_ACCESS:
+		kvm_s390_assert_primary_as(vcpu);
+		/*
+		 * This is normal operation; a page belonging to a protected
+		 * guest has not been imported yet. Try to import the page into
+		 * the protected guest.
+		 */
+		rc = gmap_convert_to_secure(vcpu->arch.gmap, gaddr);
+		if (rc == -EINVAL)
+			send_sig(SIGSEGV, current, 0);
+		if (rc != -ENXIO)
+			break;
+		flags = FAULT_FLAG_WRITE;
+		fallthrough;
 	case PGM_PROTECTION:
 	case PGM_SEGMENT_TRANSLATION:
 	case PGM_PAGE_TRANSLATION:
arch/s390/mm/gmap.c +0 -28
index 94d927785800..d14b488e7a1f 100644
@@ -2626,31 +2626,3 @@ int s390_replace_asce(struct gmap *gmap)
 	return 0;
 }
 EXPORT_SYMBOL_GPL(s390_replace_asce);
-
-/**
- * kvm_s390_wiggle_split_folio() - try to drain extra references to a folio and optionally split
- * @mm:    the mm containing the folio to work on
- * @folio: the folio
- * @split: whether to split a large folio
- *
- * Context: Must be called while holding an extra reference to the folio;
- *          the mm lock should not be held.
- */
-int kvm_s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split)
-{
-	int rc;
-
-	lockdep_assert_not_held(&mm->mmap_lock);
-	folio_wait_writeback(folio);
-	lru_add_drain_all();
-	if (split) {
-		folio_lock(folio);
-		rc = split_folio(folio);
-		folio_unlock(folio);
-
-		if (rc != -EBUSY)
-			return rc;
-	}
-	return -EAGAIN;
-}
-EXPORT_SYMBOL_GPL(kvm_s390_wiggle_split_folio);
Signature · 1 line
2.48.1
#2 lore raw

Re: [PATCH v4 1/1] KVM: s390: pv: fix race when making a page secure

Quoted 7 lines

/**
- * make_folio_secure() - make a folio secure
+ * __make_folio_secure() - make a folio secure
* @folio: the folio to make secure
* @uvcb: the uvcb that describes the UVC to be used *

arch/s390/kernel/uv.c static int expected_folio_refs(struct folio *folio) · line 282

Quoted 9 lines
arch/s390/kernel/uv.c +3 -2
@@ -243,14 +276,13 @@ static int expected_folio_refs(struct folio *folio)
   *         -EINVAL if the UVC failed for other reasons.
   *
   * Context: The caller must hold exactly one extra reference on the folio
- *          (it's the same logic as split_folio())
+ *          (it's the same logic as split_folio()), and the folio must be
+ *          locked.
   */
-int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)
+static int __make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)

One more nit: -EBUSY can no longer be returned from his function, so you might just remove it from the doc above.

While chasing a very weird folio split bug that seems to result in late validation issues (:/), I was wondering if __gmap_destroy_page could similarly be problematic.

We're now no longer holding the PTL while performing the operation.

(not that that would explain the issue I am chasing, because gmap_destroy_page() is never called in my setup)

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw

Re: [PATCH v4 1/1] KVM: s390: pv: fix race when making a page secure

Quoted 21 lines · David Hildenbrand
Quoted 7 lines
   /**

- * make_folio_secure() - make a folio secure
+ * __make_folio_secure() - make a folio secure
* @folio: the folio to make secure
* @uvcb: the uvcb that describes the UVC to be used *

Review static int expected_folio_refs(struct folio *folio) · line 282

Quoted 9 lines
(quoted diff) +3 -2
@@ -243,14 +276,13 @@ static int expected_folio_refs(struct folio *folio)
    *         -EINVAL if the UVC failed for other reasons.
    *
    * Context: The caller must hold exactly one extra reference on the folio
- *          (it's the same logic as split_folio())
+ *          (it's the same logic as split_folio()), and the folio must be
+ *          locked.
    */
-int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)
+static int __make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)

One more nit: -EBUSY can no longer be returned from his function, so you might just remove it from the doc above.

Quoted 9 lines

While chasing a very weird folio split bug that seems to result in late validation issues (:/), I was wondering if __gmap_destroy_page could similarly be problematic.

We're now no longer holding the PTL while performing the operation.

(not that that would explain the issue I am chasing, because gmap_destroy_page() is never called in my setup)

Okay, I've been debugging for way to long the weird issue I am seeing, and I did not find the root cause yet. But the following things are problematic:

1) To walk the page tables, we need the mmap lock in read mode.

2) To walk the page tables, we must know that a VMA exists

3) get_locked_pte() must not be used on hugetlb areas.

Further, the following things should be cleaned up

4) s390_wiggle_split_folio() is only used in that file

5) gmap_make_secure() likely should be returning -EFAULT

See below, I went with a folio_walk (which also checks for pte_present() like the old code did, but that should not matter here) so we can get rid of the get_locked_pte() usage completely.

From 1b9a4306b79a352daf80708252d166114e7335de Mon Sep 17 00:00:00 2001
From: David Hildenbrand <[email protected]>
Date: Thu, 6 Mar 2025 22:43:43 +0100
Subject: [PATCH] merge

---
 arch/s390/include/asm/uv.h |  1 -
 arch/s390/kernel/uv.c      | 41 ++++++++++++++++++--------------------
 arch/s390/kvm/gmap.c       |  2 +-
 3 files changed, 20 insertions(+), 24 deletions(-)
arch/s390/include/asm/uv.h +0 -1
index fa33a6ff2fabf..46fb0ef6f9847 100644
@@ -634,7 +634,6 @@ int uv_convert_from_secure_pte(pte_t pte);
 int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb);
 int uv_convert_from_secure(unsigned long paddr);
 int uv_convert_from_secure_folio(struct folio *folio);
-int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split);
 
 void setup_uv(void);
 
arch/s390/kernel/uv.c +19 -22

84 lines not on this page · raw message

arch/s390/kvm/gmap.c +1 -1
index 21580cfecc6ac..1a88b32e7c134 100644
@@ -41,7 +41,7 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
 
 	vmaddr = gfn_to_hva(kvm, gpa_to_gfn(gaddr));
 	if (kvm_is_error_hva(vmaddr))
-		rc = -ENXIO;
+		rc = -EFAULT;
 	else
 		rc = make_hva_secure(gmap->mm, vmaddr, uvcb);
 
Signature · 8 lines
2.48.1



-- 
Cheers,

David / dhildenb
#4 lore raw

Re: [PATCH v4 1/1] KVM: s390: pv: fix race when making a page secure

Quoted 175 lines · David Hildenbrand
Quoted 21 lines · David Hildenbrand
Quoted 7 lines
    /**

- * make_folio_secure() - make a folio secure
+ * __make_folio_secure() - make a folio secure
* @folio: the folio to make secure
* @uvcb: the uvcb that describes the UVC to be used *

Review static int expected_folio_refs(struct folio *folio) · line 282

Quoted 9 lines
(quoted diff) +3 -2
@@ -243,14 +276,13 @@ static int expected_folio_refs(struct folio *folio)
     *         -EINVAL if the UVC failed for other reasons.
     *
     * Context: The caller must hold exactly one extra reference on the folio
- *          (it's the same logic as split_folio())
+ *          (it's the same logic as split_folio()), and the folio must be
+ *          locked.
     */
-int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)
+static int __make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)

One more nit: -EBUSY can no longer be returned from his function, so you might just remove it from the doc above.

Quoted 9 lines

While chasing a very weird folio split bug that seems to result in late validation issues (:/), I was wondering if __gmap_destroy_page could similarly be problematic.

We're now no longer holding the PTL while performing the operation.

(not that that would explain the issue I am chasing, because gmap_destroy_page() is never called in my setup)

Okay, I've been debugging for way to long the weird issue I am seeing, and I did not find the root cause yet. But the following things are problematic:

1) To walk the page tables, we need the mmap lock in read mode.

2) To walk the page tables, we must know that a VMA exists

3) get_locked_pte() must not be used on hugetlb areas.

Further, the following things should be cleaned up

4) s390_wiggle_split_folio() is only used in that file

5) gmap_make_secure() likely should be returning -EFAULT

See below, I went with a folio_walk (which also checks for pte_present() like the old code did, but that should not matter here) so we can get rid of the get_locked_pte() usage completely.

From 1b9a4306b79a352daf80708252d166114e7335de Mon Sep 17 00:00:00 2001
From: David Hildenbrand <[email protected]>
Date: Thu, 6 Mar 2025 22:43:43 +0100
Subject: [PATCH] merge

---
   arch/s390/include/asm/uv.h |  1 -
   arch/s390/kernel/uv.c      | 41 ++++++++++++++++++--------------------
   arch/s390/kvm/gmap.c       |  2 +-
   3 files changed, 20 insertions(+), 24 deletions(-)
arch/s390/include/asm/uv.h +0 -1
index fa33a6ff2fabf..46fb0ef6f9847 100644
@@ -634,7 +634,6 @@ int uv_convert_from_secure_pte(pte_t pte);
   int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb);
   int uv_convert_from_secure(unsigned long paddr);
   int uv_convert_from_secure_folio(struct folio *folio);
-int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split);
 
   void setup_uv(void);
 
arch/s390/kernel/uv.c +19 -22
index 63420a5f3ee57..11a1894e63405 100644
@@ -270,7 +270,6 @@ static int expected_folio_refs(struct folio *folio)
    *
    * Return: 0 on success;
    *         -EBUSY if the folio is in writeback or has too many references;
- *         -E2BIG if the folio is large;
    *         -EAGAIN if the UVC needs to be attempted again;
    *         -ENXIO if the address is not mapped;
    *         -EINVAL if the UVC failed for other reasons.
@@ -324,17 +323,6 @@ static int make_folio_secure(struct mm_struct *mm, struct folio *folio, struct u
   	return rc;
   }
 
-static pte_t *get_locked_valid_pte(struct mm_struct *mm, unsigned long hva, spinlock_t **ptl)
-{
-	pte_t *ptep = get_locked_pte(mm, hva, ptl);
-
-	if (ptep && (pte_val(*ptep) & _PAGE_INVALID)) {
-		pte_unmap_unlock(ptep, *ptl);
-		ptep = NULL;
-	}
-	return ptep;
-}
-
   /**
    * s390_wiggle_split_folio() - try to drain extra references to a folio and optionally split
    * @mm:    the mm containing the folio to work on
@@ -344,7 +332,7 @@ static pte_t *get_locked_valid_pte(struct mm_struct *mm, unsigned long hva, spin
    * Context: Must be called while holding an extra reference to the folio;
    *          the mm lock should not be held.
    */
-int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split)
+static int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split)
   {
   	int rc;
 
@@ -361,20 +349,28 @@ int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool spli
   	}
   	return -EAGAIN;
   }
-EXPORT_SYMBOL_GPL(s390_wiggle_split_folio);
 
   int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
   {
+	struct vm_area_struct *vma;
+	struct folio_walk fw;
   	struct folio *folio;
-	spinlock_t *ptelock;
-	pte_t *ptep;
   	int rc;
 
-	ptep = get_locked_valid_pte(mm, hva, &ptelock);
-	if (!ptep)
+	mmap_read_lock(mm);
+
+	vma = vma_lookup(mm, hva);
+	if (!vma) {
+		mmap_read_unlock(mm);
+		return -EFAULT;
+	}
+
+	folio = folio_walk_start(&fw, vma, hva, 0);
+	if (!folio) {
+		mmap_read_unlock(mm);
   		return -ENXIO;
+	}
 
-	folio = page_folio(pte_page(*ptep));
   	folio_get(folio);
   	/*
   	 * Secure pages cannot be huge and userspace should not combine both.
@@ -385,14 +381,15 @@ int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header
   	 * KVM_RUN will return -EFAULT.
   	 */
   	if (folio_test_hugetlb(folio))
-		rc =  -EFAULT;
+		rc = -EFAULT;
   	else if (folio_test_large(folio))
   		rc = -E2BIG;
-	else if (!pte_write(*ptep))
+	else if (!pte_write(fw.pte) || (pte_val(fw.pte) & _PAGE_INVALID))
   		rc = -ENXIO;
   	else
   		rc = make_folio_secure(mm, folio, uvcb);
-	pte_unmap_unlock(ptep, ptelock);
+	folio_walk_end(&fw, vma);
+	mmap_read_unlock(mm);
 
   	if (rc == -E2BIG || rc == -EBUSY)
   		rc = s390_wiggle_split_folio(mm, folio, rc == -E2BIG);
arch/s390/kvm/gmap.c +0 -0
index 21580cfecc6ac..1a88b32e7c134 100644

arch/s390/kvm/gmap.c int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb) · line 47

Quoted 8 lines
arch/s390/kvm/gmap.c +1 -1
@@ -41,7 +41,7 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
 
   	vmaddr = gfn_to_hva(kvm, gpa_to_gfn(gaddr));
   	if (kvm_is_error_hva(vmaddr))
-		rc = -ENXIO;
+		rc = -EFAULT;
   	else
   		rc = make_hva_secure(gmap->mm, vmaddr, uvcb);
 

The following likely on top as a cleanup as well:

arch/s390/kvm/gmap.c +2 -6
index 1a88b32e7c134..6d8944d1b4a0c 100644
@@ -35,17 +35,13 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
 {
        struct kvm *kvm = gmap->private;
        unsigned long vmaddr;
-       int rc = 0;
 
        lockdep_assert_held(&kvm->srcu);
 
        vmaddr = gfn_to_hva(kvm, gpa_to_gfn(gaddr));
        if (kvm_is_error_hva(vmaddr))
-               rc = -EFAULT;
-       else
-               rc = make_hva_secure(gmap->mm, vmaddr, uvcb);
-
-       return rc;
+               return -EFAULT;
+       return make_hva_secure(gmap->mm, vmaddr, uvcb);
 }
 
 int gmap_convert_to_secure(struct gmap *gmap, unsigned long gaddr)
Signature · 3 lines
Cheers,

David / dhildenb
author maintainer
#5 lore raw

Re: [PATCH v4 1/1] KVM: s390: pv: fix race when making a page secure

In reply to David Hildenbrand
Quoted 43 lines · On Thu, 6 Mar 2025 23:07:04 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 21 lines · David Hildenbrand
Quoted 7 lines
   /**

- * make_folio_secure() - make a folio secure
+ * __make_folio_secure() - make a folio secure
* @folio: the folio to make secure
* @uvcb: the uvcb that describes the UVC to be used *

Review static int expected_folio_refs(struct folio *folio) · line 282

Quoted 9 lines
(quoted diff) +3 -2
@@ -243,14 +276,13 @@ static int expected_folio_refs(struct folio *folio)
    *         -EINVAL if the UVC failed for other reasons.
    *
    * Context: The caller must hold exactly one extra reference on the folio
- *          (it's the same logic as split_folio())
+ *          (it's the same logic as split_folio()), and the folio must be
+ *          locked.
    */
-int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)
+static int __make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb)  

One more nit: -EBUSY can no longer be returned from his function, so you might just remove it from the doc above.

Quoted 9 lines

While chasing a very weird folio split bug that seems to result in late validation issues (:/), I was wondering if __gmap_destroy_page could similarly be problematic.

We're now no longer holding the PTL while performing the operation.

(not that that would explain the issue I am chasing, because gmap_destroy_page() is never called in my setup)

Okay, I've been debugging for way to long the weird issue I am seeing, and I did not find the root cause yet. But the following things are problematic:

1) To walk the page tables, we need the mmap lock in read mode.

2) To walk the page tables, we must know that a VMA exists

3) get_locked_pte() must not be used on hugetlb areas.

Further, the following things should be cleaned up

Quoted 8 lines

4) s390_wiggle_split_folio() is only used in that file

5) gmap_make_secure() likely should be returning -EFAULT

See below, I went with a folio_walk (which also checks for pte_present() like the old code did, but that should not matter here) so we can get rid of the get_locked_pte() usage completely.

I shall merge this into my patch, thanks a lot!

Quoted 132 lines

From 1b9a4306b79a352daf80708252d166114e7335de Mon Sep 17 00:00:00 2001
From: David Hildenbrand <[email protected]>
Date: Thu, 6 Mar 2025 22:43:43 +0100
Subject: [PATCH] merge

---
  arch/s390/include/asm/uv.h |  1 -
  arch/s390/kernel/uv.c      | 41 ++++++++++++++++++--------------------
  arch/s390/kvm/gmap.c       |  2 +-
  3 files changed, 20 insertions(+), 24 deletions(-)
arch/s390/include/asm/uv.h +0 -1
index fa33a6ff2fabf..46fb0ef6f9847 100644
@@ -634,7 +634,6 @@ int uv_convert_from_secure_pte(pte_t pte);
  int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb);
  int uv_convert_from_secure(unsigned long paddr);
  int uv_convert_from_secure_folio(struct folio *folio);
-int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split);
 
  void setup_uv(void);
 
arch/s390/kernel/uv.c +19 -22
index 63420a5f3ee57..11a1894e63405 100644
@@ -270,7 +270,6 @@ static int expected_folio_refs(struct folio *folio)
   *
   * Return: 0 on success;
   *         -EBUSY if the folio is in writeback or has too many references;
- *         -E2BIG if the folio is large;
   *         -EAGAIN if the UVC needs to be attempted again;
   *         -ENXIO if the address is not mapped;
   *         -EINVAL if the UVC failed for other reasons.
@@ -324,17 +323,6 @@ static int make_folio_secure(struct mm_struct *mm, struct folio *folio, struct u
  	return rc;
  }
 
-static pte_t *get_locked_valid_pte(struct mm_struct *mm, unsigned long hva, spinlock_t **ptl)
-{
-	pte_t *ptep = get_locked_pte(mm, hva, ptl);
-
-	if (ptep && (pte_val(*ptep) & _PAGE_INVALID)) {
-		pte_unmap_unlock(ptep, *ptl);
-		ptep = NULL;
-	}
-	return ptep;
-}
-
  /**
   * s390_wiggle_split_folio() - try to drain extra references to a folio and optionally split
   * @mm:    the mm containing the folio to work on
@@ -344,7 +332,7 @@ static pte_t *get_locked_valid_pte(struct mm_struct *mm, unsigned long hva, spin
   * Context: Must be called while holding an extra reference to the folio;
   *          the mm lock should not be held.
   */
-int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split)
+static int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool split)
  {
  	int rc;
 
@@ -361,20 +349,28 @@ int s390_wiggle_split_folio(struct mm_struct *mm, struct folio *folio, bool spli
  	}
  	return -EAGAIN;
  }
-EXPORT_SYMBOL_GPL(s390_wiggle_split_folio);
 
  int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
  {
+	struct vm_area_struct *vma;
+	struct folio_walk fw;
  	struct folio *folio;
-	spinlock_t *ptelock;
-	pte_t *ptep;
  	int rc;
 
-	ptep = get_locked_valid_pte(mm, hva, &ptelock);
-	if (!ptep)
+	mmap_read_lock(mm);
+
+	vma = vma_lookup(mm, hva);
+	if (!vma) {
+		mmap_read_unlock(mm);
+		return -EFAULT;
+	}
+
+	folio = folio_walk_start(&fw, vma, hva, 0);
+	if (!folio) {
+		mmap_read_unlock(mm);
  		return -ENXIO;
+	}
 
-	folio = page_folio(pte_page(*ptep));
  	folio_get(folio);
  	/*
  	 * Secure pages cannot be huge and userspace should not combine both.
@@ -385,14 +381,15 @@ int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header
  	 * KVM_RUN will return -EFAULT.
  	 */
  	if (folio_test_hugetlb(folio))
-		rc =  -EFAULT;
+		rc = -EFAULT;
  	else if (folio_test_large(folio))
  		rc = -E2BIG;
-	else if (!pte_write(*ptep))
+	else if (!pte_write(fw.pte) || (pte_val(fw.pte) & _PAGE_INVALID))
  		rc = -ENXIO;
  	else
  		rc = make_folio_secure(mm, folio, uvcb);
-	pte_unmap_unlock(ptep, ptelock);
+	folio_walk_end(&fw, vma);
+	mmap_read_unlock(mm);
 
  	if (rc == -E2BIG || rc == -EBUSY)
  		rc = s390_wiggle_split_folio(mm, folio, rc == -E2BIG);
arch/s390/kvm/gmap.c +1 -1
index 21580cfecc6ac..1a88b32e7c134 100644
@@ -41,7 +41,7 @@ int gmap_make_secure(struct gmap *gmap, unsigned long gaddr, void *uvcb)
 
  	vmaddr = gfn_to_hva(kvm, gpa_to_gfn(gaddr));
  	if (kvm_is_error_hva(vmaddr))
-		rc = -ENXIO;
+		rc = -EFAULT;
  	else
  		rc = make_hva_secure(gmap->mm, vmaddr, uvcb);