mitigate the per-pCPU blocking list may be too long
v3 cover mitigate the per-pCPU blocking list may be too long
VT-d PI introduces a per-pCPU blocking list to track the blocked vCPU on a given pCPU. Theoretically, there are 32K domain on single host, 128 vCPUs per domain. If all vCPUs are blocked on the same pCPU, 4M vCPUs are in the same list. Traversing this list consumes too much time. More discussion can be found in [1,2,3].
To mitigate this issue, this series put vcpus to another pcpu's list when the local pcpu's list length reachs an upper bound. The upper bound is determined by total vcpu count and total pcpu count in the system.
PATCH 1/3 adds a counter to track the per-pCPU blocking list's length and to aid analysis and obtain the list length, add some relevant events to xentrace. With this patch, some data can be acquired to help to validate patch 3/3.
PATCH 2/3 uses a global variable to track how many hvm vcpus on this system. It is used to calculate the number limit of blocked vcpu on a given pcpu.
In patch 3/3, a policy is used to restrict the vcpu count on a given pcpu's pi blocking list in case the list grows too long.
In this new version, the method only putting vcpus which are referred by at least one IRTE to the list is removed for
1. The implementation is intrusive.
2. Now, no evidence shows its necessity.
[1] https://lists.gt.net/xen/devel/422661?search_string=VT-d%20posted-interrupt%20core%20logic%20handling;#422661
[2] https://lists.gt.net/xen/devel/422567?search_string=%20The%20length%20of%20the%20list%20depends;#422567
[3] https://lists.gt.net/xen/devel/472749?search_string=enable%20vt-d%20pi%20by%20default;#472749
Chao Gao (3):
VT-d PI: track the vcpu number in pi blocking list
vcpu: track hvm vcpu number on the system VT-d PI: restrict the vcpu number on a given pcpu
tools/xentrace/formats | 3 + tools/xentrace/xenalyze.c | 159 +++++++++++++++++++++++++++++++++++++--- xen/arch/x86/domain.c | 10 +++ xen/arch/x86/hvm/vmx/vmx.c | 97 +++++++++++++++++++++--- xen/include/asm-x86/domain.h | 2 + xen/include/asm-x86/hvm/trace.h | 2 + xen/include/public/trace.h | 6 ++ 7 files changed, 261 insertions(+), 18 deletions(-)
Signature · 7 lines
1.8.3.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
PATCH v3 1/1 VT-d PI: track the vcpu number in pi blocking list
This patch adds a field, counter, in struct vmx_pi_blocking_vcpu to track how many entries are on the pi blocking list. In order to analyze list operation frequence and obtain the list length, add some relevant events to xentrace and some associated code in xenalyze. As to xenalyze, for removing from pi list can happen in various contexts (interrupt context, and non-interrupt context) and be done by the vcpu itself or others, some of the contexts would incur that toplevel_assert_check() fails. To bypass this check, this patch adds a new type TRC_HVM_ASYNC and for this new type event, the context would not be checked.
--- tools/xentrace/formats | 3 + tools/xentrace/xenalyze.c | 154 +++++++++++++++++++++++++++++++++++++--- xen/arch/x86/hvm/vmx/vmx.c | 33 +++++++-- xen/include/asm-x86/hvm/trace.h | 1 + xen/include/public/trace.h | 5 ++ 5 files changed, 182 insertions(+), 14 deletions(-)
tools/xentrace/formats +3 -0
@@ -125,6 +125,9 @@ 0x00082020 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) INTR_WINDOW [ value = 0x%(1)08x ] 0x00082021 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) NPF [ gpa = 0x%(2)08x%(1)08x mfn = 0x%(4)08x%(3)08x qual = 0x%(5)04x p2mt = 0x%(6)04x ] 0x00082023 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) TRAP [ vector = 0x%(1)02x ] +0x00082026 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) PI_LIST_ADD [ domid = 0x%(1)04x vcpu = 0x%(2)04x, pcpu = 0x%(3)04x, #entry = 0x%(4)04x ] + +0x00088001 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) ASYNC_PI_LIST_DEL [ domid = 0x%(1)04x vcpu = 0x%(2)04x ] 0x0010f001 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) page_grant_map [ domid = %(1)d ] 0x0010f002 CPU%(cpu)d %(tsc)d (+%(reltsc)8d) page_grant_unmap [ domid = %(1)d ]
tools/xentrace/xenalyze.c +145 -9
289 lines not on this page · raw message
xen/arch/x86/hvm/vmx/vmx.c +28 -5
@@ -82,6 +82,7 @@ static int vmx_vmfunc_intercept(struct cpu_user_regs *regs);struct vmx_pi_blocking_vcpu {struct list_head list;spinlock_t lock; +uint32_t counter; };/*
@@ -119,6 +120,9 @@ static void vmx_vcpu_block(struct vcpu *v)*/ ASSERT (old_lock ==NULL ); +per_cpu (vmx_pi_blocking, v->processor).counter++; +TRACE_4D (TRC_HVM_PI_LIST_ADD , v->domain->domain_id, v->vcpu_id, + v->processor,per_cpu (vmx_pi_blocking, v->processor).counter);list_add_tail (&v->arch.hvm_vmx.pi_blocking.list, &per_cpu (vmx_pi_blocking, v->processor).list);spin_unlock_irqrestore (pi_blocking_list_lock, flags);
@@ -186,6 +190,9 @@ static void vmx_pi_unblock_vcpu(struct vcpu *v) {ASSERT (v->arch.hvm_vmx.pi_blocking.lock == pi_blocking_list_lock);list_del (&v->arch.hvm_vmx.pi_blocking.list); +container_of (pi_blocking_list_lock, + struct vmx_pi_blocking_vcpu, lock)->counter--; +TRACE_2D (TRC_HVM_ASYNC_PI_LIST_DEL , v->domain->domain_id, v->vcpu_id); v->arch.hvm_vmx.pi_blocking.lock =NULL ; }
@@ -220,6 +227,8 @@ void vmx_pi_desc_fixup(unsigned int cpu)list_for_each_entry_safe (vmx, tmp, blocked_vcpus, pi_blocking.list) { + struct vcpu *v = container_of(vmx, struct vcpu, arch.hvm_vmx); +/* * Suppress notification or we may miss an interrupt when the * target cpu is dying.
@@ -234,8 +243,11 @@ void vmx_pi_desc_fixup(unsigned int cpu)if (pi_test_on (&vmx->pi_desc) ) {list_del (&vmx->pi_blocking.list); +TRACE_2D (TRC_HVM_ASYNC_PI_LIST_DEL , v->domain->domain_id, + v->vcpu_id); +per_cpu (vmx_pi_blocking, cpu).counter--; vmx->pi_blocking.lock =NULL ; - vcpu_unblock(container_of(vmx, struct vcpu, arch.hvm_vmx)); +vcpu_unblock (v); }else {
@@ -258,6 +270,12 @@ void vmx_pi_desc_fixup(unsigned int cpu)list_move (&vmx->pi_blocking.list, &per_cpu (vmx_pi_blocking, new_cpu).list); +per_cpu (vmx_pi_blocking, cpu).counter--; +per_cpu (vmx_pi_blocking, new_cpu).counter++; +TRACE_2D (TRC_HVM_ASYNC_PI_LIST_DEL , v->domain->domain_id, + v->vcpu_id); +TRACE_4D (TRC_HVM_PI_LIST_ADD , v->domain->domain_id, v->vcpu_id, + new_cpu,per_cpu (vmx_pi_blocking, new_cpu).counter); vmx->pi_blocking.lock = new_lock;spin_unlock (new_lock);
@@ -2358,9 +2376,9 @@ static struct hvm_function_table __initdata vmx_function_table = {static void pi_wakeup_interrupt (struct cpu_user_regs *regs) {struct arch_vmx_struct *vmx, *tmp; -spinlock_t *lock = &per_cpu (vmx_pi_blocking,smp_processor_id ()).lock; -struct list_head *blocked_vcpus = - &per_cpu (vmx_pi_blocking,smp_processor_id ()).list; +unsigned int cpu =smp_processor_id (); +spinlock_t *lock = &per_cpu (vmx_pi_blocking, cpu).lock; +struct list_head *blocked_vcpus = &per_cpu (vmx_pi_blocking, cpu).list;ack_APIC_irq ();this_cpu (irq_count)++;
@@ -2376,10 +2394,15 @@ static void pi_wakeup_interrupt(struct cpu_user_regs *regs) {if (pi_test_on (&vmx->pi_desc) ) { + struct vcpu *v = container_of(vmx, struct vcpu, arch.hvm_vmx); +list_del (&vmx->pi_blocking.list); +TRACE_2D (TRC_HVM_ASYNC_PI_LIST_DEL , v->domain->domain_id, + v->vcpu_id); +per_cpu (vmx_pi_blocking, cpu).counter--;ASSERT (vmx->pi_blocking.lock == lock); vmx->pi_blocking.lock =NULL ; - vcpu_unblock(container_of(vmx, struct vcpu, arch.hvm_vmx)); +vcpu_unblock (v); } }
xen/include/asm-x86/hvm/trace.h +1 -0
@@ -54,6 +54,7 @@#define DO_TRC_HVM_TRAP DEFAULT_HVM_MISC#define DO_TRC_HVM_TRAP_DEBUG DEFAULT_HVM_MISC#define DO_TRC_HVM_VLAPIC DEFAULT_HVM_MISC +#define DO_TRC_HVM_PI_LIST_ADD DEFAULT_HVM_MISC#define TRC_PAR_LONG (par) ((par)&0xFFFFFFFF),((par)>>32)
xen/include/public/trace.h +5 -0
@@ -53,6 +53,7 @@#define TRC_HVM_ENTRYEXIT 0x00081000/* VMENTRY and #VMEXIT */ #define TRC_HVM_HANDLER 0x00082000/* various HVM handlers */ #define TRC_HVM_EMUL 0x00084000/* emulated devices */ +#define TRC_HVM_ASYNC 0x00088000/* Asynchronous events */ #define TRC_SCHED_MIN 0x00021000/* Just runstate changes */ #define TRC_SCHED_CLASS 0x00022000/* Scheduler-specific */
@@ -234,6 +235,7 @@#define TRC_HVM_TRAP (TRC_HVM_HANDLER + 0x23)#define TRC_HVM_TRAP_DEBUG (TRC_HVM_HANDLER + 0x24)#define TRC_HVM_VLAPIC (TRC_HVM_HANDLER + 0x25) +#define TRC_HVM_PI_LIST_ADD (TRC_HVM_HANDLER + 0x26)#define TRC_HVM_IOPORT_WRITE (TRC_HVM_HANDLER + 0x216)#define TRC_HVM_IOMEM_WRITE (TRC_HVM_HANDLER + 0x217)
@@ -257,6 +259,9 @@#define TRC_HVM_EMUL_PIC_PEND_IRQ_CALL (TRC_HVM_EMUL + 0x10)#define TRC_HVM_EMUL_LAPIC_PIC_INTR (TRC_HVM_EMUL + 0x11) +/* Trace asynconous events for HVM */ +#define TRC_HVM_ASYNC_PI_LIST_DEL (TRC_HVM_ASYNC + 0x1) +/* trace events for per class */ #define TRC_PM_FREQ_CHANGE (TRC_HW_PM + 0x01)#define TRC_PM_IDLE_ENTRY (TRC_HW_PM + 0x02)
Signature · 7 lines
1.8.3.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3] VT-d PI: track the vcpu number in pi blocking list
xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmfunc_intercept(struct cpu_user_regs *regs); · line 85
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -82,6 +82,7 @@ static int vmx_vmfunc_intercept(struct cpu_user_regs *regs);struct vmx_pi_blocking_vcpu {struct list_head list;spinlock_t lock; +uint32_t counter;
Is there any reason for this to be fixed width? Other than that the non-tracing parts look fine, but I really wonder whether the introduction of the counter and the tracing wouldn't better be split.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3] VT-d PI: track the vcpu number in pi blocking list
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmfunc_intercept(struct cpu_user_regs *regs); · line 85
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -82,6 +82,7 @@ static int vmx_vmfunc_intercept(struct cpu_user_regs *regs);struct vmx_pi_blocking_vcpu {struct list_head list;spinlock_t lock; +uint32_t counter;Is there any reason for this to be fixed width? Other than that the
I will use 'int' instead of 'uint32_t'.
Quoted 2 lines
non-tracing parts look fine, but I really wonder whether the introduction of the counter and the tracing wouldn't better be split.
Will split them apart.
Thanks
chao
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3] VT-d PI: track the vcpu number in pi blocking list
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 22.06.17 at 07:16, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
xen/arch/x86/hvm/vmx/vmx.c static int vmx_vmfunc_intercept(struct cpu_user_regs *regs); · line 85
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -82,6 +82,7 @@ static int vmx_vmfunc_intercept(struct cpu_user_regs *regs);struct vmx_pi_blocking_vcpu {struct list_head list;spinlock_t lock; +uint32_t counter;Is there any reason for this to be fixed width? Other than that the
Quoted 1 line
I will use 'int' instead of '
uint32_t'.
Can the counter go negative? I don't think so, in which case you really want to use "unsigned int".
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 2/3 vcpu: track hvm vcpu number on the system
In reply to Chao GaoThis number is used to calculate how many hvm vcpu on a pcpu on average. This counting is x86 specific.
--- xen/arch/x86/domain.c | 10 ++++++++++ xen/include/asm-x86/domain.h | 2 ++ 2 files changed, 12 insertions(+)
xen/arch/x86/domain.c +10 -0
@@ -66,6 +66,9 @@ DEFINE_PER_CPU(struct vcpu *, curr_vcpu); +/* how many hvm vcpu on this system? */ +atomic_t num_hvm_vcpus; +static void default_idle (void );void (*pm_idle) (void )__read_mostly = default_idle;void (*dead_idle) (void )__read_mostly = default_dead_idle;
@@ -467,7 +470,11 @@ int vcpu_initialise(struct vcpu *v)xfree (v->arch.pv_vcpu.trap_ctxt); }else if ( !is_idle_domain (v->domain) ) + {vpmu_initialise (v); +if (is_hvm_domain (v->domain) ) +atomic_inc (&num_hvm_vcpus); + }return rc; }
@@ -489,7 +496,10 @@ void vcpu_destroy(struct vcpu *v)vpmu_destroy (v);if (is_hvm_vcpu (v) ) + {hvm_vcpu_destroy (v); +atomic_dec (&num_hvm_vcpus); + }else xfree (v->arch.pv_vcpu.trap_ctxt); }
xen/include/asm-x86/domain.h +2 -0
@@ -31,6 +31,8 @@#define nmi_pending nmi_state.pending#define mce_pending mce_state.pending +extern atomic_t num_hvm_vcpus; +struct trap_bounce {uint32_t error_code;uint8_t flags;/* TBF_ */
Signature · 7 lines
1.8.3.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3 2/3] vcpu: track hvm vcpu number on the system
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
This number is used to calculate how many hvm vcpu on a pcpu on average.
This doesn't read well. Perhaps "This number is used to calculate the average vcpus per pcpu ratio"?
Quoted 1 line
This counting is x86 specific.
By titling the patch accordingly you wouldn't even need to say so here.
xen/arch/x86/domain.c line 69
Quoted 7 lines
xen/arch/x86/domain.c +1
-0@@ -66,6 +66,9 @@ DEFINE_PER_CPU(struct vcpu *, curr_vcpu); +/* how many hvm vcpu on this system? */
"Total number of HVM vCPU-s on this system" or some such.
xen/arch/x86/domain.c int vcpu_initialise(struct vcpu *v) · line 477
Quoted 9 lines
xen/arch/x86/domain.c +4
-0@@ -467,7 +470,11 @@ int vcpu_initialise(struct vcpu *v)xfree (v->arch.pv_vcpu.trap_ctxt); }else if ( !is_idle_domain (v->domain) ) + {vpmu_initialise (v); +if (is_hvm_domain (v->domain) ) +atomic_inc (&num_hvm_vcpus); + }
Please instead put this in the code block callinghvm_vcpu_initialise().
xen/arch/x86/domain.c void vcpu_destroy(struct vcpu *v) · line 502
Quoted 8 lines
xen/arch/x86/domain.c +3
-0@@ -489,7 +496,10 @@ void vcpu_destroy(struct vcpu *v)vpmu_destroy (v);if (is_hvm_vcpu (v) ) + {hvm_vcpu_destroy (v); +atomic_dec (&num_hvm_vcpus); + }
To mirror initialization behavior I think you want to decrement the counter before calling the function.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
PATCH v3 3/3 VT-d PI: restrict the vcpu number on a given pcpu
In reply to Chao GaoCurrently, a blocked vCPU is put in its pCPU's pi blocking list. If too many vCPUs are blocked on a given pCPU, it will incur that the list grows too long. After a simple analysis, there are 32k domains and 128 vcpu per domain, thus about 4M vCPUs may be blocked in one pCPU's PI blocking list. When a wakeup interrupt arrives, the list is traversed to find some specific vCPUs to wake them up. This traversal in that case would consume much time.
To mitigate this issue, this patch limits the vcpu number on a given pCPU, taking factors such as perfomance of common case, current hvm vcpu count and current pcpu count into consideration. With this method, for the common case, it works fast and for some extreme cases, the list length is under control.
The change in vmx_pi_unblock_vcpu() is for the following case:
vcpu is running -> try to block (this patch may change NSDT to another pCPU) but notification comes in time, thus the vcpu goes back to running station -> VM-entry (we should set NSDT again, reverting the change we make to NSDT in vmx_vcpu_block())
--- xen/arch/x86/hvm/vmx/vmx.c | 70 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 7 deletions(-)
xen/arch/x86/hvm/vmx/vmx.c +63 -7
@@ -100,16 +100,62 @@ void vmx_pi_per_cpu_init(unsigned int cpu)spin_lock_init (&per_cpu (vmx_pi_blocking, cpu).lock); } +/* +* By default, the local pcpu (means the one the vcpu is currently running on) +* is chosen as the destination of wakeup interrupt. But if the vcpu number of +* the pcpu exceeds a limit, another pcpu is chosen until we find a suitable +* one. +* +* Currently, choose (v_tot/p_tot) + K as the limit of vcpu count, where +* v_tot is the total number of hvm vcpus on the system, p_tot is the total +* number of pcpus in the system, and K is a fixed number. Experments shows +* the maximum time to wakeup a vcpu from a 128-entry blocking list is about +* 22us, which is tolerable. So choose 128 as the fixed number K. +* +* This policy makes sure: +* 1) for common cases, the limit won't be reached and the local pcpu is used +* which is beneficial to performance (at least, avoid an IPI when unblocking +* vcpu). +* 2) for the worst case, the blocking list length scales with the vcpu count +* divided by the pcpu count. +*/ +#define PI_LIST_FIXED_NUM 128 +#define PI_LIST_LIMIT (atomic_read(&num_hvm_vcpus) / num_online_cpus() + \ + PI_LIST_FIXED_NUM) + +static bool pi_over_limit (int count) +{ +/* Compare w/ constant first to save an atomic read in the common case */ +return ((count >PI_LIST_FIXED_NUM ) && + (count > (atomic_read (&num_hvm_vcpus) /num_online_cpus ()) + +PI_LIST_FIXED_NUM )); +} +static void vmx_vcpu_block (struct vcpu *v) {unsigned long flags; -unsigned int dest; +unsigned int dest, pi_cpu;spinlock_t *old_lock; -spinlock_t *pi_blocking_list_lock = - &per_cpu (vmx_pi_blocking, v->processor).lock;struct pi_desc *pi_desc = &v->arch.hvm_vmx.pi_desc; +spinlock_t *pi_blocking_list_lock; + + pi_cpu = v->processor; + retry: + pi_blocking_list_lock = &per_cpu (vmx_pi_blocking, pi_cpu).lock;spin_lock_irqsave (pi_blocking_list_lock, flags); +/* +* Since pi_cpu may now be one other than the one v is currently +* running on, check to make sure that it's still up. +*/ +if (unlikely ((!cpu_online (pi_cpu)) || +pi_over_limit (per_cpu (vmx_pi_blocking, pi_cpu).counter)) ) + { + pi_cpu =cpumask_cycle (pi_cpu, &cpu_online_map); +spin_unlock_irqrestore (pi_blocking_list_lock, flags); + goto retry; + } + old_lock =cmpxchg (&v->arch.hvm_vmx.pi_blocking.lock,NULL , pi_blocking_list_lock);
@@ -120,11 +166,11 @@ static void vmx_vcpu_block(struct vcpu *v)*/ ASSERT (old_lock ==NULL ); -per_cpu (vmx_pi_blocking, v->processor).counter++; -TRACE_4D (TRC_HVM_PI_LIST_ADD , v->domain->domain_id, v->vcpu_id, - v->processor,per_cpu (vmx_pi_blocking, v->processor).counter); +per_cpu (vmx_pi_blocking, pi_cpu).counter++; +TRACE_4D (TRC_HVM_PI_LIST_ADD , v->domain->domain_id, v->vcpu_id, pi_cpu, +per_cpu (vmx_pi_blocking, pi_cpu).counter);list_add_tail (&v->arch.hvm_vmx.pi_blocking.list, - &per_cpu (vmx_pi_blocking, v->processor).list); + &per_cpu (vmx_pi_blocking, pi_cpu).list);spin_unlock_irqrestore (pi_blocking_list_lock, flags);ASSERT (!pi_test_sn (pi_desc));
@@ -134,6 +180,13 @@ static void vmx_vcpu_block(struct vcpu *v)ASSERT (pi_desc->ndst == (x2apic_enabled ? dest :MASK_INSR (dest, PI_xAPIC_NDST_MASK))); +if (unlikely (pi_cpu != v->processor) ) + { + dest =cpu_physical_id (pi_cpu); +write_atomic (&pi_desc->ndst, + (x2apic_enabled ? dest :MASK_INSR (dest, PI_xAPIC_NDST_MASK))); + } +write_atomic (&pi_desc->nv, pi_wakeup_vector); }
@@ -163,6 +216,7 @@ static void vmx_pi_unblock_vcpu(struct vcpu *v)unsigned long flags;spinlock_t *pi_blocking_list_lock;struct pi_desc *pi_desc = &v->arch.hvm_vmx.pi_desc; +unsigned int dest =cpu_physical_id (v->processor);/* * Set 'NV' field back to posted_intr_vector, so the
@@ -170,6 +224,8 @@ static void vmx_pi_unblock_vcpu(struct vcpu *v)* it is running in non-root mode. */ write_atomic (&pi_desc->nv, posted_intr_vector); +write_atomic (&pi_desc->ndst, + x2apic_enabled ? dest :MASK_INSR (dest, PI_xAPIC_NDST_MASK)); pi_blocking_list_lock = v->arch.hvm_vmx.pi_blocking.lock;
Signature · 7 lines
1.8.3.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Re: [PATCH v3 3/3] VT-d PI: restrict the vcpu number on a given pcpu
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
Currently, a blocked vCPU is put in its pCPU's pi blocking list. If too many vCPUs are blocked on a given pCPU, it will incur that the list grows too long. After a simple analysis, there are 32k domains and 128 vcpu per domain, thus about 4M vCPUs may be blocked in one pCPU's PI blocking list. When a wakeup interrupt arrives, the list is traversed to find some specific vCPUs to wake them up. This traversal in that case would consume much time.
To mitigate this issue, this patch limits the vcpu number on a given pCPU,
This would be a bug, but I think it's the description which is wrong (or at least imprecise): You don't limit the number of vCPU-s _run_ on any pCPU, but those tracked on any pCPU-s blocking list. Please say so here to avoid confusion.
Quoted 8 lines
taking factors such as perfomance of common case, current hvm vcpu count and current pcpu count into consideration. With this method, for the common case, it works fast and for some extreme cases, the list length is under control.
The change in
vmx_pi_unblock_vcpu()is for the following case:
vcpu is running -> try to block (this patch may change NSDT to another pCPU) but notification comes in time, thus the vcpu
What does "but notification comes in time" mean?
Quoted 1 line
goes back to running station -> VM-entry (we should set NSDT again,
s/station/state/ ?
Quoted 1 line
reverting the change we make to NSDT in
vmx_vcpu_block())
Overall I'm not sure I really understand what you try to explain here.
Quoted 7 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -100,16 +100,62 @@ void vmx_pi_per_cpu_init(unsigned int cpu)spin_lock_init (&per_cpu (vmx_pi_blocking, cpu).lock); } +/*
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 113
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10
-0+* By default, the local pcpu (means the one the vcpu is currently running on) +* is chosen as the destination of wakeup interrupt. But if the vcpu number of +* the pcpu exceeds a limit, another pcpu is chosen until we find a suitable +* one. +* +* Currently, choose (v_tot/p_tot) + K as the limit of vcpu count, where +* v_tot is the total number of hvm vcpus on the system, p_tot is the total +* number of pcpus in the system, and K is a fixed number. Experments shows +* the maximum time to wakeup a vcpu from a 128-entry blocking list is about +* 22us, which is tolerable. So choose 128 as the fixed number K.
Giving and kind of absolute time value requires also stating on what hardware this was measured.
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 126
Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +12
-0+* This policy makes sure: +* 1) for common cases, the limit won't be reached and the local pcpu is used +* which is beneficial to performance (at least, avoid an IPI when unblocking +* vcpu). +* 2) for the worst case, the blocking list length scales with the vcpu count +* divided by the pcpu count. +*/ +#define PI_LIST_FIXED_NUM 128 +#define PI_LIST_LIMIT (atomic_read(&num_hvm_vcpus) / num_online_cpus() + \ + PI_LIST_FIXED_NUM) + +static bool pi_over_limit (int count)
Can a caller validly pass a negative argument? Otherwise unsigned int please.
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 128
Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +2
-0+{ +/* Compare w/ constant first to save an atomic read in the common case */
As an atomic read is just a normal read on x86, does this really matter?
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 131
Quoted 3 lines
xen/arch/x86/hvm/vmx/vmx.c +3
-0+return ((count >PI_LIST_FIXED_NUM ) && + (count > (atomic_read (&num_hvm_vcpus) /num_online_cpus ()) + +PI_LIST_FIXED_NUM ));
Right above you've #define-d PI_LIST_LIMIT - why do you open code it here? Also note that the outer pair of parentheses is pointless (and hampering readability).
Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +3
-3static void vmx_vcpu_block (struct vcpu *v) {unsigned long flags; -unsigned int dest; +unsigned int dest, pi_cpu;spinlock_t *old_lock; -spinlock_t *pi_blocking_list_lock = - &per_cpu (vmx_pi_blocking, v->processor).lock;struct pi_desc *pi_desc = &v->arch.hvm_vmx.pi_desc; +spinlock_t *pi_blocking_list_lock; +
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 151
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0+ pi_cpu = v->processor; + retry: + pi_blocking_list_lock = &per_cpu (vmx_pi_blocking, pi_cpu).lock;spin_lock_irqsave (pi_blocking_list_lock, flags); +/* +* Since pi_cpu may now be one other than the one v is currently +* running on, check to make sure that it's still up. +*/ + if ( unlikely((!cpu_online(pi_cpu)) ||
But this check comest to late then: You've already used per-CPU data of an offline CPU by the time you make it here. I'm also not you really need the lock here. A read_atomic() or the counter would suffice afaics (of course the writers then would need to use write_atomic() or add_sized()).
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 152
Quoted 1 line
xen/arch/x86/hvm/vmx/vmx.c +1
-0+ pi_over_limit(per_cpu(vmx_pi_blocking, pi_cpu).counter)) )
Indentation.
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 151
Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +2
-0+ { + pi_cpu =cpumask_cycle (pi_cpu, &cpu_online_map);
With this, how could the CPU be offline by the time you make it back to the check above.
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 156
Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +2
-0+spin_unlock_irqrestore (pi_blocking_list_lock, flags); + goto retry;
Please try (hard) to avoid the goto here (we generally accept its use only for keeping error handling reasonably simple).
xen/arch/x86/hvm/vmx/vmx.c static void vmx_vcpu_block(struct vcpu *v) · line 188
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +6
-0@@ -134,6 +180,13 @@ static void vmx_vcpu_block(struct vcpu *v)ASSERT (pi_desc->ndst == (x2apic_enabled ? dest :MASK_INSR (dest, PI_xAPIC_NDST_MASK))); +if (unlikely (pi_cpu != v->processor) ) + { + dest =cpu_physical_id (pi_cpu); +write_atomic (&pi_desc->ndst, + (x2apic_enabled ? dest :MASK_INSR (dest, PI_xAPIC_NDST_MASK))); + }
This kind of contradicts the ASSERT() visible in patch context above. I _think_ it is nevertheless correct, but it would be good to attach a comment here explaining this.
Also there's no point in parenthesizing the argument of a function call.
xen/arch/x86/hvm/vmx/vmx.c static void vmx_pi_unblock_vcpu(struct vcpu *v) · line 228
Quoted 6 lines
xen/arch/x86/hvm/vmx/vmx.c +2
-0@@ -170,6 +224,8 @@ static void vmx_pi_unblock_vcpu(struct vcpu *v)* it is running in non-root mode. */ write_atomic (&pi_desc->nv, posted_intr_vector); +write_atomic (&pi_desc->ndst, + x2apic_enabled ? dest :MASK_INSR (dest, PI_xAPIC_NDST_MASK));
The explanation of why this is needed should also be put here rather then (only) in the commit message.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 3/3] VT-d PI: restrict the vcpu number on a given pcpu
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
Currently, a blocked vCPU is put in its pCPU's pi blocking list. If too many vCPUs are blocked on a given pCPU, it will incur that the list grows too long. After a simple analysis, there are 32k domains and 128 vcpu per domain, thus about 4M vCPUs may be blocked in one pCPU's PI blocking list. When a wakeup interrupt arrives, the list is traversed to find some specific vCPUs to wake them up. This traversal in that case would consume much time.
Quoted 7 lines
Quoted 2 lines
To mitigate this issue, this patch limits the vcpu number on a given pCPU,
This would be a bug, but I think it's the description which is wrong (or at least imprecise): You don't limit the number of vCPU-s _run_ on any pCPU, but those tracked on any pCPU-s blocking list. Please say so here to avoid confusion.
Agree.
Quoted 6 lines
Quoted 6 lines
taking factors such as perfomance of common case, current hvm vcpu count and current pcpu count into consideration. With this method, for the common case, it works fast and for some extreme cases, the list length is under control.
Quoted 6 lines
Quoted 3 lines
The change in
vmx_pi_unblock_vcpu()is for the following case:
vcpu is running -> try to block (this patch may change NSDT to another pCPU) but notification comes in time, thus the vcpuWhat does "but notification comes in time" mean?
I mean when local_events_need_delivery() in vcpu_block() return true.
Quoted 8 lines
Quoted 1 line
goes back to running station -> VM-entry (we should set NSDT again,
s/station/state/ ?
Quoted 1 line
reverting the change we make to NSDT in
vmx_vcpu_block())Overall I'm not sure I really understand what you try to explain here.
Will put it above the related change.
I wanted to explain why we need this change if a vcpu can be added to a remote pcpu (means the vcpu isn't running on this pcpu).
a vcpu may go through the two different paths from calling vcpu_block() to VM-entry:
Path1: vcpu_block()->vmx_vcpu_block()->local_events_need_delivery(return true) -> vmx_pi_unblock_vcpu (during VM-entry)
Path2: vcpu_block()->vmx_vcpu_block()->local_events_need_delivery(return false) -> vmx_pi_switch_from() -> vmx_pi_switch_to() ->vmx_pi_unblock_vcpu (during VM-entry)
For migration a vcpu to another pcpu would lead to a incorrect pi_desc->ndst, vmx_pi_switch_to() re-assigns pi_desc->ndst. It was enough for Path1 (no one changed the pi_desc->ndst field and changed the binding between pcpu and vcpu) and Path2. But, now vmx_vcpu_block() would change pi_desc->ndst to another pcpu to receive wakeup interrupt. If local_events_need_delivery() returns true, we should correct pi_desc->ndst to current pcpu in vmx_pi_unblock_vcpu().
Quoted 38 lines
Quoted 8 lines
xen/arch/x86/hvm/vmx/vmx.c +1
-0@@ -100,16 +100,62 @@ void vmx_pi_per_cpu_init(unsigned int cpu)spin_lock_init (&per_cpu (vmx_pi_blocking, cpu).lock); } +/* xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 113
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +10
-0+* By default, the local pcpu (means the one the vcpu is currently running on) +* is chosen as the destination of wakeup interrupt. But if the vcpu number of +* the pcpu exceeds a limit, another pcpu is chosen until we find a suitable +* one. +* +* Currently, choose (v_tot/p_tot) + K as the limit of vcpu count, where +* v_tot is the total number of hvm vcpus on the system, p_tot is the total +* number of pcpus in the system, and K is a fixed number. Experments shows +* the maximum time to wakeup a vcpu from a 128-entry blocking list is about +* 22us, which is tolerable. So choose 128 as the fixed number K. Giving and kind of absolute time value requires also stating on what hardware this was measured.
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 126
Quoted 12 lines
xen/arch/x86/hvm/vmx/vmx.c +12
-0+* This policy makes sure: +* 1) for common cases, the limit won't be reached and the local pcpu is used +* which is beneficial to performance (at least, avoid an IPI when unblocking +* vcpu). +* 2) for the worst case, the blocking list length scales with the vcpu count +* divided by the pcpu count. +*/ +#define PI_LIST_FIXED_NUM 128 +#define PI_LIST_LIMIT (atomic_read(&num_hvm_vcpus) / num_online_cpus() + \ + PI_LIST_FIXED_NUM) + +static bool pi_over_limit (int count)Can a caller validly pass a negative argument? Otherwise unsigned int please.
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 128
Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +2
-0+{ +/* Compare w/ constant first to save an atomic read in the common case */ As an atomic read is just a normal read on x86, does this really matter?
agree.
Quoted 39 lines
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 131
Quoted 4 lines
xen/arch/x86/hvm/vmx/vmx.c +3
-0+return ((count >PI_LIST_FIXED_NUM ) && + (count > (atomic_read (&num_hvm_vcpus) /num_online_cpus ()) + +PI_LIST_FIXED_NUM ));Right above you've #define-d
PI_LIST_LIMIT- why do you open code it here? Also note that the outer pair of parentheses is pointless (and hampering readability).Quoted 11 lines
xen/arch/x86/hvm/vmx/vmx.c +3
-3static void vmx_vcpu_block (struct vcpu *v) {unsigned long flags; -unsigned int dest; +unsigned int dest, pi_cpu;spinlock_t *old_lock; -spinlock_t *pi_blocking_list_lock = - &per_cpu (vmx_pi_blocking, v->processor).lock;struct pi_desc *pi_desc = &v->arch.hvm_vmx.pi_desc; +spinlock_t *pi_blocking_list_lock; +xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 151
Quoted 10 lines
xen/arch/x86/hvm/vmx/vmx.c +8
-0+ pi_cpu = v->processor; + retry: + pi_blocking_list_lock = &per_cpu (vmx_pi_blocking, pi_cpu).lock;spin_lock_irqsave (pi_blocking_list_lock, flags); +/* +* Since pi_cpu may now be one other than the one v is currently +* running on, check to make sure that it's still up. +*/ + if ( unlikely((!cpu_online(pi_cpu)) ||But this check comest to late then: You've already used per-CPU data of an offline CPU by the time you make it here. I'm also not you really need the lock here. A
read_atomic()or the counter would suffice afaics (of course the writers then would need to usewrite_atomic()oradd_sized()).xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 152
Quoted 1 line
xen/arch/x86/hvm/vmx/vmx.c +1
-0+ pi_over_limit(per_cpu(vmx_pi_blocking, pi_cpu).counter)) )
Quoted 7 lines
Indentation.
xen/arch/x86/hvm/vmx/vmx.c void vmx_pi_per_cpu_init(unsigned int cpu) · line 151
Quoted 2 lines
xen/arch/x86/hvm/vmx/vmx.c +2
-0+ { + pi_cpu =cpumask_cycle (pi_cpu, &cpu_online_map);With this, how could the CPU be offline by the time you make it back to the check above.
Thanks to point it out. It would incur a bug.
I think we should do things like this:
IF pi_blocking_list of current pcpu doesn't over the limit:
add the vcpu to current pcpu.
ELSE
add the vcpu to another pcpu.
To add the vcpu to another pcpu, we should avoid concurrency with vmx_pi_desc_fixup(). Thus, a lock (e.g. remote_pi_list_lock) can solve this potential concurrency. Using this lock like below:
in vmx_vcpu_block():
IF pi_blocking_list of current pcpu doesn't over the limit:
add the vcpu to current pcpu
ELSE
acquire remote_pi_list_lock
choose another online pcpu (don't worry this pcpu would goes
offline for we hold the remote_pi_list_lock, which blocks calling vmx_pi_desc_fixup(), thus at least we can add this vcpu to the pi_blocking_list before cleanup)
add the vcpu to the chosen pcpu
release remote_pi_list_lock
in vmx_pi_desc_fixup():
acquire remote_pi_list_lock
...
release remote_pi_list_lock
Thanks
Chao
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 3/3] VT-d PI: restrict the vcpu number on a given pcpu
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.06.17 at 06:22, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Review
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
(quoted diff) +2
-0+ { + pi_cpu = cpumask_cycle(pi_cpu, &cpu_online_map);With this, how could the CPU be offline by the time you make it back to the check above.
Thanks to point it out. It would incur a bug.
I don't understand what you're trying to tell me here.
Quoted 6 lines
I think we should do things like this:
IF
pi_blocking_listof current pcpu doesn't over the limit:
add the vcpu to current pcpu.
ELSE
add the vcpu to another pcpu.
But that's what supposedly the patch already tries to do?
Quoted 2 lines
To add the vcpu to another pcpu, we should avoid concurrency with
vmx_pi_desc_fixup(). Thus, a lock (e.g.remote_pi_list_lock)
Please use names which actually exist in source code, or make clear what exactly you're referring to. Talking of remote_pi_list_lock, which I can't find any instance of, does not help the discussion, as you leave me guessing whose lock you mean to acquire.
Quoted 4 lines
can solve this potential concurrency. Using this lock like below:
in
vmx_vcpu_block():
Quoted 11 lines
IF
pi_blocking_listof current pcpu doesn't over the limit:
add the vcpu to current pcpu
ELSE
acquireremote_pi_list_lock
choose another online pcpu (don't worry this pcpu would goes
offline for we hold theremote_pi_list_lock, which blocks callingvmx_pi_desc_fixup(), thus at least we can add this vcpu to thepi_blocking_listbefore cleanup)
I can't see why you need to hold a lock to make sure a pCPU doesn't go offline - pCPU offlining happens in stop_machine context anyway.
Jan
Quoted 10 lines
add the vcpu to the chosen pcpu
releaseremote_pi_list_lockin
vmx_pi_desc_fixup():
acquireremote_pi_list_lock
...
releaseremote_pi_list_lockThanks
Chao
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 3/3] VT-d PI: restrict the vcpu number on a given pcpu
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 23.06.17 at 06:22, <[email protected]> wrote:
Quoted 4 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
(quoted diff) +2
-0+ { + pi_cpu = cpumask_cycle(pi_cpu, &cpu_online_map);
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
With this, how could the CPU be offline by the time you make it back to the check above.
Thanks to point it out. It would incur a bug.
I don't understand what you're trying to tell me here.
I agree with you that we might use an offline CPU's structure. and I treat it as a bug.
Quoted 8 lines
Quoted 6 lines
I think we should do things like this:
IF
pi_blocking_listof current pcpu doesn't over the limit:
add the vcpu to current pcpu.
ELSE
add the vcpu to another pcpu.But that's what supposedly the patch already tries to do?
yes. it is. but I want to unclose a key problem here is we may add a vcpu to an offline pcpu's pi blocking list. Try to avoid this we can introduce a _new_ lock to avoid racing with vmx_pi_desc_fixup(). This version tried to use existing lock ie. per_cpu(vmx_pi_blocking, pi_cpu).lock to solve the key problem. But as you pointed out, it may use a offline CPU's structure.
Quoted 9 lines
Quoted 3 lines
To add the vcpu to another pcpu, we should avoid concurrency with
vmx_pi_desc_fixup(). Thus, a lock (e.g.remote_pi_list_lock)Please use names which actually exist in source code, or make clear what exactly you're referring to. Talking of
remote_pi_list_lock, which I can't find any instance of, does not help the discussion, as you leave me guessing whose lock you mean to acquire.
It is a new lock and I try to envision how it can take effect.
Quoted 17 lines
Quoted 5 lines
can solve this potential concurrency. Using this lock like below:
in
vmx_vcpu_block():Quoted 11 lines
IF
pi_blocking_listof current pcpu doesn't over the limit:
add the vcpu to current pcpu
ELSE
acquireremote_pi_list_lock
choose another online pcpu (don't worry this pcpu would goes
offline for we hold theremote_pi_list_lock, which blocks callingvmx_pi_desc_fixup(), thus at least we can add this vcpu to thepi_blocking_listbefore cleanup)
Quoted 2 lines
I can't see why you need to hold a lock to make sure a pCPU doesn't go offline - pCPU offlining happens in
stop_machinecontext anyway.
In cpu_down(), the cpu_online_bitmap is cleared by this line:
if ( (err = stop_machine_run(take_cpu_down, NULL, cpu)) < 0 )
And vmx_pi_desc_fixup() is called later by
notifier_call_chain(&cpu_chain, CPU_DEAD, hcpu, NULL);
So I think if we acquire the new lock, namely remote_pi_list_lock before choose another online pcpu, we can make sure the vcpu will be added to the chosen pcpu's pi blocking list prior to some cleanup to the same pi blocking list.
Thanks
Chao
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Re: [PATCH v3 3/3] VT-d PI: restrict the vcpu number on a given pcpu
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 23.06.17 at 10:33, <[email protected]> wrote:
Quoted 9 lines · Jan Beulich
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 23.06.17 at 06:22, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Review
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 24.05.17 at 08:56, <[email protected]> wrote:
(quoted diff) +2
-0+ { + pi_cpu = cpumask_cycle(pi_cpu, &cpu_online_map);With this, how could the CPU be offline by the time you make it back to the check above.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Thanks to point it out. It would incur a bug.
I don't understand what you're trying to tell me here.
I agree with you that we might use an offline CPU's structure. and I treat it as a bug.
Ah, but I didn't say so. Instead I questioned how you would see the pCPU going offline between the code above and the use of pi_cpu further down. Most or all of what you say later in your reply seems inapplicable because of this apparent misunderstanding, so I'm not going to further comment on that.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel