Skip to contentSkip to conversation

KVM: s390: vsie: Consolidate CRYCB validation

PATCH Applied Pierre Morel 4 participants 25 messages Aug 23, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover KVM: s390: vsie: Consolidate CRYCB validation

Before adapting the CRYCB shadowing for a guest supporting the AP instructions we want to clean the CRYCB shadowing code.

First patch seem obvious.

Second patch introduce a change in the behavior of the virtual machine in that the CRYCB is validated whenever we use or not the key masks to mimic the real machine.

Patch 3 does not correct the compiled code but make more clear what is done concerning the formating of the CRYCB in the guest original CRYCB and in the shadow CRYCB.

Pierre Morel (3):
KVM: s390: vsie: copy wrapping keys to right place
KVM: s390: vsie: Do the CRYCB validation first
KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

 arch/s390/kvm/vsie.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
Signature · 13 lines
2.7.4

Changelog from V2:
- Suppress two useless/broken patches
- Change a patch on CRYCB format to a modification of the comment
- modified the first patch title as expected
- added R-Bs

Changelog from v1
- split into severall small patches
- insert the first cleaning patch ahead
- add a new patch to allow a host not having AP
  instructions
#1 lore raw

PATCH v3 1/3 KVM: s390: vsie: copy wrapping keys to right place

Copy the key mask to the right offset inside the shadow CRYCB

---
 arch/s390/kvm/vsie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/vsie.c +2 -1
index 9175518..12b9707 100644
@@ -173,7 +173,8 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0039U);
 
 	/* copy only the wrapping keys */
-	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
+	if (read_guest_real(vcpu, crycb_addr + 72,
+			    vsie_page->crycb.dea_wrapping_key_mask, 56))
 		return set_validity_icpt(scb_s, 0x0035U);
 
 	scb_s->ecb3 |= ecb3_flags;
Signature · 1 line
2.7.4
#9 lore raw

PATCH v3 2/3 KVM: s390: vsie: Do the CRYCB validation first

In reply to Pierre Morel

When entering the SIE the CRYCB validation better
be done independently of the instruction's
availability.

---
 arch/s390/kvm/vsie.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/s390/kvm/vsie.c +6 -5
index 12b9707..38ea5da 100644
@@ -161,17 +161,18 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	/* format-1 is supported with message-security-assist extension 3 */
 	if (!test_kvm_facility(vcpu->kvm, 76))
 		return 0;
-	/* we may only allow it if enabled for guest 2 */
-	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
-		     (ECB3_AES | ECB3_DEA);
-	if (!ecb3_flags)
-		return 0;
 
 	if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
 		return set_validity_icpt(scb_s, 0x003CU);
 	else if (!crycb_addr)
 		return set_validity_icpt(scb_s, 0x0039U);
 
+	/* we may only allow it if enabled for guest 2 */
+	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
+		     (ECB3_AES | ECB3_DEA);
+	if (!ecb3_flags)
+		return 0;
+
 	/* copy only the wrapping keys */
 	if (read_guest_real(vcpu, crycb_addr + 72,
 			    vsie_page->crycb.dea_wrapping_key_mask, 56))
Signature · 1 line
2.7.4
#15 lore raw

PATCH v3 3/3 KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

In reply to Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
 arch/s390/kvm/vsie.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +3 -3
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  * Create a shadow copy of the crycb block and setup key wrapping, if
  * requested for guest 3 and enabled for guest 2.
  *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
  * There is nothing to do for format-0.
  *
  * Returns: - 0 if shadowed or nothing to do
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0035U);
 
 	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;
 
 	/* xor both blocks in one run */
 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
Signature · 1 line
2.7.4
maintainer
#16 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;
Quoted 24 lines

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
 arch/s390/kvm/vsie.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +2 -1
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  * Create a shadow copy of the crycb block and setup key wrapping, if
  * requested for guest 3 and enabled for guest 2.
  *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
  * There is nothing to do for format-0.
  *
  * Returns: - 0 if shadowed or nothing to do

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 183

