Skip to contentSkip to conversation

KVM: s390: fix a newly introduced bug

PATCH Superseded Claudio Imbrenda 2 participants 5 messages Feb 27, 2025 – Mar 4, 2025

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

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

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/uv.h |   2 +-
 arch/s390/kernel/uv.c      | 107 ++++++++++++++++++++++++++++++++++---
 arch/s390/kvm/gmap.c       |  99 +++-------------------------------
 arch/s390/kvm/kvm-s390.c   |  25 +++++----
 4 files changed, 123 insertions(+), 110 deletions(-)
Signature · 1 line
2.48.1
author maintainer
#1 lore raw

PATCH v3 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/uv.h |   2 +-
 arch/s390/kernel/uv.c      | 107 ++++++++++++++++++++++++++++++++++---
 arch/s390/kvm/gmap.c       |  99 +++-------------------------------
 arch/s390/kvm/kvm-s390.c   |  25 +++++----
 4 files changed, 123 insertions(+), 110 deletions(-)
arch/s390/include/asm/uv.h +1 -1
index b11f5b6d0bd1..46fb0ef6f984 100644
@@ -631,7 +631,7 @@ 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);
 
arch/s390/kernel/uv.c +101 -6

135 lines not on this page · raw message

arch/s390/kvm/gmap.c +6 -93

115 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:
Signature · 1 line
2.48.1
Tested-by Reviewed-by on v3 1/1
#2 lore raw

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

Quoted 7 lines · Claudio Imbrenda

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

Tested with shmem / memory-backend-memfd that ends up using large folios / THPs.

Two comments below.

[...]

Quoted 4 lines
arch/s390/kernel/uv.c +4 -0
+
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
+{
+	struct folio *folio;

arch/s390/kernel/uv.c int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb) · line 350

Quoted 10 lines
arch/s390/kernel/uv.c +10 -0
+	spinlock_t *ptelock;
+	pte_t *ptep;
+	int rc;
+
+	ptep = get_locked_valid_pte(mm, hva, &ptelock);
+	if (!ptep)
+		return -ENXIO;
+
+	folio = page_folio(pte_page(*ptep));
+	folio_get(folio);

Grabbing a folio reference is only required if you want to keep using the folio after the pte_unmap_unlock. While the PTL is locked it cannot vanish.

So consider grabbing a reference only before dropping the PTL and you inted to call kvm_s390_wiggle_split_folio(). Then, you would effectively not require these two atomics on the expected hot path.

(I recall that the old code did that)

arch/s390/kvm/gmap.c

