Skip to content

Commit 631bc48

Browse files
Gleb Natapovavikivity
authored andcommitted
KVM: Handle async PF in a guest.
When async PF capability is detected hook up special page fault handler that will handle async page fault events and bypass other page faults to regular page fault handler. Also add async PF handling to nested SVM emulation. Async PF always generates exit to L1 where vcpu thread will be scheduled out until page is available. 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 fd10cde commit 631bc48

6 files changed

Lines changed: 243 additions & 9 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ struct kvm_mmu_op_release_pt {
6565
__u64 pt_phys;
6666
};
6767

68+
#define KVM_PV_REASON_PAGE_NOT_PRESENT 1
69+
#define KVM_PV_REASON_PAGE_READY 2
70+
6871
struct kvm_vcpu_pv_apf_data {
6972
__u32 reason;
7073
__u8 pad[60];
@@ -171,8 +174,17 @@ static inline unsigned int kvm_arch_para_features(void)
171174

172175
#ifdef CONFIG_KVM_GUEST
173176
void __init kvm_guest_init(void);
177+
void kvm_async_pf_task_wait(u32 token);
178+
void kvm_async_pf_task_wake(u32 token);
179+
u32 kvm_read_and_reset_pf_reason(void);
174180
#else
175181
#define kvm_guest_init() do { } while (0)
182+
#define kvm_async_pf_task_wait(T) do {} while(0)
183+
#define kvm_async_pf_task_wake(T) do {} while(0)
184+
static u32 kvm_read_and_reset_pf_reason(void)
185+
{
186+
return 0;
187+
}
176188
#endif
177189

178190
#endif /* __KERNEL__ */

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ asmlinkage void segment_not_present(void);
3030
asmlinkage void stack_segment(void);
3131
asmlinkage void general_protection(void);
3232
asmlinkage void page_fault(void);
33+
asmlinkage void async_page_fault(void);
3334
asmlinkage void spurious_interrupt_bug(void);
3435
asmlinkage void coprocessor_error(void);
3536
asmlinkage void alignment_check(void);

‎arch/x86/kernel/entry_32.S‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,16 @@ ENTRY(general_protection)
14061406
CFI_ENDPROC
14071407
END(general_protection)
14081408

1409+
#ifdef CONFIG_KVM_GUEST
1410+
ENTRY(async_page_fault)
1411+
RING0_EC_FRAME
1412+
pushl $do_async_page_fault
1413+
CFI_ADJUST_CFA_OFFSET 4
1414+
jmp error_code
1415+
CFI_ENDPROC
1416+
END(apf_page_fault)
1417+
#endif
1418+
14091419
/*
14101420
* End of kprobes section
14111421
*/

‎arch/x86/kernel/entry_64.S‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,9 @@ errorentry xen_stack_segment do_stack_segment
13191319
#endif
13201320
errorentry general_protection do_general_protection
13211321
errorentry page_fault do_page_fault
1322+
#ifdef CONFIG_KVM_GUEST
1323+
errorentry async_page_fault do_async_page_fault
1324+
#endif
13221325
#ifdef CONFIG_X86_MCE
13231326
paranoidzeroentry machine_check *machine_check_vector(%rip)
13241327
#endif

‎arch/x86/kernel/kvm.c‎

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@
2929
#include <linux/hardirq.h>
3030
#include <linux/notifier.h>
3131
#include <linux/reboot.h>
32+
#include <linux/hash.h>
33+
#include <linux/sched.h>
34+
#include <linux/slab.h>
35+
#include <linux/kprobes.h>
3236
#include <asm/timer.h>
3337
#include <asm/cpu.h>
38+
#include <asm/traps.h>
39+
#include <asm/desc.h>
3440

3541
#define MMU_QUEUE_SIZE 1024
3642

@@ -64,6 +70,168 @@ static void kvm_io_delay(void)
6470
{
6571
}
6672

73+
#define KVM_TASK_SLEEP_HASHBITS 8
74+
#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
75+
76+
struct kvm_task_sleep_node {
77+
struct hlist_node link;
78+
wait_queue_head_t wq;
79+
u32 token;
80+
int cpu;
81+
};
82+
83+
static struct kvm_task_sleep_head {
84+
spinlock_t lock;
85+
struct hlist_head list;
86+
} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
87+
88+
static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
89+
u32 token)
90+
{
91+
struct hlist_node *p;
92+
93+
hlist_for_each(p, &b->list) {
94+
struct kvm_task_sleep_node *n =
95+
hlist_entry(p, typeof(*n), link);
96+
if (n->token == token)
97+
return n;
98+
}
99+
100+
return NULL;
101+
}
102+
103+
void kvm_async_pf_task_wait(u32 token)
104+
{
105+
u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
106+
struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
107+
struct kvm_task_sleep_node n, *e;
108+
DEFINE_WAIT(wait);
109+
110+
spin_lock(&b->lock);
111+
e = _find_apf_task(b, token);
112+
if (e) {
113+
/* dummy entry exist -> wake up was delivered ahead of PF */
114+
hlist_del(&e->link);
115+
kfree(e);
116+
spin_unlock(&b->lock);
117+
return;
118+
}
119+
120+
n.token = token;
121+
n.cpu = smp_processor_id();
122+
init_waitqueue_head(&n.wq);
123+
hlist_add_head(&n.link, &b->list);
124+
spin_unlock(&b->lock);
125+
126+
for (;;) {
127+
prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
128+
if (hlist_unhashed(&n.link))
129+
break;
130+
local_irq_enable();
131+
schedule();
132+
local_irq_disable();
133+
}
134+
finish_wait(&n.wq, &wait);
135+
136+
return;
137+
}
138+
EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
139+
140+
static void apf_task_wake_one(struct kvm_task_sleep_node *n)
141+
{
142+
hlist_del_init(&n->link);
143+
if (waitqueue_active(&n->wq))
144+
wake_up(&n->wq);
145+
}
146+
147+
static void apf_task_wake_all(void)
148+
{
149+
int i;
150+
151+
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
152+
struct hlist_node *p, *next;
153+
struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
154+
spin_lock(&b->lock);
155+
hlist_for_each_safe(p, next, &b->list) {
156+
struct kvm_task_sleep_node *n =
157+
hlist_entry(p, typeof(*n), link);
158+
if (n->cpu == smp_processor_id())
159+
apf_task_wake_one(n);
160+
}
161+
spin_unlock(&b->lock);
162+
}
163+
}
164+
165+
void kvm_async_pf_task_wake(u32 token)
166+
{
167+
u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
168+
struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
169+
struct kvm_task_sleep_node *n;
170+
171+
if (token == ~0) {
172+
apf_task_wake_all();
173+
return;
174+
}
175+
176+
again:
177+
spin_lock(&b->lock);
178+
n = _find_apf_task(b, token);
179+
if (!n) {
180+
/*
181+
* async PF was not yet handled.
182+
* Add dummy entry for the token.
183+
*/
184+
n = kmalloc(sizeof(*n), GFP_ATOMIC);
185+
if (!n) {
186+
/*
187+
* Allocation failed! Busy wait while other cpu
188+
* handles async PF.
189+
*/
190+
spin_unlock(&b->lock);
191+
cpu_relax();
192+
goto again;
193+
}
194+
n->token = token;
195+
n->cpu = smp_processor_id();
196+
init_waitqueue_head(&n->wq);
197+
hlist_add_head(&n->link, &b->list);
198+
} else
199+
apf_task_wake_one(n);
200+
spin_unlock(&b->lock);
201+
return;
202+
}
203+
EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
204+
205+
u32 kvm_read_and_reset_pf_reason(void)
206+
{
207+
u32 reason = 0;
208+
209+
if (__get_cpu_var(apf_reason).enabled) {
210+
reason = __get_cpu_var(apf_reason).reason;
211+
__get_cpu_var(apf_reason).reason = 0;
212+
}
213+
214+
return reason;
215+
}
216+
EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
217+
218+
dotraplinkage void __kprobes
219+
do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
220+
{
221+
switch (kvm_read_and_reset_pf_reason()) {
222+
default:
223+
do_page_fault(regs, error_code);
224+
break;
225+
case KVM_PV_REASON_PAGE_NOT_PRESENT:
226+
/* page is swapped out by the host. */
227+
kvm_async_pf_task_wait((u32)read_cr2());
228+
break;
229+
case KVM_PV_REASON_PAGE_READY:
230+
kvm_async_pf_task_wake((u32)read_cr2());
231+
break;
232+
}
233+
}
234+
67235
static void kvm_mmu_op(void *buffer, unsigned len)
68236
{
69237
int r;
@@ -300,6 +468,7 @@ static void kvm_guest_cpu_online(void *dummy)
300468
static void kvm_guest_cpu_offline(void *dummy)
301469
{
302470
kvm_pv_disable_apf(NULL);
471+
apf_task_wake_all();
303472
}
304473

305474
static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
@@ -327,13 +496,25 @@ static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
327496
};
328497
#endif
329498

