Skip to content

Commit bc908e0

Browse files
sean-jcKAGA-KOKO
authored andcommitted
KVM: x86: Consolidate guest enter/exit logic to common helpers
Move the enter/exit logic in {svm,vmx}_vcpu_enter_exit() to common helpers. Opportunistically update the somewhat stale comment about the updates needing to occur immediately after VM-Exit. No functional change intended. Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 1ca0016 commit bc908e0

File tree

3 files changed

+49
-74
lines changed

3 files changed

+49
-74
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,25 +3710,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
37103710
struct vcpu_svm *svm = to_svm(vcpu);
37113711
unsigned long vmcb_pa = svm->current_vmcb->pa;
37123712

3713-
/*
3714-
* VMENTER enables interrupts (host state), but the kernel state is
3715-
* interrupts disabled when this is invoked. Also tell RCU about
3716-
* it. This is the same logic as for exit_to_user_mode().
3717-
*
3718-
* This ensures that e.g. latency analysis on the host observes
3719-
* guest mode as interrupt enabled.
3720-
*
3721-
* guest_enter_irqoff() informs context tracking about the
3722-
* transition to guest mode and if enabled adjusts RCU state
3723-
* accordingly.
3724-
*/
3725-
instrumentation_begin();
3726-
trace_hardirqs_on_prepare();
3727-
lockdep_hardirqs_on_prepare(CALLER_ADDR0);
3728-
instrumentation_end();
3729-
3730-
guest_enter_irqoff();
3731-
lockdep_hardirqs_on(CALLER_ADDR0);
3713+
kvm_guest_enter_irqoff();
37323714

37333715
if (sev_es_guest(vcpu->kvm)) {
37343716
__svm_sev_es_vcpu_run(vmcb_pa);
@@ -3748,24 +3730,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
37483730
vmload(__sme_page_pa(sd->save_area));
37493731
}
37503732

3751-
/*
3752-
* VMEXIT disables interrupts (host state), but tracing and lockdep
3753-
* have them in state 'on' as recorded before entering guest mode.
3754-
* Same as enter_from_user_mode().
3755-
*
3756-
* context_tracking_guest_exit() restores host context and reinstates
3757-
* RCU if enabled and required.
3758-
*
3759-
* This needs to be done before the below as native_read_msr()
3760-
* contains a tracepoint and x86_spec_ctrl_restore_host() calls
3761-
* into world and some more.
3762-
*/
3763-
lockdep_hardirqs_off(CALLER_ADDR0);
3764-
context_tracking_guest_exit();
3765-
3766-
instrumentation_begin();
3767-
trace_hardirqs_off_finish();
3768-
instrumentation_end();
3733+
kvm_guest_exit_irqoff();
37693734
}
37703735

37713736
static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)

arch/x86/kvm/vmx/vmx.c

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6664,25 +6664,7 @@ static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
66646664
static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
66656665
struct vcpu_vmx *vmx)
66666666
{
6667-
/*
6668-
* VMENTER enables interrupts (host state), but the kernel state is
6669-
* interrupts disabled when this is invoked. Also tell RCU about
6670-
* it. This is the same logic as for exit_to_user_mode().
6671-
*
6672-
* This ensures that e.g. latency analysis on the host observes
6673-
* guest mode as interrupt enabled.
6674-
*
6675-
* guest_enter_irqoff() informs context tracking about the
6676-
* transition to guest mode and if enabled adjusts RCU state
6677-
* accordingly.
6678-
*/
6679-
instrumentation_begin();
6680-
trace_hardirqs_on_prepare();
6681-
lockdep_hardirqs_on_prepare(CALLER_ADDR0);
6682-
instrumentation_end();
6683-
6684-
guest_enter_irqoff();
6685-
lockdep_hardirqs_on(CALLER_ADDR0);
6667+
kvm_guest_enter_irqoff();
66866668

66876669
/* L1D Flush includes CPU buffer clear to mitigate MDS */
66886670
if (static_branch_unlikely(&vmx_l1d_should_flush))
@@ -6698,24 +6680,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,
66986680

66996681
vcpu->arch.cr2 = native_read_cr2();
67006682

6701-
/*
6702-
* VMEXIT disables interrupts (host state), but tracing and lockdep
6703-
* have them in state 'on' as recorded before entering guest mode.
6704-
* Same as enter_from_user_mode().
6705-
*
6706-
* context_tracking_guest_exit() restores host context and reinstates
6707-
* RCU if enabled and required.
6708-
*
6709-
* This needs to be done before the below as native_read_msr()
6710-
* contains a tracepoint and x86_spec_ctrl_restore_host() calls
6711-
* into world and some more.
6712-
*/
6713-
lockdep_hardirqs_off(CALLER_ADDR0);
6714-
context_tracking_guest_exit();
6715-
6716-
instrumentation_begin();
6717-
trace_hardirqs_off_finish();
6718-
instrumentation_end();
6683+
kvm_guest_exit_irqoff();
67196684
}
67206685

67216686
static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu)

arch/x86/kvm/x86.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,51 @@
88
#include "kvm_cache_regs.h"
99
#include "kvm_emulate.h"
1010

11+
static __always_inline void kvm_guest_enter_irqoff(void)
12+
{
13+
/*
14+
* VMENTER enables interrupts (host state), but the kernel state is
15+
* interrupts disabled when this is invoked. Also tell RCU about
16+
* it. This is the same logic as for exit_to_user_mode().
17+
*
18+
* This ensures that e.g. latency analysis on the host observes
19+
* guest mode as interrupt enabled.
20+
*
21+
* guest_enter_irqoff() informs context tracking about the
22+
* transition to guest mode and if enabled adjusts RCU state
23+
* accordingly.
24+
*/
25+
instrumentation_begin();
26+
trace_hardirqs_on_prepare();
27+
lockdep_hardirqs_on_prepare(CALLER_ADDR0);
28+
instrumentation_end();
29+
30+
guest_enter_irqoff();
31+
lockdep_hardirqs_on(CALLER_ADDR0);
32+
}
33+
34+
static __always_inline void kvm_guest_exit_irqoff(void)
35+
{
36+
/*
37+
* VMEXIT disables interrupts (host state), but tracing and lockdep
38+
* have them in state 'on' as recorded before entering guest mode.
39+
* Same as enter_from_user_mode().
40+
*
41+
* context_tracking_guest_exit() restores host context and reinstates
42+
* RCU if enabled and required.
43+
*
44+
* This needs to be done immediately after VM-Exit, before any code
45+
* that might contain tracepoints or call out to the greater world,
46+
* e.g. before x86_spec_ctrl_restore_host().
47+
*/
48+
lockdep_hardirqs_off(CALLER_ADDR0);
49+
context_tracking_guest_exit();
50+
51+
instrumentation_begin();
52+
trace_hardirqs_off_finish();
53+
instrumentation_end();
54+
}
55+
1156
#define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check) \
1257
({ \
1358
bool failed = (consistency_check); \

0 commit comments

Comments
 (0)