xen/common: Properly reference count DOMCTL_{, un}pausedomain hypercalls
PATCH v3 1/1 xen/common: Properly reference count DOMCTL_{, un}pausedomain hypercalls
For safety reasons, c/s 6ae2df93c27 "mem_access: Add helper API to setup ring and enable mem_access" has to pause the domain while it performs a set of operations.
However without properly reference counted hypercalls, xc_mem_event_enable() now unconditionally unpauses a previously paused domain.
To prevent toolstack software running wild, there is an arbitrary limit of 255 on the toolstack pause count. This is high enough for several components of the toolstack to safely use, but prevents over/underflow of d->pause_count.
The previous domain_{,un}pause_by_systemcontroller() functions are updated to return an error code. domain_pause_by_systemcontroller() is modified to have a common stub and take a pause_fn pointer, allowing for both sync and nosync domain pauses. domain_pause_for_debugger() has a hand-rolled nosync pause replaced with the new domain_pause_by_systemcontroller_nosync(), and has its variables shuffled slightly to avoid rereading current multiple times.
---
v3:
* Squash into single patch with hand-rolled cmpxchg() loops for easier backport.
v2:
* Use atomic_{inc,dec}_bounded() instead of having a spinlock.
--- xen/arch/x86/domctl.c | 7 +++--- xen/common/domain.c | 61 ++++++++++++++++++++++++++++++++++------------- xen/common/domctl.c | 9 ++++--- xen/include/xen/sched.h | 15 +++++++++--- 4 files changed, 64 insertions(+), 28 deletions(-)
xen/arch/x86/domctl.c +4 -3
@@ -1027,7 +1027,7 @@ long arch_do_domctl(struct vcpu *v; ret = -EBUSY ; -if ( !d->is_paused_by_controller ) +if (atomic_read (&d->controller_pause_count) >0 )break ; ret = -EINVAL ; if ( domctl->u.gdbsx_pauseunp_vcpu.vcpu >= MAX_VIRT_CPUS ||
@@ -1043,7 +1043,7 @@ long arch_do_domctl(struct vcpu *v; ret = -EBUSY ; -if ( !d->is_paused_by_controller ) +if (atomic_read (&d->controller_pause_count) >0 )break ; ret = -EINVAL ; if ( domctl->u.gdbsx_pauseunp_vcpu.vcpu >= MAX_VIRT_CPUS ||
@@ -1061,7 +1061,8 @@ long arch_do_domctl(struct vcpu *v; domctl->u.gdbsx_domstatus.vcpu_id =-1 ; - domctl->u.gdbsx_domstatus.paused = d->is_paused_by_controller; + domctl->u.gdbsx_domstatus.paused = +atomic_read (&d->controller_pause_count) >0 ;if ( domctl->u.gdbsx_domstatus.paused ) {for_each_vcpu ( d, v )
xen/common/domain.c +44 -17
86 lines not on this page · raw message
xen/common/domctl.c +4 -5
@@ -181,7 +181,8 @@ void getdomaininfo(struct domain *d, struct xen_domctl_getdomaininfo *info) info->flags = (info->nr_online_vcpus ? flags :0 ) | ((d->is_dying == DOMDYING_dead) ? XEN_DOMINF_dying :0 ) | (d->is_shut_down ? XEN_DOMINF_shutdown :0 ) | - (d->is_paused_by_controller ? XEN_DOMINF_paused :0 ) | + (atomic_read (&d->controller_pause_count) >0 + ? XEN_DOMINF_paused :0 ) | (d->debugger_attached ? XEN_DOMINF_debugged :0 ) | d->shutdown_code << XEN_DOMINF_shutdownshift;
@@ -398,16 +399,14 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl) ret = -EINVAL ;if ( d != current->domain ) { -domain_pause_by_systemcontroller (d); - ret =0 ; + ret =domain_pause_by_systemcontroller (d); } }break ;case XEN_DOMCTL_unpausedomain: { -domain_unpause_by_systemcontroller (d); - ret =0 ; + ret =domain_unpause_by_systemcontroller (d); }break ;
xen/include/xen/sched.h +12 -3
@@ -366,7 +366,7 @@ struct domain/* Is this guest dying (i.e., a zombie)? */ enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;/* Domain is paused by controller software? */ -bool_t is_paused_by_controller; +atomic_t controller_pause_count;/* Domain's VCPUs are pinned 1:1 to physical CPUs? */ bool_t is_pinned;
@@ -769,8 +769,17 @@ void domain_pause(struct domain *d);void domain_pause_nosync (struct domain *d);void vcpu_unpause (struct vcpu *v);void domain_unpause (struct domain *d); -void domain_pause_by_systemcontroller (struct domain *d); -void domain_unpause_by_systemcontroller (struct domain *d); +int domain_unpause_by_systemcontroller (struct domain *d); +int __domain_pause_by_systemcontroller (struct domain *d, +void (*pause_fn)(struct domain *d)); +static inline int domain_pause_by_systemcontroller (struct domain *d) +{ +return __domain_pause_by_systemcontroller (d, domain_pause); +} +static inline int domain_pause_by_systemcontroller_nosync (struct domain *d) +{ +return __domain_pause_by_systemcontroller (d, domain_pause_nosync); +}void cpu_init (void );struct scheduler ;
Signature · 1 line
1.7.10.4
xen/include/xen/sched.h struct domain · line 372
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 17:03, <[email protected]> wrote:
xen/include/xen/sched.h +1
-1@@ -366,7 +366,7 @@ struct domain/* Is this guest dying (i.e., a zombie)? */ enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;/* Domain is paused by controller software? */ -bool_t is_paused_by_controller; +atomic_t controller_pause_count;/* Domain's VCPUs are pinned 1:1 to physical CPUs? */ bool_t is_pinned;
Now why did you leave this be an atomic_t, sitting between two bool_t-s?
Jan
Quoted 15 lines · Jan Beulich
xen/include/xen/sched.h struct domain · line 372
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 17:03, <[email protected]> wrote:
xen/include/xen/sched.h +1
-1@@ -366,7 +366,7 @@ struct domain/* Is this guest dying (i.e., a zombie)? */ enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;/* Domain is paused by controller software? */ -bool_t is_paused_by_controller; +atomic_t controller_pause_count;/* Domain's VCPUs are pinned 1:1 to physical CPUs? */ bool_t is_pinned;Now why did you leave this be an
atomic_t, sitting between two bool_t-s?
Quoted 2 lines
Jan
It is not sitting between two bool_t's. It is sitting next to an enum which has the same natural width and alignment.
Given the other holes in this area, moving it elsewhere wouldn't affect the structure size.
~Andrew
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 17:41, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
xen/include/xen/sched.h struct domain · line 372
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 17:03, <[email protected]> wrote:
xen/include/xen/sched.h +1
-1@@ -366,7 +366,7 @@ struct domain/* Is this guest dying (i.e., a zombie)? */ enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;/* Domain is paused by controller software? */ -bool_t is_paused_by_controller; +atomic_t controller_pause_count;/* Domain's VCPUs are pinned 1:1 to physical CPUs? */ bool_t is_pinned;Now why did you leave this be an
atomic_t, sitting between two bool_t-s?
Quoted 2 lines
It is not sitting between two bool_t's. It is sitting next to an enum which has the same natural width and alignment.
Oh, right, the first one is being replaced.
But you didn't say why it need to be an atomic_t, while in v1 it was a plain scalar.
Jan
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 17:41, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
xen/include/xen/sched.h struct domain · line 372
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 17:03, <[email protected]> wrote:
xen/include/xen/sched.h +1
-1@@ -366,7 +366,7 @@ struct domain/* Is this guest dying (i.e., a zombie)? */ enum { DOMDYING_alive, DOMDYING_dying, DOMDYING_dead } is_dying;/* Domain is paused by controller software? */ -bool_t is_paused_by_controller; +atomic_t controller_pause_count;/* Domain's VCPUs are pinned 1:1 to physical CPUs? */ bool_t is_pinned;Now why did you leave this be an
atomic_t, sitting between two bool_t-s?
Quoted 9 lines
Quoted 2 lines
It is not sitting between two bool_t's. It is sitting next to an enum which has the same natural width and alignment.
Oh, right, the first one is being replaced.
But you didn't say why it need to be an
atomic_t, while in v1 it was a plain scalar.Jan
v1 was a plain scalar because of the spinlock. v2 was an atomic_t because of atomic_{inc,dec}_bounded()
But given the hand rolled loops with cmpxchg(), this can indeed be a plan scalar.
v4 on its way.
~Andrew