From: "Jürgen Groß" <jgross@suse.com>
To: Vitaly Kuznetsov <vkuznets@redhat.com>,
linux-kernel@vger.kernel.org, x86@kernel.org,
kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
"H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH 05/10] KVM/x86: Add KVM_MSR_RET_* defines for values 0 and 1
Date: Fri, 5 Dec 2025 11:39:38 +0100 [thread overview]
Message-ID: <34048508-8ddd-4183-9d0d-f495af1984ab@suse.com> (raw)
In-Reply-To: <877bv1kz1a.fsf@redhat.com>
[-- Attachment #1.1.1: Type: text/plain, Size: 4315 bytes --]
On 05.12.25 11:23, Vitaly Kuznetsov wrote:
> Juergen Gross <jgross@suse.com> writes:
>
>> For MSR emulation return values only 2 special cases have defines,
>> while the most used values 0 and 1 don't.
>>
>> Reason seems to be the maze of function calls of MSR emulation
>> intertwined with the KVM guest exit handlers, which are using the
>> values 0 and 1 for other purposes. This even led to the comment above
>> the already existing defines, warning to use the values 0 and 1 (and
>> negative errno values) in the MSR emulation at all.
>>
>> Fact is that MSR emulation and exit handlers are in fact rather well
>> distinct, with only very few exceptions which are handled in a sane
>> way.
>>
>> So add defines for 0 and 1 values of MSR emulation and at the same
>> time comments where exit handlers are calling into MSR emulation.
>>
>> The new defines will be used later.
>>
>> No change of functionality intended.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>> arch/x86/kvm/x86.c | 2 ++
>> arch/x86/kvm/x86.h | 10 ++++++++--
>> 2 files changed, 10 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>> index e733cb923312..e87963a47aa5 100644
>> --- a/arch/x86/kvm/x86.c
>> +++ b/arch/x86/kvm/x86.c
>> @@ -2130,6 +2130,7 @@ static int __kvm_emulate_rdmsr(struct kvm_vcpu *vcpu, u32 msr, int reg,
>> u64 data;
>> int r;
>>
>> + /* Call MSR emulation. */
>> r = kvm_emulate_msr_read(vcpu, msr, &data);
>>
>> if (!r) {
>> @@ -2171,6 +2172,7 @@ static int __kvm_emulate_wrmsr(struct kvm_vcpu *vcpu, u32 msr, u64 data)
>> {
>> int r;
>>
>> + /* Call MSR emulation. */
>> r = kvm_emulate_msr_write(vcpu, msr, data);
>> if (!r) {
>> trace_kvm_msr_write(msr, data);
>> diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
>> index f3dc77f006f9..e44b6373b106 100644
>> --- a/arch/x86/kvm/x86.h
>> +++ b/arch/x86/kvm/x86.h
>> @@ -639,15 +639,21 @@ enum kvm_msr_access {
>> /*
>> * Internal error codes that are used to indicate that MSR emulation encountered
>> * an error that should result in #GP in the guest, unless userspace handles it.
>> - * Note, '1', '0', and negative numbers are off limits, as they are used by KVM
>> - * as part of KVM's lightly documented internal KVM_RUN return codes.
>> + * Note, negative errno values are possible for return values, too.
>> + * In case MSR emulation is called from an exit handler, any return value other
>> + * than KVM_MSR_RET_OK will normally result in a GP in the guest.
>> *
>> + * OK - Emulation succeeded. Must be 0, as in some cases return values
>> + * of functions returning 0 or -errno will just be passed on.
>> + * ERR - Some error occurred.
>> * UNSUPPORTED - The MSR isn't supported, either because it is completely
>> * unknown to KVM, or because the MSR should not exist according
>> * to the vCPU model.
>> *
>> * FILTERED - Access to the MSR is denied by a userspace MSR filter.
>> */
>> +#define KVM_MSR_RET_OK 0
>> +#define KVM_MSR_RET_ERR 1
>> #define KVM_MSR_RET_UNSUPPORTED 2
>> #define KVM_MSR_RET_FILTERED 3
>
> I like the general idea of the series as 1/0 can indeed be
> confusing. What I'm wondering is if we can do better by changing 'int'
> return type to something else. I.e. if the result of the function can be
> 'passed on' and KVM_MSR_RET_OK/KVM_MSR_RET_ERR have one meaning while
> KVM_MSR_RET_UNSUPPORTED/KVM_MSR_RET_FILTERED have another, maybe we can
> do better by changing the return type to something and then, when the
> value needs to be passed on, do proper explicit vetting of the result
> (e.g. to make sure only 1/0 pass through)? Just a thought, I think the
> series as-is makes things better and we can go with it for now.
The pass through case is always 0 or -errno, never the "1" (and of course
KVM_MSR_RET_*/-errno).
Changing from int to something else would probably require some helpers
for e.g. stuffing something like -EINVAL into it. An enum alone wouldn't
work for this, so it would need to be a specific new type, like a union
of an int (for the -errno) and an enum, but I believe this would make the
code harder to read instead of improving it.
Juergen
[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3743 bytes --]
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]
next prev parent reply other threads:[~2025-12-05 10:39 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-05 7:45 [PATCH 00/10] KVM: Avoid literal numbers as return values Juergen Gross
2025-12-05 7:45 ` [PATCH 01/10] KVM: Switch coalesced_mmio_in_range() to return bool Juergen Gross
2025-12-05 7:45 ` [PATCH 02/10] KVM/x86: Use bool for the err parameter of kvm_complete_insn_gp() Juergen Gross
2025-12-05 7:45 ` [PATCH 03/10] KVM/x86: Let x86_emulate_ops.set_cr() return a bool Juergen Gross
2025-12-05 7:45 ` [PATCH 04/10] KVM/x86: Let x86_emulate_ops.set_dr() " Juergen Gross
2025-12-05 7:45 ` [PATCH 05/10] KVM/x86: Add KVM_MSR_RET_* defines for values 0 and 1 Juergen Gross
2025-12-05 10:23 ` Vitaly Kuznetsov
2025-12-05 10:39 ` Jürgen Groß [this message]
2025-12-05 15:02 ` Sean Christopherson
2025-12-05 15:59 ` Jürgen Groß
2025-12-05 7:45 ` [PATCH 06/10] KVM/x86: Use defines for APIC related MSR emulation Juergen Gross
2025-12-05 7:45 ` [PATCH 07/10] KVM/x86: Use defines for Hyper-V " Juergen Gross
2025-12-05 7:45 ` [PATCH 08/10] KVM/x86: Use defines for VMX " Juergen Gross
2025-12-05 7:45 ` [PATCH 09/10] KVM/x86: Use defines for SVM " Juergen Gross
2025-12-05 7:45 ` [PATCH 10/10] KVM/x86: Use defines for common " Juergen Gross
2025-12-05 14:16 ` [PATCH 00/10] KVM: Avoid literal numbers as return values Sean Christopherson
2025-12-05 15:47 ` Jürgen Groß
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=34048508-8ddd-4183-9d0d-f495af1984ab@suse.com \
--to=jgross@suse.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=tglx@linutronix.de \
--cc=vkuznets@redhat.com \
--cc=x86@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
all inboxes | Powered by JetHome®