memory: fix race between TCG and accesses to dirty bitmap
commit9458a9a1df1a4c719e24512394d548c1fc7abd22
authorPaolo Bonzini <pbonzini@redhat.com>
Tue, 6 Feb 2018 17:37:39 +0000 (6 18:37 +0100)
committerPaolo Bonzini <pbonzini@redhat.com>
Tue, 20 Aug 2019 15:26:20 +0000 (20 17:26 +0200)
tree2e7dd0685486a403fda9fb52c70406a04637ef8b
parent1e8a98b53867f61da9ca09f411288e2085d323c4
memory: fix race between TCG and accesses to dirty bitmap

There is a race between TCG and accesses to the dirty log:

      vCPU thread                  reader thread
      -----------------------      -----------------------
      TLB check -> slow path
        notdirty_mem_write
          write to RAM
          set dirty flag
                                   clear dirty flag
      TLB check -> fast path
                                   read memory
        write to RAM

Fortunately, in order to fix it, no change is required to the
vCPU thread.  However, the reader thread must delay the read after
the vCPU thread has finished the write.  This can be approximated
conservatively by run_on_cpu, which waits for the end of the current
translation block.

A similar technique is used by KVM, which has to do a synchronous TLB
flush after doing a test-and-clear of the dirty-page flags.

Reported-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
exec.c
include/exec/memory.h
memory.c
migration/ram.c