public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
@ 2024-11-08 16:13 Juergen Gross
  2024-11-08 17:13 ` Paolo Bonzini
  0 siblings, 1 reply; 8+ messages in thread
From: Juergen Gross @ 2024-11-08 16:13 UTC (permalink / raw)
  To: linux-kernel, x86, kvm
  Cc: Juergen Gross, Sean Christopherson, Paolo Bonzini,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin

Using a literal 1 instead of RET_PF_RETRY is not nice, fix that.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 arch/x86/kvm/mmu/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 8e853a5fc867..d4a9f845b373 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -6157,7 +6157,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
 		vcpu->stat.pf_spurious++;
 
 	if (r != RET_PF_EMULATE)
-		return 1;
+		return RET_PF_RETRY;
 
 emulate:
 	return x86_emulate_instruction(vcpu, cr2_or_gpa, emulation_type, insn,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-08 16:13 [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY Juergen Gross
@ 2024-11-08 17:13 ` Paolo Bonzini
  2024-11-08 18:44   ` Sean Christopherson
  0 siblings, 1 reply; 8+ messages in thread
From: Paolo Bonzini @ 2024-11-08 17:13 UTC (permalink / raw)
  To: Juergen Gross
  Cc: linux-kernel, x86, kvm, Sean Christopherson, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin

Queued, thanks.

Paolo



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-08 17:13 ` Paolo Bonzini
@ 2024-11-08 18:44   ` Sean Christopherson
  2024-11-08 19:18     ` Jürgen Groß
  2024-11-09  8:03     ` Paolo Bonzini
  0 siblings, 2 replies; 8+ messages in thread
From: Sean Christopherson @ 2024-11-08 18:44 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Juergen Gross, linux-kernel, x86, kvm, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin

On Fri, Nov 08, 2024, Paolo Bonzini wrote:
> Queued, thanks.

Noooo!  Can you un-queue?

The return from kvm_mmu_page_fault() is NOT RET_PF_xxx, it's KVM outer 0/1/-errno.
I.e. '1' is saying "resume the guest", it has *nothing* to do with RET_PF_RETRY.
E.g. that path also handles RET_PF_FIXED, RET_PF_SPURIOUS, etc.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-08 18:44   ` Sean Christopherson
@ 2024-11-08 19:18     ` Jürgen Groß
  2024-11-08 22:12       ` Sean Christopherson
  2024-11-09  8:03     ` Paolo Bonzini
  1 sibling, 1 reply; 8+ messages in thread
From: Jürgen Groß @ 2024-11-08 19:18 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: linux-kernel, x86, kvm, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H . Peter Anvin


[-- Attachment #1.1.1: Type: text/plain, Size: 465 bytes --]

On 08.11.24 19:44, Sean Christopherson wrote:
> On Fri, Nov 08, 2024, Paolo Bonzini wrote:
>> Queued, thanks.
> 
> Noooo!  Can you un-queue?
> 
> The return from kvm_mmu_page_fault() is NOT RET_PF_xxx, it's KVM outer 0/1/-errno.
> I.e. '1' is saying "resume the guest", it has *nothing* to do with RET_PF_RETRY.
> E.g. that path also handles RET_PF_FIXED, RET_PF_SPURIOUS, etc.

And what about the existing "return RET_PF_RETRY" further up?


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 --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-08 19:18     ` Jürgen Groß
@ 2024-11-08 22:12       ` Sean Christopherson
  2024-11-09  7:06         ` Jürgen Groß
  0 siblings, 1 reply; 8+ messages in thread
From: Sean Christopherson @ 2024-11-08 22:12 UTC (permalink / raw)
  To: Jürgen Groß
  Cc: Paolo Bonzini, linux-kernel, x86, kvm, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin

On Fri, Nov 08, 2024, Jürgen Groß wrote:
> On 08.11.24 19:44, Sean Christopherson wrote:
> > On Fri, Nov 08, 2024, Paolo Bonzini wrote:
> > > Queued, thanks.
> > 
> > Noooo!  Can you un-queue?
> > 
> > The return from kvm_mmu_page_fault() is NOT RET_PF_xxx, it's KVM outer 0/1/-errno.
> > I.e. '1' is saying "resume the guest", it has *nothing* to do with RET_PF_RETRY.
> > E.g. that path also handles RET_PF_FIXED, RET_PF_SPURIOUS, etc.
> 
> And what about the existing "return RET_PF_RETRY" further up?

