Skip to contentSkip to conversation

virtio_pmem: enable live migration support

RFC In review Pankaj Gupta 3 participants 8 messages Dec 31, 2021 – Jan 12, 2022

lore
Share & tools
lore
#0 lore raw

RFC 1/1 virtio_pmem: enable live migration support

From: Pankaj Gupta <[email protected]>>

Enable live migration support for virtio-pmem device. Tested this: with live migration on same host.

Need suggestion on below points to support virtio-pmem live migration between two separate host systems:

- There is still possibility of stale page cache page at the destination host which we cannot invalidate currently as done in 1] for write-back mode because virtio-pmem memory backend file is mmaped in guest address space and invalidating corresponding page cache pages would also fault all the other userspace process mappings on the same file. Or we make it strict no other process would mmap this backing file?

-- In commit 1] we first fsync and then invalidate all the pages from destination
page cache. fsync would sync the stale dirty page cache page, Is this the right thing to do as we might end up in data discrepency?

- Thinking, alternatively if we transfer active corresponding guest page cache pages information from active LRU list source to destination host and refault those pages. This would also help to enable hot page cache in destination host for the guest and solve stale page cache issue as well. How we can achieve this so that we make sure we get rid of all the stale page cache pages in destination host?

Looking for suggestions on recommended and feasible solution we can implement? Thank you!

1] dd577a26ff ("block/file-posix: implement bdrv_co_invalidate_cache() on Linux")

---
 hw/virtio/virtio-pmem.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
hw/virtio/virtio-pmem.c +13 -0
index d1aeb90a31..a19619a387 100644
@@ -123,6 +123,7 @@ static void virtio_pmem_realize(DeviceState *dev, Error **errp)
     }
 
     host_memory_backend_set_mapped(pmem->memdev, true);
+    vmstate_register_ram(&pmem->memdev->mr, DEVICE(pmem));
     virtio_init(vdev, TYPE_VIRTIO_PMEM, VIRTIO_ID_PMEM,
                 sizeof(struct virtio_pmem_config));
     pmem->rq_vq = virtio_add_queue(vdev, 128, virtio_pmem_flush);
@@ -133,6 +134,7 @@ static void virtio_pmem_unrealize(DeviceState *dev)
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VirtIOPMEM *pmem = VIRTIO_PMEM(dev);
 
+    vmstate_unregister_ram(&pmem->memdev->mr, DEVICE(pmem));
     host_memory_backend_set_mapped(pmem->memdev, false);
     virtio_delete_queue(pmem->rq_vq);
     virtio_cleanup(vdev);
@@ -157,6 +159,16 @@ static MemoryRegion *virtio_pmem_get_memory_region(VirtIOPMEM *pmem,
     return &pmem->memdev->mr;
 }
 
+static const VMStateDescription vmstate_virtio_pmem = {
+    .name = "virtio-pmem",
+    .minimum_version_id = 1,
+    .version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_VIRTIO_DEVICE,
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 static Property virtio_pmem_properties[] = {
     DEFINE_PROP_UINT64(VIRTIO_PMEM_ADDR_PROP, VirtIOPMEM, start, 0),
     DEFINE_PROP_LINK(VIRTIO_PMEM_MEMDEV_PROP, VirtIOPMEM, memdev,
@@ -171,6 +183,7 @@ static void virtio_pmem_class_init(ObjectClass *klass, void *data)
     VirtIOPMEMClass *vpc = VIRTIO_PMEM_CLASS(klass);
 
     device_class_set_props(dc, virtio_pmem_properties);
+    dc->vmsd = &vmstate_virtio_pmem;
 
     vdc->realize = virtio_pmem_realize;
     vdc->unrealize = virtio_pmem_unrealize;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 7 lines · Pankaj Gupta

From: Pankaj Gupta <[email protected]>>

Enable live migration support for virtio-pmem device. Tested this: with live migration on same host.

Need suggestion on below points to support virtio-pmem live migration between two separate host systems:

I assume emulated NVDIMMs would have the exact same issue, right?

There are two cases to consider I think:

1) Backing storage is migrated manually to the destination (i.e., a file that is copied/moved/transmitted during migration)

2) Backing storage is located on a shared network storage (i.e., a file that is not copied during migration)

IIRC you're concerned about 2).

Quoted 7 lines

- There is still possibility of stale page cache page at the destination host which we cannot invalidate currently as done in 1] for write-back mode because virtio-pmem memory backend file is mmaped in guest address space and invalidating corresponding page cache pages would also fault all the other userspace process mappings on the same file. Or we make it strict no other process would mmap this backing file?

