Skip to contentSkip to conversation

separate migration bitmap

RFC PATCH Paolo Bonzini 2 participants 3 messages Aug 20, 2011 – Aug 22, 2011

lore
Share & tools
lore
#0 lore raw
Quoted 14 lines · Umesh Deshpande
(quoted diff) +8 -0
@@ -2128,8 +2132,61 @@ void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
                                       start1, length);
         }
     }
+
 }
 
+void migration_bitmap_reset_dirty(ram_addr_t start, ram_addr_t end,
+                                  int dirty_flags)
+{
+    unsigned long length, start1;
+
+    start &= TARGET_PAGE_MASK;
+    end = TARGET_PAGE_ALIGN(end);

Review void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, · line 2154

Quoted 10 lines
(quoted diff) +10 -0
+
+    length = end - start;
+    if (length == 0) {
+        return;
+    }
+
+    migration_bitmap_mask_dirty_range(start, length, dirty_flags);
+
+    /* we modify the TLB cache so that the dirty bit will be set again
+       when accessing the range */

The comment does not apply here, and the code below can also be safely deleted.

Quoted 18 lines
(quoted diff) +18 -0
+    start1 = (unsigned long)qemu_safe_ram_ptr(start);
+    /* Check that we don't span multiple blocks - this breaks the
+       address comparisons below.  */
+    if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1
+            != (end - 1) - start) {
+        abort();
+    }
+}
+
+void sync_migration_bitmap(ram_addr_t start, ram_addr_t end)
+{
+    unsigned long length, len, i;
+    ram_addr_t addr;
+    start &= TARGET_PAGE_MASK;
+    end = TARGET_PAGE_ALIGN(end);
+
+    length = end - start;
+    if (length == 0) {

Review void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, · line 2182

Quoted 10 lines
(quoted diff) +10 -0
+        return;
+    }
+
+    len = length >> TARGET_PAGE_BITS;
+    for (i = 0; i < len; i++) {
+        addr = i << TARGET_PAGE_BITS;
+        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) {
+            migration_bitmap_set_dirty(addr);
+            cpu_physical_memory_reset_dirty(addr, addr + TARGET_PAGE_SIZE,
+                                            MIGRATION_DIRTY_FLAG);

This should be run under the iothread lock. Pay attention to avoiding lock inversion: the I/O thread always takes the iothread lock outside and the ramlist lock within, so the migration thread must do the same.

BTW, I think this code in the migration thread patch also needs the iothread lock:

Review void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,

Quoted 10 lines
(quoted diff) +0 -0
    if (stage < 0) {
        cpu_physical_memory_set_dirty_tracking(0);
        return 0;
    }
 
    if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
        qemu_file_set_error(f);
        return 0;
    }

Finally, here:

Quoted 11 lines
        /* Make sure all dirty bits are set */
        QLIST_FOREACH(block, &ram_list.blocks, next) {
            for (addr = block->offset; addr < block->offset + block->length;
                 addr += TARGET_PAGE_SIZE) {
                if (!migration_bitmap_get_dirty(addr,
                                                   MIGRATION_DIRTY_FLAG)) {
                    migration_bitmap_set_dirty(addr);
                }
            }
        }

... you can skip the get_dirty operation since we are not interested in other flags than the migration flag for the migration-specific bitmap.

Paolo

#1 lore raw
Quoted 62 lines · Paolo Bonzini
Quoted 15 lines · Umesh Deshpande
(quoted diff) +8 -0
@@ -2128,8 +2132,61 @@ void 
 cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end,
                                       start1, length);
         }
     }
+
 }
 
