[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 32/62] memory: replace cpu_physical_memory_reset_dirt
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PULL 32/62] memory: replace cpu_physical_memory_reset_dirty() with test-and-clear |
Date: |
Fri, 5 Jun 2015 17:15:33 +0200 |
From: Stefan Hajnoczi <address@hidden>
The cpu_physical_memory_reset_dirty() function is sometimes used
together with cpu_physical_memory_get_dirty(). This is not atomic since
two separate accesses to the dirty memory bitmap are made.
Turn cpu_physical_memory_reset_dirty() and
cpu_physical_memory_clear_dirty_range_type() into the atomic
cpu_physical_memory_test_and_clear_dirty().
Signed-off-by: Stefan Hajnoczi <address@hidden>
Message-Id: <address@hidden>
Reviewed-by: Fam Zheng <address@hidden>
Signed-off-by: Paolo Bonzini <address@hidden>
---
cputlb.c | 4 ++--
exec.c | 23 +++++++++++++++++------
include/exec/ram_addr.h | 33 ++++++++++-----------------------
memory.c | 11 ++++-------
4 files changed, 33 insertions(+), 38 deletions(-)
diff --git a/cputlb.c b/cputlb.c
index dd1203b..a506086 100644
--- a/cputlb.c
+++ b/cputlb.c
@@ -125,8 +125,8 @@ void tlb_flush_page(CPUState *cpu, target_ulong addr)
can be detected */
void tlb_protect_code(ram_addr_t ram_addr)
{
- cpu_physical_memory_reset_dirty(ram_addr, TARGET_PAGE_SIZE,
- DIRTY_MEMORY_CODE);
+ cpu_physical_memory_test_and_clear_dirty(ram_addr, TARGET_PAGE_SIZE,
+ DIRTY_MEMORY_CODE);
}
/* update the TLB so that writes in physical page 'phys_addr' are no longer
diff --git a/exec.c b/exec.c
index 162c579..487583b 100644
--- a/exec.c
+++ b/exec.c
@@ -852,16 +852,27 @@ static void tlb_reset_dirty_range_all(ram_addr_t start,
ram_addr_t length)
}
/* Note: start and end must be within the same ram block. */
-void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t length,
- unsigned client)
+bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
+ ram_addr_t length,
+ unsigned client)
{
- if (length == 0)
- return;
- cpu_physical_memory_clear_dirty_range_type(start, length, client);
+ unsigned long end, page;
+ bool dirty;
- if (tcg_enabled()) {
+ if (length == 0) {
+ return false;
+ }
+
+ end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
+ page = start >> TARGET_PAGE_BITS;
+ dirty = bitmap_test_and_clear_atomic(ram_list.dirty_memory[client],
+ page, end - page);
+
+ if (dirty && tcg_enabled()) {
tlb_reset_dirty_range_all(start, length);
}
+
+ return dirty;
}
/* Called from RCU critical section */
diff --git a/include/exec/ram_addr.h b/include/exec/ram_addr.h
index 63db371..a2beea7 100644
--- a/include/exec/ram_addr.h
+++ b/include/exec/ram_addr.h
@@ -194,30 +194,19 @@ static inline void
cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap,
}
#endif /* not _WIN32 */
-static inline void cpu_physical_memory_clear_dirty_range_type(ram_addr_t start,
- ram_addr_t
length,
- unsigned client)
-{
- unsigned long end, page;
-
- assert(client < DIRTY_MEMORY_NUM);
- end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
- page = start >> TARGET_PAGE_BITS;
- bitmap_clear(ram_list.dirty_memory[client], page, end - page);
-}
+bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
+ ram_addr_t length,
+ unsigned client);
static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start,
ram_addr_t length)
{
- cpu_physical_memory_clear_dirty_range_type(start, length,
DIRTY_MEMORY_MIGRATION);
- cpu_physical_memory_clear_dirty_range_type(start, length,
DIRTY_MEMORY_VGA);
- cpu_physical_memory_clear_dirty_range_type(start, length,
DIRTY_MEMORY_CODE);
+ cpu_physical_memory_test_and_clear_dirty(start, length,
DIRTY_MEMORY_MIGRATION);
+ cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_VGA);
+ cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_CODE);
}
-void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t length,
- unsigned client);
-
static inline
uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest,
ram_addr_t start,
@@ -245,16 +234,14 @@ uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned
long *dest,
}
} else {
for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
- if (cpu_physical_memory_get_dirty(start + addr,
- TARGET_PAGE_SIZE,
- DIRTY_MEMORY_MIGRATION)) {
+ if (cpu_physical_memory_test_and_clear_dirty(
+ start + addr,
+ TARGET_PAGE_SIZE,
+ DIRTY_MEMORY_MIGRATION)) {
long k = (start + addr) >> TARGET_PAGE_BITS;
if (!test_and_set_bit(k, dest)) {
num_dirty++;
}
- cpu_physical_memory_reset_dirty(start + addr,
- TARGET_PAGE_SIZE,
- DIRTY_MEMORY_MIGRATION);
}
}
}
diff --git a/memory.c b/memory.c
index 418cac7..0c5d328 100644
--- a/memory.c
+++ b/memory.c
@@ -1468,13 +1468,9 @@ void memory_region_set_dirty(MemoryRegion *mr, hwaddr
addr,
bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
hwaddr size, unsigned client)
{
- bool ret;
assert(mr->terminates);
- ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client);
- if (ret) {
- cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
- }
- return ret;
+ return cpu_physical_memory_test_and_clear_dirty(mr->ram_addr + addr,
+ size, client);
}
@@ -1518,7 +1514,8 @@ void memory_region_reset_dirty(MemoryRegion *mr, hwaddr
addr,
hwaddr size, unsigned client)
{
assert(mr->terminates);
- cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
+ cpu_physical_memory_test_and_clear_dirty(mr->ram_addr + addr, size,
+ client);
}
int memory_region_get_fd(MemoryRegion *mr)
--
2.4.1
- [Qemu-devel] [PULL 24/62] exec: pass client mask to cpu_physical_memory_set_dirty_range, (continued)
- [Qemu-devel] [PULL 24/62] exec: pass client mask to cpu_physical_memory_set_dirty_range, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 23/62] translate-all: make less of tb_invalidate_phys_page_range depend on is_cpu_write_access, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 25/62] exec: invert return value of cpu_physical_memory_get_clean, rename, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 26/62] exec: only check relevant bitmaps for cleanliness, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 27/62] memory: do not touch code dirty bitmap unless TCG is enabled, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 28/62] bitmap: add atomic set functions, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 29/62] bitmap: add atomic test and clear, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 30/62] memory: use atomic ops for setting dirty memory bits, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 31/62] migration: move dirty bitmap sync to ram_addr.h, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 33/62] memory: make cpu_physical_memory_sync_dirty_bitmap() fully atomic, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 32/62] memory: replace cpu_physical_memory_reset_dirty() with test-and-clear,
Paolo Bonzini <=
- [Qemu-devel] [PULL 34/62] memory: use mr->ram_addr in "is this RAM?" assertions, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 35/62] icount: implement a new icount_sleep mode toggleing real-time cpu sleep, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 36/62] icount: add sleep parameter to the icount option to set icount_sleep mode, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 37/62] icount: print a warning if there is no more deadline in sleep=no mode, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 38/62] target-i386: introduce cpu_get_mem_attrs, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 40/62] target-i386: Use correct memory attributes for ioport accesses, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 41/62] target-i386: mask NMIs on entry to SMM, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 42/62] target-i386: set G=1 in SMM big real mode selectors, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 43/62] target-i386: wake up processors that receive an SMI, Paolo Bonzini, 2015/06/05
- [Qemu-devel] [PULL 44/62] pflash_cfi01: change big-endian property to BIT type, Paolo Bonzini, 2015/06/05