Skip to content

Commit d1ae567

Browse files
committed
KVM: Add a flag to track if a loaded vCPU is scheduled out
Add a kvm_vcpu.scheduled_out flag to track if a vCPU is in the process of being scheduled out (vCPU put path), or if the vCPU is being reloaded after being scheduled out (vCPU load path). In the short term, this will allow dropping kvm_arch_sched_in(), as arch code can query scheduled_out during kvm_arch_vcpu_load(). Longer term, scheduled_out opens up other potential optimizations, without creating subtle/brittle dependencies. E.g. it allows KVM to keep guest state (that is managed via kvm_arch_vcpu_{load,put}()) loaded across kvm_sched_{out,in}(), if KVM knows the state isn't accessed by the host kernel. Forcing arch code to coordinate between kvm_arch_sched_{in,out}() and kvm_arch_vcpu_{load,put}() is awkward, not reusable, and relies on the exact ordering of calls into arch code. Adding scheduled_out also obviates the need for a kvm_arch_sched_out() hook, e.g. if arch code needs to do something novel when putting vCPU state. And even if KVM never uses scheduled_out for anything beyond dropping kvm_arch_sched_in(), just being able to remove all of the arch stubs makes it worth adding the flag. Link: https://lore.kernel.org/all/20240430224431.490139-1-seanjc@google.com Cc: Oliver Upton <oliver.upton@linux.dev> Reviewed-by: Oliver Upton <oliver.upton@linux.dev> Acked-by: Kai Huang <kai.huang@intel.com> Link: https://lore.kernel.org/r/20240522014013.1672962-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c4201bd commit d1ae567

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

include/linux/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ struct kvm_vcpu {
380380
#endif
381381
bool preempted;
382382
bool ready;
383+
bool scheduled_out;
383384
struct kvm_vcpu_arch arch;
384385
struct kvm_vcpu_stat stat;
385386
char stats_id[KVM_STATS_NAME_SIZE];

virt/kvm/kvm_main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6294,13 +6294,17 @@ static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
62946294
__this_cpu_write(kvm_running_vcpu, vcpu);
62956295
kvm_arch_sched_in(vcpu, cpu);
62966296
kvm_arch_vcpu_load(vcpu, cpu);
6297+
6298+
WRITE_ONCE(vcpu->scheduled_out, false);
62976299
}
62986300

62996301
static void kvm_sched_out(struct preempt_notifier *pn,
63006302
struct task_struct *next)
63016303
{
63026304
struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
63036305

6306+
WRITE_ONCE(vcpu->scheduled_out, true);
6307+
63046308
if (current->on_rq) {
63056309
WRITE_ONCE(vcpu->preempted, true);
63066310
WRITE_ONCE(vcpu->ready, true);

0 commit comments

Comments
 (0)