Quoted 6 lines
arch/s390/kvm/vsie.c +1 -2
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0035U);
 
 	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;

That's purely cosmetic but valid.

Quoted 4 lines
arch/s390/kvm/vsie.c +0 -0
 	/* xor both blocks in one run */
 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
 
#2 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

In reply to Pierre Morel
Quoted 6 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

This should be cc stable I guess?

Quoted 19 lines
---
 arch/s390/kvm/vsie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/vsie.c +2 -1
index 9175518..12b9707 100644
@@ -173,7 +173,8 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0039U);
 
 	/* copy only the wrapping keys */
-	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
+	if (read_guest_real(vcpu, crycb_addr + 72,
+			    vsie_page->crycb.dea_wrapping_key_mask, 56))
 		return set_validity_icpt(scb_s, 0x0035U);
 
 	scb_s->ecb3 |= ecb3_flags;
#10 lore raw

Re: [PATCH v3 2/3] KVM: s390: vsie: Do the CRYCB validation first

In reply to Pierre Morel
Quoted 3 lines · Pierre Morel

When entering the SIE the CRYCB validation better
be done independently of the instruction's
availability.

Maybe something like

We need to handle the validity checks for the crycb, no matter what the settings for the keywrappings are. So lets move the keywrapping checks after we have done the validy checks.

?

Quoted 35 lines
---
 arch/s390/kvm/vsie.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/s390/kvm/vsie.c +6 -5
index 12b9707..38ea5da 100644
@@ -161,17 +161,18 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	/* format-1 is supported with message-security-assist extension 3 */
 	if (!test_kvm_facility(vcpu->kvm, 76))
 		return 0;
-	/* we may only allow it if enabled for guest 2 */
-	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
-		     (ECB3_AES | ECB3_DEA);
-	if (!ecb3_flags)
-		return 0;
 
 	if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
 		return set_validity_icpt(scb_s, 0x003CU);
 	else if (!crycb_addr)
 		return set_validity_icpt(scb_s, 0x0039U);
 
+	/* we may only allow it if enabled for guest 2 */
+	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
+		     (ECB3_AES | ECB3_DEA);
+	if (!ecb3_flags)
+		return 0;
+
 	/* copy only the wrapping keys */
 	if (read_guest_real(vcpu, crycb_addr + 72,
 			    vsie_page->crycb.dea_wrapping_key_mask, 56))
Reviewed-by
#11 lore raw

Re: [PATCH v3 2/3] KVM: s390: vsie: Do the CRYCB validation first

Quoted 7 lines · Christian Borntraeger
Quoted 3 lines · Pierre Morel

When entering the SIE the CRYCB validation better
be done independently of the instruction's
availability.

Quoted 7 lines

Maybe something like

We need to handle the validity checks for the crycb, no matter what the settings for the keywrappings are. So lets move the keywrapping checks after we have done the validy checks.

?

With that

Signature · 3 lines
Thanks,

David / dhildenb
#3 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

In reply to Christian Borntraeger
Quoted 12 lines · Christian Borntraeger
Quoted 6 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

This should be cc stable I guess?

Yes, g3 guests will be run with same wrapping keys.

Signature · 3 lines
Thanks,

David / dhildenb
Reviewed-by
#17 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

In reply to Janosch Frank
Quoted 9 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;

FORMAT2 includes bit FORMAT1 (backwards compatible)

Quoted 27 lines
Quoted 27 lines

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
 arch/s390/kvm/vsie.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +2 -1
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  * Create a shadow copy of the crycb block and setup key wrapping, if
  * requested for guest 3 and enabled for guest 2.
  *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
  * There is nothing to do for format-0.
  *
  * Returns: - 0 if shadowed or nothing to do
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0035U);
 
Quoted 10 lines

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 183

Quoted 4 lines
arch/s390/kvm/vsie.c +1 -2
 	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;

That's purely cosmetic but valid.

Quoted 3 lines
arch/s390/kvm/vsie.c +0 -0
 	/* xor both blocks in one run */
 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