Quoted 30 lines
arch/s390/kernel/uv.c +25 -0
+	/*
+	 * Secure pages cannot be huge and userspace should not combine both.
+	 * In case userspace does it anyway this will result in an -EFAULT for
+	 * the unpack. The guest is thus never reaching secure mode.
+	 * If userspace plays dirty tricks and decides to map huge pages at a
+	 * later point in time, it will receive a segmentation fault or
+	 * KVM_RUN will return -EFAULT.
+	 */
+	if (folio_test_hugetlb(folio))
+		rc =  -EFAULT;
+	else if (folio_test_large(folio))
+		rc = -E2BIG;
+	else if (!pte_write(*ptep))
+		rc = -ENXIO;
+	else
+		rc = make_folio_secure(mm, folio, uvcb);
+	pte_unmap_unlock(ptep, ptelock);
+
+	if (rc == -E2BIG || rc == -EBUSY)
+		rc = kvm_s390_wiggle_split_folio(mm, folio, rc == -E2BIG);
+	folio_put(folio);
+
+	return rc;
+}
+EXPORT_SYMBOL_GPL(make_hva_secure);
 
  /*
   * To be called with the folio locked or with an extra reference! This will
arch/s390/kvm/gmap.c +0 -0
index 02adf151d4de..c08950b4301c 100644

There is one remaining reference to __gmap_make_secure, which you remove:

$ git grep __gmap_make_secure
arch/s390/kvm/gmap.c: * Return: 0 on success, < 0 in case of error (see __gmap_make_secure()).
Signature · 3 lines
Cheers,

David / dhildenb
author maintainer
#3 lore raw

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

Quoted 9 lines · On Fri, 28 Feb 2025 22:15:04 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 7 lines · Claudio Imbrenda

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")
Quoted 7 lines

Tested with shmem / memory-backend-memfd that ends up using large folios / THPs.

Two comments below.

I will need to send a v4, unfortunately there are other issues with this patch (as you have probably noticed by now as well)

Quoted 22 lines

[...]

Quoted 4 lines
arch/s390/kernel/uv.c +4 -0
+
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
+{
+	struct folio *folio;

arch/s390/kernel/uv.c int make_folio_secure(struct folio *folio, struct uv_cb_header *uvcb) · line 350

Quoted 10 lines
arch/s390/kernel/uv.c +10 -0
+	spinlock_t *ptelock;
+	pte_t *ptep;
+	int rc;
+
+	ptep = get_locked_valid_pte(mm, hva, &ptelock);
+	if (!ptep)
+		return -ENXIO;
+
+	folio = page_folio(pte_page(*ptep));
+	folio_get(folio);  

Grabbing a folio reference is only required if you want to keep using the folio after the pte_unmap_unlock. While the PTL is locked it cannot vanish.

Quoted 5 lines

So consider grabbing a reference only before dropping the PTL and you inted to call kvm_s390_wiggle_split_folio(). Then, you would effectively not require these two atomics on the expected hot path.

(I recall that the old code did that)

This code will go away hopefully in the next merge window anyway (unless I get sick *again*)

Quoted 35 lines

arch/s390/kvm/gmap.c

Quoted 31 lines
arch/s390/kernel/uv.c +25 -0
+	/*
+	 * Secure pages cannot be huge and userspace should not combine both.
+	 * In case userspace does it anyway this will result in an -EFAULT for
+	 * the unpack. The guest is thus never reaching secure mode.
+	 * If userspace plays dirty tricks and decides to map huge pages at a
+	 * later point in time, it will receive a segmentation fault or
+	 * KVM_RUN will return -EFAULT.
+	 */
+	if (folio_test_hugetlb(folio))
+		rc =  -EFAULT;
+	else if (folio_test_large(folio))
+		rc = -E2BIG;
+	else if (!pte_write(*ptep))
+		rc = -ENXIO;
+	else
+		rc = make_folio_secure(mm, folio, uvcb);
+	pte_unmap_unlock(ptep, ptelock);
+
+	if (rc == -E2BIG || rc == -EBUSY)
+		rc = kvm_s390_wiggle_split_folio(mm, folio, rc == -E2BIG);
+	folio_put(folio);
+
+	return rc;
+}
+EXPORT_SYMBOL_GPL(make_hva_secure);
 
  /*
   * To be called with the folio locked or with an extra reference! This will
arch/s390/kvm/gmap.c +0 -0
index 02adf151d4de..c08950b4301c 100644  

There is one remaining reference to __gmap_make_secure, which you remove:

Quoted 3 lines
$ git grep __gmap_make_secure
arch/s390/kvm/gmap.c: * Return: 0 on success, < 0 in case of error (see 
__gmap_make_secure()).

will fix

Quoted 3 lines
#4 lore raw

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

Quoted 15 lines · Claudio Imbrenda
Quoted 12 lines · On Fri, 28 Feb 2025 22:15:04 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 7 lines · Claudio Imbrenda

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

Tested with shmem / memory-backend-memfd that ends up using large folios / THPs.

Quoted 7 lines
Quoted 4 lines

Two comments below.

I will need to send a v4, unfortunately there are other issues with this patch (as you have probably noticed by now as well)

I ran into some weird KVM_PV_VERIFY issues a couple of times, but did not find the root cause so far.

Is that what you are seeing? (and what is the root cause? :) )

Signature · 3 lines
Cheers,

David / dhildenb