+void migration_bitmap_reset_dirty(ram_addr_t start, ram_addr_t end,
+                                  int dirty_flags)
+{
+    unsigned long length, start1;
+
+    start &= TARGET_PAGE_MASK;
+    end = TARGET_PAGE_ALIGN(end);

Review void · line 2155

Quoted 10 lines
(quoted diff) +10 -0
+
+    length = end - start;
+    if (length == 0) {
+        return;
+    }
+
+    migration_bitmap_mask_dirty_range(start, length, dirty_flags);
+
+    /* we modify the TLB cache so that the dirty bit will be set again
+       when accessing the range */

The comment does not apply here, and the code below can also be safely deleted.

Quoted 20 lines
(quoted diff) +20 -0
+    start1 = (unsigned long)qemu_safe_ram_ptr(start);
+    /* Check that we don't span multiple blocks - this breaks the
+       address comparisons below.  */
+    if ((unsigned long)qemu_safe_ram_ptr(end - 1) - start1
+            != (end - 1) - start) {
+        abort();
+    }
+}
+
+void sync_migration_bitmap(ram_addr_t start, ram_addr_t end)
+{
+    unsigned long length, len, i;
+    ram_addr_t addr;
+    start &= TARGET_PAGE_MASK;
+    end = TARGET_PAGE_ALIGN(end);
+
+    length = end - start;
+    if (length == 0) {
+        return;
+    }

Review void · line 2185

Quoted 10 lines
(quoted diff) +8 -0
+
+    len = length >> TARGET_PAGE_BITS;
+    for (i = 0; i < len; i++) {
+        addr = i << TARGET_PAGE_BITS;
+        if (cpu_physical_memory_get_dirty(addr, 
 MIGRATION_DIRTY_FLAG)) {
+            migration_bitmap_set_dirty(addr);
+            cpu_physical_memory_reset_dirty(addr, addr + 
 TARGET_PAGE_SIZE,
+                                            MIGRATION_DIRTY_FLAG);

This should be run under the iothread lock. Pay attention to avoiding lock inversion: the I/O thread always takes the iothread lock outside and the ramlist lock within, so the migration thread must do the same.

Quoted 12 lines

BTW, I think this code in the migration thread patch also needs the iothread lock:

Quoted 10 lines
    if (stage < 0) {
        cpu_physical_memory_set_dirty_tracking(0);
        return 0;
    }

    if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
        qemu_file_set_error(f);
        return 0;
    }

Callers of above code snippets (sync_migration_bitmap etc.) are holding the iothread mutex. It has been made sure that the original qemu dirty bitmap is only accessed when holding the mutex.

Quoted 10 lines

Finally, here:

Quoted 7 lines
        /* Make sure all dirty bits are set */
        QLIST_FOREACH(block, &ram_list.blocks, next) {
            for (addr = block->offset; addr < block->offset + 
block->length;
                 addr += TARGET_PAGE_SIZE) {
                if (!migration_bitmap_get_dirty(addr,
Quoted 8 lines
Quoted 6 lines
MIGRATION_DIRTY_FLAG)) {
                    migration_bitmap_set_dirty(addr);
                }
            }
        }

... you can skip the get_dirty operation since we are not interested in other flags than the migration flag for the migration-specific bitmap.

okay

Thanks
Umesh

#2 lore raw
Quoted 13 lines · Umesh Deshpande
Quoted 13 lines

This should be run under the iothread lock. Pay attention to avoiding lock inversion: the I/O thread always takes the iothread lock outside and the ramlist lock within, so the migration thread must do the same.

BTW, I think this code in the migration thread patch also needs the iothread lock:

Quoted 5 lines
    if (stage < 0) {
        cpu_physical_memory_set_dirty_tracking(0);
        return 0;
    }
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
    if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
        qemu_file_set_error(f);
        return 0;
    }

Callers of above code snippets (sync_migration_bitmap etc.) are holding the iothread mutex. It has been made sure that the original qemu dirty bitmap is only accessed when holding the mutex.

But you cannot do it like in this patch, because here you have a deadlock:

Quoted 9 lines
(quoted diff) +5 -0
+    if (stage != 3) {
+        qemu_mutex_lock_ramlist();
+        qemu_mutex_unlock_iothread();
+    }
+
     while (!qemu_file_rate_limit(f)) {
         int bytes_sent;

Review int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) · line 319

Quoted 5 lines
(quoted diff) +2 -0
@@ -308,6 +315,11 @@ int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
         }
     }
 
+    if (stage != 3) {
+        qemu_mutex_lock_iothread();

Lock order: ramlist, iothread. The I/O thread instead takes the iothread lock outside and the ramlist lock inside. All this makes me even more convinced that you're locking is both too coarse and too complicated (perhaps it's not complicated, it's just under-documented; but the coarseness problem is there and it's what causes these lock inversions).

Review int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque) · line 322

Quoted 3 lines
(quoted diff) +3 -0
+        qemu_mutex_unlock_ramlist();
+    }
+

Paolo