Signature · 3 lines
Thanks,

David / dhildenb
maintainer Reviewed-by
#18 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

Quoted 6 lines
Quoted 4 lines

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;

FORMAT2 includes bit FORMAT1 (backwards compatible)

Right, this check is very misleading because of the constant, we effectively test against Format 0 and Format 2.

Can we make this clearer by explicitly ANDing 0x01 or adding a comment?

Code makes sense:

Quoted 41 lines
Quoted 28 lines
Quoted 28 lines

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
 arch/s390/kvm/vsie.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +2 -1
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  * Create a shadow copy of the crycb block and setup key wrapping, if
  * requested for guest 3 and enabled for guest 2.
  *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
  * There is nothing to do for format-0.
  *
  * Returns: - 0 if shadowed or nothing to do
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0035U);
 
Quoted 10 lines

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 183

Quoted 4 lines
arch/s390/kvm/vsie.c +1 -2
 	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;

That's purely cosmetic but valid.

Quoted 3 lines
arch/s390/kvm/vsie.c +0 -0
 	/* xor both blocks in one run */
 	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
#13 lore raw

Re: [PATCH v3 2/3] KVM: s390: vsie: Do the CRYCB validation first

In reply to Christian Borntraeger
Quoted 7 lines · Christian Borntraeger
Quoted 3 lines · Pierre Morel

When entering the SIE the CRYCB validation better
be done independently of the instruction's
availability.

Quoted 7 lines

Maybe something like

We need to handle the validity checks for the crycb, no matter what the settings for the keywrappings are. So lets move the keywrapping checks after we have done the validy checks.

?

OK thanks, I use this.

regards,
Pierre

Quoted 36 lines
Quoted 36 lines
---
  arch/s390/kvm/vsie.c | 11 ++++++-----
  1 file changed, 6 insertions(+), 5 deletions(-)
arch/s390/kvm/vsie.c +6 -5
index 12b9707..38ea5da 100644
@@ -161,17 +161,18 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  	/* format-1 is supported with message-security-assist extension 3 */
  	if (!test_kvm_facility(vcpu->kvm, 76))
  		return 0;
-	/* we may only allow it if enabled for guest 2 */
-	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
-		     (ECB3_AES | ECB3_DEA);
-	if (!ecb3_flags)
-		return 0;
 
  	if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
  		return set_validity_icpt(scb_s, 0x003CU);
  	else if (!crycb_addr)
  		return set_validity_icpt(scb_s, 0x0039U);
 
+	/* we may only allow it if enabled for guest 2 */
+	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
+		     (ECB3_AES | ECB3_DEA);
+	if (!ecb3_flags)
+		return 0;
+
  	/* copy only the wrapping keys */
  	if (read_guest_real(vcpu, crycb_addr + 72,
  			    vsie_page->crycb.dea_wrapping_key_mask, 56))
Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#23 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

In reply to David Hildenbrand
Quoted 48 lines · David Hildenbrand
Quoted 9 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;

FORMAT2 includes bit FORMAT1 (backwards compatible)

Quoted 35 lines
Quoted 25 lines

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
  arch/s390/kvm/vsie.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +2 -1
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
   * Create a shadow copy of the crycb block and setup key wrapping, if
   * requested for guest 3 and enabled for guest 2.
   *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
   * There is nothing to do for format-0.
   *
   * Returns: - 0 if shadowed or nothing to do

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 183

Quoted 6 lines
arch/s390/kvm/vsie.c +1 -2
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  		return set_validity_icpt(scb_s, 0x0035U);
 
  	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;

That's purely cosmetic but valid.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

/* xor both blocks in one run */
b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;

Thanks,

regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#4 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

In reply to David Hildenbrand
Quoted 11 lines · David Hildenbrand
Quoted 10 lines · Christian Borntraeger
Quoted 6 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

Quoted 5 lines
Quoted 2 lines

This should be cc stable I guess?

Yes, g3 guests will be run with same wrapping keys.

OK I do send it separated from the other patches.

regard,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#12 lore raw

