@@ -773,16 +773,17 @@ static void virtio_mem_device_realize(DeviceState *dev, Error **errp)
sizeof(struct virtio_mem_config));
vmem->vq = virtio_add_queue(vdev, 128, virtio_mem_handle_request);
- host_memory_backend_set_mapped(vmem->memdev, true);
- vmstate_register_ram(&vmem->memdev->mr, DEVICE(vmem));
- qemu_register_reset(virtio_mem_system_reset, vmem);
-
/*
- * Set ourselves as RamDiscardManager before the plug handler maps the
- * memory region and exposes it via an address space.
+ * Set ourselves as RamDiscardManager before we expose the memory region
+ * to the system (e.g., marking the RAMBlock migratable, mapping the
+ * region).
*/
memory_region_set_ram_discard_manager(&vmem->memdev->mr,
RAM_DISCARD_MANAGER(vmem));
+
+ host_memory_backend_set_mapped(vmem->memdev, true);
+ vmstate_register_ram(&vmem->memdev->mr, DEVICE(vmem));
+ qemu_register_reset(virtio_mem_system_reset, vmem);
}
static void virtio_mem_device_unrealize(DeviceState *dev)
@@ -790,14 +791,10 @@ static void virtio_mem_device_unrealize(DeviceState *dev)
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
VirtIOMEM *vmem = VIRTIO_MEM(dev);
- /*
- * The unplug handler unmapped the memory region, it cannot be
- * found via an address space anymore. Unset ourselves.
- */
- memory_region_set_ram_discard_manager(&vmem->memdev->mr, NULL);
qemu_unregister_reset(virtio_mem_system_reset, vmem);
vmstate_unregister_ram(&vmem->memdev->mr, DEVICE(vmem));
host_memory_backend_set_mapped(vmem->memdev, false);
+ memory_region_set_ram_discard_manager(&vmem->memdev->mr, NULL);
virtio_del_queue(vdev, 0);
virtio_cleanup(vdev);
g_free(vmem->bitmap);
Let's set the RamDiscardManager earlier, logically before we expose the RAM memory region to the system. This is a preparation for further changes and is logically cleaner: before we expose the RAM memory region to migration code, make sure we have the RamDiscardManager setup. Signed-off-by: David Hildenbrand <david@redhat.com> --- hw/virtio/virtio-mem.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)