Skip to content

Commit 7c90705

Browse files
Gleb Natapovavikivity
authored andcommitted
KVM: Inject asynchronous page fault into a PV guest if page is swapped out.
Send async page fault to a PV guest if it accesses swapped out memory. Guest will choose another task to run upon receiving the fault. Allow async page fault injection only when guest is in user mode since otherwise guest may be in non-sleepable context and will not be able to reschedule. Vcpu will be halted if guest will fault on the same page again or if vcpu executes kernel code. Acked-by: Rik van Riel <riel@redhat.com> Signed-off-by: Gleb Natapov <gleb@redhat.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent 631bc48 commit 7c90705

5 files changed

Lines changed: 55 additions & 12 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ struct kvm_vcpu_arch {
421421
gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
422422
struct gfn_to_hva_cache data;
423423
u64 msr_val;
424+
u32 id;
424425
} apf;
425426
};
426427

@@ -596,6 +597,7 @@ struct kvm_x86_ops {
596597
};
597598

598599
struct kvm_arch_async_pf {
600+
u32 token;
599601
gfn_t gfn;
600602
};
601603

@@ -819,6 +821,7 @@ void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
819821
struct kvm_async_pf *work);
820822
void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
821823
struct kvm_async_pf *work);
824+
bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu);
822825
extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
823826

824827
#endif /* _ASM_X86_KVM_HOST_H */

‎arch/x86/kvm/mmu.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,7 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
25922592
int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn)
25932593
{
25942594
struct kvm_arch_async_pf arch;
2595+
arch.token = (vcpu->arch.apf.id++ << 12) | vcpu->vcpu_id;
25952596
arch.gfn = gfn;
25962597

25972598
return kvm_setup_async_pf(vcpu, gva, gfn, &arch);

‎arch/x86/kvm/x86.c‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6248,20 +6248,53 @@ static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
62486248
}
62496249
}
62506250

6251+
static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
6252+
{
6253+
6254+
return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
6255+
sizeof(val));
6256+
}
6257+
62516258
void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
62526259
struct kvm_async_pf *work)
62536260
{
6254-
trace_kvm_async_pf_not_present(work->gva);
6255-
6256-
kvm_make_request(KVM_REQ_APF_HALT, vcpu);
6261+
trace_kvm_async_pf_not_present(work->arch.token, work->gva);
62576262
kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
6263+
6264+
if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
6265+
kvm_x86_ops->get_cpl(vcpu) == 0)
6266+
kvm_make_request(KVM_REQ_APF_HALT, vcpu);
6267+
else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
6268+
vcpu->arch.fault.error_code = 0;
6269+
vcpu->arch.fault.address = work->arch.token;
6270+
kvm_inject_page_fault(vcpu);
6271+
}
62586272
}
62596273

62606274
void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
62616275
struct kvm_async_pf *work)
62626276
{
6263-
trace_kvm_async_pf_ready(work->gva);
6264-
kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
6277+
trace_kvm_async_pf_ready(work->arch.token, work->gva);
6278+
if (is_error_page(work->page))
6279+
work->arch.token = ~0; /* broadcast wakeup */
6280+
else
6281+
kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
6282+
6283+
if ((vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
6284+
!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
6285+
vcpu->arch.fault.error_code = 0;
6286+
vcpu->arch.fault.address = work->arch.token;
6287+
kvm_inject_page_fault(vcpu);
6288+
}
6289+
}
6290+
6291+
bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
6292+
{
6293+
if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
6294+
return true;
6295+
else
6296+
return !kvm_event_needs_reinjection(vcpu) &&
6297+
kvm_x86_ops->interrupt_allowed(vcpu);
62656298
}
62666299

62676300
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);

‎include/trace/events/kvm.h‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,34 +204,39 @@ TRACE_EVENT(
204204

205205
TRACE_EVENT(
206206
kvm_async_pf_not_present,
207-
TP_PROTO(u64 gva),
208-
TP_ARGS(gva),
207+
TP_PROTO(u64 token, u64 gva),
208+
TP_ARGS(token, gva),
209209

210210
TP_STRUCT__entry(
211+
__field(__u64, token)
211212
__field(__u64, gva)
212213
),
213214

214215
TP_fast_assign(
216+
__entry->token = token;
215217
__entry->gva = gva;
216218
),
217219

218-
TP_printk("gva %#llx not present", __entry->gva)
220+
TP_printk("token %#llx gva %#llx not present", __entry->token,
221+
__entry->gva)
219222
);
220223

221224
TRACE_EVENT(
222225
kvm_async_pf_ready,
223-
TP_PROTO(u64 gva),
224-
TP_ARGS(gva),
226+
TP_PROTO(u64 token, u64 gva),
227+
TP_ARGS(token, gva),
225228

226229
TP_STRUCT__entry(
230+
__field(__u64, token)
227231
__field(__u64, gva)
228232
),
229233

230234
TP_fast_assign(
235+
__entry->token = token;
231236
__entry->gva = gva;
232237
),
233238

234-
TP_printk("gva %#llx ready", __entry->gva)
239+
TP_printk("token %#llx gva %#llx ready", __entry->token, __entry->gva)
235240
);
236241

237242
TRACE_EVENT(

‎virt/kvm/async_pf.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
124124
{
125125
struct kvm_async_pf *work;
126126

127-
if (list_empty_careful(&vcpu->async_pf.done))
127+
if (list_empty_careful(&vcpu->async_pf.done) ||
128+
!kvm_arch_can_inject_async_page_present(vcpu))
128129
return;
129130

130131
spin_lock(&vcpu->async_pf.lock);

0 commit comments

Comments
 (0)