Re: [PATCH v3 2/3] KVM: s390: vsie: Do the CRYCB validation first

In reply to David Hildenbrand
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Christian Borntraeger
Quoted 3 lines · Pierre Morel

When entering the SIE the CRYCB validation better
be done independently of the instruction's
availability.

Maybe something like

We need to handle the validity checks for the crycb, no matter what the settings for the keywrappings are. So lets move the keywrapping checks after we have done the validy checks.

Quoted 7 lines
Quoted 1 line

?

With that

Thanks, I use the description proposed by Christian.

regards,
Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#19 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

In reply to Janosch Frank
Quoted 12 lines · Janosch Frank
Quoted 11 lines · David Hildenbrand
Quoted 9 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;
Quoted 6 lines
Quoted 1 line

FORMAT2 includes bit FORMAT1 (backwards compatible)

Right, this check is very misleading because of the constant, we effectively test against Format 0 and Format 2.

Can we make this clearer by explicitly ANDing 0x01 or adding a comment?

yes, done, I modified the comment in front of the function.

Quoted 3 lines

Code makes sense:

Thanks,

regards,
Pierre

Quoted 44 lines
Quoted 42 lines
Quoted 29 lines
Quoted 29 lines

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
  arch/s390/kvm/vsie.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +2 -1
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
   * Create a shadow copy of the crycb block and setup key wrapping, if
   * requested for guest 3 and enabled for guest 2.
   *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
   * There is nothing to do for format-0.
   *
   * Returns: - 0 if shadowed or nothing to do
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  		return set_validity_icpt(scb_s, 0x0035U);
 
Quoted 10 lines

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 183

Quoted 4 lines
arch/s390/kvm/vsie.c +1 -2
  	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;

That's purely cosmetic but valid.

Quoted 3 lines
arch/s390/kvm/vsie.c +0 -0
  	/* xor both blocks in one run */
  	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
maintainer
#20 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

Quoted 13 lines · Pierre Morel
Quoted 12 lines · Janosch Frank
Quoted 11 lines · David Hildenbrand
Quoted 9 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;
Quoted 8 lines
Quoted 6 lines
Quoted 1 line

FORMAT2 includes bit FORMAT1 (backwards compatible)

Right, this check is very misleading because of the constant, we effectively test against Format 0 and Format 2.

Can we make this clearer by explicitly ANDing 0x01 or adding a comment?

yes, done, I modified the comment in front of the function.

Which is not what I want, what I want is:

/* CRYCB_FORMAT2 includes the bit for CRYCB_FORMAT1, so we allow both formats here */
if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

	return 0;
Quoted 56 lines
Quoted 4 lines

Code makes sense:

Thanks,

regards,
Pierre

Quoted 44 lines
Quoted 42 lines
Quoted 29 lines
Quoted 29 lines

When using FORMAT2 in the host we do not need to or with FORMAT1.

---
  arch/s390/kvm/vsie.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/vsie.c +2 -1