499+
static void __init kvm_apf_trap_init(void)
500+
{
501+
set_intr_gate(14, &async_page_fault);
502+
}
503+
330504
void __init kvm_guest_init(void)
331505
{
506+
int i;
507+
332508
if (!kvm_para_available())
333509
return;
334510

335511
paravirt_ops_setup();
336512
register_reboot_notifier(&kvm_pv_reboot_nb);
513+
for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
514+
spin_lock_init(&async_pf_sleepers[i].lock);
515+
if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
516+
x86_init.irqs.trap_init = kvm_apf_trap_init;
517+
337518
#ifdef CONFIG_SMP
338519
smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
339520
register_cpu_notifier(&kvm_cpu_notifier);

‎arch/x86/kvm/svm.c‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <asm/tlbflush.h>
3333
#include <asm/desc.h>
34+
#include <asm/kvm_para.h>
3435

3536
#include <asm/virtext.h>
3637
#include "trace.h"
@@ -133,6 +134,7 @@ struct vcpu_svm {
133134

134135
unsigned int3_injected;
135136
unsigned long int3_rip;
137+
u32 apf_reason;
136138
};
137139

138140
#define MSR_INVALID 0xffffffffU
@@ -1383,16 +1385,33 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
13831385

13841386
static int pf_interception(struct vcpu_svm *svm)
13851387
{
1386-
u64 fault_address;
1388+
u64 fault_address = svm->vmcb->control.exit_info_2;
13871389
u32 error_code;
1390+
int r = 1;
13881391

1389-
fault_address = svm->vmcb->control.exit_info_2;
1390-
error_code = svm->vmcb->control.exit_info_1;
1392+
switch (svm->apf_reason) {
1393+
default:
1394+
error_code = svm->vmcb->control.exit_info_1;
13911395

1392-
trace_kvm_page_fault(fault_address, error_code);
1393-
if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1394-
kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1395-
return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1396+
trace_kvm_page_fault(fault_address, error_code);
1397+
if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1398+
kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1399+
r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1400+
break;
1401+
case KVM_PV_REASON_PAGE_NOT_PRESENT:
1402+
svm->apf_reason = 0;
1403+
local_irq_disable();
1404+
kvm_async_pf_task_wait(fault_address);
1405+
local_irq_enable();
1406+
break;
1407+
case KVM_PV_REASON_PAGE_READY:
1408+
svm->apf_reason = 0;
1409+
local_irq_disable();
1410+
kvm_async_pf_task_wake(fault_address);
1411+
local_irq_enable();
1412+
break;
1413+
}
1414+
return r;
13961415
}
13971416

13981417
static int db_interception(struct vcpu_svm *svm)
@@ -1836,8 +1855,8 @@ static int nested_svm_exit_special(struct vcpu_svm *svm)
18361855
return NESTED_EXIT_HOST;
18371856
break;
18381857
case SVM_EXIT_EXCP_BASE + PF_VECTOR:
1839-
/* When we're shadowing, trap PFs */
1840-
if (!npt_enabled)
1858+
/* When we're shadowing, trap PFs, but not async PF */
1859+
if (!npt_enabled && svm->apf_reason == 0)
18411860
return NESTED_EXIT_HOST;
18421861
break;
18431862
case SVM_EXIT_EXCP_BASE + NM_VECTOR:
@@ -1893,6 +1912,10 @@ static int nested_svm_intercept(struct vcpu_svm *svm)
18931912
u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
18941913
if (svm->nested.intercept_exceptions & excp_bits)
18951914
vmexit = NESTED_EXIT_DONE;
1915+
/* async page fault always cause vmexit */
1916+
else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
1917+
svm->apf_reason != 0)
1918+
vmexit = NESTED_EXIT_DONE;
18961919
break;
18971920
}
18981921
case SVM_EXIT_ERR: {
@@ -3414,6 +3437,10 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu)
34143437

34153438
svm->next_rip = 0;
34163439

3440+
/* if exit due to PF check for async PF */
3441+
if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3442+
svm->apf_reason = kvm_read_and_reset_pf_reason();
3443+
34173444
if (npt_enabled) {
34183445
vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
34193446
vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);

0 commit comments

Comments
 (0)