KVM: Add host swap event notifications for PV guest
Subsystems (10)
v5 cover KVM: Add host swap event notifications for PV guest
KVM virtualizes guest memory by means of shadow pages or HW assistance like NPT/EPT. Not all memory used by a guest is mapped into the guest address space or even present in a host memory at any given time. When vcpu tries to access memory page that is not mapped into the guest address space KVM is notified about it. KVM maps the page into the guest address space and resumes vcpu execution. If the page is swapped out from host memory vcpu execution is suspended till the page is not swapped into the memory again. This is inefficient since vcpu can do other work (run other task or serve interrupts) while page gets swapped in.
To overcome this inefficiency this patch series implements "asynchronous page fault" for paravirtualized KVM guests. If a page that vcpu is trying to access is swapped out KVM sends an async PF to the vcpu and continues vcpu execution. Requested page is swapped in by another thread in parallel. When vcpu gets async PF it puts faulted task to sleep until "wake up" interrupt is delivered. When the page is brought to the host memory KVM sends "wake up" interrupt and the guest's task resumes execution.
To measure performance benefits I use a simple benchmark program (below) that starts number of threads. Some of them do work (increment counter), others access huge array in random location trying to generate host page faults. The size of the array is smaller then guest memory bug bigger then host memory so we are guarantied that host will swap out part of the array.
Running the benchmark inside guest with 4 cpus 2G memory running in 512M container in the host using the command line "./bm -f 4 -w 4 -t 60" (run 4 faulting threads and 4 working threads for a minute) I get this result:
With async pf:
start
worker 0: 63972141051
worker 1: 65149033299
worker 2: 66301967246
worker 3: 63423000989
total: 258846142585
Without async pf:
start
worker 0: 30619912622
worker 1: 33951339266
worker 2: 31577780093
worker 3: 33603607972
total: 129752639953
The gain is 50%.
Perf data look like this:
With async pf:
97.93% bm bm [.] work_thread
1.74% bm [kernel.kallsyms] [k] retint_careful
0.10% bm [kernel.kallsyms] [k] _raw_spin_unlock_irq
0.08% bm bm [.] fault_thread
0.05% bm [kernel.kallsyms] [k] _raw_spin_unlock_irqrestore
0.02% bm [kernel.kallsyms] [k] __do_softirq
0.02% bm [kernel.kallsyms] [k] rcu_process_gp_endWithout async pf:
63.42% bm bm [.] work_thread
13.64% bm [kernel.kallsyms] [k] __do_softirq
8.95% bm bm [.] fault_thread
5.27% bm [kernel.kallsyms] [k] _raw_spin_unlock_irq
2.79% bm [kernel.kallsyms] [k] hrtimer_run_pending
2.35% bm [kernel.kallsyms] [k] run_timer_softirq
1.28% bm [kernel.kallsyms] [k] _raw_spin_lock_irq
1.16% bm [kernel.kallsyms] [k] debug_smp_processor_id
0.23% bm libc-2.10.2.so [.] random_r
0.18% bm [kernel.kallsyms] [k] rcu_bh_qs
0.18% bm [kernel.kallsyms] [k] find_busiest_group
0.14% bm [kernel.kallsyms] [k] retint_careful
0.14% bm libc-2.10.2.so [.] randomChanges:
v1->v2
Use MSR instead of hypercall.
Move most of the code into arch independent place.
halt inside a guest instead of doing "wait for page" hypercall if
preemption is disabled.
v2->v3
Use MSR from range 0x4b564dxx.
Add slot version tracking.
Support migration by restarting all guest processes after migration. Drop patch that tract preemptability for non-preemptable kernels
due to performance concerns. Send async PF to non-preemptable guests only when vcpu is executing userspace code.
v3->v4
Provide alternative page fault handler in PV guest instead of adding hook to
standard page fault handler and patch it out on non-PV guests.
Allow only limited number of outstanding async page fault per vcpu. Unify gfn_to_pfn and gfn_to_pfn_async code.
Cancel outstanding slow work on reset.
v4->v5
Move async pv cpu initialization into cpu hotplug notifier.
Use GFP_NOWAIT instead of GFP_ATOMIC for allocation that shouldn't sleep Process KVM_REQ_MMU_SYNC even in page_fault_other_cr3() before changing
cr3 back
Gleb Natapov (12):
Move kvm_smp_prepare_boot_cpu() from kvmclock.c to kvm.c. Add PV MSR to enable asynchronous page faults delivery. Add async PF initialization to PV guest.
Provide special async page fault handler when async PF capability is
detected
Export __get_user_pages_fast.
Add get_user_pages() variant that fails if major fault is required. Maintain memslot version number
Inject asynchronous page fault into a guest if page is swapped out. Retry fault before vmentry
Handle async PF in non preemptable context
Let host know whether the guest can handle async PF in non-userspace
context.
Send async PF when guest is not in userspace too.
arch/x86/include/asm/kvm_host.h | 27 ++++- arch/x86/include/asm/kvm_para.h | 14 ++ arch/x86/include/asm/traps.h | 1 + arch/x86/kernel/entry_32.S | 10 ++ arch/x86/kernel/entry_64.S | 3 + arch/x86/kernel/kvm.c | 280 +++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/kvmclock.c | 13 +-- arch/x86/kvm/Kconfig | 2 + arch/x86/kvm/mmu.c | 62 ++++++++- arch/x86/kvm/paging_tmpl.h | 53 +++++++- arch/x86/kvm/x86.c | 122 +++++++++++++++++- arch/x86/mm/gup.c | 2 + fs/ncpfs/mmap.c | 2 + include/linux/kvm.h | 1 + include/linux/kvm_host.h | 32 +++++ include/linux/mm.h | 5 + include/trace/events/kvm.h | 60 +++++++++ mm/filemap.c | 3 + mm/memory.c | 31 ++++- mm/shmem.c | 8 +- virt/kvm/Kconfig | 3 + virt/kvm/kvm_main.c | 266 ++++++++++++++++++++++++++++++++++++- 22 files changed, 966 insertions(+), 34 deletions(-)
=== benchmark.c ===
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#define FAULTING_THREADS 1
#define WORKING_THREADS 1
#define TIMEOUT 5
#define MEMORY 1024*1024*1024
pthread_barrier_t barrier;
volatile int stop;size_t pages;
void *fault_thread(void* p)
{
char *mem = p;
pthread_barrier_wait(&barrier);
while (!stop)
mem[(random() % pages) << 12] = 10;
pthread_barrier_wait(&barrier);
return NULL;
}void *work_thread(void* p)
{
unsigned long *i = p;
pthread_barrier_wait(&barrier);
while (!stop)
(*i)++;
pthread_barrier_wait(&barrier);
return NULL;
}int main(int argc, char **argv)
{
int ft = FAULTING_THREADS, wt = WORKING_THREADS;
unsigned int timeout = TIMEOUT;
size_t mem = MEMORY;
void *buf;
int i, opt, verbose = 0;
pthread_t t;
pthread_attr_t pattr;
unsigned long *res, sum = 0;
while((opt = getopt(argc, argv, "f:w:m:t:v")) != -1) {
switch (opt) {
case 'f':
ft = atoi(optarg);
break;
case 'w':
wt = atoi(optarg);
break;
case 'm':
mem = atoi(optarg);
break;
case 't':
timeout = atoi(optarg);
break;
case 'v':
verbose++;
break;
default:
fprintf(stderr, "Usage %s [-f num] [-w num] [-m byte] [-t secs]\n", argv[0]);
exit(1);
}
}
if (verbose)
printf("fault=%d work=%d mem=%lu timeout=%d\n", ft, wt, mem, timeout);
pages = mem >> 12;
posix_memalign(&buf, 4096, pages << 12);
res = malloc(sizeof (unsigned long) * wt);
memset(res, 0, sizeof (unsigned long) * wt);
pthread_attr_init(&pattr);
pthread_barrier_init(&barrier, NULL, ft + wt + 1);
for (i = 0; i < ft; i++) {
pthread_create(&t, &pattr, fault_thread, buf);
pthread_detach(t);
}
for (i = 0; i < wt; i++) {
pthread_create(&t, &pattr, work_thread, &res[i]);
pthread_detach(t);
}
/* prefault memory */
memset(buf, 0, pages << 12);
printf("start\n");
pthread_barrier_wait(&barrier);
pthread_barrier_destroy(&barrier);
pthread_barrier_init(&barrier, NULL, ft + wt + 1);
sleep(timeout);
stop = 1;
pthread_barrier_wait(&barrier);
for (i = 0; i < wt; i++) {
sum += res[i];
printf("worker %d: %lu\n", i, res[i]);
}
printf("total: %lu\n", sum);
return 0;
}--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 1/12 Move kvm_smp_prepare_boot_cpu() from kvmclock.c to kvm.c.
Async PF also needs to hook into smp_prepare_boot_cpu so move the hook into generic code.
--- arch/x86/include/asm/kvm_para.h | 1 + arch/x86/kernel/kvm.c | 11 +++++++++++ arch/x86/kernel/kvmclock.c | 13 +------------ 3 files changed, 13 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_para.h +1 -0
@@ -65,6 +65,7 @@ struct kvm_mmu_op_release_pt {#include <asm/processor.h> extern void kvmclock_init (void ); +extern int kvm_register_clock (char *txt);/* This instruction is vmcall. On non-VT architectures, it will generate a
arch/x86/kernel/kvm.c +11 -0
@@ -231,10 +231,21 @@ static void __init paravirt_ops_setup(void)#endif } +#ifdef CONFIG_SMP +static void __init kvm_smp_prepare_boot_cpu (void ) +{ +WARN_ON (kvm_register_clock ("primary cpu clock" )); +native_smp_prepare_boot_cpu (); +} +#endif +void __init kvm_guest_init (void ) {if (!kvm_para_available ())return ;paravirt_ops_setup (); +#ifdef CONFIG_SMP + smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; +#endif }
arch/x86/kernel/kvmclock.c +1 -12
@@ -125,7 +125,7 @@ static struct clocksource kvm_clock = { .flags = CLOCK_SOURCE_IS_CONTINUOUS, }; -static int kvm_register_clock (char *txt) +int kvm_register_clock (char *txt) {int cpu =smp_processor_id ();int low, high;
@@ -150,14 +150,6 @@ static void __cpuinit kvm_setup_secondary_clock(void) }#endif -#ifdef CONFIG_SMP -static void __init kvm_smp_prepare_boot_cpu (void ) -{ -WARN_ON (kvm_register_clock ("primary cpu clock" )); -native_smp_prepare_boot_cpu (); -} -#endif -/* * After the clock is registered, the host will keep writing to the * registered memory location. If the guest happens to shutdown, this memory
@@ -204,9 +196,6 @@ void __init kvmclock_init(void) x86_cpuinit.setup_percpu_clockev = kvm_setup_secondary_clock;#endif -#ifdef CONFIG_SMP - smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; -#endif machine_ops.shutdown = kvm_shutdown;#ifdef CONFIG_KEXEC machine_ops.crash_shutdown = kvm_crash_shutdown;
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 2/12 Add PV MSR to enable asynchronous page faults delivery.
In reply to Gleb NatapovGuess enables async PF vcpu functionality using this MSR.
--- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/include/asm/kvm_para.h | 4 +++ arch/x86/kvm/x86.c | 49 +++++++++++++++++++++++++++++++++++++- include/linux/kvm.h | 1 + 4 files changed, 55 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -364,6 +364,9 @@ struct kvm_vcpu_arch {u64 hv_vapic;cpumask_var_t wbinvd_dirty_mask; + +u32 __user *apf_data; +u64 apf_msr_val; };struct kvm_arch {
arch/x86/include/asm/kvm_para.h +4 -0
@@ -20,6 +20,7 @@* are available. The use of 0x11 and 0x12 is deprecated */ #define KVM_FEATURE_CLOCKSOURCE2 3 +#define KVM_FEATURE_ASYNC_PF 4/* The last 8 bits are used to indicate how to interpret the flags field * in pvclock structure. If no bits are set, all flags are ignored.
@@ -32,9 +33,12 @@/* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */ #define MSR_KVM_WALL_CLOCK_NEW 0x4b564d00#define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01 +#define MSR_KVM_ASYNC_PF_EN 0x4b564d02#define KVM_MAX_MMU_OP_BATCH 32 +#define KVM_ASYNC_PF_ENABLED (1 << 0) +/* Operations for KVM_HC_MMU_OP */ #define KVM_MMU_OP_WRITE_PTE 1#define KVM_MMU_OP_FLUSH_TLB 2
arch/x86/kvm/x86.c +47 -2
95 lines not on this page · raw message
include/linux/kvm.h +1 -0
@@ -530,6 +530,7 @@ struct kvm_enable_cap {#ifdef __KVM_HAVE_XCRS#define KVM_CAP_XCRS 56#endif +#define KVM_CAP_ASYNC_PF 57#ifdef KVM_CAP_IRQ_ROUTING
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 02/12] Add PV MSR to enable asynchronous page faults delivery.
Quoted 16 lines · Gleb Natapov
Guess enables async PF vcpu functionality using this MSR.
arch/x86/kvm/x86.c +12
-0+static int kvm_pv_enable_async_pf (struct kvm_vcpu *vcpu,u64 data) +{ +u64 gpa = data& ~0x3f ; +int offset =offset_in_page (gpa); +unsigned long addr; + +/* Bits 1:5 are resrved, Should be zero */ +if (data&0x3e ) +return 1 ; + + vcpu->arch.apf_msr_val = data; +
arch/x86/kvm/x86.c static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1238
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+if (!(data&KVM_ASYNC_PF_ENABLED )) { + vcpu->arch.apf_data =NULL ; +return 0 ; + } + + addr =gfn_to_hva (vcpu->kvm, gpa>>PAGE_SHIFT ); +if (kvm_is_error_hva (addr)) +return 1 ; + + vcpu->arch.apf_data = (u32 __user *)(addr + offset);
This can be invalidated by host userspace playing with memory regions. It needs to be recalculated on memory map changes, and it may disappear from under the guest's feet (in which case we're allowed to KVM_REQ_TRIPLE_FAULT it).
(note: this is a much better approach than kvmclock's and vapic's, we should copy it there)
arch/x86/kvm/x86.c static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1244
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+ +/* check if address is mapped */ +if (get_user (offset, vcpu->arch.apf_data)) { + vcpu->arch.apf_data =NULL ; +return 1 ; + }
So, this check can succeed today but fail tomorrow.
Quoted 3 lines
arch/x86/kvm/x86.c +3
-0+return 0 ; +} +
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 02/12] Add PV MSR to enable asynchronous page faults delivery.
Quoted 33 lines · Avi Kivity
Quoted 16 lines · Gleb Natapov
Guess enables async PF vcpu functionality using this MSR.
arch/x86/kvm/x86.c +12
-0+static int kvm_pv_enable_async_pf (struct kvm_vcpu *vcpu,u64 data) +{ +u64 gpa = data& ~0x3f ; +int offset =offset_in_page (gpa); +unsigned long addr; + +/* Bits 1:5 are resrved, Should be zero */ +if (data&0x3e ) +return 1 ; + + vcpu->arch.apf_msr_val = data; +arch/x86/kvm/x86.c static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1238
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+if (!(data&KVM_ASYNC_PF_ENABLED )) { + vcpu->arch.apf_data =NULL ; +return 0 ; + } + + addr =gfn_to_hva (vcpu->kvm, gpa>>PAGE_SHIFT ); +if (kvm_is_error_hva (addr)) +return 1 ; + + vcpu->arch.apf_data = (u32 __user *)(addr + offset);This can be invalidated by host userspace playing with memory regions. It needs to be recalculated on memory map changes, and it may disappear from under the guest's feet (in which case we're allowed to
KVM_REQ_TRIPLE_FAULTit).
Quoted 3 lines
(note: this is a much better approach than kvmclock's and vapic's, we should copy it there)
apf_put_user() tracks memory slot changes and revalidate the address if needed.
Quoted 10 lines
arch/x86/kvm/x86.c static int set_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1244
Quoted 6 lines
arch/x86/kvm/x86.c +6
-0+ +/* check if address is mapped */ +if (get_user (offset, vcpu->arch.apf_data)) { + vcpu->arch.apf_data =NULL ; +return 1 ; + }So, this check can succeed today but fail tomorrow.
Quoted 6 lines
Quoted 3 lines
arch/x86/kvm/x86.c +3
-0+return 0 ; +} +Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 3/12 Add async PF initialization to PV guest.
In reply to Gleb NatapovEnable async PF in a guest if async PF capability is discovered.
--- arch/x86/include/asm/kvm_para.h | 5 +++ arch/x86/kernel/kvm.c | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +5 -0
@@ -65,6 +65,11 @@ struct kvm_mmu_op_release_pt {__u64 pt_phys; }; +struct kvm_vcpu_pv_apf_data { +__u32 reason; +__u32 enabled; +}; +#ifdef __KERNEL__#include <asm/processor.h>
arch/x86/kernel/kvm.c +68 -0
100 lines not on this page · raw message
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 03/12] Add async PF initialization to PV guest.
Quoted 3 lines · Gleb Natapov
Enable async PF in a guest if async PF capability is discovered.
Signed-off-by Gleb Natapov<[email protected]>
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 03/12] Add async PF initialization to PV guest.
In reply to Gleb NatapovQuoted 13 lines · Gleb Natapov
Enable async PF in a guest if async PF capability is discovered.
Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/include/asm/kvm_para.h | 5 +++ arch/x86/kernel/kvm.c | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 0 deletions(-)arch/x86/include/asm/kvm_para.h +0
-0
arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt { · line 72
Quoted 8 lines
arch/x86/include/asm/kvm_para.h +5
-0@@ -65,6 +65,11 @@ struct kvm_mmu_op_release_pt {__u64 pt_phys; }; +struct kvm_vcpu_pv_apf_data { +__u32 reason; +__u32 enabled; +}; +
The guest will have to align this on a 64 byte boundary, should this be marked __aligned(64) here?
Quoted 10 lines
arch/x86/include/asm/kvm_para.h +6
-0@@ -231,12 +235,72 @@ static void __init paravirt_ops_setup(void)#endif } +void __cpuinit kvm_guest_cpu_init (void ) +{ +if (!kvm_para_available ()) +return ; + +if (kvm_para_has_feature (KVM_FEATURE_ASYNC_PF )) {
arch/x86/include/asm/kvm_para.h static void __init paravirt_ops_setup(void) · line 253
Quoted 10 lines
arch/x86/include/asm/kvm_para.h +10
-0+u64 pa =__pa (&__get_cpu_var (apf_reason)); + +if (native_write_msr_safe (MSR_KVM_ASYNC_PF_EN , + pa |KVM_ASYNC_PF_ENABLED , pa>>32 )) +return ; +__get_cpu_var (apf_reason).enabled =1 ; +printk (KERN_INFO "KVM setup async PF for cpu %d\n" , +smp_processor_id ()); + } +}
Need a way to disable apf from the guest kernel command line.
arch/x86/include/asm/kvm_para.h static void __init paravirt_ops_setup(void) · line 264
Quoted 11 lines
arch/x86/include/asm/kvm_para.h +11
-0+ +static int __cpuinit kvm_cpu_notify (struct notifier_block *self, +unsigned long action,void *hcpu) +{ +switch (action) { +case CPU_ONLINE : +case CPU_ONLINE_FROZEN : +kvm_guest_cpu_init (); +break ; +default : +break ;
Should we disable apf if the cpu is dying here?
Quoted 4 lines
arch/x86/include/asm/kvm_para.h +4
-0+ } +return NOTIFY_OK ; +} +
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 03/12] Add async PF initialization to PV guest.
Quoted 17 lines · Avi Kivity
Quoted 16 lines · Gleb Natapov
Enable async PF in a guest if async PF capability is discovered.
Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/include/asm/kvm_para.h | 5 +++ arch/x86/kernel/kvm.c | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 0 deletions(-)arch/x86/include/asm/kvm_para.h +0
-0@@ -65,6 +65,11 @@ struct kvm_mmu_op_release_pt {__u64 pt_phys; };
Quoted 9 lines
arch/x86/include/asm/kvm_para.h struct kvm_mmu_op_release_pt { · line 72
Quoted 5 lines
arch/x86/include/asm/kvm_para.h +5
-0+struct kvm_vcpu_pv_apf_data { +__u32 reason; +__u32 enabled; +}; +The guest will have to align this on a 64 byte boundary, should this be marked
__aligned(64)here?
I do __aligned(64) when I declare variable of that type:
static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
Quoted 14 lines
Quoted 14 lines
arch/x86/include/asm/kvm_para.h +10
-0@@ -231,12 +235,72 @@ static void __init paravirt_ops_setup(void)#endif } +void __cpuinit kvm_guest_cpu_init (void ) +{ +if (!kvm_para_available ()) +return ; + +if (kvm_para_has_feature (KVM_FEATURE_ASYNC_PF )) { +u64 pa =__pa (&__get_cpu_var (apf_reason)); + +if (native_write_msr_safe (MSR_KVM_ASYNC_PF_EN , + pa |KVM_ASYNC_PF_ENABLED , pa>>32 ))
Quoted 9 lines
arch/x86/include/asm/kvm_para.h static void __init paravirt_ops_setup(void) · line 253
Quoted 6 lines
arch/x86/include/asm/kvm_para.h +6
-0+return ; +__get_cpu_var (apf_reason).enabled =1 ; +printk (KERN_INFO "KVM setup async PF for cpu %d\n" , +smp_processor_id ()); + } +}Need a way to disable apf from the guest kernel command line.
OK.
Quoted 5 lines
Quoted 5 lines
arch/x86/include/asm/kvm_para.h +5
-0+ +static int __cpuinit kvm_cpu_notify (struct notifier_block *self, +unsigned long action,void *hcpu) +{ +switch (action) {
Quoted 9 lines
arch/x86/include/asm/kvm_para.h static void __init paravirt_ops_setup(void) · line 264
Quoted 6 lines
arch/x86/include/asm/kvm_para.h +6
-0+case CPU_ONLINE : +case CPU_ONLINE_FROZEN : +kvm_guest_cpu_init (); +break ; +default : +break ;Should we disable apf if the cpu is dying here?
Why? Can CPU die with outstanding sleeping tasks?
Quoted 7 lines
Quoted 4 lines
arch/x86/include/asm/kvm_para.h +4
-0+ } +return NOTIFY_OK ; +} +Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 03/12] Add async PF initialization to PV guest.
Quoted 6 lines · Gleb Natapov
Quoted 3 lines
The guest will have to align this on a 64 byte boundary, should this be marked
__aligned(64)here?I do
__aligned(64)when I declare variable of that type:static
DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason)__aligned(64);
64 byte boundary: You mean cacheline aligned? We have a special define for that.
DEFINE_PER_CPU_SHARED_ALIGNED
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 03/12] Add async PF initialization to PV guest.
Quoted 5 lines · Christoph Lameter
Quoted 3 lines · Gleb Natapov
Quoted 3 lines
The guest will have to align this on a 64 byte boundary, should this be marked
__aligned(64)here?
Quoted 8 lines
Quoted 3 lines
I do
__aligned(64)when I declare variable of that type:static
DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason)__aligned(64);64 byte boundary: You mean cacheline aligned? We have a special define for that.
DEFINE_PER_CPU_SHARED_ALIGNED
IIRC I tried to use it and it does different alignment on 64/32 bit. The alignment here is part of guest/host interface so it should be the same on both.
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 03/12] Add async PF initialization to PV guest.
In reply to Christoph LameterQuoted 5 lines · Christoph Lameter
Quoted 3 lines · Gleb Natapov
Quoted 3 lines
The guest will have to align this on a 64 byte boundary, should this be marked
__aligned(64)here?
Quoted 8 lines
Quoted 3 lines
I do
__aligned(64)when I declare variable of that type:static
DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason)__aligned(64);64 byte boundary: You mean cacheline aligned? We have a special define for that.
DEFINE_PER_CPU_SHARED_ALIGNED
It's an ABI, so we can't use something that might change when Intel releases a cpu with 75.2 byte cache lines.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 4/12 Provide special async page fault handler when async PF capability is detected
In reply to Gleb NatapovWhen 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.
--- arch/x86/include/asm/kvm_para.h | 3 + arch/x86/include/asm/traps.h | 1 + arch/x86/kernel/entry_32.S | 10 +++ arch/x86/kernel/entry_64.S | 3 + arch/x86/kernel/kvm.c | 170 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +3 -0
@@ -65,6 +65,9 @@ struct kvm_mmu_op_release_pt {__u64 pt_phys; }; +#define KVM_PV_REASON_PAGE_NOT_PRESENT 1 +#define KVM_PV_REASON_PAGE_READY 2 +struct kvm_vcpu_pv_apf_data {__u32 reason;__u32 enabled;
arch/x86/include/asm/traps.h +1 -0
@@ -30,6 +30,7 @@ asmlinkage void segment_not_present(void); asmlinkage void stack_segment(void); asmlinkage void general_protection(void); asmlinkage void page_fault(void); +asmlinkage void async_page_fault(void); asmlinkage void spurious_interrupt_bug(void); asmlinkage void coprocessor_error(void); asmlinkage void alignment_check(void);
arch/x86/kernel/entry_32.S +10 -0
@@ -1494,6 +1494,16 @@ ENTRY(general_protection) CFI_ENDPROC END(general_protection) +#ifdef CONFIG_KVM_GUEST +ENTRY(async_page_fault) + RING0_EC_FRAME + pushl $do_async_page_fault + CFI_ADJUST_CFA_OFFSET 4 + jmp error_code + CFI_ENDPROC +END(apf_page_fault) +#endif + /* * End of kprobes section */
arch/x86/kernel/entry_64.S +3 -0
@@ -1346,6 +1346,9 @@ errorentry xen_stack_segment do_stack_segment #endif errorentry general_protection do_general_protection errorentry page_fault do_page_fault +#ifdef CONFIG_KVM_GUEST +errorentry async_page_fault do_async_page_fault +#endif #ifdef CONFIG_X86_MCE paranoidzeroentry machine_check *machine_check_vector(%rip) #endif
arch/x86/kernel/kvm.c +170 -0
197 lines not on this page · raw message
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 04/12] Provide special async page fault handler when async PF capability is detected
Quoted 105 lines · Gleb Natapov
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.
Acked-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/include/asm/kvm_para.h | 3 + arch/x86/include/asm/traps.h | 1 + arch/x86/kernel/entry_32.S | 10 +++ arch/x86/kernel/entry_64.S | 3 + arch/x86/kernel/kvm.c | 170 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 0 deletions(-)arch/x86/include/asm/kvm_para.h +3
-0@@ -65,6 +65,9 @@ struct kvm_mmu_op_release_pt {__u64 pt_phys; }; +#define KVM_PV_REASON_PAGE_NOT_PRESENT 1 +#define KVM_PV_REASON_PAGE_READY 2 +struct kvm_vcpu_pv_apf_data {__u32 reason;__u32 enabled;arch/x86/include/asm/traps.h +1
-0@@ -30,6 +30,7 @@ asmlinkage void segment_not_present(void); asmlinkage void stack_segment(void); asmlinkage void general_protection(void); asmlinkage void page_fault(void); +asmlinkage void async_page_fault(void); asmlinkage void spurious_interrupt_bug(void); asmlinkage void coprocessor_error(void); asmlinkage void alignment_check(void);arch/x86/kernel/entry_32.S +10
-0@@ -1494,6 +1494,16 @@ ENTRY(general_protection) CFI_ENDPROC END(general_protection) +#ifdef CONFIG_KVM_GUEST +ENTRY(async_page_fault) + RING0_EC_FRAME + pushl $do_async_page_fault + CFI_ADJUST_CFA_OFFSET 4 + jmp error_code + CFI_ENDPROC +END(apf_page_fault) +#endif + /* * End of kprobes section */arch/x86/kernel/entry_64.S +3
-0@@ -1346,6 +1346,9 @@ errorentry xen_stack_segment do_stack_segment #endif errorentry general_protection do_general_protection errorentry page_fault do_page_fault +#ifdef CONFIG_KVM_GUEST +errorentry async_page_fault do_async_page_fault +#endif #ifdef CONFIG_X86_MCE paranoidzeroentry machine_check *machine_check_vector(%rip) #endifarch/x86/kernel/kvm.c +13
-0@@ -29,8 +29,14 @@#include <linux/hardirq.h> #include <linux/notifier.h> #include <linux/reboot.h> +#include <linux/hash.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/kprobes.h> #include <asm/timer.h> #include <asm/cpu.h> +#include <asm/traps.h> +#include <asm/desc.h> #define MMU_QUEUE_SIZE 1024@@ -54,6 +60,158 @@ static void kvm_io_delay(void) { } +#define KVM_TASK_SLEEP_HASHBITS 8 +#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS) + +struct kvm_task_sleep_node { +struct hlist_node link; +wait_queue_head_t wq; +u32 token;
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 79
Quoted 10 lines
arch/x86/kernel/kvm.c +10
-0+int cpu; +}; + +static struct kvm_task_sleep_head { +spinlock_t lock; +struct hlist_head list; +} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE ]; + +static struct kvm_task_sleep_node *_find_apf_task (struct kvm_task_sleep_head *b, +u64 token)
u64 token?
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 87
Quoted 8 lines
arch/x86/kernel/kvm.c +8
-0+{ +struct hlist_node *p; + +hlist_for_each (p,&b->list) { +struct kvm_task_sleep_node *n = +hlist_entry (p,typeof (*n), link); +if (n->token == token) +return n;
Do you need to match cpu here as well? Or is token globally unique?
Perhaps we should make it locally unique to remove a requirement from the host to synchronize? I haven't seen how you generate it yet.
Quoted 14 lines
arch/x86/kernel/kvm.c +14
-0+ } + +return NULL ; +} + +static void apf_task_wait (struct task_struct *tsk,u32 token) +{ +u32 key =hash_32 (token,KVM_TASK_SLEEP_HASHBITS ); +struct kvm_task_sleep_head *b =&async_pf_sleepers[key]; +struct kvm_task_sleep_node n, *e; +DEFINE_WAIT (wait); + +spin_lock (&b->lock); + e =_find_apf_task (b, token);
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 111
Quoted 10 lines
arch/x86/kernel/kvm.c +10
-0+if (e) { +/* dummy entry exist -> wake up was delivered ahead of PF */ +hlist_del (&e->link); +kfree (e); +spin_unlock (&b->lock); +return ; + } + + n.token = token; + n.cpu =smp_processor_id ();
What's the meaning of cpu? Won't the waiter migrate to other cpus? Can apf_task_wait() start on a different cpu than the one we got our apf on?
arch/x86/kernel/kvm.c static void kvm_io_delay(void)
Quoted 6 lines
arch/x86/kernel/kvm.c +6
-0+init_waitqueue_head (&n.wq); +hlist_add_head (&n.link,&b->list); +spin_unlock (&b->lock); + +for (;;) { +prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE );
In theory we could make it interruptible if it's in user context. The signal could arrive before the page and we could handle it. Not worth the complexity I think (having a wakeup with no task to wake).
The user might be confused why they have uninterruptible tasks and no disk load, but more than likely they're confused already, so no big loss.
Quoted 26 lines
arch/x86/kernel/kvm.c +26
-0+if (hlist_unhashed (&n.link)) +break ; +schedule (); + } +finish_wait (&n.wq,&wait); + +return ; +} + +static void apf_task_wake_one (struct kvm_task_sleep_node *n) +{ +hlist_del_init (&n->link); +if (waitqueue_active (&n->wq)) +wake_up (&n->wq); +} + +static void apf_task_wake (u32 token) +{ +u32 key =hash_32 (token,KVM_TASK_SLEEP_HASHBITS ); +struct kvm_task_sleep_head *b =&async_pf_sleepers[key]; +struct kvm_task_sleep_node *n; + +again: +spin_lock (&b->lock); + n =_find_apf_task (b, token); +if (!n) {
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 153
Quoted 10 lines
arch/x86/kernel/kvm.c +10
-0+/* +* async PF was not yet handled. +* Add dummy entry for the token. +*/ + n =kmalloc (sizeof (*n),GFP_ATOMIC ); +if (!n) { +/* +* Allocation failed! Busy wait while other vcpu +* handles async PF. +*/
In guest code, please use 'cpu', not 'vcpu'.
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 157
Quoted 4 lines
arch/x86/kernel/kvm.c +4
-0+spin_unlock (&b->lock); +cpu_relax (); + goto again; + }
The other cpu might be waiting for us to yield. We can fix it later with the the pv spinlock infrastructure.
Or, we can avoid the allocation. If at most one apf can be pending (is this true?), we can use a per-cpu variable for this dummy entry.
Quoted 26 lines
arch/x86/kernel/kvm.c +26
-0+ n->token = token; + n->cpu =smp_processor_id (); +init_waitqueue_head (&n->wq); +hlist_add_head (&n->link,&b->list); + }else +apf_task_wake_one (n); +spin_unlock (&b->lock); +return ; +} + +static void apf_task_wake_all (void ) +{ +int i; + +for (i =0 ; i<KVM_TASK_SLEEP_HASHSIZE ; i++) { +struct hlist_node *p, *next; +struct kvm_task_sleep_head *b =&async_pf_sleepers[i]; +spin_lock (&b->lock); +hlist_for_each_safe (p, next,&b->list) { +struct kvm_task_sleep_node *n = +hlist_entry (p,typeof (*n), link); +if (n->cpu ==smp_processor_id ()) +apf_task_wake_one (n); + } +spin_unlock (&b->lock); + }
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 193
Quoted 10 lines
arch/x86/kernel/kvm.c +10
-0+} + +dotraplinkage void __kprobes +do_async_page_fault (struct pt_regs *regs,unsigned long error_code) +{ +u32 reason =0 , token; + +if (__get_cpu_var (apf_reason).enabled) { + reason =__get_cpu_var (apf_reason).reason; +__get_cpu_var (apf_reason).reason =0 ;
Can per-cpu vars be in vmalloc space? if so they may trigger nested faults.
I don't think that's the case for core code, so probably safe here.
Quoted 25 lines
arch/x86/kernel/kvm.c +21
-0+ + token = (u32 )read_cr2 (); + } + +switch (reason) { +default : +do_page_fault (regs, error_code); +break ; +case KVM_PV_REASON_PAGE_NOT_PRESENT : +/* page is swapped out by the host. */ +apf_task_wait (current, token); +break ; +case KVM_PV_REASON_PAGE_READY : +if (unlikely (token == ~0 )) +apf_task_wake_all (); +else +apf_task_wake (token); +break ; + } +} +static void kvm_mmu_op (void *buffer,unsigned len) {int r;
arch/x86/kernel/kvm.c static struct notifier_block __cpuinitdata kvm_cpu_notifier = { · line 467
Quoted 7 lines
arch/x86/kernel/kvm.c +4
-0@@ -303,13 +461,25 @@ static struct notifier_block __cpuinitdata kvm_cpu_notifier = { };#endif +static void __init kvm_apf_trap_init (void ) +{ +set_intr_gate (14 ,&async_page_fault); +}
Nice! Zero impact on non-virt.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 04/12] Provide special async page fault handler when async PF capability is detected
Quoted 4 lines · Avi Kivity
Do you need to match cpu here as well? Or is token globally unique?
Perhaps we should make it locally unique to remove a requirement from the host to synchronize? I haven't seen how you generate it yet.
If a task goes to sleep on one VCPU, but that VCPU ends up not being runnable later on, it would be nice to wake the task up on on a different VCPU.
I do not remember why it is safe to send this wakeup event as an exception rather than an interrupt...
Quoted 5 lines
The other cpu might be waiting for us to yield. We can fix it later with the the pv spinlock infrastructure.
Or, we can avoid the allocation. If at most one apf can be pending (is this true?), we can use a per-cpu variable for this dummy entry.
Having a limit of just one APF pending kind of defeats the point.
At that point, a second one of these faults would put the VCPU to sleep, which prevents the first task from running once its pagefault (which started earlier) completes...
Signature · 7 lines
All rights reversed -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 04/12] Provide special async page fault handler when async PF capability is detected
Quoted 7 lines · Rik van Riel
Quoted 4 lines · Avi Kivity
Do you need to match cpu here as well? Or is token globally unique?
Perhaps we should make it locally unique to remove a requirement from the host to synchronize? I haven't seen how you generate it yet.
Quoted 6 lines
If a task goes to sleep on one VCPU, but that VCPU ends up not being runnable later on, it would be nice to wake the task up on on a different VCPU.
I do not remember why it is safe to send this wakeup event as an exception rather than an interrupt...
Wakeup could definitely be an interrupt, but the apf needs to be an exception so we reuse it.
Quoted 9 lines
Quoted 6 lines
The other cpu might be waiting for us to yield. We can fix it later with the the pv spinlock infrastructure.
Or, we can avoid the allocation. If at most one apf can be pending (is this true?), we can use a per-cpu variable for this dummy entry.
Having a limit of just one APF pending kind of defeats the point.
Yes. How about, one APF pending before it is seen by the guest - but how can we tell without an annoying xchg?
Quoted 6 lines
At that point, a second one of these faults would put the VCPU to sleep, which prevents the first task from running once its pagefault (which started earlier) completes...
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 04/12] Provide special async page fault handler when async PF capability is detected
In reply to Avi KivityQuoted 110 lines · Avi Kivity
Quoted 109 lines · Gleb Natapov
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.
Acked-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/include/asm/kvm_para.h | 3 + arch/x86/include/asm/traps.h | 1 + arch/x86/kernel/entry_32.S | 10 +++ arch/x86/kernel/entry_64.S | 3 + arch/x86/kernel/kvm.c | 170 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 0 deletions(-)arch/x86/include/asm/kvm_para.h +3
-0@@ -65,6 +65,9 @@ struct kvm_mmu_op_release_pt {__u64 pt_phys; }; +#define KVM_PV_REASON_PAGE_NOT_PRESENT 1 +#define KVM_PV_REASON_PAGE_READY 2 +struct kvm_vcpu_pv_apf_data {__u32 reason;__u32 enabled;arch/x86/include/asm/traps.h +1
-0@@ -30,6 +30,7 @@ asmlinkage void segment_not_present(void); asmlinkage void stack_segment(void); asmlinkage void general_protection(void); asmlinkage void page_fault(void); +asmlinkage void async_page_fault(void); asmlinkage void spurious_interrupt_bug(void); asmlinkage void coprocessor_error(void); asmlinkage void alignment_check(void);arch/x86/kernel/entry_32.S +10
-0@@ -1494,6 +1494,16 @@ ENTRY(general_protection) CFI_ENDPROC END(general_protection) +#ifdef CONFIG_KVM_GUEST +ENTRY(async_page_fault) + RING0_EC_FRAME + pushl $do_async_page_fault + CFI_ADJUST_CFA_OFFSET 4 + jmp error_code + CFI_ENDPROC +END(apf_page_fault) +#endif + /* * End of kprobes section */arch/x86/kernel/entry_64.S +3
-0@@ -1346,6 +1346,9 @@ errorentry xen_stack_segment do_stack_segment #endif errorentry general_protection do_general_protection errorentry page_fault do_page_fault +#ifdef CONFIG_KVM_GUEST +errorentry async_page_fault do_async_page_fault +#endif #ifdef CONFIG_X86_MCE paranoidzeroentry machine_check *machine_check_vector(%rip) #endifarch/x86/kernel/kvm.c +17
-0@@ -29,8 +29,14 @@#include <linux/hardirq.h> #include <linux/notifier.h> #include <linux/reboot.h> +#include <linux/hash.h> +#include <linux/sched.h> +#include <linux/slab.h> +#include <linux/kprobes.h> #include <asm/timer.h> #include <asm/cpu.h> +#include <asm/traps.h> +#include <asm/desc.h> #define MMU_QUEUE_SIZE 1024@@ -54,6 +60,158 @@ static void kvm_io_delay(void) { } +#define KVM_TASK_SLEEP_HASHBITS 8 +#define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS) + +struct kvm_task_sleep_node { +struct hlist_node link; +wait_queue_head_t wq; +u32 token; +int cpu; +}; + +static struct kvm_task_sleep_head {
Quoted 9 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 79
Quoted 6 lines
arch/x86/kernel/kvm.c +6
-0+spinlock_t lock; +struct hlist_head list; +} async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE ]; + +static struct kvm_task_sleep_node *_find_apf_task (struct kvm_task_sleep_head *b, +u64 token)u64 token?
Should be u32. Will fix.
Quoted 10 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 87
Quoted 8 lines
arch/x86/kernel/kvm.c +8
-0+{ +struct hlist_node *p; + +hlist_for_each (p,&b->list) { +struct kvm_task_sleep_node *n = +hlist_entry (p,typeof (*n), link); +if (n->token == token) +return n;Do you need to match cpu here as well? Or is token globally unique?
Tokens are globally unique.
Quoted 4 lines
Perhaps we should make it locally unique to remove a requirement from the host to synchronize? I haven't seen how you generate it yet.
Host does not need to synchronize to generate globally unique token since vcpu id is part of a token.
Quoted 19 lines
Quoted 19 lines
arch/x86/kernel/kvm.c +18
-0+ } + +return NULL ; +} + +static void apf_task_wait (struct task_struct *tsk,u32 token) +{ +u32 key =hash_32 (token,KVM_TASK_SLEEP_HASHBITS ); +struct kvm_task_sleep_head *b =&async_pf_sleepers[key]; +struct kvm_task_sleep_node n, *e; +DEFINE_WAIT (wait); + +spin_lock (&b->lock); + e =_find_apf_task (b, token); +if (e) { +/* dummy entry exist -> wake up was delivered ahead of PF */ +hlist_del (&e->link); +kfree (e);
Quoted 8 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 111
Quoted 6 lines
arch/x86/kernel/kvm.c +6
-0+spin_unlock (&b->lock); +return ; + } + + n.token = token; + n.cpu =smp_processor_id ();What's the meaning of cpu? Won't the waiter migrate to other cpus?
Waiter cannot migrate to other cpu since it is sleeping. It may be scheduled to run on any cpu when it will be waked.
Quoted 2 lines
Can
apf_task_wait()start on a different cpu than the one we got our apf on?
No. It is called directly from exception handler.
Quoted 46 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void)
Quoted 7 lines
arch/x86/kernel/kvm.c +6
-0+init_waitqueue_head (&n.wq); +hlist_add_head (&n.link,&b->list); +spin_unlock (&b->lock); + +for (;;) { +prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE );In theory we could make it interruptible if it's in user context. The signal could arrive before the page and we could handle it. Not worth the complexity I think (having a wakeup with no task to wake).
The user might be confused why they have uninterruptible tasks and no disk load, but more than likely they're confused already, so no big loss.
Quoted 30 lines
arch/x86/kernel/kvm.c +30
-0+if (hlist_unhashed (&n.link)) +break ; +schedule (); + } +finish_wait (&n.wq,&wait); + +return ; +} + +static void apf_task_wake_one (struct kvm_task_sleep_node *n) +{ +hlist_del_init (&n->link); +if (waitqueue_active (&n->wq)) +wake_up (&n->wq); +} + +static void apf_task_wake (u32 token) +{ +u32 key =hash_32 (token,KVM_TASK_SLEEP_HASHBITS ); +struct kvm_task_sleep_head *b =&async_pf_sleepers[key]; +struct kvm_task_sleep_node *n; + +again: +spin_lock (&b->lock); + n =_find_apf_task (b, token); +if (!n) { +/* +* async PF was not yet handled. +* Add dummy entry for the token. +*/
Quoted 9 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 153
Quoted 6 lines
arch/x86/kernel/kvm.c +6
-0+ n =kmalloc (sizeof (*n),GFP_ATOMIC ); +if (!n) { +/* +* Allocation failed! Busy wait while other vcpu +* handles async PF. +*/ In guest code, please use 'cpu', not 'vcpu'.
OK. This is just a comment :)
Quoted 8 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 157
Quoted 4 lines
arch/x86/kernel/kvm.c +4
-0+spin_unlock (&b->lock); +cpu_relax (); + goto again; + }The other cpu might be waiting for us to yield. We can fix it later with the the pv spinlock infrastructure.
This busy wait happens only if (very small) allocation fails, so if a guest ever hits this code path I expect it to be on his way to die anyway.
Quoted 3 lines
Or, we can avoid the allocation. If at most one apf can be pending (is this true?), we can use a per-cpu variable for this dummy entry.
We can have may outstanding apfs.
Quoted 69 lines
Quoted 26 lines
arch/x86/kernel/kvm.c +26
-0+ n->token = token; + n->cpu =smp_processor_id (); +init_waitqueue_head (&n->wq); +hlist_add_head (&n->link,&b->list); + }else +apf_task_wake_one (n); +spin_unlock (&b->lock); +return ; +} + +static void apf_task_wake_all (void ) +{ +int i; + +for (i =0 ; i<KVM_TASK_SLEEP_HASHSIZE ; i++) { +struct hlist_node *p, *next; +struct kvm_task_sleep_head *b =&async_pf_sleepers[i]; +spin_lock (&b->lock); +hlist_for_each_safe (p, next,&b->list) { +struct kvm_task_sleep_node *n = +hlist_entry (p,typeof (*n), link); +if (n->cpu ==smp_processor_id ()) +apf_task_wake_one (n); + } +spin_unlock (&b->lock); + }arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 193
Quoted 10 lines
arch/x86/kernel/kvm.c +10
-0+} + +dotraplinkage void __kprobes +do_async_page_fault (struct pt_regs *regs,unsigned long error_code) +{ +u32 reason =0 , token; + +if (__get_cpu_var (apf_reason).enabled) { + reason =__get_cpu_var (apf_reason).reason; +__get_cpu_var (apf_reason).reason =0 ;Can per-cpu vars be in vmalloc space? if so they may trigger nested faults.
I don't think that's the case for core code, so probably safe here.
Quoted 28 lines
arch/x86/kernel/kvm.c +21
-0+ + token = (u32 )read_cr2 (); + } + +switch (reason) { +default : +do_page_fault (regs, error_code); +break ; +case KVM_PV_REASON_PAGE_NOT_PRESENT : +/* page is swapped out by the host. */ +apf_task_wait (current, token); +break ; +case KVM_PV_REASON_PAGE_READY : +if (unlikely (token == ~0 )) +apf_task_wake_all (); +else +apf_task_wake (token); +break ; + } +} +static void kvm_mmu_op (void *buffer,unsigned len) {int r;@@ -303,13 +461,25 @@ static struct notifier_block __cpuinitdata kvm_cpu_notifier = { };#endif
Quoted 9 lines
arch/x86/kernel/kvm.c static struct notifier_block __cpuinitdata kvm_cpu_notifier = { · line 467
Quoted 4 lines
arch/x86/kernel/kvm.c +4
-0+static void __init kvm_apf_trap_init (void ) +{ +set_intr_gate (14 ,&async_page_fault); +}Nice! Zero impact on non-virt.
Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 04/12] Provide special async page fault handler when async PF capability is detected
Quoted 21 lines · Gleb Natapov
+
Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/kvm.c +12
-0+static void apf_task_wait (struct task_struct *tsk,u32 token) +{ +u32 key =hash_32 (token,KVM_TASK_SLEEP_HASHBITS ); +struct kvm_task_sleep_head *b =&async_pf_sleepers[key]; +struct kvm_task_sleep_node n, *e; +DEFINE_WAIT (wait); + +spin_lock (&b->lock); + e =_find_apf_task (b, token); +if (e) { +/* dummy entry exist -> wake up was delivered ahead of PF */ +hlist_del (&e->link);Quoted 8 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 111
Quoted 7 lines
arch/x86/kernel/kvm.c +7
-0+kfree (e); +spin_unlock (&b->lock); +return ; + } + + n.token = token; + n.cpu =smp_processor_id ();What's the meaning of cpu? Won't the waiter migrate to other cpus?
Quoted 2 lines
Waiter cannot migrate to other cpu since it is sleeping. It may be scheduled to run on any cpu when it will be waked.
What if you have a spurious wakeup? Also, nothing prevents the scheduler from migrating the thread even if it is sleeping. It may not do so now, but it might do it in the future.
Oh, it probably does now on cpu hotunplug.
Why do you need n.cpu?
Quoted 10 lines
Quoted 7 lines
arch/x86/kernel/kvm.c static void kvm_io_delay(void) · line 157
Quoted 4 lines
arch/x86/kernel/kvm.c +4
-0+spin_unlock (&b->lock); +cpu_relax (); + goto again; + }The other cpu might be waiting for us to yield. We can fix it later with the the pv spinlock infrastructure.
This busy wait happens only if (very small) allocation fails, so if a guest ever hits this code path I expect it to be on his way to die anyway.
Hm. I don't have a good feel on how rare atomic allocation failures are on common workloads.
Note a kmem_cache for apfs will make failures even more rare.
Quoted 4 lines
Quoted 3 lines
Or, we can avoid the allocation. If at most one apf can be pending (is this true?), we can use a per-cpu variable for this dummy entry.
We can have may outstanding apfs.
But, while we're processing an apf, we can't take any more.
So we can have a buffer of one pre-allocated entry per cpu, and do something like:
apf:
disable apf for this cpu
handle apf using buffered entry
enable interrupts
allocate new entry
buffer it
enable apf for that cpu
this trades off a bigger apf disabled window for not busy looping.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 5/12 Export __get_user_pages_fast.
In reply to Gleb NatapovKVM will use it to try and find a page without falling back to slow gup. That is why get_user_pages_fast() is not enough.
--- arch/x86/mm/gup.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
arch/x86/mm/gup.c +2 -0
@@ -8,6 +8,7 @@#include <linux/mm.h> #include <linux/vmstat.h> #include <linux/highmem.h> +#include <linux/module.h> #include <asm/pgtable.h>
@@ -274,6 +275,7 @@ int __get_user_pages_fast(unsigned long start, int nr_pages, int write,return nr; } +EXPORT_SYMBOL_GPL (__get_user_pages_fast);/** * get_user_pages_fast() - pin user pages in memory
Signature · 1 line
1.7.1
PATCH v5 6/12 Add get_user_pages() variant that fails if major fault is required.
In reply to Gleb NatapovThis patch add get_user_pages() variant that only succeeds if getting a reference to a page doesn't require major fault.
--- fs/ncpfs/mmap.c | 2 ++ include/linux/mm.h | 5 +++++ mm/filemap.c | 3 +++ mm/memory.c | 31 ++++++++++++++++++++++++++++--- mm/shmem.c | 8 +++++++- 5 files changed, 45 insertions(+), 4 deletions(-)
fs/ncpfs/mmap.c +2 -0
@@ -39,6 +39,8 @@ static int ncp_file_mmap_fault(struct vm_area_struct *area,int bufsize;int pos;/* XXX: loff_t ? */ +if (vmf->flags &FAULT_FLAG_MINOR ) +return VM_FAULT_MAJOR |VM_FAULT_ERROR ;/* * ncpfs has nothing against high pages as long * as recvmsg and memset works on it
include/linux/mm.h +5 -0
@@ -140,6 +140,7 @@ extern pgprot_t protection_map[16];#define FAULT_FLAG_WRITE 0x01/* Fault was a write access */ #define FAULT_FLAG_NONLINEAR 0x02/* Fault was via a nonlinear mapping */ #define FAULT_FLAG_MKWRITE 0x04/* Fault was mkwrite of existing pte */ +#define FAULT_FLAG_MINOR 0x08/* Do only minor fault */ /* * This interface is used by x86 PAT code to identify a pfn mapping that is
@@ -843,6 +844,9 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *int get_user_pages (struct task_struct *tsk,struct mm_struct *mm,unsigned long start,int nr_pages,int write,int force,struct page **pages,struct vm_area_struct **vmas); +int get_user_pages_noio (struct task_struct *tsk,struct mm_struct *mm, +unsigned long start,int nr_pages,int write,int force, +struct page **pages,struct vm_area_struct **vmas);int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages);struct page *get_dump_page (unsigned long addr);
@@ -1373,6 +1377,7 @@ struct page *follow_page(struct vm_area_struct *, unsigned long address,#define FOLL_GET 0x04/* do get_page on page */ #define FOLL_DUMP 0x08/* give error on hole if it would be zero */ #define FOLL_FORCE 0x10/* get_user_pages read/write w/o permission */ +#define FOLL_MINOR 0x20/* do only minor page faults */ typedef int (*pte_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr,void *data);
mm/filemap.c +3 -0
@@ -1548,6 +1548,9 @@ int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) goto no_cached_page; } }else { +if (vmf->flags &FAULT_FLAG_MINOR ) +return VM_FAULT_MAJOR |VM_FAULT_ERROR ; +/* No page in the page cache at all */ do_sync_mmap_readahead (vma, ra, file, offset);count_vm_event (PGMAJFAULT );
mm/memory.c +28 -3
@@ -1433,10 +1433,13 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,cond_resched ();while (!(page =follow_page (vma, start, foll_flags))) {int ret; +unsigned int fault_fl = + ((foll_flags &FOLL_WRITE ) ? +FAULT_FLAG_WRITE :0 ) | + ((foll_flags &FOLL_MINOR ) ? +FAULT_FLAG_MINOR :0 ); - ret =handle_mm_fault (mm, vma, start, - (foll_flags &FOLL_WRITE ) ? -FAULT_FLAG_WRITE :0 ); + ret =handle_mm_fault (mm, vma, start, fault_fl);if (ret &VM_FAULT_ERROR ) {if (ret &VM_FAULT_OOM )
@@ -1444,6 +1447,8 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,if (ret & (VM_FAULT_HWPOISON |VM_FAULT_SIGBUS ))return i ? i : -EFAULT ; +else if (ret &VM_FAULT_MAJOR ) +return i ? i : -EFAULT ;BUG (); }if (ret &VM_FAULT_MAJOR )
@@ -1554,6 +1559,23 @@ int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, }EXPORT_SYMBOL (get_user_pages); +int get_user_pages_noio (struct task_struct *tsk,struct mm_struct *mm, +unsigned long start,int nr_pages,int write,int force, +struct page **pages,struct vm_area_struct **vmas) +{ +int flags =FOLL_TOUCH |FOLL_MINOR ; + +if (pages) + flags |=FOLL_GET ; +if (write) + flags |=FOLL_WRITE ; +if (force) + flags |=FOLL_FORCE ; + +return __get_user_pages (tsk, mm, start, nr_pages, flags, pages, vmas); +} +EXPORT_SYMBOL (get_user_pages_noio); +/** * get_dump_page() - pin user page in memory while writing it to core dump * @addr: user address
@@ -2640,6 +2662,9 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,delayacct_set_flag (DELAYACCT_PF_SWAPIN ); page =lookup_swap_cache (entry);if (!page) { +if (flags &FAULT_FLAG_MINOR ) +return VM_FAULT_MAJOR |VM_FAULT_ERROR ; +grab_swap_token (mm);/* Contend for token _before_ read-in */ page =swapin_readahead (entry,GFP_HIGHUSER_MOVABLE , vma, address);
mm/shmem.c +7 -1
@@ -1227,6 +1227,7 @@ static int shmem_getpage(struct inode *inode, unsigned long idx,swp_entry_t swap;gfp_t gfp;int error; +int flags = type ? *type :0 ;if (idx >=SHMEM_MAX_INDEX )return -EFBIG ;
@@ -1275,6 +1276,11 @@ repeat: swappage =lookup_swap_cache (swap);if (!swappage) {shmem_swp_unmap (entry); +if (flags &FAULT_FLAG_MINOR ) { +spin_unlock (&info->lock); + *type =VM_FAULT_MAJOR |VM_FAULT_ERROR ; + goto failed; + }/* here we actually do the io */ if (type && !(*type &VM_FAULT_MAJOR )) {__count_vm_event (PGMAJFAULT );
@@ -1483,7 +1489,7 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct inode *inode = vma->vm_file->f_path.dentry->d_inode;int error; -int ret; +int ret = (int )vmf->flags;if (((loff_t )vmf->pgoff <<PAGE_CACHE_SHIFT ) >=i_size_read (inode))return VM_FAULT_SIGBUS ;
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 06/12] Add get_user_pages() variant that fails if major fault is required.
Quoted 3 lines · Gleb Natapov
This patch add
get_user_pages()variant that only succeeds if getting a reference to a page doesn't require major fault.
Andrew, can this go in through kvm.git?
Quoted 164 lines
Reviewed-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- fs/ncpfs/mmap.c | 2 ++ include/linux/mm.h | 5 +++++ mm/filemap.c | 3 +++ mm/memory.c | 31 ++++++++++++++++++++++++++++--- mm/shmem.c | 8 +++++++- 5 files changed, 45 insertions(+), 4 deletions(-)fs/ncpfs/mmap.c +2
-0@@ -39,6 +39,8 @@ static int ncp_file_mmap_fault(struct vm_area_struct *area,int bufsize;int pos;/* XXX: loff_t ? */ +if (vmf->flags&FAULT_FLAG_MINOR ) +return VM_FAULT_MAJOR |VM_FAULT_ERROR ;/* * ncpfs has nothing against high pages as long * as recvmsg and memset works on it include/linux/mm.h +5
-0@@ -140,6 +140,7 @@ extern pgprot_t protection_map[16];#define FAULT_FLAG_WRITE 0x01/* Fault was a write access */ #define FAULT_FLAG_NONLINEAR 0x02/* Fault was via a nonlinear mapping */ #define FAULT_FLAG_MKWRITE 0x04/* Fault was mkwrite of existing pte */ +#define FAULT_FLAG_MINOR 0x08/* Do only minor fault */ /* * This interface is used by x86 PAT code to identify a pfn mapping that is @@ -843,6 +844,9 @@ extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *int get_user_pages (struct task_struct *tsk,struct mm_struct *mm,unsigned long start,int nr_pages,int write,int force,struct page **pages,struct vm_area_struct **vmas); +int get_user_pages_noio (struct task_struct *tsk,struct mm_struct *mm, +unsigned long start,int nr_pages,int write,int force, +struct page **pages,struct vm_area_struct **vmas);int get_user_pages_fast (unsigned long start,int nr_pages,int write,struct page **pages);struct page *get_dump_page (unsigned long addr);@@ -1373,6 +1377,7 @@ struct page *follow_page(struct vm_area_struct *, unsigned long address,#define FOLL_GET 0x04/* do get_page on page */ #define FOLL_DUMP 0x08/* give error on hole if it would be zero */ #define FOLL_FORCE 0x10/* get_user_pages read/write w/o permission */ +#define FOLL_MINOR 0x20/* do only minor page faults */ typedef int (*pte_fn_t )(pte_t *pte,pgtable_t token,unsigned long addr,void *data);mm/filemap.c +3
-0@@ -1548,6 +1548,9 @@ int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) goto no_cached_page; } }else { +if (vmf->flags&FAULT_FLAG_MINOR ) +return VM_FAULT_MAJOR |VM_FAULT_ERROR ; +/* No page in the page cache at all */ do_sync_mmap_readahead (vma, ra, file, offset);count_vm_event (PGMAJFAULT );mm/memory.c +28
-3@@ -1433,10 +1433,13 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,cond_resched ();while (!(page =follow_page (vma, start, foll_flags))) {int ret; +unsigned int fault_fl = + ((foll_flags&FOLL_WRITE ) ? +FAULT_FLAG_WRITE :0 ) | + ((foll_flags&FOLL_MINOR ) ? +FAULT_FLAG_MINOR :0 ); - ret =handle_mm_fault (mm, vma, start, - (foll_flags&FOLL_WRITE ) ? -FAULT_FLAG_WRITE :0 ); + ret =handle_mm_fault (mm, vma, start, fault_fl);if (ret&VM_FAULT_ERROR ) {if (ret&VM_FAULT_OOM )@@ -1444,6 +1447,8 @@ int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,if (ret& (VM_FAULT_HWPOISON |VM_FAULT_SIGBUS ))return i ? i : -EFAULT ; +else if (ret&VM_FAULT_MAJOR ) +return i ? i : -EFAULT ;BUG (); }if (ret&VM_FAULT_MAJOR )@@ -1554,6 +1559,23 @@ int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, }EXPORT_SYMBOL (get_user_pages); +int get_user_pages_noio (struct task_struct *tsk,struct mm_struct *mm, +unsigned long start,int nr_pages,int write,int force, +struct page **pages,struct vm_area_struct **vmas) +{ +int flags =FOLL_TOUCH |FOLL_MINOR ; + +if (pages) + flags |=FOLL_GET ; +if (write) + flags |=FOLL_WRITE ; +if (force) + flags |=FOLL_FORCE ; + +return __get_user_pages (tsk, mm, start, nr_pages, flags, pages, vmas); +} +EXPORT_SYMBOL (get_user_pages_noio); +/** * get_dump_page() - pin user page in memory while writing it to core dump * @addr: user address @@ -2640,6 +2662,9 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,delayacct_set_flag (DELAYACCT_PF_SWAPIN ); page =lookup_swap_cache (entry);if (!page) { +if (flags&FAULT_FLAG_MINOR ) +return VM_FAULT_MAJOR |VM_FAULT_ERROR ; +grab_swap_token (mm);/* Contend for token _before_ read-in */ page =swapin_readahead (entry,GFP_HIGHUSER_MOVABLE , vma, address);mm/shmem.c +7
-1@@ -1227,6 +1227,7 @@ static int shmem_getpage(struct inode *inode, unsigned long idx,swp_entry_t swap;gfp_t gfp;int error; +int flags = type ? *type :0 ;if (idx>=SHMEM_MAX_INDEX )return -EFBIG ;@@ -1275,6 +1276,11 @@ repeat: swappage =lookup_swap_cache (swap);if (!swappage) {shmem_swp_unmap (entry); +if (flags&FAULT_FLAG_MINOR ) { +spin_unlock (&info->lock); + *type =VM_FAULT_MAJOR |VM_FAULT_ERROR ; + goto failed; + }/* here we actually do the io */ if (type&& !(*type&VM_FAULT_MAJOR )) {__count_vm_event (PGMAJFAULT );@@ -1483,7 +1489,7 @@ static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) {struct inode *inode = vma->vm_file->f_path.dentry->d_inode;int error; -int ret; +int ret = (int )vmf->flags;if (((loff_t )vmf->pgoff<<PAGE_CACHE_SHIFT )>=i_size_read (inode))return VM_FAULT_SIGBUS ;
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 7/12 Maintain memslot version number
In reply to Gleb NatapovCode that depends on particular memslot layout can track changes and adjust to new layout.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -177,6 +177,7 @@ struct kvm {raw_spinlock_t requests_lock;struct mutex slots_lock;struct mm_struct *mm;/* userspace tied to this vm */ +u32 memslot_version;struct kvm_memslots *memslots;struct srcu_struct srcu;#ifdef CONFIG_KVM_APIC_ARCHITECTURE
virt/kvm/kvm_main.c +1 -0
@@ -733,6 +733,7 @@ skip_lpage: slots->memslots[mem->slot] = new; old_memslots = kvm->memslots;rcu_assign_pointer (kvm->memslots, slots); + kvm->memslot_version++;synchronize_srcu_expedited (&kvm->srcu);kvm_arch_commit_memory_region (kvm, mem, old, user_alloc);
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 07/12] Maintain memslot version number
Quoted 21 lines · Gleb Natapov
Code that depends on particular memslot layout can track changes and adjust to new layout.
include/linux/kvm_host.h +1
-0@@ -177,6 +177,7 @@ struct kvm {raw_spinlock_t requests_lock;struct mutex slots_lock;struct mm_struct *mm;/* userspace tied to this vm */ +u32 memslot_version;struct kvm_memslots *memslots;struct srcu_struct srcu;#ifdef CONFIG_KVM_APIC_ARCHITECTURE virt/kvm/kvm_main.c +0
-0
virt/kvm/kvm_main.c skip_lpage: · line 739
Quoted 7 lines
virt/kvm/kvm_main.c +1
-0@@ -733,6 +733,7 @@ skip_lpage: slots->memslots[mem->slot] = new; old_memslots = kvm->memslots;rcu_assign_pointer (kvm->memslots, slots); + kvm->memslot_version++;synchronize_srcu_expedited (&kvm->srcu);kvm_arch_commit_memory_region (kvm, mem, old, user_alloc);
How does this interact with rcu? Nothing enforces consistency between rcu_dereference(kvm->memslots) and kvm->memslot_version.
Should probably be rcu_dereference(kvm->memslots)->version.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 8/12 Inject asynchronous page fault into a guest if page is swapped out.
In reply to Gleb NatapovIf guest access swapped out memory do not swap it in from vcpu thread context. Setup slow work to do swapping and send async page fault to a guest.
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.
--- arch/x86/include/asm/kvm_host.h | 16 +++ arch/x86/kvm/Kconfig | 2 + arch/x86/kvm/mmu.c | 35 +++++- arch/x86/kvm/paging_tmpl.h | 17 +++- arch/x86/kvm/x86.c | 63 +++++++++- include/linux/kvm_host.h | 31 +++++ include/trace/events/kvm.h | 60 +++++++++ virt/kvm/Kconfig | 3 + virt/kvm/kvm_main.c | 263 ++++++++++++++++++++++++++++++++++++++- 9 files changed, 481 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +16 -0
@@ -366,7 +366,9 @@ struct kvm_vcpu_arch {cpumask_var_t wbinvd_dirty_mask;u32 __user *apf_data; +u32 apf_memslot_ver;u64 apf_msr_val; +u32 async_pf_id; };struct kvm_arch {
@@ -444,6 +446,8 @@ struct kvm_vcpu_stat {u32 hypercalls;u32 irq_injections;u32 nmi_injections; +u32 apf_not_present; +u32 apf_present; };struct kvm_x86_ops {
@@ -528,6 +532,10 @@ struct kvm_x86_ops {const struct trace_print_flags *exit_reasons_str; }; +struct kvm_arch_async_pf { +u32 token; +}; +extern struct kvm_x86_ops *kvm_x86_ops;int kvm_mmu_module_init (void );
@@ -763,4 +771,12 @@ void kvm_set_shared_msr(unsigned index, u64 val, u64 mask);bool kvm_is_linear_rip (struct kvm_vcpu *vcpu,unsigned long linear_rip); +struct kvm_async_pf ; + +void kvm_arch_inject_async_page_not_present (struct kvm_vcpu *vcpu, +struct kvm_async_pf *work); +void kvm_arch_inject_async_page_present (struct kvm_vcpu *vcpu, +struct kvm_async_pf *work); +bool kvm_arch_can_inject_async_page_present (struct kvm_vcpu *vcpu);#endif /* _ASM_X86_KVM_HOST_H */ +
arch/x86/kvm/Kconfig +2 -0
@@ -28,6 +28,8 @@ config KVM select HAVE_KVM_IRQCHIP select HAVE_KVM_EVENTFD select KVM_APIC_ARCHITECTURE + select KVM_ASYNC_PF + select SLOW_WORK select USER_RETURN_NOTIFIER select KVM_MMIO ---help---
arch/x86/kvm/mmu.c +34 -1
@@ -21,6 +21,7 @@#include "mmu.h" #include "x86.h" #include "kvm_cache_regs.h" +#include "x86.h" #include <linux/kvm_host.h> #include <linux/types.h>
@@ -2345,6 +2346,21 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, error_code & PFERR_WRITE_MASK, gfn); } +int kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn) +{ +struct kvm_arch_async_pf arch; + arch.token = (vcpu->arch.async_pf_id++ <<12 ) | vcpu->vcpu_id; +return kvm_setup_async_pf (vcpu, gva, gfn, &arch); +} + +static bool can_do_async_pf (struct kvm_vcpu *vcpu) +{ +if (!vcpu->arch.apf_data ||kvm_event_needs_reinjection (vcpu)) +return false; + +return !!kvm_x86_ops->get_cpl (vcpu); +} +static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code) {
@@ -2353,6 +2369,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,int level;gfn_t gfn = gpa >>PAGE_SHIFT ;unsigned long mmu_seq; +bool async;ASSERT (vcpu);ASSERT (VALID_PAGE (vcpu->arch.mmu.root_hpa));
@@ -2367,7 +2384,23 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); - pfn =gfn_to_pfn (vcpu->kvm, gfn); + +if (can_do_async_pf (vcpu)) { + pfn =gfn_to_pfn_async (vcpu->kvm, gfn, &async); +trace_kvm_try_async_get_page (async, pfn); + }else { +do_sync: + async = false; + pfn =gfn_to_pfn (vcpu->kvm, gfn); + } + +if (async) { +if (!kvm_arch_setup_async_pf (vcpu, gpa, gfn)) + goto do_sync; +return 0 ; + } + +/* mmio */ if (is_error_pfn (pfn))return kvm_handle_bad_page (vcpu->kvm, gfn, pfn);spin_lock (&vcpu->kvm->mmu_lock);
arch/x86/kvm/paging_tmpl.h +16 -1
@@ -428,6 +428,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr,pfn_t pfn;int level =PT_PAGE_TABLE_LEVEL ;unsigned long mmu_seq; +bool async;pgprintk ("%s: addr %lx err %x\n" , __func__, addr, error_code);kvm_mmu_audit (vcpu,"pre page fault" );
@@ -459,7 +460,21 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); - pfn =gfn_to_pfn (vcpu->kvm, walker.gfn); + +if (can_do_async_pf (vcpu)) { + pfn =gfn_to_pfn_async (vcpu->kvm, walker.gfn, &async); +trace_kvm_try_async_get_page (async, pfn); + }else { +do_sync: + async = false; + pfn =gfn_to_pfn (vcpu->kvm, walker.gfn); + } + +if (async) { +if (!kvm_arch_setup_async_pf (vcpu, addr, walker.gfn)) + goto do_sync; +return 0 ; + }/* mmio */ if (is_error_pfn (pfn))
arch/x86/kvm/x86.c +62 -1
112 lines not on this page · raw message
include/linux/kvm_host.h +31 -0
@@ -16,6 +16,7 @@#include <linux/mm.h> #include <linux/preempt.h> #include <linux/msi.h> +#include <linux/slow-work.h> #include <asm/signal.h> #include <linux/kvm.h>
@@ -73,6 +74,27 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx,int kvm_io_bus_unregister_dev (struct kvm *kvm,enum kvm_bus bus_idx,struct kvm_io_device *dev); +#ifdef CONFIG_KVM_ASYNC_PF +struct kvm_async_pf { +struct slow_work work; +struct list_head link; +struct list_head queue; +struct kvm_vcpu *vcpu; +struct mm_struct *mm; +gva_t gva; +unsigned long addr; +struct kvm_arch_async_pf arch; +struct page *page; +atomic_t used; +}; + +void kvm_clear_async_pf_completion_queue (struct kvm_vcpu *vcpu); +void kvm_check_async_pf_completion (struct kvm_vcpu *vcpu); +int kvm_setup_async_pf (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn, +struct kvm_arch_async_pf *arch); +int kvm_async_pf_wakeup_all (struct kvm_vcpu *vcpu); +#endif +struct kvm_vcpu {struct kvm *kvm;#ifdef CONFIG_PREEMPT_NOTIFIERS
@@ -103,6 +125,14 @@ struct kvm_vcpu {gpa_t mmio_phys_addr;#endif +#ifdef CONFIG_KVM_ASYNC_PF +u32 async_pf_queued; +struct list_head async_pf_queue; +struct list_head async_pf_done; +spinlock_t async_pf_lock; +struct kvm_async_pf *async_pf_work; +#endif +struct kvm_vcpu_arch arch; };
@@ -297,6 +327,7 @@ void kvm_release_page_dirty(struct page *page);void kvm_set_page_dirty (struct page *page);void kvm_set_page_accessed (struct page *page); +pfn_t gfn_to_pfn_async (struct kvm *kvm,gfn_t gfn,bool *async);pfn_t gfn_to_pfn (struct kvm *kvm,gfn_t gfn);pfn_t gfn_to_pfn_memslot (struct kvm *kvm,struct kvm_memory_slot *slot,gfn_t gfn);
include/trace/events/kvm.h +60 -0
66 lines not on this page · raw message
virt/kvm/Kconfig +3 -0
@@ -15,3 +15,6 @@ config KVM_APIC_ARCHITECTURE config KVM_MMIO bool + +config KVM_ASYNC_PF + bool
virt/kvm/kvm_main.c +257 -6
346 lines not on this page · raw message
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 08/12] Inject asynchronous page fault into a guest if page is swapped out.
Quoted 12 lines · Gleb Natapov
If guest access swapped out memory do not swap it in from vcpu thread context. Setup slow work to do swapping and send async page fault to a guest.
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.
struct kvm_arch {
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_stat { · line 451
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +2
-0@@ -444,6 +446,8 @@ struct kvm_vcpu_stat {u32 hypercalls;u32 irq_injections;u32 nmi_injections; +u32 apf_not_present; +u32 apf_present; };
Please don't add more stats, instead add tracepoints which can be converted to stats by userspace.
Would be good to have both guest and host tracepoints.
arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2354
Quoted 10 lines
arch/x86/kvm/mmu.c +6
-0@@ -2345,6 +2346,21 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, error_code& PFERR_WRITE_MASK, gfn); } +int kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn) +{ +struct kvm_arch_async_pf arch; + arch.token = (vcpu->arch.async_pf_id++<<12 ) | vcpu->vcpu_id; +return kvm_setup_async_pf (vcpu, gva, gfn,&arch); +}
Ok. so token is globally unique. We're limited to 4096 vcpus, I guess that's fine. Wraparound at 1M faults/vcpu, what's the impact? failure if we have 1M faulting processes on one vcpu?
I guess that's fine too.
arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2361
Quoted 7 lines
arch/x86/kvm/mmu.c +7
-0+ +static bool can_do_async_pf (struct kvm_vcpu *vcpu) +{ +if (!vcpu->arch.apf_data ||kvm_event_needs_reinjection (vcpu)) +return false; + +return !!kvm_x86_ops->get_cpl (vcpu);
!! !needed, bool autoconverts. But > 0 is more readable.
Quoted 24 lines
arch/x86/kvm/mmu.c +9
-1+} +static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code) {@@ -2353,6 +2369,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,int level;gfn_t gfn = gpa>>PAGE_SHIFT ;unsigned long mmu_seq; +bool async;ASSERT (vcpu);ASSERT (VALID_PAGE (vcpu->arch.mmu.root_hpa));@@ -2367,7 +2384,23 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); - pfn =gfn_to_pfn (vcpu->kvm, gfn); + +if (can_do_async_pf (vcpu)) { + pfn =gfn_to_pfn_async (vcpu->kvm, gfn,&async); +trace_kvm_try_async_get_page (async, pfn); + }else { +do_sync:
arch/x86/kvm/mmu.c static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, · line 2402
Quoted 10 lines
arch/x86/kvm/mmu.c +10
-0+ async = false; + pfn =gfn_to_pfn (vcpu->kvm, gfn); + } + +if (async) { +if (!kvm_arch_setup_async_pf (vcpu, gpa, gfn)) + goto do_sync; +return 0 ; + } +
This goto is pretty ugly. How about:
async = false;
if (can_do_async_pf(&async)) {
}
if (async && !setup())
async = false;
if (async)
...or something.
Quoted 11 lines
arch/x86/kvm/paging_tmpl.h +6
-1@@ -459,7 +460,21 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); - pfn =gfn_to_pfn (vcpu->kvm, walker.gfn); + +if (can_do_async_pf (vcpu)) { + pfn =gfn_to_pfn_async (vcpu->kvm, walker.gfn,&async); +trace_kvm_try_async_get_page (async, pfn); + }else { +do_sync:
arch/x86/kvm/paging_tmpl.h static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, · line 478
Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +9
-0+ async = false; + pfn =gfn_to_pfn (vcpu->kvm, walker.gfn); + } + +if (async) { +if (!kvm_arch_setup_async_pf (vcpu, addr, walker.gfn)) + goto do_sync; +return 0 ; + }
This repetition is ugly too.
Quoted 9 lines
arch/x86/kvm/x86.c +8
-0+static int apf_put_user (struct kvm_vcpu *vcpu,u32 val) +{ +if (unlikely (vcpu->arch.apf_memslot_ver != + vcpu->kvm->memslot_version)) { +u64 gpa = vcpu->arch.apf_msr_val& ~0x3f ; +unsigned long addr; +int offset =offset_in_page (gpa); +
arch/x86/kvm/x86.c void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 5763
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ addr =gfn_to_hva (vcpu->kvm, gpa>>PAGE_SHIFT ); + vcpu->arch.apf_data = (u32 __user *)(addr + offset); +if (kvm_is_error_hva (addr)) { + vcpu->arch.apf_data =NULL ; +return -EFAULT ; + } + } + +return put_user (val, vcpu->arch.apf_data); +}
This nice cache needs to be outside apf to reduce complexity for reviewers and since it is useful for others.
Would be good to have memslot-cached kvm_put_guest() and kvm_get_guest().
virt/kvm/kvm_main.c static atomic_t hardware_enable_failed; · line 84
Quoted 12 lines
virt/kvm/kvm_main.c +4
-0@@ -78,6 +78,11 @@ static atomic_t hardware_enable_failed;struct kmem_cache *kvm_vcpu_cache;EXPORT_SYMBOL_GPL (kvm_vcpu_cache); +#ifdef CONFIG_KVM_ASYNC_PF +#define ASYNC_PF_PER_VCPU 100 +static struct kmem_cache *async_pf_cache; +#endif
All those #ifdefs can be eliminated with virt/kvm/apf.[ch].
virt/kvm/kvm_main.c int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) · line 198
Quoted 13 lines
virt/kvm/kvm_main.c +6
-0+static __read_mostly struct preempt_ops kvm_preempt_ops;struct dentry *kvm_debugfs_dir;@@ -186,6 +191,11 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) vcpu->kvm = kvm; vcpu->vcpu_id = id;init_waitqueue_head (&vcpu->wq); +#ifdef CONFIG_KVM_ASYNC_PF +INIT_LIST_HEAD (&vcpu->async_pf_done); +INIT_LIST_HEAD (&vcpu->async_pf_queue); +spin_lock_init (&vcpu->async_pf_lock); +#endif
kvm_apf_init() etc.
virt/kvm/kvm_main.c void mark_page_dirty(struct kvm *kvm, gfn_t gfn) · line 1381
Quoted 6 lines
virt/kvm/kvm_main.c +6
-0+ struct kvm_arch_async_pf *arch) +{ +struct kvm_async_pf *work; + +if (vcpu->async_pf_queued>=ASYNC_PF_PER_VCPU ) +return 0 ;
100 == too high. At 16 vcpus, this allows 1600 kernel threads to wait for I/O.
Would have been best if we could ask for a page to be paged in asynchronously.
Quoted 40 lines
virt/kvm/kvm_main.c +40
-0+ +/* setup slow work */ + +/* do alloc nowait since if we are going to sleep anyway we +may as well sleep faulting in page */ + work =kmem_cache_zalloc (async_pf_cache,GFP_NOWAIT ); +if (!work) +return 0 ; + +atomic_set (&work->used,1 ); + work->page =NULL ; + work->vcpu = vcpu; + work->gva = gva; + work->addr =gfn_to_hva (vcpu->kvm, gfn); + work->arch = *arch; + work->mm = current->mm; +atomic_inc (&work->mm->mm_count); +kvm_get_kvm (work->vcpu->kvm); + +/* this can't really happen otherwise gfn_to_pfn_async +would succeed */ +if (unlikely (kvm_is_error_hva (work->addr))) + goto retry_sync; + +slow_work_init (&work->work,&async_pf_ops); +if (slow_work_enqueue (&work->work) !=0 ) + goto retry_sync; + + vcpu->async_pf_work = work; +list_add_tail (&work->queue,&vcpu->async_pf_queue); + vcpu->async_pf_queued++; +return 1 ; +retry_sync: +kvm_put_kvm (work->vcpu->kvm); +mmdrop (work->mm); +kmem_cache_free (async_pf_cache, work); +return 0 ; +} + +
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 08/12] Inject asynchronous page fault into a guest if page is swapped out.
Quoted 23 lines · Avi Kivity
Quoted 12 lines · Gleb Natapov
If guest access swapped out memory do not swap it in from vcpu thread context. Setup slow work to do swapping and send async page fault to a guest.
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.
struct
kvm_arch{arch/x86/include/asm/kvm_host.h struct kvm_vcpu_stat { · line 451
Quoted 6 lines
arch/x86/include/asm/kvm_host.h +2
-0@@ -444,6 +446,8 @@ struct kvm_vcpu_stat {u32 hypercalls;u32 irq_injections;u32 nmi_injections; +u32 apf_not_present; +u32 apf_present; };Please don't add more stats, instead add tracepoints which can be converted to stats by userspace.
Quoted 2 lines
Would be good to have both guest and host tracepoints.
I do have host tracepoints for all events. I still prefer to have also kvm stats since they are so much easier to use right now. We can delete them later when we replace kvm_stat with perf.
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -2345,6 +2346,21 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, error_code& PFERR_WRITE_MASK, gfn); }
Quoted 10 lines
arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2354
Quoted 6 lines
arch/x86/kvm/mmu.c +6
-0+int kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn) +{ +struct kvm_arch_async_pf arch; + arch.token = (vcpu->arch.async_pf_id++<<12 ) | vcpu->vcpu_id; +return kvm_setup_async_pf (vcpu, gva, gfn,&arch); +}Ok. so token is globally unique. We're limited to 4096 vcpus, I guess that's fine. Wraparound at 1M faults/vcpu, what's the impact? failure if we have 1M faulting processes on one vcpu?
1M faulting processes on one vcpu simultaneously. And we limit number of outstanding apfs anyway.
Quoted 12 lines
I guess that's fine too.
arch/x86/kvm/mmu.c static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, · line 2361
Quoted 7 lines
arch/x86/kvm/mmu.c +7
-0+ +static bool can_do_async_pf (struct kvm_vcpu *vcpu) +{ +if (!vcpu->arch.apf_data ||kvm_event_needs_reinjection (vcpu)) +return false; + +return !!kvm_x86_ops->get_cpl (vcpu);!! !needed, bool autoconverts. But > 0 is more readable.
OK.
Quoted 46 lines
Quoted 25 lines
arch/x86/kvm/mmu.c +9
-1+} +static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code) {@@ -2353,6 +2369,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa,int level;gfn_t gfn = gpa>>PAGE_SHIFT ;unsigned long mmu_seq; +bool async;ASSERT (vcpu);ASSERT (VALID_PAGE (vcpu->arch.mmu.root_hpa));@@ -2367,7 +2384,23 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); - pfn =gfn_to_pfn (vcpu->kvm, gfn); + +if (can_do_async_pf (vcpu)) { + pfn =gfn_to_pfn_async (vcpu->kvm, gfn,&async); +trace_kvm_try_async_get_page (async, pfn); + }else { +do_sync:arch/x86/kvm/mmu.c static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, · line 2402
Quoted 10 lines
arch/x86/kvm/mmu.c +10
-0+ async = false; + pfn =gfn_to_pfn (vcpu->kvm, gfn); + } + +if (async) { +if (!kvm_arch_setup_async_pf (vcpu, gpa, gfn)) + goto do_sync; +return 0 ; + } +This goto is pretty ugly. How about:
async = false; if (can_do_async_pf(&async)) { } if (async && !setup()) async = false; if (async) ...
Quoted 2 lines
or something.
Will try to re-factor.
Quoted 15 lines
Quoted 15 lines
arch/x86/kvm/paging_tmpl.h +10
-1@@ -459,7 +460,21 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); - pfn =gfn_to_pfn (vcpu->kvm, walker.gfn); + +if (can_do_async_pf (vcpu)) { + pfn =gfn_to_pfn_async (vcpu->kvm, walker.gfn,&async); +trace_kvm_try_async_get_page (async, pfn); + }else { +do_sync: + async = false; + pfn =gfn_to_pfn (vcpu->kvm, walker.gfn); + } +
Quoted 9 lines
arch/x86/kvm/paging_tmpl.h static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, · line 478
Quoted 6 lines
arch/x86/kvm/paging_tmpl.h +5
-0+if (async) { +if (!kvm_arch_setup_async_pf (vcpu, addr, walker.gfn)) + goto do_sync; +return 0 ; + }This repetition is ugly too.
Yeah. May be it can be moved to separate function and this will help to get rid of goto as well.
Quoted 23 lines
Quoted 9 lines
arch/x86/kvm/x86.c +8
-0+static int apf_put_user (struct kvm_vcpu *vcpu,u32 val) +{ +if (unlikely (vcpu->arch.apf_memslot_ver != + vcpu->kvm->memslot_version)) { +u64 gpa = vcpu->arch.apf_msr_val& ~0x3f ; +unsigned long addr; +int offset =offset_in_page (gpa); +arch/x86/kvm/x86.c void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) · line 5763
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ addr =gfn_to_hva (vcpu->kvm, gpa>>PAGE_SHIFT ); + vcpu->arch.apf_data = (u32 __user *)(addr + offset); +if (kvm_is_error_hva (addr)) { + vcpu->arch.apf_data =NULL ; +return -EFAULT ; + } + } + +return put_user (val, vcpu->arch.apf_data); +}This nice cache needs to be outside apf to reduce complexity for reviewers and since it is useful for others.
Quoted 1 line
Would be good to have memslot-cached
kvm_put_guest()andkvm_get_guest().
Will look into it.
Quoted 9 lines
Quoted 9 lines
virt/kvm/kvm_main.c +0
-0@@ -78,6 +78,11 @@ static atomic_t hardware_enable_failed;struct kmem_cache *kvm_vcpu_cache;EXPORT_SYMBOL_GPL (kvm_vcpu_cache);
Quoted 7 lines
virt/kvm/kvm_main.c static atomic_t hardware_enable_failed; · line 84
Quoted 4 lines
virt/kvm/kvm_main.c +4
-0+#ifdef CONFIG_KVM_ASYNC_PF +#define ASYNC_PF_PER_VCPU 100 +static struct kmem_cache *async_pf_cache; +#endif All those #ifdefs can be eliminated with virt/kvm/apf.[ch].
OK.
Quoted 16 lines
virt/kvm/kvm_main.c int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) · line 198
Quoted 13 lines
virt/kvm/kvm_main.c +6
-0+static __read_mostly struct preempt_ops kvm_preempt_ops;struct dentry *kvm_debugfs_dir;@@ -186,6 +191,11 @@ int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id) vcpu->kvm = kvm; vcpu->vcpu_id = id;init_waitqueue_head (&vcpu->wq); +#ifdef CONFIG_KVM_ASYNC_PF +INIT_LIST_HEAD (&vcpu->async_pf_done); +INIT_LIST_HEAD (&vcpu->async_pf_queue); +spin_lock_init (&vcpu->async_pf_lock); +#endif
kvm_apf_init()etc.
Quoted 9 lines
virt/kvm/kvm_main.c void mark_page_dirty(struct kvm *kvm, gfn_t gfn) · line 1381
Quoted 6 lines
virt/kvm/kvm_main.c +6
-0+ struct kvm_arch_async_pf *arch) +{ +struct kvm_async_pf *work; + +if (vcpu->async_pf_queued>=ASYNC_PF_PER_VCPU ) +return 0 ;100 == too high. At 16 vcpus, this allows 1600 kernel threads to wait for I/O.
Number of kernel threads are limited by other means. Slow work subsystem has its own knobs to tune that. Here we limit how much slow work items can be queued per vcpu.
Quoted 4 lines
Would have been best if we could ask for a page to be paged in asynchronously.
You mean to have core kernel facility for that? I agree it would be nice, but much harder.
Quoted 32 lines
Quoted 32 lines
virt/kvm/kvm_main.c +32
-0+ +/* setup slow work */ + +/* do alloc nowait since if we are going to sleep anyway we +may as well sleep faulting in page */ + work =kmem_cache_zalloc (async_pf_cache,GFP_NOWAIT ); +if (!work) +return 0 ; + +atomic_set (&work->used,1 ); + work->page =NULL ; + work->vcpu = vcpu; + work->gva = gva; + work->addr =gfn_to_hva (vcpu->kvm, gfn); + work->arch = *arch; + work->mm = current->mm; +atomic_inc (&work->mm->mm_count); +kvm_get_kvm (work->vcpu->kvm); + +/* this can't really happen otherwise gfn_to_pfn_async +would succeed */ +if (unlikely (kvm_is_error_hva (work->addr))) + goto retry_sync; + +slow_work_init (&work->work,&async_pf_ops); +if (slow_work_enqueue (&work->work) !=0 ) + goto retry_sync; + + vcpu->async_pf_work = work; +list_add_tail (&work->queue,&vcpu->async_pf_queue); + vcpu->async_pf_queued++; +return 1 ;
Quoted 11 lines
Quoted 8 lines
virt/kvm/kvm_main.c +8
-0+retry_sync: +kvm_put_kvm (work->vcpu->kvm); +mmdrop (work->mm); +kmem_cache_free (async_pf_cache, work); +return 0 ; +} + +Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 08/12] Inject asynchronous page fault into a guest if page is swapped out.
Quoted 5 lines · Gleb Natapov
Quoted 4 lines
This nice cache needs to be outside apf to reduce complexity for reviewers and since it is useful for others.
Would be good to have memslot-cached
kvm_put_guest()andkvm_get_guest().Will look into it.
In the meantime, you can just drop the caching.
Quoted 11 lines
Quoted 8 lines
virt/kvm/kvm_main.c void mark_page_dirty(struct kvm *kvm, gfn_t gfn) · line 1381
Quoted 6 lines
virt/kvm/kvm_main.c +6
-0+ struct kvm_arch_async_pf *arch) +{ +struct kvm_async_pf *work; + +if (vcpu->async_pf_queued>=ASYNC_PF_PER_VCPU ) +return 0 ;100 == too high. At 16 vcpus, this allows 1600 kernel threads to wait for I/O.
Number of kernel threads are limited by other means. Slow work subsystem has its own knobs to tune that. Here we limit how much slow work items can be queued per vcpu.
OK.
Quoted 5 lines
Quoted 3 lines
Would have been best if we could ask for a page to be paged in asynchronously.
You mean to have core kernel facility for that? I agree it would be nice, but much harder.
Yes, that's what I meant.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 08/12] Inject asynchronous page fault into a guest if page is swapped out.
In reply to Avi KivityQuoted 23 lines · Avi Kivity
Quoted 9 lines
(quoted diff) +8
-0+static int apf_put_user(struct kvm_vcpu *vcpu, u32 val) +{ + if (unlikely(vcpu->arch.apf_memslot_ver != + vcpu->kvm->memslot_version)) { + u64 gpa = vcpu->arch.apf_msr_val& ~0x3f; + unsigned long addr; + int offset = offset_in_page(gpa); +Review
Quoted 10 lines
(quoted diff) +10
-0+ addr = gfn_to_hva(vcpu->kvm, gpa>> PAGE_SHIFT); + vcpu->arch.apf_data = (u32 __user *)(addr + offset); + if (kvm_is_error_hva(addr)) { + vcpu->arch.apf_data = NULL; + return -EFAULT; + } + } + + return put_user(val, vcpu->arch.apf_data); +}This nice cache needs to be outside apf to reduce complexity for reviewers and since it is useful for others.
Quoted 2 lines
Would be good to have memslot-cached
kvm_put_guest()andkvm_get_guest().
Something like this? (only compile tested)
include/linux/kvm_host.h +12 -0
@@ -168,10 +168,18 @@ struct kvm_irq_routing_table {};struct kvm_memslots {int nmemslots; +u32 generation;struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS +KVM_PRIVATE_MEM_SLOTS ]; }; +struct gfn_to_hva_cache { +u32 generation; +gpa_t gpa; +unsigned long hva; +struct kvm_memory_slot *memslot; +}; +struct kvm {spinlock_t mmu_lock;raw_spinlock_t requests_lock;
@@ -315,12 +323,16 @@ int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, int offset, int len);int kvm_write_guest (struct kvm *kvm,gpa_t gpa,const void *data,unsigned long len); +int kvm_write_guest_cached (struct kvm *kvm,struct gfn_to_hva_cache *ghc, +void *data,unsigned long len);int kvm_clear_guest_page (struct kvm *kvm,gfn_t gfn,int offset,int len);int kvm_clear_guest (struct kvm *kvm,gpa_t gpa,unsigned long len);struct kvm_memory_slot *gfn_to_memslot (struct kvm *kvm,gfn_t gfn);int kvm_is_visible_gfn (struct kvm *kvm,gfn_t gfn);unsigned long kvm_host_page_size (struct kvm *kvm,gfn_t gfn);void mark_page_dirty (struct kvm *kvm,gfn_t gfn); +void mark_page_dirty_in_slot (struct kvm *kvm,struct kvm_memory_slot *memslot, +gfn_t gfn);void kvm_vcpu_block (struct kvm_vcpu *vcpu);void kvm_vcpu_on_spin (struct kvm_vcpu *vcpu);
virt/kvm/kvm_main.c +42 -4
77 lines not on this page · raw message
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 08/12] Inject asynchronous page fault into a guest if page is swapped out.
Quoted 22 lines · Gleb Natapov
Quoted 21 lines · Avi Kivity
Quoted 8 lines
(quoted diff) +8
-0+static int apf_put_user(struct kvm_vcpu *vcpu, u32 val) +{ + if (unlikely(vcpu->arch.apf_memslot_ver != + vcpu->kvm->memslot_version)) { + u64 gpa = vcpu->arch.apf_msr_val& ~0x3f; + unsigned long addr; + int offset = offset_in_page(gpa); +Review
Quoted 10 lines
(quoted diff) +10
-0+ addr = gfn_to_hva(vcpu->kvm, gpa>> PAGE_SHIFT); + vcpu->arch.apf_data = (u32 __user *)(addr + offset); + if (kvm_is_error_hva(addr)) { + vcpu->arch.apf_data = NULL; + return -EFAULT; + } + } + + return put_user(val, vcpu->arch.apf_data); +}This nice cache needs to be outside apf to reduce complexity for reviewers and since it is useful for others.
Quoted 3 lines
Quoted 2 lines
Would be good to have memslot-cached
kvm_put_guest()andkvm_get_guest().Something like this? (only compile tested)
Yes, exactly.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 9/12 Retry fault before vmentry
In reply to Gleb NatapovWhen page is swapped in it is mapped into guest memory only after guest tries to access it again and generate another fault. To save this fault we can map it immediately since we know that guest is going to access the page.
--- arch/x86/include/asm/kvm_host.h | 7 +++++- arch/x86/kvm/mmu.c | 27 +++++++++++++++++++------ arch/x86/kvm/paging_tmpl.h | 40 +++++++++++++++++++++++++++++++++++--- arch/x86/kvm/x86.c | 9 ++++++++ virt/kvm/kvm_main.c | 2 + 5 files changed, 73 insertions(+), 12 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -1
@@ -236,7 +236,8 @@ struct kvm_pio_request {*/ struct kvm_mmu {void (*new_cr3)(struct kvm_vcpu *vcpu); -int (*page_fault)(struct kvm_vcpu *vcpu,gva_t gva,u32 err); +int (*page_fault)(struct kvm_vcpu *vcpu,gva_t gva,u32 err,bool sync); +int (*page_fault_other_cr3)(struct kvm_vcpu *vcpu,gpa_t cr3,gva_t gva,u32 err);void (*free)(struct kvm_vcpu *vcpu);gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,gva_t gva,u32 access,u32 *error);
@@ -534,6 +535,8 @@ struct kvm_x86_ops {struct kvm_arch_async_pf {u32 token; +gpa_t cr3; +u32 error_code; };extern struct kvm_x86_ops *kvm_x86_ops;
@@ -777,6 +780,8 @@ void kvm_arch_inject_async_page_not_present(struct kvm_vcpu *vcpu, struct kvm_async_pf *work);void kvm_arch_inject_async_page_present (struct kvm_vcpu *vcpu,struct kvm_async_pf *work); +void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu, +struct kvm_async_pf *work);bool kvm_arch_can_inject_async_page_present (struct kvm_vcpu *vcpu);#endif /* _ASM_X86_KVM_HOST_H */
arch/x86/kvm/mmu.c +20 -7
@@ -2327,7 +2327,7 @@ static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, gva_t vaddr, }static int nonpaging_page_fault (struct kvm_vcpu *vcpu,gva_t gva, -u32 error_code) +u32 error_code,bool sync) {gfn_t gfn;int r;
@@ -2346,10 +2346,13 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva, error_code & PFERR_WRITE_MASK, gfn); } -int kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gva_t gva,gfn_t gfn) +int kvm_arch_setup_async_pf (struct kvm_vcpu *vcpu,gpa_t cr3,gva_t gva, +gfn_t gfn,u32 error_code) {struct kvm_arch_async_pf arch; arch.token = (vcpu->arch.async_pf_id++ <<12 ) | vcpu->vcpu_id; + arch.cr3 = cr3; + arch.error_code = error_code;return kvm_setup_async_pf (vcpu, gva, gfn, &arch); }
@@ -2361,8 +2364,8 @@ static bool can_do_async_pf(struct kvm_vcpu *vcpu)return !!kvm_x86_ops->get_cpl (vcpu); } -static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa, -u32 error_code) +static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code, +bool sync) {pfn_t pfn;int r;
@@ -2385,7 +2388,7 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (can_do_async_pf (vcpu)) { +if (!sync &&can_do_async_pf (vcpu)) { pfn =gfn_to_pfn_async (vcpu->kvm, gfn, &async);trace_kvm_try_async_get_page (async, pfn); }else {
@@ -2395,7 +2398,8 @@ do_sync: }if (async) { -if (!kvm_arch_setup_async_pf (vcpu, gpa, gfn)) +if (!kvm_arch_setup_async_pf (vcpu, vcpu->arch.cr3, gpa, gfn, + error_code)) goto do_sync;return 0 ; }
@@ -2419,6 +2423,12 @@ out_unlock:return 0 ; } +static int tdp_page_fault_sync (struct kvm_vcpu *vcpu,gpa_t cr3,gva_t gpa, +u32 error_code) +{ +return tdp_page_fault (vcpu, gpa, error_code, true); +} +static void nonpaging_free (struct kvm_vcpu *vcpu) {mmu_free_roots (vcpu);
@@ -2549,6 +2559,7 @@ static int paging64_init_context_common(struct kvm_vcpu *vcpu, int level)ASSERT (is_pae (vcpu)); context->new_cr3 = paging_new_cr3; context->page_fault = paging64_page_fault; + context->page_fault_other_cr3 = paging64_page_fault_other_cr3; context->gva_to_gpa = paging64_gva_to_gpa; context->prefetch_page = paging64_prefetch_page; context->sync_page = paging64_sync_page;
@@ -2573,6 +2584,7 @@ static int paging32_init_context(struct kvm_vcpu *vcpu)reset_rsvds_bits_mask (vcpu,PT32_ROOT_LEVEL ); context->new_cr3 = paging_new_cr3; context->page_fault = paging32_page_fault; + context->page_fault_other_cr3 = paging32_page_fault_other_cr3; context->gva_to_gpa = paging32_gva_to_gpa; context->free = paging_free; context->prefetch_page = paging32_prefetch_page;
@@ -2596,6 +2608,7 @@ static int init_kvm_tdp_mmu(struct kvm_vcpu *vcpu) context->new_cr3 = nonpaging_new_cr3; context->page_fault = tdp_page_fault; + context->page_fault_other_cr3 = tdp_page_fault_sync; context->free = nonpaging_free; context->prefetch_page = nonpaging_prefetch_page; context->sync_page = nonpaging_sync_page;
@@ -2983,7 +2996,7 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u32 error_code)int r;enum emulation_result er; - r = vcpu->arch.mmu.page_fault (vcpu, cr2, error_code); + r = vcpu->arch.mmu.page_fault (vcpu, cr2, error_code, false);if (r <0 ) goto out;
arch/x86/kvm/paging_tmpl.h +36 -4
@@ -415,8 +415,8 @@ out_gpte_changed:* Returns: 1 if we need to emulate the instruction, 0 otherwise, or * a negative value on error. */ -static int FNAME (page_fault )(struct kvm_vcpu *vcpu,gva_t addr, -u32 error_code) +static int FNAME (page_fault )(struct kvm_vcpu *vcpu,gva_t addr,u32 error_code, +bool sync) {int write_fault = error_code &PFERR_WRITE_MASK ;int user_fault = error_code &PFERR_USER_MASK ;
@@ -461,7 +461,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (can_do_async_pf (vcpu)) { +if (!sync &&can_do_async_pf (vcpu)) { pfn =gfn_to_pfn_async (vcpu->kvm, walker.gfn, &async);trace_kvm_try_async_get_page (async, pfn); }else {
@@ -471,7 +471,8 @@ do_sync: }if (async) { -if (!kvm_arch_setup_async_pf (vcpu, addr, walker.gfn)) +if (!kvm_arch_setup_async_pf (vcpu, vcpu->arch.cr3, addr, + walker.gfn, error_code)) goto do_sync;return 0 ; }
@@ -505,6 +506,37 @@ out_unlock:return 0 ; } +static int FNAME (page_fault_other_cr3 )(struct kvm_vcpu *vcpu,gpa_t cr3, +gva_t addr,u32 error_code) +{ +int r =0 ; +gpa_t curr_cr3 = vcpu->arch.cr3; + +if (curr_cr3 != cr3) { +/* +* We do page fault on behalf of a process that is sleeping +* because of async PF. PV guest takes reference to mm that cr3 +* belongs too, so it has to be valid here. +*/ +kvm_set_cr3 (vcpu, cr3); +if (kvm_mmu_reload (vcpu)) + goto switch_cr3; + } + + r =FNAME (page_fault)(vcpu, addr, error_code, true); + +if (kvm_check_request (KVM_REQ_MMU_SYNC , vcpu)) +kvm_mmu_sync_roots (vcpu); + +switch_cr3: +if (curr_cr3 != vcpu->arch.cr3) { +kvm_set_cr3 (vcpu, curr_cr3); +kvm_mmu_reload (vcpu); + } + +return r; +} +static void FNAME (invlpg )(struct kvm_vcpu *vcpu,gva_t gva) {struct kvm_shadow_walk_iterator iterator;
arch/x86/kvm/x86.c +9 -0
@@ -5743,6 +5743,15 @@ void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) }EXPORT_SYMBOL_GPL (kvm_set_rflags); +void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu, +struct kvm_async_pf *work) +{ +if (!vcpu->arch.mmu.page_fault_other_cr3 ||is_error_page (work->page)) +return ; + vcpu->arch.mmu.page_fault_other_cr3 (vcpu, work->arch.cr3, work->gva, + work->arch.error_code); +} +static int apf_put_user (struct kvm_vcpu *vcpu,u32 val) { if (unlikely(vcpu->arch.apf_memslot_ver !=
virt/kvm/kvm_main.c +2 -0
@@ -1348,6 +1348,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)spin_lock (&vcpu->async_pf_lock);list_del (&work->link);spin_unlock (&vcpu->async_pf_lock); +kvm_arch_async_page_ready (vcpu, work);put_page (work->page);async_pf_work_free (work);list_del (&work->queue);
@@ -1366,6 +1367,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)list_del (&work->queue); vcpu->async_pf_queued--; +kvm_arch_async_page_ready (vcpu, work);kvm_arch_inject_async_page_present (vcpu, work);put_page (work->page);
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 09/12] Retry fault before vmentry
arch/x86/kvm/mmu.c static bool can_do_async_pf(struct kvm_vcpu *vcpu) · line 2368
Quoted 11 lines · Gleb Natapov
When page is swapped in it is mapped into guest memory only after guest tries to access it again and generate another fault. To save this fault we can map it immediately since we know that guest is going to access the page.
arch/x86/kvm/mmu.c +2
-2-static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa, -u32 error_code) +static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code, +bool sync)
'sync' means something else in the shadow mmu. Please rename to something longer, maybe 'apf_completion'.
Alternatively, split to two functions, a base function that doesn't do apf and a wrapper that handles apf.
Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +6
-0@@ -505,6 +506,37 @@ out_unlock:return 0 ; } +static int FNAME (page_fault_other_cr3 )(struct kvm_vcpu *vcpu,gpa_t cr3, +gva_t addr,u32 error_code) +{ +int r =0 ; +gpa_t curr_cr3 = vcpu->arch.cr3; +
arch/x86/kvm/paging_tmpl.h out_unlock: · line 524
Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +10
-0+if (curr_cr3 != cr3) { +/* +* We do page fault on behalf of a process that is sleeping +* because of async PF. PV guest takes reference to mm that cr3 +* belongs too, so it has to be valid here. +*/ +kvm_set_cr3 (vcpu, cr3); +if (kvm_mmu_reload (vcpu)) + goto switch_cr3; + }
With nested virtualization, we need to switch cr0, cr4, and efer as well...
arch/x86/kvm/paging_tmpl.h out_unlock: · line 529
Quoted 5 lines
arch/x86/kvm/paging_tmpl.h +5
-0+ + r =FNAME (page_fault)(vcpu, addr, error_code, true); + +if (kvm_check_request (KVM_REQ_MMU_SYNC , vcpu)) +kvm_mmu_sync_roots (vcpu);
Why is this needed?
arch/x86/kvm/paging_tmpl.h out_unlock: · line 538
Quoted 9 lines
arch/x86/kvm/paging_tmpl.h +9
-0+ +switch_cr3: +if (curr_cr3 != vcpu->arch.cr3) { +kvm_set_cr3 (vcpu, curr_cr3); +kvm_mmu_reload (vcpu); + } + +return r; +}
This has the nasty effect of flushing the TLB on AMD.
Quoted 44 lines
arch/x86/kvm/paging_tmpl.h +1
-0+static void FNAME (invlpg )(struct kvm_vcpu *vcpu,gva_t gva) {struct kvm_shadow_walk_iterator iterator;arch/x86/kvm/x86.c +9
-0@@ -5743,6 +5743,15 @@ void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) }EXPORT_SYMBOL_GPL (kvm_set_rflags); +void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu, +struct kvm_async_pf *work) +{ +if (!vcpu->arch.mmu.page_fault_other_cr3 ||is_error_page (work->page)) +return ; + vcpu->arch.mmu.page_fault_other_cr3 (vcpu, work->arch.cr3, work->gva, + work->arch.error_code); +} +static int apf_put_user (struct kvm_vcpu *vcpu,u32 val) { if (unlikely(vcpu->arch.apf_memslot_ver !=virt/kvm/kvm_main.c +2
-0@@ -1348,6 +1348,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)spin_lock (&vcpu->async_pf_lock);list_del (&work->link);spin_unlock (&vcpu->async_pf_lock); +kvm_arch_async_page_ready (vcpu, work);put_page (work->page);async_pf_work_free (work);list_del (&work->queue);@@ -1366,6 +1367,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)list_del (&work->queue); vcpu->async_pf_queued--; +kvm_arch_async_page_ready (vcpu, work);kvm_arch_inject_async_page_present (vcpu, work);put_page (work->page);
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 09/12] Retry fault before vmentry
Quoted 16 lines · Avi Kivity
arch/x86/kvm/mmu.c static bool can_do_async_pf(struct kvm_vcpu *vcpu) · line 2368
Quoted 11 lines · Gleb Natapov
When page is swapped in it is mapped into guest memory only after guest tries to access it again and generate another fault. To save this fault we can map it immediately since we know that guest is going to access the page.
arch/x86/kvm/mmu.c +2
-2-static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa, -u32 error_code) +static int tdp_page_fault (struct kvm_vcpu *vcpu,gva_t gpa,u32 error_code, +bool sync)'sync' means something else in the shadow mmu. Please rename to something longer, maybe '
apf_completion'.
Quoted 3 lines
Alternatively, split to two functions, a base function that doesn't do apf and a wrapper that handles apf.
Will rename to something else.
Quoted 14 lines
Quoted 14 lines
arch/x86/kvm/paging_tmpl.h +10
-0@@ -505,6 +506,37 @@ out_unlock:return 0 ; } +static int FNAME (page_fault_other_cr3 )(struct kvm_vcpu *vcpu,gpa_t cr3, +gva_t addr,u32 error_code) +{ +int r =0 ; +gpa_t curr_cr3 = vcpu->arch.cr3; + +if (curr_cr3 != cr3) { +/* +* We do page fault on behalf of a process that is sleeping +* because of async PF. PV guest takes reference to mm that cr3
Quoted 9 lines
arch/x86/kvm/paging_tmpl.h out_unlock: · line 524
Quoted 6 lines
arch/x86/kvm/paging_tmpl.h +6
-0+* belongs too, so it has to be valid here. +*/ +kvm_set_cr3 (vcpu, cr3); +if (kvm_mmu_reload (vcpu)) + goto switch_cr3; + }With nested virtualization, we need to switch cr0, cr4, and efer as well...
On SVM or VMX or both?
Quoted 8 lines
arch/x86/kvm/paging_tmpl.h out_unlock: · line 529
Quoted 5 lines
arch/x86/kvm/paging_tmpl.h +5
-0+ + r =FNAME (page_fault)(vcpu, addr, error_code, true); + +if (kvm_check_request (KVM_REQ_MMU_SYNC , vcpu)) +kvm_mmu_sync_roots (vcpu);Why is this needed?
http://www.mail-archive.com/[email protected]/msg37827.html
KVM_REQ_MMU_SYNC request generated here must be processed before switching to a different cr3 (otherwise vcpu_enter_guest will process it with the wrong cr3 in place).
Quoted 12 lines
arch/x86/kvm/paging_tmpl.h out_unlock: · line 538
Quoted 9 lines
arch/x86/kvm/paging_tmpl.h +9
-0+ +switch_cr3: +if (curr_cr3 != vcpu->arch.cr3) { +kvm_set_cr3 (vcpu, curr_cr3); +kvm_mmu_reload (vcpu); + } + +return r; +}This has the nasty effect of flushing the TLB on AMD.
What is more expansive reenter the guest and handle one more fault, or flash TLB here?
Quoted 40 lines
Quoted 40 lines
arch/x86/kvm/paging_tmpl.h +1
-0+static void FNAME (invlpg )(struct kvm_vcpu *vcpu,gva_t gva) {struct kvm_shadow_walk_iterator iterator;arch/x86/kvm/x86.c +9
-0@@ -5743,6 +5743,15 @@ void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) }EXPORT_SYMBOL_GPL (kvm_set_rflags); +void kvm_arch_async_page_ready (struct kvm_vcpu *vcpu, +struct kvm_async_pf *work) +{ +if (!vcpu->arch.mmu.page_fault_other_cr3 ||is_error_page (work->page)) +return ; + vcpu->arch.mmu.page_fault_other_cr3 (vcpu, work->arch.cr3, work->gva, + work->arch.error_code); +} +static int apf_put_user (struct kvm_vcpu *vcpu,u32 val) { if (unlikely(vcpu->arch.apf_memslot_ver !=virt/kvm/kvm_main.c +1
-0@@ -1348,6 +1348,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)spin_lock (&vcpu->async_pf_lock);list_del (&work->link);spin_unlock (&vcpu->async_pf_lock); +kvm_arch_async_page_ready (vcpu, work);put_page (work->page);async_pf_work_free (work);list_del (&work->queue);@@ -1366,6 +1367,7 @@ void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)list_del (&work->queue); vcpu->async_pf_queued--;
Quoted 8 lines
Quoted 4 lines
virt/kvm/kvm_main.c +1
-0+kvm_arch_async_page_ready (vcpu, work);kvm_arch_inject_async_page_present (vcpu, work);put_page (work->page);Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 09/12] Retry fault before vmentry
Quoted 24 lines · Gleb Natapov
Quoted 15 lines
Quoted 15 lines
arch/x86/kvm/paging_tmpl.h +9
-0@@ -505,6 +506,37 @@ out_unlock:return 0 ; } +static int FNAME (page_fault_other_cr3 )(struct kvm_vcpu *vcpu,gpa_t cr3, +gva_t addr,u32 error_code) +{ +int r =0 ; +gpa_t curr_cr3 = vcpu->arch.cr3; + +if (curr_cr3 != cr3) { +/* +* We do page fault on behalf of a process that is sleeping Quoted 9 lines
arch/x86/kvm/paging_tmpl.h out_unlock: · line 524
Quoted 7 lines
arch/x86/kvm/paging_tmpl.h +7
-0+* because of async PF. PV guest takes reference to mm that cr3 +* belongs too, so it has to be valid here. +*/ +kvm_set_cr3 (vcpu, cr3); +if (kvm_mmu_reload (vcpu)) + goto switch_cr3; + }With nested virtualization, we need to switch cr0, cr4, and efer as well...
Quoted 1 line
On SVM or VMX or both?
Both. Let's defer this patch since it's an optimization, this is really complicated.
Quoted 7 lines
Quoted 7 lines
arch/x86/kvm/paging_tmpl.h out_unlock: · line 529
Quoted 5 lines
arch/x86/kvm/paging_tmpl.h +5
-0+ + r =FNAME (page_fault)(vcpu, addr, error_code, true); + +if (kvm_check_request (KVM_REQ_MMU_SYNC , vcpu)) +kvm_mmu_sync_roots (vcpu);Why is this needed?
Quoted 5 lines
http://www.mail-archive.com/[email protected]/msg37827.html
KVM_REQ_MMU_SYNCrequest generated here must be processed before switching to a different cr3 (otherwisevcpu_enter_guestwill process it with the wrong cr3 in place).
Ah, it should be part of the cr3 switch block above.
Quoted 11 lines
Quoted 11 lines
arch/x86/kvm/paging_tmpl.h out_unlock: · line 538
Quoted 9 lines
arch/x86/kvm/paging_tmpl.h +9
-0+ +switch_cr3: +if (curr_cr3 != vcpu->arch.cr3) { +kvm_set_cr3 (vcpu, curr_cr3); +kvm_mmu_reload (vcpu); + } + +return r; +}This has the nasty effect of flushing the TLB on AMD.
Quoted 2 lines
What is more expansive reenter the guest and handle one more fault, or flash TLB here?
No idea. Probably the reentry. On Intel the tlb is flushed anyway.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 10/12 Handle async PF in non preemptable context
In reply to Gleb NatapovIf async page fault is received by idle task or when preemp_count is not zero guest cannot reschedule, so do sti; hlt and wait for page to be ready. vcpu can still process interrupts while it waits for the page to be ready.
--- arch/x86/kernel/kvm.c | 36 ++++++++++++++++++++++++++++++++---- 1 files changed, 32 insertions(+), 4 deletions(-)
arch/x86/kernel/kvm.c +32 -4
@@ -37,6 +37,7 @@#include <asm/cpu.h> #include <asm/traps.h> #include <asm/desc.h> +#include <asm/tlbflush.h> #define MMU_QUEUE_SIZE 1024
@@ -68,6 +69,8 @@ struct kvm_task_sleep_node {wait_queue_head_t wq;u32 token;int cpu; +bool halted; +struct mm_struct *mm; }; static struct kvm_task_sleep_head {
@@ -96,6 +99,11 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)struct kvm_task_sleep_head *b = &async_pf_sleepers[key];struct kvm_task_sleep_node n, *e;DEFINE_WAIT (wait); +int cpu, idle; + + cpu =get_cpu (); + idle =idle_cpu (cpu); +put_cpu ();spin_lock (&b->lock); e =_find_apf_task (b, token);
@@ -109,17 +117,31 @@ static void apf_task_wait(struct task_struct *tsk, u32 token) n.token = token; n.cpu =smp_processor_id (); + n.mm = current->active_mm; + n.halted = idle ||preempt_count () >1 ; +atomic_inc (&n.mm->mm_count);init_waitqueue_head (&n.wq);hlist_add_head (&n.link, &b->list);spin_unlock (&b->lock);for (;;) { -prepare_to_wait (&n.wq, &wait,TASK_UNINTERRUPTIBLE ); +if (!n.halted) +prepare_to_wait (&n.wq, &wait,TASK_UNINTERRUPTIBLE );if (hlist_unhashed (&n.link))break ; -schedule (); + +if (!n.halted) { +schedule (); + }else { +/* +* We cannot reschedule. So halt. +*/ +native_safe_halt (); +local_irq_disable (); + } } -finish_wait (&n.wq, &wait); +if (!n.halted) +finish_wait (&n.wq, &wait);return ; }
@@ -127,7 +149,12 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)static void apf_task_wake_one (struct kvm_task_sleep_node *n) {hlist_del_init (&n->link); -if (waitqueue_active (&n->wq)) +if (!n->mm) +return ; +mmdrop (n->mm); +if (n->halted) +smp_send_reschedule (n->cpu); +else if (waitqueue_active (&n->wq))wake_up (&n->wq); }
@@ -157,6 +184,7 @@ again: } n->token = token; n->cpu =smp_processor_id (); + n->mm =NULL ;init_waitqueue_head (&n->wq);hlist_add_head (&n->link, &b->list); }else
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 10/12] Handle async PF in non preemptable context
Quoted 59 lines · Gleb Natapov
If async page fault is received by idle task or when
preemp_countis not zero guest cannot reschedule, so do sti; hlt and wait for page to be ready. vcpu can still process interrupts while it waits for the page to be ready.Acked-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kernel/kvm.c | 36 ++++++++++++++++++++++++++++++++---- 1 files changed, 32 insertions(+), 4 deletions(-)arch/x86/kernel/kvm.c +13
-1@@ -37,6 +37,7 @@#include <asm/cpu.h> #include <asm/traps.h> #include <asm/desc.h> +#include <asm/tlbflush.h> #define MMU_QUEUE_SIZE 1024@@ -68,6 +69,8 @@ struct kvm_task_sleep_node {wait_queue_head_t wq;u32 token;int cpu; +bool halted; +struct mm_struct *mm; }; static struct kvm_task_sleep_head {@@ -96,6 +99,11 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)struct kvm_task_sleep_head *b =&async_pf_sleepers[key];struct kvm_task_sleep_node n, *e;DEFINE_WAIT (wait); +int cpu, idle; + + cpu =get_cpu (); + idle =idle_cpu (cpu); +put_cpu ();spin_lock (&b->lock); e =_find_apf_task (b, token);@@ -109,17 +117,31 @@ static void apf_task_wait(struct task_struct *tsk, u32 token) n.token = token; n.cpu =smp_processor_id (); + n.mm = current->active_mm; + n.halted = idle ||preempt_count ()>1 ; +atomic_inc (&n.mm->mm_count);init_waitqueue_head (&n.wq);hlist_add_head (&n.link,&b->list);spin_unlock (&b->lock);for (;;) { -prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE ); +if (!n.halted) +prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE );
arch/x86/kernel/kvm.c static void apf_task_wait(struct task_struct *tsk, u32 token) · line 138
Quoted 10 lines
arch/x86/kernel/kvm.c +7
-1if (hlist_unhashed (&n.link))break ; -schedule (); + +if (!n.halted) { +schedule (); + }else { +/* +* We cannot reschedule. So halt. +*/
If we get the wakeup here, we'll halt and never wake up again.
arch/x86/kernel/kvm.c static void apf_task_wait(struct task_struct *tsk, u32 token) · line 140
Quoted 2 lines
arch/x86/kernel/kvm.c +2
-0+native_safe_halt (); +local_irq_disable ();
So we need a local_irq_disable() before the hlish_unhashed() check.
Quoted 30 lines
arch/x86/kernel/kvm.c +10
-2+ } } -finish_wait (&n.wq,&wait); +if (!n.halted) +finish_wait (&n.wq,&wait);return ; }@@ -127,7 +149,12 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)static void apf_task_wake_one (struct kvm_task_sleep_node *n) {hlist_del_init (&n->link); -if (waitqueue_active (&n->wq)) +if (!n->mm) +return ; +mmdrop (n->mm); +if (n->halted) +smp_send_reschedule (n->cpu); +else if (waitqueue_active (&n->wq))wake_up (&n->wq); }@@ -157,6 +184,7 @@ again: } n->token = token; n->cpu =smp_processor_id (); + n->mm =NULL ;init_waitqueue_head (&n->wq);hlist_add_head (&n->link,&b->list); }else
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 10/12] Handle async PF in non preemptable context
Quoted 56 lines · Avi Kivity
Quoted 55 lines · Gleb Natapov
If async page fault is received by idle task or when
preemp_countis not zero guest cannot reschedule, so do sti; hlt and wait for page to be ready. vcpu can still process interrupts while it waits for the page to be ready.Acked-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kernel/kvm.c | 36 ++++++++++++++++++++++++++++++++---- 1 files changed, 32 insertions(+), 4 deletions(-)arch/x86/kernel/kvm.c +11
-0@@ -37,6 +37,7 @@#include <asm/cpu.h> #include <asm/traps.h> #include <asm/desc.h> +#include <asm/tlbflush.h> #define MMU_QUEUE_SIZE 1024@@ -68,6 +69,8 @@ struct kvm_task_sleep_node {wait_queue_head_t wq;u32 token;int cpu; +bool halted; +struct mm_struct *mm; }; static struct kvm_task_sleep_head {@@ -96,6 +99,11 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)struct kvm_task_sleep_head *b =&async_pf_sleepers[key];struct kvm_task_sleep_node n, *e;DEFINE_WAIT (wait); +int cpu, idle; + + cpu =get_cpu (); + idle =idle_cpu (cpu); +put_cpu ();spin_lock (&b->lock); e =_find_apf_task (b, token);@@ -109,17 +117,31 @@ static void apf_task_wait(struct task_struct *tsk, u32 token) n.token = token; n.cpu =smp_processor_id (); + n.mm = current->active_mm; + n.halted = idle ||preempt_count ()>1 ; +atomic_inc (&n.mm->mm_count);init_waitqueue_head (&n.wq);hlist_add_head (&n.link,&b->list);spin_unlock (&b->lock);
Quoted 17 lines
Quoted 4 lines
arch/x86/kernel/kvm.c +2
-1for (;;) { -prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE ); +if (!n.halted) +prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE );arch/x86/kernel/kvm.c static void apf_task_wait(struct task_struct *tsk, u32 token) · line 138
Quoted 10 lines
arch/x86/kernel/kvm.c +7
-1if (hlist_unhashed (&n.link))break ; -schedule (); + +if (!n.halted) { +schedule (); + }else { +/* +* We cannot reschedule. So halt. +*/ If we get the wakeup here, we'll halt and never wake up again.
We will not. IRQs are disabled here. native_safe_halt() enables them.
Quoted 4 lines
arch/x86/kernel/kvm.c static void apf_task_wait(struct task_struct *tsk, u32 token) · line 140
Quoted 2 lines
arch/x86/kernel/kvm.c +2
-0+native_safe_halt (); +local_irq_disable ();So we need a
local_irq_disable()before thehlish_unhashed()check.
We are still in exception handler, so IRQ should be off.
Quoted 23 lines
Quoted 23 lines
arch/x86/kernel/kvm.c +9
-2+ } } -finish_wait (&n.wq,&wait); +if (!n.halted) +finish_wait (&n.wq,&wait);return ; }@@ -127,7 +149,12 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)static void apf_task_wake_one (struct kvm_task_sleep_node *n) {hlist_del_init (&n->link); -if (waitqueue_active (&n->wq)) +if (!n->mm) +return ; +mmdrop (n->mm); +if (n->halted) +smp_send_reschedule (n->cpu); +else if (waitqueue_active (&n->wq))wake_up (&n->wq); }
Quoted 12 lines
Quoted 8 lines
arch/x86/kernel/kvm.c +1
-0@@ -157,6 +184,7 @@ again: } n->token = token; n->cpu =smp_processor_id (); + n->mm =NULL ;init_waitqueue_head (&n->wq);hlist_add_head (&n->link,&b->list); }else Signature · 1 line
error compiling committee.c: too many arguments to function
--
Gleb.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 10/12] Handle async PF in non preemptable context
Quoted 73 lines · Gleb Natapov
Quoted 56 lines · Avi Kivity
Quoted 55 lines · Gleb Natapov
If async page fault is received by idle task or when
preemp_countis not zero guest cannot reschedule, so do sti; hlt and wait for page to be ready. vcpu can still process interrupts while it waits for the page to be ready.Acked-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kernel/kvm.c | 36 ++++++++++++++++++++++++++++++++---- 1 files changed, 32 insertions(+), 4 deletions(-)arch/x86/kernel/kvm.c +11
-0@@ -37,6 +37,7 @@#include <asm/cpu.h> #include <asm/traps.h> #include <asm/desc.h> +#include <asm/tlbflush.h> #define MMU_QUEUE_SIZE 1024@@ -68,6 +69,8 @@ struct kvm_task_sleep_node {wait_queue_head_t wq;u32 token;int cpu; +bool halted; +struct mm_struct *mm; }; static struct kvm_task_sleep_head {@@ -96,6 +99,11 @@ static void apf_task_wait(struct task_struct *tsk, u32 token)struct kvm_task_sleep_head *b =&async_pf_sleepers[key];struct kvm_task_sleep_node n, *e;DEFINE_WAIT (wait); +int cpu, idle; + + cpu =get_cpu (); + idle =idle_cpu (cpu); +put_cpu ();spin_lock (&b->lock); e =_find_apf_task (b, token);@@ -109,17 +117,31 @@ static void apf_task_wait(struct task_struct *tsk, u32 token) n.token = token; n.cpu =smp_processor_id (); + n.mm = current->active_mm; + n.halted = idle ||preempt_count ()>1 ; +atomic_inc (&n.mm->mm_count);init_waitqueue_head (&n.wq);hlist_add_head (&n.link,&b->list);spin_unlock (&b->lock);Quoted 16 lines
Quoted 4 lines
arch/x86/kernel/kvm.c +2
-1for (;;) { -prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE ); +if (!n.halted) +prepare_to_wait (&n.wq,&wait,TASK_UNINTERRUPTIBLE );arch/x86/kernel/kvm.c static void apf_task_wait(struct task_struct *tsk, u32 token) · line 138
Quoted 10 lines
arch/x86/kernel/kvm.c +7
-1if (hlist_unhashed (&n.link))break ; -schedule (); + +if (!n.halted) { +schedule (); + }else { +/* +* We cannot reschedule. So halt. +*/ If we get the wakeup here, we'll halt and never wake up again.
Quoted 1 line
We will not. IRQs are disabled here.
native_safe_halt()enables them.
Ok.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 11/12 Let host know whether the guest can handle async PF in non-userspace context.
In reply to Gleb NatapovIf guest can detect that it runs in non-preemptable context it can handle async PFs at any time, so let host know that it can send async PF even if guest cpu is not in userspace.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/kvm_para.h | 1 + arch/x86/kernel/kvm.c | 3 +++ arch/x86/kvm/x86.c | 5 +++-- 4 files changed, 8 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -367,6 +367,7 @@ struct kvm_vcpu_arch {cpumask_var_t wbinvd_dirty_mask;u32 __user *apf_data; +bool apf_send_user_only;u32 apf_memslot_ver;u64 apf_msr_val;u32 async_pf_id;
arch/x86/include/asm/kvm_para.h +1 -0
@@ -38,6 +38,7 @@#define KVM_MAX_MMU_OP_BATCH 32#define KVM_ASYNC_PF_ENABLED (1 << 0) +#define KVM_ASYNC_PF_SEND_ALWAYS (1 << 1)/* Operations for KVM_HC_MMU_OP */ #define KVM_MMU_OP_WRITE_PTE 1
arch/x86/kernel/kvm.c +3 -0
@@ -429,6 +429,9 @@ void __cpuinit kvm_guest_cpu_init(void)if (kvm_para_has_feature (KVM_FEATURE_ASYNC_PF )) {u64 pa =__pa (&__get_cpu_var (apf_reason)); +#ifdef CONFIG_PREEMPT + pa |=KVM_ASYNC_PF_SEND_ALWAYS ; +#endif if (native_write_msr_safe (MSR_KVM_ASYNC_PF_EN , pa |KVM_ASYNC_PF_ENABLED , pa >>32 ))return ;
arch/x86/kvm/x86.c +3 -2
@@ -1222,8 +1222,8 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)int offset =offset_in_page (gpa);unsigned long addr; -/* Bits 1:5 are resrved, Should be zero */ -if (data &0x3e ) +/* Bits 2:5 are resrved, Should be zero */ +if (data &0x3c )return 1 ; vcpu->arch.apf_msr_val = data;
@@ -1246,6 +1246,7 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)return 1 ; } vcpu->arch.apf_memslot_ver = vcpu->kvm->memslot_version; + vcpu->arch.apf_send_user_only = !(data &KVM_ASYNC_PF_SEND_ALWAYS );kvm_async_pf_wakeup_all (vcpu);return 0 ; }
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 11/12] Let host know whether the guest can handle async PF in non-userspace context.
Quoted 18 lines · Gleb Natapov
If guest can detect that it runs in non-preemptable context it can handle async PFs at any time, so let host know that it can send async PF even if guest cpu is not in userspace.
Acked-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/asm/kvm_para.h | 1 + arch/x86/kernel/kvm.c | 3 +++ arch/x86/kvm/x86.c | 5 +++-- 4 files changed, 8 insertions(+), 2 deletions(-)arch/x86/include/asm/kvm_host.h +0
-0
arch/x86/include/asm/kvm_host.h struct kvm_vcpu_arch { · line 373
Quoted 7 lines
arch/x86/include/asm/kvm_host.h +1
-0@@ -367,6 +367,7 @@ struct kvm_vcpu_arch {cpumask_var_t wbinvd_dirty_mask;u32 __user *apf_data; +bool apf_send_user_only;u32 apf_memslot_ver;u64 apf_msr_val;u32 async_pf_id;
Lots of apf stuff in here. Make it apg.data etc.?
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
PATCH v5 12/12 Send async PF when guest is not in userspace too.
In reply to Gleb NatapovIf guest indicates that it can handle async pf in kernel mode too send it, but only if interrupt are enabled.
--- arch/x86/kvm/mmu.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
arch/x86/kvm/mmu.c +7 -1
@@ -2361,7 +2361,13 @@ static bool can_do_async_pf(struct kvm_vcpu *vcpu)if (!vcpu->arch.apf_data ||kvm_event_needs_reinjection (vcpu))return false; -return !!kvm_x86_ops->get_cpl (vcpu); +if (vcpu->arch.apf_send_user_only) +return !!kvm_x86_ops->get_cpl (vcpu); + +if (!kvm_x86_ops->interrupt_allowed (vcpu)) +return false; + +return true; } static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
Signature · 7 lines
1.7.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH v5 12/12] Send async PF when guest is not in userspace too.
Quoted 14 lines · Gleb Natapov
If guest indicates that it can handle async pf in kernel mode too send it, but only if interrupt are enabled.
Reviewed-by Rik van Riel<[email protected]>Signed-off-by Gleb Natapov<[email protected]>--- arch/x86/kvm/mmu.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)arch/x86/kvm/mmu.c +0
-0
arch/x86/kvm/mmu.c static bool can_do_async_pf(struct kvm_vcpu *vcpu) · line 2365
Quoted 6 lines
arch/x86/kvm/mmu.c +2
-1@@ -2361,7 +2361,13 @@ static bool can_do_async_pf(struct kvm_vcpu *vcpu)if (!vcpu->arch.apf_data ||kvm_event_needs_reinjection (vcpu))return false; -return !!kvm_x86_ops->get_cpl (vcpu); +if (vcpu->arch.apf_send_user_only) +return !!kvm_x86_ops->get_cpl (vcpu);
cpl is not a bool. Compare it with 0.
arch/x86/kvm/mmu.c static bool can_do_async_pf(struct kvm_vcpu *vcpu) · line 2371
Quoted 6 lines
arch/x86/kvm/mmu.c +5
-0+ +if (!kvm_x86_ops->interrupt_allowed (vcpu)) +return false; + +return true; }
Should have commented before, but get_cpl() is not accurate when doing nested virtualization. When L1 intercepts page faults, being in L2 is equivalent to CPL 3. But we need to get the apf information to L1 somehow.
Signature · 7 lines
error compiling committee.c: too many arguments to function -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>