index 38ea5da..e0e6fbf 100644
@@ -140,7 +140,8 @@ static int prepare_cpuflags(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
   * Create a shadow copy of the crycb block and setup key wrapping, if
   * requested for guest 3 and enabled for guest 2.
   *
- * We only accept format-1 (no AP in g2), but convert it into format-2
+ * We accept format-1 or format-2, but we treat it as a format-1 (no AP in g2),
+ * and we convert it into format-2 in the shadow CRYCB.
   * There is nothing to do for format-0.
   *
   * Returns: - 0 if shadowed or nothing to do
@@ -179,8 +180,7 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
  		return set_validity_icpt(scb_s, 0x0035U);
 
Quoted 10 lines

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 183

Quoted 4 lines
arch/s390/kvm/vsie.c +1 -2
  	scb_s->ecb3 |= ecb3_flags;
-	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT1 |
-			CRYCB_FORMAT2;
+	scb_s->crycbd = ((__u32)(__u64) &vsie_page->crycb) | CRYCB_FORMAT2;

That's purely cosmetic but valid.

Quoted 3 lines
arch/s390/kvm/vsie.c +0 -0
  	/* xor both blocks in one run */
  	b1 = (unsigned long *) vsie_page->crycb.dea_wrapping_key_mask;
#21 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

Quoted 21 lines · Janosch Frank
Quoted 20 lines · Pierre Morel
Quoted 18 lines · Janosch Frank
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

Quoted 6 lines
Quoted 4 lines

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;

FORMAT2 includes bit FORMAT1 (backwards compatible)

Right, this check is very misleading because of the constant, we effectively test against Format 0 and Format 2.

Can we make this clearer by explicitly ANDing 0x01 or adding a comment?

Quoted 8 lines
Quoted 1 line

yes, done, I modified the comment in front of the function.

Which is not what I want, what I want is:

/* CRYCB_FORMAT2 includes the bit for CRYCB_FORMAT1, so we allow both formats here */
if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

	return 0;

While it's not wrong, it is also not required. And it might soon be obsolete again (with APXA, as you said, there we always have to check).

But I'll leave that to you

Signature · 3 lines
Thanks,

David / dhildenb
maintainer
#22 lore raw

Re: [PATCH v3 3/3] KVM: s390: vsie: Make use of CRYCB FORMAT2 clear

Quoted 31 lines · David Hildenbrand
Quoted 21 lines · Janosch Frank
Quoted 20 lines · Pierre Morel
Quoted 18 lines · Janosch Frank
Quoted 6 lines · David Hildenbrand
Quoted 5 lines · Janosch Frank
Quoted 3 lines · Pierre Morel

The comment preceding the shadow_crycb function is misleading, we effectively accept FORMAT2 CRYCB in the guest.

Quoted 6 lines
Quoted 4 lines

I beg to differ:

if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

		return 0;

FORMAT2 includes bit FORMAT1 (backwards compatible)

Right, this check is very misleading because of the constant, we effectively test against Format 0 and Format 2.

Can we make this clearer by explicitly ANDing 0x01 or adding a comment?

Quoted 8 lines
Quoted 1 line

yes, done, I modified the comment in front of the function.

Which is not what I want, what I want is:

/* CRYCB_FORMAT2 includes the bit for CRYCB_FORMAT1, so we allow both formats here */
if (!(crycbd_o & vcpu->arch.sie_block->crycbd & CRYCB_FORMAT1))

	return 0;
Quoted 5 lines

While it's not wrong, it is also not required. And it might soon be obsolete again (with APXA, as you said, there we always have to check).

But I'll leave that to you

I have not checked the vfio-ap patches, Pierre just told me that it goes away in a few weeks anyway, so let's leave it out.

#5 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

In reply to Pierre Morel
Quoted 12 lines · Pierre Morel
Quoted 11 lines · David Hildenbrand
Quoted 10 lines · Christian Borntraeger
Quoted 6 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This should be cc stable I guess?

Yes, g3 guests will be run with same wrapping keys.

OK I do send it separated from the other patches.

No need to do that. I can add that when picking up.

#6 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

Quoted 18 lines · Christian Borntraeger
Quoted 15 lines · Pierre Morel
Quoted 14 lines · David Hildenbrand
Quoted 12 lines · Christian Borntraeger
Quoted 6 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

This should be cc stable I guess?

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Yes, g3 guests will be run with same wrapping keys.

OK I do send it separated from the other patches.

No need to do that. I can add that when picking up.

OK
Thank

Pierre

Signature · 2 lines
Pierre Morel
Linux/KVM/QEMU in Böblingen - Germany
#7 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

In reply to Pierre Morel
Quoted 15 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

---
 arch/s390/kvm/vsie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/vsie.c +0 -0
index 9175518..12b9707 100644

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 177

Quoted 6 lines
arch/s390/kvm/vsie.c +2 -1
@@ -173,7 +173,8 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0039U);
 
 	/* copy only the wrapping keys */
-	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
+	if (read_guest_real(vcpu, crycb_addr + 72,
+			    vsie_page->crycb.dea_wrapping_key_mask, 56))

