Improvements to allow refcoutning of DOMCTL_{, un}pausedomain
v2 cover Improvements to allow refcoutning of DOMCTL_{, un}pausedomain
The first patch has no functional change, but introduces a common atomic.h
The second patch introduces new common atomic_t manipulation function
The third patch uses these new common functions as a basis for fixing the DOMCTL_{,un}pausedomain reference counting issues.
This has been functionally tested on x86, and compile tested on both flavours of ARM.
Andrew Cooper (3):
xen/atomic: Introduce common atomic header and update includes xen/atomic: Implement atomic_{inc,dec}_bounded() xen/common: Properly reference count DOMCTL_{,un}pausedomain
hypercalls
xen/arch/x86/apic.c | 2 +- xen/arch/x86/cpu/mcheck/barrier.h | 2 +- xen/arch/x86/cpu/mcheck/mce.h | 2 +- xen/arch/x86/crash.c | 2 +- xen/arch/x86/domctl.c | 7 +++--- xen/arch/x86/i8259.c | 2 +- xen/arch/x86/mm/mem_sharing.c | 2 +- xen/arch/x86/mm/shadow/private.h | 2 +- xen/arch/x86/traps.c | 2 +- xen/common/Makefile | 1 + xen/common/atomic.c | 35 ++++++++++++++++++++++++++++++ xen/common/domain.c | 41 ++++++++++++++++++++--------------- xen/common/domctl.c | 9 ++++---- xen/common/kexec.c | 2 +- xen/common/rcupdate.c | 2 +- xen/common/sched_credit.c | 2 +- xen/common/spinlock.c | 2 +- xen/common/timer.c | 2 +- xen/common/trace.c | 2 +- xen/drivers/passthrough/arm/smmu.c | 2 +- xen/include/acpi/platform/aclinux.h | 2 +- xen/include/asm-x86/atomic.h | 5 +++++ xen/include/asm-x86/irq.h | 2 +- xen/include/asm-x86/spinlock.h | 2 +- xen/include/xen/atomic.h | 26 ++++++++++++++++++++++ xen/include/xen/gdbstub.h | 2 +- xen/include/xen/sched.h | 17 +++++++++++---- xen/xsm/flask/avc.c | 2 +- 28 files changed, 132 insertions(+), 49 deletions(-) create mode 100644 xen/common/atomic.c create mode 100644 xen/include/xen/atomic.h
Signature · 1 line
1.7.10.4
PATCH v2 1/3 xen/atomic: Introduce common atomic header and update includes
This patch does reorder the #includes in asm-arm/atomic.h to pull in definitions in the correct order, which would otherwise cause a build failure in patch #2 because of struct vcpu_guest_core_regs being referenced before definition.
No functional change.
--- xen/arch/x86/apic.c | 2 +- xen/arch/x86/cpu/mcheck/barrier.h | 2 +- xen/arch/x86/cpu/mcheck/mce.h | 2 +- xen/arch/x86/crash.c | 2 +- xen/arch/x86/i8259.c | 2 +- xen/arch/x86/mm/mem_sharing.c | 2 +- xen/arch/x86/mm/shadow/private.h | 2 +- xen/arch/x86/traps.c | 2 +- xen/common/kexec.c | 2 +- xen/common/rcupdate.c | 2 +- xen/common/sched_credit.c | 2 +- xen/common/spinlock.c | 2 +- xen/common/timer.c | 2 +- xen/common/trace.c | 2 +- xen/drivers/passthrough/arm/smmu.c | 2 +- xen/include/acpi/platform/aclinux.h | 2 +- xen/include/asm-arm/atomic.h | 2 +- xen/include/asm-x86/irq.h | 2 +- xen/include/asm-x86/spinlock.h | 2 +- xen/include/xen/atomic.h | 6 ++++++ xen/include/xen/gdbstub.h | 2 +- xen/include/xen/sched.h | 2 +- xen/xsm/flask/avc.c | 2 +- 23 files changed, 28 insertions(+), 22 deletions(-) create mode 100644 xen/include/xen/atomic.h
xen/arch/x86/apic.c +1 -1
@@ -28,7 +28,7 @@#include <xen/softirq.h> #include <asm/mc146818rtc.h> #include <asm/msr.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/mpspec.h> #include <asm/flushtlb.h> #include <asm/hardirq.h>
xen/arch/x86/cpu/mcheck/barrier.h +1 -1
@@ -1,7 +1,7 @@#ifndef _MCHECK_BARRIER_H#define _MCHECK_BARRIER_H -#include <asm/atomic.h> +#include <xen/atomic.h> /* MCE handling */ struct mce_softirq_barrier {
xen/arch/x86/cpu/mcheck/mce.h +1 -1
@@ -7,7 +7,7 @@#include <xen/smp.h> #include <asm/types.h> #include <asm/traps.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/percpu.h> #include "x86_mca.h"
xen/arch/x86/crash.c +1 -1
@@ -8,7 +8,7 @@* - Magnus Damm <[email protected]> */ -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/elf.h> #include <asm/percpu.h> #include <xen/types.h>
xen/arch/x86/i8259.c +1 -1
@@ -12,7 +12,7 @@#include <xen/errno.h> #include <xen/sched.h> #include <xen/irq.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/system.h> #include <asm/io.h> #include <asm/desc.h>
xen/arch/x86/mm/mem_sharing.c +1 -1
@@ -31,7 +31,7 @@#include <asm/string.h> #include <asm/p2m.h> #include <asm/mem_event.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <xen/rcupdate.h> #include <asm/event.h> #include <xsm/xsm.h>
xen/arch/x86/mm/shadow/private.h +1 -1
@@ -30,7 +30,7 @@#include <xen/domain_page.h> #include <asm/x86_emulate.h> #include <asm/hvm/support.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include "../mm-locks.h"
xen/arch/x86/traps.c +1 -1
@@ -52,7 +52,7 @@#include <xen/watchdog.h> #include <asm/system.h> #include <asm/io.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <xen/bitops.h> #include <asm/desc.h> #include <asm/debugreg.h>
xen/common/kexec.c +1 -1
@@ -20,7 +20,7 @@#include <xen/keyhandler.h> #include <public/kexec.h> #include <xen/cpumask.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <xen/spinlock.h> #include <xen/version.h> #include <xen/console.h>
xen/common/rcupdate.c +1 -1
@@ -39,7 +39,7 @@#include <xen/smp.h> #include <xen/rcupdate.h> #include <xen/sched.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <xen/bitops.h> #include <xen/percpu.h> #include <xen/softirq.h>
xen/common/sched_credit.c +1 -1
@@ -18,7 +18,7 @@#include <xen/time.h> #include <xen/sched-if.h> #include <xen/softirq.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/div64.h> #include <xen/errno.h> #include <xen/keyhandler.h>
xen/common/spinlock.c +1 -1
@@ -8,7 +8,7 @@#include <xen/preempt.h> #include <public/sysctl.h> #include <asm/processor.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #ifndef NDEBUG
xen/common/timer.c +1 -1
@@ -23,7 +23,7 @@#include <xen/symbols.h> #include <asm/system.h> #include <asm/desc.h> -#include <asm/atomic.h> +#include <xen/atomic.h> /* We program the time hardware this far behind the closest deadline. */ static unsigned int timer_slop__read_mostly =50000 ;/* 50 us */
xen/common/trace.c +1 -1
@@ -31,7 +31,7 @@#include <xen/percpu.h> #include <xen/pfn.h> #include <xen/cpu.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <public/sysctl.h> #ifdef CONFIG_COMPAT
xen/drivers/passthrough/arm/smmu.c +1 -1
@@ -43,7 +43,7 @@#include <xen/vmap.h> #include <xen/rbtree.h> #include <xen/sched.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/device.h> #include <asm/io.h> #include <asm/platform.h>
xen/include/acpi/platform/aclinux.h +1 -1
@@ -54,7 +54,7 @@#include <xen/ctype.h> #include <xen/spinlock.h> #include <asm/system.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/div64.h> #include <asm/acpi.h> #include <asm/current.h>
xen/include/asm-arm/atomic.h +1 -1
@@ -2,8 +2,8 @@#define __ARCH_ARM_ATOMIC__#include <xen/config.h> -#include <xen/prefetch.h> #include <asm/system.h> +#include <xen/prefetch.h> #define build_atomic_read (name, size, width, type, reg)\ static inline type name(const volatile type *addr) \
xen/include/asm-x86/irq.h +1 -1
@@ -4,7 +4,7 @@/* (C) 1992, 1993 Linus Torvalds, (C) 1997 Ingo Molnar */ #include <xen/config.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <xen/cpumask.h> #include <xen/smp.h> #include <xen/hvm/irq.h>
xen/include/asm-x86/spinlock.h +1 -1
@@ -3,7 +3,7 @@#include <xen/config.h> #include <xen/lib.h> -#include <asm/atomic.h> +#include <xen/atomic.h> typedef struct { volatile s16 lock;
xen/include/xen/atomic.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef __XEN_ATOMIC_H__ +#define __XEN_ATOMIC_H__ + +#include <asm/atomic.h> + +#endif /* __XEN_ATOMIC_H__ */
xen/include/xen/gdbstub.h +1 -1
@@ -21,7 +21,7 @@#ifndef __XEN_GDBSTUB_H__#define __XEN_GDBSTUB_H__ -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/page.h> #ifdef CRASH_DEBUG
xen/include/xen/sched.h +1 -1
@@ -17,7 +17,7 @@#include <xen/mm.h> #include <xen/smp.h> #include <xen/perfc.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <xen/wait.h> #include <public/xen.h> #include <public/domctl.h>
xen/xsm/flask/avc.c +1 -1
@@ -26,7 +26,7 @@#include <xen/sched.h> #include <xen/init.h> #include <xen/rcupdate.h> -#include <asm/atomic.h> +#include <xen/atomic.h> #include <asm/current.h> #include <public/xsm/flask_op.h>
Signature · 1 line
1.7.10.4
Re: [PATCH v2 1/3] xen/atomic: Introduce common atomic header and update includes
xen/arch/x86/apic.c line 31
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 15:47, <[email protected]> wrote:
xen/arch/x86/apic.c +1
-1@@ -28,7 +28,7 @@#include <xen/softirq.h> #include <asm/mc146818rtc.h> #include <asm/msr.h> -#include <asm/atomic.h> +#include <xen/atomic.h>
I appreciate you having taken the time to replace all these, but - do we really need to do this replacement at this point? Especially arch code should be fine using the asm/ header instad of the xen/ one.
Jan
Re: [PATCH v2 1/3] xen/atomic: Introduce common atomic header and update includes
Quoted 13 lines · Jan Beulich
xen/arch/x86/apic.c line 31
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 15:47, <[email protected]> wrote:
xen/arch/x86/apic.c +1
-1@@ -28,7 +28,7 @@#include <xen/softirq.h> #include <asm/mc146818rtc.h> #include <asm/msr.h> -#include <asm/atomic.h> +#include <xen/atomic.h> I appreciate you having taken the time to replace all these, but - do we really need to do this replacement at this point? Especially arch code should be fine using the asm/ header instad of the xen/ one.
Quoted 2 lines
Jan
Not strictly, but I figured it was cleaner to update everything at once than to have a mix of common and arch specific includes.
(Also it didn't take long at all. git grep ... | xargs sed is a remakably quick refactoring tool, and gcc will quickly tell you if it went wrong)
~Andrew
PATCH v2 2/3 xen/atomic: Implement atomic_{inc, dec}_bounded()
In reply to Andrew CooperThese will increment/decremented an atomic_t, while ensuring that the atomic_t doesn't hit a specific bound.
This involves introducing atomic_cmpxchg() for x86, which previously only existed on ARM.
--- xen/common/Makefile | 1 + xen/common/atomic.c | 35 +++++++++++++++++++++++++++++++++++ xen/include/asm-x86/atomic.h | 5 +++++ xen/include/xen/atomic.h | 20 ++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 xen/common/atomic.c
xen/common/Makefile +1 -0
@@ -1,3 +1,4 @@ +obj-y += atomic.o obj-y += bitmap.o obj-y += core_parking.o obj-y += cpu.o
xen/common/atomic.c new file +35 -0
@@ -0,0 +1,35 @@ +#include <xen/atomic.h> + +bool_t atomic_inc_bounded (atomic_t *v,int bound) +{ +int old, new, prev =atomic_read (v); + +do + { + old = prev; + new = old +1 ; +if ( new >= bound ) +return 0 ; + + prev =atomic_cmpxchg (v, old, new); + }while ( prev != old ); + +return 1 ; +} + +bool_t atomic_dec_bounded (atomic_t *v,int bound) +{ +int old, new, prev =atomic_read (v); + +do + { + old = prev; + new = old -1 ; +if ( new <= bound ) +return 0 ; + + prev =atomic_cmpxchg (v, old, new); + }while ( prev != old ); + +return 1 ; +}
xen/include/asm-x86/atomic.h +5 -0
@@ -255,4 +255,9 @@ static inline atomic_t atomic_compareandswap( return rc; } +static inline int atomic_cmpxchg (atomic_t *ptr,int old,int new) +{ +return __cmpxchg (&ptr->counter, old, new,sizeof (ptr->counter)); +} +#endif /* __ARCH_X86_ATOMIC__ */
xen/include/xen/atomic.h +20 -0
@@ -3,4 +3,24 @@#include <asm/atomic.h> +/** +* atomic_inc_bounded - increment @v if it wouldn't exceed @bound +* @v: pointer of type atomic_t +* @bound: boundary to compare against +* +* returns true if @v was incremented, or false if incrementing @v would hit +* @bound. +*/ +bool_t atomic_inc_bounded (atomic_t *v,int bound); + +/** +* atomic_dec_bounded - decrement @v if it wouldn't exceed @bound +* @v: pointer of type atomic_t +* @bound: boundary to compare against +* +* returns true if @v was decremented, or false if decrementing @v would hit +* @bound. +*/ +bool_t atomic_dec_bounded (atomic_t *v,int bound); +#endif /* __XEN_ATOMIC_H__ */
Signature · 1 line
1.7.10.4
Re: [PATCH v2 2/3] xen/atomic: Implement atomic_{inc, dec}_bounded()
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 15:47, <[email protected]> wrote:
These will increment/decremented an
atomic_t, while ensuring that theatomic_t
doesn't hit a specific bound.
Quoted 7 lines
This involves introducing
atomic_cmpxchg()for x86, which previously only existed on ARM.Suggested-by Don Slutz <[email protected]>Signed-off-by Andrew Cooper <[email protected]>CC Keir Fraser <[email protected]>CC Jan Beulich <[email protected]>
You should be getting used to Cc all "REST" maintainers on respective changes.
Quoted 28 lines
xen/common/atomic.c new file +25
-0@@ -0,0 +1,35 @@ +#include <xen/atomic.h> + +bool_t atomic_inc_bounded (atomic_t *v,int bound) +{ +int old, new, prev =atomic_read (v); + +do + { + old = prev; + new = old +1 ; +if ( new >= bound ) +return 0 ; + + prev =atomic_cmpxchg (v, old, new); + }while ( prev != old ); + +return 1 ; +} + +bool_t atomic_dec_bounded (atomic_t *v,int bound) +{ +int old, new, prev =atomic_read (v); + + do + {
xen/common/atomic.c line 35
Quoted 10 lines
xen/common/atomic.c new file +10
-0+ old = prev; + new = old -1 ; +if ( new <= bound ) +return 0 ; + + prev =atomic_cmpxchg (v, old, new); + }while ( prev != old ); + +return 1 ; +}
Same question here: Do we really need these? There are various uses of cmpxchg() in common code already, and in patch 3 you don't really need the cmpxchg to happen on an atomic_t, so plain cmpxchg() with the little bit of extra logic open coded would seem fine to me.
Jan
Re: [PATCH v2 2/3] xen/atomic: Implement atomic_{inc, dec}_bounded()
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 02.07.14 at 15:47, <[email protected]> wrote:
These will increment/decremented an
atomic_t, while ensuring that theatomic_t
doesn't hit a specific bound.This involves introducing
atomic_cmpxchg()for x86, which previously only existed on ARM.
Quoted 6 lines
Quoted 4 lines
Suggested-by Don Slutz <[email protected]>Signed-off-by Andrew Cooper <[email protected]>CC Keir Fraser <[email protected]>CC Jan Beulich <[email protected]>You should be getting used to Cc all "REST" maintainers on respective changes.
Oops yes - I will see about updating my auto-cc list hooks.
Quoted 44 lines
Quoted 29 lines
xen/common/atomic.c new file +25
-0@@ -0,0 +1,35 @@ +#include <xen/atomic.h> + +bool_t atomic_inc_bounded (atomic_t *v,int bound) +{ +int old, new, prev =atomic_read (v); + +do + { + old = prev; + new = old +1 ; +if ( new >= bound ) +return 0 ; + + prev =atomic_cmpxchg (v, old, new); + }while ( prev != old ); + +return 1 ; +} + +bool_t atomic_dec_bounded (atomic_t *v,int bound) +{ +int old, new, prev =atomic_read (v); + + do + {xen/common/atomic.c line 35
Quoted 10 lines
xen/common/atomic.c new file +10
-0+ old = prev; + new = old -1 ; +if ( new <= bound ) +return 0 ; + + prev =atomic_cmpxchg (v, old, new); + }while ( prev != old ); + +return 1 ; +}Same question here: Do we really need these? There are various uses of
cmpxchg()in common code already, and in patch 3 you don't really need the cmpxchg to happen on anatomic_t, so plaincmpxchg()with the little bit of extra logic open coded would seem fine to me.
Quoted 2 lines
Jan
These as in atomic_{inc,dec}_bounded() ?
They logically fit alongside atomic_{inc,dec}_and_test(), and I came to the conclusion that they would likely be useful elsewhere.
I realise that these are not fantastic reasons alone...
~Andrew
PATCH v2 3/3 xen/common: Properly reference count DOMCTL_{, un}pausedomain hypercalls
In reply to Andrew CooperFor 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.
---
This has been functionally tested on x86, and compile tested on both flavours of ARM.
v2:
* Use atomic_{inc,dec}_bounded() instead of having a spinlock.
--- xen/arch/x86/domctl.c | 7 ++++--- xen/common/domain.c | 41 ++++++++++++++++++++++++----------------- xen/common/domctl.c | 9 ++++----- xen/include/xen/sched.h | 15 ++++++++++++--- 4 files changed, 44 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 +24 -17
66 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