Skip to contentSkip to conversation

KVM: s390: fix a newly introduced bug

PATCH Superseded Claudio Imbrenda 2 participants 6 messages Feb 26, 2025

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Claudio Imbrenda

author maintainer
#0 lore raw

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

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

PATCH v2 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      | 105 ++++++++++++++++++++++++++++++++++---
 arch/s390/kvm/gmap.c       |  99 +++-------------------------------
 arch/s390/kvm/kvm-s390.c   |  25 +++++----
 4 files changed, 121 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 +99 -6

133 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
#2 lore raw

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

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

Quoted 10 lines
arch/s390/kernel/uv.c +10 -0
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
+{
+	struct folio *folio;
+	spinlock_t *ptelock;
+	pte_t *ptep;
+	int rc;
+
+	ptep = get_locked_valid_pte(mm, hva, &ptelock);
+	if (!ptep)
+		return -ENXIO;

You very likely need a pte_write() check we had there before, as you might effectively modify page content by clearing the page.

Signature · 3 lines
Cheers,

David / dhildenb
author maintainer
#3 lore raw

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

Quoted 5 lines · On Wed, 26 Feb 2025 16:05:11 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 5 lines
arch/s390/kernel/uv.c +5 -0
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
+{
+	struct folio *folio;
+	spinlock_t *ptelock;
+	pte_t *ptep;
Quoted 8 lines

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

Quoted 5 lines
arch/s390/kernel/uv.c +5 -0
+	int rc;
+
+	ptep = get_locked_valid_pte(mm, hva, &ptelock);
+	if (!ptep)
+		return -ENXIO;  

You very likely need a pte_write() check we had there before, as you might effectively modify page content by clearing the page.

it's not really needed, but it doesn't hurt either, I'll add a check

#4 lore raw

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

Quoted 17 lines · Claudio Imbrenda
Quoted 5 lines · On Wed, 26 Feb 2025 16:05:11 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 5 lines
arch/s390/kernel/uv.c +5 -0
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
+{
+	struct folio *folio;
+	spinlock_t *ptelock;
+	pte_t *ptep;
Quoted 8 lines

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

Quoted 5 lines
arch/s390/kernel/uv.c +5 -0
+	int rc;
+
+	ptep = get_locked_valid_pte(mm, hva, &ptelock);
+	if (!ptep)
+		return -ENXIO;

You very likely need a pte_write() check we had there before, as you might effectively modify page content by clearing the page.

Quoted 1 line

it's not really needed, but it doesn't hurt either, I'll add a check

Can you elaborate why it is not needed? Would the HW enforce that writability check already?

Signature · 3 lines
Cheers,

David / dhildenb
author maintainer
#5 lore raw

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

Quoted 18 lines · On Wed, 26 Feb 2025 18:01:04 +0100 David Hildenbrand <[email protected]> wrote:
Quoted 17 lines · Claudio Imbrenda
Quoted 13 lines · On Wed, 26 Feb 2025 16:05:11 +0100 David Hildenbrand <[email protected]> wrote:

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

Quoted 10 lines
arch/s390/kernel/uv.c +10 -0
+int make_hva_secure(struct mm_struct *mm, unsigned long hva, struct uv_cb_header *uvcb)
+{
+	struct folio *folio;
+	spinlock_t *ptelock;
+	pte_t *ptep;
+	int rc;
+
+	ptep = get_locked_valid_pte(mm, hva, &ptelock);
+	if (!ptep)
+		return -ENXIO;  

You very likely need a pte_write() check we had there before, as you might effectively modify page content by clearing the page.

Quoted 4 lines
Quoted 1 line

it's not really needed, but it doesn't hurt either, I'll add a check

Can you elaborate why it is not needed? Would the HW enforce that writability check already?

as I have discovered the hard way while working on this v2, yes

but as I said, it looks better with the check, so I'll add it