Skip to contentSkip to conversation

removing shadow TLB

DISCUSSION Hollis Blanchard 2 participants 4 messages Sep 9, 2008 – Sep 11, 2008

lore
Share & tools
lore
#0 lore raw

I've been considering if keeping the shadow TLB data structure around is the right approach. To begin with, Linux does not keep an analogous structure, and it's basically the same problem: when resuming the guest after a context switch, the TLB will be mostly empty of guest entries, and suffer a high TLB miss rate until it can fault more back in.

The shadow TLB allows us to skip the repopulation phase, but at the expense of some memory and some overhead on every exit. Also, it's difficult or impossible to implement a shadow TLB on cores where software cannot address TLB entries by index (i.e. the hardware automatically selects the index).

If we did remove the shadow TLB, I think we'd suffer even more TLB misses, so we'd need to really optimize our TLB miss handler. (Arguably we should already be doing that anyways.) Linux's TLB miss handler walks the Linux page tables entirely in assembly, which also removes the need to save/restore the full C ABI set of GPRs.

Logically speaking, our TLB miss handler must do the following:
1. walk guest TLB(s) to find a matching entry
2. if not present, deliver fault to guest
3. if present
1. calculate guest physical address from TLB entry
2. check if the guest is allowed to access that address
3. if yes, write new TLB entry into hardware
4. if no, deliver fault to userspace

I was hoping to be able implement 1 through 3.3 in assembly. Doing that would require hoisting some of the logic currently in kvmppc_mmu_map() to execute before the page fault occurs, mostly because it's implemented in C. In particular we need kvm_is_visible_gfn(), and most unfortunately gfn_to_pfn(). I started moving the logic around, but gfn_to_pfn() has me stuck because calling that on *all* gfns at memory registration time would a) pin all guest memory all the time, and b) require us to keep a large data structure to remember all the pfns.

Avi suggested that we could keep a cache of "gotten" pfns (gfn_to_pfn() calls get_user_pages()). That could obviate both C calls, but now I'm not sure it's worth the development complexity.

I'll have to keep thinking about those other cores without directly index-able TLBs.

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#1 lore raw
Quoted 8 lines

I've been considering if keeping the shadow TLB data structure around is
the right approach. To begin with, Linux does not keep an analogous structure, and it's basically the same problem: when resuming the guest
after a context switch, the TLB will be mostly empty of guest entries, and suffer a high TLB miss rate until it can fault more back in.

If we remove the shadow TLB, can we improve this?
In kvm booke implementation, after a host context switch, I think the TLB should be invalidated unless the core won't switch to another vcpu.

Quoted 6 lines

The shadow TLB allows us to skip the repopulation phase, but at the expense of some memory and some overhead on every exit. Also, it's difficult or impossible to implement a shadow TLB on cores where software cannot address TLB entries by index (i.e. the hardware automatically selects the index).

Shadow TLB also avoid recalculation, the value of shadow tlb entry can be directly written into hardware.
And get and put page can be save as well.

btw, why do we need to update TLB on every exit, why can't we just update it on miss?

Quoted 3 lines

If we did remove the shadow TLB, I think we'd suffer even more TLB misses, so we'd need to really optimize our TLB miss handler.

Could you give more pros. about removing the shadow TLB? To make TLB handler can fit more processors?

Quoted 6 lines

(Arguably
we should already be doing that anyways.) Linux's TLB miss handler walks
the Linux page tables entirely in assembly, which also removes the need
to save/restore the full C ABI set of GPRs.

