• M
    kvm: add guest_state_{enter,exit}_irqoff() · ef9989af
    Mark Rutland 提交于
    When transitioning to/from guest mode, it is necessary to inform
    lockdep, tracing, and RCU in a specific order, similar to the
    requirements for transitions to/from user mode. Additionally, it is
    necessary to perform vtime accounting for a window around running the
    guest, with RCU enabled, such that timer interrupts taken from the guest
    can be accounted as guest time.
    
    Most architectures don't handle all the necessary pieces, and a have a
    number of common bugs, including unsafe usage of RCU during the window
    between guest_enter() and guest_exit().
    
    On x86, this was dealt with across commits:
    
      87fa7f3e ("x86/kvm: Move context tracking where it belongs")
      0642391e ("x86/kvm/vmx: Add hardirq tracing to guest enter/exit")
      9fc975e9 ("x86/kvm/svm: Add hardirq tracing on guest enter/exit")
      3ebccdf3 ("x86/kvm/vmx: Move guest enter/exit into .noinstr.text")
      135961e0 ("x86/kvm/svm: Move guest enter/exit into .noinstr.text")
      16045714 ("KVM: x86: Defer vtime accounting 'til after IRQ handling")
      bc908e09 ("KVM: x86: Consolidate guest enter/exit logic to common helpers")
    
    ... but those fixes are specific to x86, and as the resulting logic
    (while correct) is split across generic helper functions and
    x86-specific helper functions, it is difficult to see that the
    entry/exit accounting is balanced.
    
    This patch adds generic helpers which architectures can use to handle
    guest entry/exit consistently and correctly. The guest_{enter,exit}()
    helpers are split into guest_timing_{enter,exit}() to perform vtime
    accounting, and guest_context_{enter,exit}() to perform the necessary
    context tracking and RCU management. The existing guest_{enter,exit}()
    heleprs are left as wrappers of these.
    
    Atop this, new guest_state_enter_irqoff() and guest_state_exit_irqoff()
    helpers are added to handle the ordering of lockdep, tracing, and RCU
    manageent. These are inteneded to mirror exit_to_user_mode() and
    enter_from_user_mode().
    
    Subsequent patches will migrate architectures over to the new helpers,
    following a sequence:
    
    	guest_timing_enter_irqoff();
    
    	guest_state_enter_irqoff();
    	< run the vcpu >
    	guest_state_exit_irqoff();
    
    	< take any pending IRQs >
    
    	guest_timing_exit_irqoff();
    
    This sequences handles all of the above correctly, and more clearly
    balances the entry and exit portions, making it easier to understand.
    
    The existing helpers are marked as deprecated, and will be removed once
    all architectures have been converted.
    
    There should be no functional change as a result of this patch.
    Signed-off-by: NMark Rutland <mark.rutland@arm.com>
    Reviewed-by: NMarc Zyngier <maz@kernel.org>
    Reviewed-by: NPaolo Bonzini <pbonzini@redhat.com>
    Reviewed-by: NNicolas Saenz Julienne <nsaenzju@redhat.com>
    Message-Id: <20220201132926.3301912-2-mark.rutland@arm.com>
    Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
    ef9989af
kvm_host.h 66.8 KB