cputlb: track dirty tlbs and general cleanup
cover cputlb: track dirty tlbs and general cleanup
The motivation here is reducing the total overhead.
Before a few patches went into target-arm.next, I measured total tlb flush overhead for aarch64 at 25%. This appears to reduce the total overhead to about 5% (I do need to re-run the control tests, not just watch perf top as I'm doing now).
The final patch is somewhat of an RFC. I'd like to know what benchmark was used when putting in pending_tlb_flushes, and I have not done any archaeology to find out. I suspect that it does make any measurable difference beyond tlb_c.dirty, and I think the code is a bit cleaner without it.
r~
Richard Henderson (10):
cputlb: Move tlb_lock to CPUTLBCommon
cputlb: Remove tcg_enabled hack from tlb_flush_nocheck
cputlb: Move cpu->pending_tlb_flush to env->tlb_c.pending_flush
cputlb: Split large page tracking per mmu_idx
cputlb: Move env->vtlb_index to env->tlb_d.vindex
cputlb: Merge tlb_flush_nocheck into tlb_flush_by_mmuidx_async_work
cputlb: Merge tlb_flush_page into tlb_flush_page_by_mmuidx
cputlb: Count "partial" and "elided" tlb flushes
cputlb: Filter flushes on already clean tlbs
cputlb: Remove tlb_c.pending_flushes
include/exec/cpu-defs.h | 51 +++++- include/exec/cputlb.h | 2 +- include/qom/cpu.h | 6 - accel/tcg/cputlb.c | 354 +++++++++++++++----------------------- accel/tcg/translate-all.c | 8 +- 5 files changed, 184 insertions(+), 237 deletions(-)
Signature · 1 line
2.17.2
PATCH 1/10 cputlb: Move tlb_lock to CPUTLBCommon
This is the first of several moves to reduce the size of the CPU_COMMON_TLB macro and improve some locality of refernce.
--- include/exec/cpu-defs.h | 17 ++++++++++++--- accel/tcg/cputlb.c | 48 ++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 27 deletions(-)
include/exec/cpu-defs.h +14 -3
@@ -141,10 +141,21 @@ typedef struct CPUIOTLBEntry {MemTxAttrs attrs; } CPUIOTLBEntry; +/* +* Data elements that are shared between all MMU modes. +*/ +typedef struct CPUTLBCommon { +/* lock serializes updates to tlb_table and tlb_v_table */ +QemuSpin lock; +}CPUTLBCommon ; + +/* +* The meaning of each of the MMU modes is defined in the target code. +* Note that NB_MMU_MODES is not yet defined; we can only reference it +* within preprocessor defines that will be expanded later. +*/ #define CPU_COMMON_TLB \ -/* The meaning of the MMU modes is defined in the target code. */ \ -/* tlb_lock serializes updates to tlb_table and tlb_v_table */ \ - QemuSpin tlb_lock; \ + CPUTLBCommon tlb_c; \ CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE]; \ CPUTLBEntry tlb_v_table[NB_MMU_MODES][CPU_VTLB_SIZE]; \ CPUIOTLBEntry iotlb[NB_MMU_MODES][CPU_TLB_SIZE]; \
accel/tcg/cputlb.c +24 -24
177 lines not on this page · raw message
Signature · 1 line
2.17.2
Re: [Qemu-devel] [PATCH 01/10] cputlb: Move tlb_lock to CPUTLBCommon
Quoted 2 lines · Richard Henderson
This is the first of several moves to reduce the size of the
CPU_COMMON_TLBmacro and improve some locality of refernce.
"reference"
Quoted 2 lines
Signed-off-by Richard Henderson <[email protected]>
Quoted 234 lines
--- include/exec/cpu-defs.h | 17 ++++++++++++--- accel/tcg/cputlb.c | 48 ++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 27 deletions(-)include/exec/cpu-defs.h +14
-3@@ -141,10 +141,21 @@ typedef struct CPUIOTLBEntry {MemTxAttrs attrs; } CPUIOTLBEntry; +/* +* Data elements that are shared between all MMU modes. +*/ +typedef struct CPUTLBCommon { +/* lock serializes updates to tlb_table and tlb_v_table */ +QemuSpin lock; +}CPUTLBCommon ; + +/* +* The meaning of each of the MMU modes is defined in the target code. +* Note that NB_MMU_MODES is not yet defined; we can only reference it +* within preprocessor defines that will be expanded later. +*/ #define CPU_COMMON_TLB \ -/* The meaning of the MMU modes is defined in the target code. */ \ -/* tlb_lock serializes updates to tlb_table and tlb_v_table */ \ - QemuSpin tlb_lock; \ + CPUTLBCommon tlb_c; \ CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE]; \ CPUTLBEntry tlb_v_table[NB_MMU_MODES][CPU_VTLB_SIZE]; \ CPUIOTLBEntry iotlb[NB_MMU_MODES][CPU_TLB_SIZE]; \accel/tcg/cputlb.c +24
-24@@ -78,7 +78,7 @@ void tlb_init(CPUState *cpu) {CPUArchState *env = cpu->env_ptr; -qemu_spin_init (&env->tlb_lock); +qemu_spin_init (&env->tlb_c.lock); }/* flush_all_helper: run fn across all cpus @@ -134,15 +134,15 @@ static void tlb_flush_nocheck(CPUState *cpu)tlb_debug ("(count: %zu)\n" ,tlb_flush_count ());/* -* tlb_table/tlb_v_table updates from any thread must hold tlb_lock. +* tlb_table/tlb_v_table updates from any thread must hold tlb_c.lock. * However, updates from the owner thread (as is the case here; see the * above assert_cpu_is_self) do not need atomic_set because all reads * that do not hold the lock are performed by the same owner thread. */ -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);memset (env->tlb_table,-1 ,sizeof (env->tlb_table));memset (env->tlb_v_table,-1 ,sizeof (env->tlb_v_table)); -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock);cpu_tb_jmp_cache_clear (cpu);@@ -195,7 +195,7 @@ static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)tlb_debug ("start: mmu_idx:0x%04lx\n" , mmu_idx_bitmask); -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) {if (test_bit (mmu_idx, &mmu_idx_bitmask)) {@@ -205,7 +205,7 @@ static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)memset (env->tlb_v_table[mmu_idx],-1 ,sizeof (env->tlb_v_table[0 ])); } } -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock);cpu_tb_jmp_cache_clear (cpu);@@ -262,7 +262,7 @@ static inline bool tlb_hit_page_anyprot(CPUTLBEntry *tlb_entry,tlb_hit_page (tlb_entry->addr_code, page); } -/* Called with tlb_lock held */ +/* Called with tlb_c.lock held */ static inline void tlb_flush_entry_locked (CPUTLBEntry *tlb_entry,target_ulong page) {@@ -271,7 +271,7 @@ static inline void tlb_flush_entry_locked(CPUTLBEntry *tlb_entry, } } -/* Called with tlb_lock held */ +/* Called with tlb_c.lock held */ static inline void tlb_flush_vtlb_page_locked (CPUArchState *env,int mmu_idx,target_ulong page) {@@ -304,12 +304,12 @@ static void tlb_flush_page_async_work(CPUState *cpu, run_on_cpu_data data) } addr &=TARGET_PAGE_MASK ; -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) {tlb_flush_entry_locked (tlb_entry (env, mmu_idx, addr), addr);tlb_flush_vtlb_page_locked (env, mmu_idx, addr); } -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock);tb_flush_jmp_cache (cpu, addr); }@@ -345,14 +345,14 @@ static void tlb_flush_page_by_mmuidx_async_work(CPUState *cpu,tlb_debug ("flush page addr:" TARGET_FMT_lx" mmu_idx:0x%lx\n" , addr, mmu_idx_bitmap); -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) {if (test_bit (mmu_idx, &mmu_idx_bitmap)) {tlb_flush_entry_locked (tlb_entry (env, mmu_idx, addr), addr);tlb_flush_vtlb_page_locked (env, mmu_idx, addr); } } -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock);tb_flush_jmp_cache (cpu, addr); }@@ -479,7 +479,7 @@ void tlb_unprotect_code(ram_addr_t ram_addr)* te->addr_write with atomic_set. We don't need to worry about this for * oversized guests as MTTCG is disabled for them. * -* Called with tlb_lock held. +* Called with tlb_c.lock held. */ static void tlb_reset_dirty_range_locked (CPUTLBEntry *tlb_entry,uintptr_t start,uintptr_t length)@@ -501,7 +501,7 @@ static void tlb_reset_dirty_range_locked(CPUTLBEntry *tlb_entry, }/* -* Called with tlb_lock held. +* Called with tlb_c.lock held. * Called only from the vCPU context, i.e. the TLB's owner thread. */ static inline void copy_tlb_helper_locked (CPUTLBEntry *d,const CPUTLBEntry *s)@@ -511,7 +511,7 @@ static inline void copy_tlb_helper_locked(CPUTLBEntry *d, const CPUTLBEntry *s)/* This is a cross vCPU call (i.e. another vCPU resetting the flags of * the target vCPU). -* We must take tlb_lock to avoid racing with another vCPU update. The only +* We must take tlb_c.lock to avoid racing with another vCPU update. The only * thing actually updated is the target TLB entry ->addr_write flags. */ void tlb_reset_dirty (CPUState *cpu,ram_addr_t start1,ram_addr_t length)@@ -521,7 +521,7 @@ void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length)int mmu_idx; env = cpu->env_ptr; -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) {unsigned int i;@@ -535,10 +535,10 @@ void tlb_reset_dirty(CPUState *cpu, ram_addr_t start1, ram_addr_t length) length); } } -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock); } -/* Called with tlb_lock held */ +/* Called with tlb_c.lock held */ static inline void tlb_set_dirty1_locked (CPUTLBEntry *tlb_entry,target_ulong vaddr) {@@ -557,7 +557,7 @@ void tlb_set_dirty(CPUState *cpu, target_ulong vaddr)assert_cpu_is_self (cpu); vaddr &=TARGET_PAGE_MASK ; -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) {tlb_set_dirty1_locked (tlb_entry (env, mmu_idx, vaddr), vaddr); }@@ -568,7 +568,7 @@ void tlb_set_dirty(CPUState *cpu, target_ulong vaddr)tlb_set_dirty1_locked (&env->tlb_v_table[mmu_idx][k], vaddr); } } -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock); }/* Our TLB does not support large pages, so remember the area covered by @@ -669,7 +669,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,* a longer critical section, but this is not a concern since the TLB lock * is unlikely to be contended. */ -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);/* Make sure there's no cached translation for the new page. */ tlb_flush_vtlb_page_locked (env, mmu_idx, vaddr_page);@@ -736,7 +736,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr, }copy_tlb_helper_locked (te, &tn); -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock); }/* Add a new TLB entry, but without specifying the memory @@ -917,11 +917,11 @@ static bool victim_tlb_hit(CPUArchState *env, size_t mmu_idx, size_t index,/* Found entry in victim tlb, swap tlb and iotlb. */ CPUTLBEntry tmptlb, *tlb = &env->tlb_table[mmu_idx][index]; -qemu_spin_lock (&env->tlb_lock); +qemu_spin_lock (&env->tlb_c.lock);copy_tlb_helper_locked (&tmptlb, tlb);copy_tlb_helper_locked (tlb, vtlb);copy_tlb_helper_locked (vtlb, &tmptlb); -qemu_spin_unlock (&env->tlb_lock); +qemu_spin_unlock (&env->tlb_c.lock);CPUIOTLBEntry tmpio, *io = &env->iotlb[mmu_idx][index];CPUIOTLBEntry *vio = &env->iotlb_v[mmu_idx][vidx];
PATCH 2/10 cputlb: Remove tcg_enabled hack from tlb_flush_nocheck
In reply to Richard HendersonThe bugs this was working around were fixed with commits 022d6378c7fd target/unicore32: remove tlb_flush from uc32_init_fn 6e11beecfde0 target/alpha: remove tlb_flush from alpha_cpu_initfn
--- accel/tcg/cputlb.c | 7 ------- 1 file changed, 7 deletions(-)
accel/tcg/cputlb.c +0 -7
@@ -122,13 +122,6 @@ static void tlb_flush_nocheck(CPUState *cpu) {CPUArchState *env = cpu->env_ptr; -/* The QOM tests will trigger tlb_flushes without setting up TCG -* so we bug out here in that case. -*/ -if (!tcg_enabled ()) { -return ; - } -assert_cpu_is_self (cpu);atomic_set (&env->tlb_flush_count, env->tlb_flush_count +1 );tlb_debug ("(count: %zu)\n" ,tlb_flush_count ());
Signature · 1 line
2.17.2
PATCH 3/10 cputlb: Move cpu->pending_tlb_flush to env->tlb_c.pending_flush
In reply to Richard HendersonProtect it with the tlb_lock instead of using atomics. The move puts it in or near the same cacheline as the lock; using the lock means we don't need a second atomic operation in order to perform the update. Which makes it cheap to also update pending_flush in tlb_flush_by_mmuidx_async_work.
--- include/exec/cpu-defs.h | 8 +++++++- include/qom/cpu.h | 6 ------ accel/tcg/cputlb.c | 35 +++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 19 deletions(-)
include/exec/cpu-defs.h +7 -1
@@ -145,8 +145,14 @@ typedef struct CPUIOTLBEntry {* Data elements that are shared between all MMU modes. */ typedef struct CPUTLBCommon { -/* lock serializes updates to tlb_table and tlb_v_table */ +/* Serialize updates to tlb_table and tlb_v_table, and others as noted. */ QemuSpin lock; +/* +* Within pending_flush, for each bit N, there exists an outstanding +* cross-cpu flush for mmu_idx N. Further cross-cpu flushes to that +* mmu_idx may be discarded. Protected by tlb_c.lock. +*/ +uint16_t pending_flush; }CPUTLBCommon ;/*
include/qom/cpu.h +0 -6
@@ -429,12 +429,6 @@ struct CPUState {struct hax_vcpu_state *hax_vcpu; -/* The pending_tlb_flush flag is set and cleared atomically to -* avoid potential races. The aim of the flag is to avoid -* unnecessary flushes. -*/ -uint16_t pending_tlb_flush; -int hvf_fd;/* track IOMMUs whose translations we've cached in the TCG TLB */
accel/tcg/cputlb.c +23 -12
@@ -133,6 +133,7 @@ static void tlb_flush_nocheck(CPUState *cpu)* that do not hold the lock are performed by the same owner thread. */ qemu_spin_lock (&env->tlb_c.lock); + env->tlb_c.pending_flush =0 ;memset (env->tlb_table,-1 ,sizeof (env->tlb_table));memset (env->tlb_v_table,-1 ,sizeof (env->tlb_v_table));qemu_spin_unlock (&env->tlb_c.lock);
@@ -142,8 +143,6 @@ static void tlb_flush_nocheck(CPUState *cpu) env->vtlb_index =0 ; env->tlb_flush_addr =-1 ; env->tlb_flush_mask =0 ; - -atomic_mb_set (&cpu->pending_tlb_flush,0 ); }static void tlb_flush_global_async_work (CPUState *cpu,run_on_cpu_data data)
@@ -154,8 +153,15 @@ static void tlb_flush_global_async_work(CPUState *cpu, run_on_cpu_data data)void tlb_flush (CPUState *cpu) {if (cpu->created && !qemu_cpu_is_self (cpu)) { -if (atomic_mb_read (&cpu->pending_tlb_flush) !=ALL_MMUIDX_BITS ) { -atomic_mb_set (&cpu->pending_tlb_flush,ALL_MMUIDX_BITS ); +CPUArchState *env = cpu->env_ptr; +uint16_t pending; + +qemu_spin_lock (&env->tlb_c.lock); + pending = env->tlb_c.pending_flush; + env->tlb_c.pending_flush =ALL_MMUIDX_BITS ; +qemu_spin_unlock (&env->tlb_c.lock); + +if (pending !=ALL_MMUIDX_BITS ) {async_run_on_cpu (cpu, tlb_flush_global_async_work,RUN_ON_CPU_NULL ); }
@@ -189,6 +195,8 @@ static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)tlb_debug ("start: mmu_idx:0x%04lx\n" , mmu_idx_bitmask);qemu_spin_lock (&env->tlb_c.lock); + env->tlb_c.pending_flush &= ~mmu_idx_bitmask; +for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) {if (test_bit (mmu_idx, &mmu_idx_bitmask)) {
@@ -210,19 +218,22 @@ void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)tlb_debug ("mmu_idx: 0x%" PRIx16"\n" , idxmap);if (!qemu_cpu_is_self (cpu)) { -uint16_t pending_flushes = idxmap; - pending_flushes &= ~atomic_mb_read (&cpu->pending_tlb_flush); +CPUArchState *env = cpu->env_ptr; +uint16_t pending, to_clean; -if (pending_flushes) { -tlb_debug ("reduced mmu_idx: 0x%" PRIx16"\n" , pending_flushes); +qemu_spin_lock (&env->tlb_c.lock); + pending = env->tlb_c.pending_flush; + to_clean = idxmap & ~pending; + env->tlb_c.pending_flush = pending | idxmap; +qemu_spin_unlock (&env->tlb_c.lock); -atomic_or (&cpu->pending_tlb_flush, pending_flushes); +if (to_clean) { +tlb_debug ("reduced mmu_idx: 0x%" PRIx16"\n" , to_clean);async_run_on_cpu (cpu, tlb_flush_by_mmuidx_async_work, -RUN_ON_CPU_HOST_INT (pending_flushes)); +RUN_ON_CPU_HOST_INT (to_clean)); } }else { -tlb_flush_by_mmuidx_async_work (cpu, -RUN_ON_CPU_HOST_INT (idxmap)); +tlb_flush_by_mmuidx_async_work (cpu,RUN_ON_CPU_HOST_INT (idxmap)); } }
Signature · 1 line
2.17.2
PATCH 4/10 cputlb: Split large page tracking per mmu_idx
In reply to Richard HendersonThe set of large pages in the kernel is probably not the same as the set of large pages in the application. Forcing one range to cover both will flush more often than necessary.
This allows tlb_flush_page_async_work to flush just the one mmu_idx implicated, which in turn allows us to remove tlb_check_page_and_flush_by_mmuidx_async_work.
--- include/exec/cpu-defs.h | 14 +++- accel/tcg/cputlb.c | 139 ++++++++++++++++++---------------------- 2 files changed, 74 insertions(+), 79 deletions(-)
include/exec/cpu-defs.h +12 -2
@@ -141,6 +141,17 @@ typedef struct CPUIOTLBEntry {MemTxAttrs attrs; } CPUIOTLBEntry; +typedef struct CPUTLBDesc { +/* +* Describe a region covering all of the large pages allocated +* into the tlb. When any page within this region is flushed, +* we must flush the entire tlb. The region is matched if +* (addr & large_page_mask) == large_page_addr. +*/ +target_ulong large_page_addr; +target_ulong large_page_mask; +}CPUTLBDesc ; +/* * Data elements that are shared between all MMU modes. */
@@ -162,13 +173,12 @@ typedef struct CPUTLBCommon {*/ #define CPU_COMMON_TLB \ CPUTLBCommon tlb_c; \ + CPUTLBDesc tlb_d[NB_MMU_MODES]; \ CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE]; \ CPUTLBEntry tlb_v_table[NB_MMU_MODES][CPU_VTLB_SIZE]; \ CPUIOTLBEntry iotlb[NB_MMU_MODES][CPU_TLB_SIZE]; \ CPUIOTLBEntry iotlb_v[NB_MMU_MODES][CPU_VTLB_SIZE]; \ size_t tlb_flush_count; \ - target_ulong tlb_flush_addr; \ - target_ulong tlb_flush_mask; \ target_ulong vtlb_index; \#else
accel/tcg/cputlb.c +62 -77
250 lines not on this page · raw message
Signature · 1 line
2.17.2
Re: [Qemu-devel] [PATCH 04/10] cputlb: Split large page tracking per mmu_idx
Quoted 6 lines · Richard Henderson
accel/tcg/cputlb.c +6
-0+static void tlb_flush_page_locked (CPUArchState *env,int midx, +target_ulong addr) +{ +target_ulong lp_addr = env->tlb_d[midx].large_page_addr; +target_ulong lp_mask = env->tlb_d[midx].large_page_mask; +
accel/tcg/cputlb.c static inline void tlb_flush_vtlb_page_locked(CPUArchState *env, int mmu_idx, · line 307
Quoted 10 lines
accel/tcg/cputlb.c +10
-0+/* Check if we need to flush due to large pages. */ +if ((addr & lp_mask) == lp_addr) { +tlb_debug ("forcing full flush midx %d (" + TARGET_FMT_lx"/" TARGET_FMT_lx")\n" , + midx, lp_addr, lp_mask); +tlb_flush_one_mmuidx_locked (env, midx); + }else { +int pidx = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); +tlb_flush_entry_locked (&env->tlb_table[midx][pidx], addr); +tlb_flush_vtlb_page_locked (env, midx, addr);
Just noticed that we should use tlb_entry here, e.g.:
} else {
- int pidx = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
- tlb_flush_entry_locked(&env->tlb_table[midx][pidx], addr);
+ CPUTLBEntry *entry = tlb_entry(env, midx, addr);
+
+ tlb_flush_entry_locked(entry, addr);
tlb_flush_vtlb_page_locked(env, midx, addr);
}Thanks,
Emilio
Re: [Qemu-devel] [PATCH 04/10] cputlb: Split large page tracking per mmu_idx
Quoted 21 lines · Emilio G. Cota
Quoted 6 lines · Richard Henderson
accel/tcg/cputlb.c +6
-0+static void tlb_flush_page_locked (CPUArchState *env,int midx, +target_ulong addr) +{ +target_ulong lp_addr = env->tlb_d[midx].large_page_addr; +target_ulong lp_mask = env->tlb_d[midx].large_page_mask; +accel/tcg/cputlb.c static inline void tlb_flush_vtlb_page_locked(CPUArchState *env, int mmu_idx, · line 307
Quoted 10 lines
accel/tcg/cputlb.c +10
-0+/* Check if we need to flush due to large pages. */ +if ((addr & lp_mask) == lp_addr) { +tlb_debug ("forcing full flush midx %d (" + TARGET_FMT_lx"/" TARGET_FMT_lx")\n" , + midx, lp_addr, lp_mask); +tlb_flush_one_mmuidx_locked (env, midx); + }else { +int pidx = (addr >>TARGET_PAGE_BITS ) & (CPU_TLB_SIZE -1 ); +tlb_flush_entry_locked (&env->tlb_table[midx][pidx], addr); +tlb_flush_vtlb_page_locked (env, midx, addr);Just noticed that we should use
tlb_entryhere, e.g.:} else {
Review
Quoted 7 lines
(quoted diff) +3
-2- int pidx = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1); - tlb_flush_entry_locked(&env->tlb_table[midx][pidx], addr); + CPUTLBEntry *entry = tlb_entry(env, midx, addr); + + tlb_flush_entry_locked(entry, addr); tlb_flush_vtlb_page_locked(env, midx, addr); }
Fixed, thanks.
r~
PATCH 1/1 osdep: Work around MinGW assert
In reply to Richard HendersonIn several places we use assert(FEATURE), and assume that if FEATURE is disabled, all following code is removed as unreachable. Which allows us to compile-out functions that are only present with FEATURE, and have a link-time failure if the functions remain used.
MinGW does not mark its internal function _assert() as noreturn, so the compiler cannot see when code is unreachable, which leads to link errors for this host that are not present elsewhere.
The current build-time failure concerns 62823083b8a2, but I remember having seen this same error before. Fix it once and for all for MinGW.
--- include/qemu/osdep.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/qemu/osdep.h +12 -0
@@ -122,6 +122,18 @@ extern int daemon(int, int);#include "glib-compat.h" #include "qemu/typedefs.h" +/* +* For mingw, as of v6.0.0, the function implementing the assert macro is +* not marked a noreturn, so the compiler cannot delete code following an +* assert(false) as unused. We rely on this within the code base to delete +* code that is unreachable when features are disabled. +* All supported versions of Glib's g_assert() satisfy this requirement. +*/ +#ifdef __MINGW32__ +#undef assert +#define assert (x) g_assert(x) +#endif +/* * According to waitpid man page: * WCOREDUMP
Signature · 1 line
2.17.2
Re: [Qemu-devel] [PATCH, build fix] osdep: Work around MinGW assert
Quoted 5 lines · Richard Henderson
In several places we use assert(FEATURE), and assume that if FEATURE is disabled, all following code is removed as unreachable. Which allows us to compile-out functions that are only present with FEATURE, and have a link-time failure if the functions remain used.
Quoted 8 lines
MinGW does not mark its internal function
_assert()as noreturn, so the compiler cannot see when code is unreachable, which leads to link errors for this host that are not present elsewhere.The current build-time failure concerns
62823083b8a2, but I remember having seen this same error before. Fix it once and for all for MinGW.Signed-off-by Richard Henderson <[email protected]>
Quoted 28 lines
--- include/qemu/osdep.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)include/qemu/osdep.h +12
-0@@ -122,6 +122,18 @@ extern int daemon(int, int);#include "glib-compat.h" #include "qemu/typedefs.h" +/* +* For mingw, as of v6.0.0, the function implementing the assert macro is +* not marked a noreturn, so the compiler cannot delete code following an +* assert(false) as unused. We rely on this within the code base to delete +* code that is unreachable when features are disabled. +* All supported versions of Glib's g_assert() satisfy this requirement. +*/ +#ifdef __MINGW32__ +#undef assert +#define assert (x) g_assert(x) +#endif +/* * According to waitpid man page: * WCOREDUMP
PATCH 5/10 cputlb: Move env->vtlb_index to env->tlb_d.vindex
In reply to Richard HendersonThe rest of the tlb victim cache is per-tlb,
the next use index should be as well.
--- include/exec/cpu-defs.h | 5 +++-- accel/tcg/cputlb.c | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-)
include/exec/cpu-defs.h +3 -2
@@ -150,6 +150,8 @@ typedef struct CPUTLBDesc {*/ target_ulong large_page_addr;target_ulong large_page_mask; +/* The next index to use in the tlb victim table. */ +size_t vindex; } CPUTLBDesc;/*
@@ -178,8 +180,7 @@ typedef struct CPUTLBCommon {CPUTLBEntry tlb_v_table[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \CPUIOTLBEntry iotlb[NB_MMU_MODES ][CPU_TLB_SIZE ]; \CPUIOTLBEntry iotlb_v[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \ -size_t tlb_flush_count; \ -target_ulong vtlb_index; \ +size_t tlb_flush_count;#else
accel/tcg/cputlb.c +2 -3
@@ -119,6 +119,7 @@ static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx)memset (env->tlb_v_table[mmu_idx],-1 ,sizeof (env->tlb_v_table[0 ])); env->tlb_d[mmu_idx].large_page_addr =-1 ; env->tlb_d[mmu_idx].large_page_mask =-1 ; + env->tlb_d[mmu_idx].vindex =0 ; }/* This is OK because CPU architectures generally permit an
@@ -149,8 +150,6 @@ static void tlb_flush_nocheck(CPUState *cpu)qemu_spin_unlock (&env->tlb_c.lock);cpu_tb_jmp_cache_clear (cpu); - - env->vtlb_index =0 ; }static void tlb_flush_global_async_work (CPUState *cpu,run_on_cpu_data data)
@@ -668,7 +667,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,* different page; otherwise just overwrite the stale data. */ if (!tlb_hit_page_anyprot (te, vaddr_page)) { -unsigned vidx = env->vtlb_index++ %CPU_VTLB_SIZE ; +unsigned vidx = env->tlb_d[mmu_idx].vindex++ %CPU_VTLB_SIZE ;CPUTLBEntry *tv = &env->tlb_v_table[mmu_idx][vidx];/* Evict the old entry into the victim tlb. */
Signature · 1 line
2.17.2
Re: [Qemu-devel] [PATCH 05/10] cputlb: Move env->vtlb_index to env->tlb_d.vindex
Quoted 4 lines · Richard Henderson
The rest of the tlb victim cache is per-tlb,
the next use index should be as well.Signed-off-by Richard Henderson <[email protected]>
Quoted 59 lines
--- include/exec/cpu-defs.h | 5 +++-- accel/tcg/cputlb.c | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-)include/exec/cpu-defs.h +3
-2@@ -150,6 +150,8 @@ typedef struct CPUTLBDesc {*/ target_ulong large_page_addr;target_ulong large_page_mask; +/* The next index to use in the tlb victim table. */ +size_t vindex; } CPUTLBDesc;/* @@ -178,8 +180,7 @@ typedef struct CPUTLBCommon {CPUTLBEntry tlb_v_table[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \CPUIOTLBEntry iotlb[NB_MMU_MODES ][CPU_TLB_SIZE ]; \CPUIOTLBEntry iotlb_v[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \ -size_t tlb_flush_count; \ -target_ulong vtlb_index; \ +size_t tlb_flush_count;#else accel/tcg/cputlb.c +2
-3@@ -119,6 +119,7 @@ static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx)memset (env->tlb_v_table[mmu_idx],-1 ,sizeof (env->tlb_v_table[0 ])); env->tlb_d[mmu_idx].large_page_addr =-1 ; env->tlb_d[mmu_idx].large_page_mask =-1 ; + env->tlb_d[mmu_idx].vindex =0 ; }/* This is OK because CPU architectures generally permit an @@ -149,8 +150,6 @@ static void tlb_flush_nocheck(CPUState *cpu)qemu_spin_unlock (&env->tlb_c.lock);cpu_tb_jmp_cache_clear (cpu); - - env->vtlb_index =0 ; }static void tlb_flush_global_async_work (CPUState *cpu,run_on_cpu_data data)@@ -668,7 +667,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,* different page; otherwise just overwrite the stale data. */ if (!tlb_hit_page_anyprot (te, vaddr_page)) { -unsigned vidx = env->vtlb_index++ %CPU_VTLB_SIZE ; +unsigned vidx = env->tlb_d[mmu_idx].vindex++ %CPU_VTLB_SIZE ;CPUTLBEntry *tv = &env->tlb_v_table[mmu_idx][vidx];/* Evict the old entry into the victim tlb. */
PATCH 6/10 cputlb: Merge tlb_flush_nocheck into tlb_flush_by_mmuidx_async_work
In reply to Richard HendersonThe difference between the two sets of APIs is now miniscule.
This allows tlb_flush, tlb_flush_all_cpus, and tlb_flush_all_cpus_synced to be merged with their corresponding by_mmuidx functions as well. For accounting, consider mmu_idx_bitmask = ALL_MMUIDX_BITS to be a full flush.
--- accel/tcg/cputlb.c | 93 +++++++++++----------------------------------- 1 file changed, 21 insertions(+), 72 deletions(-)
accel/tcg/cputlb.c +21 -72
@@ -122,75 +122,6 @@ static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx) env->tlb_d[mmu_idx].vindex =0 ; } -/* This is OK because CPU architectures generally permit an -* implementation to drop entries from the TLB at any time, so -* flushing more entries than required is only an efficiency issue, -* not a correctness issue. -*/ -static void tlb_flush_nocheck (CPUState *cpu) -{ -CPUArchState *env = cpu->env_ptr; -int mmu_idx; - -assert_cpu_is_self (cpu); -atomic_set (&env->tlb_flush_count, env->tlb_flush_count +1 ); -tlb_debug ("(count: %zu)\n" ,tlb_flush_count ()); - -/* -* tlb_table/tlb_v_table updates from any thread must hold tlb_c.lock. -* However, updates from the owner thread (as is the case here; see the -* above assert_cpu_is_self) do not need atomic_set because all reads -* that do not hold the lock are performed by the same owner thread. -*/ -qemu_spin_lock (&env->tlb_c.lock); - env->tlb_c.pending_flush =0 ; -for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) { -tlb_flush_one_mmuidx_locked (env, mmu_idx); - } -qemu_spin_unlock (&env->tlb_c.lock); - -cpu_tb_jmp_cache_clear (cpu); -} - -static void tlb_flush_global_async_work (CPUState *cpu,run_on_cpu_data data) -{ -tlb_flush_nocheck (cpu); -} - -void tlb_flush (CPUState *cpu) -{ -if (cpu->created && !qemu_cpu_is_self (cpu)) { -CPUArchState *env = cpu->env_ptr; -uint16_t pending; - -qemu_spin_lock (&env->tlb_c.lock); - pending = env->tlb_c.pending_flush; - env->tlb_c.pending_flush =ALL_MMUIDX_BITS ; -qemu_spin_unlock (&env->tlb_c.lock); - -if (pending !=ALL_MMUIDX_BITS ) { -async_run_on_cpu (cpu, tlb_flush_global_async_work, -RUN_ON_CPU_NULL ); - } - }else { -tlb_flush_nocheck (cpu); - } -} - -void tlb_flush_all_cpus (CPUState *src_cpu) -{ -const run_on_cpu_func fn = tlb_flush_global_async_work; -flush_all_helper (src_cpu, fn,RUN_ON_CPU_NULL ); -fn (src_cpu,RUN_ON_CPU_NULL ); -} - -void tlb_flush_all_cpus_synced (CPUState *src_cpu) -{ -const run_on_cpu_func fn = tlb_flush_global_async_work; -flush_all_helper (src_cpu, fn,RUN_ON_CPU_NULL ); -async_safe_run_on_cpu (src_cpu, fn,RUN_ON_CPU_NULL ); -} -static void tlb_flush_by_mmuidx_async_work (CPUState *cpu,run_on_cpu_data data) {CPUArchState *env = cpu->env_ptr;
@@ -212,13 +143,17 @@ static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)qemu_spin_unlock (&env->tlb_c.lock);cpu_tb_jmp_cache_clear (cpu); + +if (mmu_idx_bitmask ==ALL_MMUIDX_BITS ) { +atomic_set (&env->tlb_flush_count, env->tlb_flush_count +1 ); + } }void tlb_flush_by_mmuidx (CPUState *cpu,uint16_t idxmap) {tlb_debug ("mmu_idx: 0x%" PRIx16"\n" , idxmap); -if (!qemu_cpu_is_self (cpu)) { +if (cpu->created && !qemu_cpu_is_self (cpu)) {CPUArchState *env = cpu->env_ptr;uint16_t pending, to_clean;
@@ -238,6 +173,11 @@ void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap) } } +void tlb_flush (CPUState *cpu) +{ +tlb_flush_by_mmuidx (cpu,ALL_MMUIDX_BITS ); +} +void tlb_flush_by_mmuidx_all_cpus (CPUState *src_cpu,uint16_t idxmap) {const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
@@ -248,8 +188,12 @@ void tlb_flush_by_mmuidx_all_cpus(CPUState *src_cpu, uint16_t idxmap)fn (src_cpu,RUN_ON_CPU_HOST_INT (idxmap)); } -void tlb_flush_by_mmuidx_all_cpus_synced (CPUState *src_cpu, -uint16_t idxmap) +void tlb_flush_all_cpus (CPUState *src_cpu) +{ +tlb_flush_by_mmuidx_all_cpus (src_cpu,ALL_MMUIDX_BITS ); +} + +void tlb_flush_by_mmuidx_all_cpus_synced (CPUState *src_cpu,uint16_t idxmap) {const run_on_cpu_func fn = tlb_flush_by_mmuidx_async_work;
@@ -259,6 +203,11 @@ void tlb_flush_by_mmuidx_all_cpus_synced(CPUState *src_cpu,async_safe_run_on_cpu (src_cpu, fn,RUN_ON_CPU_HOST_INT (idxmap)); } +void tlb_flush_all_cpus_synced (CPUState *src_cpu) +{ +tlb_flush_by_mmuidx_all_cpus_synced (src_cpu,ALL_MMUIDX_BITS ); +} +static inline bool tlb_hit_page_anyprot (CPUTLBEntry *tlb_entry,target_ulong page) {
Signature · 1 line
2.17.2
PATCH 7/10 cputlb: Merge tlb_flush_page into tlb_flush_page_by_mmuidx
In reply to Richard HendersonThe difference between the two sets of APIs is now miniscule.
--- accel/tcg/cputlb.c | 58 ++++++++++------------------------------------ 1 file changed, 12 insertions(+), 46 deletions(-)
accel/tcg/cputlb.c +12 -46
@@ -256,38 +256,6 @@ static void tlb_flush_page_locked(CPUArchState *env, int midx, } } -static void tlb_flush_page_async_work (CPUState *cpu,run_on_cpu_data data) -{ -CPUArchState *env = cpu->env_ptr; -target_ulong addr = (target_ulong ) data.target_ptr; -int mmu_idx; - -assert_cpu_is_self (cpu); - -tlb_debug ("page addr:" TARGET_FMT_lx"\n" , addr); - - addr &=TARGET_PAGE_MASK ; -qemu_spin_lock (&env->tlb_c.lock); -for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) { -tlb_flush_page_locked (env, mmu_idx, addr); - } -qemu_spin_unlock (&env->tlb_c.lock); - -tb_flush_jmp_cache (cpu, addr); -} - -void tlb_flush_page (CPUState *cpu,target_ulong addr) -{ -tlb_debug ("page :" TARGET_FMT_lx"\n" , addr); - -if (!qemu_cpu_is_self (cpu)) { -async_run_on_cpu (cpu, tlb_flush_page_async_work, -RUN_ON_CPU_TARGET_PTR (addr)); - }else { -tlb_flush_page_async_work (cpu,RUN_ON_CPU_TARGET_PTR (addr)); - } -} -/* As we are going to hijack the bottom bits of the page address for a * mmuidx bit mask we need to fail to build if we can't do that */
@@ -337,6 +305,11 @@ void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, uint16_t idxmap) } } +void tlb_flush_page (CPUState *cpu,target_ulong addr) +{ +tlb_flush_page_by_mmuidx (cpu, addr,ALL_MMUIDX_BITS ); +} +void tlb_flush_page_by_mmuidx_all_cpus (CPUState *src_cpu,target_ulong addr,uint16_t idxmap) {
@@ -353,6 +326,11 @@ void tlb_flush_page_by_mmuidx_all_cpus(CPUState *src_cpu, target_ulong addr,fn (src_cpu,RUN_ON_CPU_TARGET_PTR (addr_and_mmu_idx)); } +void tlb_flush_page_all_cpus (CPUState *src,target_ulong addr) +{ +tlb_flush_page_by_mmuidx_all_cpus (src, addr,ALL_MMUIDX_BITS ); +} +void tlb_flush_page_by_mmuidx_all_cpus_synced (CPUState *src_cpu,target_ulong addr,uint16_t idxmap)
@@ -370,21 +348,9 @@ void tlb_flush_page_by_mmuidx_all_cpus_synced(CPUState *src_cpu,async_safe_run_on_cpu (src_cpu, fn,RUN_ON_CPU_TARGET_PTR (addr_and_mmu_idx)); } -void tlb_flush_page_all_cpus (CPUState *src,target_ulong addr) +void tlb_flush_page_all_cpus_synced (CPUState *src,target_ulong addr) { -const run_on_cpu_func fn = tlb_flush_page_async_work; - -flush_all_helper (src, fn,RUN_ON_CPU_TARGET_PTR (addr)); -fn (src,RUN_ON_CPU_TARGET_PTR (addr)); -} - -void tlb_flush_page_all_cpus_synced (CPUState *src, -target_ulong addr) -{ -const run_on_cpu_func fn = tlb_flush_page_async_work; - -flush_all_helper (src, fn,RUN_ON_CPU_TARGET_PTR (addr)); -async_safe_run_on_cpu (src, fn,RUN_ON_CPU_TARGET_PTR (addr)); +tlb_flush_page_by_mmuidx_all_cpus_synced (src, addr,ALL_MMUIDX_BITS ); }/* update the TLBs so that writes to code in the virtual page 'addr'
Signature · 1 line
2.17.2
PATCH 8/10 cputlb: Count "partial" and "elided" tlb flushes
In reply to Richard HendersonOur only statistic so far was "full" tlb flushes, where all mmu_idx are flushed at the same time.
Now count "partial" tlb flushes where sets of mmu_idx are flushed, but the set is not maximal. Account one per mmu_idx flushed, as that is the unit of work performed.
We don't actually count elided flushes yet, but go ahead and change the interface presented to the monitor all at once.
--- include/exec/cpu-defs.h | 12 ++++++++++-- include/exec/cputlb.h | 2 +- accel/tcg/cputlb.c | 18 +++++++++++++----- accel/tcg/translate-all.c | 8 ++++++-- 4 files changed, 30 insertions(+), 10 deletions(-)
include/exec/cpu-defs.h +10 -2
@@ -166,6 +166,15 @@ typedef struct CPUTLBCommon {* mmu_idx may be discarded. Protected by tlb_c.lock. */ uint16_t pending_flush; + +/* +* Statistics. These are not lock protected, but are read and +* written atomically. This allows the monitor to print a snapshot +* of the stats without interfering with the cpu. +*/ +size_t full_flush_count; +size_t part_flush_count; +size_t elide_flush_count; } CPUTLBCommon;/*
@@ -179,8 +188,7 @@ typedef struct CPUTLBCommon {CPUTLBEntry tlb_table[NB_MMU_MODES ][CPU_TLB_SIZE ]; \CPUTLBEntry tlb_v_table[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \CPUIOTLBEntry iotlb[NB_MMU_MODES ][CPU_TLB_SIZE ]; \ -CPUIOTLBEntry iotlb_v[NB_MMU_MODES ][CPU_VTLB_SIZE ]; \ -size_t tlb_flush_count; +CPUIOTLBEntry iotlb_v[NB_MMU_MODES ][CPU_VTLB_SIZE ];#else
include/exec/cputlb.h +1 -1
@@ -23,6 +23,6 @@/* cputlb.c */ void tlb_protect_code (ram_addr_t ram_addr);void tlb_unprotect_code (ram_addr_t ram_addr); -size_t tlb_flush_count (void ); +void tlb_flush_counts (size_t *full,size_t *part,size_t *elide);#endif #endif
accel/tcg/cputlb.c +13 -5
@@ -100,17 +100,21 @@ static void flush_all_helper(CPUState *src, run_on_cpu_func fn, } } -size_t tlb_flush_count (void ) +void tlb_flush_counts (size_t *pfull,size_t *ppart,size_t *pelide) {CPUState *cpu; -size_t count =0 ; +size_t full =0 , part =0 , elide =0 ;CPU_FOREACH (cpu) {CPUArchState *env = cpu->env_ptr; - count +=atomic_read (&env->tlb_flush_count); + full +=atomic_read (&env->tlb_c.full_flush_count); + part +=atomic_read (&env->tlb_c.part_flush_count); + elide +=atomic_read (&env->tlb_c.elide_flush_count); } -return count; + *pfull = full; + *ppart = part; + *pelide = elide; }static void tlb_flush_one_mmuidx_locked (CPUArchState *env,int mmu_idx)
@@ -145,7 +149,11 @@ static void tlb_flush_by_mmuidx_async_work(CPUState *cpu, run_on_cpu_data data)cpu_tb_jmp_cache_clear (cpu);if (mmu_idx_bitmask ==ALL_MMUIDX_BITS ) { -atomic_set (&env->tlb_flush_count, env->tlb_flush_count +1 ); +atomic_set (&env->tlb_c.full_flush_count, + env->tlb_c.full_flush_count +1 ); + }else { +atomic_set (&env->tlb_c.part_flush_count, + env->tlb_c.part_flush_count +ctpop16 (mmu_idx_bitmask)); } }
accel/tcg/translate-all.c +6 -2
@@ -2290,7 +2290,7 @@ void dump_exec_info(FILE *f, fprintf_function cpu_fprintf) {struct tb_tree_stats tst = {};struct qht_stats hst; -size_t nb_tbs; +size_t nb_tbs, flush_full, flush_part, flush_elide;tcg_tb_foreach (tb_tree_stats_iter, &tst); nb_tbs = tst.nb_tbs;
@@ -2326,7 +2326,11 @@ void dump_exec_info(FILE *f, fprintf_function cpu_fprintf)cpu_fprintf (f,"TB flush count %u\n" ,atomic_read (&tb_ctx.tb_flush_count));cpu_fprintf (f,"TB invalidate count %zu\n" ,tcg_tb_phys_invalidate_count ()); -cpu_fprintf (f,"TLB flush count %zu\n" ,tlb_flush_count ()); + +tlb_flush_counts (&flush_full, &flush_part, &flush_elide); +cpu_fprintf (f,"TLB full flushes %zu\n" , flush_full); +cpu_fprintf (f,"TLB partial flushes %zu\n" , flush_part); +cpu_fprintf (f,"TLB elided flushes %zu\n" , flush_elide);tcg_dump_info (f, cpu_fprintf); }
Signature · 1 line
2.17.2
PATCH 9/10 cputlb: Filter flushes on already clean tlbs
In reply to Richard HendersonEspecially for guests with large numbers of tlbs, like ARM or PPC, we may well not use all of them in between flush operations. Remember which tlbs have been used since the last flush, and avoid any useless flushing.
--- include/exec/cpu-defs.h | 7 ++++++- accel/tcg/cputlb.c | 35 +++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-)
include/exec/cpu-defs.h +6 -1
@@ -166,7 +166,12 @@ typedef struct CPUTLBCommon {* mmu_idx may be discarded. Protected by tlb_c.lock. */ uint16_t pending_flush; - +/* +* Within dirty, for each bit N, modifications have been made to +* mmu_idx N since the last time that mmu_idx was flushed. +* Protected by tlb_c.lock. +*/ +uint16_t dirty;/* * Statistics. These are not lock protected, but are read and * written atomically. This allows the monitor to print a snapshot
accel/tcg/cputlb.c +25 -10
@@ -79,6 +79,9 @@ void tlb_init(CPUState *cpu)CPUArchState *env = cpu->env_ptr;qemu_spin_init (&env->tlb_c.lock); + +/* Ensure that cpu_reset performs a full flush. */ + env->tlb_c.dirty =ALL_MMUIDX_BITS ; }/* flush_all_helper: run fn across all cpus
@@ -129,31 +132,40 @@ static void tlb_flush_one_mmuidx_locked(CPUArchState *env, int mmu_idx)static void tlb_flush_by_mmuidx_async_work (CPUState *cpu,run_on_cpu_data data) {CPUArchState *env = cpu->env_ptr; -unsigned long mmu_idx_bitmask = data.host_int; -int mmu_idx; +uint16_t asked = data.host_int; +uint16_t all_dirty, work, to_clean;assert_cpu_is_self (cpu); -tlb_debug ("mmu_idx:0x%04lx\n" , mmu_idx_bitmask); +tlb_debug ("mmu_idx:0x%04" PRIx16"\n" , asked);qemu_spin_lock (&env->tlb_c.lock); - env->tlb_c.pending_flush &= ~mmu_idx_bitmask; -for (mmu_idx =0 ; mmu_idx <NB_MMU_MODES ; mmu_idx++) { -if (test_bit (mmu_idx, &mmu_idx_bitmask)) { -tlb_flush_one_mmuidx_locked (env, mmu_idx); - } + all_dirty = env->tlb_c.dirty; + to_clean = asked & all_dirty; + all_dirty &= ~to_clean; + env->tlb_c.dirty = all_dirty; + +for (work = to_clean; work !=0 ; work &= work -1 ) { +int mmu_idx =ctz32 (work); +tlb_flush_one_mmuidx_locked (env, mmu_idx); } +qemu_spin_unlock (&env->tlb_c.lock);cpu_tb_jmp_cache_clear (cpu); -if (mmu_idx_bitmask ==ALL_MMUIDX_BITS ) { +if (to_clean ==ALL_MMUIDX_BITS ) {atomic_set (&env->tlb_c.full_flush_count, env->tlb_c.full_flush_count +1 ); }else {atomic_set (&env->tlb_c.part_flush_count, - env->tlb_c.part_flush_count +ctpop16 (mmu_idx_bitmask)); + env->tlb_c.part_flush_count +ctpop16 (to_clean)); +if (to_clean != asked) { +atomic_set (&env->tlb_c.elide_flush_count, + env->tlb_c.elide_flush_count + +ctpop16 (asked & ~to_clean)); + } } }
@@ -582,6 +594,9 @@ void tlb_set_page_with_attrs(CPUState *cpu, target_ulong vaddr,*/ qemu_spin_lock (&env->tlb_c.lock); +/* Note that the tlb is no longer clean. */ + env->tlb_c.dirty |=1 << mmu_idx; +/* Make sure there's no cached translation for the new page. */ tlb_flush_vtlb_page_locked (env, mmu_idx, vaddr_page);
Signature · 1 line
2.17.2
PATCH 10/10 cputlb: Remove tlb_c.pending_flushes
In reply to Richard HendersonThis is essentially redundant with tlb_c.dirty.
[??? Collect data to back up this supposition]
--- include/exec/cpu-defs.h | 6 ------ accel/tcg/cputlb.c | 16 ++-------------- 2 files changed, 2 insertions(+), 20 deletions(-)
include/exec/cpu-defs.h +0 -6
@@ -160,12 +160,6 @@ typedef struct CPUTLBDesc { typedef struct CPUTLBCommon { /* Serialize updates to tlb_table and tlb_v_table, and others as noted. */ QemuSpin lock; - /* - * Within pending_flush, for each bit N, there exists an outstanding - * cross-cpu flush for mmu_idx N. Further cross-cpu flushes to that - * mmu_idx may be discarded. Protected by tlb_c.lock. - */ - uint16_t pending_flush; /* * Within dirty, for each bit N, modifications have been made to * mmu_idx N since the last time that mmu_idx was flushed.
accel/tcg/cputlb.c +2 -14
@@ -174,20 +174,8 @@ void tlb_flush_by_mmuidx(CPUState *cpu, uint16_t idxmap)tlb_debug ("mmu_idx: 0x%" PRIx16"\n" , idxmap);if (cpu->created && !qemu_cpu_is_self (cpu)) { -CPUArchState *env = cpu->env_ptr; -uint16_t pending, to_clean; - -qemu_spin_lock (&env->tlb_c.lock); - pending = env->tlb_c.pending_flush; - to_clean = idxmap & ~pending; - env->tlb_c.pending_flush = pending | idxmap; -qemu_spin_unlock (&env->tlb_c.lock); - -if (to_clean) { -tlb_debug ("reduced mmu_idx: 0x%" PRIx16"\n" , to_clean); -async_run_on_cpu (cpu, tlb_flush_by_mmuidx_async_work, -RUN_ON_CPU_HOST_INT (to_clean)); - } +async_run_on_cpu (cpu, tlb_flush_by_mmuidx_async_work, +RUN_ON_CPU_HOST_INT (idxmap)); }else {tlb_flush_by_mmuidx_async_work (cpu,RUN_ON_CPU_HOST_INT (idxmap)); }
Signature · 1 line
2.17.2
Quoted 6 lines · Richard Henderson
The motivation here is reducing the total overhead.
Before a few patches went into target-arm.next, I measured total tlb flush overhead for aarch64 at 25%. This appears to reduce the total overhead to about 5% (I do need to re-run the control tests, not just watch perf top as I'm doing now).
I'd like to see those absolute perf numbers; I ran a few Ubuntu aarch64 boots and the noise is just too high to draw any conclusions (I'm using your tlb-dirty branch on github).
When booting the much smaller debian image, these patches are performance-neutral though. So,
for the series.
(On a pedantic note: consider s/miniscule/minuscule/ in patches 6-7)
Quoted 5 lines
The final patch is somewhat of an RFC. I'd like to know what benchmark was used when putting in
pending_tlb_flushes, and I have not done any archaeology to find out. I suspect that it does make any measurable difference beyond tlb_c.dirty, and I think the code is a bit cleaner without it.
I suspect that pending_tlb_flushes was premature optimization. Avoiding an async job sounds like a good idea, since it is very expensive for the remote vCPU.
However, in most cases we'll be taking a lock (or a full barrier in the original code) but we won't avoid the async job (because a race when flushing other vCPUs is unlikely), therefore wasting cycles in the lock (formerly barrier).
Thanks,
Emilio