Linux's TLB miss handler handles page table which is simple to address (the input argument is entry's address),
while our handler handles tlb entry table which may be complex to address (the input argument is entry's tag).

Quoted 3 lines

Logically speaking, our TLB miss handler must do the following:
1. walk guest TLB(s) to find a matching entry

If we implement large guest TLB in the future,
we might need to involve advanced data structure such as rb-tree.

#2 lore raw
In reply to Hollis Blanchard
Quoted 6 lines · On Wed, 2008-09-10 at 10:54 +0800, Liu Yu-B13201 wrote:
Quoted 5 lines

The shadow TLB allows us to skip the repopulation phase, but at the expense of some memory and some overhead on every exit. Also, it's difficult or impossible to implement a shadow TLB on cores where software cannot address TLB entries by index (i.e. the hardware automatically selects the index).

Quoted 6 lines

Shadow TLB also avoid recalculation, the value of shadow tlb entry can be directly written into hardware.
And get and put page can be save as well.

btw, why do we need to update TLB on every exit, why can't we just update it on miss?

Linux can actually modify the host TLB at any time. For example, if Linux gets a fault touching vmalloc'ed memory, or printk faults trying to touch the UART, a new host TLB entry will be inserted without the knowledge of KVM.

If that happens though, it's just clobbered a guest TLB entry, so we put it back. Ideally we don't want to reload the *entire* TLB, only entries that the host has changed, but I think that optimization is not yet implemented. That's what this comment is about:

	/* Save the host's non-pinned TLB mappings, and load the guest mappings
	 * over them. Leave the host's "pinned" kernel mappings in place. */
	/* XXX optimization: use generation count to avoid swapping unmodified
	 * entries. */
Quoted 5 lines
Quoted 2 lines

If we did remove the shadow TLB, I think we'd suffer even more TLB misses, so we'd need to really optimize our TLB miss handler.

Could you give more pros. about removing the shadow TLB? To make TLB handler can fit more processors?

Yeah, to more easily adapt the 440 handler to similar cores I guess.

Well, also I really want to minimize the impact of a TLB miss. We get a *lot* of them, and without huge page support in the host we get way more than Linux on native hardware would.

Quoted 5 lines
Quoted 2 lines

Logically speaking, our TLB miss handler must do the following:
1. walk guest TLB(s) to find a matching entry

If we implement large guest TLB in the future,
we might need to involve advanced data structure such as rb-tree.

Absolutely, and I guess e500 (2048-entry TLB0) already has this problem worse than 440 (64-entry TLB). I'll let you submit a patch for that. ;)

Signature · 2 lines
Hollis Blanchard
IBM Linux Technology Center
#3 lore raw
In reply to Hollis Blanchard
Quoted 29 lines · On Wed, 2008-09-10 at 10:54 +0800, Liu Yu-B13201 wrote:
Quoted 1 line
Quoted 1 line

The shadow TLB allows us to skip the repopulation phase,

but at the

Quoted 6 lines
Quoted 4 lines

expense of some memory and some overhead on every exit. Also, it's difficult or impossible to implement a shadow TLB on cores where software cannot address TLB entries by index (i.e. the hardware automatically selects the index).

Shadow TLB also avoid recalculation, the value of shadow

tlb entry can

Quoted 5 lines

be directly written into hardware.
And get and put page can be save as well.

btw, why do we need to update TLB on every exit, why can't we just update it on miss?

Linux can actually modify the host TLB at any time. For example, if Linux gets a fault touching vmalloc'ed memory, or printk faults trying to touch the UART, a new host TLB entry will be inserted without the knowledge of KVM.

If that happens though, it's just clobbered a guest TLB entry, so we put
it back. Ideally we don't want to reload the *entire* TLB, only entries
that the host has changed, but I think that optimization is not yet implemented. That's what this comment is about:

Quoted 8 lines
	/* Save the host's non-pinned TLB mappings, and load 
the guest mappings
	 * over them. Leave the host's "pinned" kernel mappings 
in place. */
	/* XXX optimization: use generation count to avoid 

swapping unmodified
* entries. */

To avoid host kernel disturbing TLB, I think we'd better prevent kernel's unnecessary action, not rob TLB resource.
In some sense, updating all entries influence the host's performance. E.g, reload all entries and then enter guest, after a couple of instructions, then exit and back to qemu which will certainly encounter tlb miss.

You have checked that the optimization of writing only modifed entry improved the performance.
Maybe it's not only because alleviating workload on entering path, but also because just updating needed entry leave qemu's tlb more space.

How about using host's method to update TLB in miss case? Then the conflict may be reduced to the least.
And the entry written by kvm has little chance to be rewritten by host kernel.

E.g. when tlb miss but guest tlb hit, then
local_disable_irq();
map splited shadow entry on index "tlb_44x_index" write shadow entry tlb[tlb_44x_index] into hardware
tlb_44x_index ++ // so that host kernel knows
this update

	local_enable_irq();

Then updating TLB can be removed from the entering guest path. Also, the invalidation need some adjustment.