Skip to contentSkip to conversation

block/file-posix: allow -drive cache.direct=off live migration

PATCH Applied Stefan Hajnoczi 4 participants 7 messages Apr 27, 2018 – May 11, 2018 Block layer

lore
Share & tools
lore
#0 lore raw

v2 cover block/file-posix: allow -drive cache.direct=off live migration

v2:
* Add comment on !__linux__ situation [Fam]
* Add file-posix.c x-check-cache-dropped=on|off option [DaveG, Kevin]

file-posix.c only supports shared storage live migration with -drive cache.direct=off due to cache consistency issues. There are two main shared storage configurations: files on NFS and host block devices on SAN LUNs.

The problem is that QEMU starts on the destination host before the source host has written everything out to the disk. The page cache on the destination host may contain stale data read when QEMU opened the image file (before migration handover). Using O_DIRECT avoids this problem but prevents users from taking advantage of the host page cache.

Although cache=none is the recommended setting for virtualization use cases, there are scenarios where cache=writeback makes sense. If the guest has much less RAM than the host or many guests share the same backing file, then the host page cache can significantly improve disk I/O performance.

This patch series implements .bdrv_co_invalidate_cache() for block/file-posix.c on Linux so that shared storage live migration works. I have sent it as an RFC because cache consistency is not binary, there are corner cases which I've described in the actual patch, and this may require more discussion.

Regarding NFS, QEMU relies on O_DIRECT rather than the close-to-open consistency model (see nfs(5)), which is the basic guarantee provided by NFS. After this patch cache consistency is no longer provided by O_DIRECT.

This patch series relies on fdatasync(2) (source) + posix_fadvise(POSIX_FADV_DONTNEED) (destination) instead. I believe it is safe for both NFS and SAN LUNs. Maybe we should use fsync(2) instead of fdatasync(2) so that NFS has up-to-date inode metadata?

Stefan Hajnoczi (2):
block/file-posix: implement bdrv_co_invalidate_cache() on Linux block/file-posix: add x-check-page-cache=on|off option

 qapi/block-core.json |   7 ++-
 block/file-posix.c   | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 150 insertions(+), 3 deletions(-)
Signature · 1 line
2.14.3
#1 lore raw

PATCH v2 1/2 block/file-posix: implement bdrv_co_invalidate_cache() on Linux

On Linux posix_fadvise(POSIX_FADV_DONTNEED) invalidates pages*. Use this to drop page cache on the destination host during shared storage migration. This way the destination host will read the latest copy of the data and will not use stale data from the page cache.

The flow is as follows:

1. Source host writes out all dirty pages and inactivates drives.
2. QEMU_VM_EOF is sent on migration stream.
3. Destination host invalidates caches before accessing drives.

This patch enables live migration even with -drive cache.direct=off.

* Terms and conditions may apply, please see patch for details.

---
 block/file-posix.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
block/file-posix.c +46 -0
index 3794c0007a..3707ea2d1c 100644
@@ -2236,6 +2236,49 @@ static int coroutine_fn raw_co_block_status(BlockDriverState *bs,
     return ret | BDRV_BLOCK_OFFSET_VALID;
 }
 
+static void coroutine_fn raw_co_invalidate_cache(BlockDriverState *bs,
+                                                 Error **errp)
+{
+    BDRVRawState *s = bs->opaque;
+    int ret;
+
+    ret = fd_open(bs);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "The file descriptor is not open");
+        return;
+    }
+
+    if (s->open_flags & O_DIRECT) {
+        return; /* No host kernel page cache */
+    }
+
+#if defined(__linux__)
+    /* This sets the scene for the next syscall... */
+    ret = bdrv_co_flush(bs);
+    if (ret < 0) {
+        error_setg_errno(errp, -ret, "flush failed");
+        return;
+    }
+
+    /* Linux does not invalidate pages that are dirty, locked, or mmapped by a
+     * process.  These limitations are okay because we just fsynced the file,
+     * we don't use mmap, and the file should not be in use by other processes.
+     */
+    ret = posix_fadvise(s->fd, 0, 0, POSIX_FADV_DONTNEED);
+    if (ret != 0) { /* the return value is a positive errno */
+        error_setg_errno(errp, ret, "fadvise failed");
+        return;
+    }
+#else /* __linux__ */
+    /* Do nothing.  Live migration to a remote host with cache.direct=off is
+     * unsupported on other host operating systems.  Cache consistency issues
+     * may occur but no error is reported here, partly because that's the
+     * historical behavior and partly because it's hard to differentiate valid
+     * configurations that should not cause errors.
+     */
+#endif /* !__linux__ */
+}
+
 static coroutine_fn BlockAIOCB *raw_aio_pdiscard(BlockDriverState *bs,
     int64_t offset, int bytes,
     BlockCompletionFunc *cb, void *opaque)