Oof.  Works by coincidence.  The intent in that case is to retry the fault, but
the fact that RET_PF_RETRY happens to be '1' is mostly luck.  Returning a postive
value other than '1' should work, but as called out by the comments for the enum,
using '0' for CONTINUE isn't a hard requirement.  E.g. if for some reason we used
'0' for RET_PF_RETRY, this code would break.

 * Note, all values must be greater than or equal to zero so as not to encroach
 * on -errno return values.  Somewhat arbitrarily use '0' for CONTINUE, which
 * will allow for efficient machine code when checking for CONTINUE, e.g.
 * "TEST %rax, %rax, JNZ", as all "stop!" values are non-zero.

FWIW, you are far from the first person to complain about KVM's mostly-undocumented
0/1/-errno return encoding scheme.  The problems is that it's so pervasive
throughout KVM, that in some cases it's not easy to understand if a function is
actually using that scheme, or just happens to return similar values.  I.e.
converting to enums (or #defines) would require a lot of work and churn.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-08 22:12       ` Sean Christopherson
@ 2024-11-09  7:06         ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2024-11-09  7:06 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, linux-kernel, x86, kvm, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin


[-- Attachment #1.1.1: Type: text/plain, Size: 2257 bytes --]

On 08.11.24 23:12, Sean Christopherson wrote:
> On Fri, Nov 08, 2024, Jürgen Groß wrote:
>> On 08.11.24 19:44, Sean Christopherson wrote:
>>> On Fri, Nov 08, 2024, Paolo Bonzini wrote:
>>>> Queued, thanks.
>>>
>>> Noooo!  Can you un-queue?
>>>
>>> The return from kvm_mmu_page_fault() is NOT RET_PF_xxx, it's KVM outer 0/1/-errno.
>>> I.e. '1' is saying "resume the guest", it has *nothing* to do with RET_PF_RETRY.
>>> E.g. that path also handles RET_PF_FIXED, RET_PF_SPURIOUS, etc.
>>
>> And what about the existing "return RET_PF_RETRY" further up?
> 
> Oof.  Works by coincidence.  The intent in that case is to retry the fault, but
> the fact that RET_PF_RETRY happens to be '1' is mostly luck.  Returning a postive
> value other than '1' should work, but as called out by the comments for the enum,
> using '0' for CONTINUE isn't a hard requirement.  E.g. if for some reason we used
> '0' for RET_PF_RETRY, this code would break.

I think this function is an especially awful case, as it seems to be natural
to return a RET_PF_ value from a function named kvm_mmu_page_fault().

> 
>   * Note, all values must be greater than or equal to zero so as not to encroach
>   * on -errno return values.  Somewhat arbitrarily use '0' for CONTINUE, which
>   * will allow for efficient machine code when checking for CONTINUE, e.g.
>   * "TEST %rax, %rax, JNZ", as all "stop!" values are non-zero.
> 
> FWIW, you are far from the first person to complain about KVM's mostly-undocumented
> 0/1/-errno return encoding scheme.  The problems is that it's so pervasive
> throughout KVM, that in some cases it's not easy to understand if a function is
> actually using that scheme, or just happens to return similar values.  I.e.
> converting to enums (or #defines) would require a lot of work and churn.

I think it would be helpful to at least add comments to the functions
returning the 0/1/-errno value.

And it would be even better to have #defines for the 0 and 1. New use cases
should use the #defines, and whether we convert current users is another
question (I'd go for it, as it is only a minor additional work when adding
the comments anyway).

If you are fine with that, I can start the effort.


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 --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-08 18:44   ` Sean Christopherson
  2024-11-08 19:18     ` Jürgen Groß
@ 2024-11-09  8:03     ` Paolo Bonzini
  2024-11-09  9:29       ` Jürgen Groß
  1 sibling, 1 reply; 8+ messages in thread
From: Paolo Bonzini @ 2024-11-09  8:03 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Juergen Gross, linux-kernel, x86, kvm, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, Dave Hansen, H . Peter Anvin

On 11/8/24 19:44, Sean Christopherson wrote:
> On Fri, Nov 08, 2024, Paolo Bonzini wrote:
>> Queued, thanks.
> 
> Noooo!  Can you un-queue?

Yes, I hadn't even pushed it to kvm/queue.  I applied it out of a whim 
but then realized that it wasn't really -rc7 material.

> The return from kvm_mmu_page_fault() is NOT RET_PF_xxx, it's KVM outer 0/1/-errno.
> I.e. '1' is saying "resume the guest", it has *nothing* to do with RET_PF_RETRY.
> E.g. that path also handles RET_PF_FIXED, RET_PF_SPURIOUS, etc.

Gah, I even checked the function and was messed up by the other "return 
RET_PF_RETRY".

If you add X86EMUL_* to the mix, it's even worse.  I had to read this 
three times to understand that it was *not* returning X86EMUL_CONTINUE 
by mistake.  Can I haz strongly-typed enums like in C++?...

         r = kvm_check_emulate_insn(vcpu, emulation_type, insn, insn_len);
         if (r != X86EMUL_CONTINUE) {
		...
         }

         if (!(emulation_type & EMULTYPE_NO_DECODE)) {
                 kvm_clear_exception_queue(vcpu);
                 if (kvm_vcpu_check_code_breakpoint(vcpu, 
emulation_type, &r))
                         return r;
		...
	}

So yeah this really has to be fixed the right way, after all even 
RET_PF_* started out as a conversion from 0/1.

Obligatory bikeshedding, how do KVM_RET_USER and KVM_RET_GUEST sound like?

Paolo


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY
  2024-11-09  8:03     ` Paolo Bonzini
@ 2024-11-09  9:29       ` Jürgen Groß
  0 siblings, 0 replies; 8+ messages in thread
From: Jürgen Groß @ 2024-11-09  9:29 UTC (permalink / raw)
  To: Paolo Bonzini, Sean Christopherson
  Cc: linux-kernel, x86, kvm, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H . Peter Anvin


[-- Attachment #1.1.1: Type: text/plain, Size: 1633 bytes --]

On 09.11.24 09:03, Paolo Bonzini wrote:
> On 11/8/24 19:44, Sean Christopherson wrote:
>> On Fri, Nov 08, 2024, Paolo Bonzini wrote:
>>> Queued, thanks.
>>
>> Noooo!  Can you un-queue?
> 
> Yes, I hadn't even pushed it to kvm/queue.  I applied it out of a whim but then 
> realized that it wasn't really -rc7 material.
> 
>> The return from kvm_mmu_page_fault() is NOT RET_PF_xxx, it's KVM outer 0/1/- 
>> errno.
>> I.e. '1' is saying "resume the guest", it has *nothing* to do with RET_PF_RETRY.
>> E.g. that path also handles RET_PF_FIXED, RET_PF_SPURIOUS, etc.
> 
> Gah, I even checked the function and was messed up by the other "return 
> RET_PF_RETRY".
> 
> If you add X86EMUL_* to the mix, it's even worse.  I had to read this three 
> times to understand that it was *not* returning X86EMUL_CONTINUE by mistake.  
> Can I haz strongly-typed enums like in C++?...
> 
>          r = kvm_check_emulate_insn(vcpu, emulation_type, insn, insn_len);
>          if (r != X86EMUL_CONTINUE) {
>          ...
>          }
> 
>          if (!(emulation_type & EMULTYPE_NO_DECODE)) {
>                  kvm_clear_exception_queue(vcpu);
>                  if (kvm_vcpu_check_code_breakpoint(vcpu, emulation_type, &r))
>                          return r;
>          ...
>      }
> 
> So yeah this really has to be fixed the right way, after all even RET_PF_* 
> started out as a conversion from 0/1.
> 
> Obligatory bikeshedding, how do KVM_RET_USER and KVM_RET_GUEST sound like?

+1


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 --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-11-09  9:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-08 16:13 [PATCH] KVM/x86: don't use a literal 1 instead of RET_PF_RETRY Juergen Gross
2024-11-08 17:13 ` Paolo Bonzini
2024-11-08 18:44   ` Sean Christopherson
2024-11-08 19:18     ` Jürgen Groß
2024-11-08 22:12       ` Sean Christopherson
2024-11-09  7:06         ` Jürgen Groß
2024-11-09  8:03     ` Paolo Bonzini
2024-11-09  9:29       ` Jürgen Groß

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox