fix nested domain_pause() calls
v3 cover fix nested domain_pause() calls
Fix nested doamin_pause() calls, which will call arch_domain_unpause() with the domain still left paused.
Changes in V3:
- switch to pause_lock in patch 1
- add new patch 2
Juergen Gross (2):
xen/domain: fix domain_pause() and domain_unpause() xen/domain: make domain pause_count a plain unsigned int
xen/arch/arm/mem_access.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 8 ++++---- xen/arch/x86/mm/mem_access.c | 2 +- xen/arch/x86/mm/mem_sharing.c | 3 +-- xen/arch/x86/mm/p2m-ept.c | 6 +++--- xen/common/domain.c | 34 +++++++++++++++++++++++++--------- xen/common/keyhandler.c | 5 ++--- xen/include/xen/sched.h | 6 ++++-- 8 files changed, 41 insertions(+), 25 deletions(-)
Signature · 1 line
2.56.0
PATCH v3 1/2 xen/domain: fix domain_pause() and domain_unpause()
Calls of domain_pause() can be nested. So there should be no action other than handling the paused counter if a nested call is detected.
Especially calling arch_domain_unpause() unconditionally in domain_unpause() is wrong, but the arch_domain_pause() call in domain_pause() can be skipped, too, if the domain was paused already.
This requires to drop the scheme of the pause counter being modified just via atomic updates, as the calling of the arch_domain_[un]pause() functions needs to be guarded against races, too.
Introduce a pause_lock instead, which is being held across the update of the pause counter AND the potential call of arch_domain_[un]pause(). While there might still be races with concurrent pause/unpause calls, the vcpu_sleep() and vcpu_wake() calls can happen outside the lock, as especially vcpu_wake() is looking at the pause count, too.
Note that a race of domain_pause() seeing an initial count of 0 and domain_unpause() concurrently setting it back to 0 can't happen, as the domain_unpause() can be valid only, if the initial count was 1 already. This means, that there is no risk of the count being 0 at the end AND vcpus having been put to sleep by an unpause action.
---
V3:
- switch to lock
--- xen/common/domain.c | 30 ++++++++++++++++++++++++------ xen/include/xen/sched.h | 1 + 2 files changed, 25 insertions(+), 6 deletions(-)
xen/common/domain.c +24 -6
@@ -943,6 +943,7 @@ struct domain *domain_create(domid_t domid,rspin_lock_init_prof (d, domain_lock);rspin_lock_init_prof (d, page_alloc_lock);spin_lock_init (&d->hypercall_deadlock_mutex); +spin_lock_init (&d->pause_lock);rwlock_init (&d->caps_lock);INIT_PAGE_LIST_HEAD (&d->page_list);INIT_PAGE_LIST_HEAD (&d->extra_page_list);
@@ -1624,17 +1625,25 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v)static void _domain_pause (struct domain *d,bool sync) {struct vcpu *v; +bool pause; +spin_lock (&d->pause_lock); + + pause = !atomic_read (&d->pause_count);atomic_inc (&d->pause_count); +if ( pause ) +arch_domain_pause (d); - for_each_vcpu ( d, v ) -vcpu_sleep_nosync (v); +spin_unlock (&d->pause_lock); + +if ( pause ) + for_each_vcpu ( d, v ) +vcpu_sleep_nosync (v); +/* Wait might be needed even if already paused, could have been nosync. */ if ( sync ) for_each_vcpu ( d, v )vcpu_wait_sleep (v); - -arch_domain_pause (d); }void domain_pause (struct domain *d)
@@ -1651,10 +1660,19 @@ void domain_pause_nosync(struct domain *d)void domain_unpause (struct domain *d) {struct vcpu *v; +bool unpause; + +spin_lock (&d->pause_lock); + +ASSERT (atomic_read (&d->pause_count)); +atomic_dec (&d->pause_count); + unpause = !atomic_read (&d->pause_count); +if ( unpause ) +arch_domain_unpause (d); -arch_domain_unpause (d); +spin_unlock (&d->pause_lock); -if (atomic_dec_and_test (&d->pause_count) ) +if ( unpause )for_each_vcpu ( d, v )vcpu_wake (v); }
xen/include/xen/sched.h +1 -0
@@ -573,6 +573,7 @@ struct domain* raising DOM_EXC */ evtchn_port_t suspend_evtchn; +spinlock_t pause_lock;atomic_t pause_count;atomic_t refcnt;
Signature · 1 line
2.56.0
PATCH v3 2/2 xen/domain: make domain pause_count a plain unsigned int
In reply to Juergen GrossNow that the domain's pause count is modified only under lock, there is no need to use atomic updates on it.
Make it a plain unsigned int.
--- xen/arch/arm/mem_access.c | 2 +- xen/arch/x86/hvm/vmx/vmcs.c | 8 ++++---- xen/arch/x86/mm/mem_access.c | 2 +- xen/arch/x86/mm/mem_sharing.c | 3 +-- xen/arch/x86/mm/p2m-ept.c | 6 +++--- xen/common/domain.c | 12 +++++------- xen/common/keyhandler.c | 5 ++--- xen/include/xen/sched.h | 5 +++-- 8 files changed, 20 insertions(+), 23 deletions(-)
xen/arch/arm/mem_access.c +1 -1
@@ -452,7 +452,7 @@ int p2m_get_mem_access(struct domain *d, gfn_t gfn,void arch_p2m_set_access_required (struct domain *d,bool access_required) { -ASSERT (atomic_read (&d->pause_count)); +ASSERT (d->pause_count);p2m_get_hostp2m (d)->access_required = access_required; }
xen/arch/x86/hvm/vmx/vmcs.c +4 -4
@@ -1710,7 +1710,7 @@ int vmx_domain_enable_pml(struct domain *d)struct vcpu *v;int rc; -ASSERT (atomic_read (&d->pause_count)); +ASSERT (d->pause_count);if (vmx_domain_pml_enabled (d) )return 0 ;
@@ -1740,7 +1740,7 @@ void vmx_domain_disable_pml(struct domain *d) {struct vcpu *v; -ASSERT (atomic_read (&d->pause_count)); +ASSERT (d->pause_count);if ( !vmx_domain_pml_enabled (d) )return ;
@@ -1759,7 +1759,7 @@ void vmx_domain_flush_pml_buffers(struct domain *d) {struct vcpu *v; -ASSERT (atomic_read (&d->pause_count)); +ASSERT (d->pause_count);if ( !vmx_domain_pml_enabled (d) )return ;
@@ -1784,7 +1784,7 @@ void vmx_domain_update_eptp(struct domain *d)struct p2m_domain *p2m =p2m_get_hostp2m (d);struct vcpu *v; -ASSERT (atomic_read (&d->pause_count)); +ASSERT (d->pause_count); for_each_vcpu ( d, v )vmx_vcpu_update_eptp (v, p2m->ept.eptp);
xen/arch/x86/mm/mem_access.c +1 -1
@@ -446,7 +446,7 @@ int p2m_get_mem_access(struct domain *d, gfn_t gfn, xenmem_access_t *access,void arch_p2m_set_access_required (struct domain *d,bool access_required) { -ASSERT (atomic_read (&d->pause_count)); +ASSERT (d->pause_count);p2m_get_hostp2m (d)->access_required = access_required;
xen/arch/x86/mm/mem_sharing.c +1 -2
@@ -2200,8 +2200,7 @@ int mem_sharing_memop(XEN_GUEST_HANDLE_PARAM(xen_mem_sharing_op_t) arg)* Sanity check only, the client should keep the domains paused for * the duration of this op. */ -if ( !atomic_read (&d->pause_count) || - !atomic_read (&cd->pause_count) ) +if ( !d->pause_count || !cd->pause_count ) {rcu_unlock_domain (cd); rc = -EINVAL ;
xen/arch/x86/mm/p2m-ept.c +3 -3
@@ -1325,7 +1325,7 @@ static void ept_set_ad_sync(struct domain *d, bool value)static void ept_enable_pml (struct p2m_domain *p2m) {/* Domain must have been paused */ -ASSERT (atomic_read (&p2m->domain->pause_count)); +ASSERT (p2m->domain->pause_count);/* * No need to return whether vmx_domain_enable_pml has succeeded, as
@@ -1343,7 +1343,7 @@ static void ept_enable_pml(struct p2m_domain *p2m)static void ept_disable_pml (struct p2m_domain *p2m) {/* Domain must have been paused */ -ASSERT (atomic_read (&p2m->domain->pause_count)); +ASSERT (p2m->domain->pause_count);vmx_domain_disable_pml (p2m->domain);
@@ -1373,7 +1373,7 @@ static void cf_check ept_disable_hardware_log_dirty(struct p2m_domain *p2m) static void cf_check ept_flush_pml_buffers(struct p2m_domain *p2m) {/* Domain must have been paused */ -ASSERT (atomic_read (&p2m->domain->pause_count)); +ASSERT (p2m->domain->pause_count);vmx_domain_flush_pml_buffers (p2m->domain); }
xen/common/domain.c +5 -7
@@ -1050,7 +1050,7 @@ struct domain *domain_create(domid_t domid, goto fail; d->controller_pause_count =1 ; -atomic_inc (&d->pause_count); + d->pause_count =1 ;if ( (err =evtchn_init (d, config->max_evtchn_port)) !=0 ) goto fail;
@@ -1629,8 +1629,7 @@ static void _domain_pause(struct domain *d, bool sync)spin_lock (&d->pause_lock); - pause = !atomic_read (&d->pause_count); -atomic_inc (&d->pause_count); + pause = !d->pause_count++;if ( pause )arch_domain_pause (d);
@@ -1664,9 +1663,8 @@ void domain_unpause(struct domain *d)spin_lock (&d->pause_lock); -ASSERT (atomic_read (&d->pause_count)); -atomic_dec (&d->pause_count); - unpause = !atomic_read (&d->pause_count); +ASSERT (d->pause_count); + unpause = !--d->pause_count;if ( unpause )arch_domain_unpause (d);
@@ -1988,7 +1986,7 @@ void unmap_guest_area(struct vcpu *v, struct guest_area *area)struct page_info *pg;if ( v != current ) -ASSERT (atomic_read (&v->pause_count) |atomic_read (&d->pause_count)); +ASSERT (atomic_read (&v->pause_count) | d->pause_count);domain_lock (d); map = area->map;
xen/common/keyhandler.c +2 -3
@@ -283,9 +283,8 @@ static void cf_check dump_domains(unsigned char key)process_pending_softirqs ();printk ("General information for domain %u:\n" , d->domain_id); -printk (" refcnt=%d dying=%d pause_count=%d\n" , -atomic_read (&d->refcnt), d->is_dying, -atomic_read (&d->pause_count)); +printk (" refcnt=%d dying=%d pause_count=%u\n" , +atomic_read (&d->refcnt), d->is_dying, d->pause_count); printk(" nr_pages=%u xenheap_pages=%u"#ifdef CONFIG_MEM_SHARING " shared_pages=%u"
xen/include/xen/sched.h +3 -2
@@ -574,7 +574,8 @@ struct domainevtchn_port_t suspend_evtchn;spinlock_t pause_lock; -atomic_t pause_count; +unsigned int pause_count; +atomic_t refcnt;unsigned long vm_assist;
@@ -1054,7 +1055,7 @@ static inline bool vcpu_runnable(const struct vcpu *v) {return !(v->pause_flags |atomic_read (&v->pause_count) | -atomic_read (&v->domain->pause_count)); + v->domain->pause_count); }static inline bool is_vcpu_dirty_cpu (unsigned int cpu)
Signature · 1 line
2.56.0