Skip to content

Commit 557a961

Browse files
vittyvkbonzini
authored andcommitted
KVM: x86: acknowledgment mechanism for async pf page ready notifications
If two page ready notifications happen back to back the second one is not delivered and the only mechanism we currently have is kvm_check_async_pf_completion() check in vcpu_run() loop. The check will only be performed with the next vmexit when it happens and in some cases it may take a while. With interrupt based page ready notification delivery the situation is even worse: unlike exceptions, interrupts are not handled immediately so we must check if the slot is empty. This is slow and unnecessary. Introduce dedicated MSR_KVM_ASYNC_PF_ACK MSR to communicate the fact that the slot is free and host should check its notification queue. Mandate using it for interrupt based 'page ready' APF event delivery. As kvm_check_async_pf_completion() is going away from vcpu_run() we need a way to communicate the fact that vcpu->async_pf.done queue has transitioned from empty to non-empty state. Introduce kvm_arch_async_page_present_queued() and KVM_REQ_APF_READY to do the job. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Message-Id: <20200525144125.143875-7-vkuznets@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 2635b5c commit 557a961

6 files changed

Lines changed: 52 additions & 5 deletions

File tree

‎Documentation/virt/kvm/msr.rst‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ data:
236236
of these bytes is a token which was previously delivered as 'page not
237237
present' event. The event indicates the page in now available. Guest is
238238
supposed to write '0' to 'token' when it is done handling 'page ready'
239-
event so the next one can be delivered.
239+
event and to write 1' to MSR_KVM_ASYNC_PF_ACK after clearing the location;
240+
writing to the MSR forces KVM to re-scan its queue and deliver the next
241+
pending notification.
240242

241243
Note, MSR_KVM_ASYNC_PF_INT MSR specifying the interrupt vector for 'page
242244
ready' APF delivery needs to be written to before enabling APF mechanism
@@ -359,3 +361,14 @@ data:
359361
Interrupt vector for asynchnonous 'page ready' notifications delivery.
360362
The vector has to be set up before asynchronous page fault mechanism
361363
is enabled in MSR_KVM_ASYNC_PF_EN.
364+
365+
MSR_KVM_ASYNC_PF_ACK:
366+
0x4b564d07
367+
368+
data:
369+
Asynchronous page fault (APF) acknowledgment.
370+
371+
When the guest is done processing 'page ready' APF event and 'token'
372+
field in 'struct kvm_vcpu_pv_apf_data' is cleared it is supposed to
373+
write '1' to bit 0 of the MSR, this causes the host to re-scan its queue
374+
and check if there are more notifications pending.

‎arch/s390/include/asm/kvm_host.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,8 @@ void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
984984
void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
985985
struct kvm_async_pf *work);
986986

987+
static inline void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) {}
988+
987989
void kvm_arch_crypto_clear_masks(struct kvm *kvm);
988990
void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
989991
unsigned long *aqm, unsigned long *adm);

‎arch/x86/include/asm/kvm_host.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#define KVM_REQ_TLB_FLUSH_CURRENT KVM_ARCH_REQ(26)
8787
#define KVM_REQ_HV_TLB_FLUSH \
8888
KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_NO_WAKEUP)
89+
#define KVM_REQ_APF_READY KVM_ARCH_REQ(28)
8990

9091
#define CR0_RESERVED_BITS \
9192
(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -775,6 +776,7 @@ struct kvm_vcpu_arch {
775776
u32 host_apf_flags;
776777
unsigned long nested_apf_token;
777778
bool delivery_as_pf_vmexit;
779+
bool pageready_pending;
778780
} apf;
779781

780782
/* OSVW MSRs (AMD only) */
@@ -1662,6 +1664,7 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
16621664
struct kvm_async_pf *work);
16631665
void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
16641666
struct kvm_async_pf *work);
1667+
void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu);
16651668
bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu);
16661669
extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
16671670

‎arch/x86/include/uapi/asm/kvm_para.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#define MSR_KVM_PV_EOI_EN 0x4b564d04
5252
#define MSR_KVM_POLL_CONTROL 0x4b564d05
5353
#define MSR_KVM_ASYNC_PF_INT 0x4b564d06
54+
#define MSR_KVM_ASYNC_PF_ACK 0x4b564d07
5455

