kvm-s390: asnychronous page faults
PATCH 1/2 kvm: nowait retry for asynchronous page faults
In reply to Carsten OtteFrom: Martin Schwidefsky <[email protected]>
Add FAULT_FLAG_RETRY_NOWAIT to the fault flags if the host is processing a guest fault. In case of a fault retry exit sie64a() with the gmap_fault indication set. This makes it possible to handle asynchronous page faults without the need for mm notifiers.
---
arch/s390/include/asm/pgtable.h | 1 + arch/s390/include/asm/processor.h | 1 + arch/s390/mm/fault.c | 28 +++++++++++++++++++++++----- arch/s390/mm/pgtable.c | 7 +++++++ 4 files changed, 32 insertions(+), 5 deletions(-)
arch/s390/include/asm/pgtable.h +1 -0
@@ -688,6 +688,7 @@ int gmap_unmap_segment(struct gmap *gmapunsigned long __gmap_fault (unsigned long address,struct gmap *);unsigned long gmap_fault (unsigned long address,struct gmap *);void gmap_discard (unsigned long from,unsigned long to,struct gmap *); +int gmap_pfault (struct gmap *gmap);/* * Certain architectures need to do special things when PTEs
arch/s390/include/asm/processor.h +1 -0
@@ -83,6 +83,7 @@ struct thread_struct {unsigned long prot_addr;/* address of protection-excep. */ unsigned int trap_no;unsigned long gmap_addr;/* address of last gmap fault. */ +unsigned int gmap_pfault;/* bool to indicate a guest pfault */ struct per_regs per_user;/* User specified PER registers */ struct per_event per_event;/* Cause of the last PER trap */ /* pfault_wait is used to block the process on a pfault event */
arch/s390/mm/fault.c +23 -5
@@ -52,6 +52,7 @@#define VM_FAULT_BADCONTEXT 0x010000#define VM_FAULT_BADMAP 0x020000#define VM_FAULT_BADACCESS 0x040000 +#define VM_FAULT_PFAULT 0x080000static unsigned long store_indication;
@@ -242,6 +243,7 @@ static noinline void do_fault_error(strureturn ; }case VM_FAULT_BADCONTEXT : +case VM_FAULT_PFAULT :do_no_context (regs, int_code, trans_exc_code);break ;default :/* fault & VM_FAULT_ERROR */
@@ -276,6 +278,9 @@ static noinline void do_fault_error(strustatic inline int do_exception (struct pt_regs *regs,int access,unsigned long trans_exc_code) { +#ifdef CONFIG_PGSTE +struct gmap *gmap; +#endif struct task_struct *tsk;struct mm_struct *mm;struct vm_area_struct *vma;
@@ -306,9 +311,10 @@ static inline int do_exception(struct ptdown_read (&mm->mmap_sem);#ifdef CONFIG_PGSTE -if (test_tsk_thread_flag (current,TIF_SIE ) && S390_lowcore.gmap) { - address =__gmap_fault (address, - (struct gmap *) S390_lowcore.gmap); + gmap = (struct gmap *) + (test_thread_flag (TIF_SIE ) ? S390_lowcore.gmap :0 ); +if (gmap) { + address =__gmap_fault (address, gmap);if (address == -EFAULT ) { fault =VM_FAULT_BADMAP ; goto out_up;
@@ -317,6 +323,7 @@ static inline int do_exception(struct pt fault =VM_FAULT_OOM ; goto out_up; } + flags |=FAULT_FLAG_RETRY_NOWAIT ; }#endif
@@ -368,10 +375,21 @@ retry: regs, address); }if (fault &VM_FAULT_RETRY ) { +#ifdef CONFIG_PGSTE +if (gmap) { +/* FAULT_FLAG_RETRY_NOWAIT has been set, +* mmap_sem has not been released. */ +if (gmap_pfault (gmap)) { + fault =VM_FAULT_PFAULT ; + goto out_up; + } + } else +#endif +down_read (&mm->mmap_sem);/* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk * of starvation. */ - flags &= ~FAULT_FLAG_ALLOW_RETRY ; -down_read (&mm->mmap_sem); + flags &= ~(FAULT_FLAG_ALLOW_RETRY | +FAULT_FLAG_RETRY_NOWAIT ); goto retry; } }
arch/s390/mm/pgtable.c +7 -0
@@ -398,6 +398,7 @@ unsigned long __gmap_fault(unsigned longpmd_t *pmd; current->thread.gmap_addr = address; + current->thread.gmap_pfault =0 ; mm = gmap->mm;/* Walk the gmap address space page table */ table = gmap->table + ((address >>53 ) &0x7ff );
@@ -514,6 +515,12 @@ void gmap_discard(unsigned long from, un }EXPORT_SYMBOL_GPL (gmap_discard); +int gmap_pfault (struct gmap *gmap) +{ + current->thread.gmap_pfault =1 ; +return 1 ; +} +void gmap_unmap_notifier (struct mm_struct *mm,unsigned long *table) {struct gmap_rmap *rmap, *next;
cover kvm-s390: asnychronous page faults
Hi Avi,
these patches implement asynchronous page faults for kvm on s390. Sorry for being slower on sending patches than you're reviewing, I guess these will answer your question on __gmap_fault ;-). I think we should push both of these via Martin, his patch alone would break kvm on s390, mine alone won't compile. Could you please ack these so that we queue them for 3.3?
thanks,
Carsten
PATCH 2/2 kvm-s390: pseudo page fault support
From: Carsten Otte <[email protected]>
This patch adds support for pseudo page faults. The corresponding interface is implemented according to the documentation in CP programming services.
Diagnose 258 allows to register compare and check masks for pseudo page faults, and the guest can cancel these masks again. For both operations, like everywhere else in KVM on z, access register mode is not supported (ALET is assumed to be 0).
In case a major fault is recognized for a virtual machine, the page fault path triggers IO and kvm_s390_handle_pfault is called in order to determine if the fault can be handled asynchronously. In case the fault condition qualifies for asynchronous treatment, the guest is notified. Otherwise the vcpu thread synchronously waits for the page to become available prior to reentry into SIE.
One kernel thread per virtual machine gets notified for all asynchronous page fault events for its VM. Subsequently it waits for the page to be faulted in by calling fault_in_user_pages, and it notifies the guest that the page fault operation has completed.
---
arch/s390/include/asm/kvm_host.h | 8 + arch/s390/kvm/Makefile | 2 arch/s390/kvm/diag.c | 3 arch/s390/kvm/interrupt.c | 109 ++++++++++++++--- arch/s390/kvm/kvm-s390.c | 20 ++- arch/s390/kvm/kvm-s390.h | 9 + arch/s390/kvm/pfault.c | 247 +++++++++++++++++++++++++++++++++++++++ include/linux/kvm.h | 2 8 files changed, 380 insertions(+), 20 deletions(-)
arch/s390/include/asm/kvm_host.h +8 -0
@@ -240,6 +240,10 @@ struct kvm_vcpu_arch {u64 stidp_data; };struct gmap *gmap; +#define KVM_S390_PFAULT_TOKEN_INVALID (-1UL) +unsigned long pfault_token; +unsigned long pfault_select; +unsigned long pfault_compare; };struct kvm_vm_stat {
@@ -251,6 +255,10 @@ struct kvm_arch{debug_info_t *dbf;struct kvm_s390_float_interrupt float_int;struct gmap *gmap; +struct task_struct *pfault_thread; +spinlock_t pfault_list_lock; +struct list_head pfault_list; +wait_queue_head_t pfault_wait; };extern int sie64a (struct kvm_s390_sie_block *,unsigned long *);
arch/s390/kvm/diag.c +3 -0
@@ -14,6 +14,7 @@#include <linux/kvm.h> #include <linux/kvm_host.h> #include "kvm-s390.h" +#include "gaccess.h" static int diag_release_pages (struct kvm_vcpu *vcpu) {
@@ -89,6 +90,8 @@ int kvm_s390_handle_diag(struct kvm_vcpureturn diag_release_pages (vcpu);case 0x44 :return __diag_time_slice_end (vcpu); +case 0x258 : +return kvm_s390_diag_page_reference_service (vcpu);case 0x308 :return __diag_ipl_functions (vcpu);default :
arch/s390/kvm/interrupt.c +94 -15
160 lines not on this page · raw message
arch/s390/kvm/kvm-s390.c +16 -4
@@ -201,6 +201,8 @@ int kvm_arch_init_vm(struct kvm *kvm)if (!kvm->arch.gmap) goto out_nogmap; +kvm_s390_pfault_init (kvm); +return 0 ; out_nogmap:debug_unregister (kvm->arch.dbf);
@@ -213,6 +215,7 @@ out_err:void kvm_arch_vcpu_destroy (struct kvm_vcpu *vcpu) {VCPU_EVENT (vcpu,3 ,"%s" ,"free cpu" ); +kvm_s390_pfault_clear_cpu (vcpu);clear_bit (63 - vcpu->vcpu_id, (unsigned long *) &vcpu->kvm->arch.sca->mcn);if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda == (__u64 ) vcpu->arch.sie_block)
@@ -249,6 +252,7 @@ void kvm_arch_destroy_vm(struct kvm *kvmfree_page ((unsigned long )(kvm->arch.sca));debug_unregister (kvm->arch.dbf);gmap_free (kvm->arch.gmap); +kvm_s390_pfault_exit (kvm); }/* Section: vcpu related */
@@ -356,6 +360,7 @@ struct kvm_vcpu *kvm_arch_vcpu_create(stinit_waitqueue_head (&vcpu->arch.local_int.wq); vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;spin_unlock (&kvm->arch.float_int.lock); + vcpu->arch.pfault_token =KVM_S390_PFAULT_TOKEN_INVALID ; rc =kvm_vcpu_init (vcpu, kvm, id);if (rc)
@@ -468,6 +473,8 @@ int kvm_arch_vcpu_ioctl_set_mpstate(strustatic void __vcpu_run (struct kvm_vcpu *vcpu) { +int rc; +memcpy (&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14 ],16 );if (need_resched ())
@@ -484,12 +491,17 @@ static void __vcpu_run(struct kvm_vcpu *local_irq_enable ();VCPU_EVENT (vcpu,6 ,"entering sie flags %x" ,atomic_read (&vcpu->arch.sie_block->cpuflags)); -if (sie64a (vcpu->arch.sie_block, vcpu->arch.guest_gprs)) { -VCPU_EVENT (vcpu,3 ,"%s" ,"fault in sie instruction" ); -kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING ); - } + rc =sie64a (vcpu->arch.sie_block, vcpu->arch.guest_gprs);VCPU_EVENT (vcpu,6 ,"exit sie icptcode %d" , vcpu->arch.sie_block->icptcode); +if (rc) { +if (current->thread.gmap_pfault) { +kvm_s390_handle_pfault (vcpu); + }else { +VCPU_EVENT (vcpu,3 ,"%s" ,"fault in sie instruction" ); +kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING ); + } + }local_irq_disable ();kvm_guest_exit ();local_irq_enable ();
arch/s390/kvm/kvm-s390.h +9 -0
@@ -53,10 +53,13 @@ void kvm_s390_tasklet(unsigned long parmvoid kvm_s390_deliver_pending_interrupts (struct kvm_vcpu *vcpu);int kvm_s390_inject_vm (struct kvm *kvm,struct kvm_s390_interrupt *s390int); +void __kvm_s390_inject_vcpu (struct kvm_s390_local_interrupt *, +struct kvm_s390_interrupt_info *);int kvm_s390_inject_vcpu (struct kvm_vcpu *vcpu,struct kvm_s390_interrupt *s390int);int kvm_s390_inject_program_int (struct kvm_vcpu *vcpu,u16 code);int kvm_s390_inject_sigp_stop (struct kvm_vcpu *vcpu,int action); +int kvm_s390_should_pfault (struct kvm_vcpu *vcpu);/* implemented in priv.c */ int kvm_s390_handle_b2 (struct kvm_vcpu *vcpu);
@@ -71,4 +74,10 @@ int kvm_s390_vcpu_store_status(struct kv/* implemented in diag.c */ int kvm_s390_handle_diag (struct kvm_vcpu *vcpu); +/* implemented in pfault.c */ +int kvm_s390_diag_page_reference_service (struct kvm_vcpu *vcpu); +void kvm_s390_handle_pfault (struct kvm_vcpu *vcpu); +void kvm_s390_pfault_init (struct kvm *kvm); +void kvm_s390_pfault_exit (struct kvm *kvm); +void kvm_s390_pfault_clear_cpu (struct kvm_vcpu *vcpu);#endif
arch/s390/kvm/Makefile +1 -1
@@ -10,5 +10,5 @@ common-objs = $(addprefix ../../../virt/ ccflags-y := -Ivirt/kvm -Iarch/s390/kvm -kvm-objs := $(common-objs) kvm-s390.o intercept.o interrupt.o priv.o sigp.o diag.o +kvm-objs := $(common-objs) kvm-s390.o intercept.o interrupt.o priv.o sigp.o diag.o pfault.o obj-$(CONFIG_KVM) += kvm.o
arch/s390/kvm/pfault.c +247 -0
247 lines not on this page · raw message
include/linux/kvm.h +2 -0
@@ -372,6 +372,8 @@ struct kvm_s390_psw {#define KVM_S390_INT_SERVICE 0xffff2401u#define KVM_S390_INT_EMERGENCY 0xffff1201u#define KVM_S390_INT_EXTERNAL_CALL 0xffff1202u +#define KVM_S390_INT_PFAULT_INIT 0xfffe0004u +#define KVM_S390_INT_PFAULT_DONE 0xfffe0005ustruct kvm_s390_interrupt {__u32 type;
Re: [patch 2/2] [PATCH] kvm-s390: pseudo page fault support
Quoted 23 lines · Carsten Otte
From: Carsten Otte <[email protected]>
This patch adds support for pseudo page faults. The corresponding interface is implemented according to the documentation in CP programming services.
Diagnose 258 allows to register compare and check masks for pseudo page faults, and the guest can cancel these masks again. For both operations, like everywhere else in KVM on z, access register mode is not supported (ALET is assumed to be 0).
In case a major fault is recognized for a virtual machine, the page fault path triggers IO andkvm_s390_handle_pfaultis called in order to determine if the fault can be handled asynchronously. In case the fault condition qualifies for asynchronous treatment, the guest is notified. Otherwise the vcpu thread synchronously waits for the page to become available prior to reentry into SIE.
One kernel thread per virtual machine gets notified for all asynchronous page fault events for its VM. Subsequently it waits for the page to be faulted in by callingfault_in_user_pages, and it notifies the guest that the page fault operation has completed.arch/s390/kvm/pfault.c +3
-0+static void kvm_s390_pfault_sync (struct kvm_vcpu *vcpu) +{ + unsigned long uaddr = gmap_fault(current->thread.gmap_addr,
arch/s390/kvm/pfault.c line 94
Quoted 10 lines
arch/s390/kvm/pfault.c +10
-0+ vcpu->arch.gmap); + +if (IS_ERR_VALUE (uaddr)) +return ; + +VCPU_EVENT (vcpu,5 ,"synchronous page fault at guest %lx user %lx" , + current->thread.gmap_addr, uaddr); + +fault_in_pages_readable ((char __user *)uaddr,PAGE_SIZE ); +}
These may make sense as tracepoints (this is what x86 does). The kvm_stat script knows how to pick them up and generate an event histogram dynamically, along with all the other goodies tracepoints bring.
Quoted 9 lines
arch/s390/kvm/pfault.c +9
-0+ +static void kvm_s390_pfault_async (struct kvm_vcpu *vcpu) +{ +unsigned long uaddr =gmap_fault (current->thread.gmap_addr, + vcpu->arch.gmap); +struct pfault_event *event; +struct kvm_s390_interrupt_info *init, *done; +unsigned long pfault_token; +
arch/s390/kvm/pfault.c line 113
Quoted 10 lines
arch/s390/kvm/pfault.c +10
-0+if (IS_ERR_VALUE (uaddr)) +return ; + +if (!kvm_s390_should_pfault (vcpu)) { +kvm_s390_pfault_sync (vcpu); +return ; + } + +copy_from_guest (vcpu, &pfault_token, vcpu->arch.pfault_token, +8 );
Missing error check?
arch/s390/kvm/pfault.c line 125
Quoted 12 lines
arch/s390/kvm/pfault.c +12
-0+ + init =kzalloc (sizeof (*init),GFP_ATOMIC ); +if (!init) +return ; + + done =kzalloc (sizeof (*done),GFP_ATOMIC ); +if (!done) + goto out_init; + + event =kzalloc (sizeof (*event),GFP_ATOMIC ); +if (!event) + goto out_done;
Three allocs? Maybe combine them? Even if their lifetimes are not exactly the same.
Quoted 76 lines
arch/s390/kvm/pfault.c +76
-0+ + init->type =KVM_S390_INT_PFAULT_INIT ; + init->ext.ext_params2 = pfault_token; + + done->type =KVM_S390_INT_PFAULT_DONE ; + done->ext.ext_params2 = pfault_token; + + event->inti = done; + event->uaddr = uaddr; + event->local_int = &vcpu->arch.local_int; + +VCPU_EVENT (vcpu,5 , +"initiating pfault for token %lx at guest %lx user %lx" , + pfault_token, current->thread.gmap_addr, uaddr); + +__kvm_s390_inject_vcpu (&vcpu->arch.local_int, init); + +spin_lock_bh (&vcpu->kvm->arch.pfault_list_lock); +list_add_tail (&event->pfault_list_element, + &vcpu->kvm->arch.pfault_list); +wake_up (&vcpu->kvm->arch.pfault_wait); +spin_unlock_bh (&vcpu->kvm->arch.pfault_list_lock); +return ; + +out_done: +kfree (done); +out_init: +kfree (init); +} + +void kvm_s390_handle_pfault (struct kvm_vcpu *vcpu) +{ +unsigned long mask; + +if (vcpu->arch.pfault_token ==KVM_S390_PFAULT_TOKEN_INVALID ) + goto synchronous; + + mask = vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select; + +if (mask != vcpu->arch.pfault_compare) + goto synchronous; + +kvm_s390_pfault_async (vcpu); +return ; +synchronous: +kvm_s390_pfault_sync (vcpu); +} + +static int pfault_thread_fn (void *data) +{ +struct pfault_event *event, *n; +struct pfault_event *dequeued; +struct kvm *kvm = (struct kvm *)data; +wait_queue_t wait; + +init_waitqueue_entry (&wait, current); +add_wait_queue (&kvm->arch.pfault_wait, &wait); +while (1 ) { +spin_lock_bh (&kvm->arch.pfault_list_lock); + current->state =TASK_INTERRUPTIBLE ; + dequeued =NULL ; +list_for_each_entry_safe (event, n, &kvm->arch.pfault_list, + pfault_list_element) { +if (!dequeued) { +list_del_init (&event->pfault_list_element); + dequeued = event; + } + } +spin_unlock_bh (&kvm->arch.pfault_list_lock); +if (kthread_should_stop ()) { + current->state =TASK_RUNNING ; +remove_wait_queue (&kvm->arch.pfault_wait, &wait); +return 0 ; + } +if (dequeued) { + current->state =TASK_RUNNING ;
arch/s390/kvm/pfault.c line 211
Quoted 10 lines
arch/s390/kvm/pfault.c +10
-0+fault_in_pages_readable ((char __user *)dequeued->uaddr, +PAGE_SIZE ); +__kvm_s390_inject_vcpu (dequeued->local_int, + dequeued->inti); +kfree (dequeued); + }else { +schedule (); + } + } +}
Is this duplicating virt/kvm/async_pf.c?
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [patch 2/2] [PATCH] kvm-s390: pseudo page fault support
Quoted 4 lines · Avi Kivity
Review
Quoted 2 lines
(quoted diff) +2
-0+ copy_from_guest(vcpu,&pfault_token, vcpu->arch.pfault_token, + 8);Missing error check?
Good catch, will fix.
Quoted 6 lines
Quoted 6 lines
arch/s390/kvm/pfault.c +6
-0+ init =kzalloc (sizeof (*init),GFP_ATOMIC ); +if (!init) +return ; + + done =kzalloc (sizeof (*done),GFP_ATOMIC ); +if (!done)
Quoted 8 lines
arch/s390/kvm/pfault.c line 125
Quoted 5 lines
arch/s390/kvm/pfault.c +5
-0+ goto out_init; + + event =kzalloc (sizeof (*event),GFP_ATOMIC ); +if (!event) + goto out_done;Three allocs? Maybe combine them? Even if their lifetimes are not exactly the same.
The interrupt stack frees the interrupt info types for done and init in context of the target vcpu thread when delivering. Thus, as far as I can see, these cannot be easily converged.
Quoted 1 line
Is this duplicating
virt/kvm/async_pf.c?
We intend to be able to backport this to 2.6.32 for RHEL6. async_pf.c
relies on asynchonous work items which is not available on RHEL6. Thus
we decided to go forward with our own implementation first, and move towards an aproach similar to async_pf.c as soon as we find time for it. I think in the end both pathes can and should be converged. The
implementation in async_pf.c clearly has advantages over this approach.
Re: [patch 2/2] [PATCH] kvm-s390: pseudo page fault support
Quoted 15 lines · Carsten Otte
Quoted 15 lines
arch/s390/kvm/pfault.c line 125
Quoted 12 lines
arch/s390/kvm/pfault.c +11
-0+ init =kzalloc (sizeof (*init),GFP_ATOMIC ); +if (!init) +return ; + + done =kzalloc (sizeof (*done),GFP_ATOMIC ); +if (!done) + goto out_init; + + event =kzalloc (sizeof (*event),GFP_ATOMIC ); +if (!event) + goto out_done;Three allocs? Maybe combine them? Even if their lifetimes are not exactly the same.
Quoted 3 lines
The interrupt stack frees the interrupt info types for done and init in context of the target vcpu thread when delivering. Thus, as far as I can see, these cannot be easily converged.
I mean, if delivery is always the last thing that happens, just do a single free there.
Quoted 7 lines
Quoted 1 line
Is this duplicating
virt/kvm/async_pf.c?We intend to be able to backport this to 2.6.32 for RHEL6.
async_pf.crelies on asynchonous work items which is not available on RHEL6. Thus we decided to go forward with our own implementation first, and move towards an aproach similar toasync_pf.cas soon as we find time for it. I think in the end both pathes can and should be converged. The implementation inasync_pf.cclearly has advantages over this approach.
Backports to distros, even if you happened to name my favourite, do not override upstream considerations. "as soon as we find time" doesn't inspire confidence either. If async_pf.c is better, let's do that (btw async_pf.c did not find itself in generic code by accident).
(for the backport, I imagine it will be possible to backport async_pf.c)
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [patch 2/2] [PATCH] kvm-s390: pseudo page fault support
Quoted 4 lines · Avi Kivity
Backports to distros, even if you happened to name my favourite, do not override upstream considerations. "as soon as we find time" doesn't inspire confidence either. If
async_pf.cis better, let's do that (btwasync_pf.cdid not find itself in generic code by accident).
That makes sense to me.
Re: [patch 2/2] [PATCH] kvm-s390: pseudo page fault support
In reply to Avi KivityQuoted 9 lines
arch/s390/kvm/pfault.c line 94
Quoted 5 lines
arch/s390/kvm/pfault.c +5
-0+VCPU_EVENT (vcpu,5 ,"synchronous page fault at guest %lx user %lx" , + current->thread.gmap_addr, uaddr); + +fault_in_pages_readable ((char __user *)uaddr,PAGE_SIZE ); +}These may make sense as tracepoints (this is what x86 does). The
kvm_statscript knows how to pick them up and generate an event histogram dynamically, along with all the other goodies tracepoints bring.
Coverting these to trace points is on my todo list.