KVM: s390: Refactor absolute vm mem_op function
authorJanis Schoetterl-Glausch <scgl@linux.ibm.com>
Mon, 6 Feb 2023 16:45:58 +0000 (17:45 +0100)
committerJanosch Frank <frankja@linux.ibm.com>
Tue, 7 Feb 2023 17:05:59 +0000 (18:05 +0100)
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>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Janosch Frank <frankja@linux.ibm.com>
Link: https://lore.kernel.org/r/20230206164602.138068-11-scgl@linux.ibm.com
Message-Id: <20230206164602.138068-11-scgl@linux.ibm.com>
Signed-off-by: Janosch Frank <frankja@linux.ibm.com>
arch/s390/kvm/kvm-s390.c

index 0053b1fbfc7680a03032cd978715df366963dbd6..23f50437a328df36d1ddd02bf961d7bc9d83bebc 100644 (file)
@@ -2782,6 +2782,7 @@ static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_fla
 static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
 {
        void __user *uaddr = (void __user *)mop->buf;
+       enum gacc_mode acc_mode;
        void *tmpbuf = NULL;
        int r, srcu_idx;
 
@@ -2803,33 +2804,25 @@ static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
                goto out_unlock;
        }
 
-       switch (mop->op) {
-       case KVM_S390_MEMOP_ABSOLUTE_READ: {
-               if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
-                       r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_FETCH, mop->key);
-               } else {
-                       r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
-                                                     mop->size, GACC_FETCH, mop->key);
-                       if (r == 0) {
-                               if (copy_to_user(uaddr, tmpbuf, mop->size))
-                                       r = -EFAULT;
-                       }
-               }
-               break;
+       acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
+       if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
+               r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
+               goto out_unlock;
        }
-       case KVM_S390_MEMOP_ABSOLUTE_WRITE: {
-               if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
-                       r = check_gpa_range(kvm, mop->gaddr, mop->size, GACC_STORE, mop->key);
-               } else {
-                       if (copy_from_user(tmpbuf, uaddr, mop->size)) {
-                               r = -EFAULT;
-                               break;
-                       }
-                       r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
-                                                     mop->size, GACC_STORE, mop->key);
+       if (acc_mode == GACC_FETCH) {
+               r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
+                                             mop->size, GACC_FETCH, mop->key);
+               if (r)
+                       goto out_unlock;
+               if (copy_to_user(uaddr, tmpbuf, mop->size))
+                       r = -EFAULT;
+       } else {
+               if (copy_from_user(tmpbuf, uaddr, mop->size)) {
+                       r = -EFAULT;
+                       goto out_unlock;
                }
-               break;
-       }
+               r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
+                                             mop->size, GACC_STORE, mop->key);
        }
 
 out_unlock: