diff options
author | kfraser@localhost.localdomain <kfraser@localhost.localdomain> | 2007-04-03 11:44:10 +0100 |
---|---|---|
committer | kfraser@localhost.localdomain <kfraser@localhost.localdomain> | 2007-04-03 11:44:10 +0100 |
commit | a9344ed5826043957f11d480e0cf48ec4bdc73b8 (patch) | |
tree | 0626f6f329ff0cba3be28a1ffd8fcbd57b5a2ca7 | |
parent | 9123bf1da67844083f2fc514f09b27eb61fda716 (diff) | |
download | xen-a9344ed5826043957f11d480e0cf48ec4bdc73b8.tar.gz xen-a9344ed5826043957f11d480e0cf48ec4bdc73b8.tar.bz2 xen-a9344ed5826043957f11d480e0cf48ec4bdc73b8.zip |
xen: Allow vcpus to defer a shutdown request across critical
asynchronous operations (in particular, hvm ioreq requests).
Signed-off-by: Keir Fraser <keir@xensource.com>
-rw-r--r-- | xen/arch/x86/hvm/hvm.c | 3 | ||||
-rw-r--r-- | xen/arch/x86/hvm/io.c | 9 | ||||
-rw-r--r-- | xen/arch/x86/mm.c | 3 | ||||
-rw-r--r-- | xen/arch/x86/mm/shadow/multi.c | 4 | ||||
-rw-r--r-- | xen/common/domain.c | 116 | ||||
-rw-r--r-- | xen/common/domctl.c | 5 | ||||
-rw-r--r-- | xen/include/xen/sched.h | 12 |
7 files changed, 134 insertions, 18 deletions
diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 552caca822..888fe818d0 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -373,6 +373,9 @@ void hvm_send_assist_req(struct vcpu *v) { ioreq_t *p; + if ( unlikely(!vcpu_start_shutdown_deferral(v)) ) + return; /* implicitly bins the i/o operation */ + p = &get_vio(v->domain, v->vcpu_id)->vp_ioreq; if ( unlikely(p->state != STATE_IOREQ_NONE) ) { diff --git a/xen/arch/x86/hvm/io.c b/xen/arch/x86/hvm/io.c index 605a42978b..4bff30b2b9 100644 --- a/xen/arch/x86/hvm/io.c +++ b/xen/arch/x86/hvm/io.c @@ -771,10 +771,11 @@ void hvm_io_assist(struct vcpu *v) struct cpu_user_regs *regs; struct hvm_io_op *io_opp; unsigned long gmfn; + struct domain *d = v->domain; io_opp = &v->arch.hvm_vcpu.io_op; regs = &io_opp->io_context; - vio = get_vio(v->domain, v->vcpu_id); + vio = get_vio(d, v->vcpu_id); p = &vio->vp_ioreq; if ( p->state != STATE_IORESP_READY ) @@ -797,11 +798,13 @@ void hvm_io_assist(struct vcpu *v) memcpy(guest_cpu_user_regs(), regs, HVM_CONTEXT_STACK_BYTES); /* Has memory been dirtied? */ - if ( p->dir == IOREQ_READ && p->data_is_ptr ) + if ( (p->dir == IOREQ_READ) && p->data_is_ptr ) { gmfn = get_mfn_from_gpfn(paging_gva_to_gfn(v, p->data)); - mark_dirty(v->domain, gmfn); + mark_dirty(d, gmfn); } + + vcpu_end_shutdown_deferral(v); } /* diff --git a/xen/arch/x86/mm.c b/xen/arch/x86/mm.c index b559b1f567..fac87bcf02 100644 --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -806,7 +806,8 @@ void put_page_from_l1e(l1_pgentry_t l1e, struct domain *d) * (Note that the undestroyable active grants are not a security hole in * Xen. All active grants can safely be cleaned up when the domain dies.) */ - if ( (l1e_get_flags(l1e) & _PAGE_GNTTAB) && !d->is_shutdown && !d->is_dying ) + if ( (l1e_get_flags(l1e) & _PAGE_GNTTAB) && + !d->is_shutting_down && !d->is_dying ) { MEM_LOG("Attempt to implicitly unmap a granted PTE %" PRIpte, l1e_get_intpte(l1e)); diff --git a/xen/arch/x86/mm/shadow/multi.c b/xen/arch/x86/mm/shadow/multi.c index b1419a7b27..0a49f07add 100644 --- a/xen/arch/x86/mm/shadow/multi.c +++ b/xen/arch/x86/mm/shadow/multi.c @@ -2823,8 +2823,8 @@ static int sh_page_fault(struct vcpu *v, * are OK, this can only have been caused by a failed * shadow_set_l*e(), which will have crashed the guest. * Get out of the fault handler immediately. */ - ASSERT(d->is_shutdown); - unmap_walk(v, &gw); + ASSERT(d->is_shutting_down); + unmap_walk(v, &gw); shadow_unlock(d); return 0; } diff --git a/xen/common/domain.c b/xen/common/domain.c index b31ad9ce47..d94e65914f 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -59,6 +59,7 @@ struct domain *alloc_domain(domid_t domid) atomic_set(&d->refcnt, 1); spin_lock_init(&d->big_lock); spin_lock_init(&d->page_alloc_lock); + spin_lock_init(&d->shutdown_lock); INIT_LIST_HEAD(&d->page_list); INIT_LIST_HEAD(&d->xenpage_list); @@ -83,6 +84,45 @@ void free_domain(struct domain *d) xfree(d); } +static void __domain_finalise_shutdown(struct domain *d) +{ + struct vcpu *v; + + BUG_ON(!spin_is_locked(&d->shutdown_lock)); + + if ( d->is_shut_down ) + return; + + for_each_vcpu ( d, v ) + if ( !v->paused_for_shutdown ) + return; + + d->is_shut_down = 1; + + for_each_vcpu ( d, v ) + vcpu_sleep_nosync(v); + + send_guest_global_virq(dom0, VIRQ_DOM_EXC); +} + +static void vcpu_check_shutdown(struct vcpu *v) +{ + struct domain *d = v->domain; + + spin_lock(&d->shutdown_lock); + + if ( d->is_shutting_down ) + { + if ( !v->paused_for_shutdown ) + atomic_inc(&v->pause_count); + v->paused_for_shutdown = 1; + v->defer_shutdown = 0; + __domain_finalise_shutdown(d); + } + + spin_unlock(&d->shutdown_lock); +} + struct vcpu *alloc_vcpu( struct domain *d, unsigned int vcpu_id, unsigned int cpu_id) { @@ -122,6 +162,9 @@ struct vcpu *alloc_vcpu( if ( vcpu_id != 0 ) d->vcpu[v->vcpu_id-1]->next_in_list = v; + /* Must be called after making new vcpu visible to for_each_vcpu(). */ + vcpu_check_shutdown(v); + return v; } @@ -286,7 +329,7 @@ void domain_kill(struct domain *d) void __domain_crash(struct domain *d) { - if ( d->is_shutdown ) + if ( d->is_shutting_down ) { /* Print nothing: the domain is already shutting down. */ } @@ -335,16 +378,73 @@ void domain_shutdown(struct domain *d, u8 reason) if ( d->domain_id == 0 ) dom0_shutdown(reason); - atomic_inc(&d->pause_count); - if ( !xchg(&d->is_shutdown, 1) ) - d->shutdown_code = reason; - else - domain_unpause(d); + spin_lock(&d->shutdown_lock); + + if ( d->is_shutting_down ) + { + spin_unlock(&d->shutdown_lock); + return; + } + + d->is_shutting_down = 1; + d->shutdown_code = reason; + + smp_mb(); /* set shutdown status /then/ check for per-cpu deferrals */ for_each_vcpu ( d, v ) - vcpu_sleep_nosync(v); + { + if ( v->defer_shutdown ) + continue; + atomic_inc(&v->pause_count); + v->paused_for_shutdown = 1; + } - send_guest_global_virq(dom0, VIRQ_DOM_EXC); + __domain_finalise_shutdown(d); + + spin_unlock(&d->shutdown_lock); +} + +void domain_resume(struct domain *d) +{ + struct vcpu *v; + + /* + * Some code paths assume that shutdown status does not get reset under + * their feet (e.g., some assertions make this assumption). + */ + domain_pause(d); + + spin_lock(&d->shutdown_lock); + + d->is_shutting_down = d->is_shut_down = 0; + + for_each_vcpu ( d, v ) + { + if ( v->paused_for_shutdown ) + vcpu_unpause(v); + v->paused_for_shutdown = 0; + } + + spin_unlock(&d->shutdown_lock); + + domain_unpause(d); +} + +int vcpu_start_shutdown_deferral(struct vcpu *v) +{ + v->defer_shutdown = 1; + smp_mb(); /* set deferral status /then/ check for shutdown */ + if ( unlikely(v->domain->is_shutting_down) ) + vcpu_check_shutdown(v); + return v->defer_shutdown; +} + +void vcpu_end_shutdown_deferral(struct vcpu *v) +{ + v->defer_shutdown = 0; + smp_mb(); /* clear deferral status /then/ check for shutdown */ + if ( unlikely(v->domain->is_shutting_down) ) + vcpu_check_shutdown(v); } void domain_pause_for_debugger(void) diff --git a/xen/common/domctl.c b/xen/common/domctl.c index 27fd411363..2e1090df8d 100644 --- a/xen/common/domctl.c +++ b/xen/common/domctl.c @@ -115,7 +115,7 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) info->flags = flags | (d->is_dying ? XEN_DOMINF_dying : 0) | - (d->is_shutdown ? XEN_DOMINF_shutdown : 0) | + (d->is_shut_down ? XEN_DOMINF_shutdown : 0) | (d->is_paused_by_controller ? XEN_DOMINF_paused : 0) | d->shutdown_code << XEN_DOMINF_shutdownshift; @@ -287,8 +287,7 @@ long do_domctl(XEN_GUEST_HANDLE(xen_domctl_t) u_domctl) if ( d == NULL ) break; - if ( xchg(&d->is_shutdown, 0) ) - domain_unpause(d); + domain_resume(d); rcu_unlock_domain(d); ret = 0; } diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 20c0a8f9c9..f99206cb93 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -114,6 +114,10 @@ struct vcpu bool_t nmi_pending; /* Avoid NMI reentry by allowing NMIs to be masked for short periods. */ bool_t nmi_masked; + /* Require shutdown to be deferred for some asynchronous operation? */ + bool_t defer_shutdown; + /* VCPU is paused following shutdown request (d->is_shutting_down)? */ + bool_t paused_for_shutdown; unsigned long pause_flags; atomic_t pause_count; @@ -193,7 +197,9 @@ struct domain bool_t is_paused_by_controller; /* Guest has shut down (inc. reason code)? */ - bool_t is_shutdown; + spinlock_t shutdown_lock; + bool_t is_shutting_down; /* in process of shutting down? */ + bool_t is_shut_down; /* fully shut down? */ int shutdown_code; atomic_t pause_count; @@ -331,8 +337,12 @@ struct domain *get_domain_by_id(domid_t dom); void domain_destroy(struct domain *d); void domain_kill(struct domain *d); void domain_shutdown(struct domain *d, u8 reason); +void domain_resume(struct domain *d); void domain_pause_for_debugger(void); +int vcpu_start_shutdown_deferral(struct vcpu *v); +void vcpu_end_shutdown_deferral(struct vcpu *v); + /* * Mark specified domain as crashed. This function always returns, even if the * caller is the specified domain. The domain is not synchronously descheduled |