kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Aneesh Kumar K.V <aneesh.kumar@kernel.org>
To: Suzuki K Poulose <suzuki.poulose@arm.com>, kvm@vger.kernel.org
Cc: Steven Price <steven.price@arm.com>,
	Will Deacon <will@kernel.org>,
	Julien Thierry <julien.thierry.kdev@gmail.com>
Subject: Re: [PATCH kvmtool v3 2/3] cpu: vmexit: Retry KVM_RUN ioctl on EINTR and EAGAIN
Date: Tue, 29 Apr 2025 09:01:08 +0530	[thread overview]
Message-ID: <yq5aecxbve2r.fsf@kernel.org> (raw)
In-Reply-To: <9e2fe85c-f3ad-4e13-b635-78a80c115499@arm.com>

Suzuki K Poulose <suzuki.poulose@arm.com> writes:

> Hi Aneesh
>
> On 28/04/2025 12:57, Aneesh Kumar K.V (Arm) wrote:
>> When KVM_RUN fails with EINTR or EAGAIN, we should retry the ioctl
>> without checking kvm_run->exit_reason. These errors don't indicate a
>> valid VM exit, hence exit_reason may contain stale or undefined values.
>> 
>> Signed-off-by: Aneesh Kumar K.V (Arm) <aneesh.kumar@kernel.org>
>> ---
>>   include/kvm/kvm-cpu.h |  2 +-
>>   kvm-cpu.c             | 17 ++++++++++++-----
>>   2 files changed, 13 insertions(+), 6 deletions(-)
>> 
>> diff --git a/include/kvm/kvm-cpu.h b/include/kvm/kvm-cpu.h
>> index 8f76f8a1123a..72cbb86e6cef 100644
>> --- a/include/kvm/kvm-cpu.h
>> +++ b/include/kvm/kvm-cpu.h
>> @@ -16,7 +16,7 @@ void kvm_cpu__delete(struct kvm_cpu *vcpu);
>>   void kvm_cpu__reset_vcpu(struct kvm_cpu *vcpu);
>>   void kvm_cpu__setup_cpuid(struct kvm_cpu *vcpu);
>>   void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu);
>> -void kvm_cpu__run(struct kvm_cpu *vcpu);
>> +int kvm_cpu__run(struct kvm_cpu *vcpu);
>>   int kvm_cpu__start(struct kvm_cpu *cpu);
>>   bool kvm_cpu__handle_exit(struct kvm_cpu *vcpu);
>>   int kvm_cpu__get_endianness(struct kvm_cpu *vcpu);
>> diff --git a/kvm-cpu.c b/kvm-cpu.c
>> index 40041a22b3fe..7abbdcebf075 100644
>> --- a/kvm-cpu.c
>> +++ b/kvm-cpu.c
>> @@ -35,27 +35,32 @@ void kvm_cpu__enable_singlestep(struct kvm_cpu *vcpu)
>>   		pr_warning("KVM_SET_GUEST_DEBUG failed");
>>   }
>>   
>> -void kvm_cpu__run(struct kvm_cpu *vcpu)
>> +/*
>> + * return value -1 if we need to call the kvm_cpu__run again without checking
>> + * exit_reason. return value 0 results in taking action based on exit_reason.
>> + */
>
> minor nit: Should we make the return value meaningful, say -EAGAIN 
> instead of -1 ?
>

I was not sure. Having both EINTR and EAGAIN map to just EAGAIN is
confusing IMHO. Instead having a proper return value (-1) which is
documented to imply a retry is better?

>> +int kvm_cpu__run(struct kvm_cpu *vcpu)
>>   {
>>   	int err;
>>   
>>   	if (!vcpu->is_running)
>> -		return;
>> +		return -1;
>>   
>>   	err = ioctl(vcpu->vcpu_fd, KVM_RUN, 0);
>>   	if (err < 0) {
>>   		switch (errno) {
>>   		case EINTR:
>>   		case EAGAIN:
>> -			return;
>> +			return -1;
>>   		case EFAULT:
>>   			if (vcpu->kvm_run->exit_reason == KVM_EXIT_MEMORY_FAULT)
>> -				return;
>> +				return 0;
>>   			/* fallthrough */
>>   		default:
>>   			die_perror("KVM_RUN failed");
>>   		}
>>   	}
>> +	return 0;
>>   }
>>   
>>   static void kvm_cpu_signal_handler(int signum)
>> @@ -179,7 +184,9 @@ int kvm_cpu__start(struct kvm_cpu *cpu)
>>   		if (cpu->task)
>>   			kvm_cpu__run_task(cpu);
>>   
>> -		kvm_cpu__run(cpu);
>> +		if (kvm_cpu__run(cpu) == -1)
>
> and this could be :
> 		if (kvm_cpu__run(cpu) == -EAGAIN)
>
>> +			/* retry without an exit_reason check */
>> +			continue;
>>   
>>   		switch (cpu->kvm_run->exit_reason) {
>>   		case KVM_EXIT_UNKNOWN:
>
>
> Suzuki

  reply	other threads:[~2025-04-29  3:37 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-28 11:57 [PATCH kvmtool v3 0/3] cpu: vmexit: KVM_RUN ioctl error handling fixes Aneesh Kumar K.V (Arm)
2025-04-28 11:57 ` [PATCH kvmtool v3 1/3] cpu: vmexit: Handle KVM_EXIT_MEMORY_FAULT in KVM_RUN ioctl return Aneesh Kumar K.V (Arm)
2025-04-29 11:07   ` Suzuki K Poulose
2025-04-28 11:57 ` [PATCH kvmtool v3 2/3] cpu: vmexit: Retry KVM_RUN ioctl on EINTR and EAGAIN Aneesh Kumar K.V (Arm)
2025-04-28 13:37   ` Suzuki K Poulose
2025-04-29  3:31     ` Aneesh Kumar K.V [this message]
2025-04-28 15:22   ` Alexandru Elisei
2025-04-29  3:37     ` Aneesh Kumar K.V
2025-04-29  8:38       ` Alexandru Elisei
2025-04-28 11:57 ` [PATCH kvmtool v3 3/3] cpu: vmexit: Handle KVM_EXIT_UNKNOWN exit reason correctly Aneesh Kumar K.V (Arm)
2025-06-10  8:01 ` [PATCH kvmtool v3 0/3] cpu: vmexit: KVM_RUN ioctl error handling fixes Aneesh Kumar K.V

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=yq5aecxbve2r.fsf@kernel.org \
    --to=aneesh.kumar@kernel.org \
    --cc=julien.thierry.kdev@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=steven.price@arm.com \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).