When we fix that code.., dont we have to XOR the guest3 wrapping keys with the guest2 ones?

Quoted 4 lines
arch/s390/kvm/vsie.c +0 -0
 		return set_validity_icpt(scb_s, 0x0035U);
 
 	scb_s->ecb3 |= ecb3_flags;
#8 lore raw

Re: [PATCH v3 1/3] KVM: s390: vsie: copy wrapping keys to right place

Quoted 20 lines · Christian Borntraeger
Quoted 17 lines · Pierre Morel

Copy the key mask to the right offset inside the shadow CRYCB

---
 arch/s390/kvm/vsie.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/vsie.c +0 -0
index 9175518..12b9707 100644
@@ -173,7 +173,8 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 		return set_validity_icpt(scb_s, 0x0039U);
 
Quoted 7 lines

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 177

Quoted 4 lines
arch/s390/kvm/vsie.c +2 -1
 	/* copy only the wrapping keys */
-	if (read_guest_real(vcpu, crycb_addr + 72, &vsie_page->crycb, 56))
+	if (read_guest_real(vcpu, crycb_addr + 72,
+			    vsie_page->crycb.dea_wrapping_key_mask, 56))

When we fix that code.., dont we have to XOR the guest3 wrapping keys with the guest2 ones?

That's done further down in that function.

Quoted 7 lines
Quoted 6 lines
arch/s390/kvm/vsie.c +0 -0
 		return set_validity_icpt(scb_s, 0x0035U);
 
 	scb_s->ecb3 |= ecb3_flags;
Signature · 3 lines
Thanks,

David / dhildenb
maintainer Applied
#24 lore raw
In reply to Pierre Morel
Quoted 10 lines · Pierre Morel

Before adapting the CRYCB shadowing for a guest supporting the AP instructions we want to clean the CRYCB shadowing code.

First patch seem obvious.

Second patch introduce a change in the behavior of the virtual machine in that the CRYCB is validated whenever we use or not the key masks to mimic the real machine.

Quoted 5 lines

Patch 3 does not correct the compiled code but make more clear what is done concerning the formating of the CRYCB in the guest original CRYCB and in the shadow CRYCB.

Thanks, applied.

maintainer Reviewed-by on v3 2/3
#14 lore raw

Re: [PATCH v3 2/3] KVM: s390: vsie: Do the CRYCB validation first

In reply to Pierre Morel
Quoted 28 lines · Pierre Morel

When entering the SIE the CRYCB validation better
be done independently of the instruction's
availability.

---
 arch/s390/kvm/vsie.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
arch/s390/kvm/vsie.c +0 -5
index 12b9707..38ea5da 100644
@@ -161,17 +161,18 @@ static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)
 	/* format-1 is supported with message-security-assist extension 3 */
 	if (!test_kvm_facility(vcpu->kvm, 76))
 		return 0;
-	/* we may only allow it if enabled for guest 2 */
-	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
-		     (ECB3_AES | ECB3_DEA);
-	if (!ecb3_flags)
-		return 0;
 
 	if ((crycb_addr & PAGE_MASK) != ((crycb_addr + 128) & PAGE_MASK))
 		return set_validity_icpt(scb_s, 0x003CU);
 	else if (!crycb_addr)
 		return set_validity_icpt(scb_s, 0x0039U);

arch/s390/kvm/vsie.c static int shadow_crycb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 178

Quoted 10 lines
arch/s390/kvm/vsie.c +6 -0
 
+	/* we may only allow it if enabled for guest 2 */
+	ecb3_flags = scb_o->ecb3 & vcpu->arch.sie_block->ecb3 &
+		     (ECB3_AES | ECB3_DEA);
+	if (!ecb3_flags)
+		return 0;
+
 	/* copy only the wrapping keys */
 	if (read_guest_real(vcpu, crycb_addr + 72,
 			    vsie_page->crycb.dea_wrapping_key_mask, 56))

I seemed to have forgotten to add this while being preoccupied with the search for the validity discussion in #3.