Skip to contentSkip to conversation

KVM: s390: Fix AR parameter in ioctl

PATCH Superseded Eric Farman 5 participants 9 messages Feb 16, 2024 – Feb 20, 2024

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM: s390: Fix AR parameter in ioctl

Hi Christian, Janosch, Heiko,

Here is a new version for the AR/MEM_OP issue I'm attempting to address. (Thank you, Heiko, for the offline chat!)

Changes:
v3:
[HC] Drop the AR swap in MEM_OP path
[HC] Remove WARN and don't do save_access_regs on !bool
v2: https://lore.kernel.org/r/[email protected]/
[HC] Add a flag to indicate access registers have been loaded
v1: https://lore.kernel.org/r/[email protected]/
[CB] Store access registers around memop ioctl
[JF] Add a kernel selftest
RFC: https://lore.kernel.org/r/[email protected]/

Eric Farman (2):
KVM: s390: fix access register usage in ioctls
KVM: s390: selftests: memop: add a simple AR test

 arch/s390/include/asm/kvm_host.h          |  1 +
 arch/s390/kvm/gaccess.c                   |  3 ++-
 arch/s390/kvm/kvm-s390.c                  |  3 +++
 tools/testing/selftests/kvm/s390x/memop.c | 28 +++++++++++++++++++++++
 4 files changed, 34 insertions(+), 1 deletion(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v3 1/2 KVM: s390: fix access register usage in ioctls

The routine ar_translation() can be reached by both the instruction intercept path (where the access registers had been loaded with the guest register contents), and the MEM_OP ioctls (which hadn't). Since this routine saves the current registers to vcpu->run, this routine erroneously saves host registers into the guest space.

Introduce a boolean in the kvm_vcpu_arch struct to indicate whether the registers contain guest contents. If they do (the instruction intercept path), the save can be performed and the AR translation is done just as it is today. If they don't (the MEM_OP path), the AR can be read from vcpu->run without stashing the current contents.

---
 arch/s390/include/asm/kvm_host.h | 1 +
 arch/s390/kvm/gaccess.c          | 3 ++-
 arch/s390/kvm/kvm-s390.c         | 3 +++
 3 files changed, 6 insertions(+), 1 deletion(-)
arch/s390/include/asm/kvm_host.h +1 -0
index 52664105a473..c86215eb4ca7 100644
@@ -765,6 +765,7 @@ struct kvm_vcpu_arch {
 	__u64 cputm_start;
 	bool gs_enabled;
 	bool skey_enabled;
+	bool acrs_loaded;
 	struct kvm_s390_pv_vcpu pv;
 	union diag318_info diag318_info;
 };
arch/s390/kvm/gaccess.c +2 -1
index 5bfcc50c1a68..b4c805092021 100644
@@ -391,7 +391,8 @@ static int ar_translation(struct kvm_vcpu *vcpu, union asce *asce, u8 ar,
 	if (ar >= NUM_ACRS)
 		return -EINVAL;
 
-	save_access_regs(vcpu->run->s.regs.acrs);
+	if (vcpu->arch.acrs_loaded)
+		save_access_regs(vcpu->run->s.regs.acrs);
 	alet.val = vcpu->run->s.regs.acrs[ar];
 
 	if (ar == 0 || alet.val == 0) {
arch/s390/kvm/kvm-s390.c +3 -0
index ea63ac769889..61092f0e0a66 100644
@@ -3951,6 +3951,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 				    KVM_SYNC_ARCH0 |
 				    KVM_SYNC_PFAULT |
 				    KVM_SYNC_DIAG318;
+	vcpu->arch.acrs_loaded = false;
 	kvm_s390_set_prefix(vcpu, 0);
 	if (test_kvm_facility(vcpu->kvm, 64))
 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
@@ -4951,6 +4952,7 @@ static void sync_regs(struct kvm_vcpu *vcpu)
 	}
 	save_access_regs(vcpu->arch.host_acrs);
 	restore_access_regs(vcpu->run->s.regs.acrs);
+	vcpu->arch.acrs_loaded = true;
 	/* save host (userspace) fprs/vrs */
 	save_fpu_regs();
 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
@@ -5021,6 +5023,7 @@ static void store_regs(struct kvm_vcpu *vcpu)
 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
 	save_access_regs(vcpu->run->s.regs.acrs);
 	restore_access_regs(vcpu->arch.host_acrs);
+	vcpu->arch.acrs_loaded = false;
 	/* Save guest register state */
 	save_fpu_regs();
 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
Signature · 1 line
2.40.1
maintainer Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] KVM: s390: fix access register usage in ioctls

Quoted 12 lines · Eric Farman

The routine ar_translation() can be reached by both the instruction intercept path (where the access registers had been loaded with the guest register contents), and the MEM_OP ioctls (which hadn't). Since this routine saves the current registers to vcpu->run, this routine erroneously saves host registers into the guest space.

Introduce a boolean in the kvm_vcpu_arch struct to indicate whether the registers contain guest contents. If they do (the instruction intercept path), the save can be performed and the AR translation is done just as it is today. If they don't (the MEM_OP path), the AR can be read from vcpu->run without stashing the current contents.

Quoted 6 lines
---
 arch/s390/include/asm/kvm_host.h | 1 +
 arch/s390/kvm/gaccess.c          | 3 ++-
 arch/s390/kvm/kvm-s390.c         | 3 +++
 3 files changed, 6 insertions(+), 1 deletion(-)
Reviewed-by on v3 1/2
#3 lore raw

Re: [PATCH v3 1/2] KVM: s390: fix access register usage in ioctls

In reply to Eric Farman
Quoted 6 lines · Eric Farman

The routine ar_translation() can be reached by both the instruction intercept path (where the access registers had been loaded with the guest register contents), and the MEM_OP ioctls (which hadn't). Since this routine saves the current registers to vcpu->run, this routine erroneously saves host registers into the guest space.

Quoted 7 lines

Introduce a boolean in the kvm_vcpu_arch struct to indicate whether the registers contain guest contents. If they do (the instruction intercept path), the save can be performed and the AR translation is done just as it is today. If they don't (the MEM_OP path), the AR can be read from vcpu->run without stashing the current contents.

Quoted 11 lines
---
 arch/s390/include/asm/kvm_host.h | 1 +
 arch/s390/kvm/gaccess.c          | 3 ++-
 arch/s390/kvm/kvm-s390.c         | 3 +++
 3 files changed, 6 insertions(+), 1 deletion(-)
arch/s390/include/asm/kvm_host.h +0 -0
index 52664105a473..c86215eb4ca7 100644

arch/s390/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 768

Quoted 4 lines
arch/s390/include/asm/kvm_host.h +1 -0
@@ -765,6 +765,7 @@ struct kvm_vcpu_arch {
 	__u64 cputm_start;
 	bool gs_enabled;
 	bool skey_enabled;
+	bool acrs_loaded;

Not sure how descriptive that name is.
Maybe add a comment.

Quoted 3 lines
arch/s390/include/asm/kvm_host.h +0 -0
 	struct kvm_s390_pv_vcpu pv;
 	union diag318_info diag318_info;
 };

[...]

maintainer Acked-by Reviewed-by on v3 1/2
#4 lore raw

Re: [PATCH v3 1/2] KVM: s390: fix access register usage in ioctls

In reply to Eric Farman

Am 16.02.24 um 22:36 schrieb Eric Farman:

Quoted 6 lines

The routine ar_translation() can be reached by both the instruction intercept path (where the access registers had been loaded with the guest register contents), and the MEM_OP ioctls (which hadn't). Since this routine saves the current registers to vcpu->run, this routine erroneously saves host registers into the guest space.

Quoted 7 lines

Introduce a boolean in the kvm_vcpu_arch struct to indicate whether the registers contain guest contents. If they do (the instruction intercept path), the save can be performed and the AR translation is done just as it is today. If they don't (the MEM_OP path), the AR can be read from vcpu->run without stashing the current contents.

Quoted 60 lines
---
  arch/s390/include/asm/kvm_host.h | 1 +
  arch/s390/kvm/gaccess.c          | 3 ++-
  arch/s390/kvm/kvm-s390.c         | 3 +++
  3 files changed, 6 insertions(+), 1 deletion(-)
arch/s390/include/asm/kvm_host.h +1 -0
index 52664105a473..c86215eb4ca7 100644
@@ -765,6 +765,7 @@ struct kvm_vcpu_arch {
  	__u64 cputm_start;
  	bool gs_enabled;
  	bool skey_enabled;
+	bool acrs_loaded;
  	struct kvm_s390_pv_vcpu pv;
  	union diag318_info diag318_info;
  };
arch/s390/kvm/gaccess.c +2 -1
index 5bfcc50c1a68..b4c805092021 100644
@@ -391,7 +391,8 @@ static int ar_translation(struct kvm_vcpu *vcpu, union asce *asce, u8 ar,
  	if (ar >= NUM_ACRS)
  		return -EINVAL;
 
-	save_access_regs(vcpu->run->s.regs.acrs);
+	if (vcpu->arch.acrs_loaded)
+		save_access_regs(vcpu->run->s.regs.acrs);
  	alet.val = vcpu->run->s.regs.acrs[ar];
 
  	if (ar == 0 || alet.val == 0) {
arch/s390/kvm/kvm-s390.c +3 -0
index ea63ac769889..61092f0e0a66 100644
@@ -3951,6 +3951,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
  				    KVM_SYNC_ARCH0 |
  				    KVM_SYNC_PFAULT |
  				    KVM_SYNC_DIAG318;
+	vcpu->arch.acrs_loaded = false;
  	kvm_s390_set_prefix(vcpu, 0);
  	if (test_kvm_facility(vcpu->kvm, 64))
  		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
@@ -4951,6 +4952,7 @@ static void sync_regs(struct kvm_vcpu *vcpu)
  	}
  	save_access_regs(vcpu->arch.host_acrs);
  	restore_access_regs(vcpu->run->s.regs.acrs);
+	vcpu->arch.acrs_loaded = true;
  	/* save host (userspace) fprs/vrs */
  	save_fpu_regs();
  	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
@@ -5021,6 +5023,7 @@ static void store_regs(struct kvm_vcpu *vcpu)
  	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
  	save_access_regs(vcpu->run->s.regs.acrs);
  	restore_access_regs(vcpu->arch.host_acrs);
+	vcpu->arch.acrs_loaded = false;
  	/* Save guest register state */
  	save_fpu_regs();
  	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
maintainer
#5 lore raw

Re: [PATCH v3 1/2] KVM: s390: fix access register usage in ioctls

Quoted 17 lines · Christian Borntraeger

Am 16.02.24 um 22:36 schrieb Eric Farman:

Quoted 13 lines

The routine ar_translation() can be reached by both the instruction intercept path (where the access registers had been loaded with the guest register contents), and the MEM_OP ioctls (which hadn't). Since this routine saves the current registers to vcpu->run, this routine erroneously saves host registers into the guest space.

Introduce a boolean in the kvm_vcpu_arch struct to indicate whether the registers contain guest contents. If they do (the instruction intercept path), the save can be performed and the AR translation is done just as it is today. If they don't (the MEM_OP path), the AR can be read from vcpu->run without stashing the current contents.

Just spoke with Christian since that's a mixed signal.

To move this over the s390 tree:

Quoted 1 line

For the Patch in general:

Quoted 1 line
#6 lore raw

Re: [PATCH v3 1/2] KVM: s390: fix access register usage in ioctls

Quoted 24 lines · Janosch Frank
Quoted 22 lines · Christian Borntraeger

Am 16.02.24 um 22:36 schrieb Eric Farman:

Quoted 18 lines

The routine ar_translation() can be reached by both the instruction
intercept path (where the access registers had been loaded with the
guest register contents), and the MEM_OP ioctls (which hadn't). Since this routine saves the current registers to vcpu->run, this routine erroneously saves host registers into the guest space.

Introduce a boolean in the kvm_vcpu_arch struct to indicate whether
the registers contain guest contents. If they do (the instruction intercept path), the save can be performed and the AR translation is done just as it is today. If they don't (the MEM_OP path), the AR can be read from vcpu->run without stashing the current contents.

Quoted 8 lines

Just spoke with Christian since that's a mixed signal.

To move this over the s390 tree:

Quoted 1 line

For the Patch in general:

Quoted 1 line

Shall I send a v4 with the tags, and comments Nina suggested, then?

#7 lore raw

PATCH v3 2/2 KVM: s390: selftests: memop: add a simple AR test

In reply to Eric Farman

There is a selftest that checks for an (expected) error when an invalid AR is specified, but not one that exercises the AR path.

Add a simple test that mirrors the vanilla write/read test while providing an AR. An AR that contains zero will direct the CPU to use the primary address space normally used anyway. AR[1] is selected for this test because the host AR[1] is usually non-zero, and KVM needs to correctly swap those values.

---
 tools/testing/selftests/kvm/s390x/memop.c | 28 +++++++++++++++++++++++
 1 file changed, 28 insertions(+)
tools/testing/selftests/kvm/s390x/memop.c +28 -0
index bb3ca9a5d731..be20c26ee545 100644
@@ -375,6 +375,29 @@ static void test_copy(void)
 	kvm_vm_free(t.kvm_vm);
 }
 
+static void test_copy_access_register(void)
+{
+	struct test_default t = test_default_init(guest_copy);
+
+	HOST_SYNC(t.vcpu, STAGE_INITED);
+
+	prepare_mem12();
+	t.run->psw_mask &= ~(3UL << (63 - 17));
+	t.run->psw_mask |= 1UL << (63 - 17);  /* Enable AR mode */
+
+	CHECK_N_DO(MOP, t.vcpu, LOGICAL, WRITE, mem1, t.size,
+		   GADDR_V(mem1), AR(1));
+	HOST_SYNC(t.vcpu, STAGE_COPIED);
+
+	CHECK_N_DO(MOP, t.vcpu, LOGICAL, READ, mem2, t.size,
+		   GADDR_V(mem2), AR(1));
+	ASSERT_MEM_EQ(mem1, mem2, t.size);
+
+	t.run->psw_mask &= ~(3UL << (63 - 17));   /* Disable AR mode */
+
+	kvm_vm_free(t.kvm_vm);
+}
+
 static void set_storage_key_range(void *addr, size_t len, uint8_t key)
 {
 	uintptr_t _addr, abs, i;
@@ -1101,6 +1124,11 @@ int main(int argc, char *argv[])
 			.test = test_copy_key_fetch_prot_override,
 			.requirements_met = extension_cap > 0,
 		},
+		{
+			.name = "copy with access register mode",
+			.test = test_copy_access_register,
+			.requirements_met = true,
+		},
 		{
 			.name = "error checks with key",
 			.test = test_errors_key,
Signature · 1 line
2.40.1
maintainer Acked-by on v3 2/2
#8 lore raw

Re: [PATCH v3 2/2] KVM: s390: selftests: memop: add a simple AR test

Am 16.02.24 um 22:36 schrieb Eric Farman:

Quoted 10 lines

There is a selftest that checks for an (expected) error when an invalid AR is specified, but not one that exercises the AR path.

Add a simple test that mirrors the vanilla write/read test while providing an AR. An AR that contains zero will direct the CPU to use the primary address space normally used anyway. AR[1] is selected for this test because the host AR[1] is usually non-zero, and KVM needs to correctly swap those values.