RFC Multi-threaded TCG design document
Hi,
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============
This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================
Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------
Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new tcg_memory_barrier op
This would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------
This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------
There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will use qemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]
Shared Data Structures
======================
Global TCG State
----------------
We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the tb_cache.
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Memory maps and TLBs
--------------------
The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guest address->real address)
There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, via tlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forced
Emulated hardware state
-----------------------
Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)
IO Subsystem
------------
The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========
[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/memory-barriers.txt
[2] http://thread.gmane.org/gmane.comp.emulators.qemu/334561
[3] http://thread.gmane.org/gmane.comp.emulators.qemu/335297
Signature · 1 line
Alex Bennée
Quoted 1 line · Alex Bennée
Hi,
Hi Alex,
I've completed some of the points below. We will also work on a design decisions document to add to this one.
We probably want to merge that with what we did on the wiki?
http://wiki.qemu.org/Features/tcg-multithread
Quoted 130 lines
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new
tcg_memory_barrieropThis would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will useqemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.
Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Actually from my point of view jump cache modification requires more than a lock as other VCPU thread can be executing code during the modification.
Fortunately this happen "only" with tlb_flush, tlb_page_flush, tb_flush and tb_invalidate which need all CPU to be halted anyway.
Quoted 16 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
Quoted 8 lines
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forced
Actually as we have approximately the same behaviour for all of this memory handling operation eg: tb_flush, tb_*_invalidate, tlb_*_flush which are all playing with the TranslationBlock and the jump cache across-CPU I think we have to add a generic "exit and do something" mechanism for the CPU threads.
So every VCPU threads has a list of thing to do when they exit (such as clearing it's own tb_jmp_cache during a tlb_flush or wait other CPU and flush only one entry for tb_invalidate).
Quoted 11 lines
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
Quoted 8 lines
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)
That might be done with the global mutex as it is today? We need changes here anyway to have VCPU threads running in parallel.
Thanks,
Fred
Quoted 21 lines
IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/memory-barriers.txt
[2] http://thread.gmane.org/gmane.comp.emulators.qemu/334561
[3] http://thread.gmane.org/gmane.comp.emulators.qemu/335297
Quoted 11 lines · Frederic Konrad
Quoted 1 line · Alex Bennée
Hi,
Hi Alex,
I've completed some of the points below. We will also work on a design decisions
document to add to this one.We probably want to merge that with what we did on the wiki?
http://wiki.qemu.org/Features/tcg-multithread
Well hopefully there is cross-over as I started with the wiki as a basic ;-)
Do we want to just keep the wiki as the live design document or put pointers to the current drafts? I'm hoping eventually the page will just point to the design in the doc directory at git.qemu.org.
Quoted 130 lines
Quoted 130 lines
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new
tcg_memory_barrieropThis would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will useqemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.
Quoted 7 lines
Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Actually from my point of view jump cache modification requires more than a lock as other VCPU thread can be executing code during the modification.
Fortunately this happen "only" with
tlb_flush,tlb_page_flush,tb_flushandtb_invalidatewhich need all CPU to be halted anyway.
How about:
DESIGN REQUIREMENT:
- Code generation and patching will be protected by a lock
- Jump cache modification will assert all CPUs are halted
Quoted 24 lines
Quoted 16 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
Quoted 8 lines
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forced
Quoted 10 lines
Actually as we have approximately the same behaviour for all of this memory handling operation eg:
tb_flush, tb_*_invalidate, tlb_*_flush which are all playing with
the TranslationBlock and the jump cache across-CPU I think we have to add a generic "exit and do something" mechanism for the CPU threads. So every VCPU threads has a list of thing to do when they exit (such as clearing it's
owntb_jmp_cacheduring atlb_flushor wait other CPU and flush only one entry fortb_invalidate).
Sounds like I should write an additional section to describe the process of halting CPUs and carrying out deferred per-CPU actions as well as ensuring we can tell when they are all halted.
Quoted 14 lines
Quoted 14 lines
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
Quoted 6 lines
Quoted 4 lines
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)That might be done with the global mutex as it is today? We need changes here anyway to have VCPU threads running in parallel.
I'm not sure re-using the global mutex is a good idea. I've had to hack the global mutex to allow recursive locking to get around the virtio hang I discovered last week. While it works I'm uneasy making such a radical change upstream given how widely the global mutex is used hence the suggestion to have an explicit IOMem mutex.
Actually I'm surprised the iothread muxtex just re-uses the global one. I guess I need to talk to the IO guys as to why they took that decision.
Quoted 2 lines
Thanks,
Thanks for your quick review :-)
Quoted 23 lines
Fred
Quoted 21 lines
IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/memory-barriers.txt
[2] http://thread.gmane.org/gmane.comp.emulators.qemu/334561
[3] http://thread.gmane.org/gmane.comp.emulators.qemu/335297
Signature · 1 line
Alex Bennée
I think we SHOUDL use the wiki - and keep it current. A lot of what you have is in the wiki too, but I’d like to see the wiki updated. We will add our stuff there too…
Cheers
Mark.
Quoted 260 lines · Alex Bennée
Quoted 11 lines · Frederic Konrad
Quoted 1 line · Alex Bennée
Hi,
Hi Alex,
I've completed some of the points below. We will also work on a design decisions
document to add to this one.We probably want to merge that with what we did on the wiki?
http://wiki.qemu.org/Features/tcg-multithreadWell hopefully there is cross-over as I started with the wiki as a basic ;-)
Do we want to just keep the wiki as the live design document or put pointers to the current drafts? I'm hoping eventually the page will just point to the design in the doc directory at git.qemu.org.
Quoted 130 lines
Quoted 130 lines
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new
tcg_memory_barrieropThis would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will useqemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.Quoted 7 lines
Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Actually from my point of view jump cache modification requires more than a lock as other VCPU thread can be executing code during the modification.
Fortunately this happen "only" with
tlb_flush,tlb_page_flush,tb_flushandtb_invalidatewhich need all CPU to be halted anyway.How about:
DESIGN REQUIREMENT:
- Code generation and patching will be protected by a lock
- Jump cache modification will assert all CPUs are haltedQuoted 24 lines
Quoted 16 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
Quoted 8 lines
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedQuoted 10 lines
Actually as we have approximately the same behaviour for all of this memory handling operation eg:
tb_flush, tb_*_invalidate, tlb_*_flush which are all playing with
the TranslationBlock and the jump cache across-CPU I think we have to add a generic "exit and do something" mechanism for the CPU threads. So every VCPU threads has a list of thing to do when they exit (such as clearing it's
owntb_jmp_cacheduring atlb_flushor wait other CPU and flush only one entry fortb_invalidate).Sounds like I should write an additional section to describe the process of halting CPUs and carrying out deferred per-CPU actions as well as ensuring we can tell when they are all halted.
Quoted 14 lines
Quoted 14 lines
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
Quoted 6 lines
Quoted 4 lines
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)That might be done with the global mutex as it is today? We need changes here anyway to have VCPU threads running in parallel.
I'm not sure re-using the global mutex is a good idea. I've had to hack the global mutex to allow recursive locking to get around the virtio hang I discovered last week. While it works I'm uneasy making such a radical change upstream given how widely the global mutex is used hence the suggestion to have an explicit IOMem mutex.
Actually I'm surprised the iothread muxtex just re-uses the global one. I guess I need to talk to the IO guys as to why they took that decision.
Quoted 2 lines
Thanks,
Thanks for your quick review :-)
Quoted 17 lines
Fred
Quoted 15 lines
IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========
Quoted 9 lines
Quoted 6 lines
Signature · 1 line
Alex Bennée
+44 (0)20 7100 3485 x 210
+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Quoted 2 lines · Mark Burton
I think we SHOUDL use the wiki - and keep it current. A lot of what you have is in the wiki too, but I’d like to see the wiki updated. We will add our stuff there too…
I'll do a pass today and update it to point to lists, discussions and WIP trees.
Quoted 282 lines
Cheers
Mark.Quoted 260 lines · Alex Bennée
Quoted 11 lines · Frederic Konrad
Quoted 1 line · Alex Bennée
Hi,
Hi Alex,
I've completed some of the points below. We will also work on a design decisions
document to add to this one.We probably want to merge that with what we did on the wiki?
http://wiki.qemu.org/Features/tcg-multithreadWell hopefully there is cross-over as I started with the wiki as a basic ;-)
Do we want to just keep the wiki as the live design document or put pointers to the current drafts? I'm hoping eventually the page will just point to the design in the doc directory at git.qemu.org.
Quoted 130 lines
Quoted 130 lines
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new
tcg_memory_barrieropThis would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will useqemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.Quoted 7 lines
Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Actually from my point of view jump cache modification requires more than a lock as other VCPU thread can be executing code during the modification.
Fortunately this happen "only" with
tlb_flush,tlb_page_flush,tb_flushandtb_invalidatewhich need all CPU to be halted anyway.How about:
DESIGN REQUIREMENT:
- Code generation and patching will be protected by a lock
- Jump cache modification will assert all CPUs are haltedQuoted 24 lines
Quoted 16 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
Quoted 8 lines
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedQuoted 10 lines
Actually as we have approximately the same behaviour for all of this memory handling operation eg:
tb_flush, tb_*_invalidate, tlb_*_flush which are all playing with
the TranslationBlock and the jump cache across-CPU I think we have to add a generic "exit and do something" mechanism for the CPU threads. So every VCPU threads has a list of thing to do when they exit (such as clearing it's
owntb_jmp_cacheduring atlb_flushor wait other CPU and flush only one entry fortb_invalidate).Sounds like I should write an additional section to describe the process of halting CPUs and carrying out deferred per-CPU actions as well as ensuring we can tell when they are all halted.
Quoted 14 lines
Quoted 14 lines
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
Quoted 6 lines
Quoted 4 lines
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)That might be done with the global mutex as it is today? We need changes here anyway to have VCPU threads running in parallel.
I'm not sure re-using the global mutex is a good idea. I've had to hack the global mutex to allow recursive locking to get around the virtio hang I discovered last week. While it works I'm uneasy making such a radical change upstream given how widely the global mutex is used hence the suggestion to have an explicit IOMem mutex.
Actually I'm surprised the iothread muxtex just re-uses the global one. I guess I need to talk to the IO guys as to why they took that decision.
Quoted 2 lines
Thanks,
Thanks for your quick review :-)
Quoted 17 lines
Fred
Quoted 15 lines
IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========Quoted 9 lines
Quoted 6 lines
Signature · 1 line
Alex Bennée+44 (0)20 7100 3485 x 210+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Signature · 1 line
Alex Bennée
Hi Alex,
Let me just add one comment.
Quoted 64 lines · Alex Bennée
Hi,
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
Quoted 5 lines
DESIGN REQUIREMENT: Add a new
tcg_memory_barrieropThis would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
I believe the main problem here is not just about translating guest barriers to host barriers, but also about adding barriers in the TCG generated code where they are needed i.e. when, in the guest code, the synchronization/memory barriers don't wrap atomic instructions.
To give a concrete example, let's suppose a case where we emulate an x86 guest on ARM (on ARMv8 the situation should be not so complicated). At some point TCG will be asked to translate a Linux spin_lock(), that eventually uses arch_spin_lock(). Simplifying a bit, what happens is along the lines of:
- barrier() // meaning a compiler barrier
- atomic update of the (spin)lock value
- barrier()
The architecture dependent part is of course the "atomic update of the spinlock" implementation, which, on ARM, relies on ldrex/strex instructions and eventually issues a full hardware memory barrier (dmb). On the other hand, on x86, only the cmpxchg instructions is used (coupled with a memory compiler clobber), but no hardware full memory barrier is required because of a stronger memory model. I'm pretty sure that the TCG code generated from spin_lock() will not be the same as the one present in an ARM kernel binary compiled with the latest GCC, but still, that full memory barrier is likely to be required also in the TCG generated code.
Now the question could be: looking at the bare flow of asm x86 instructions used to implement spin_lock(), how can we deduce that a dmb instruction has to be added after the atomic instructions? Should we pair every guest atomic instruction with a dmb?
Regards,
alvise
Quoted 132 lines
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will useqemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedEmulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/memory-barriers.txt
[2] http://thread.gmane.org/gmane.comp.emulators.qemu/334561
[3] http://thread.gmane.org/gmane.comp.emulators.qemu/335297--
Alex Bennée
Quoted 4 lines · alvise rigo
Hi Alex,
Let me just add one comment.
<snip>
Quoted 19 lines
Quoted 19 lines
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new
tcg_memory_barrierop
Quoted 8 lines
Quoted 3 lines
This would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
I believe the main problem here is not just about translating guest barriers to host barriers, but also about adding barriers in the TCG generated code where they are needed i.e. when, in the guest code, the synchronization/memory barriers don't wrap atomic instructions.
Not all atomic instructions imply memory barriers. AIUI on ARMv8 you only have explicit memory barriers is you use the load-acquire/store-release variants of load/store exclusive.
Quoted 22 lines
To give a concrete example, let's suppose a case where we emulate an x86 guest on ARM (on ARMv8 the situation should be not so complicated). At some point TCG will be asked to translate a Linux
spin_lock(), that eventually usesarch_spin_lock(). Simplifying a bit, what happens is along the lines of:-
barrier()// meaning a compiler barrier
- atomic update of the (spin)lock value
-barrier()The architecture dependent part is of course the "atomic update of the spinlock" implementation, which, on ARM, relies on ldrex/strex instructions and eventually issues a full hardware memory barrier (dmb). On the other hand, on x86, only the cmpxchg instructions is used (coupled with a memory compiler clobber), but no hardware full memory barrier is required because of a stronger memory model. I'm pretty sure that the TCG code generated from
spin_lock()will not be the same as the one present in an ARM kernel binary compiled with the latest GCC, but still, that full memory barrier is likely to be required also in the TCG generated code.
Quoted 4 lines
Now the question could be: looking at the bare flow of asm x86 instructions used to implement
spin_lock(), how can we deduce that a dmb instruction has to be added after the atomic instructions? Should we pair every guest atomic instruction with a dmb?
I don't think so. We should follow the guest processors semantics which AIUI for x86 is cmpxchg does enforce memory ordering across cores if prefixed with the LOCK prefix. At that point we can prefix the cmpxchg TCG ops with our new tcg_dmb barrier.
Without the LOCK prefix we still guarantee an atomic update but without any explicit synchronisation between the cores.
In practice Linux at least uses LOCK prefixed cmpxchg instructions in its synchronisation code.
x86 code will still emit s/m/lfence instructions to ensure external devices see memory accesses in the right order. These should certainly cause memory barriers tcg ops to be emitted.
Quoted 4 lines
Regards,
alvise
Signature · 1 line
Alex Bennée
Quoted 2 lines · Mark Burton
I think we SHOUDL use the wiki - and keep it current. A lot of what you have is in the wiki too, but I’d like to see the wiki updated. We will add our stuff there too…
Well I've added some pointer for now. Reading the wiki reminded me of the informal phone conference. What are the details for that?
Quoted 282 lines
Cheers
Mark.Quoted 260 lines · Alex Bennée
Quoted 11 lines · Frederic Konrad
Quoted 1 line · Alex Bennée
Hi,
Hi Alex,
I've completed some of the points below. We will also work on a design decisions
document to add to this one.We probably want to merge that with what we did on the wiki?
http://wiki.qemu.org/Features/tcg-multithreadWell hopefully there is cross-over as I started with the wiki as a basic ;-)
Do we want to just keep the wiki as the live design document or put pointers to the current drafts? I'm hoping eventually the page will just point to the design in the doc directory at git.qemu.org.
Quoted 130 lines
Quoted 130 lines
One thing that Peter has been asking for is a design document for the way we are going to approach multi-threaded TCG emulation. I started with the information that was captured on the wiki and tried to build on that. It's almost certainly incomplete but I thought it would be worth posting for wider discussion early rather than later.
One obvious omission at the moment is the lack of discussion about other non-TLB shared data structures in QEMU (I'm thinking of the various dirty page tracking bits, I'm sure there is more).
I've also deliberately tried to avoid documenting the design decisions made in the current Greensoc's patch series. This is so we can concentrate on the big picture before getting side-tracked into the implementation details.
I have now started digging into the Greensocs code in earnest and the plan is eventually the design and the implementation will converge on a final documented complete solution ;-)
Anyway as ever I look forward to the comments and discussion:
STATUS: DRAFTING
Introduction
============This document outlines the design for multi-threaded TCG emulation. The original TCG implementation was single threaded and dealt with multiple CPUs by with simple round-robin scheduling. This simplified a lot of things but became increasingly limited as systems being emulated gained additional cores and per-core performance gains for host systems started to level off.
Memory Consistency
==================Between emulated guests and host systems there are a range of memory consistency models. While emulating weakly ordered systems on strongly ordered hosts shouldn't cause any problems the same is not true for the reverse setup.
The proposed design currently does not address the problem of emulating strong ordering on a weakly ordered host although even on strongly ordered systems software should be using synchronisation primitives to ensure correct operation.
Memory Barriers
---------------Barriers (sometimes known as fences) provide a mechanism for software to enforce a particular ordering of memory operations from the point of view of external observers (e.g. another processor core). They can apply to any memory operations as well as just loads or stores.
The Linux kernel has an excellent write-up on the various forms of memory barrier and the guarantees they can provide [1].
Barriers are often wrapped around synchronisation primitives to provide explicit memory ordering semantics. However they can be used by themselves to provide safe lockless access by ensuring for example a signal flag will always be set after a payload.
DESIGN REQUIREMENT: Add a new
tcg_memory_barrieropThis would enforce a strong load/store ordering so all loads/stores complete at the memory barrier. On single-core non-SMP strongly ordered backends this could become a NOP.
There may be a case for further refinement if this causes performance bottlenecks.
Memory Control and Maintenance
------------------------------This includes a class of instructions for controlling system cache behaviour. While QEMU doesn't model cache behaviour these instructions are often seen when code modification has taken place to ensure the changes take effect.
Synchronisation Primitives
--------------------------There are two broad types of synchronisation primitives found in modern ISAs: atomic instructions and exclusive regions.
The first type offer a simple atomic instruction which will guarantee some sort of test and conditional store will be truly atomic w.r.t. other cores sharing access to the memory. The classic example is the x86 cmpxchg instruction.
The second type offer a pair of load/store instructions which offer a guarantee that an region of memory has not been touched between the load and store instructions. An example of this is ARM's ldrex/strex pair where the strex instruction will return a flag indicating a successful store only if no other CPU has accessed the memory region since the ldrex.
Traditionally TCG has generated a series of operations that work because they are within the context of a single translation block so will have completed before another CPU is scheduled. However with the ability to have multiple threads running to emulate multiple CPUs we will need to explicitly expose these semantics.
DESIGN REQUIREMENTS:
- atomics
- Introduce some atomic TCG ops for the common semantics
- The default fallback helper function will useqemu_atomics
- Each backend can then add a more efficient implementation
- load/store exclusive
[AJB:
There are currently a number proposals of interest:
- Greensocs tweaks to ldst ex (using locks)
- Slow-path for atomic instruction translation [2]
- Helper-based Atomic Instruction Emulation (AIE) [3]
]Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.Quoted 7 lines
Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Actually from my point of view jump cache modification requires more than a lock as other VCPU thread can be executing code during the modification.
Fortunately this happen "only" with
tlb_flush,tlb_page_flush,tb_flushandtb_invalidatewhich need all CPU to be halted anyway.How about:
DESIGN REQUIREMENT:
- Code generation and patching will be protected by a lock
- Jump cache modification will assert all CPUs are haltedQuoted 24 lines
Quoted 16 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
Quoted 8 lines
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedQuoted 10 lines
Actually as we have approximately the same behaviour for all of this memory handling operation eg:
tb_flush, tb_*_invalidate, tlb_*_flush which are all playing with
the TranslationBlock and the jump cache across-CPU I think we have to add a generic "exit and do something" mechanism for the CPU threads. So every VCPU threads has a list of thing to do when they exit (such as clearing it's
owntb_jmp_cacheduring atlb_flushor wait other CPU and flush only one entry fortb_invalidate).Sounds like I should write an additional section to describe the process of halting CPUs and carrying out deferred per-CPU actions as well as ensuring we can tell when they are all halted.
Quoted 14 lines
Quoted 14 lines
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
Quoted 6 lines
Quoted 4 lines
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)That might be done with the global mutex as it is today? We need changes here anyway to have VCPU threads running in parallel.
I'm not sure re-using the global mutex is a good idea. I've had to hack the global mutex to allow recursive locking to get around the virtio hang I discovered last week. While it works I'm uneasy making such a radical change upstream given how widely the global mutex is used hence the suggestion to have an explicit IOMem mutex.
Actually I'm surprised the iothread muxtex just re-uses the global one. I guess I need to talk to the IO guys as to why they took that decision.
Quoted 2 lines
Thanks,
Thanks for your quick review :-)
Quoted 17 lines
Fred
Quoted 15 lines
IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========Quoted 9 lines
Quoted 6 lines
Signature · 1 line
Alex Bennée+44 (0)20 7100 3485 x 210+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Signature · 1 line
Alex Bennée
Quoted 10 lines · Alex Bennée
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
Quoted 7 lines
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
This should simply be the "big QEMU lock", which also protects the I/O subsystem.
With BQL-free TCG (a subset of multi-threaded TCG), the code in qemu_mutex_lock_iothread that forces VCPUs to relinquish the mutex can be dropped.
Paolo
Quoted 23 lines
- The mutex should be recursive (e.g. allowing pid to relock itself)
IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
References
==========[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/memory-barriers.txt
[2] http://thread.gmane.org/gmane.comp.emulators.qemu/334561
[3] http://thread.gmane.org/gmane.comp.emulators.qemu/335297
Quoted 19 lines · Paolo Bonzini
Quoted 10 lines · Alex Bennée
Emulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
Quoted 7 lines
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
Quoted 11 lines
This should simply be the "big QEMU lock", which also protects the I/O subsystem.
With BQL-free TCG (a subset of multi-threaded TCG), the code in
qemu_mutex_lock_iothreadthat forces VCPUs to relinquish the mutex can be dropped.Paolo
Quoted 2 lines
- The mutex should be recursive (e.g. allowing pid to relock itself)
Paolo,
But would there be a risk is we make the BQL recursive?
I had to do this because the iomem accesses either side of a virt-io transaction would deadlock otherwise.
Signature · 1 line
Alex Bennée
Quoted 4 lines · Alex Bennée
But would there be a risk is we make the BQL recursive?
I had to do this because the iomem accesses either side of a virt-io transaction would deadlock otherwise.
The idea was to check if the BQL is taken, and if not take it in memory_region_dispatch_read/memory_region_dispatch_write.
Paolo
Quoted 15 lines · * Alex Benn?e ([email protected]) wrote:
Hi,
Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.
Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
I don't think that you require a shared translation buffer between cores to do this - although it *might* be the easiest way.
You could have a per-core translation buffer, the only requirement is that most invalidation operations happen on all the buffers (although that might depend on the emulated architecture).
With a per-core translation buffer, each core could generate new translations without locking the other cores as long as no one is doing invalidations.
Quoted 46 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedEmulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)IO Subsystem
------------
Quoted 7 lines
The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
Watch out for where DMA invalidates the translated code.
Dave
Quoted 13 lines
References
==========[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/memory-barriers.txt
[2] http://thread.gmane.org/gmane.comp.emulators.qemu/334561
[3] http://thread.gmane.org/gmane.comp.emulators.qemu/335297Signature · 1 line
Alex Bennée
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 22 lines · Dr. David Alan Gilbert
Quoted 15 lines · * Alex Benn?e ([email protected]) wrote:
Hi,
Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Quoted 7 lines
I don't think that you require a shared translation buffer between cores to do this - although it *might* be the easiest way.
You could have a per-core translation buffer, the only requirement is that most invalidation operations happen on all the buffers (although that might depend on the emulated architecture).
With a per-core translation buffer, each core could generate new translations without locking the other cores as long as no one is doing invalidations.
I agree it’s not a design requirement - however we’ve kind of gone round this loop in terms of getting things to work.
Fred will doubtless fill in some details, but basically it looks like making the TCG so you could run several in parallel is a nightmare. We seem to get reasonable performance having just one CPU at a time generating TBs. At the same time, of course, the way Qemu is constructed there are actually several ‘layers’ of buffer - from the CPU local ones through to the TB ‘pool’. So, actually, my accident or design, we benefit from a sort of caching structure.
Quoted 51 lines
Quoted 51 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedEmulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)IO Subsystem
------------The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
Quoted 6 lines
Quoted 3 lines
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
Watch out for where DMA invalidates the translated code.
need to check - that might be a great catch !
Cheers
Mark.
Quoted 13 lines
Dave
Quoted 4 lines
Quoted 2 lines
Signature · 1 line
Alex Bennée--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
+44 (0)20 7100 3485 x 210
+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton
Quoted 26 lines · Mark Burton
Quoted 19 lines · Dr. David Alan Gilbert
Quoted 14 lines · * Alex Benn?e ([email protected]) wrote:
Hi,
Shared Data Structures
======================Global TCG State
----------------We need to protect the entire code generation cycle including any post generation patching of the translated code. This also implies a shared translation buffer which contains code running on all cores. Any execution path that comes to the main run loop will need to hold a mutex for code generation. This also includes times when we need flush code or jumps from the
tb_cache.Quoted 2 lines
DESIGN REQUIREMENT: Add locking around all code generation, patching and jump cache modification
Quoted 7 lines
I don't think that you require a shared translation buffer between cores to do this - although it *might* be the easiest way.
You could have a per-core translation buffer, the only requirement is that most invalidation operations happen on all the buffers (although that might depend on the emulated architecture).
With a per-core translation buffer, each core could generate new translations without locking the other cores as long as no one is doing invalidations.
Quoted 3 lines
I agree it’s not a design requirement - however we’ve kind of gone round this loop in terms of getting things to work.
Fred will doubtless fill in some details, but basically it looks like making the TCG so you could run several in parallel is a nightmare. We seem to get reasonable performance having just one CPU at a time generating TBs. At the same time, of course, the way Qemu is constructed there are actually several ‘layers’ of buffer - from the CPU local ones through to the TB ‘pool’. So, actually, my accident or design, we benefit from a sort of caching structure.
True, it seems to be very complex at least on ARM because of the disassemble context etc.. But on the other side the invalidation might be easier I guess. For performance I'm not sure of what is the better way..
Fred
Quoted 86 lines
Quoted 46 lines
Quoted 46 lines
Memory maps and TLBs
--------------------The memory handling code is fairly critical to the speed of memory access in the emulated system.
- Memory regions (dividing up access to PIO, MMIO and RAM)
- Dirty page tracking (for code gen, migration and display)
- Virtual TLB (for translating guestaddress->realaddress)There is a both a fast path walked by the generated code and a slow path when resolution is required. When the TLB tables are updated we need to ensure they are done in a safe way by bringing all executing threads to a halt before making the modifications.
DESIGN REQUIREMENTS:
- TLB Flush All/Page
- can be across-CPUs
- will need all other CPUs brought to a halt
- TLB Update (update a CPUTLBEntry, viatlb_set_page_with_attrs)
- This is a per-CPU table - by definition can't race
- updated by it's own thread when the slow-path is forcedEmulated hardware state
-----------------------Currently the hardware emulation has no protection against multiple-accesses. However guest systems accessing emulated hardware should be carrying out their own locking to prevent multiple CPUs confusing the hardware. Of course there is no guarantee the there couldn't be a broken guest that doesn't lock so you could get racing accesses to the hardware.
There is the class of paravirtualized hardware (VIRTIO) that works in a purely mmio mode. Often setting flags directly in guest memory as a result of a guest triggered transaction.
DESIGN REQUIREMENTS:
- Access to IO Memory should be serialised by an IOMem mutex
- The mutex should be recursive (e.g. allowing pid to relock itself)IO Subsystem
------------Quoted 9 lines
Quoted 7 lines
The I/O subsystem is heavily used by KVM and has seen a lot of improvements to offload I/O tasks to dedicated IOThreads. There should be no additional locking required once we reach the Block Driver.
DESIGN REQUIREMENTS:
- The dataplane should continue to be protected by the iothread locks
Watch out for where DMA invalidates the translated code.
need to check - that might be a great catch !
Cheers
Mark.
Quoted 12 lines
Dave
Quoted 4 lines
Quoted 2 lines
Signature · 1 line
Alex Bennée--
Dr. David Alan Gilbert / [email protected] / Manchester, UK+44 (0)20 7100 3485 x 210+33 (0)5 33 52 01 77x 210
+33 (0)603762104 mark.burton