@@ -2328,6 +2371,7 @@ BlockDriver bdrv_file = {
     .bdrv_co_create_opts = raw_co_create_opts,
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
     .bdrv_co_block_status = raw_co_block_status,
+    .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
     .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes,
 
     .bdrv_co_preadv         = raw_co_preadv,
@@ -2805,6 +2849,7 @@ static BlockDriver bdrv_host_device = {
     .bdrv_reopen_abort   = raw_reopen_abort,
     .bdrv_co_create_opts = hdev_co_create_opts,
     .create_opts         = &raw_create_opts,
+    .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
     .bdrv_co_pwrite_zeroes = hdev_co_pwrite_zeroes,
 
     .bdrv_co_preadv         = raw_co_preadv,
@@ -2927,6 +2972,7 @@ static BlockDriver bdrv_host_cdrom = {
     .bdrv_reopen_abort   = raw_reopen_abort,
     .bdrv_co_create_opts = hdev_co_create_opts,
     .create_opts         = &raw_create_opts,
+    .bdrv_co_invalidate_cache = raw_co_invalidate_cache,
 
 
     .bdrv_co_preadv         = raw_co_preadv,
Signature · 1 line
2.14.3
#2 lore raw

PATCH v2 2/2 block/file-posix: add x-check-page-cache=on|off option

In reply to Stefan Hajnoczi

mincore(2) checks whether pages are resident. Use it to verify that page cache has been dropped.

You can trigger a verification failure by mmapping the image file from another process that loads a byte from a page, forcing it to become resident. bdrv_co_invalidate_cache() will fail while that process is alive.

---
 qapi/block-core.json |   7 +++-
 block/file-posix.c   | 100 +++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 104 insertions(+), 3 deletions(-)
qapi/block-core.json +6 -1
index c50517bff3..21c3470234 100644
@@ -2530,6 +2530,10 @@
 # @locking:     whether to enable file locking. If set to 'auto', only enable
 #               when Open File Descriptor (OFD) locking API is available
 #               (default: auto, since 2.10)
+# @x-check-cache-dropped: whether to check that page cache was dropped on live
+#                         migration.  May cause noticeable delays if the image
+#                         file is large, do not use in production.
+#                         (default: off) (since: 2.13)
 #
 # Since: 2.9
 ##
@@ -2537,7 +2541,8 @@
   'data': { 'filename': 'str',
             '*pr-manager': 'str',
             '*locking': 'OnOffAuto',
-            '*aio': 'BlockdevAioOptions' } }
+            '*aio': 'BlockdevAioOptions',
+            '*x-check-cache-dropped': 'bool' } }
 
 ##
 # @BlockdevOptionsNull:
block/file-posix.c +98 -2

166 lines not on this page · raw message

Signature · 1 line
2.14.3
#4 lore raw
In reply to Stefan Hajnoczi
Quoted 33 lines · Stefan Hajnoczi

v2:
* Add comment on !__linux__ situation [Fam]
* Add file-posix.c x-check-cache-dropped=on|off option [DaveG, Kevin]

file-posix.c only supports shared storage live migration with -drive cache.direct=off due to cache consistency issues. There are two main shared storage configurations: files on NFS and host block devices on SAN LUNs.

The problem is that QEMU starts on the destination host before the source host has written everything out to the disk. The page cache on the destination host may contain stale data read when QEMU opened the image file (before migration handover). Using O_DIRECT avoids this problem but prevents users from taking advantage of the host page cache.

Although cache=none is the recommended setting for virtualization use cases, there are scenarios where cache=writeback makes sense. If the guest has much less RAM than the host or many guests share the same backing file, then the host page cache can significantly improve disk I/O performance.

This patch series implements .bdrv_co_invalidate_cache() for block/file-posix.c on Linux so that shared storage live migration works. I have sent it as an RFC because cache consistency is not binary, there are corner cases which I've described in the actual patch, and this may require more discussion.

Regarding NFS, QEMU relies on O_DIRECT rather than the close-to-open consistency model (see nfs(5)), which is the basic guarantee provided by NFS. After this patch cache consistency is no longer provided by O_DIRECT.

This patch series relies on fdatasync(2) (source) + posix_fadvise(POSIX_FADV_DONTNEED) (destination) instead. I believe it is safe for both NFS and SAN LUNs. Maybe we should use fsync(2) instead of fdatasync(2) so that NFS has up-to-date inode metadata?

