2023-01-25 21:27:00

by Janis Schoetterl-Glausch

[permalink] [raw]
Subject: [PATCH v6 10/14] KVM: s390: Refactor absolute vm mem_op function

Remove code duplication with regards to the CHECK_ONLY flag.
Decrease the number of indents.
No functional change indented.

Suggested-by: Janosch Frank <[email protected]>
Signed-off-by: Janis Schoetterl-Glausch <[email protected]>
---


Cosmetic only, can be dropped.


arch/s390/kvm/kvm-s390.c | 43 ++++++++++++++++------------------------
1 file changed, 17 insertions(+), 26 deletions(-)

diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
index 588cf70dc81e..cfd09cb43ef6 100644
--- a/arch/s390/kvm/kvm-s390.c
+++ b/arch/s390/kvm/kvm-s390.c
@@ -2794,6 +2794,7 @@ static void *mem_op_alloc_buf(struct kvm_s390_mem_op *mop)
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;

@@ -2813,33 +2814,23 @@ 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;
- }
- 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);
+ 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);
+ } else 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:
--
2.34.1



2023-01-26 12:19:57

by Thomas Huth

[permalink] [raw]
Subject: Re: [PATCH v6 10/14] KVM: s390: Refactor absolute vm mem_op function

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 <[email protected]>
> Signed-off-by: Janis Schoetterl-Glausch <[email protected]>
> ---
>
>
> 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 <[email protected]>


2023-01-26 13:03:07

by Janosch Frank

[permalink] [raw]
Subject: Re: [PATCH v6 10/14] KVM: s390: Refactor absolute vm mem_op function

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 <[email protected]>
>> Signed-off-by: Janis Schoetterl-Glausch <[email protected]>
>> ---
>>
>>
>> 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 <[email protected]>
>

As long as we're not adding to this function in the future then I'm
okish with leaving it as is.

2023-02-03 14:49:19

by Janosch Frank

[permalink] [raw]
Subject: Re: [PATCH v6 10/14] KVM: s390: Refactor absolute vm mem_op function

On 1/25/23 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 <[email protected]>
> Signed-off-by: Janis Schoetterl-Glausch <[email protected]>
> ---
>
>
> Cosmetic only, can be dropped.
>
>
> arch/s390/kvm/kvm-s390.c | 43 ++++++++++++++++------------------------
> 1 file changed, 17 insertions(+), 26 deletions(-)
>
> diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
> index 588cf70dc81e..cfd09cb43ef6 100644
> --- a/arch/s390/kvm/kvm-s390.c
> +++ b/arch/s390/kvm/kvm-s390.c
> @@ -2794,6 +2794,7 @@ static void *mem_op_alloc_buf(struct kvm_s390_mem_op *mop)
> 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;
>
> @@ -2813,33 +2814,23 @@ 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;
> - }
> - 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);
> + acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;

Would the line be too long if that variable would be initialized where
it's defined?

> + if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
> + r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);

We should early return i.e. goto out_unlock.

IMHO else if, else patterns should either be switches (testing the same
variable) or kept as short as possible / be avoided.

> + } else if (acc_mode == GACC_FETCH) {
> + r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
> + mop->size, GACC_FETCH, mop->key);

I'd guess it's personal taste whether you use GACC_FETCH or access_mode
but if you don't use it here then we can remove the variable all
together, no?

> + 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:


2023-02-03 15:38:02

by Janis Schoetterl-Glausch

[permalink] [raw]
Subject: Re: [PATCH v6 10/14] KVM: s390: Refactor absolute vm mem_op function

On Fri, 2023-02-03 at 15:48 +0100, Janosch Frank wrote:
> On 1/25/23 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 <[email protected]>
> > Signed-off-by: Janis Schoetterl-Glausch <[email protected]>
> > ---
> >
> >
> > Cosmetic only, can be dropped.
> >
> >
> > arch/s390/kvm/kvm-s390.c | 43 ++++++++++++++++------------------------
> > 1 file changed, 17 insertions(+), 26 deletions(-)
> >
> > diff --git a/arch/s390/kvm/kvm-s390.c b/arch/s390/kvm/kvm-s390.c
> > index 588cf70dc81e..cfd09cb43ef6 100644
> > --- a/arch/s390/kvm/kvm-s390.c
> > +++ b/arch/s390/kvm/kvm-s390.c
> > @@ -2794,6 +2794,7 @@ static void *mem_op_alloc_buf(struct kvm_s390_mem_op *mop)
> > 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;
> >
> > @@ -2813,33 +2814,23 @@ 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;
> > - }
> > - 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);
> > + acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
>
> Would the line be too long if that variable would be initialized where
> it's defined?

Just fits at 100 columns. Want me to move it?

>
> > + if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
> > + r = check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
>
> We should early return i.e. goto out_unlock.
>
> IMHO else if, else patterns should either be switches (testing the same
> variable) or kept as short as possible / be avoided.
>
> > + } else if (acc_mode == GACC_FETCH) {
> > + r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
> > + mop->size, GACC_FETCH, mop->key);
>
> I'd guess it's personal taste whether you use GACC_FETCH or access_mode
> but if you don't use it here then we can remove the variable all
> together, no?

Yeah, I think I did replace it, but then undid it.
Probably just because it is a bit more explicit.
It's used in check_gpa_range, so no, unless you want to dump the expression
directly in there.
>
> > + 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:
>