Message ID | 20230125212608.1860251-1-scgl@linux.ibm.com |
---|---|
Headers | show |
Series | KVM: s390: Extend MEM_OP ioctl by storage key checked cmpxchg | expand |
On 1/25/23 22:25, Janis Schoetterl-Glausch wrote: > Replace the DEFAULT_* test helpers by functions, as they don't > need the exta flexibility. s/exta/extra/ But if you want I can fix that up. The __VA_ARGS__ often don't make it easier to understand therefore I'd rather have a function so I'm happy this patch removes a bit of the magic: Acked-by: Janosch Frank <frankja@linux.ibm.com> > > Signed-off-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com> > Reviewed-by: Thomas Huth <thuth@redhat.com> > --- > tools/testing/selftests/kvm/s390x/memop.c | 82 +++++++++++------------ > 1 file changed, 39 insertions(+), 43 deletions(-) > > diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c > index 9c05d1205114..df1c726294b2 100644 > --- a/tools/testing/selftests/kvm/s390x/memop.c > +++ b/tools/testing/selftests/kvm/s390x/memop.c > @@ -48,6 +48,8 @@ struct mop_desc { > uint8_t key; > }; > > +const uint8_t NO_KEY = 0xff; > + > static struct kvm_s390_mem_op ksmo_from_desc(const struct mop_desc *desc) > { > struct kvm_s390_mem_op ksmo = { > @@ -85,7 +87,7 @@ static struct kvm_s390_mem_op ksmo_from_desc(const struct mop_desc *desc) > ksmo.flags |= KVM_S390_MEMOP_F_INJECT_EXCEPTION; > if (desc->_set_flags) > ksmo.flags = desc->set_flags; > - if (desc->f_key) { > + if (desc->f_key && desc->key != NO_KEY) { > ksmo.flags |= KVM_S390_MEMOP_F_SKEY_PROTECTION; > ksmo.key = desc->key; > } > @@ -268,34 +270,28 @@ static void prepare_mem12(void) > #define ASSERT_MEM_EQ(p1, p2, size) \ > TEST_ASSERT(!memcmp(p1, p2, size), "Memory contents do not match!") > > -#define DEFAULT_WRITE_READ(copy_cpu, mop_cpu, mop_target_p, size, ...) \ > -({ \ > - struct test_info __copy_cpu = (copy_cpu), __mop_cpu = (mop_cpu); \ > - enum mop_target __target = (mop_target_p); \ > - uint32_t __size = (size); \ > - \ > - prepare_mem12(); \ > - CHECK_N_DO(MOP, __mop_cpu, __target, WRITE, mem1, __size, \ > - GADDR_V(mem1), ##__VA_ARGS__); \ > - HOST_SYNC(__copy_cpu, STAGE_COPIED); \ > - CHECK_N_DO(MOP, __mop_cpu, __target, READ, mem2, __size, \ > - GADDR_V(mem2), ##__VA_ARGS__); \ > - ASSERT_MEM_EQ(mem1, mem2, __size); \ > -}) > +static void default_write_read(struct test_info copy_cpu, struct test_info mop_cpu, > + enum mop_target mop_target, uint32_t size, uint8_t key) > +{ > + prepare_mem12(); > + CHECK_N_DO(MOP, mop_cpu, mop_target, WRITE, mem1, size, > + GADDR_V(mem1), KEY(key)); > + HOST_SYNC(copy_cpu, STAGE_COPIED); > + CHECK_N_DO(MOP, mop_cpu, mop_target, READ, mem2, size, > + GADDR_V(mem2), KEY(key)); > + ASSERT_MEM_EQ(mem1, mem2, size); > +} > > -#define DEFAULT_READ(copy_cpu, mop_cpu, mop_target_p, size, ...) \ > -({ \ > - struct test_info __copy_cpu = (copy_cpu), __mop_cpu = (mop_cpu); \ > - enum mop_target __target = (mop_target_p); \ > - uint32_t __size = (size); \ > - \ > - prepare_mem12(); \ > - CHECK_N_DO(MOP, __mop_cpu, __target, WRITE, mem1, __size, \ > - GADDR_V(mem1)); \ > - HOST_SYNC(__copy_cpu, STAGE_COPIED); \ > - CHECK_N_DO(MOP, __mop_cpu, __target, READ, mem2, __size, ##__VA_ARGS__);\ > - ASSERT_MEM_EQ(mem1, mem2, __size); \ > -}) > +static void default_read(struct test_info copy_cpu, struct test_info mop_cpu, > + enum mop_target mop_target, uint32_t size, uint8_t key) > +{ > + prepare_mem12(); > + CHECK_N_DO(MOP, mop_cpu, mop_target, WRITE, mem1, size, GADDR_V(mem1)); > + HOST_SYNC(copy_cpu, STAGE_COPIED); > + CHECK_N_DO(MOP, mop_cpu, mop_target, READ, mem2, size, > + GADDR_V(mem2), KEY(key)); > + ASSERT_MEM_EQ(mem1, mem2, size); > +} > > static void guest_copy(void) > { > @@ -310,7 +306,7 @@ static void test_copy(void) > > HOST_SYNC(t.vcpu, STAGE_INITED); > > - DEFAULT_WRITE_READ(t.vcpu, t.vcpu, LOGICAL, t.size); > + default_write_read(t.vcpu, t.vcpu, LOGICAL, t.size, NO_KEY); > > kvm_vm_free(t.kvm_vm); > } > @@ -357,26 +353,26 @@ static void test_copy_key(void) > HOST_SYNC(t.vcpu, STAGE_SKEYS_SET); > > /* vm, no key */ > - DEFAULT_WRITE_READ(t.vcpu, t.vm, ABSOLUTE, t.size); > + default_write_read(t.vcpu, t.vm, ABSOLUTE, t.size, NO_KEY); > > /* vm/vcpu, machting key or key 0 */ > - DEFAULT_WRITE_READ(t.vcpu, t.vcpu, LOGICAL, t.size, KEY(0)); > - DEFAULT_WRITE_READ(t.vcpu, t.vcpu, LOGICAL, t.size, KEY(9)); > - DEFAULT_WRITE_READ(t.vcpu, t.vm, ABSOLUTE, t.size, KEY(0)); > - DEFAULT_WRITE_READ(t.vcpu, t.vm, ABSOLUTE, t.size, KEY(9)); > + default_write_read(t.vcpu, t.vcpu, LOGICAL, t.size, 0); > + default_write_read(t.vcpu, t.vcpu, LOGICAL, t.size, 9); > + default_write_read(t.vcpu, t.vm, ABSOLUTE, t.size, 0); > + default_write_read(t.vcpu, t.vm, ABSOLUTE, t.size, 9); > /* > * There used to be different code paths for key handling depending on > * if the region crossed a page boundary. > * There currently are not, but the more tests the merrier. > */ > - DEFAULT_WRITE_READ(t.vcpu, t.vcpu, LOGICAL, 1, KEY(0)); > - DEFAULT_WRITE_READ(t.vcpu, t.vcpu, LOGICAL, 1, KEY(9)); > - DEFAULT_WRITE_READ(t.vcpu, t.vm, ABSOLUTE, 1, KEY(0)); > - DEFAULT_WRITE_READ(t.vcpu, t.vm, ABSOLUTE, 1, KEY(9)); > + default_write_read(t.vcpu, t.vcpu, LOGICAL, 1, 0); > + default_write_read(t.vcpu, t.vcpu, LOGICAL, 1, 9); > + default_write_read(t.vcpu, t.vm, ABSOLUTE, 1, 0); > + default_write_read(t.vcpu, t.vm, ABSOLUTE, 1, 9); > > /* vm/vcpu, mismatching keys on read, but no fetch protection */ > - DEFAULT_READ(t.vcpu, t.vcpu, LOGICAL, t.size, GADDR_V(mem2), KEY(2)); > - DEFAULT_READ(t.vcpu, t.vm, ABSOLUTE, t.size, GADDR_V(mem1), KEY(2)); > + default_read(t.vcpu, t.vcpu, LOGICAL, t.size, 2); > + default_read(t.vcpu, t.vm, ABSOLUTE, t.size, 2); > > kvm_vm_free(t.kvm_vm); > } > @@ -409,7 +405,7 @@ static void test_copy_key_storage_prot_override(void) > HOST_SYNC(t.vcpu, STAGE_SKEYS_SET); > > /* vcpu, mismatching keys, storage protection override in effect */ > - DEFAULT_WRITE_READ(t.vcpu, t.vcpu, LOGICAL, t.size, KEY(2)); > + default_write_read(t.vcpu, t.vcpu, LOGICAL, t.size, 2); > > kvm_vm_free(t.kvm_vm); > } > @@ -422,8 +418,8 @@ static void test_copy_key_fetch_prot(void) > HOST_SYNC(t.vcpu, STAGE_SKEYS_SET); > > /* vm/vcpu, matching key, fetch protection in effect */ > - DEFAULT_READ(t.vcpu, t.vcpu, LOGICAL, t.size, GADDR_V(mem2), KEY(9)); > - DEFAULT_READ(t.vcpu, t.vm, ABSOLUTE, t.size, GADDR_V(mem2), KEY(9)); > + default_read(t.vcpu, t.vcpu, LOGICAL, t.size, 9); > + default_read(t.vcpu, t.vm, ABSOLUTE, t.size, 9); > > kvm_vm_free(t.kvm_vm); > }
On 1/26/23 13:18, Thomas Huth wrote: > On 25/01/2023 22.26, Janis Schoetterl-Glausch wrote: >> Remove code duplication with regards to the CHECK_ONLY flag. >> Decrease the number of indents. >> No functional change indented. >> >> Suggested-by: Janosch Frank <frankja@linux.ibm.com> >> Signed-off-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com> >> --- >> >> >> Cosmetic only, can be dropped. > > I'm torn between unnecessary-code-churn and > nice-to-get-rid-of-one-indentation-level here ... anyway, patch looks sane > to me, so: > > Reviewed-by: Thomas Huth <thuth@redhat.com> > As long as we're not adding to this function in the future then I'm okish with leaving it as is.
On 1/25/23 22:25, Janis Schoetterl-Glausch wrote: > Add test that tries to access, instead of CHECK_ONLY. "" Add a test that tries a real write to a bad address. A CHECK_ONLY test doesn't cover all paths. "" At first I thought you were replacing a test. > > Signed-off-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com> > Reviewed-by: Nico Boehr <nrb@linux.ibm.com> > --- > tools/testing/selftests/kvm/s390x/memop.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/tools/testing/selftests/kvm/s390x/memop.c b/tools/testing/selftests/kvm/s390x/memop.c > index bbc191a13760..5aae27549437 100644 > --- a/tools/testing/selftests/kvm/s390x/memop.c > +++ b/tools/testing/selftests/kvm/s390x/memop.c > @@ -641,7 +641,9 @@ static void _test_errors_common(struct test_info info, enum mop_target target, i > > /* Bad guest address: */ > rv = ERR_MOP(info, target, WRITE, mem1, size, GADDR((void *)~0xfffUL), CHECK_ONLY); > - TEST_ASSERT(rv > 0, "ioctl does not report bad guest memory access"); > + TEST_ASSERT(rv > 0, "ioctl does not report bad guest memory address"); "ioctl does not report bad guest memory address on CHECK_ONLY write" ? > + rv = ERR_MOP(info, target, WRITE, mem1, size, GADDR((void *)~0xfffUL)); > + TEST_ASSERT(rv > 0, "ioctl does not report bad guest memory address"); "ioctl does not report bad guest memory address on write" ? Not really necessary in this case, it just needs to be different from the one on top. > > /* Bad host address: */ > rv = ERR_MOP(info, target, WRITE, 0, size, GADDR_V(mem1));
On Thu, 2023-01-26 at 14:02 +0100, Janosch Frank wrote: > On 1/26/23 07:48, Thomas Huth wrote: > > On 25/01/2023 22.26, Janis Schoetterl-Glausch wrote: > > > The vcpu and vm mem_op ioctl implementations share some functionality. > > > Move argument checking and buffer allocation into functions and call > > > them from both implementations. > > > This allows code reuse in case of additional future mem_op operations. > > > > > > Suggested-by: Janosch Frank <frankja@linux.ibm.com> > > > Signed-off-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com> > > > --- > > > arch/s390/kvm/kvm-s390.c | 80 +++++++++++++++++++++------------------- > > > 1 file changed, 42 insertions(+), 38 deletions(-) > > > > > > diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c > > > index e4890e04b210..e0dfaa195949 100644 > > > --- a/arch/s390/kvm/kvm-s390.c > > > +++ b/arch/s390/kvm/kvm-s390.c > > > @@ -2764,24 +2764,44 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) > > > return r; > > > } > > > > > > -static bool access_key_invalid(u8 access_key) > > > +static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags) > > > { > > > - return access_key > 0xf; > > > + if (mop->flags & ~supported_flags || !mop->size) > > > + return -EINVAL; > > > + if (mop->size > MEM_OP_MAX_SIZE) > > > + return -E2BIG; > > > + if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { > > > + if (mop->key > 0xf) > > > + return -EINVAL; > > > + } else { > > > + mop->key = 0; > > > + } > > > + return 0; > > > +} > > > + > > > +static void *mem_op_alloc_buf(struct kvm_s390_mem_op *mop) > > > +{ > > > + void *buf; > > > + > > > + if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) > > > + return NULL; > > > + buf = vmalloc(mop->size); > > > + if (!buf) > > > + return ERR_PTR(-ENOMEM); > > > + return buf; > > > } > > > > > > static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) > > > { > > > void __user *uaddr = (void __user *)mop->buf; > > > - u64 supported_flags; > > > void *tmpbuf = NULL; > > > > You likely can now remove the "= NULL" here, I guess? > > > > > int r, srcu_idx; > > > > > > - supported_flags = KVM_S390_MEMOP_F_SKEY_PROTECTION > > > - | KVM_S390_MEMOP_F_CHECK_ONLY; > > > - if (mop->flags & ~supported_flags || !mop->size) > > > - return -EINVAL; > > > - if (mop->size > MEM_OP_MAX_SIZE) > > > - return -E2BIG; > > > + r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION | > > > + KVM_S390_MEMOP_F_CHECK_ONLY); > > > + if (r) > > > + return r; > > > + > > > /* > > > * This is technically a heuristic only, if the kvm->lock is not > > > * taken, it is not guaranteed that the vm is/remains non-protected. > > > @@ -2793,17 +2813,9 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) > > > */ > > > if (kvm_s390_pv_get_handle(kvm)) > > > return -EINVAL; > > > - if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { > > > - if (access_key_invalid(mop->key)) > > > - return -EINVAL; > > > - } else { > > > - mop->key = 0; > > > - } > > > - if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { > > > - tmpbuf = vmalloc(mop->size); > > > - if (!tmpbuf) > > > - return -ENOMEM; > > > - } > > > + tmpbuf = mem_op_alloc_buf(mop); > > > + if (IS_ERR(tmpbuf)) > > > + return PTR_ERR(tmpbuf); > > > > > > srcu_idx = srcu_read_lock(&kvm->srcu); > > > > > > @@ -5250,28 +5262,20 @@ static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, > > > { > > > void __user *uaddr = (void __user *)mop->buf; > > > void *tmpbuf = NULL; > > > > ... and here, too. > > > > But I have to admit that I'm also not sure whether I like the > > mem_op_alloc_buf() part or not (the mem_op_validate_common() part looks fine > > to me) : mem_op_alloc_buf() is a new function with 11 lines of code, and the > > old spots that allocate memory were only 5 lines of code each, so you now > > increased the LoC count and additionally have to fiddly with IS_ERR and > > PTR_ERR which is always a little bit ugly in my eyes ... IMHO I'd rather > > keep the old code here. But that's just my 0.02 €, if you think it's nicer > > with mem_op_alloc_buf(), I won't insist on keeping the old code. > > > > Thomas > > > > I've done a PoC that has a **buff argument and combines the check with > the alloc. I just didn't like that much because it felt like an unspecific memop_do_things function.
On Thu, 2023-01-26 at 07:48 +0100, Thomas Huth wrote: > On 25/01/2023 22.26, Janis Schoetterl-Glausch wrote: > > The vcpu and vm mem_op ioctl implementations share some functionality. > > Move argument checking and buffer allocation into functions and call > > them from both implementations. > > This allows code reuse in case of additional future mem_op operations. > > > > Suggested-by: Janosch Frank <frankja@linux.ibm.com> > > Signed-off-by: Janis Schoetterl-Glausch <scgl@linux.ibm.com> > > --- > > arch/s390/kvm/kvm-s390.c | 80 +++++++++++++++++++++------------------- > > 1 file changed, 42 insertions(+), 38 deletions(-) > > > > diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c > > index e4890e04b210..e0dfaa195949 100644 > > --- a/arch/s390/kvm/kvm-s390.c > > +++ b/arch/s390/kvm/kvm-s390.c > > @@ -2764,24 +2764,44 @@ static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd) > > return r; > > } > > > > -static bool access_key_invalid(u8 access_key) > > +static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags) > > { > > - return access_key > 0xf; > > + if (mop->flags & ~supported_flags || !mop->size) > > + return -EINVAL; > > + if (mop->size > MEM_OP_MAX_SIZE) > > + return -E2BIG; > > + if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { > > + if (mop->key > 0xf) > > + return -EINVAL; > > + } else { > > + mop->key = 0; > > + } > > + return 0; > > +} > > + > > +static void *mem_op_alloc_buf(struct kvm_s390_mem_op *mop) > > +{ > > + void *buf; > > + > > + if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) > > + return NULL; > > + buf = vmalloc(mop->size); > > + if (!buf) > > + return ERR_PTR(-ENOMEM); > > + return buf; > > } > > > > static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) > > { > > void __user *uaddr = (void __user *)mop->buf; > > - u64 supported_flags; > > void *tmpbuf = NULL; > > You likely can now remove the "= NULL" here, I guess? Yeah, I thought about it, but wasn't sure if I like moving the line down because of some people's insistence on reverse christmas tree. It's entirely arbitrary in a different way, but I like the return value being the last thing declared. In the end I forgot to make a decision on it. > > > int r, srcu_idx; > > > > - supported_flags = KVM_S390_MEMOP_F_SKEY_PROTECTION > > - | KVM_S390_MEMOP_F_CHECK_ONLY; > > - if (mop->flags & ~supported_flags || !mop->size) > > - return -EINVAL; > > - if (mop->size > MEM_OP_MAX_SIZE) > > - return -E2BIG; > > + r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION | > > + KVM_S390_MEMOP_F_CHECK_ONLY); > > + if (r) > > + return r; > > + > > /* > > * This is technically a heuristic only, if the kvm->lock is not > > * taken, it is not guaranteed that the vm is/remains non-protected. > > @@ -2793,17 +2813,9 @@ static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop) > > */ > > if (kvm_s390_pv_get_handle(kvm)) > > return -EINVAL; > > - if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { > > - if (access_key_invalid(mop->key)) > > - return -EINVAL; > > - } else { > > - mop->key = 0; > > - } > > - if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { > > - tmpbuf = vmalloc(mop->size); > > - if (!tmpbuf) > > - return -ENOMEM; > > - } > > + tmpbuf = mem_op_alloc_buf(mop); > > + if (IS_ERR(tmpbuf)) > > + return PTR_ERR(tmpbuf); > > > > srcu_idx = srcu_read_lock(&kvm->srcu); > > > > @@ -5250,28 +5262,20 @@ static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu, > > { > > void __user *uaddr = (void __user *)mop->buf; > > void *tmpbuf = NULL; > > ... and here, too. > > But I have to admit that I'm also not sure whether I like the > mem_op_alloc_buf() part or not (the mem_op_validate_common() part looks fine > to me) : mem_op_alloc_buf() is a new function with 11 lines of code, and the > old spots that allocate memory were only 5 lines of code each, so you now > increased the LoC count and additionally have to fiddly with IS_ERR and > PTR_ERR which is always a little bit ugly in my eyes ... IMHO I'd rather > keep the old code here. But that's just my 0.02 €, if you think it's nicer > with mem_op_alloc_buf(), I won't insist on keeping the old code. Yeah, that's fair. > > Thomas > > > > - int r = 0; > > - const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION > > - | KVM_S390_MEMOP_F_CHECK_ONLY > > - | KVM_S390_MEMOP_F_SKEY_PROTECTION; > > + int r; > > > > - if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size) > > + r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION | > > + KVM_S390_MEMOP_F_CHECK_ONLY | > > + KVM_S390_MEMOP_F_SKEY_PROTECTION); > > + if (r) > > + return r; > > + if (mop->ar >= NUM_ACRS) > > return -EINVAL; > > - if (mop->size > MEM_OP_MAX_SIZE) > > - return -E2BIG; > > if (kvm_s390_pv_cpu_is_protected(vcpu)) > > return -EINVAL; > > - if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) { > > - if (access_key_invalid(mop->key)) > > - return -EINVAL; > > - } else { > > - mop->key = 0; > > - } > > - if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) { > > - tmpbuf = vmalloc(mop->size); > > - if (!tmpbuf) > > - return -ENOMEM; > > - } > > + tmpbuf = mem_op_alloc_buf(mop); > > + if (IS_ERR(tmpbuf)) > > + return PTR_ERR(tmpbuf); > > > > switch (mop->op) { > > case KVM_S390_MEMOP_LOGICAL_READ: >