I'd have assume that a simple fsync on the src once migration is about to switch over (e.g., pre_save/post_save handler) should be enough to trigger writeback to the backing storage, at which point the dst can take over. So handling the src is easy.

So is the issue that the dst might still have stale pagecache information, because it already accessed some of that file previously, correct?

Quoted 4 lines

-- In commit 1] we first fsync and then invalidate all the pages from destination
page cache. fsync would sync the stale dirty page cache page, Is this the right thing to do as we might end up in data discrepency?

It would be weird if

a) The src used/modified the file and fsync'ed the modifications back to backing storage
b) The dst has stale dirty pagecache pages that would result in a modification of backing storage during fsync()

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

IIRC, an existing mmap of the file on the dst should not really be problematic *as long as* we didn't actually access file content that way and faulted in the pages. So *maybe*, if we do the POSIX_FADV_DONTNEED on the dst before accessing file content via the mmap, there shouldn't be an issue. Unless the mmap itself is already problematic.

I think we can assume that once QEMU starts on the dst and wants to mmap the file that it's not mapped into any other process yet. vhost-user will only mmap *after* being told from QEMU about the mmap region and the location in GPA.

So if the existing QEMU mmap is not problematic, it should be easy, just do the POSIX_FADV_DONTNEED on the destination when initializing virtio-pmem. If we have to POSIX_FADV_DONTNEED *before* performing the mmap, we might need a way to tell QEMU to POSIX_FADV_DONTNEED before doing the mmap. The could be a parameter for memory-backend-file like "flush=on", or doing that implicitly when we're told that we expect an incoming migration.

Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw

Thank you David for replying!

Quoted 12 lines
Quoted 7 lines

From: Pankaj Gupta <[email protected]>>

Enable live migration support for virtio-pmem device. Tested this: with live migration on same host.

Need suggestion on below points to support virtio-pmem live migration between two separate host systems:

I assume emulated NVDIMMs would have the exact same issue, right?

There are two cases to consider I think:

Quoted 7 lines

1) Backing storage is migrated manually to the destination (i.e., a file that is copied/moved/transmitted during migration)

2) Backing storage is located on a shared network storage (i.e., a file that is not copied during migration)

IIRC you're concerned about 2).

Yes.

Quoted 9 lines
Quoted 8 lines

- There is still possibility of stale page cache page at the destination host which we cannot invalidate currently as done in 1] for write-back mode because virtio-pmem memory backend file is mmaped in guest address space and invalidating corresponding page cache pages would also fault all the other userspace process mappings on the same file. Or we make it strict no other process would mmap this backing file?

Quoted 8 lines

I'd have assume that a simple fsync on the src once migration is about to switch over (e.g., pre_save/post_save handler) should be enough to trigger writeback to the backing storage, at which point the dst can take over. So handling the src is easy.

So is the issue that the dst might still have stale pagecache information, because it already accessed some of that file previously, correct?

yes.

Quoted 6 lines
Quoted 5 lines

-- In commit 1] we first fsync and then invalidate all the pages from destination
page cache. fsync would sync the stale dirty page cache page, Is this the right thing to do as we might end up in data discrepancy?

Quoted 6 lines

It would be weird if

a) The src used/modified the file and fsync'ed the modifications back to backing storage
b) The dst has stale dirty pagecache pages that would result in a modification of backing storage during fsync()

Yes. That's what I thought currently we are doing with commit 1] maybe Stefan can confirm. If yes, itvirg is broken as well.

Quoted 5 lines

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

In case of write back cache mode, we could still have stale dirty pages at the destination
host and destination fsync is not the right thing to do. We need to invalidate these pages
(Can we invalidate dirty pages resident in page cache with POSIX_FADV_DONTNEED as
well?) man pages say, we cannot (unless i misunderstood it).

Quoted 6 lines

IIRC, an existing mmap of the file on the dst should not really be problematic *as long as* we didn't actually access file content that way and faulted in the pages. So *maybe*, if we do the POSIX_FADV_DONTNEED on the dst before accessing file content via the mmap, there shouldn't be an issue. Unless the mmap itself is already problematic.

mmap with shared=ON, might result in stale dirty page cache pages?

Quoted 5 lines

I think we can assume that once QEMU starts on the dst and wants to mmap the file that it's not mapped into any other process yet. vhost-user will only mmap *after* being told from QEMU about the mmap region and the location in GPA.