Quoted 7 lines

Stefan Hajnoczi (2):
block/file-posix: implement bdrv_co_invalidate_cache() on Linux block/file-posix: add x-check-page-cache=on|off option

 qapi/block-core.json |   7 ++-
 block/file-posix.c   | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 150 insertions(+), 3 deletions(-)

Kevin: Are you happy with this series?

Stefan

#5 lore raw

Am 08.05.2018 um 12:32 hat Stefan Hajnoczi geschrieben:

Quoted 38 lines · Stefan Hajnoczi
Quoted 37 lines

v2:
* Add comment on !__linux__ situation [Fam]
* Add file-posix.c x-check-cache-dropped=on|off option [DaveG, Kevin]

file-posix.c only supports shared storage live migration with -drive cache.direct=off due to cache consistency issues. There are two main shared storage configurations: files on NFS and host block devices on SAN LUNs.

The problem is that QEMU starts on the destination host before the source host has written everything out to the disk. The page cache on the destination host may contain stale data read when QEMU opened the image file (before migration handover). Using O_DIRECT avoids this problem but prevents users from taking advantage of the host page cache.

Although cache=none is the recommended setting for virtualization use cases, there are scenarios where cache=writeback makes sense. If the guest has much less RAM than the host or many guests share the same backing file, then the host page cache can significantly improve disk I/O performance.

This patch series implements .bdrv_co_invalidate_cache() for block/file-posix.c on Linux so that shared storage live migration works. I have sent it as an RFC because cache consistency is not binary, there are corner cases which I've described in the actual patch, and this may require more discussion.

Regarding NFS, QEMU relies on O_DIRECT rather than the close-to-open consistency model (see nfs(5)), which is the basic guarantee provided by NFS. After this patch cache consistency is no longer provided by O_DIRECT.

This patch series relies on fdatasync(2) (source) + posix_fadvise(POSIX_FADV_DONTNEED) (destination) instead. I believe it is safe for both NFS and SAN LUNs. Maybe we should use fsync(2) instead of fdatasync(2) so that NFS has up-to-date inode metadata?

Stefan Hajnoczi (2):
block/file-posix: implement bdrv_co_invalidate_cache() on Linux block/file-posix: add x-check-page-cache=on|off option

Quoted 5 lines
Quoted 3 lines
 qapi/block-core.json |   7 ++-
 block/file-posix.c   | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 150 insertions(+), 3 deletions(-)

Kevin: Are you happy with this series?

Yes, I think I am.

I'm still kind of concerned about misleading people into believing that cache=writeback + live migration is generally safe when it's only in special cases, but that's not really a concern about the code, but about how we communicate the feature.

Kevin

author Applied
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 33 lines · Stefan Hajnoczi

v2:
* Add comment on !__linux__ situation [Fam]
* Add file-posix.c x-check-cache-dropped=on|off option [DaveG, Kevin]

file-posix.c only supports shared storage live migration with -drive cache.direct=off due to cache consistency issues. There are two main shared storage configurations: files on NFS and host block devices on SAN LUNs.

The problem is that QEMU starts on the destination host before the source host has written everything out to the disk. The page cache on the destination host may contain stale data read when QEMU opened the image file (before migration handover). Using O_DIRECT avoids this problem but prevents users from taking advantage of the host page cache.

Although cache=none is the recommended setting for virtualization use cases, there are scenarios where cache=writeback makes sense. If the guest has much less RAM than the host or many guests share the same backing file, then the host page cache can significantly improve disk I/O performance.

This patch series implements .bdrv_co_invalidate_cache() for block/file-posix.c on Linux so that shared storage live migration works. I have sent it as an RFC because cache consistency is not binary, there are corner cases which I've described in the actual patch, and this may require more discussion.

Regarding NFS, QEMU relies on O_DIRECT rather than the close-to-open consistency model (see nfs(5)), which is the basic guarantee provided by NFS. After this patch cache consistency is no longer provided by O_DIRECT.

This patch series relies on fdatasync(2) (source) + posix_fadvise(POSIX_FADV_DONTNEED) (destination) instead. I believe it is safe for both NFS and SAN LUNs. Maybe we should use fsync(2) instead of fdatasync(2) so that NFS has up-to-date inode metadata?

Quoted 11 lines

Stefan Hajnoczi (2):
block/file-posix: implement bdrv_co_invalidate_cache() on Linux block/file-posix: add x-check-page-cache=on|off option

 qapi/block-core.json |   7 ++-
 block/file-posix.c   | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 150 insertions(+), 3 deletions(-)
Signature · 1 line
2.14.3

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan