kvm: Note an RCU quiescent state on guest exit
authorLeonardo Bras <leobras@redhat.com>
Sat, 11 May 2024 02:05:56 +0000 (23:05 -0300)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 4 Sep 2024 14:44:42 +0000 (10:44 -0400)
As of today, KVM notes a quiescent state only in guest entry, which is good
as it avoids the guest being interrupted for current RCU operations.

While the guest vcpu runs, it can be interrupted by a timer IRQ that will
check for any RCU operations waiting for this CPU. In case there are any of
such, it invokes rcu_core() in order to sched-out the current thread and
note a quiescent state.

This occasional schedule work will introduce tens of microsseconds of
latency, which is really bad for vcpus running latency-sensitive
applications, such as real-time workloads.

So, note a quiescent state in guest exit, so the interrupted guests is able
to deal with any pending RCU operations before being required to invoke
rcu_core(), and thus avoid the overhead of related scheduler work.

Signed-off-by: Leonardo Bras <leobras@redhat.com>
Acked-by: Paul E. McKenney <paulmck@kernel.org>
Acked-by: Sean Christopherson <seanjc@google.com>
Message-ID: <20240511020557.1198200-1-leobras@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/linux/context_tracking.h
include/linux/kvm_host.h

index 6e76b9dba00e77d9e28e6044f6684acf6ff28ee1..8a78fabeafc3a92967ea766ef1fca175af39e8d3 100644 (file)
@@ -80,10 +80,12 @@ static __always_inline bool context_tracking_guest_enter(void)
        return context_tracking_enabled_this_cpu();
 }
 
-static __always_inline void context_tracking_guest_exit(void)
+static __always_inline bool context_tracking_guest_exit(void)
 {
        if (context_tracking_enabled())
                __ct_user_exit(CONTEXT_GUEST);
+
+       return context_tracking_enabled_this_cpu();
 }
 
 #define CT_WARN_ON(cond) WARN_ON(context_tracking_enabled() && (cond))
@@ -98,7 +100,7 @@ static inline void exception_exit(enum ctx_state prev_ctx) { }
 static inline int ct_state(void) { return -1; }
 static inline int __ct_state(void) { return -1; }
 static __always_inline bool context_tracking_guest_enter(void) { return false; }
-static __always_inline void context_tracking_guest_exit(void) { }
+static __always_inline bool context_tracking_guest_exit(void) { return false; }
 #define CT_WARN_ON(cond) do { } while (0)
 #endif /* !CONFIG_CONTEXT_TRACKING_USER */
 
index b23c6d48392f7c4e37252bc19de9aacfe6759b5f..0d5125a3e31a9db7f3cd74aa885da818ee7963b5 100644 (file)
@@ -485,7 +485,15 @@ static __always_inline void guest_state_enter_irqoff(void)
  */
 static __always_inline void guest_context_exit_irqoff(void)
 {
-       context_tracking_guest_exit();
+       /*
+        * Guest mode is treated as a quiescent state, see
+        * guest_context_enter_irqoff() for more details.
+        */
+       if (!context_tracking_guest_exit()) {
+               instrumentation_begin();
+               rcu_virt_note_context_switch();
+               instrumentation_end();
+       }
 }
 
 /*