maybe we have an old stale dirty page cache page even if there no mmap process alive before mmaping virtio-pmem backend file in destination?

Quoted 8 lines

So if the existing QEMU mmap is not problematic, it should be easy, just do the POSIX_FADV_DONTNEED on the destination when initializing virtio-pmem. If we have to POSIX_FADV_DONTNEED *before* performing the mmap, we might need a way to tell QEMU to POSIX_FADV_DONTNEED before doing the mmap. The could be a parameter for memory-backend-file like "flush=on", or doing that implicitly when we're told that we expect an incoming migration.

Yes, that's what I had in mind. Just wanted to confirm some of my doubts for correct
implementation. As I see it, page cache coherency across multiple host systems with
live migration needs to be addressed or used to avoid such scenarios.

Thanks,
Pankaj

Quoted 5 lines

--
Thanks,

David / dhildenb

#3 lore raw
Quoted 6 lines
Quoted 5 lines

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

Quoted 8 lines

In case of write back cache mode, we could still have stale dirty pages at the destination
host and destination fsync is not the right thing to do. We need to invalidate these pages
(Can we invalidate dirty pages resident in page cache with POSIX_FADV_DONTNEED as
well?) man pages say, we cannot (unless i misunderstood it).

I think you'd have to fsync + POSIX_FADV_DONTNEED. But I am still confused how we could end up with dirty pagecache pages on the destination. In my opinion, there should only be clean pagecache pages -- can someone enlighten me? :)

Quoted 8 lines
Quoted 6 lines

IIRC, an existing mmap of the file on the dst should not really be problematic *as long as* we didn't actually access file content that way and faulted in the pages. So *maybe*, if we do the POSIX_FADV_DONTNEED on the dst before accessing file content via the mmap, there shouldn't be an issue. Unless the mmap itself is already problematic.

mmap with shared=ON, might result in stale dirty page cache pages?

But only if actually accessing memory, especially writing to it, no?

Quoted 9 lines
Quoted 6 lines

I think we can assume that once QEMU starts on the dst and wants to mmap the file that it's not mapped into any other process yet. vhost-user will only mmap *after* being told from QEMU about the mmap region and the location in GPA.

maybe we have an old stale dirty page cache page even if there no mmap process alive before mmaping virtio-pmem backend file in destination?

But how could that happen in a sane environment? As I said, any writeback on that file from the destination would actually corrupt the file that has been used+modified on the source in the meantime, no?

Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw
Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

Quoted 8 lines

In case of write back cache mode, we could still have stale dirty pages at the destination
host and destination fsync is not the right thing to do. We need to invalidate these pages
(Can we invalidate dirty pages resident in page cache with POSIX_FADV_DONTNEED as
well?) man pages say, we cannot (unless i misunderstood it).

Quoted 4 lines

I think you'd have to fsync + POSIX_FADV_DONTNEED. But I am still confused how we could end up with dirty pagecache pages on the destination. In my opinion, there should only be clean pagecache pages -- can someone enlighten me? :)

because of activity on the page cache pages corresponding to mmap region in the past which is not synced yet or not reclaimed yet. Maybe this is hypothetical
or not possible, happy to learn?

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines

IIRC, an existing mmap of the file on the dst should not really be problematic *as long as* we didn't actually access file content that way and faulted in the pages. So *maybe*, if we do the POSIX_FADV_DONTNEED on the dst before accessing file content via the mmap, there shouldn't be an issue. Unless the mmap itself is already problematic.

mmap with shared=ON, might result in stale dirty page cache pages?

But only if actually accessing memory, especially writing to it, no?

yes.

Quoted 21 lines
Quoted 10 lines
Quoted 7 lines

I think we can assume that once QEMU starts on the dst and wants to mmap the file that it's not mapped into any other process yet. vhost-user will only mmap *after* being told from QEMU about the mmap region and the location in GPA.

maybe we have an old stale dirty page cache page even if there no mmap process alive before mmaping virtio-pmem backend file in destination?

But how could that happen in a sane environment? As I said, any writeback on that file from the destination would actually corrupt the file that has been used+modified on the source in the meantime, no?

--
Thanks,

David / dhildenb

#5 lore raw
Quoted 19 lines · Pankaj Gupta
Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

Quoted 8 lines

In case of write back cache mode, we could still have stale dirty pages at the destination
host and destination fsync is not the right thing to do. We need to invalidate these pages
(Can we invalidate dirty pages resident in page cache with POSIX_FADV_DONTNEED as
well?) man pages say, we cannot (unless i misunderstood it).

