TPR write optimization
For anyone who is interested, I have implemented TPR write optimization based on Travis Betak's amdvopt driver. My implementation gets the base address and length of hal.dll when loaded into memory and scans that for patchable instructions, instead of the patch table approach that Travis used.
A preliminary test under XP shows a 10x increase in iperf performance.
I'll test under 2003sp1 soon, unfortunately my sp1 decided to automatically install sp2 and I 'xm destroy'd it in the middle of that and now it's doing a chkdsk...
James
RE: TPR write optimization (even improves 2003 sp2)
Quoted 2 lines
For anyone who is interested, I have implemented TPR write
optimization
Quoted 1 line
based on Travis Betak's amdvopt driver. My implementation gets the
base
Quoted 1 line
address and length of hal.dll when loaded into memory and scans that
for
Quoted 1 line
patchable instructions, instead of the patch table approach that
Travis
Quoted 8 lines
used.
A preliminary test under XP shows a 10x increase in iperf performance.
I'll test under 2003sp1 soon, unfortunately my sp1 decided to automatically install sp2 and I 'xm destroy'd it in the middle of that and now it's doing a chkdsk...
2003sp1 iperf throughput went from 150mbit/second without the patch to 950mbit/second with the patch
Surprisingly, the AMD TPR write patch under 2003sp2 increases iperf throughput from 1Gbit/second (without the patch) to 1.5Gbit/second (patched). I guess this makes sense - 2003sp2 includes 'lazy write', but the AMD patch optimises the write operation itself.
Is there such a facility available under Intel CPU's, other than the latest 'flex priority' stuff which I am about to read up on?
Thanks
James
Re: TPR write optimization (even improves 2003 sp2)
Quoted 2 lines · James Harper
Is there such a facility available under Intel CPU's, other than the latest 'flex priority' stuff which I am about to read up on?
The LOCK MOV CR0 trick doesn't work on Intel CPUs. You should have space to patch in a JMP instruction though, and then you can shadow the TPR value and only do actual writes when the value changes.
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Quoted 2 lines
On 07/01/2009 02:59, "James Harper" <[email protected]>
Quoted 5 lines · wrote:
Quoted 3 lines
Is there such a facility available under Intel CPU's, other than the latest 'flex priority' stuff which I am about to read up on?
The LOCK MOV CR0 trick doesn't work on Intel CPUs. You should have
space
Quoted 3 lines
to patch in a JMP instruction though, and then you can shadow the TPR value and only do actual writes when the value changes.
Travis said that the testing he did showed that the value changed every write. The 'lazy' bit works by only writing when it actually makes a difference (eg don't write TPR until there is an actual interrupt pending I think). It's (probably much) less efficient for the case when a lower priority interrupt interrupts what should be a higher priority interrupt (but isn't, because we haven't written TPR yet), but is much more efficient for the most common case where it doesn't get interrupted. That's quite a bit more complicated than 'if new_tpr == old_tpr then write else nop'...
I'm trying to get xentrace working to get visibility to the TPR writes so I can see for myself, but I don't think I am seeing what I want to see. Any suggestions on that? (see previous email with an incorrect subject of 'xentrace and MSR_READ/MSR_WRITE')
How does flex priority work - does it require any work on the DomU side, or does it 'just work'?
Does the VMExit caused by writing the TPR amount to the same sort of overhead as a hypercall? (eg replacing TPR writes with hypercalls is not going to make any difference is it?)
James
Re: TPR write optimization (even improves 2003 sp2)
Quoted 9 lines · James Harper
Travis said that the testing he did showed that the value changed every write. The 'lazy' bit works by only writing when it actually makes a difference (eg don't write TPR until there is an actual interrupt pending I think). It's (probably much) less efficient for the case when a lower priority interrupt interrupts what should be a higher priority interrupt (but isn't, because we haven't written TPR yet), but is much more efficient for the most common case where it doesn't get interrupted. That's quite a bit more complicated than 'if
new_tpr==old_tprthen write else nop'...
Hmmm, you could be right. I suppose xentrace can confirm one way or the other once you have it set up...
Quoted 4 lines
I'm trying to get xentrace working to get visibility to the TPR writes so I can see for myself, but I don't think I am seeing what I want to see. Any suggestions on that? (see previous email with an incorrect subject of 'xentrace and MSR_READ/MSR_WRITE')
George Dunlap should be able to advise.
Quoted 2 lines
How does flex priority work - does it require any work on the DomU side, or does it 'just work'?
It's completely transparent and just works.
Quoted 3 lines
Does the VMExit caused by writing the TPR amount to the same sort of overhead as a hypercall? (eg replacing TPR writes with hypercalls is not going to make any difference is it?)
Very little. As you say, avoiding the VMEXITs entirely is the key.
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
On 07/01/2009 09:14, "James Harper" <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
Travis said that the testing he did showed that the value changed
every
Quoted 3 lines
Quoted 3 lines
write. The 'lazy' bit works by only writing when it actually makes a difference (eg don't write TPR until there is an actual interrupt pending I think). It's (probably much) less efficient for the case
when
Quoted 1 line
Quoted 1 line
a lower priority interrupt interrupts what should be a higher
priority
Quoted 1 line
Quoted 1 line
interrupt (but isn't, because we haven't written TPR yet), but is
much
Quoted 5 lines
Quoted 3 lines
more efficient for the most common case where it doesn't get interrupted. That's quite a bit more complicated than 'if
new_tpr==old_tprthen write else nop'...Hmmm, you could be right. I suppose xentrace can confirm one way or
the
Quoted 2 lines
other once you have it set up...
I am probably showing a lack of understanding about how the APIC works here, but from a very brief look at what writing to the TPR register does, all I can see is that it sets a value in vlapic->regs->data. Could I just map a page from the DomU into xen and assign a byte in there as the TPR register (one byte per CPU). I would need a new hypercall to turn this on but that's no big deal. Or is there more to a TPR write than just setting vlapic->regs->data?
Thanks
James
Re: TPR write optimization (even improves 2003 sp2)
Quoted 5 lines · James Harper
Quoted 1 line
Hmmm, you could be right. I suppose xentrace can confirm one way or
the
Quoted 2 lines
other once you have it set up...
Quoted 7 lines
I am probably showing a lack of understanding about how the APIC works here, but from a very brief look at what writing to the TPR register does, all I can see is that it sets a value in
vlapic->regs->data. Could I just map a page from the DomU into xen and assign a byte in there as the TPR register (one byte per CPU). I would need a new hypercall to turn this on but that's no big deal. Or is there more to a TPR write than just settingvlapic->regs->data?
Sometimes it triggers an interrupt to be delivered. So you'd need a TPR threshold too to cause a VMEXIT sometimes. Or perhaps map the whole APIC page and work it out yourself (possibly the Citrix drivers do this, but I'm not really familiar with them).
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
Quoted 1 line
I am probably showing a lack of understanding about how the APIC
works
Quoted 2 lines
Quoted 2 lines
here, but from a very brief look at what writing to the TPR register does, all I can see is that it sets a value in
vlapic->regs->data.
Could
Quoted 1 line
Quoted 1 line
I just map a page from the DomU into xen and assign a byte in there
as
Quoted 5 lines
Quoted 3 lines
the TPR register (one byte per CPU). I would need a new hypercall to turn this on but that's no big deal. Or is there more to a TPR write than just setting
vlapic->regs->data?Sometimes it triggers an interrupt to be delivered. So you'd need a
TPR
Quoted 1 line
threshold too to cause a VMEXIT sometimes. Or perhaps map the whole
APIC
Quoted 1 line
page and work it out yourself (possibly the Citrix drivers do this,
but
Quoted 1 line
I'm not really familiar with them).
I can't see anything that would trigger an interrupt beyond vlapic_write(). Is it just the act of invoking a VMEXIT that triggers the interrupt delivery?
Quoted 1 line
From memory, there is a register somewhere that says what the highest
interrupt level that is currently blocked is... I'll look at that. This doesn't actually sound that hard...
James
Re: TPR write optimization (even improves 2003 sp2)
Quoted 11 lines · James Harper
Quoted 1 line
Sometimes it triggers an interrupt to be delivered. So you'd need a
TPR
Quoted 1 line
threshold too to cause a VMEXIT sometimes. Or perhaps map the whole
APIC
Quoted 1 line
page and work it out yourself (possibly the Citrix drivers do this,
but
Quoted 1 line
I'm not really familiar with them).
I can't see anything that would trigger an interrupt beyond
vlapic_write(). Is it just the act of invoking a VMEXIT that triggers the interrupt delivery?
Yes. See vmx_intr_assist().
Quoted 3 lines
From memory, there is a register somewhere that says what the highest interrupt level that is currently blocked is... I'll look at that. This doesn't actually sound that hard...
An interrupt is delivered if:
highest_prio(IRR) > max(highest_prio(ISR), TPR).
Where highest_prio() is based on a backwards bitmap scan.
If you go down the route of setting a TPR threshold for vmexit, that would be (hp = highest_prio):
TPR_thresh = ((hp(IRR) > hp(ISR)) && (hp(IRR) < TPR)) ? hp(IRR) : 0
Where VMEXIT occurs if TPR is updated to a value less than the threshold.
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
On 07/01/2009 10:05, "James Harper" <[email protected]>
Quoted 11 lines · wrote:
Quoted 11 lines
Quoted 2 lines
Sometimes it triggers an interrupt to be delivered. So you'd need a
TPR
Quoted 1 line
threshold too to cause a VMEXIT sometimes. Or perhaps map the whole
APIC
Quoted 1 line
page and work it out yourself (possibly the Citrix drivers do this,
but
Quoted 1 line
I'm not really familiar with them).
I can't see anything that would trigger an interrupt beyond
vlapic_write(). Is it just the act of invoking a VMEXIT that
triggers
Quoted 3 lines
Quoted 1 line
the interrupt delivery?
Yes. See
vmx_intr_assist().
I can see the vmx_intr_assist function, but I can't find what calls it.
Is vmx code for both amd and intel? Or is it vmx=intel and svm=amd? I've never got my head around those terms...
Quoted 1 line
Quoted 1 line
From memory, there is a register somewhere that says what the
highest
Quoted 1 line
Quoted 1 line
interrupt level that is currently blocked is... I'll look at that.
This
Quoted 8 lines
Quoted 1 line
doesn't actually sound that hard...
An interrupt is delivered if:
highest_prio(IRR)>max(highest_prio(ISR), TPR).Where
highest_prio()is based on a backwards bitmap scan.If you go down the route of setting a TPR threshold for vmexit, that
would
Quoted 4 lines
be (hp =
highest_prio):
TPR_thresh= ((hp(IRR) > hp(ISR)) && (hp(IRR) < TPR)) ? hp(IRR) : 0Where VMEXIT occurs if TPR is updated to a value less than the
threshold.
Quoted 1 line
Hmmm... if I do things in the right order I should be able to avoid races.
James
RE: TPR write optimization (even improves 2003 sp2)
Quoted 4 lines
Quoted 2 lines
Yes. See
vmx_intr_assist().I can see the
vmx_intr_assistfunction, but I can't find what calls
it.
Quoted 1 line
Disregard. I wasn't grepping .S files :)
James
Re: TPR write optimization (even improves 2003 sp2)
In reply to James HarperQuoted 6 lines · James Harper
Quoted 1 line
Yes. See
vmx_intr_assist().I can see the
vmx_intr_assistfunction, but I can't find what calls it.Is vmx code for both amd and intel? Or is it vmx=intel and svm=amd? I've never got my head around those terms...
svm=amd, vmx=intel. There's a svm_intr_assist() too.
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Looking through vlapic.c some more, the vlapic regs are stored on a discrete page which lends itself to mapping into a DomU as has been suggested previously.
I think I could do this based on imitating what XENMAPSPACE_shared_info does and create a new hypercall to map each vcpu's vlapic regs to an mfn given by the DomU, probably on a cpu by cpu basis. Then I would patch windows to read and write tpr from this new space instead of the real vlapic mmio space, and based on the tpr threshold (as per your previous email) do a VMEXIT only when necessary.
Should this be a new hypercall, or could I add a new XENMAPSPACE_ function to the existing XENMEM_add_to_physmap hypercall (eg am I allowed to add HVM related code here from a design point of view).
James
RE: TPR write optimization (even improves 2003 sp2)
Quoted 6 lines
Looking through
vlapic.csome more, the vlapic regs are stored on a discrete page which lends itself to mapping into a DomU as has been suggested previously.I think I could do this based on imitating what
XENMAPSPACE_shared_info
Quoted 1 line
does and create a new hypercall to map each vcpu's vlapic regs to an
mfn
Quoted 3 lines
given by the DomU, probably on a cpu by cpu basis. Then I would patch windows to read and write tpr from this new space instead of the real vlapic mmio space, and based on the tpr threshold (as per your
previous
Quoted 6 lines
email) do a VMEXIT only when necessary.
Should this be a new hypercall, or could I add a new
XENMAPSPACE_function to the existingXENMEM_add_to_physmaphypercall (eg am I allowed to add HVM related code here from a design point of view).
I just quickly implemented this as a 'proof of concept' by adding a XENMAPSPACE_vlapic_regs function to XENMEM_add_to_physmap, and it works, but only after I removed regs_page from the vlapic struct and used alloc_xenheap_page instead of alloc_domheap_page and then share_xen_page_with_guest. Can I do it with alloc_domheap_page? It didn't 'just work' when I did it that way... (and I had to comment out the use of regs_page in the vmx code to get it to compile, so as it stands it probably wouldn't work on Intel).
Anyway, in my xp guest when I KeRaiseIrql and then read the mapped vlapic->regs from xp I see that the TPR register is changing, so things are looking good.
Next will be to patch windows to:
. On read, just read from the mapped space instead of the mmio space for at least the TPR register, and maybe others if there are potential performance gains
. On write, write the value to the mapped space, then check if an interrupt could be pending (as per Keirs algorithm in a previous email), and if so, write to the mmio space to force a VMEXIT (or use some other way of invoking a VMEXIT if it's faster...). By doing the mapped write first before checking for pending interrupts I should avoid any races...
James
RE: TPR write optimization (even improves 2003 sp2)
Quoted 10 lines
Quoted 6 lines
Looking through
vlapic.csome more, the vlapic regs are stored on a discrete page which lends itself to mapping into a DomU as has been suggested previously.I think I could do this based on imitating what
XENMAPSPACE_shared_infoQuoted 1 line
does and create a new hypercall to map each vcpu's vlapic regs to an
mfn
Quoted 1 line
given by the DomU, probably on a cpu by cpu basis. Then I would
patch
Quoted 1 line
Quoted 1 line
windows to read and write tpr from this new space instead of the
real
Quoted 11 lines
Quoted 1 line
vlapic mmio space, and based on the tpr threshold (as per your
previous
Quoted 6 lines
email) do a VMEXIT only when necessary.
Should this be a new hypercall, or could I add a new
XENMAPSPACE_function to the existingXENMEM_add_to_physmaphypercall (eg am I allowed to add HVM related code here from a design point of view).I just quickly implemented this as a 'proof of concept' by adding a
XENMAPSPACE_vlapic_regsfunction toXENMEM_add_to_physmap, and it
works,
Quoted 9 lines
but only after I removed
regs_pagefrom the vlapic struct and usedalloc_xenheap_pageinstead ofalloc_domheap_pageand thenshare_xen_page_with_guest. Can I do it withalloc_domheap_page? It didn't 'just work' when I did it that way... (and I had to comment out the use ofregs_pagein the vmx code to get it to compile, so as it stands it probably wouldn't work on Intel).Anyway, in my xp guest when I KeRaiseIrql and then read the mapped
vlapic->regsfrom xp I see that the TPR register is changing, so
things
Quoted 4 lines
are looking good.
Next will be to patch windows to:
. On read, just read from the mapped space instead of the mmio space
for
Quoted 4 lines
at least the TPR register, and maybe others if there are potential performance gains
. On write, write the value to the mapped space, then check if an interrupt could be pending (as per Keirs algorithm in a previous
email),
Quoted 1 line
and if so, write to the mmio space to force a VMEXIT (or use some
other
Quoted 2 lines
way of invoking a VMEXIT if it's faster...). By doing the mapped write first before checking for pending interrupts I should avoid any
races...
Quoted 1 line
One (big?) hiccup... how do I make sure that I am accessing the vlapic structure for the correct CPU? My current idea of vlapic[current_cpu] is flawed because a thread switch could occur between getting the address of the vlapic structure for the current cpu and writing a value to the mapped register.
Is it possible to have per-cpu mapping?
Does disabling interrupts via cli cause a VMEXIT?
James
Re: TPR write optimization (even improves 2003 sp2)
Quoted 7 lines · James Harper
One (big?) hiccup... how do I make sure that I am accessing the vlapic structure for the correct CPU? My current idea of vlapic[current_cpu] is flawed because a thread switch could occur between getting the address of the vlapic structure for the current cpu and writing a value to the mapped register.
Is it possible to have per-cpu mapping?
No.
Quoted 1 line
Does disabling interrupts via cli cause a VMEXIT?
No.
K.
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
On 08/01/2009 03:53, "James Harper" <[email protected]>
Quoted 2 lines · wrote:
Quoted 2 lines
One (big?) hiccup... how do I make sure that I am accessing the
vlapic
Quoted 1 line
Quoted 1 line
structure for the correct CPU? My current idea of
vlapic[current_cpu] is
Quoted 1 line
Quoted 1 line
flawed because a thread switch could occur between getting the
address
Quoted 1 line
Quoted 1 line
of the vlapic structure for the current cpu and writing a value to
the
Quoted 10 lines
Quoted 3 lines
mapped register.
Is it possible to have per-cpu mapping?
No.
Quoted 1 line
Does disabling interrupts via cli cause a VMEXIT?
No.
Hmmm... so I could use CLI to disable interrupts to ensure that I'll remain on the same CPU. This idea isn't totally sunk then...
James
Re: TPR write optimization (even improves 2003 sp2)
In reply to James HarperQuoted 8 lines · James Harper
I just quickly implemented this as a 'proof of concept' by adding a
XENMAPSPACE_vlapic_regsfunction toXENMEM_add_to_physmap, and it works, but only after I removedregs_pagefrom the vlapic struct and usedalloc_xenheap_pageinstead ofalloc_domheap_pageand thenshare_xen_page_with_guest. Can I do it withalloc_domheap_page? It didn't 'just work' when I did it that way... (and I had to comment out the use ofregs_pagein the vmx code to get it to compile, so as it stands it probably wouldn't work on Intel).
Using alloc_xenheap_page would be okay.
Quoted 4 lines
Anyway, in my xp guest when I KeRaiseIrql and then read the mapped
vlapic->regsfrom xp I see that the TPR register is changing, so things are looking good.
Quoted 9 lines
Next will be to patch windows to:
. On read, just read from the mapped space instead of the mmio space for at least the TPR register, and maybe others if there are potential performance gains
. On write, write the value to the mapped space, then check if an interrupt could be pending (as per Keirs algorithm in a previous email), and if so, write to the mmio space to force a VMEXIT (or use some other way of invoking a VMEXIT if it's faster...). By doing the mapped write first before checking for pending interrupts I should avoid any races...
Yes, sounds okay.
K.
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
On 08/01/2009 00:40, "James Harper" <[email protected]>
Quoted 3 lines · wrote:
Quoted 3 lines
I just quickly implemented this as a 'proof of concept' by adding a
XENMAPSPACE_vlapic_regsfunction toXENMEM_add_to_physmap, and it
works,
Quoted 4 lines
Quoted 4 lines
but only after I removed
regs_pagefrom the vlapic struct and usedalloc_xenheap_pageinstead ofalloc_domheap_pageand thenshare_xen_page_with_guest. Can I do it withalloc_domheap_page? It didn't 'just work' when I did it that way... (and I had to comment
out
Quoted 4 lines
Quoted 2 lines
the use of
regs_pagein the vmx code to get it to compile, so as it stands it probably wouldn't work on Intel).Using
alloc_xenheap_pagewould be okay.
Is it possible to map a page allocated with alloc_domheap_page into the domain? The semantics must be different as it didn't work the way I expected it to...
James
Re: TPR write optimization (even improves 2003 sp2)
Quoted 5 lines · James Harper
Quoted 1 line
Using
alloc_xenheap_pagewould be okay.Is it possible to map a page allocated with
alloc_domheap_pageinto the domain? The semantics must be different as it didn't work the way I expected it to...
It wouldn't be easy. Just use alloc_xenheap_page(). You can get rid of the map_domain_page_global() then, too.
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
On 08/01/2009 09:09, "James Harper" <[email protected]>
Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 2 lines
Using
alloc_xenheap_pagewould be okay.Is it possible to map a page allocated with
alloc_domheap_pageinto
the
Quoted 4 lines
Quoted 2 lines
domain? The semantics must be different as it didn't work the way I expected it to...
It wouldn't be easy. Just use
alloc_xenheap_page(). You can get rid of
the
Quoted 2 lines
map_domain_page_global()then, too.
Okay. I might need assistance with changing the vmx use of the vlapic page as it now needs to use xenheap semantics instead of domheap semantics. I'll try and work it out myself first though.
Thanks
James
Re: TPR write optimization (even improves 2003 sp2)
In reply to James HarperQuoted 5 lines · James Harper
Quoted 1 line
Using
alloc_xenheap_pagewould be okay.Is it possible to map a page allocated with
alloc_domheap_pageinto the domain? The semantics must be different as it didn't work the way I expected it to...
OTOH I think Intel's FlexPriority depends on us using alloc_domheap_pages(), so we may have an issue both ways round here. The other issue with giving the guest write access to the APIC page is whether Xen can deal with random crap being written over it. Might need to audit vlapic.c for that.
-- Keir
RE: TPR write optimization (even improves 2003 sp2)
Quoted 1 line
On 08/01/2009 09:09, "James Harper" <[email protected]>
Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 2 lines
Using
alloc_xenheap_pagewould be okay.Is it possible to map a page allocated with
alloc_domheap_pageinto
the
Quoted 6 lines
Quoted 2 lines
domain? The semantics must be different as it didn't work the way I expected it to...
OTOH I think Intel's FlexPriority depends on us using
alloc_domheap_pages(),
so we may have an issue both ways round here.
I was afraid of that - I guess that's where the vmx side of things uses the regs_page.
Quoted 4 lines
The other issue with giving
the guest write access to the APIC page is whether Xen can deal with random
crap being written over it. Might need to auditvlapic.cfor that.
Are you concerned about an errant (or malicious) DomU crashing the vlapic code (and therefore Xen)? That was something I hadn't thought of...
James
RE: TPR write optimization (even improves 2003 sp2)
In reply to Keir FraserQuoted 1 line
Quoted 1 line
From memory, there is a register somewhere that says what the
highest
Quoted 1 line
Quoted 1 line
interrupt level that is currently blocked is... I'll look at that.
This
Quoted 8 lines
Quoted 1 line
doesn't actually sound that hard...
An interrupt is delivered if:
highest_prio(IRR)>max(highest_prio(ISR), TPR).Where
highest_prio()is based on a backwards bitmap scan.If you go down the route of setting a TPR threshold for vmexit, that
would
Quoted 4 lines
be (hp =
highest_prio):
TPR_thresh= ((hp(IRR) > hp(ISR)) && (hp(IRR) < TPR)) ? hp(IRR) : 0Where VMEXIT occurs if TPR is updated to a value less than the
threshold.
Quoted 1 line
I really need to get xentrace working... if there are lots of reads and writes to the IRR and ISR registers that all involve VMEXIT's then it may well be worth mapping the entire APIC register range into the DomU's memory space (somehow).
Otherwise I can make sure that each write to IRR and ISR (by xen or domU) gets echo's into DomU's memory in such a form that I can calculate the value of TPR_thresh that you have given above in my kernel patch and VMEXIT when necessary from there...
I think I should figure out why xentrace isn't telling me what I want to know as there are too many conditionals at the moment to know what the right solution is...
James