5556
struct kvm_steal_time {
5657
__u64 steal;

‎arch/x86/kvm/x86.c‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ static const u32 emulated_msrs_all[] = {
12481248
HV_X64_MSR_TSC_EMULATION_STATUS,
12491249

12501250
MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
1251-
MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT,
1251+
MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK,
12521252

12531253
MSR_IA32_TSC_ADJUST,
12541254
MSR_IA32_TSCDEADLINE,
@@ -2946,6 +2946,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
29462946
if (kvm_pv_enable_async_pf_int(vcpu, data))
29472947
return 1;
29482948
break;
2949+
case MSR_KVM_ASYNC_PF_ACK:
2950+
if (data & 0x1) {
2951+
vcpu->arch.apf.pageready_pending = false;
2952+
kvm_check_async_pf_completion(vcpu);
2953+
}
2954+
break;
29492955
case MSR_KVM_STEAL_TIME:
29502956

29512957
if (unlikely(!sched_info_on()))
@@ -3225,6 +3231,9 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
32253231
case MSR_KVM_ASYNC_PF_INT:
32263232
msr_info->data = vcpu->arch.apf.msr_int_val;
32273233
break;
3234+
case MSR_KVM_ASYNC_PF_ACK:
3235+
msr_info->data = 0;
3236+
break;
32283237
case MSR_KVM_STEAL_TIME:
32293238
msr_info->data = vcpu->arch.st.msr_val;
32303239
break;
@@ -8413,6 +8422,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
84138422
kvm_hv_process_stimers(vcpu);
84148423
if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu))
84158424
kvm_vcpu_update_apicv(vcpu);
8425+
if (kvm_check_request(KVM_REQ_APF_READY, vcpu))
8426+
kvm_check_async_pf_completion(vcpu);
84168427
}
84178428

84188429
if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
@@ -8664,8 +8675,6 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
86648675
break;
86658676
}
86668677

8667-
kvm_check_async_pf_completion(vcpu);
8668-
86698678
if (signal_pending(current)) {
86708679
r = -EINTR;
86718680
vcpu->run->exit_reason = KVM_EXIT_INTR;
@@ -10555,13 +10564,22 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1055510564
trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa);
1055610565

1055710566
if (kvm_pv_async_pf_enabled(vcpu) &&
10558-
!apf_put_user_ready(vcpu, work->arch.token))
10567+
!apf_put_user_ready(vcpu, work->arch.token)) {
10568+
vcpu->arch.apf.pageready_pending = true;
1055910569
kvm_apic_set_irq(vcpu, &irq, NULL);
10570+
}
1056010571

1056110572
vcpu->arch.apf.halted = false;
1056210573
vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1056310574
}
1056410575

10576+
void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu)
10577+
{
10578+
kvm_make_request(KVM_REQ_APF_READY, vcpu);
10579+
if (!vcpu->arch.apf.pageready_pending)
10580+
kvm_vcpu_kick(vcpu);
10581+
}
10582+
1056510583
bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
1056610584
{
1056710585
if (!kvm_pv_async_pf_enabled(vcpu))

‎virt/kvm/async_pf.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static void async_pf_execute(struct work_struct *work)
5151
unsigned long addr = apf->addr;
5252
gpa_t cr2_or_gpa = apf->cr2_or_gpa;
5353
int locked = 1;
54+
bool first;
5455

5556
might_sleep();
5657

@@ -69,10 +70,14 @@ static void async_pf_execute(struct work_struct *work)
6970
kvm_arch_async_page_present(vcpu, apf);
7071

7172
spin_lock(&vcpu->async_pf.lock);
73+
first = list_empty(&vcpu->async_pf.done);
7274
list_add_tail(&apf->link, &vcpu->async_pf.done);
7375
apf->vcpu = NULL;
7476
spin_unlock(&vcpu->async_pf.lock);
7577

78+
if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
79+
kvm_arch_async_page_present_queued(vcpu);
80+
7681
/*
7782
* apf may be freed by kvm_check_async_pf_completion() after
7883
* this point
@@ -201,6 +206,7 @@ int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
201206
int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu)
202207
{
203208
struct kvm_async_pf *work;
209+
bool first;
204210

205211
if (!list_empty_careful(&vcpu->async_pf.done))
206212
return 0;
@@ -213,9 +219,13 @@ int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu)
213219
INIT_LIST_HEAD(&work->queue); /* for list_del to work */
214220

215221
spin_lock(&vcpu->async_pf.lock);
222+
first = list_empty(&vcpu->async_pf.done);
216223
list_add_tail(&work->link, &vcpu->async_pf.done);
217224
spin_unlock(&vcpu->async_pf.lock);
218225

226+
if (!IS_ENABLED(CONFIG_KVM_ASYNC_PF_SYNC) && first)
227+
kvm_arch_async_page_present_queued(vcpu);
228+
219229
vcpu->async_pf.queued++;
220230
return 0;
221231
}

0 commit comments

Comments
 (0)