Quoted 4 lines

I think you'd have to fsync + POSIX_FADV_DONTNEED. But I am still confused how we could end up with dirty pagecache pages on the destination. In my opinion, there should only be clean pagecache pages -- can someone enlighten me? :)

Quoted 4 lines

because of activity on the page cache pages corresponding to mmap region in the past which is not synced yet or not reclaimed yet. Maybe this is hypothetical
or not possible, happy to learn?

Right, but assume the following *sane*

#1 H0 starts and runs VM.
#2 H0 migrates VM to H1.
#3 H1 runs VM.
#4 H1 migrates VM to H0.
#5 H0 runs VM.

We'd expect a proper fsync during #2, writing back any dirty pages to the memory backend. Otherwise, #3 would already be broken. Similarly, we'd expect a proper fsync during #4.

I assume during #4 we could find clean pagecache pages that are actually invalid, because the underlying file was changed by H1. So we have to make sure to invalidate all pagecache pages (all clean).

Signature · 3 lines
Thanks,

David / dhildenb
#6 lore raw
Quoted 32 lines
Quoted 19 lines
Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

Quoted 8 lines

In case of write back cache mode, we could still have stale dirty pages at the destination
host and destination fsync is not the right thing to do. We need to invalidate these pages
(Can we invalidate dirty pages resident in page cache with POSIX_FADV_DONTNEED as
well?) man pages say, we cannot (unless i misunderstood it).

Quoted 4 lines

I think you'd have to fsync + POSIX_FADV_DONTNEED. But I am still confused how we could end up with dirty pagecache pages on the destination. In my opinion, there should only be clean pagecache pages -- can someone enlighten me? :)

Quoted 4 lines

because of activity on the page cache pages corresponding to mmap region in the past which is not synced yet or not reclaimed yet. Maybe this is hypothetical
or not possible, happy to learn?

Right, but assume the following *sane*

#1 H0 starts and runs VM.
#2 H0 migrates VM to H1.
#3 H1 runs VM.
#4 H1 migrates VM to H0.
#5 H0 runs VM.

Quoted 7 lines

We'd expect a proper fsync during #2, writing back any dirty pages to the memory backend. Otherwise, #3 would already be broken. Similarly, we'd expect a proper fsync during #4.

I assume during #4 we could find clean pagecache pages that are actually invalid, because the underlying file was changed by H1. So we have to make sure to invalidate all pagecache pages (all clean).

Yes, you mean fsync on source host before migration starts. My point is something
like another process mmap same backend file on destination host and/or guest/qemu
crashing abruptly.

#7 lore raw
Quoted 40 lines
Quoted 32 lines
Quoted 19 lines
Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

I mean, that would be fundamentally broken, because the fsync() would corrupt the file. So I assume in a sane environment, the dst could only have stale clean pagecache pages. And we'd have to get rid of these to re-read everything from file.

Quoted 8 lines

In case of write back cache mode, we could still have stale dirty pages at the destination
host and destination fsync is not the right thing to do. We need to invalidate these pages
(Can we invalidate dirty pages resident in page cache with POSIX_FADV_DONTNEED as
well?) man pages say, we cannot (unless i misunderstood it).

Quoted 4 lines

I think you'd have to fsync + POSIX_FADV_DONTNEED. But I am still confused how we could end up with dirty pagecache pages on the destination. In my opinion, there should only be clean pagecache pages -- can someone enlighten me? :)

Quoted 4 lines

because of activity on the page cache pages corresponding to mmap region in the past which is not synced yet or not reclaimed yet. Maybe this is hypothetical
or not possible, happy to learn?

Right, but assume the following *sane*

#1 H0 starts and runs VM.
#2 H0 migrates VM to H1.
#3 H1 runs VM.
#4 H1 migrates VM to H0.
#5 H0 runs VM.

Quoted 7 lines

We'd expect a proper fsync during #2, writing back any dirty pages to the memory backend. Otherwise, #3 would already be broken. Similarly, we'd expect a proper fsync during #4.

I assume during #4 we could find clean pagecache pages that are actually invalid, because the underlying file was changed by H1. So we have to make sure to invalidate all pagecache pages (all clean).

Quoted 5 lines

Yes, you mean fsync on source host before migration starts. My point is something
like another process mmap same backend file on destination host and/or guest/qemu
crashing abruptly.

In that case we should not start guest if we cannot invalidate all the corresponding
page cache pages before starting guest i.e mmaping virtio-pmem backend file.

Thank you for the discussion!

Best regards,
Pankaj