public inbox for linux-s390@vger.kernel.org
 help / color / mirror / Atom feed
From: Janosch Frank <frankja@linux.ibm.com>
To: Thomas Huth <thuth@redhat.com>,
	Janis Schoetterl-Glausch <scgl@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Claudio Imbrenda <imbrenda@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>
Cc: David Hildenbrand <david@redhat.com>,
	Jonathan Corbet <corbet@lwn.net>,
	kvm@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-s390@vger.kernel.org, Paolo Bonzini <pbonzini@redhat.com>,
	Shuah Khan <shuah@kernel.org>,
	Sven Schnelle <svens@linux.ibm.com>
Subject: Re: [PATCH v6 08/14] KVM: s390: Move common code of mem_op functions into functions
Date: Thu, 26 Jan 2023 14:02:15 +0100	[thread overview]
Message-ID: <dbd8204a-5413-b593-7ede-1c5ea7ee4425@linux.ibm.com> (raw)
In-Reply-To: <a1141cf5-8c44-5e9e-688c-c9dab3ebe8d4@redhat.com>

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.

@Nina: Any reason why this was split up?

  reply	other threads:[~2023-01-26 13:02 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-25 21:25 [PATCH v6 00/14] KVM: s390: Extend MEM_OP ioctl by storage key checked cmpxchg Janis Schoetterl-Glausch
2023-01-25 21:25 ` [PATCH v6 01/14] KVM: s390: selftest: memop: Pass mop_desc via pointer Janis Schoetterl-Glausch
2023-01-26 11:51   ` Janosch Frank
2023-01-25 21:25 ` [PATCH v6 02/14] KVM: s390: selftest: memop: Replace macros by functions Janis Schoetterl-Glausch
2023-01-26 12:00   ` Janosch Frank
2023-01-25 21:25 ` [PATCH v6 03/14] KVM: s390: selftest: memop: Move testlist into main Janis Schoetterl-Glausch
2023-01-26 12:03   ` Janosch Frank
2023-01-25 21:25 ` [PATCH v6 04/14] KVM: s390: selftest: memop: Add bad address test Janis Schoetterl-Glausch
2023-01-26 15:23   ` Janosch Frank
2023-01-25 21:25 ` [PATCH v6 05/14] KVM: s390: selftest: memop: Fix typo Janis Schoetterl-Glausch
2023-01-25 21:26 ` [PATCH v6 06/14] KVM: s390: selftest: memop: Fix wrong address being used in test Janis Schoetterl-Glausch
2023-01-25 21:26 ` [PATCH v6 07/14] KVM: s390: selftest: memop: Fix integer literal Janis Schoetterl-Glausch
2023-01-26  6:38   ` Thomas Huth
2023-01-25 21:26 ` [PATCH v6 08/14] KVM: s390: Move common code of mem_op functions into functions Janis Schoetterl-Glausch
2023-01-26  6:48   ` Thomas Huth
2023-01-26 13:02     ` Janosch Frank [this message]
2023-01-26 16:47       ` Janis Schoetterl-Glausch
2023-01-26 17:01     ` Janis Schoetterl-Glausch
2023-01-25 21:26 ` [PATCH v6 09/14] KVM: s390: Dispatch to implementing function at top level of vm mem_op Janis Schoetterl-Glausch
2023-01-26 12:13   ` Thomas Huth
2023-01-25 21:26 ` [PATCH v6 10/14] KVM: s390: Refactor absolute vm mem_op function Janis Schoetterl-Glausch
2023-01-26 12:18   ` Thomas Huth
2023-01-26 13:02     ` Janosch Frank
2023-02-03 14:48   ` Janosch Frank
2023-02-03 15:32     ` Janis Schoetterl-Glausch
2023-01-25 21:26 ` [PATCH v6 11/14] KVM: s390: Refactor absolute vcpu " Janis Schoetterl-Glausch
2023-01-25 21:26 ` [PATCH v6 12/14] KVM: s390: Extend MEM_OP ioctl by storage key checked cmpxchg Janis Schoetterl-Glausch
2023-01-26  8:19   ` Heiko Carstens
2023-01-26 16:10   ` Janosch Frank
2023-01-27 18:15     ` Janis Schoetterl-Glausch
2023-01-28  9:29   ` kernel test robot
2023-01-28 14:38   ` kernel test robot
2023-01-28 14:38   ` kernel test robot
2023-01-25 21:26 ` [PATCH v6 13/14] Documentation: KVM: s390: Describe KVM_S390_MEMOP_F_CMPXCHG Janis Schoetterl-Glausch
2023-01-25 21:26 ` [PATCH v6 14/14] KVM: s390: selftest: memop: Add cmpxchg tests Janis Schoetterl-Glausch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=dbd8204a-5413-b593-7ede-1c5ea7ee4425@linux.ibm.com \
    --to=frankja@linux.ibm.com \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=corbet@lwn.net \
    --cc=david@redhat.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=scgl@linux.ibm.com \
    --cc=shuah@kernel.org \
    --cc=svens@linux.ibm.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox