softmmu: Use async_run_on_cpu in tcg_commit
cover softmmu: Use async_run_on_cpu in tcg_commit
This seems like something that simply hasn't been updated for MTTCG, since the last functional change appears to be:
commit 79e2b9aeccedbfde762b05da662132c7fda292be
Author: Paolo Bonzini <[email protected]> Date: Wed Jan 21 12:09:14 2015 +0100
exec: RCUify AddressSpaceDispatch
and the MTTCG work starts to appear in September 2015.
r~
Richard Henderson (3):
softmmu: Assert data in bounds in iotlb_to_section
softmmu: Use async_run_on_cpu in tcg_commit
softmmu: Remove cpu_reloading_memory_map as unused
include/exec/cpu-common.h | 1 - accel/tcg/cpu-exec-common.c | 30 ---------------------- softmmu/physmem.c | 50 +++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 44 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 softmmu: Assert data in bounds in iotlb_to_section
--- softmmu/physmem.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
softmmu/physmem.c +8 -2
@@ -2413,9 +2413,15 @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu,int asidx =cpu_asidx_from_attrs (cpu, attrs);CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx];AddressSpaceDispatch *d =qatomic_rcu_read (&cpuas->memory_dispatch); -MemoryRegionSection *sections = d->map.sections; +int section_index = index & ~TARGET_PAGE_MASK ; +MemoryRegionSection *ret; -return §ions[index & ~TARGET_PAGE_MASK ]; +assert (section_index < d->map.sections_nb); + ret = d->map.sections + section_index; +assert (ret->mr); +assert (ret->mr->ops); + +return ret; }static void io_mem_init (void )
Signature · 1 line
2.34.1
Re: [PATCH 1/3] softmmu: Assert data in bounds in iotlb_to_section
Quoted 11 lines · Richard Henderson
Suggested-by Alex Bennée <[email protected]>Signed-off-by Richard Henderson <[email protected]>--- softmmu/physmem.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)softmmu/physmem.c +0
-0
softmmu/physmem.c MemoryRegionSection *iotlb_to_section(CPUState *cpu, · line 2417
Quoted 6 lines
softmmu/physmem.c +2
-1@@ -2413,9 +2413,15 @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu,int asidx =cpu_asidx_from_attrs (cpu, attrs);CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx];AddressSpaceDispatch *d =qatomic_rcu_read (&cpuas->memory_dispatch); -MemoryRegionSection *sections = d->map.sections; +int section_index = index & ~TARGET_PAGE_MASK ; +MemoryRegionSection *ret;
It might be worth a quick comment where section_index comes from because I was confused initially expecting it to be address like.
softmmu/physmem.c MemoryRegionSection *iotlb_to_section(CPUState *cpu, · line 2427
Quoted 11 lines
softmmu/physmem.c +6
-1-return §ions[index & ~TARGET_PAGE_MASK ]; +assert (section_index < d->map.sections_nb); + ret = d->map.sections + section_index; +assert (ret->mr); +assert (ret->mr->ops); + +return ret; }static void io_mem_init (void )
Otherwise:
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 2/3 softmmu: Use async_run_on_cpu in tcg_commit
In reply to Richard HendersonAfter system startup, run the update to memory_dispatch and the tlb_flush on the cpu. This eliminates a race, wherein a running cpu sees the memory_dispatch change but has not yet seen the tlb_flush.
Since the update now happens on the cpu, we need not use qatomic_rcu_read to protect the read of memory_dispatch.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1826
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1834
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1846
--- softmmu/physmem.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-)
softmmu/physmem.c +29 -11
@@ -680,8 +680,7 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr,IOMMUTLBEntry iotlb;int iommu_idx;hwaddr addr = orig_addr; -AddressSpaceDispatch *d = -qatomic_rcu_read (&cpu->cpu_ases[asidx].memory_dispatch); +AddressSpaceDispatch *d = cpu->cpu_ases[asidx].memory_dispatch;for (;;) { section =address_space_translate_internal (d, addr, &addr, plen, false);
@@ -2412,7 +2411,7 @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu, {int asidx =cpu_asidx_from_attrs (cpu, attrs);CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx]; -AddressSpaceDispatch *d =qatomic_rcu_read (&cpuas->memory_dispatch); +AddressSpaceDispatch *d = cpuas->memory_dispatch;int section_index = index & ~TARGET_PAGE_MASK ;MemoryRegionSection *ret;
@@ -2487,23 +2486,42 @@ static void tcg_log_global_after_sync(MemoryListener *listener) } } +static void tcg_commit_cpu (CPUState *cpu,run_on_cpu_data data) +{ +CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); +tlb_flush (cpu); +} +static void tcg_commit (MemoryListener *listener) {CPUAddressSpace *cpuas; -AddressSpaceDispatch *d; +CPUState *cpu;assert (tcg_enabled ());/* since each CPU stores ram addresses in its TLB cache, we must reset the modified entries */ cpuas =container_of (listener, CPUAddressSpace, tcg_as_listener); -cpu_reloading_memory_map (); -/* The CPU and TLB are protected by the iothread lock. -* We reload the dispatch pointer now because cpu_reloading_memory_map() -* may have split the RCU critical section. + cpu = cpuas->cpu; + +/* +* Defer changes to as->memory_dispatch until the cpu is quiescent. +* Otherwise we race between (1) other cpu threads and (2) ongoing +* i/o for the current cpu thread, with data cached by mmu_lookup(). +* +* In addition, queueing the work function will kick the cpu back to +* the main loop, which will end the RCU critical section and reclaim +* the memory data structures. +* +* That said, the listener is also called during realize, before +* all of the tcg machinery for run-on is initialized: thus halt_cond. */ - d =address_space_to_dispatch (cpuas->as); -qatomic_rcu_set (&cpuas->memory_dispatch, d); -tlb_flush (cpuas->cpu); +if (cpu->halt_cond) { +async_run_on_cpu (cpu, tcg_commit_cpu,RUN_ON_CPU_HOST_PTR (cpuas)); + }else { +tcg_commit_cpu (cpu,RUN_ON_CPU_HOST_PTR (cpuas)); + } }static void memory_map_init (void )
Signature · 1 line
2.34.1
Quoted 5 lines · Richard Henderson
After system startup, run the update to
memory_dispatchand thetlb_flushon the cpu. This eliminates a race, wherein a running cpu sees thememory_dispatchchange but has not yet seen thetlb_flush.
Quoted 7 lines
Since the update now happens on the cpu, we need not use
qatomic_rcu_readto protect the read ofmemory_dispatch.Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1826
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1834
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1846Signed-off-by Richard Henderson <[email protected]>
Quoted 78 lines
--- softmmu/physmem.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-)softmmu/physmem.c +29
-11@@ -680,8 +680,7 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr,IOMMUTLBEntry iotlb;int iommu_idx;hwaddr addr = orig_addr; -AddressSpaceDispatch *d = -qatomic_rcu_read (&cpu->cpu_ases[asidx].memory_dispatch); +AddressSpaceDispatch *d = cpu->cpu_ases[asidx].memory_dispatch;for (;;) { section =address_space_translate_internal (d, addr, &addr, plen, false);@@ -2412,7 +2411,7 @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu, {int asidx =cpu_asidx_from_attrs (cpu, attrs);CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx]; -AddressSpaceDispatch *d =qatomic_rcu_read (&cpuas->memory_dispatch); +AddressSpaceDispatch *d = cpuas->memory_dispatch;int section_index = index & ~TARGET_PAGE_MASK ;MemoryRegionSection *ret;@@ -2487,23 +2486,42 @@ static void tcg_log_global_after_sync(MemoryListener *listener) } } +static void tcg_commit_cpu (CPUState *cpu,run_on_cpu_data data) +{ +CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); +tlb_flush (cpu); +} +static void tcg_commit (MemoryListener *listener) {CPUAddressSpace *cpuas; -AddressSpaceDispatch *d; +CPUState *cpu;assert (tcg_enabled ());/* since each CPU stores ram addresses in its TLB cache, we must reset the modified entries */ cpuas =container_of (listener, CPUAddressSpace, tcg_as_listener); -cpu_reloading_memory_map (); -/* The CPU and TLB are protected by the iothread lock. -* We reload the dispatch pointer now because cpu_reloading_memory_map() -* may have split the RCU critical section. + cpu = cpuas->cpu; + +/* +* Defer changes to as->memory_dispatch until the cpu is quiescent. +* Otherwise we race between (1) other cpu threads and (2) ongoing +* i/o for the current cpu thread, with data cached by mmu_lookup(). +* +* In addition, queueing the work function will kick the cpu back to +* the main loop, which will end the RCU critical section and reclaim +* the memory data structures. +* +* That said, the listener is also called during realize, before +* all of the tcg machinery for run-on is initialized: thus halt_cond. */ - d =address_space_to_dispatch (cpuas->as); -qatomic_rcu_set (&cpuas->memory_dispatch, d); -tlb_flush (cpuas->cpu); +if (cpu->halt_cond) { +async_run_on_cpu (cpu, tcg_commit_cpu,RUN_ON_CPU_HOST_PTR (cpuas)); + }else { +tcg_commit_cpu (cpu,RUN_ON_CPU_HOST_PTR (cpuas)); + } }static void memory_map_init (void )
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
softmmu/physmem.c static void tcg_log_global_after_sync(MemoryListener *listener) · line 2495
Quoted 7 lines · Richard Henderson
softmmu/physmem.c +7
-0+static void tcg_commit_cpu (CPUState *cpu,run_on_cpu_data data) +{ +CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); +tlb_flush (cpu); +}
Question: do I need to take the iothread lock here, while re-generating the address space dispatch?
r~
Quoted 11 lines · Richard Henderson
softmmu/physmem.c static void tcg_log_global_after_sync(MemoryListener *listener) · line 2495
Quoted 7 lines · Richard Henderson
softmmu/physmem.c +7
-0+static void tcg_commit_cpu (CPUState *cpu,run_on_cpu_data data) +{ +CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); +tlb_flush (cpu); +}Question: do I need to take the iothread lock here, while re-generating the address space dispatch?
Does it regenerate or just collect a current live version under RCU?
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Quoted 15 lines · Alex Bennée
Quoted 11 lines · Richard Henderson
softmmu/physmem.c static void tcg_log_global_after_sync(MemoryListener *listener) · line 2495
Quoted 7 lines · Richard Henderson
softmmu/physmem.c +7
-0+static void tcg_commit_cpu (CPUState *cpu,run_on_cpu_data data) +{ +CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); +tlb_flush (cpu); +}Question: do I need to take the iothread lock here, while re-generating the address space dispatch?
Quoted 1 line
Does it regenerate or just collect a current live version under RCU?
Quite right, just reads.
r~
Quoted 5 lines · On Sat, 26 Aug 2023 16:24:14 -0700 Richard Henderson…
After system startup, run the update to
memory_dispatchand thetlb_flushon the cpu. This eliminates a race, wherein a running cpu sees thememory_dispatchchange but has not yet seen thetlb_flush.
Quoted 7 lines
Since the update now happens on the cpu, we need not use
qatomic_rcu_readto protect the read ofmemory_dispatch.Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1826
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1834
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1846Signed-off-by Richard Henderson <[email protected]>
I'm not pretending I've understood the change though, just that it makes the crashes I saw go away.
Jonathan
Quoted 78 lines
--- softmmu/physmem.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-)softmmu/physmem.c +29
-11@@ -680,8 +680,7 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr,IOMMUTLBEntry iotlb;int iommu_idx;hwaddr addr = orig_addr; -AddressSpaceDispatch *d = -qatomic_rcu_read (&cpu->cpu_ases[asidx].memory_dispatch); +AddressSpaceDispatch *d = cpu->cpu_ases[asidx].memory_dispatch;for (;;) { section =address_space_translate_internal (d, addr, &addr, plen, false);@@ -2412,7 +2411,7 @@ MemoryRegionSection *iotlb_to_section(CPUState *cpu, {int asidx =cpu_asidx_from_attrs (cpu, attrs);CPUAddressSpace *cpuas = &cpu->cpu_ases[asidx]; -AddressSpaceDispatch *d =qatomic_rcu_read (&cpuas->memory_dispatch); +AddressSpaceDispatch *d = cpuas->memory_dispatch;int section_index = index & ~TARGET_PAGE_MASK ;MemoryRegionSection *ret;@@ -2487,23 +2486,42 @@ static void tcg_log_global_after_sync(MemoryListener *listener) } } +static void tcg_commit_cpu (CPUState *cpu,run_on_cpu_data data) +{ +CPUAddressSpace *cpuas = data.host_ptr; + + cpuas->memory_dispatch =address_space_to_dispatch (cpuas->as); +tlb_flush (cpu); +} +static void tcg_commit (MemoryListener *listener) {CPUAddressSpace *cpuas; -AddressSpaceDispatch *d; +CPUState *cpu;assert (tcg_enabled ());/* since each CPU stores ram addresses in its TLB cache, we must reset the modified entries */ cpuas =container_of (listener, CPUAddressSpace, tcg_as_listener); -cpu_reloading_memory_map (); -/* The CPU and TLB are protected by the iothread lock. -* We reload the dispatch pointer now because cpu_reloading_memory_map() -* may have split the RCU critical section. + cpu = cpuas->cpu; + +/* +* Defer changes to as->memory_dispatch until the cpu is quiescent. +* Otherwise we race between (1) other cpu threads and (2) ongoing +* i/o for the current cpu thread, with data cached by mmu_lookup(). +* +* In addition, queueing the work function will kick the cpu back to +* the main loop, which will end the RCU critical section and reclaim +* the memory data structures. +* +* That said, the listener is also called during realize, before +* all of the tcg machinery for run-on is initialized: thus halt_cond. */ - d =address_space_to_dispatch (cpuas->as); -qatomic_rcu_set (&cpuas->memory_dispatch, d); -tlb_flush (cpuas->cpu); +if (cpu->halt_cond) { +async_run_on_cpu (cpu, tcg_commit_cpu,RUN_ON_CPU_HOST_PTR (cpuas)); + }else { +tcg_commit_cpu (cpu,RUN_ON_CPU_HOST_PTR (cpuas)); + } }static void memory_map_init (void )
PATCH 3/3 softmmu: Remove cpu_reloading_memory_map as unused
In reply to Richard Henderson--- include/exec/cpu-common.h | 1 - accel/tcg/cpu-exec-common.c | 30 ------------------------------ 2 files changed, 31 deletions(-)
include/exec/cpu-common.h +0 -1
@@ -133,7 +133,6 @@ static inline void cpu_physical_memory_write(hwaddr addr, {cpu_physical_memory_rw (addr, (void *)buf, len, true); } -void cpu_reloading_memory_map (void );void *cpu_physical_memory_map (hwaddr addr,hwaddr *plen,bool is_write);
accel/tcg/cpu-exec-common.c +0 -30
@@ -33,36 +33,6 @@ void cpu_loop_exit_noexc(CPUState *cpu)cpu_loop_exit (cpu); } -#if defined(CONFIG_SOFTMMU ) -void cpu_reloading_memory_map (void ) -{ -if (qemu_in_vcpu_thread () && current_cpu->running) { -/* The guest can in theory prolong the RCU critical section as long -* as it feels like. The major problem with this is that because it -* can do multiple reconfigurations of the memory map within the -* critical section, we could potentially accumulate an unbounded -* collection of memory data structures awaiting reclamation. -* -* Because the only thing we're currently protecting with RCU is the -* memory data structures, it's sufficient to break the critical section -* in this callback, which we know will get called every time the -* memory map is rearranged. -* -* (If we add anything else in the system that uses RCU to protect -* its data structures, we will need to implement some other mechanism -* to force TCG CPUs to exit the critical section, at which point this -* part of this callback might become unnecessary.) -* -* This pair matches cpu_exec's rcu_read_lock()/rcu_read_unlock(), which -* only protects cpu->as->dispatch. Since we know our caller is about -* to reload it, it's safe to split the critical section. -*/ -rcu_read_unlock (); -rcu_read_lock (); - } -} -#endif -void cpu_loop_exit (CPUState *cpu) {/* Undo the setting in cpu_tb_exec. */
Signature · 1 line
2.34.1
Re: [PATCH 3/3] softmmu: Remove cpu_reloading_memory_map as unused
Quoted 1 line · Richard Henderson
Signed-off-by Richard Henderson <[email protected]>
This is just cleanup, I think it should be merged with 2/3
Quoted 58 lines
--- include/exec/cpu-common.h | 1 - accel/tcg/cpu-exec-common.c | 30 ------------------------------ 2 files changed, 31 deletions(-)include/exec/cpu-common.h +0
-1@@ -133,7 +133,6 @@ static inline void cpu_physical_memory_write(hwaddr addr, {cpu_physical_memory_rw (addr, (void *)buf, len, true); } -void cpu_reloading_memory_map (void );void *cpu_physical_memory_map (hwaddr addr,hwaddr *plen,bool is_write);accel/tcg/cpu-exec-common.c +0
-30@@ -33,36 +33,6 @@ void cpu_loop_exit_noexc(CPUState *cpu)cpu_loop_exit (cpu); } -#if defined(CONFIG_SOFTMMU ) -void cpu_reloading_memory_map (void ) -{ -if (qemu_in_vcpu_thread () && current_cpu->running) { -/* The guest can in theory prolong the RCU critical section as long -* as it feels like. The major problem with this is that because it -* can do multiple reconfigurations of the memory map within the -* critical section, we could potentially accumulate an unbounded -* collection of memory data structures awaiting reclamation. -* -* Because the only thing we're currently protecting with RCU is the -* memory data structures, it's sufficient to break the critical section -* in this callback, which we know will get called every time the -* memory map is rearranged. -* -* (If we add anything else in the system that uses RCU to protect -* its data structures, we will need to implement some other mechanism -* to force TCG CPUs to exit the critical section, at which point this -* part of this callback might become unnecessary.) -* -* This pair matches cpu_exec's rcu_read_lock()/rcu_read_unlock(), which -* only protects cpu->as->dispatch. Since we know our caller is about -* to reload it, it's safe to split the critical section. -*/ -rcu_read_unlock (); -rcu_read_lock (); - } -} -#endif -void cpu_loop_exit (CPUState *cpu) {/* Undo the setting in cpu_tb_exec. */
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro