Skip to contentSkip to conversation

migration: Support hugetlb doublemaps

RFC PATCH In review Peter Xu 4 participants 69 messages Jan 17, 2023 – Feb 6, 2023

Subsystems (2)
lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover migration: Support hugetlb doublemaps

Based-on: <[email protected]>
[PATCH 0/5] migration: Fix disorder of channel creations

Trees for reference:
https://github.com/xzpeter/linux/releases/tag/doublemap-v0.1
https://github.com/xzpeter/qemu/releases/tag/doublemap-v0.1

This is an RFC series that only for early discussion purpose but not for merging.

This patchset allows postcopy to work with huge pages better by migrating huge pages in small page sizes. It relies on a kernel feature called "hugetlb HGM" which is currently proposed on the Linux kernel mailing list by James Houghton, latest version v1:

https://lore.kernel.org/r/[email protected]

[PS: The kernel v1 patchset may need a few fixups to make QEMU work, which
are all contained in the tree link provided tagged doublemap-v0.1 above]

The kernel series is still during review upstream, so the API is still not stable.

I kept the old name of "doublemap" in this QEMU patchset to represent HGM. With that, huge pages can be mapped with even smaller sizes than the huge page itself. It can drastically reduce page fault latencies during postcopy if the guest has hugepage backed memories and make postcopy start working with huge pages. The average page request latency can drop from ~1sec to ~250us for 1G backed in the initial test results.

UFFDIO_COPY doesn't support mapping huge pages in small sizes, so one major part of this series introduced UFFDIO_CONTINUE to resolve page faults for hugetlb mappings.

Sampled page latency histogram for 18G guest with/without doublemap (preempt=on, single thread busy spin workload over 18G map):

Before:

@delay_us:

[64, 128)              3 |@                                                   |
[128, 256)            84 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[256, 512)            10 |@@@@@@                                              |
[512, 1K)              1 |                                                    |
[1K, 2K)               0 |                                                    |
[2K, 4K)               0 |                                                    |
[4K, 8K)               0 |                                                    |
[8K, 16K)              0 |                                                    |
[16K, 32K)             0 |                                                    |
[32K, 64K)             0 |                                                    |
[64K, 128K)            0 |                                                    |
[128K, 256K)           0 |                                                    |
[256K, 512K)           0 |                                                    |
[512K, 1M)             0 |                                                    |
[1M, 2M)              17 |@@@@@@@@@@                                          |
[2M, 4M)              21 |@@@@@@@@@@@@@                                       |
[4M, 8M)               8 |@@@@                                                |
[8M, 16M)              4 |@@                                                  |

After:

@delay_us:

[16, 32)               6 |                                                    |
[32, 64)               6 |                                                    |
[64, 128)           3117 |@@                                                  |
[128, 256)         70815 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@|
[256, 512)         30460 |@@@@@@@@@@@@@@@@@@@@@@                              |
[512, 1K)           1135 |                                                    |
[1K, 2K)              34 |                                                    |
[2K, 4K)              42 |                                                    |
[4K, 8K)             126 |                                                    |
[8K, 16K)             91 |                                                    |
[16K, 32K)             0 |                                                    |
[32K, 64K)             1 |                                                    |

Any early comment welcomed. Thanks.

Peter Xu (21):
update linux headers
util: Include osdep.h first in util/mmap-alloc.c
physmem: Add qemu_ram_is_hugetlb()
madvise: Include linux/mman.h under linux-headers/
madvise: Add QEMU_MADV_SPLIT
madvise: Add QEMU_MADV_COLLAPSE
ramblock: Cache file offset for file-backed ramblocks
ramblock: Cache the length to do file mmap() on ramblocks
ramblock: Add RAM_READONLY
ramblock: Add ramblock_file_map()
migration: Add hugetlb-doublemap cap
migration: Introduce page size for-migration-only
migration: Add migration_ram_pagesize_largest()
migration: Map hugetlbfs ramblocks twice, and pre-allocate
migration: Teach qemu about minor faults and doublemap
migration: Enable doublemap with MADV_SPLIT
migration: Rework ram discard logic for hugetlb double-map
migration: Allow postcopy_register_shared_ufd() to fail
migration: Add postcopy_mark_received()
migration: Handle page faults using UFFDIO_CONTINUE
migration: Collapse huge pages again after postcopy finished

 backends/hostmem-file.c                       |   3 +-
 hw/virtio/vhost-user.c                        |   9 +-
 include/exec/cpu-common.h                     |   3 +-
 include/exec/memory.h                         |   4 +-
 include/exec/ram_addr.h                       |   6 +-
 include/exec/ramblock.h                       |  14 +
 include/qemu/madvise.h                        |  18 ++
 include/standard-headers/drm/drm_fourcc.h     |  63 +++-
 include/standard-headers/linux/ethtool.h      |  81 ++++-
 include/standard-headers/linux/fuse.h         |  20 +-
 .../linux/input-event-codes.h                 |   4 +
 include/standard-headers/linux/pci_regs.h     |   2 +
 include/standard-headers/linux/virtio_blk.h   |  19 ++
 include/standard-headers/linux/virtio_bt.h    |   8 +
 include/standard-headers/linux/virtio_net.h   |   4 +
 linux-headers/asm-arm64/kvm.h                 |   1 +
 linux-headers/asm-generic/hugetlb_encode.h    |  26 +-
 linux-headers/asm-generic/mman-common.h       |   4 +
 linux-headers/asm-mips/mman.h                 |   4 +
 linux-headers/asm-riscv/kvm.h                 |   7 +
 linux-headers/asm-x86/kvm.h                   |  11 +-
 linux-headers/linux/kvm.h                     |  32 +-
 linux-headers/linux/psci.h                    |  14 +
 linux-headers/linux/userfaultfd.h             |   4 +
 linux-headers/linux/vfio.h                    | 278 +++++++++++++++++-
 migration/migration.c                         |  56 +++-
 migration/migration.h                         |   1 +
 migration/postcopy-ram.c                      | 228 +++++++++++---
 migration/postcopy-ram.h                      |   5 +-
 migration/ram.c                               | 165 ++++++++++-
 migration/ram.h                               |   2 +
 migration/trace-events                        |   6 +-
 qapi/migration.json                           |   7 +-
 softmmu/memory.c                              |   8 +-
 softmmu/physmem.c                             |  92 ++++--
 util/mmap-alloc.c                             |   2 +-
 36 files changed, 1051 insertions(+), 160 deletions(-)
Signature · 1 line
2.37.3
author
#1 lore raw

RFC PATCH 1/21 update linux headers

---
 include/standard-headers/drm/drm_fourcc.h     |  63 +++-
 include/standard-headers/linux/ethtool.h      |  81 ++++-
 include/standard-headers/linux/fuse.h         |  20 +-
 .../linux/input-event-codes.h                 |   4 +
 include/standard-headers/linux/pci_regs.h     |   2 +
 include/standard-headers/linux/virtio_blk.h   |  19 ++
 include/standard-headers/linux/virtio_bt.h    |   8 +
 include/standard-headers/linux/virtio_net.h   |   4 +
 linux-headers/asm-arm64/kvm.h                 |   1 +
 linux-headers/asm-generic/hugetlb_encode.h    |  26 +-
 linux-headers/asm-generic/mman-common.h       |   4 +
 linux-headers/asm-mips/mman.h                 |   4 +
 linux-headers/asm-riscv/kvm.h                 |   7 +
 linux-headers/asm-x86/kvm.h                   |  11 +-
 linux-headers/linux/kvm.h                     |  32 +-
 linux-headers/linux/psci.h                    |  14 +
 linux-headers/linux/userfaultfd.h             |   4 +
 linux-headers/linux/vfio.h                    | 278 +++++++++++++++++-
 18 files changed, 526 insertions(+), 56 deletions(-)
include/standard-headers/drm/drm_fourcc.h +59 -4
index 48b620cbef..69cab17b38 100644
@@ -98,18 +98,42 @@ extern "C" {
 #define DRM_FORMAT_INVALID	0
 
 /* color index */
+#define DRM_FORMAT_C1		fourcc_code('C', '1', ' ', ' ') /* [7:0] C0:C1:C2:C3:C4:C5:C6:C7 1:1:1:1:1:1:1:1 eight pixels/byte */
+#define DRM_FORMAT_C2		fourcc_code('C', '2', ' ', ' ') /* [7:0] C0:C1:C2:C3 2:2:2:2 four pixels/byte */
+#define DRM_FORMAT_C4		fourcc_code('C', '4', ' ', ' ') /* [7:0] C0:C1 4:4 two pixels/byte */
 #define DRM_FORMAT_C8		fourcc_code('C', '8', ' ', ' ') /* [7:0] C */
 
-/* 8 bpp Red */
+/* 1 bpp Darkness (inverse relationship between channel value and brightness) */
+#define DRM_FORMAT_D1		fourcc_code('D', '1', ' ', ' ') /* [7:0] D0:D1:D2:D3:D4:D5:D6:D7 1:1:1:1:1:1:1:1 eight pixels/byte */
+
+/* 2 bpp Darkness (inverse relationship between channel value and brightness) */
+#define DRM_FORMAT_D2		fourcc_code('D', '2', ' ', ' ') /* [7:0] D0:D1:D2:D3 2:2:2:2 four pixels/byte */
+
+/* 4 bpp Darkness (inverse relationship between channel value and brightness) */
+#define DRM_FORMAT_D4		fourcc_code('D', '4', ' ', ' ') /* [7:0] D0:D1 4:4 two pixels/byte */
+
+/* 8 bpp Darkness (inverse relationship between channel value and brightness) */
+#define DRM_FORMAT_D8		fourcc_code('D', '8', ' ', ' ') /* [7:0] D */
+
+/* 1 bpp Red (direct relationship between channel value and brightness) */
+#define DRM_FORMAT_R1		fourcc_code('R', '1', ' ', ' ') /* [7:0] R0:R1:R2:R3:R4:R5:R6:R7 1:1:1:1:1:1:1:1 eight pixels/byte */
+
+/* 2 bpp Red (direct relationship between channel value and brightness) */
+#define DRM_FORMAT_R2		fourcc_code('R', '2', ' ', ' ') /* [7:0] R0:R1:R2:R3 2:2:2:2 four pixels/byte */
+
+/* 4 bpp Red (direct relationship between channel value and brightness) */
+#define DRM_FORMAT_R4		fourcc_code('R', '4', ' ', ' ') /* [7:0] R0:R1 4:4 two pixels/byte */
+
+/* 8 bpp Red (direct relationship between channel value and brightness) */
 #define DRM_FORMAT_R8		fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
 
-/* 10 bpp Red */
+/* 10 bpp Red (direct relationship between channel value and brightness) */
 #define DRM_FORMAT_R10		fourcc_code('R', '1', '0', ' ') /* [15:0] x:R 6:10 little endian */
 
-/* 12 bpp Red */
+/* 12 bpp Red (direct relationship between channel value and brightness) */
 #define DRM_FORMAT_R12		fourcc_code('R', '1', '2', ' ') /* [15:0] x:R 4:12 little endian */
 
-/* 16 bpp Red */
+/* 16 bpp Red (direct relationship between channel value and brightness) */
 #define DRM_FORMAT_R16		fourcc_code('R', '1', '6', ' ') /* [15:0] R little endian */
 
 /* 16 bpp RG */
@@ -204,7 +228,9 @@ extern "C" {
 #define DRM_FORMAT_VYUY		fourcc_code('V', 'Y', 'U', 'Y') /* [31:0] Y1:Cb0:Y0:Cr0 8:8:8:8 little endian */
 
 #define DRM_FORMAT_AYUV		fourcc_code('A', 'Y', 'U', 'V') /* [31:0] A:Y:Cb:Cr 8:8:8:8 little endian */
+#define DRM_FORMAT_AVUY8888	fourcc_code('A', 'V', 'U', 'Y') /* [31:0] A:Cr:Cb:Y 8:8:8:8 little endian */
 #define DRM_FORMAT_XYUV8888	fourcc_code('X', 'Y', 'U', 'V') /* [31:0] X:Y:Cb:Cr 8:8:8:8 little endian */
+#define DRM_FORMAT_XVUY8888	fourcc_code('X', 'V', 'U', 'Y') /* [31:0] X:Cr:Cb:Y 8:8:8:8 little endian */
 #define DRM_FORMAT_VUY888	fourcc_code('V', 'U', '2', '4') /* [23:0] Cr:Cb:Y 8:8:8 little endian */
 #define DRM_FORMAT_VUY101010	fourcc_code('V', 'U', '3', '0') /* Y followed by U then V, 10:10:10. Non-linear modifier only */
 
@@ -717,6 +743,35 @@ extern "C" {
  */
 #define DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED fourcc_mod_code(VIVANTE, 4)
 
+/*
+ * Vivante TS (tile-status) buffer modifiers. They can be combined with all of
+ * the color buffer tiling modifiers defined above. When TS is present it's a
+ * separate buffer containing the clear/compression status of each tile. The
+ * modifiers are defined as VIVANTE_MOD_TS_c_s, where c is the color buffer
+ * tile size in bytes covered by one entry in the status buffer and s is the
+ * number of status bits per entry.
+ * We reserve the top 8 bits of the Vivante modifier space for tile status
+ * clear/compression modifiers, as future cores might add some more TS layout
+ * variations.
+ */
+#define VIVANTE_MOD_TS_64_4               (1ULL << 48)
+#define VIVANTE_MOD_TS_64_2               (2ULL << 48)
+#define VIVANTE_MOD_TS_128_4              (3ULL << 48)
+#define VIVANTE_MOD_TS_256_4              (4ULL << 48)
+#define VIVANTE_MOD_TS_MASK               (0xfULL << 48)
+
+/*
+ * Vivante compression modifiers. Those depend on a TS modifier being present
+ * as the TS bits get reinterpreted as compression tags instead of simple
+ * clear markers when compression is enabled.
+ */
+#define VIVANTE_MOD_COMP_DEC400           (1ULL << 52)
+#define VIVANTE_MOD_COMP_MASK             (0xfULL << 52)
+
+/* Masking out the extension bits will yield the base modifier. */
+#define VIVANTE_MOD_EXT_MASK              (VIVANTE_MOD_TS_MASK | \
+                                           VIVANTE_MOD_COMP_MASK)
+
 /* NVIDIA frame buffer modifiers */
 
 /*
include/standard-headers/linux/ethtool.h +73 -8

130 lines not on this page · raw message

include/standard-headers/linux/fuse.h +18 -2
index bda06258be..a1af78d989 100644
@@ -194,6 +194,13 @@
  *  - add FUSE_SECURITY_CTX init flag
  *  - add security context to create, mkdir, symlink, and mknod requests
  *  - add FUSE_HAS_INODE_DAX, FUSE_ATTR_DAX
+ *
+ *  7.37
+ *  - add FUSE_TMPFILE
+ *
+ *  7.38
+ *  - add FUSE_EXPIRE_ONLY flag to fuse_notify_inval_entry
+ *  - add FOPEN_PARALLEL_DIRECT_WRITES
  */
 
 #ifndef _LINUX_FUSE_H
@@ -225,7 +232,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 36
+#define FUSE_KERNEL_MINOR_VERSION 38
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
@@ -297,6 +304,7 @@ struct fuse_file_lock {
  * FOPEN_CACHE_DIR: allow caching this directory
  * FOPEN_STREAM: the file is stream-like (no file position at all)
  * FOPEN_NOFLUSH: don't flush data cache on close (unless FUSE_WRITEBACK_CACHE)
+ * FOPEN_PARALLEL_DIRECT_WRITES: Allow concurrent direct writes on the same inode
  */
 #define FOPEN_DIRECT_IO		(1 << 0)
 #define FOPEN_KEEP_CACHE	(1 << 1)
@@ -304,6 +312,7 @@ struct fuse_file_lock {
 #define FOPEN_CACHE_DIR		(1 << 3)
 #define FOPEN_STREAM		(1 << 4)
 #define FOPEN_NOFLUSH		(1 << 5)
+#define FOPEN_PARALLEL_DIRECT_WRITES	(1 << 6)
 
 /**
  * INIT request/reply flags
@@ -484,6 +493,12 @@ struct fuse_file_lock {
  */
 #define FUSE_SETXATTR_ACL_KILL_SGID	(1 << 0)
 
+/**
+ * notify_inval_entry flags
+ * FUSE_EXPIRE_ONLY
+ */
+#define FUSE_EXPIRE_ONLY		(1 << 0)
+
 enum fuse_opcode {
 	FUSE_LOOKUP		= 1,
 	FUSE_FORGET		= 2,  /* no reply */
@@ -533,6 +548,7 @@ enum fuse_opcode {
 	FUSE_SETUPMAPPING	= 48,
 	FUSE_REMOVEMAPPING	= 49,
 	FUSE_SYNCFS		= 50,
+	FUSE_TMPFILE		= 51,
 
 	/* CUSE specific operations */
 	CUSE_INIT		= 4096,
@@ -911,7 +927,7 @@ struct fuse_notify_inval_inode_out {
 struct fuse_notify_inval_entry_out {
 	uint64_t	parent;
 	uint32_t	namelen;
-	uint32_t	padding;
+	uint32_t	flags;
 };
 
 struct fuse_notify_delete_out {
include/standard-headers/linux/input-event-codes.h +4 -0
index 50790aee5a..f6bab08540 100644
@@ -614,6 +614,9 @@
 #define KEY_KBD_LAYOUT_NEXT	0x248	/* AC Next Keyboard Layout Select */
 #define KEY_EMOJI_PICKER	0x249	/* Show/hide emoji picker (HUTRR101) */
 #define KEY_DICTATE		0x24a	/* Start or Stop Voice Dictation Session (HUTRR99) */
+#define KEY_CAMERA_ACCESS_ENABLE	0x24b	/* Enables programmatic access to camera devices. (HUTRR72) */
+#define KEY_CAMERA_ACCESS_DISABLE	0x24c	/* Disables programmatic access to camera devices. (HUTRR72) */
+#define KEY_CAMERA_ACCESS_TOGGLE	0x24d	/* Toggles the current state of the camera access control. (HUTRR72) */
 
 #define KEY_BRIGHTNESS_MIN		0x250	/* Set Brightness to Minimum */
 #define KEY_BRIGHTNESS_MAX		0x251	/* Set Brightness to Maximum */
@@ -862,6 +865,7 @@
 #define ABS_TOOL_WIDTH		0x1c
 
 #define ABS_VOLUME		0x20
+#define ABS_PROFILE		0x21
 
 #define ABS_MISC		0x28
 
include/standard-headers/linux/pci_regs.h +2 -0
index 57b8e2ffb1..85ab127881 100644
@@ -1058,6 +1058,7 @@
 /* Precision Time Measurement */
 #define PCI_PTM_CAP			0x04	    /* PTM Capability */
 #define  PCI_PTM_CAP_REQ		0x00000001  /* Requester capable */
+#define  PCI_PTM_CAP_RES		0x00000002  /* Responder capable */
 #define  PCI_PTM_CAP_ROOT		0x00000004  /* Root capable */
 #define  PCI_PTM_GRANULARITY_MASK	0x0000FF00  /* Clock granularity */
 #define PCI_PTM_CTRL			0x08	    /* PTM Control */
@@ -1119,6 +1120,7 @@
 #define  PCI_DOE_STATUS_DATA_OBJECT_READY	0x80000000  /* Data Object Ready */
 #define PCI_DOE_WRITE		0x10    /* DOE Write Data Mailbox Register */
 #define PCI_DOE_READ		0x14    /* DOE Read Data Mailbox Register */
+#define PCI_DOE_CAP_SIZEOF	0x18	/* Size of DOE register block */
 
 /* DOE Data Object - note not actually registers */
 #define PCI_DOE_DATA_OBJECT_HEADER_1_VID		0x0000ffff
include/standard-headers/linux/virtio_blk.h +19 -0
index 2dcc90826a..e81715cd70 100644
@@ -40,6 +40,7 @@
 #define VIRTIO_BLK_F_MQ		12	/* support more than one vq */
 #define VIRTIO_BLK_F_DISCARD	13	/* DISCARD is supported */
 #define VIRTIO_BLK_F_WRITE_ZEROES	14	/* WRITE ZEROES is supported */
+#define VIRTIO_BLK_F_SECURE_ERASE	16 /* Secure Erase is supported */
 
 /* Legacy feature bits */
 #ifndef VIRTIO_BLK_NO_LEGACY
@@ -119,6 +120,21 @@ struct virtio_blk_config {
 	uint8_t write_zeroes_may_unmap;
 
 	uint8_t unused1[3];
+
+	/* the next 3 entries are guarded by VIRTIO_BLK_F_SECURE_ERASE */
+	/*
+	 * The maximum secure erase sectors (in 512-byte sectors) for
+	 * one segment.
+	 */
+	__virtio32 max_secure_erase_sectors;
+	/*
+	 * The maximum number of secure erase segments in a
+	 * secure erase command.
+	 */
+	__virtio32 max_secure_erase_seg;
+	/* Secure erase commands must be aligned to this number of sectors. */
+	__virtio32 secure_erase_sector_alignment;
+
 } QEMU_PACKED;
 
 /*
@@ -153,6 +169,9 @@ struct virtio_blk_config {
 /* Write zeroes command */
 #define VIRTIO_BLK_T_WRITE_ZEROES	13
 
+/* Secure erase command */
+#define VIRTIO_BLK_T_SECURE_ERASE	14
+
 #ifndef VIRTIO_BLK_NO_LEGACY
 /* Barrier before this op. */
 #define VIRTIO_BLK_T_BARRIER	0x80000000
include/standard-headers/linux/virtio_bt.h +8 -0
index 245e1eff4b..a11ecc3f92 100644
@@ -9,6 +9,7 @@
 #define VIRTIO_BT_F_VND_HCI	0	/* Indicates vendor command support */
 #define VIRTIO_BT_F_MSFT_EXT	1	/* Indicates MSFT vendor support */
 #define VIRTIO_BT_F_AOSP_EXT	2	/* Indicates AOSP vendor support */
+#define VIRTIO_BT_F_CONFIG_V2	3	/* Use second version configuration */
 
 enum virtio_bt_config_type {
 	VIRTIO_BT_CONFIG_TYPE_PRIMARY	= 0,
@@ -28,4 +29,11 @@ struct virtio_bt_config {
 	uint16_t msft_opcode;
 } QEMU_PACKED;
 
+struct virtio_bt_config_v2 {
+	uint8_t  type;
+	uint8_t  alignment;
+	uint16_t vendor;
+	uint16_t msft_opcode;
+};
+
 #endif /* _LINUX_VIRTIO_BT_H */
include/standard-headers/linux/virtio_net.h +4 -0
index 42c68caf71..c0e797067a 100644
@@ -57,6 +57,9 @@
 					 * Steering */
 #define VIRTIO_NET_F_CTRL_MAC_ADDR 23	/* Set MAC address */
 #define VIRTIO_NET_F_NOTF_COAL	53	/* Device supports notifications coalescing */
+#define VIRTIO_NET_F_GUEST_USO4	54	/* Guest can handle USOv4 in. */
+#define VIRTIO_NET_F_GUEST_USO6	55	/* Guest can handle USOv6 in. */
+#define VIRTIO_NET_F_HOST_USO	56	/* Host can handle USO in. */
 #define VIRTIO_NET_F_HASH_REPORT  57	/* Supports hash report */
 #define VIRTIO_NET_F_RSS	  60	/* Supports RSS RX steering */
 #define VIRTIO_NET_F_RSC_EXT	  61	/* extended coalescing info */
@@ -130,6 +133,7 @@ struct virtio_net_hdr_v1 {
 #define VIRTIO_NET_HDR_GSO_TCPV4	1	/* GSO frame, IPv4 TCP (TSO) */
 #define VIRTIO_NET_HDR_GSO_UDP		3	/* GSO frame, IPv4 UDP (UFO) */
 #define VIRTIO_NET_HDR_GSO_TCPV6	4	/* GSO frame, IPv6 TCP */
+#define VIRTIO_NET_HDR_GSO_UDP_L4	5	/* GSO frame, IPv4& IPv6 UDP (USO) */
 #define VIRTIO_NET_HDR_GSO_ECN		0x80	/* TCP has ECN set */
 	uint8_t gso_type;
 	__virtio16 hdr_len;	/* Ethernet + IP + tcp/udp hdrs */
linux-headers/asm-arm64/kvm.h +1 -0
index 4bf2d7246e..a7cfefb3a8 100644
@@ -43,6 +43,7 @@
 #define __KVM_HAVE_VCPU_EVENTS
 
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
+#define KVM_DIRTY_LOG_PAGE_OFFSET 64
 
 #define KVM_REG_SIZE(id)						\
 	(1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT))
linux-headers/asm-generic/hugetlb_encode.h +13 -13
index 4f3d5aaa11..de687009bf 100644
@@ -20,18 +20,18 @@
 #define HUGETLB_FLAG_ENCODE_SHIFT	26
 #define HUGETLB_FLAG_ENCODE_MASK	0x3f
 
-#define HUGETLB_FLAG_ENCODE_16KB	(14 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_64KB	(16 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_512KB	(19 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_1MB		(20 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_2MB		(21 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_8MB		(23 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_16MB	(24 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_32MB	(25 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_256MB	(28 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_512MB	(29 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_1GB		(30 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_2GB		(31 << HUGETLB_FLAG_ENCODE_SHIFT)
-#define HUGETLB_FLAG_ENCODE_16GB	(34 << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_16KB	(14U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_64KB	(16U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_512KB	(19U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_1MB		(20U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_2MB		(21U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_8MB		(23U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_16MB	(24U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_32MB	(25U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_256MB	(28U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_512MB	(29U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_1GB		(30U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_2GB		(31U << HUGETLB_FLAG_ENCODE_SHIFT)
+#define HUGETLB_FLAG_ENCODE_16GB	(34U << HUGETLB_FLAG_ENCODE_SHIFT)
 
 #endif /* _ASM_GENERIC_HUGETLB_ENCODE_H_ */
linux-headers/asm-generic/mman-common.h +4 -0
index 6c1aa92a92..996e8ded09 100644
@@ -77,6 +77,10 @@
 
 #define MADV_DONTNEED_LOCKED	24	/* like DONTNEED, but drop locked pages too */
 
+#define MADV_COLLAPSE	25		/* Synchronous hugepage collapse */
+
+#define MADV_SPLIT	26		/* Enable hugepage high-granularity APIs */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
linux-headers/asm-mips/mman.h +4 -0
index 1be428663c..f8a74a3a09 100644
@@ -103,6 +103,10 @@
 
 #define MADV_DONTNEED_LOCKED	24	/* like DONTNEED, but drop locked pages too */
 
+#define MADV_COLLAPSE	25		/* Synchronous hugepage collapse */
+
+#define MADV_SPLIT	26		/* Enable hugepage high-granularity APIs */
+
 /* compatibility flags */
 #define MAP_FILE	0
 
linux-headers/asm-riscv/kvm.h +7 -0
index 7351417afd..92af6f3f05 100644
@@ -48,6 +48,10 @@ struct kvm_sregs {
 /* CONFIG registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
 struct kvm_riscv_config {
 	unsigned long isa;
+	unsigned long zicbom_block_size;
+	unsigned long mvendorid;
+	unsigned long marchid;
+	unsigned long mimpid;
 };
 
 /* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */
@@ -98,6 +102,9 @@ enum KVM_RISCV_ISA_EXT_ID {
 	KVM_RISCV_ISA_EXT_M,
 	KVM_RISCV_ISA_EXT_SVPBMT,
 	KVM_RISCV_ISA_EXT_SSTC,
+	KVM_RISCV_ISA_EXT_SVINVAL,
+	KVM_RISCV_ISA_EXT_ZIHINTPAUSE,
+	KVM_RISCV_ISA_EXT_ZICBOM,
 	KVM_RISCV_ISA_EXT_MAX,
 };
 
linux-headers/asm-x86/kvm.h +3 -8
index 46de10a809..2747d2ce14 100644
@@ -53,14 +53,6 @@
 /* Architectural interrupt line count. */
 #define KVM_NR_INTERRUPTS 256
 
-struct kvm_memory_alias {
-	__u32 slot;  /* this has a different namespace than memory slots */
-	__u32 flags;
-	__u64 guest_phys_addr;
-	__u64 memory_size;
-	__u64 target_phys_addr;
-};
-
 /* for KVM_GET_IRQCHIP and KVM_SET_IRQCHIP */
 struct kvm_pic_state {
 	__u8 last_irr;	/* edge detection */
@@ -214,6 +206,8 @@ struct kvm_msr_list {
 struct kvm_msr_filter_range {
 #define KVM_MSR_FILTER_READ  (1 << 0)
 #define KVM_MSR_FILTER_WRITE (1 << 1)
+#define KVM_MSR_FILTER_RANGE_VALID_MASK (KVM_MSR_FILTER_READ | \
+					 KVM_MSR_FILTER_WRITE)
 	__u32 flags;
 	__u32 nmsrs; /* number of msrs in bitmap */
 	__u32 base;  /* MSR index the bitmap starts at */
@@ -224,6 +218,7 @@ struct kvm_msr_filter_range {
 struct kvm_msr_filter {
 #define KVM_MSR_FILTER_DEFAULT_ALLOW (0 << 0)
 #define KVM_MSR_FILTER_DEFAULT_DENY  (1 << 0)
+#define KVM_MSR_FILTER_VALID_MASK (KVM_MSR_FILTER_DEFAULT_DENY)
 	__u32 flags;
 	struct kvm_msr_filter_range ranges[KVM_MSR_FILTER_MAX_RANGES];
 };
linux-headers/linux/kvm.h +15 -17
index ebdafa576d..30b2795d10 100644
@@ -86,14 +86,6 @@ struct kvm_debug_guest {
 /* *** End of deprecated interfaces *** */
 
 
-/* for KVM_CREATE_MEMORY_REGION */
-struct kvm_memory_region {
-	__u32 slot;
-	__u32 flags;
-	__u64 guest_phys_addr;
-	__u64 memory_size; /* bytes */
-};
-
 /* for KVM_SET_USER_MEMORY_REGION */
 struct kvm_userspace_memory_region {
 	__u32 slot;
@@ -104,9 +96,9 @@ struct kvm_userspace_memory_region {
 };
 
 /*
- * The bit 0 ~ bit 15 of kvm_memory_region::flags are visible for userspace,
- * other bits are reserved for kvm internal use which are defined in
- * include/linux/kvm_host.h.
+ * The bit 0 ~ bit 15 of kvm_userspace_memory_region::flags are visible for
+ * userspace, other bits are reserved for kvm internal use which are defined
+ * in include/linux/kvm_host.h.
  */
 #define KVM_MEM_LOG_DIRTY_PAGES	(1UL << 0)
 #define KVM_MEM_READONLY	(1UL << 1)
@@ -483,6 +475,9 @@ struct kvm_run {
 #define KVM_MSR_EXIT_REASON_INVAL	(1 << 0)
 #define KVM_MSR_EXIT_REASON_UNKNOWN	(1 << 1)
 #define KVM_MSR_EXIT_REASON_FILTER	(1 << 2)
+#define KVM_MSR_EXIT_REASON_VALID_MASK	(KVM_MSR_EXIT_REASON_INVAL   |	\
+					 KVM_MSR_EXIT_REASON_UNKNOWN |	\
+					 KVM_MSR_EXIT_REASON_FILTER)
 			__u32 reason; /* kernel -> user */
 			__u32 index; /* kernel -> user */
 			__u64 data; /* kernel <-> user */
@@ -1175,6 +1170,9 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_VM_DISABLE_NX_HUGE_PAGES 220
 #define KVM_CAP_S390_ZPCI_OP 221
 #define KVM_CAP_S390_CPU_TOPOLOGY 222
+#define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223
+#define KVM_CAP_S390_PROTECTED_ASYNC_DISABLE 224
+#define KVM_CAP_DIRTY_LOG_RING_WITH_BITMAP 225
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1264,6 +1262,7 @@ struct kvm_x86_mce {
 #define KVM_XEN_HVM_CONFIG_RUNSTATE		(1 << 3)
 #define KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL	(1 << 4)
 #define KVM_XEN_HVM_CONFIG_EVTCHN_SEND		(1 << 5)
+#define KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG	(1 << 6)
 
 struct kvm_xen_hvm_config {
 	__u32 flags;
@@ -1434,18 +1433,12 @@ struct kvm_vfio_spapr_tce {
 	__s32	tablefd;
 };
 
-/*
- * ioctls for VM fds
- */
-#define KVM_SET_MEMORY_REGION     _IOW(KVMIO,  0x40, struct kvm_memory_region)
 /*
  * KVM_CREATE_VCPU receives as a parameter the vcpu slot, and returns
  * a vcpu fd.
  */
 #define KVM_CREATE_VCPU           _IO(KVMIO,   0x41)
 #define KVM_GET_DIRTY_LOG         _IOW(KVMIO,  0x42, struct kvm_dirty_log)
-/* KVM_SET_MEMORY_ALIAS is obsolete: */
-#define KVM_SET_MEMORY_ALIAS      _IOW(KVMIO,  0x43, struct kvm_memory_alias)
 #define KVM_SET_NR_MMU_PAGES      _IO(KVMIO,   0x44)
 #define KVM_GET_NR_MMU_PAGES      _IO(KVMIO,   0x45)
 #define KVM_SET_USER_MEMORY_REGION _IOW(KVMIO, 0x46, \
@@ -1737,6 +1730,8 @@ enum pv_cmd_id {
 	KVM_PV_UNSHARE_ALL,
 	KVM_PV_INFO,
 	KVM_PV_DUMP,
+	KVM_PV_ASYNC_CLEANUP_PREPARE,
+	KVM_PV_ASYNC_CLEANUP_PERFORM,
 };
 
 struct kvm_pv_cmd {
@@ -1767,6 +1762,7 @@ struct kvm_xen_hvm_attr {
 	union {
 		__u8 long_mode;
 		__u8 vector;
+		__u8 runstate_update_flag;
 		struct {
 			__u64 gfn;
 		} shared_info;
@@ -1807,6 +1803,8 @@ struct kvm_xen_hvm_attr {
 /* Available with KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_EVTCHN_SEND */
 #define KVM_XEN_ATTR_TYPE_EVTCHN		0x3
 #define KVM_XEN_ATTR_TYPE_XEN_VERSION		0x4
+/* Available with KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG */
+#define KVM_XEN_ATTR_TYPE_RUNSTATE_UPDATE_FLAG	0x5
 
 /* Per-vCPU Xen attributes */
 #define KVM_XEN_VCPU_GET_ATTR	_IOWR(KVMIO, 0xca, struct kvm_xen_vcpu_attr)
linux-headers/linux/psci.h +14 -0
index 213b2a0f70..e60dfd8907 100644
@@ -48,12 +48,26 @@
 #define PSCI_0_2_FN64_MIGRATE_INFO_UP_CPU	PSCI_0_2_FN64(7)
 
 #define PSCI_1_0_FN_PSCI_FEATURES		PSCI_0_2_FN(10)
+#define PSCI_1_0_FN_CPU_FREEZE			PSCI_0_2_FN(11)
+#define PSCI_1_0_FN_CPU_DEFAULT_SUSPEND		PSCI_0_2_FN(12)
+#define PSCI_1_0_FN_NODE_HW_STATE		PSCI_0_2_FN(13)
 #define PSCI_1_0_FN_SYSTEM_SUSPEND		PSCI_0_2_FN(14)
 #define PSCI_1_0_FN_SET_SUSPEND_MODE		PSCI_0_2_FN(15)
+#define PSCI_1_0_FN_STAT_RESIDENCY		PSCI_0_2_FN(16)
+#define PSCI_1_0_FN_STAT_COUNT			PSCI_0_2_FN(17)
+
 #define PSCI_1_1_FN_SYSTEM_RESET2		PSCI_0_2_FN(18)
+#define PSCI_1_1_FN_MEM_PROTECT			PSCI_0_2_FN(19)
+#define PSCI_1_1_FN_MEM_PROTECT_CHECK_RANGE	PSCI_0_2_FN(19)
 
+#define PSCI_1_0_FN64_CPU_DEFAULT_SUSPEND	PSCI_0_2_FN64(12)
+#define PSCI_1_0_FN64_NODE_HW_STATE		PSCI_0_2_FN64(13)
 #define PSCI_1_0_FN64_SYSTEM_SUSPEND		PSCI_0_2_FN64(14)
+#define PSCI_1_0_FN64_STAT_RESIDENCY		PSCI_0_2_FN64(16)
+#define PSCI_1_0_FN64_STAT_COUNT		PSCI_0_2_FN64(17)
+
 #define PSCI_1_1_FN64_SYSTEM_RESET2		PSCI_0_2_FN64(18)
+#define PSCI_1_1_FN64_MEM_PROTECT_CHECK_RANGE	PSCI_0_2_FN64(19)
 
 /* PSCI v0.2 power state encoding for CPU_SUSPEND function */
 #define PSCI_0_2_POWER_STATE_ID_MASK		0xffff
linux-headers/linux/userfaultfd.h +4 -0
index a3a377cd44..ba5d0df52f 100644
@@ -12,6 +12,10 @@
 
 #include <linux/types.h>
 
+/* ioctls for /dev/userfaultfd */
+#define USERFAULTFD_IOC 0xAA
+#define USERFAULTFD_IOC_NEW _IO(USERFAULTFD_IOC, 0x00)
+
 /*
  * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and
  * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR.  In
linux-headers/linux/vfio.h +274 -4

347 lines not on this page · raw message

Signature · 1 line
2.37.3
author
#2 lore raw

RFC PATCH 2/21 util: Include osdep.h first in util/mmap-alloc.c

In reply to Peter Xu

Without it, we never have CONFIG_LINUX defined even if on linux, so linux/mman.h is never really included.

---
 util/mmap-alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/mmap-alloc.c +1 -1
index 5ed7d29183..040599b0e3 100644
@@ -9,6 +9,7 @@
  * This work is licensed under the terms of the GNU GPL, version 2 or
  * later.  See the COPYING file in the top-level directory.
  */
+#include "qemu/osdep.h"
 
 #ifdef CONFIG_LINUX
 #include <linux/mman.h>
@@ -17,7 +18,6 @@
 #define MAP_SHARED_VALIDATE   0x0
 #endif /* CONFIG_LINUX */
 
-#include "qemu/osdep.h"
 #include "qemu/mmap-alloc.h"
 #include "qemu/host-utils.h"
 #include "qemu/cutils.h"
Signature · 1 line
2.37.3
Reviewed-by on v1 2/21
#3 lore raw

Re: [PATCH RFC 02/21] util: Include osdep.h first in util/mmap-alloc.c

Quoted 4 lines · * Peter Xu ([email protected]) wrote:

Without it, we never have CONFIG_LINUX defined even if on linux, so linux/mman.h is never really included.

Quoted 27 lines
---
 util/mmap-alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/mmap-alloc.c +1 -1
index 5ed7d29183..040599b0e3 100644
@@ -9,6 +9,7 @@
  * This work is licensed under the terms of the GNU GPL, version 2 or
  * later.  See the COPYING file in the top-level directory.
  */
+#include "qemu/osdep.h"
 
 #ifdef CONFIG_LINUX
 #include <linux/mman.h>
@@ -17,7 +18,6 @@
 #define MAP_SHARED_VALIDATE   0x0
 #endif /* CONFIG_LINUX */
 
-#include "qemu/osdep.h"
 #include "qemu/mmap-alloc.h"
 #include "qemu/host-utils.h"
 #include "qemu/cutils.h"
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#6 lore raw

RFC PATCH 3/21 physmem: Add qemu_ram_is_hugetlb()

In reply to Peter Xu

Returns true for a hugetlbfs mapping, false otherwise.

---
 include/exec/cpu-common.h | 1 +
 softmmu/physmem.c         | 5 +++++
 2 files changed, 6 insertions(+)
include/exec/cpu-common.h +1 -0
index 6feaa40ca7..94452aa17f 100644
@@ -95,6 +95,7 @@ void qemu_ram_unset_migratable(RAMBlock *rb);
 int qemu_ram_get_fd(RAMBlock *rb);
 
 size_t qemu_ram_pagesize(RAMBlock *block);
+bool qemu_ram_is_hugetlb(RAMBlock *rb);
 size_t qemu_ram_pagesize_largest(void);
 
 /**
softmmu/physmem.c +5 -0
index edec095c7a..a4fb129d8f 100644
@@ -1798,6 +1798,11 @@ size_t qemu_ram_pagesize(RAMBlock *rb)
     return rb->page_size;
 }
 
+bool qemu_ram_is_hugetlb(RAMBlock *rb)
+{
+    return rb->page_size > qemu_real_host_page_size();
+}
+
 /* Returns the largest size of page in use */
 size_t qemu_ram_pagesize_largest(void)
 {
Signature · 1 line
2.37.3
Reviewed-by on v1 3/21
#7 lore raw

Re: [PATCH RFC 03/21] physmem: Add qemu_ram_is_hugetlb()

Quoted 3 lines · * Peter Xu ([email protected]) wrote:

Returns true for a hugetlbfs mapping, false otherwise.

Yeh OK, it feels a little delecate perhaps if anything else ever allows large mappings.

Quoted 36 lines
---
 include/exec/cpu-common.h | 1 +
 softmmu/physmem.c         | 5 +++++
 2 files changed, 6 insertions(+)
include/exec/cpu-common.h +1 -0
index 6feaa40ca7..94452aa17f 100644
@@ -95,6 +95,7 @@ void qemu_ram_unset_migratable(RAMBlock *rb);
 int qemu_ram_get_fd(RAMBlock *rb);
 
 size_t qemu_ram_pagesize(RAMBlock *block);
+bool qemu_ram_is_hugetlb(RAMBlock *rb);
 size_t qemu_ram_pagesize_largest(void);
 
 /**
softmmu/physmem.c +5 -0
index edec095c7a..a4fb129d8f 100644
@@ -1798,6 +1798,11 @@ size_t qemu_ram_pagesize(RAMBlock *rb)
     return rb->page_size;
 }
 
+bool qemu_ram_is_hugetlb(RAMBlock *rb)
+{
+    return rb->page_size > qemu_real_host_page_size();
+}
+
 /* Returns the largest size of page in use */
 size_t qemu_ram_pagesize_largest(void)
 {
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#9 lore raw

RFC PATCH 4/21 madvise: Include linux/mman.h under linux-headers/

In reply to Peter Xu

This will allow qemu/madvise.h to always include linux/mman.h under the linux-headers/.

---
 include/qemu/madvise.h | 4 ++++
 1 file changed, 4 insertions(+)
include/qemu/madvise.h +4 -0
index e155f59a0d..b6fa49553f 100644
@@ -8,6 +8,10 @@
 #ifndef QEMU_MADVISE_H
 #define QEMU_MADVISE_H
 
+#ifdef CONFIG_LINUX
+#include "linux/mman.h"
+#endif
+
 #define QEMU_MADV_INVALID -1
 
 #if defined(CONFIG_MADVISE)
Signature · 1 line
2.37.3
Reviewed-by on v1 4/21
#10 lore raw

Re: [PATCH RFC 04/21] madvise: Include linux/mman.h under linux-headers/

Quoted 4 lines · * Peter Xu ([email protected]) wrote:

This will allow qemu/madvise.h to always include linux/mman.h under the linux-headers/.

Quoted 22 lines
---
 include/qemu/madvise.h | 4 ++++
 1 file changed, 4 insertions(+)
include/qemu/madvise.h +4 -0
index e155f59a0d..b6fa49553f 100644
@@ -8,6 +8,10 @@
 #ifndef QEMU_MADVISE_H
 #define QEMU_MADVISE_H
 
+#ifdef CONFIG_LINUX
+#include "linux/mman.h"
+#endif
+
 #define QEMU_MADV_INVALID -1
 
 #if defined(CONFIG_MADVISE)
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#12 lore raw

RFC PATCH 5/21 madvise: Add QEMU_MADV_SPLIT

In reply to Peter Xu

MADV_SPLIT is a new madvise() on Linux. Define QEMU_MADV_SPLIT.

---
 include/qemu/madvise.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu/madvise.h +7 -0
index b6fa49553f..3dddd25065 100644
@@ -63,6 +63,11 @@
 #else
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
 #endif
+#ifdef MADV_SPLIT
+#define QEMU_MADV_SPLIT MADV_SPLIT
+#else
+#define QEMU_MADV_SPLIT QEMU_MADV_INVALID
+#endif
 
 #elif defined(CONFIG_POSIX_MADVISE)
 
@@ -77,6 +82,7 @@
 #define QEMU_MADV_NOHUGEPAGE  QEMU_MADV_INVALID
 #define QEMU_MADV_REMOVE QEMU_MADV_DONTNEED
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
+#define QEMU_MADV_SPLIT QEMU_MADV_INVALID
 
 #else /* no-op */
 
@@ -91,6 +97,7 @@
 #define QEMU_MADV_NOHUGEPAGE  QEMU_MADV_INVALID
 #define QEMU_MADV_REMOVE QEMU_MADV_INVALID
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
+#define QEMU_MADV_SPLIT QEMU_MADV_INVALID
 
 #endif
 
Signature · 1 line
2.37.3
author
#14 lore raw

RFC PATCH 6/21 madvise: Add QEMU_MADV_COLLAPSE

In reply to Peter Xu

MADV_COLLAPSE is a new madvise() on Linux. Define it.

---
 include/qemu/madvise.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu/madvise.h +7 -0
index 3dddd25065..794e5fb0a7 100644
@@ -68,6 +68,11 @@
 #else
 #define QEMU_MADV_SPLIT QEMU_MADV_INVALID
 #endif
+#ifdef MADV_COLLAPSE
+#define QEMU_MADV_COLLAPSE MADV_COLLAPSE
+#else
+#define QEMU_MADV_COLLAPSE QEMU_MADV_INVALID
+#endif
 
 #elif defined(CONFIG_POSIX_MADVISE)
 
@@ -83,6 +88,7 @@
 #define QEMU_MADV_REMOVE QEMU_MADV_DONTNEED
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
 #define QEMU_MADV_SPLIT QEMU_MADV_INVALID
+#define QEMU_MADV_COLLAPSE QEMU_MADV_INVALID
 
 #else /* no-op */
 
@@ -98,6 +104,7 @@
 #define QEMU_MADV_REMOVE QEMU_MADV_INVALID
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
 #define QEMU_MADV_SPLIT QEMU_MADV_INVALID
+#define QEMU_MADV_COLLAPSE QEMU_MADV_INVALID
 
 #endif
 
Signature · 1 line
2.37.3
#15 lore raw

Re: [PATCH RFC 06/21] madvise: Add QEMU_MADV_COLLAPSE

Quoted 1 line · * Peter Xu ([email protected]) wrote:

MADV_COLLAPSE is a new madvise() on Linux. Define it.

I'd probably have merged this with the MADV_SPLIT one since they go together; but also, it would be good in the commit message for Qemu to include either the definition or a pointer to the kernel definiton of them.

Dave

Quoted 40 lines
---
 include/qemu/madvise.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/qemu/madvise.h +7 -0
index 3dddd25065..794e5fb0a7 100644
@@ -68,6 +68,11 @@
 #else
 #define QEMU_MADV_SPLIT QEMU_MADV_INVALID
 #endif
+#ifdef MADV_COLLAPSE
+#define QEMU_MADV_COLLAPSE MADV_COLLAPSE
+#else
+#define QEMU_MADV_COLLAPSE QEMU_MADV_INVALID
+#endif
 
 #elif defined(CONFIG_POSIX_MADVISE)
 
@@ -83,6 +88,7 @@
 #define QEMU_MADV_REMOVE QEMU_MADV_DONTNEED
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
 #define QEMU_MADV_SPLIT QEMU_MADV_INVALID
+#define QEMU_MADV_COLLAPSE QEMU_MADV_INVALID
 
 #else /* no-op */
 
@@ -98,6 +104,7 @@
 #define QEMU_MADV_REMOVE QEMU_MADV_INVALID
 #define QEMU_MADV_POPULATE_WRITE QEMU_MADV_INVALID
 #define QEMU_MADV_SPLIT QEMU_MADV_INVALID
+#define QEMU_MADV_COLLAPSE QEMU_MADV_INVALID
 
 #endif
 
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#16 lore raw

Re: [PATCH RFC 06/21] madvise: Add QEMU_MADV_COLLAPSE

Quoted 7 lines · Dr. David Alan Gilbert
Quoted 1 line · * Peter Xu ([email protected]) wrote:

MADV_COLLAPSE is a new madvise() on Linux. Define it.

I'd probably have merged this with the MADV_SPLIT one since they go together; but also, it would be good in the commit message for Qemu to include either the definition or a pointer to the kernel definiton of them.

Will do.

I don't have good links for them yet because both of them are still not in upstream man-page project. Even THP version of MADV_COLLAPSE man page just got added into the man-page repository in Nov 2022 so most of the websites that host man pages won't even have MADV_COLLAPSE..

For now I'll add some more paragraphs trying to explain everything, and I'll also link to madvise(2) where both of them will be discussed in the future.

Thanks,

Signature · 1 line
Peter Xu
author
#18 lore raw

RFC PATCH 7/21 ramblock: Cache file offset for file-backed ramblocks

In reply to Peter Xu

This value was only used for mmap() when we want to map at a specific offset of the file for memory. To be prepared that we might do another map upon the same range for whatever reason, cache the offset so we know how to map again on the same range.

---
 include/exec/ramblock.h | 5 +++++
 softmmu/physmem.c       | 2 ++
 2 files changed, 7 insertions(+)
include/exec/ramblock.h +5 -0
index adc03df59c..76cd0812c8 100644
@@ -41,6 +41,11 @@ struct RAMBlock {
     QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
     int fd;
     size_t page_size;
+    /*
+     * Cache for file offset to map the ramblock.  Only used for
+     * file-backed ramblocks.
+     */
+    off_t file_offset;
     /* dirty bitmap used during migration */
     unsigned long *bmap;
     /* bitmap of already received pages in postcopy */
softmmu/physmem.c +2 -0
index a4fb129d8f..aa1a7466e5 100644
@@ -1543,6 +1543,8 @@ static void *file_ram_alloc(RAMBlock *block,
     uint32_t qemu_map_flags;
     void *area;
 
+    /* Remember the offset just in case we'll need to map the range again */
+    block->file_offset = offset;
     block->page_size = qemu_fd_getpagesize(fd);
     if (block->mr->align % block->page_size) {
         error_setg(errp, "alignment 0x%" PRIx64
Signature · 1 line
2.37.3
Reviewed-by on v1 7/21
#19 lore raw

Re: [PATCH RFC 07/21] ramblock: Cache file offset for file-backed ramblocks

Quoted 6 lines · Peter Xu

This value was only used for mmap() when we want to map at a specific offset of the file for memory. To be prepared that we might do another map upon the same range for whatever reason, cache the offset so we know how to map again on the same range.

A bit weird that we don't use it (yet) anywhere, but that is life.

author
#20 lore raw

RFC PATCH 8/21 ramblock: Cache the length to do file mmap() on ramblocks

In reply to Peter Xu

We do proper page size alignment for file backed mmap()s for ramblocks. Even if it's as simple as that, cache the value because it'll be used in multiple places.

Since at it, drop size for file_ram_alloc() and just use max_length because that's always true for file-backed ramblocks.

---
 include/exec/ramblock.h |  2 ++
 softmmu/physmem.c       | 14 +++++++-------
 2 files changed, 9 insertions(+), 7 deletions(-)
include/exec/ramblock.h +2 -0
index 76cd0812c8..3f31ce1591 100644
@@ -32,6 +32,8 @@ struct RAMBlock {
     ram_addr_t offset;
     ram_addr_t used_length;
     ram_addr_t max_length;
+    /* Only used for file-backed ramblocks */
+    ram_addr_t mmap_length;
     void (*resized)(const char*, uint64_t length, void *host);
     uint32_t flags;
     /* Protected by iothread lock.  */
softmmu/physmem.c +7 -7
index aa1a7466e5..b5be02f1cb 100644
@@ -1533,7 +1533,6 @@ static int file_ram_open(const char *path,
 }
 
 static void *file_ram_alloc(RAMBlock *block,
-                            ram_addr_t memory,
                             int fd,
                             bool readonly,
                             bool truncate,
@@ -1563,14 +1562,14 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 #endif
 
-    if (memory < block->page_size) {
+    if (block->max_length < block->page_size) {
         error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
                    "or larger than page size 0x%zx",
-                   memory, block->page_size);
+                   block->max_length, block->page_size);
         return NULL;
     }
 
-    memory = ROUND_UP(memory, block->page_size);
+    block->mmap_length = ROUND_UP(block->max_length, block->page_size);
 
     /*
      * ftruncate is not supported by hugetlbfs in older
@@ -1586,7 +1585,7 @@ static void *file_ram_alloc(RAMBlock *block,
      * those labels. Therefore, extending the non-empty backend file
      * is disabled as well.
      */
-    if (truncate && ftruncate(fd, memory)) {
+    if (truncate && ftruncate(fd, block->mmap_length)) {
         perror("ftruncate");
     }
 
@@ -1594,7 +1593,8 @@ static void *file_ram_alloc(RAMBlock *block,
     qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
     qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
     qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
-    area = qemu_ram_mmap(fd, memory, block->mr->align, qemu_map_flags, offset);
+    area = qemu_ram_mmap(fd, block->mmap_length, block->mr->align,
+                         qemu_map_flags, offset);
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
@@ -2100,7 +2100,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, size, fd, readonly,
+    new_block->host = file_ram_alloc(new_block, fd, readonly,
                                      !file_size, offset, errp);
     if (!new_block->host) {
         g_free(new_block);
Signature · 1 line
2.37.3
Reviewed-by on v1 8/21
#21 lore raw

Re: [PATCH RFC 08/21] ramblock: Cache the length to do file mmap() on ramblocks

Quoted 6 lines · * Peter Xu ([email protected]) wrote:

We do proper page size alignment for file backed mmap()s for ramblocks. Even if it's as simple as that, cache the value because it'll be used in multiple places.

Since at it, drop size for file_ram_alloc() and just use max_length because that's always true for file-backed ramblocks.

Having a length previously called 'memory' was a bit odd!

Quoted 1 line
Quoted 79 lines
---
 include/exec/ramblock.h |  2 ++
 softmmu/physmem.c       | 14 +++++++-------
 2 files changed, 9 insertions(+), 7 deletions(-)
include/exec/ramblock.h +2 -0
index 76cd0812c8..3f31ce1591 100644
@@ -32,6 +32,8 @@ struct RAMBlock {
     ram_addr_t offset;
     ram_addr_t used_length;
     ram_addr_t max_length;
+    /* Only used for file-backed ramblocks */
+    ram_addr_t mmap_length;
     void (*resized)(const char*, uint64_t length, void *host);
     uint32_t flags;
     /* Protected by iothread lock.  */
softmmu/physmem.c +7 -7
index aa1a7466e5..b5be02f1cb 100644
@@ -1533,7 +1533,6 @@ static int file_ram_open(const char *path,
 }
 
 static void *file_ram_alloc(RAMBlock *block,
-                            ram_addr_t memory,
                             int fd,
                             bool readonly,
                             bool truncate,
@@ -1563,14 +1562,14 @@ static void *file_ram_alloc(RAMBlock *block,
     }
 #endif
 
-    if (memory < block->page_size) {
+    if (block->max_length < block->page_size) {
         error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
                    "or larger than page size 0x%zx",
-                   memory, block->page_size);
+                   block->max_length, block->page_size);
         return NULL;
     }
 
-    memory = ROUND_UP(memory, block->page_size);
+    block->mmap_length = ROUND_UP(block->max_length, block->page_size);
 
     /*
      * ftruncate is not supported by hugetlbfs in older
@@ -1586,7 +1585,7 @@ static void *file_ram_alloc(RAMBlock *block,
      * those labels. Therefore, extending the non-empty backend file
      * is disabled as well.
      */
-    if (truncate && ftruncate(fd, memory)) {
+    if (truncate && ftruncate(fd, block->mmap_length)) {
         perror("ftruncate");
     }
 
@@ -1594,7 +1593,8 @@ static void *file_ram_alloc(RAMBlock *block,
     qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
     qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
     qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
-    area = qemu_ram_mmap(fd, memory, block->mr->align, qemu_map_flags, offset);
+    area = qemu_ram_mmap(fd, block->mmap_length, block->mr->align,
+                         qemu_map_flags, offset);
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
@@ -2100,7 +2100,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, size, fd, readonly,
+    new_block->host = file_ram_alloc(new_block, fd, readonly,
                                      !file_size, offset, errp);
     if (!new_block->host) {
         g_free(new_block);
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#22 lore raw

Re: [PATCH RFC 08/21] ramblock: Cache the length to do file mmap() on ramblocks

Quoted 9 lines · Dr. David Alan Gilbert
Quoted 6 lines · * Peter Xu ([email protected]) wrote:

We do proper page size alignment for file backed mmap()s for ramblocks. Even if it's as simple as that, cache the value because it'll be used in multiple places.

Since at it, drop size for file_ram_alloc() and just use max_length because that's always true for file-backed ramblocks.

Having a length previously called 'memory' was a bit odd!

:-D

Quoted 4 lines
Quoted 2 lines

Thanks,

Signature · 1 line
Peter Xu
Reviewed-by on v1 8/21
#23 lore raw

Re: [PATCH RFC 08/21] ramblock: Cache the length to do file mmap() on ramblocks

In reply to Peter Xu
Quoted 8 lines · Peter Xu

We do proper page size alignment for file backed mmap()s for ramblocks. Even if it's as simple as that, cache the value because it'll be used in multiple places.

Since at it, drop size for file_ram_alloc() and just use max_length because that's always true for file-backed ramblocks.

softmmu/physmem.c RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, · line 2106

Quoted 9 lines
softmmu/physmem.c +1 -1
@@ -2100,7 +2100,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, size, fd, readonly,
+    new_block->host = file_ram_alloc(new_block, fd, readonly,
                                      !file_size, offset, errp);
     if (!new_block->host) {
         g_free(new_block);

Passing "size" in three places, not bad at all O:-)

author
#24 lore raw

Re: [PATCH RFC 08/21] ramblock: Cache the length to do file mmap() on ramblocks

Quoted 11 lines · Juan Quintela
Quoted 8 lines · Peter Xu

We do proper page size alignment for file backed mmap()s for ramblocks. Even if it's as simple as that, cache the value because it'll be used in multiple places.

Since at it, drop size for file_ram_alloc() and just use max_length because that's always true for file-backed ramblocks.

Thanks for reviewing the set!

Quoted 12 lines

softmmu/physmem.c RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr, · line 2106

Quoted 10 lines
softmmu/physmem.c +1 -1
@@ -2100,7 +2100,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, size, fd, readonly,
+    new_block->host = file_ram_alloc(new_block, fd, readonly,
                                      !file_size, offset, errp);
     if (!new_block->host) {
         g_free(new_block);

Passing "size" in three places, not bad at all O:-)

Yes it's a bit unfortunate. :(

Signature · 1 line
Peter Xu
author
#25 lore raw

RFC PATCH 9/21 ramblock: Add RAM_READONLY

In reply to Peter Xu

This allows us to have RAM_READONLY to be set in ram_flags to show that this ramblock can only be read not write.

We used to pass in readonly boolean along the way for allocating the ramblock, now let it be together with the rest ramblock flags.

The main purpose of this patch is not for clean up though, it's for caching mapping information of each ramblock so when we want to mmap() it again for whatever reason we can have all the information on hand.

---
 backends/hostmem-file.c |  3 ++-
 include/exec/memory.h   |  4 ++--
 include/exec/ram_addr.h |  5 ++---
 softmmu/memory.c        |  8 +++-----
 softmmu/physmem.c       | 16 +++++++---------
 5 files changed, 16 insertions(+), 20 deletions(-)
backends/hostmem-file.c +2 -1
index 25141283c4..1daf00d2da 100644
@@ -56,9 +56,10 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     ram_flags = backend->share ? RAM_SHARED : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
+    ram_flags |= fb->readonly ? RAM_READONLY : 0;
     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
                                      backend->size, fb->align, ram_flags,
-                                     fb->mem_path, fb->readonly, errp);
+                                     fb->mem_path, errp);
     g_free(name);
 #endif
 }
include/exec/memory.h +2 -2
index c37ffdbcd1..006ba77ede 100644
@@ -188,6 +188,8 @@ typedef struct IOMMUTLBEvent {
 /* RAM is a persistent kind memory */
 #define RAM_PMEM (1 << 5)
 
+/* RAM is read-only */
+#define RAM_READONLY (1 << 6)
 
 /*
  * UFFDIO_WRITEPROTECT is used on this RAMBlock to
@@ -1292,7 +1294,6 @@ void memory_region_init_resizeable_ram(MemoryRegion *mr,
  * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *             RAM_NORESERVE,
  * @path: the path in which to allocate the RAM.
- * @readonly: true to open @path for reading, false for read/write.
  * @errp: pointer to Error*, to store an error if it happens.
  *
  * Note that this function does not do anything to cause the data in the
@@ -1305,7 +1306,6 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
                                       uint64_t align,
                                       uint32_t ram_flags,
                                       const char *path,
-                                      bool readonly,
                                       Error **errp);
 
 /**
include/exec/ram_addr.h +2 -3
index f4fb6a2111..0bf9cfc659 100644
@@ -110,7 +110,6 @@ long qemu_maxrampagesize(void);
  *  @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *              RAM_NORESERVE.
  *  @mem_path or @fd: specify the backing file or device
- *  @readonly: true to open @path for reading, false for read/write.
  *  @errp: pointer to Error*, to store an error if it happens
  *
  * Return:
@@ -119,10 +118,10 @@ long qemu_maxrampagesize(void);
  */
 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    uint32_t ram_flags, const char *mem_path,
-                                   bool readonly, Error **errp);
+                                   Error **errp);
 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                  uint32_t ram_flags, int fd, off_t offset,
-                                 bool readonly, Error **errp);
+                                 Error **errp);
 
 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr, Error **errp);
softmmu/memory.c +3 -5
index e05332d07f..2137028773 100644
@@ -1601,18 +1601,16 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
                                       uint64_t align,
                                       uint32_t ram_flags,
                                       const char *path,
-                                      bool readonly,
                                       Error **errp)
 {
     Error *err = NULL;
     memory_region_init(mr, owner, name, size);
     mr->ram = true;
-    mr->readonly = readonly;
+    mr->readonly = ram_flags & RAM_READONLY;
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
     mr->align = align;
-    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path,
-                                             readonly, &err);
+    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
@@ -1635,7 +1633,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
     mr->ram_block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, offset,
-                                           false, &err);
+                                           &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
softmmu/physmem.c +7 -9
index b5be02f1cb..6096eac286 100644
@@ -1534,7 +1534,6 @@ static int file_ram_open(const char *path,
 
 static void *file_ram_alloc(RAMBlock *block,
                             int fd,
-                            bool readonly,
                             bool truncate,
                             off_t offset,
                             Error **errp)
@@ -1589,7 +1588,7 @@ static void *file_ram_alloc(RAMBlock *block,
         perror("ftruncate");
     }
 
-    qemu_map_flags = readonly ? QEMU_MAP_READONLY : 0;
+    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
     qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
     qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
     qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
@@ -2057,7 +2056,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
 #ifdef CONFIG_POSIX
 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                  uint32_t ram_flags, int fd, off_t offset,
-                                 bool readonly, Error **errp)
+                                 Error **errp)
 {
     RAMBlock *new_block;
     Error *local_err = NULL;
@@ -2065,7 +2064,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
 
     /* Just support these ram flags by now. */
     assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
-                          RAM_PROTECTED)) == 0);
+                          RAM_PROTECTED | RAM_READONLY)) == 0);
 
     if (xen_enabled()) {
         error_setg(errp, "-mem-path not supported with Xen");
@@ -2100,8 +2099,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, fd, readonly,
-                                     !file_size, offset, errp);
+    new_block->host = file_ram_alloc(new_block, fd, !file_size, offset, errp);
     if (!new_block->host) {
         g_free(new_block);
         return NULL;
@@ -2120,11 +2118,11 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
 
 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    uint32_t ram_flags, const char *mem_path,
-                                   bool readonly, Error **errp)
+                                   Error **errp)
 {
     int fd;
-    bool created;
     RAMBlock *block;
+    bool created, readonly = ram_flags & RAM_READONLY;
 
     fd = file_ram_open(mem_path, memory_region_name(mr), readonly, &created,
                        errp);
@@ -2132,7 +2130,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
 
-    block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, readonly, errp);
+    block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, errp);
     if (!block) {
         if (created) {
             unlink(mem_path);
Signature · 1 line
2.37.3
Reviewed-by on v1 9/21
#26 lore raw

Re: [PATCH RFC 09/21] ramblock: Add RAM_READONLY

Quoted 11 lines · * Peter Xu ([email protected]) wrote:

This allows us to have RAM_READONLY to be set in ram_flags to show that this ramblock can only be read not write.

We used to pass in readonly boolean along the way for allocating the ramblock, now let it be together with the rest ramblock flags.

The main purpose of this patch is not for clean up though, it's for caching mapping information of each ramblock so when we want to mmap() it again for whatever reason we can have all the information on hand.

Quoted 187 lines
---
 backends/hostmem-file.c |  3 ++-
 include/exec/memory.h   |  4 ++--
 include/exec/ram_addr.h |  5 ++---
 softmmu/memory.c        |  8 +++-----
 softmmu/physmem.c       | 16 +++++++---------
 5 files changed, 16 insertions(+), 20 deletions(-)
backends/hostmem-file.c +2 -1
index 25141283c4..1daf00d2da 100644
@@ -56,9 +56,10 @@ file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
     ram_flags = backend->share ? RAM_SHARED : 0;
     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
+    ram_flags |= fb->readonly ? RAM_READONLY : 0;
     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
                                      backend->size, fb->align, ram_flags,
-                                     fb->mem_path, fb->readonly, errp);
+                                     fb->mem_path, errp);
     g_free(name);
 #endif
 }
include/exec/memory.h +2 -2
index c37ffdbcd1..006ba77ede 100644
@@ -188,6 +188,8 @@ typedef struct IOMMUTLBEvent {
 /* RAM is a persistent kind memory */
 #define RAM_PMEM (1 << 5)
 
+/* RAM is read-only */
+#define RAM_READONLY (1 << 6)
 
 /*
  * UFFDIO_WRITEPROTECT is used on this RAMBlock to
@@ -1292,7 +1294,6 @@ void memory_region_init_resizeable_ram(MemoryRegion *mr,
  * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *             RAM_NORESERVE,
  * @path: the path in which to allocate the RAM.
- * @readonly: true to open @path for reading, false for read/write.
  * @errp: pointer to Error*, to store an error if it happens.
  *
  * Note that this function does not do anything to cause the data in the
@@ -1305,7 +1306,6 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
                                       uint64_t align,
                                       uint32_t ram_flags,
                                       const char *path,
-                                      bool readonly,
                                       Error **errp);
 
 /**
include/exec/ram_addr.h +2 -3
index f4fb6a2111..0bf9cfc659 100644
@@ -110,7 +110,6 @@ long qemu_maxrampagesize(void);
  *  @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
  *              RAM_NORESERVE.
  *  @mem_path or @fd: specify the backing file or device
- *  @readonly: true to open @path for reading, false for read/write.
  *  @errp: pointer to Error*, to store an error if it happens
  *
  * Return:
@@ -119,10 +118,10 @@ long qemu_maxrampagesize(void);
  */
 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    uint32_t ram_flags, const char *mem_path,
-                                   bool readonly, Error **errp);
+                                   Error **errp);
 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                  uint32_t ram_flags, int fd, off_t offset,
-                                 bool readonly, Error **errp);
+                                 Error **errp);
 
 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                   MemoryRegion *mr, Error **errp);
softmmu/memory.c +3 -5
index e05332d07f..2137028773 100644
@@ -1601,18 +1601,16 @@ void memory_region_init_ram_from_file(MemoryRegion *mr,
                                       uint64_t align,
                                       uint32_t ram_flags,
                                       const char *path,
-                                      bool readonly,
                                       Error **errp)
 {
     Error *err = NULL;
     memory_region_init(mr, owner, name, size);
     mr->ram = true;
-    mr->readonly = readonly;
+    mr->readonly = ram_flags & RAM_READONLY;
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
     mr->align = align;
-    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path,
-                                             readonly, &err);
+    mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
@@ -1635,7 +1633,7 @@ void memory_region_init_ram_from_fd(MemoryRegion *mr,
     mr->terminates = true;
     mr->destructor = memory_region_destructor_ram;
     mr->ram_block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, offset,
-                                           false, &err);
+                                           &err);
     if (err) {
         mr->size = int128_zero();
         object_unparent(OBJECT(mr));
softmmu/physmem.c +7 -9
index b5be02f1cb..6096eac286 100644
@@ -1534,7 +1534,6 @@ static int file_ram_open(const char *path,
 
 static void *file_ram_alloc(RAMBlock *block,
                             int fd,
-                            bool readonly,
                             bool truncate,
                             off_t offset,
                             Error **errp)
@@ -1589,7 +1588,7 @@ static void *file_ram_alloc(RAMBlock *block,
         perror("ftruncate");
     }
 
-    qemu_map_flags = readonly ? QEMU_MAP_READONLY : 0;
+    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
     qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
     qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
     qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
@@ -2057,7 +2056,7 @@ static void ram_block_add(RAMBlock *new_block, Error **errp)
 #ifdef CONFIG_POSIX
 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                  uint32_t ram_flags, int fd, off_t offset,
-                                 bool readonly, Error **errp)
+                                 Error **errp)
 {
     RAMBlock *new_block;
     Error *local_err = NULL;
@@ -2065,7 +2064,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
 
     /* Just support these ram flags by now. */
     assert((ram_flags & ~(RAM_SHARED | RAM_PMEM | RAM_NORESERVE |
-                          RAM_PROTECTED)) == 0);
+                          RAM_PROTECTED | RAM_READONLY)) == 0);
 
     if (xen_enabled()) {
         error_setg(errp, "-mem-path not supported with Xen");
@@ -2100,8 +2099,7 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
     new_block->used_length = size;
     new_block->max_length = size;
     new_block->flags = ram_flags;
-    new_block->host = file_ram_alloc(new_block, fd, readonly,
-                                     !file_size, offset, errp);
+    new_block->host = file_ram_alloc(new_block, fd, !file_size, offset, errp);
     if (!new_block->host) {
         g_free(new_block);
         return NULL;
@@ -2120,11 +2118,11 @@ RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
 
 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                    uint32_t ram_flags, const char *mem_path,
-                                   bool readonly, Error **errp)
+                                   Error **errp)
 {
     int fd;
-    bool created;
     RAMBlock *block;
+    bool created, readonly = ram_flags & RAM_READONLY;
 
     fd = file_ram_open(mem_path, memory_region_name(mr), readonly, &created,
                        errp);
@@ -2132,7 +2130,7 @@ RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
         return NULL;
     }
 
-    block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, readonly, errp);
+    block = qemu_ram_alloc_from_fd(size, mr, ram_flags, fd, 0, errp);
     if (!block) {
         if (created) {
             unlink(mem_path);
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Reviewed-by on v1 9/21
#27 lore raw

Re: [PATCH RFC 09/21] ramblock: Add RAM_READONLY

In reply to Peter Xu
Quoted 11 lines · Peter Xu

This allows us to have RAM_READONLY to be set in ram_flags to show that this ramblock can only be read not write.

We used to pass in readonly boolean along the way for allocating the ramblock, now let it be together with the rest ramblock flags.

The main purpose of this patch is not for clean up though, it's for caching mapping information of each ramblock so when we want to mmap() it again for whatever reason we can have all the information on hand.

author
#28 lore raw

RFC PATCH 10/21 ramblock: Add ramblock_file_map()

In reply to Peter Xu

Add a helper to do mmap() for a ramblock based on the cached informations.

A trivial thing to mention is we need to move ramblock->fd setup to be earlier, before the ramblock_file_map() call, because it'll need to reference the fd being mapped. However that should not be a problem at all, majorly because the fd won't be freed if successful, and if it failed the fd will be freeed (or to be explicit, close()ed) by the caller.

Export it - prepare to be used outside this file.

---
 include/exec/ram_addr.h |  1 +
 softmmu/physmem.c       | 25 +++++++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)
include/exec/ram_addr.h +1 -0
index 0bf9cfc659..56db25009a 100644
@@ -98,6 +98,7 @@ bool ramblock_is_pmem(RAMBlock *rb);
 
 long qemu_minrampagesize(void);
 long qemu_maxrampagesize(void);
+void *ramblock_file_map(RAMBlock *block);
 
 /**
  * qemu_ram_alloc_from_file,
softmmu/physmem.c +17 -8
index 6096eac286..cdda7eaea5 100644
@@ -1532,17 +1532,31 @@ static int file_ram_open(const char *path,
     return fd;
 }
 
+/* Do the mmap() for a ramblock based on information already setup */
+void *ramblock_file_map(RAMBlock *block)
+{
+    uint32_t qemu_map_flags;
+
+    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
+    qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
+    qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
+    qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
+
+    return qemu_ram_mmap(block->fd, block->mmap_length, block->mr->align,
+                         qemu_map_flags, block->file_offset);
+}
+
 static void *file_ram_alloc(RAMBlock *block,
                             int fd,
                             bool truncate,
                             off_t offset,
                             Error **errp)
 {
-    uint32_t qemu_map_flags;
     void *area;
 
     /* Remember the offset just in case we'll need to map the range again */
     block->file_offset = offset;
+    block->fd = fd;
     block->page_size = qemu_fd_getpagesize(fd);
     if (block->mr->align % block->page_size) {
         error_setg(errp, "alignment 0x%" PRIx64
@@ -1588,19 +1602,14 @@ static void *file_ram_alloc(RAMBlock *block,
         perror("ftruncate");
     }
 
-    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
-    qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
-    qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
-    qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
-    area = qemu_ram_mmap(fd, block->mmap_length, block->mr->align,
-                         qemu_map_flags, offset);
+    area = ramblock_file_map(block);
+
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
         return NULL;
     }
 
-    block->fd = fd;
     return area;
 }
 #endif
Signature · 1 line
2.37.3
Reviewed-by on v1 10/21
#29 lore raw

Re: [PATCH RFC 10/21] ramblock: Add ramblock_file_map()

Quoted 50 lines · * Peter Xu ([email protected]) wrote:

Add a helper to do mmap() for a ramblock based on the cached informations.

A trivial thing to mention is we need to move ramblock->fd setup to be earlier, before the ramblock_file_map() call, because it'll need to reference the fd being mapped. However that should not be a problem at all, majorly because the fd won't be freed if successful, and if it failed the fd will be freeed (or to be explicit, close()ed) by the caller.

Export it - prepare to be used outside this file.

---
 include/exec/ram_addr.h |  1 +
 softmmu/physmem.c       | 25 +++++++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)
include/exec/ram_addr.h +1 -0
index 0bf9cfc659..56db25009a 100644
@@ -98,6 +98,7 @@ bool ramblock_is_pmem(RAMBlock *rb);
 
 long qemu_minrampagesize(void);
 long qemu_maxrampagesize(void);
+void *ramblock_file_map(RAMBlock *block);
 
 /**
  * qemu_ram_alloc_from_file,
softmmu/physmem.c +14 -0
index 6096eac286..cdda7eaea5 100644
@@ -1532,17 +1532,31 @@ static int file_ram_open(const char *path,
     return fd;
 }
 
+/* Do the mmap() for a ramblock based on information already setup */
+void *ramblock_file_map(RAMBlock *block)
+{
+    uint32_t qemu_map_flags;
+
+    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
+    qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
+    qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
+    qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
+
+    return qemu_ram_mmap(block->fd, block->mmap_length, block->mr->align,
+                         qemu_map_flags, block->file_offset);
+}
+

softmmu/physmem.c static int file_ram_open(const char *path, · line 1557

Quoted 10 lines
softmmu/physmem.c +0 -1
 static void *file_ram_alloc(RAMBlock *block,
                             int fd,
                             bool truncate,
                             off_t offset,
                             Error **errp)
 {
-    uint32_t qemu_map_flags;
     void *area;
 
     /* Remember the offset just in case we'll need to map the range again */

Note that this comment is now wrong; you need to always set that for the map call.

Other than that,

Quoted 30 lines
softmmu/physmem.c +3 -7
     block->file_offset = offset;
+    block->fd = fd;
     block->page_size = qemu_fd_getpagesize(fd);
     if (block->mr->align % block->page_size) {
         error_setg(errp, "alignment 0x%" PRIx64
@@ -1588,19 +1602,14 @@ static void *file_ram_alloc(RAMBlock *block,
         perror("ftruncate");
     }
 
-    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
-    qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
-    qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
-    qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
-    area = qemu_ram_mmap(fd, block->mmap_length, block->mr->align,
-                         qemu_map_flags, offset);
+    area = ramblock_file_map(block);
+
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
         return NULL;
     }
 
-    block->fd = fd;
     return area;
 }
 #endif
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#30 lore raw

Re: [PATCH RFC 10/21] ramblock: Add ramblock_file_map()

Quoted 60 lines · Dr. David Alan Gilbert
Quoted 59 lines · * Peter Xu ([email protected]) wrote:

Add a helper to do mmap() for a ramblock based on the cached informations.

A trivial thing to mention is we need to move ramblock->fd setup to be earlier, before the ramblock_file_map() call, because it'll need to reference the fd being mapped. However that should not be a problem at all, majorly because the fd won't be freed if successful, and if it failed the fd will be freeed (or to be explicit, close()ed) by the caller.

Export it - prepare to be used outside this file.

---
 include/exec/ram_addr.h |  1 +
 softmmu/physmem.c       | 25 +++++++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)
include/exec/ram_addr.h +1 -0
index 0bf9cfc659..56db25009a 100644
@@ -98,6 +98,7 @@ bool ramblock_is_pmem(RAMBlock *rb);
 
 long qemu_minrampagesize(void);
 long qemu_maxrampagesize(void);
+void *ramblock_file_map(RAMBlock *block);
 
 /**
  * qemu_ram_alloc_from_file,
softmmu/physmem.c +14 -1
index 6096eac286..cdda7eaea5 100644
@@ -1532,17 +1532,31 @@ static int file_ram_open(const char *path,
     return fd;
 }
 
+/* Do the mmap() for a ramblock based on information already setup */
+void *ramblock_file_map(RAMBlock *block)
+{
+    uint32_t qemu_map_flags;
+
+    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
+    qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
+    qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
+    qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
+
+    return qemu_ram_mmap(block->fd, block->mmap_length, block->mr->align,
+                         qemu_map_flags, block->file_offset);
+}
+
 static void *file_ram_alloc(RAMBlock *block,
                             int fd,
                             bool truncate,
                             off_t offset,
                             Error **errp)
 {
-    uint32_t qemu_map_flags;
     void *area;
 
Quoted 4 lines

softmmu/physmem.c static int file_ram_open(const char *path, · line 1557

Quoted 1 line
softmmu/physmem.c +0 -0
     /* Remember the offset just in case we'll need to map the range again */

Note that this comment is now wrong; you need to always set that for the map call.

This line is added in patch 7. After this patch, a ramblock should always be mapped with ramblock_file_map(), so it keeps being true?

Quoted 4 lines

Other than that,

Thanks,

Signature · 1 line
Peter Xu
#31 lore raw

Re: [PATCH RFC 10/21] ramblock: Add ramblock_file_map()

Quoted 61 lines · * Peter Xu ([email protected]) wrote:
Quoted 60 lines · Dr. David Alan Gilbert
Quoted 59 lines · * Peter Xu ([email protected]) wrote:

Add a helper to do mmap() for a ramblock based on the cached informations.

A trivial thing to mention is we need to move ramblock->fd setup to be earlier, before the ramblock_file_map() call, because it'll need to reference the fd being mapped. However that should not be a problem at all, majorly because the fd won't be freed if successful, and if it failed the fd will be freeed (or to be explicit, close()ed) by the caller.

Export it - prepare to be used outside this file.

---
 include/exec/ram_addr.h |  1 +
 softmmu/physmem.c       | 25 +++++++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)
include/exec/ram_addr.h +1 -0
index 0bf9cfc659..56db25009a 100644
@@ -98,6 +98,7 @@ bool ramblock_is_pmem(RAMBlock *rb);
 
 long qemu_minrampagesize(void);
 long qemu_maxrampagesize(void);
+void *ramblock_file_map(RAMBlock *block);
 
 /**
  * qemu_ram_alloc_from_file,
softmmu/physmem.c +14 -1
index 6096eac286..cdda7eaea5 100644
@@ -1532,17 +1532,31 @@ static int file_ram_open(const char *path,
     return fd;
 }
 
+/* Do the mmap() for a ramblock based on information already setup */
+void *ramblock_file_map(RAMBlock *block)
+{
+    uint32_t qemu_map_flags;
+
+    qemu_map_flags = (block->flags & RAM_READONLY) ? QEMU_MAP_READONLY : 0;
+    qemu_map_flags |= (block->flags & RAM_SHARED) ? QEMU_MAP_SHARED : 0;
+    qemu_map_flags |= (block->flags & RAM_PMEM) ? QEMU_MAP_SYNC : 0;
+    qemu_map_flags |= (block->flags & RAM_NORESERVE) ? QEMU_MAP_NORESERVE : 0;
+
+    return qemu_ram_mmap(block->fd, block->mmap_length, block->mr->align,
+                         qemu_map_flags, block->file_offset);
+}
+
 static void *file_ram_alloc(RAMBlock *block,
                             int fd,
                             bool truncate,
                             off_t offset,
                             Error **errp)
 {
-    uint32_t qemu_map_flags;
     void *area;
 
Quoted 7 lines
Quoted 4 lines

softmmu/physmem.c static int file_ram_open(const char *path, · line 1557

Quoted 1 line
softmmu/physmem.c +0 -0
     /* Remember the offset just in case we'll need to map the range again */

Note that this comment is now wrong; you need to always set that for the map call.

This line is added in patch 7. After this patch, a ramblock should always be mapped with ramblock_file_map(), so it keeps being true?

With ramblock_file_map() it's not a 'just in case' any more though is it? This value always goes through the block-> now?

Dave

Quoted 10 lines
Quoted 4 lines

Other than that,

Thanks,

Signature · 1 line
Peter Xu
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#32 lore raw

Re: [PATCH RFC 10/21] ramblock: Add ramblock_file_map()

Quoted 11 lines · Dr. David Alan Gilbert
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

static void *file_ram_alloc(RAMBlock *block,
int fd,
bool truncate,
off_t offset,
Error **errp)
{

-    uint32_t qemu_map_flags;
     void *area;
 
     /* Remember the offset just in case we'll need to map the range again */
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Note that this comment is now wrong; you need to always set that for the map call.

This line is added in patch 7. After this patch, a ramblock should always be mapped with ramblock_file_map(), so it keeps being true?

With ramblock_file_map() it's not a 'just in case' any more though is it? This value always goes through the block-> now?

Ah yes.. Since the comment is not extremely informative, instead of changing it, I can drop it in the previous patch when introduced.

Signature · 1 line
Peter Xu
Reviewed-by on v1 10/21
#33 lore raw

Re: [PATCH RFC 10/21] ramblock: Add ramblock_file_map()

In reply to Peter Xu
Quoted 11 lines · Peter Xu

Add a helper to do mmap() for a ramblock based on the cached informations.

A trivial thing to mention is we need to move ramblock->fd setup to be earlier, before the ramblock_file_map() call, because it'll need to reference the fd being mapped. However that should not be a problem at all, majorly because the fd won't be freed if successful, and if it failed the fd will be freeed (or to be explicit, close()ed) by the caller.

Export it - prepare to be used outside this file.

Quoted 1 line

+void *ramblock_file_map(RAMBlock *block);

I would have called it:

void *qemu_ram_mmap_file(RAMBlock *block);

To make clear that it is 'like' qemu_ram_mmap(), but for a file.

But that is just a suggestion. Whoever does the patch, get the right to name the functions.

author
#34 lore raw

RFC PATCH 11/21 migration: Add hugetlb-doublemap cap

In reply to Peter Xu

Add a new cap to allow mapping hugetlbfs backed RAMs in small page sizes.

---
 migration/migration.c | 48 ++++++++++++++++++++++++++++++++++++++++++-
 migration/migration.h |  1 +
 qapi/migration.json   |  7 ++++++-
 3 files changed, 54 insertions(+), 2 deletions(-)
migration/migration.c +47 -1
index 64f74534e2..b174f2af92 100644
@@ -17,6 +17,7 @@
 #include "qemu/cutils.h"
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
+#include "qemu/madvise.h"
 #include "migration/blocker.h"
 #include "exec.h"
 #include "fd.h"
@@ -62,6 +63,7 @@
 #include "sysemu/cpus.h"
 #include "yank_functions.h"
 #include "sysemu/qtest.h"
+#include "exec/ramblock.h"
 
 #define MAX_THROTTLE  (128 << 20)      /* Migration transfer speed throttling */
 
@@ -1363,12 +1365,47 @@ static bool migrate_caps_check(bool *cap_list,
                    "Zero copy only available for non-compressed non-TLS multifd migration");
         return false;
     }
+
+    if (cap_list[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP]) {
+        RAMBlock *rb;
+
+        /* Check whether the platform/binary supports the new madvise()s */
+
+#if QEMU_MADV_SPLIT == QEMU_MADV_INVALID
+        error_setg(errp, "MADV_SPLIT is not supported by the QEMU binary");
+        return false;
+#endif
+
+#if QEMU_MADV_COLLAPSE == QEMU_MADV_INVALID
+        error_setg(errp, "MADV_COLLAPSE is not supported by the QEMU binary");
+        return false;
+#endif
+
+        /*
+         * Check against kernel support of MADV_SPLIT is not easy, delay
+         * that until we have all the hugetlb mappings ready on dest node,
+         * meanwhile do the best effort check here because doublemap
+         * requires the hugetlb ramblocks to be shared first.
+         */
+        RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
+            if (qemu_ram_is_hugetlb(rb) && !qemu_ram_is_shared(rb)) {
+                error_setg(errp, "RAMBlock '%s' needs to be shared for doublemap",
+                           rb->idstr);
+                return false;
+            }
+        }
+    }
 #else
     if (cap_list[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
         error_setg(errp,
                    "Zero copy currently only available on Linux");
         return false;
     }
+
+    if (cap_list[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP]) {
+        error_setg(errp, "Hugetlb doublemap is only supported on Linux");
+        return false;
+    }
 #endif
 
     if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
@@ -2792,6 +2829,13 @@ bool migrate_postcopy_preempt(void)
     return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
 }
 
+bool migrate_hugetlb_doublemap(void)
+{
+    MigrationState *s = migrate_get_current();
+
+    return s->enabled_capabilities[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP];
+}
+
 /* migration thread support */
 /*
  * Something bad happened to the RP stream, mark an error
@@ -4472,7 +4516,9 @@ static Property migration_properties[] = {
     DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
     DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
     DEFINE_PROP_MIG_CAP("x-background-snapshot",
-            MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
+                        MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
+    DEFINE_PROP_MIG_CAP("hugetlb-doublemap",
+                        MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP),
 #ifdef CONFIG_LINUX
     DEFINE_PROP_MIG_CAP("x-zero-copy-send",
             MIGRATION_CAPABILITY_ZERO_COPY_SEND),
migration/migration.h +1 -0
index 5674a13876..bbd610a2d5 100644
@@ -447,6 +447,7 @@ bool migrate_use_events(void);
 bool migrate_postcopy_blocktime(void);
 bool migrate_background_snapshot(void);
 bool migrate_postcopy_preempt(void);
+bool migrate_hugetlb_doublemap(void);
 
 /* Sending on the return path - generic and then for each message type */
 void migrate_send_rp_shut(MigrationIncomingState *mis,
qapi/migration.json +6 -1
index 88ecf86ac8..b23516e75e 100644
@@ -477,6 +477,11 @@
 #                    will be handled faster.  This is a performance feature and
 #                    should not affect the correctness of postcopy migration.
 #                    (since 7.1)
+# @hugetlb-doublemap: If enabled, the migration process will allow postcopy
+#                     to handle page faults based on small pages even if
+#                     hugetlb is used.  This will drastically reduce page
+#                     fault latencies when hugetlb is used as the guest RAM
+#                     backends. (since 7.3)
 #
 # Features:
 # @unstable: Members @x-colo and @x-ignore-shared are experimental.
@@ -492,7 +497,7 @@
            'dirty-bitmaps', 'postcopy-blocktime', 'late-block-activate',
            { 'name': 'x-ignore-shared', 'features': [ 'unstable' ] },
            'validate-uuid', 'background-snapshot',
-           'zero-copy-send', 'postcopy-preempt'] }
+           'zero-copy-send', 'postcopy-preempt', 'hugetlb-doublemap'] }
 
 ##
 # @MigrationCapabilityStatus:
Signature · 1 line
2.37.3
Reviewed-by on v1 11/21
#35 lore raw

Re: [PATCH RFC 11/21] migration: Add hugetlb-doublemap cap

Quoted 3 lines · * Peter Xu ([email protected]) wrote:

Add a new cap to allow mapping hugetlbfs backed RAMs in small page sizes.

although, I'm curious if the protocol actually changes - or whether a doublepage enabled destination would work with an unmodified source? I guess potentially you can get away without the dirty clearing of the partially sent hugepages that the source normally does.

Dave

Quoted 139 lines
---
 migration/migration.c | 48 ++++++++++++++++++++++++++++++++++++++++++-
 migration/migration.h |  1 +
 qapi/migration.json   |  7 ++++++-
 3 files changed, 54 insertions(+), 2 deletions(-)
migration/migration.c +47 -1
index 64f74534e2..b174f2af92 100644
@@ -17,6 +17,7 @@
 #include "qemu/cutils.h"
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
+#include "qemu/madvise.h"
 #include "migration/blocker.h"
 #include "exec.h"
 #include "fd.h"
@@ -62,6 +63,7 @@
 #include "sysemu/cpus.h"
 #include "yank_functions.h"
 #include "sysemu/qtest.h"
+#include "exec/ramblock.h"
 
 #define MAX_THROTTLE  (128 << 20)      /* Migration transfer speed throttling */
 
@@ -1363,12 +1365,47 @@ static bool migrate_caps_check(bool *cap_list,
                    "Zero copy only available for non-compressed non-TLS multifd migration");
         return false;
     }
+
+    if (cap_list[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP]) {
+        RAMBlock *rb;
+
+        /* Check whether the platform/binary supports the new madvise()s */
+
+#if QEMU_MADV_SPLIT == QEMU_MADV_INVALID
+        error_setg(errp, "MADV_SPLIT is not supported by the QEMU binary");
+        return false;
+#endif
+
+#if QEMU_MADV_COLLAPSE == QEMU_MADV_INVALID
+        error_setg(errp, "MADV_COLLAPSE is not supported by the QEMU binary");
+        return false;
+#endif
+
+        /*
+         * Check against kernel support of MADV_SPLIT is not easy, delay
+         * that until we have all the hugetlb mappings ready on dest node,
+         * meanwhile do the best effort check here because doublemap
+         * requires the hugetlb ramblocks to be shared first.
+         */
+        RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
+            if (qemu_ram_is_hugetlb(rb) && !qemu_ram_is_shared(rb)) {
+                error_setg(errp, "RAMBlock '%s' needs to be shared for doublemap",
+                           rb->idstr);
+                return false;
+            }
+        }
+    }
 #else
     if (cap_list[MIGRATION_CAPABILITY_ZERO_COPY_SEND]) {
         error_setg(errp,
                    "Zero copy currently only available on Linux");
         return false;
     }
+
+    if (cap_list[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP]) {
+        error_setg(errp, "Hugetlb doublemap is only supported on Linux");
+        return false;
+    }
 #endif
 
     if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT]) {
@@ -2792,6 +2829,13 @@ bool migrate_postcopy_preempt(void)
     return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_PREEMPT];
 }
 
+bool migrate_hugetlb_doublemap(void)
+{
+    MigrationState *s = migrate_get_current();
+
+    return s->enabled_capabilities[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP];
+}
+
 /* migration thread support */
 /*
  * Something bad happened to the RP stream, mark an error
@@ -4472,7 +4516,9 @@ static Property migration_properties[] = {
     DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH),
     DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD),
     DEFINE_PROP_MIG_CAP("x-background-snapshot",
-            MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
+                        MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT),
+    DEFINE_PROP_MIG_CAP("hugetlb-doublemap",
+                        MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP),
 #ifdef CONFIG_LINUX
     DEFINE_PROP_MIG_CAP("x-zero-copy-send",
             MIGRATION_CAPABILITY_ZERO_COPY_SEND),
migration/migration.h +1 -0
index 5674a13876..bbd610a2d5 100644
@@ -447,6 +447,7 @@ bool migrate_use_events(void);
 bool migrate_postcopy_blocktime(void);
 bool migrate_background_snapshot(void);
 bool migrate_postcopy_preempt(void);
+bool migrate_hugetlb_doublemap(void);
 
 /* Sending on the return path - generic and then for each message type */
 void migrate_send_rp_shut(MigrationIncomingState *mis,
qapi/migration.json +6 -1
index 88ecf86ac8..b23516e75e 100644
@@ -477,6 +477,11 @@
 #                    will be handled faster.  This is a performance feature and
 #                    should not affect the correctness of postcopy migration.
 #                    (since 7.1)
+# @hugetlb-doublemap: If enabled, the migration process will allow postcopy
+#                     to handle page faults based on small pages even if
+#                     hugetlb is used.  This will drastically reduce page
+#                     fault latencies when hugetlb is used as the guest RAM
+#                     backends. (since 7.3)
 #
 # Features:
 # @unstable: Members @x-colo and @x-ignore-shared are experimental.
@@ -492,7 +497,7 @@
            'dirty-bitmaps', 'postcopy-blocktime', 'late-block-activate',
            { 'name': 'x-ignore-shared', 'features': [ 'unstable' ] },
            'validate-uuid', 'background-snapshot',
-           'zero-copy-send', 'postcopy-preempt'] }
+           'zero-copy-send', 'postcopy-preempt', 'hugetlb-doublemap'] }
 
 ##
 # @MigrationCapabilityStatus:
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#36 lore raw

Re: [PATCH RFC 11/21] migration: Add hugetlb-doublemap cap

Quoted 7 lines · Dr. David Alan Gilbert
Quoted 3 lines · * Peter Xu ([email protected]) wrote:

Add a new cap to allow mapping hugetlbfs backed RAMs in small page sizes.

Thanks.

Quoted 2 lines

although, I'm curious if the protocol actually changes

Yes it does.

It differs not in the form of a changed header or any frame definitions, but in the format of how huge pages are sent. The old binary can only send a huge page by sending all the small pages sequentially starting from index 0 to index N_HUGE-1; while the new binary can send the huge page out of order. For the latter it's the same as when huge page is not used.

Quoted 2 lines

or whether a doublepage enabled destination would work with an unmodified source?

This is an interesting question.

I would expect old -> new work as usual, because the page frames are not modified so the dest node will just see pages being migrated in a sequential manner. The latency of page request will be the same as old binary though because even if dest host can handle small pages it won't be able to get asap on the pages it wants - src host decides which page to send.

Meanwhile new -> old shouldn't work I think as described above, because the dest host should see weird things happening, e.g., a huge page was sent not starting fron index 0 but index X (0<X<N_HUGE-1). It should quickly bail out assuming there's something wrong.

Quoted 2 lines

I guess potentially you can get away without the dirty clearing of the partially sent hugepages that the source normally does.

Good point. It's actually more relevant to the other patch later on reworking the discard logic. I kept it as-is for majorly two reasons:

1) It is still not 100% confirmed on how MADV_DONTNEED should behave on HGM enabled memory ranges where huge pages used to be mapped. It's part of the discussion upstream on the kernel patchset. I think it's settling, but in the current series I kept it in a form so it'll work in all cases.

2) Not dirtying the partially sent huge pages can always reduce small pages being migrated, but it can also change the content of discard messages due to the frame format of MIG_CMD_POSTCOPY_RAM_DISCARD, in that we can have a lot more scattered ranges, so a lot more messaging can be needed. While when with the existing logic, since we'll always re-dirty the partial sent pages, the ranges are more likely to be efficient.

* CMD_POSTCOPY_RAM_DISCARD consist of:
* byte version (0)
* byte Length of name field (not including 0)
* n x byte RAM block name
* byte 0 terminator (just for safety)
* n x Byte ranges within the named RAMBlock
* be64 Start of the range
* be64 Length

I think 1) may not hold as the kernel series evolves, so it may not be true anymore. 2) may still be true, but I think worth some testing (especially on 1G pages) to see how it could interfere the discard procedure. Maybe it won't be as bad as I think. Even if it could, we can evaluate the tradeoff between "slower discard sync" and "less page need to send". E.g., we can consider changing the frame layout by boosting postcopy_ram_discard_version.

I'll take a note on this one and provide more update in the next version.

Signature · 1 line
Peter Xu
Reviewed-by on v1 11/21
#37 lore raw

Re: [PATCH RFC 11/21] migration: Add hugetlb-doublemap cap

In reply to Peter Xu
Quoted 3 lines · Peter Xu

Add a new cap to allow mapping hugetlbfs backed RAMs in small page sizes.

migration/migration.c bool migrate_postcopy_preempt(void) · line 2837

Quoted 6 lines
migration/migration.c +6 -0
+bool migrate_hugetlb_doublemap(void)
+{
+    MigrationState *s = migrate_get_current();
+
+    return s->enabled_capabilities[MIGRATION_CAPABILITY_HUGETLB_DOUBLEMAP];
+}

I think it was not our finest moment when we decided to name the functions that query capabilities without a verb, but well, everything else uses "this" convention, so ....

author
#38 lore raw

RFC PATCH 12/21 migration: Introduce page size for-migration-only

In reply to Peter Xu

Migration may not want to recognize memory chunks in page size of the host only, but sometimes we may want to recognize the memory in smaller chunks if e.g. they're doubly mapped as both huge and small.

In those cases we'll prefer to assume the memory page size is always mapped small (qemu_real_host_page_size) and we'll do things just like when the pages was only smally mapped.

Let's do this to be prepared of postcopy double-mapping for hugetlbfs.

---
 migration/migration.c    |  6 ++++--
 migration/postcopy-ram.c | 16 +++++++++-------
 migration/ram.c          | 29 ++++++++++++++++++++++-------
 migration/ram.h          |  1 +
 4 files changed, 36 insertions(+), 16 deletions(-)
migration/migration.c +4 -2
index b174f2af92..f6fe474fc3 100644
@@ -408,7 +408,7 @@ int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
 {
     uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
     size_t msglen = 12; /* start + len */
-    size_t len = qemu_ram_pagesize(rb);
+    size_t len = migration_ram_pagesize(rb);
     enum mig_rp_message_type msg_type;
     const char *rbname;
     int rbname_len;
@@ -443,8 +443,10 @@ int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
 int migrate_send_rp_req_pages(MigrationIncomingState *mis,
                               RAMBlock *rb, ram_addr_t start, uint64_t haddr)
 {
-    void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
     bool received = false;
+    void *aligned;
+
+    aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, migration_ram_pagesize(rb));
 
     WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
         received = ramblock_recv_bitmap_test_byte_offset(rb, start);
migration/postcopy-ram.c +9 -7
index 2c86bfc091..acae1dc6ae 100644
@@ -694,7 +694,7 @@ int postcopy_wake_shared(struct PostCopyFD *pcfd,
                          uint64_t client_addr,
                          RAMBlock *rb)
 {
-    size_t pagesize = qemu_ram_pagesize(rb);
+    size_t pagesize = migration_ram_pagesize(rb);
     struct uffdio_range range;
     int ret;
     trace_postcopy_wake_shared(client_addr, qemu_ram_get_idstr(rb));
@@ -712,7 +712,9 @@ int postcopy_wake_shared(struct PostCopyFD *pcfd,
 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
                                  ram_addr_t start, uint64_t haddr)
 {
-    void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
+    void *aligned;
+
+    aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, migration_ram_pagesize(rb));
 
     /*
      * Discarded pages (via RamDiscardManager) are never migrated. On unlikely
@@ -722,7 +724,7 @@ static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
      * Checking a single bit is sufficient to handle pagesize > TPS as either
      * all relevant bits are set or not.
      */
-    assert(QEMU_IS_ALIGNED(start, qemu_ram_pagesize(rb)));
+    assert(QEMU_IS_ALIGNED(start, migration_ram_pagesize(rb)));
     if (ramblock_page_is_discarded(rb, start)) {
         bool received = ramblock_recv_bitmap_test_byte_offset(rb, start);
 
@@ -740,7 +742,7 @@ static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
                                  uint64_t client_addr, uint64_t rb_offset)
 {
-    uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
+    uint64_t aligned_rbo = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
     MigrationIncomingState *mis = migration_incoming_get_current();
 
     trace_postcopy_request_shared_page(pcfd->idstr, qemu_ram_get_idstr(rb),
@@ -1020,7 +1022,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
                 break;
             }
 
-            rb_offset = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
+            rb_offset = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
             trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
                                                 qemu_ram_get_idstr(rb),
                                                 rb_offset,
@@ -1281,7 +1283,7 @@ int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
                         RAMBlock *rb)
 {
-    size_t pagesize = qemu_ram_pagesize(rb);
+    size_t pagesize = migration_ram_pagesize(rb);
 
     /* copy also acks to the kernel waking the stalled thread up
      * TODO: We can inhibit that ack and only do it if it was requested
@@ -1308,7 +1310,7 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
                              RAMBlock *rb)
 {
-    size_t pagesize = qemu_ram_pagesize(rb);
+    size_t pagesize = migration_ram_pagesize(rb);
     trace_postcopy_place_page_zero(host);
 
     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
migration/ram.c +22 -7
index 334309f1c6..945c6477fd 100644
@@ -121,6 +121,20 @@ static struct {
     uint8_t *decoded_buf;
 } XBZRLE;
 
+/* Get the page size we should use for migration purpose. */
+size_t migration_ram_pagesize(RAMBlock *block)
+{
+    /*
+     * When hugetlb doublemap is enabled, we should always use the smallest
+     * page for migration.
+     */
+    if (migrate_hugetlb_doublemap()) {
+        return qemu_real_host_page_size();
+    }
+
+    return qemu_ram_pagesize(block);
+}
+
 static void XBZRLE_cache_lock(void)
 {
     if (migrate_use_xbzrle()) {
@@ -1049,7 +1063,7 @@ bool ramblock_page_is_discarded(RAMBlock *rb, ram_addr_t start)
         MemoryRegionSection section = {
             .mr = rb->mr,
             .offset_within_region = start,
-            .size = int128_make64(qemu_ram_pagesize(rb)),
+            .size = int128_make64(migration_ram_pagesize(rb)),
         };
 
         return !ram_discard_manager_is_populated(rdm, &section);
@@ -2152,7 +2166,7 @@ int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len)
      */
     if (postcopy_preempt_active()) {
         ram_addr_t page_start = start >> TARGET_PAGE_BITS;
-        size_t page_size = qemu_ram_pagesize(ramblock);
+        size_t page_size = migration_ram_pagesize(ramblock);
         PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_POSTCOPY];
         int ret = 0;
 
@@ -2316,7 +2330,7 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss)
 static void pss_host_page_prepare(PageSearchStatus *pss)
 {
     /* How many guest pages are there in one host page? */
-    size_t guest_pfns = qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+    size_t guest_pfns = migration_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
 
     pss->host_page_sending = true;
     pss->host_page_start = ROUND_DOWN(pss->page, guest_pfns);
@@ -2425,7 +2439,7 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss)
     bool page_dirty, preempt_active = postcopy_preempt_active();
     int tmppages, pages = 0;
     size_t pagesize_bits =
-        qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+        migration_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
     unsigned long start_page = pss->page;
     int res;
 
@@ -3518,7 +3532,7 @@ static void *host_page_from_ram_block_offset(RAMBlock *block,
 {
     /* Note: Explicitly no check against offset_in_ramblock(). */
     return (void *)QEMU_ALIGN_DOWN((uintptr_t)(block->host + offset),
-                                   block->page_size);
+                                   migration_ram_pagesize(block));
 }
 
 static ram_addr_t host_page_offset_from_ram_block_offset(RAMBlock *block,
@@ -3970,7 +3984,8 @@ int ram_load_postcopy(QEMUFile *f, int channel)
                 break;
             }
             tmp_page->target_pages++;
-            matches_target_page_size = block->page_size == TARGET_PAGE_SIZE;
+            matches_target_page_size =
+                migration_ram_pagesize(block) == TARGET_PAGE_SIZE;
             /*
              * Postcopy requires that we place whole host pages atomically;
              * these may be huge pages for RAMBlocks that are backed by
@@ -4005,7 +4020,7 @@ int ram_load_postcopy(QEMUFile *f, int channel)
              * page
              */
             if (tmp_page->target_pages ==
-                (block->page_size / TARGET_PAGE_SIZE)) {
+                (migration_ram_pagesize(block) / TARGET_PAGE_SIZE)) {
                 place_needed = true;
             }
             place_source = tmp_page->tmp_huge_page;
migration/ram.h +1 -0
index 81cbb0947c..162b3e7cb8 100644
@@ -68,6 +68,7 @@ bool ramblock_is_ignored(RAMBlock *block);
         if (!qemu_ram_is_migratable(block)) {} else
 
 int xbzrle_cache_resize(uint64_t new_size, Error **errp);
+size_t migration_ram_pagesize(RAMBlock *block);
 uint64_t ram_bytes_remaining(void);
 uint64_t ram_bytes_total(void);
 void mig_throttle_counter_reset(void);
Signature · 1 line
2.37.3
#39 lore raw

Re: [PATCH RFC 12/21] migration: Introduce page size for-migration-only

Quoted 183 lines · * Peter Xu ([email protected]) wrote:

Migration may not want to recognize memory chunks in page size of the host only, but sometimes we may want to recognize the memory in smaller chunks if e.g. they're doubly mapped as both huge and small.

In those cases we'll prefer to assume the memory page size is always mapped small (qemu_real_host_page_size) and we'll do things just like when the pages was only smally mapped.

Let's do this to be prepared of postcopy double-mapping for hugetlbfs.

---
 migration/migration.c    |  6 ++++--
 migration/postcopy-ram.c | 16 +++++++++-------
 migration/ram.c          | 29 ++++++++++++++++++++++-------
 migration/ram.h          |  1 +
 4 files changed, 36 insertions(+), 16 deletions(-)
migration/migration.c +4 -2
index b174f2af92..f6fe474fc3 100644
@@ -408,7 +408,7 @@ int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
 {
     uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
     size_t msglen = 12; /* start + len */
-    size_t len = qemu_ram_pagesize(rb);
+    size_t len = migration_ram_pagesize(rb);
     enum mig_rp_message_type msg_type;
     const char *rbname;
     int rbname_len;
@@ -443,8 +443,10 @@ int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
 int migrate_send_rp_req_pages(MigrationIncomingState *mis,
                               RAMBlock *rb, ram_addr_t start, uint64_t haddr)
 {
-    void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
     bool received = false;
+    void *aligned;
+
+    aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, migration_ram_pagesize(rb));
 
     WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
         received = ramblock_recv_bitmap_test_byte_offset(rb, start);
migration/postcopy-ram.c +9 -7
index 2c86bfc091..acae1dc6ae 100644
@@ -694,7 +694,7 @@ int postcopy_wake_shared(struct PostCopyFD *pcfd,
                          uint64_t client_addr,
                          RAMBlock *rb)
 {
-    size_t pagesize = qemu_ram_pagesize(rb);
+    size_t pagesize = migration_ram_pagesize(rb);
     struct uffdio_range range;
     int ret;
     trace_postcopy_wake_shared(client_addr, qemu_ram_get_idstr(rb));
@@ -712,7 +712,9 @@ int postcopy_wake_shared(struct PostCopyFD *pcfd,
 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
                                  ram_addr_t start, uint64_t haddr)
 {
-    void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
+    void *aligned;
+
+    aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, migration_ram_pagesize(rb));
 
     /*
      * Discarded pages (via RamDiscardManager) are never migrated. On unlikely
@@ -722,7 +724,7 @@ static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
      * Checking a single bit is sufficient to handle pagesize > TPS as either
      * all relevant bits are set or not.
      */
-    assert(QEMU_IS_ALIGNED(start, qemu_ram_pagesize(rb)));
+    assert(QEMU_IS_ALIGNED(start, migration_ram_pagesize(rb)));
     if (ramblock_page_is_discarded(rb, start)) {
         bool received = ramblock_recv_bitmap_test_byte_offset(rb, start);
 
@@ -740,7 +742,7 @@ static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
                                  uint64_t client_addr, uint64_t rb_offset)
 {
-    uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
+    uint64_t aligned_rbo = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
     MigrationIncomingState *mis = migration_incoming_get_current();
 
     trace_postcopy_request_shared_page(pcfd->idstr, qemu_ram_get_idstr(rb),
@@ -1020,7 +1022,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
                 break;
             }
 
-            rb_offset = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
+            rb_offset = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
             trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
                                                 qemu_ram_get_idstr(rb),
                                                 rb_offset,
@@ -1281,7 +1283,7 @@ int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
                         RAMBlock *rb)
 {
-    size_t pagesize = qemu_ram_pagesize(rb);
+    size_t pagesize = migration_ram_pagesize(rb);
 
     /* copy also acks to the kernel waking the stalled thread up
      * TODO: We can inhibit that ack and only do it if it was requested
@@ -1308,7 +1310,7 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
                              RAMBlock *rb)
 {
-    size_t pagesize = qemu_ram_pagesize(rb);
+    size_t pagesize = migration_ram_pagesize(rb);
     trace_postcopy_place_page_zero(host);
 
     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
migration/ram.c +19 -5
index 334309f1c6..945c6477fd 100644
@@ -121,6 +121,20 @@ static struct {
     uint8_t *decoded_buf;
 } XBZRLE;
 
+/* Get the page size we should use for migration purpose. */
+size_t migration_ram_pagesize(RAMBlock *block)
+{
+    /*
+     * When hugetlb doublemap is enabled, we should always use the smallest
+     * page for migration.
+     */
+    if (migrate_hugetlb_doublemap()) {
+        return qemu_real_host_page_size();
+    }
+
+    return qemu_ram_pagesize(block);
+}
+
 static void XBZRLE_cache_lock(void)
 {
     if (migrate_use_xbzrle()) {
@@ -1049,7 +1063,7 @@ bool ramblock_page_is_discarded(RAMBlock *rb, ram_addr_t start)
         MemoryRegionSection section = {
             .mr = rb->mr,
             .offset_within_region = start,
-            .size = int128_make64(qemu_ram_pagesize(rb)),
+            .size = int128_make64(migration_ram_pagesize(rb)),
         };
 
         return !ram_discard_manager_is_populated(rdm, &section);
@@ -2152,7 +2166,7 @@ int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len)
      */
     if (postcopy_preempt_active()) {
         ram_addr_t page_start = start >> TARGET_PAGE_BITS;
-        size_t page_size = qemu_ram_pagesize(ramblock);
+        size_t page_size = migration_ram_pagesize(ramblock);
         PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_POSTCOPY];
         int ret = 0;
 
@@ -2316,7 +2330,7 @@ static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss)
 static void pss_host_page_prepare(PageSearchStatus *pss)
 {
     /* How many guest pages are there in one host page? */
-    size_t guest_pfns = qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+    size_t guest_pfns = migration_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
 
     pss->host_page_sending = true;
     pss->host_page_start = ROUND_DOWN(pss->page, guest_pfns);
@@ -2425,7 +2439,7 @@ static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss)
     bool page_dirty, preempt_active = postcopy_preempt_active();
     int tmppages, pages = 0;
     size_t pagesize_bits =
-        qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
+        migration_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
     unsigned long start_page = pss->page;
     int res;
 
@@ -3518,7 +3532,7 @@ static void *host_page_from_ram_block_offset(RAMBlock *block,
 {
     /* Note: Explicitly no check against offset_in_ramblock(). */
     return (void *)QEMU_ALIGN_DOWN((uintptr_t)(block->host + offset),
-                                   block->page_size);
+                                   migration_ram_pagesize(block));
 }
 
 static ram_addr_t host_page_offset_from_ram_block_offset(RAMBlock *block,

migration/ram.c int ram_load_postcopy(QEMUFile *f, int channel) · line 3991

Quoted 9 lines
migration/ram.c +2 -1
@@ -3970,7 +3984,8 @@ int ram_load_postcopy(QEMUFile *f, int channel)
                 break;
             }
             tmp_page->target_pages++;
-            matches_target_page_size = block->page_size == TARGET_PAGE_SIZE;
+            matches_target_page_size =
+                migration_ram_pagesize(block) == TARGET_PAGE_SIZE;
             /*
              * Postcopy requires that we place whole host pages atomically;
              * these may be huge pages for RAMBlocks that are backed by

Hmm do you really want this change?

Dave

Quoted 24 lines
migration/ram.c +1 -1
@@ -4005,7 +4020,7 @@ int ram_load_postcopy(QEMUFile *f, int channel)
              * page
              */
             if (tmp_page->target_pages ==
-                (block->page_size / TARGET_PAGE_SIZE)) {
+                (migration_ram_pagesize(block) / TARGET_PAGE_SIZE)) {
                 place_needed = true;
             }
             place_source = tmp_page->tmp_huge_page;
migration/ram.h +1 -0
index 81cbb0947c..162b3e7cb8 100644
@@ -68,6 +68,7 @@ bool ramblock_is_ignored(RAMBlock *block);
         if (!qemu_ram_is_migratable(block)) {} else
 
 int xbzrle_cache_resize(uint64_t new_size, Error **errp);
+size_t migration_ram_pagesize(RAMBlock *block);
 uint64_t ram_bytes_remaining(void);
 uint64_t ram_bytes_total(void);
 void mig_throttle_counter_reset(void);
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#40 lore raw

Re: [PATCH RFC 12/21] migration: Introduce page size for-migration-only

Quoted 4 lines · Dr. David Alan Gilbert
Quoted 4 lines
migration/ram.c +0 -0
@@ -3970,7 +3984,8 @@ int ram_load_postcopy(QEMUFile *f, int channel)
                 break;
             }
             tmp_page->target_pages++;
Quoted 8 lines

migration/ram.c int ram_load_postcopy(QEMUFile *f, int channel) · line 3991

Quoted 6 lines
migration/ram.c +2 -1
-            matches_target_page_size = block->page_size == TARGET_PAGE_SIZE;
+            matches_target_page_size =
+                migration_ram_pagesize(block) == TARGET_PAGE_SIZE;
             /*
              * Postcopy requires that we place whole host pages atomically;
              * these may be huge pages for RAMBlocks that are backed by

Hmm do you really want this change?

Yes that's intended. I want to reuse the same logic here when receiving small pages from huge pages, just like when we're receiving small pages on non-hugetlb mappings.

matches_target_page_size majorly affects two things:

1) For a small zero page, whether we want to pre-set the page_buffer, or

     simply use postcopy_place_page_zero():
  
        case RAM_SAVE_FLAG_ZERO:
            ch = qemu_get_byte(f);
            /*
             * Can skip to set page_buffer when
             * this is a zero page and (block->page_size == TARGET_PAGE_SIZE).
             */
            if (ch || !matches_target_page_size) {
                memset(page_buffer, ch, TARGET_PAGE_SIZE);
            }

2) For normal page, whether we need to use a page buffer or we can

     directly reuse the page buffer in QEMUFile:

            if (!matches_target_page_size) {
                /* For huge pages, we always use temporary buffer */
                qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE);
            } else {
                /*
                 * For small pages that matches target page size, we
                 * avoid the qemu_file copy.  Instead we directly use
                 * the buffer of QEMUFile to place the page.  Note: we
                 * cannot do any QEMUFile operation before using that
                 * buffer to make sure the buffer is valid when
                 * placing the page.
                 */
                qemu_get_buffer_in_place(f, (uint8_t **)&place_source,
                                         TARGET_PAGE_SIZE);
            }

Here:

I want 1) to reuse postcopy_place_page_zero(). For the doublemap case, it'll reuse postcopy_tmp_zero_page() (because qemu_ram_is_uf_zeroable() will return false for such a ramblock).

I want 2) to reuse qemu_get_buffer_in_place(), so we avoid a copy process for the small page which is faster (even if it's hugetlb backed, now we can reuse the qemufile buffer safely).

Thanks,

Signature · 1 line
Peter Xu
author
#41 lore raw

Re: [PATCH RFC 12/21] migration: Introduce page size for-migration-only

Quoted 54 lines · Peter Xu
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 4 lines
migration/ram.c +0 -0
@@ -3970,7 +3984,8 @@ int ram_load_postcopy(QEMUFile *f, int channel)
                 break;
             }
             tmp_page->target_pages++;
Quoted 8 lines

migration/ram.c int ram_load_postcopy(QEMUFile *f, int channel) · line 3991

Quoted 6 lines
migration/ram.c +2 -1
-            matches_target_page_size = block->page_size == TARGET_PAGE_SIZE;
+            matches_target_page_size =
+                migration_ram_pagesize(block) == TARGET_PAGE_SIZE;
             /*
              * Postcopy requires that we place whole host pages atomically;
              * these may be huge pages for RAMBlocks that are backed by

Hmm do you really want this change?

Yes that's intended. I want to reuse the same logic here when receiving small pages from huge pages, just like when we're receiving small pages on non-hugetlb mappings.

matches_target_page_size majorly affects two things:

1) For a small zero page, whether we want to pre-set the page_buffer, or

     simply use postcopy_place_page_zero():
  
        case RAM_SAVE_FLAG_ZERO:
            ch = qemu_get_byte(f);
            /*
             * Can skip to set page_buffer when
             * this is a zero page and (block->page_size == TARGET_PAGE_SIZE).
             */
            if (ch || !matches_target_page_size) {
                memset(page_buffer, ch, TARGET_PAGE_SIZE);
            }

2) For normal page, whether we need to use a page buffer or we can

     directly reuse the page buffer in QEMUFile:

            if (!matches_target_page_size) {
                /* For huge pages, we always use temporary buffer */
                qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE);
            } else {
                /*
                 * For small pages that matches target page size, we
                 * avoid the qemu_file copy.  Instead we directly use
                 * the buffer of QEMUFile to place the page.  Note: we
                 * cannot do any QEMUFile operation before using that
                 * buffer to make sure the buffer is valid when
                 * placing the page.
                 */
                qemu_get_buffer_in_place(f, (uint8_t **)&place_source,
                                         TARGET_PAGE_SIZE);
            }

Here:

Quoted 7 lines

I want 1) to reuse postcopy_place_page_zero(). For the doublemap case, it'll reuse postcopy_tmp_zero_page() (because qemu_ram_is_uf_zeroable() will return false for such a ramblock).

I want 2) to reuse qemu_get_buffer_in_place(), so we avoid a copy process for the small page which is faster (even if it's hugetlb backed, now we can reuse the qemufile buffer safely).

Since at it, one more thing worth mentioning is I didn't actually know whether the original code is always correct when target and host small psizes don't match.. This is the original line:

  matches_target_page_size = block->page_size == TARGET_PAGE_SIZE;

The problem is we're comparing block page size against target page size, however block page size should be in host page size granule:

  RAMBlock *qemu_ram_alloc_internal()
  {
    new_block->page_size = qemu_real_host_page_size();

IOW, I am not sure whether postcopy will run at all in that case. For example, when we run an Alpha emulator upon x86_64, we can have target psize 8K while host psize 4K.

The migration protocol should be TARGET_PAGE_SIZE based. It means, for postcopy when receiving a single page for Alpha VM being migrated, maybe we should call UFFDIO_COPY (or UFFDIO_CONTINUE; doesn't matter here) twice because one guest page contains two host pages.

I'm not sure whether I get all these right.. if so, we have two options:

a) Forbid postcopy as a whole when detecting qemu_real_host_page_size()

     != TARGET_PAGE_SIZE.

b) Implement postcopy for that case

I'd go with a) even if it's an issue because it means no one is migrating that thing in postcopy way in the past N years, so it justifies that maybe
b) doesn't worth it.

Signature · 1 line
Peter Xu
Reviewed-by on v1 12/21
#42 lore raw

Re: [PATCH RFC 12/21] migration: Introduce page size for-migration-only

In reply to Peter Xu
Quoted 11 lines · Peter Xu

Migration may not want to recognize memory chunks in page size of the host only, but sometimes we may want to recognize the memory in smaller chunks if e.g. they're doubly mapped as both huge and small.

In those cases we'll prefer to assume the memory page size is always mapped small (qemu_real_host_page_size) and we'll do things just like when the pages was only smally mapped.

Let's do this to be prepared of postcopy double-mapping for hugetlbfs.

Quoted 21 lines
---
 migration/migration.c    |  6 ++++--
 migration/postcopy-ram.c | 16 +++++++++-------
 migration/ram.c          | 29 ++++++++++++++++++++++-------
 migration/ram.h          |  1 +
 4 files changed, 36 insertions(+), 16 deletions(-)
migration/migration.c +1 -1
index b174f2af92..f6fe474fc3 100644
@@ -408,7 +408,7 @@ int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
 {
     uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */
     size_t msglen = 12; /* start + len */
-    size_t len = qemu_ram_pagesize(rb);
+    size_t len = migration_ram_pagesize(rb);
     enum mig_rp_message_type msg_type;
     const char *rbname;
     int rbname_len;

migration/migration.c int migrate_send_rp_message_req_pages(MigrationIncomingState *mis, · line 449

Quoted 8 lines
migration/migration.c +3 -1
@@ -443,8 +443,10 @@ int migrate_send_rp_message_req_pages(MigrationIncomingState *mis,
 int migrate_send_rp_req_pages(MigrationIncomingState *mis,
                               RAMBlock *rb, ram_addr_t start, uint64_t haddr)
 {
-    void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
     bool received = false;
+    void *aligned;
+
+    aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, migration_ram_pagesize(rb));

I am trying that all new code declares variables at 1st use, and this goes in the wrong direction. As this happens more than once in this patch, can we change the macro (or create another macro) that also does the cast?

author
#43 lore raw

RFC PATCH 13/21 migration: Add migration_ram_pagesize_largest()

In reply to Peter Xu

Let it replace the old qemu_ram_pagesize_largest() just to fetch the page sizes using migration_ram_pagesize(), because it'll start to consider double mapping effect in migrations.

Also don't account the ignored ramblocks as they won't be migrated.

---
 include/exec/cpu-common.h |  1 -
 migration/migration.c     |  2 +-
 migration/ram.c           | 12 ++++++++++++
 migration/ram.h           |  1 +
 softmmu/physmem.c         | 13 -------------
 5 files changed, 14 insertions(+), 15 deletions(-)
include/exec/cpu-common.h +0 -1
index 94452aa17f..4c394ccdfc 100644
@@ -96,7 +96,6 @@ int qemu_ram_get_fd(RAMBlock *rb);
 
 size_t qemu_ram_pagesize(RAMBlock *block);
 bool qemu_ram_is_hugetlb(RAMBlock *rb);
-size_t qemu_ram_pagesize_largest(void);
 
 /**
  * cpu_address_space_init:
migration/migration.c +1 -1
index f6fe474fc3..7724e00c47 100644
@@ -604,7 +604,7 @@ process_incoming_migration_co(void *opaque)
 
     assert(mis->from_src_file);
     mis->migration_incoming_co = qemu_coroutine_self();
-    mis->largest_page_size = qemu_ram_pagesize_largest();
+    mis->largest_page_size = migration_ram_pagesize_largest();
     postcopy_state_set(POSTCOPY_INCOMING_NONE);
     migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
                       MIGRATION_STATUS_ACTIVE);
migration/ram.c +12 -0
index 945c6477fd..2ebf414f5f 100644
@@ -135,6 +135,18 @@ size_t migration_ram_pagesize(RAMBlock *block)
     return qemu_ram_pagesize(block);
 }
 
+size_t migration_ram_pagesize_largest(void)
+{
+    RAMBlock *block;
+    size_t largest = 0;
+
+    RAMBLOCK_FOREACH_NOT_IGNORED(block) {
+        largest = MAX(largest, migration_ram_pagesize(block));
+    }
+
+    return largest;
+}
+
 static void XBZRLE_cache_lock(void)
 {
     if (migrate_use_xbzrle()) {
migration/ram.h +1 -0
index 162b3e7cb8..cefe166841 100644
@@ -69,6 +69,7 @@ bool ramblock_is_ignored(RAMBlock *block);
 
 int xbzrle_cache_resize(uint64_t new_size, Error **errp);
 size_t migration_ram_pagesize(RAMBlock *block);
+size_t migration_ram_pagesize_largest(void);
 uint64_t ram_bytes_remaining(void);
 uint64_t ram_bytes_total(void);
 void mig_throttle_counter_reset(void);
softmmu/physmem.c +0 -13
index cdda7eaea5..536c204811 100644
@@ -1813,19 +1813,6 @@ bool qemu_ram_is_hugetlb(RAMBlock *rb)
     return rb->page_size > qemu_real_host_page_size();
 }
 
-/* Returns the largest size of page in use */
-size_t qemu_ram_pagesize_largest(void)
-{
-    RAMBlock *block;
-    size_t largest = 0;
-
-    RAMBLOCK_FOREACH(block) {
-        largest = MAX(largest, qemu_ram_pagesize(block));
-    }
-
-    return largest;
-}
-
 static int memory_try_enable_merging(void *addr, size_t len)
 {
     if (!machine_mem_merge(current_machine)) {
Signature · 1 line
2.37.3
Reviewed-by on v1 13/21
#44 lore raw

Re: [PATCH RFC 13/21] migration: Add migration_ram_pagesize_largest()

Quoted 7 lines · * Peter Xu ([email protected]) wrote:

Let it replace the old qemu_ram_pagesize_largest() just to fetch the page sizes using migration_ram_pagesize(), because it'll start to consider double mapping effect in migrations.

Also don't account the ignored ramblocks as they won't be migrated.

Quoted 95 lines
---
 include/exec/cpu-common.h |  1 -
 migration/migration.c     |  2 +-
 migration/ram.c           | 12 ++++++++++++
 migration/ram.h           |  1 +
 softmmu/physmem.c         | 13 -------------
 5 files changed, 14 insertions(+), 15 deletions(-)
include/exec/cpu-common.h +0 -1
index 94452aa17f..4c394ccdfc 100644
@@ -96,7 +96,6 @@ int qemu_ram_get_fd(RAMBlock *rb);
 
 size_t qemu_ram_pagesize(RAMBlock *block);
 bool qemu_ram_is_hugetlb(RAMBlock *rb);
-size_t qemu_ram_pagesize_largest(void);
 
 /**
  * cpu_address_space_init:
migration/migration.c +1 -1
index f6fe474fc3..7724e00c47 100644
@@ -604,7 +604,7 @@ process_incoming_migration_co(void *opaque)
 
     assert(mis->from_src_file);
     mis->migration_incoming_co = qemu_coroutine_self();
-    mis->largest_page_size = qemu_ram_pagesize_largest();
+    mis->largest_page_size = migration_ram_pagesize_largest();
     postcopy_state_set(POSTCOPY_INCOMING_NONE);
     migrate_set_state(&mis->state, MIGRATION_STATUS_NONE,
                       MIGRATION_STATUS_ACTIVE);
migration/ram.c +12 -0
index 945c6477fd..2ebf414f5f 100644
@@ -135,6 +135,18 @@ size_t migration_ram_pagesize(RAMBlock *block)
     return qemu_ram_pagesize(block);
 }
 
+size_t migration_ram_pagesize_largest(void)
+{
+    RAMBlock *block;
+    size_t largest = 0;
+
+    RAMBLOCK_FOREACH_NOT_IGNORED(block) {
+        largest = MAX(largest, migration_ram_pagesize(block));
+    }
+
+    return largest;
+}
+
 static void XBZRLE_cache_lock(void)
 {
     if (migrate_use_xbzrle()) {
migration/ram.h +1 -0
index 162b3e7cb8..cefe166841 100644
@@ -69,6 +69,7 @@ bool ramblock_is_ignored(RAMBlock *block);
 
 int xbzrle_cache_resize(uint64_t new_size, Error **errp);
 size_t migration_ram_pagesize(RAMBlock *block);
+size_t migration_ram_pagesize_largest(void);
 uint64_t ram_bytes_remaining(void);
 uint64_t ram_bytes_total(void);
 void mig_throttle_counter_reset(void);
softmmu/physmem.c +0 -13
index cdda7eaea5..536c204811 100644
@@ -1813,19 +1813,6 @@ bool qemu_ram_is_hugetlb(RAMBlock *rb)
     return rb->page_size > qemu_real_host_page_size();
 }
 
-/* Returns the largest size of page in use */
-size_t qemu_ram_pagesize_largest(void)
-{
-    RAMBlock *block;
-    size_t largest = 0;
-
-    RAMBLOCK_FOREACH(block) {
-        largest = MAX(largest, qemu_ram_pagesize(block));
-    }
-
-    return largest;
-}
-
 static int memory_try_enable_merging(void *addr, size_t len)
 {
     if (!machine_mem_merge(current_machine)) {
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Reviewed-by on v1 13/21
#45 lore raw

Re: [PATCH RFC 13/21] migration: Add migration_ram_pagesize_largest()

In reply to Peter Xu
Quoted 7 lines · Peter Xu

Let it replace the old qemu_ram_pagesize_largest() just to fetch the page sizes using migration_ram_pagesize(), because it'll start to consider double mapping effect in migrations.

Also don't account the ignored ramblocks as they won't be migrated.

author
#46 lore raw

RFC PATCH 14/21 migration: Map hugetlbfs ramblocks twice, and pre-allocate

In reply to Peter Xu

Add a RAMBlock.host_mirror for all the hugetlbfs backed guest memories. It'll be used to remap the same region twice and it'll be used to service page faults using UFFDIO_CONTINUE.

To make sure all accesses to these ranges will generate minor page faults not missing page faults, we need to pre-allocate the files to make sure page cache exist start from the beginning.

---
 include/exec/ramblock.h |  7 +++++
 migration/ram.c         | 59 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
include/exec/ramblock.h +7 -0
index 3f31ce1591..c76683c3c8 100644
@@ -28,6 +28,13 @@ struct RAMBlock {
     struct rcu_head rcu;
     struct MemoryRegion *mr;
     uint8_t *host;
+    /*
+     * This is only used for hugetlbfs ramblocks where doublemap is
+     * enabled.  The pointer is managed by dest host migration code, and
+     * should be NULL when migration is finished.  On src host, it should
+     * always be NULL.
+     */
+    uint8_t *host_mirror;
     uint8_t *colo_cache; /* For colo, VM's ram cache */
     ram_addr_t offset;
     ram_addr_t used_length;
migration/ram.c +59 -0

77 lines not on this page · raw message

Signature · 1 line
2.37.3
Reviewed-by on v1 14/21
#47 lore raw

Re: [PATCH RFC 14/21] migration: Map hugetlbfs ramblocks twice, and pre-allocate

Quoted 9 lines · * Peter Xu ([email protected]) wrote:

Add a RAMBlock.host_mirror for all the hugetlbfs backed guest memories. It'll be used to remap the same region twice and it'll be used to service page faults using UFFDIO_CONTINUE.

To make sure all accesses to these ranges will generate minor page faults not missing page faults, we need to pre-allocate the files to make sure page cache exist start from the beginning.

Quoted 110 lines
---
 include/exec/ramblock.h |  7 +++++
 migration/ram.c         | 59 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
include/exec/ramblock.h +7 -0
index 3f31ce1591..c76683c3c8 100644
@@ -28,6 +28,13 @@ struct RAMBlock {
     struct rcu_head rcu;
     struct MemoryRegion *mr;
     uint8_t *host;
+    /*
+     * This is only used for hugetlbfs ramblocks where doublemap is
+     * enabled.  The pointer is managed by dest host migration code, and
+     * should be NULL when migration is finished.  On src host, it should
+     * always be NULL.
+     */
+    uint8_t *host_mirror;
     uint8_t *colo_cache; /* For colo, VM's ram cache */
     ram_addr_t offset;
     ram_addr_t used_length;
migration/ram.c +59 -0
index 2ebf414f5f..37d7b3553a 100644
@@ -3879,6 +3879,57 @@ void colo_release_ram_cache(void)
     ram_state_cleanup(&ram_state);
 }
 
+static int migrate_hugetlb_doublemap_init(void)
+{
+    RAMBlock *rb;
+    void *addr;
+    int ret;
+
+    if (!migrate_hugetlb_doublemap()) {
+        return 0;
+    }
+
+    RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
+        if (qemu_ram_is_hugetlb(rb)) {
+            /*
+             * Firstly, we remap the same ramblock into another range of
+             * virtual address, so that we can write to the pages without
+             * touching the page tables that directly mapped for the guest.
+             */
+            addr = ramblock_file_map(rb);
+            if (addr == MAP_FAILED) {
+                ret = -errno;
+                error_report("%s: Duplicate mapping for hugetlb ramblock '%s'"
+                             "failed: %s", __func__, qemu_ram_get_idstr(rb),
+                             strerror(errno));
+                return ret;
+            }
+            rb->host_mirror = addr;
+
+            /*
+             * We need to make sure we pre-allocate the range with
+             * hugetlbfs pages before hand, so that all the page fault will
+             * be trapped as MINOR faults always, rather than MISSING
+             * faults in userfaultfd.
+             */
+            ret = qemu_madvise(addr, rb->mmap_length, QEMU_MADV_POPULATE_WRITE);
+            if (ret) {
+                error_report("Failed to populate hugetlb ramblock '%s': "
+                             "%s", qemu_ram_get_idstr(rb), strerror(-ret));
+                return ret;
+            }
+        }
+    }
+
+    /*
+     * When reach here, it means we've setup the mirror mapping for all the
+     * hugetlbfs pages.  Hence when page fault happens, we'll be able to
+     * resolve page faults using UFFDIO_CONTINUE for hugetlbfs pages, but
+     * we'll keep using UFFDIO_COPY for anonymous pages.
+     */
+    return 0;
+}
+
 /**
  * ram_load_setup: Setup RAM for migration incoming side
  *
@@ -3893,6 +3944,10 @@ static int ram_load_setup(QEMUFile *f, void *opaque)
         return -1;
     }
 
+    if (migrate_hugetlb_doublemap_init()) {
+        return -1;
+    }
+
     xbzrle_load_setup();
     ramblock_recv_map_init();
 
@@ -3913,6 +3968,10 @@ static int ram_load_cleanup(void *opaque)
     RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
         g_free(rb->receivedmap);
         rb->receivedmap = NULL;
+        if (rb->host_mirror) {
+            munmap(rb->host_mirror, rb->mmap_length);
+            rb->host_mirror = NULL;
+        }
     }
 
     return 0;
Signature · 1 line
2.37.3
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Reviewed-by on v1 14/21
#48 lore raw

Re: [PATCH RFC 14/21] migration: Map hugetlbfs ramblocks twice, and pre-allocate

In reply to Peter Xu
Quoted 9 lines · Peter Xu

Add a RAMBlock.host_mirror for all the hugetlbfs backed guest memories. It'll be used to remap the same region twice and it'll be used to service page faults using UFFDIO_CONTINUE.

To make sure all accesses to these ranges will generate minor page faults not missing page faults, we need to pre-allocate the files to make sure page cache exist start from the beginning.

but what about this change

Quoted 10 lines
---
 include/exec/ramblock.h |  7 +++++
 migration/ram.c         | 59 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
include/exec/ramblock.h +0 -0
index 3f31ce1591..c76683c3c8 100644

include/exec/ramblock.h struct RAMBlock { · line 37

Quoted 10 lines
include/exec/ramblock.h +7 -0
@@ -28,6 +28,13 @@ struct RAMBlock {
     struct rcu_head rcu;
     struct MemoryRegion *mr;
     uint8_t *host;
+    /*
+     * This is only used for hugetlbfs ramblocks where doublemap is
+     * enabled.  The pointer is managed by dest host migration code, and
+     * should be NULL when migration is finished.  On src host, it should
+     * always be NULL.
+     */
+    uint8_t *host_mirror;

I would consider here:

    uint8_t *host_doublemap;

as I have not a small name that means

    uint8_t *host_map_smaller_size_pages;

That explains why we need it.

Quoted 8 lines
include/exec/ramblock.h +0 -0
     uint8_t *colo_cache; /* For colo, VM's ram cache */
     ram_addr_t offset;
     ram_addr_t used_length;
migration/ram.c +0 -0
index 2ebf414f5f..37d7b3553a 100644

migration/ram.c void colo_release_ram_cache(void) · line 3886

Quoted 8 lines
migration/ram.c +5 -0
@@ -3879,6 +3879,57 @@ void colo_release_ram_cache(void)
     ram_state_cleanup(&ram_state);
 }
 
+static int migrate_hugetlb_doublemap_init(void)
+{
+    RAMBlock *rb;
+    void *addr;
+    int ret;

Not initialized variables, remove the last two.

migration/ram.c void colo_release_ram_cache(void) · line 3891

Quoted 4 lines
migration/ram.c +4 -0
+    if (!migrate_hugetlb_doublemap()) {
+        return 0;
+    }
+

I would move the declaration of the RAMBlock here.

migration/ram.c void colo_release_ram_cache(void) · line 3899

Quoted 8 lines
migration/ram.c +8 -0
+    RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
+        if (qemu_ram_is_hugetlb(rb)) {
+            /*
+             * Firstly, we remap the same ramblock into another range of
+             * virtual address, so that we can write to the pages without
+             * touching the page tables that directly mapped for the guest.
+             */
+            addr = ramblock_file_map(rb);
               void *addr = ramblock_file_map(rb);

migration/ram.c void colo_release_ram_cache(void) · line 3901

Quoted 2 lines
migration/ram.c +2 -0
+            if (addr == MAP_FAILED) {
+                ret = -errno;
                   int ret = -errno;
Quoted 4 lines
migration/ram.c +4 -0
+                error_report("%s: Duplicate mapping for hugetlb ramblock '%s'"
+                             "failed: %s", __func__, qemu_ram_get_idstr(rb),
+                             strerror(errno));
+                return ret;

migration/ram.c void colo_release_ram_cache(void) · line 3915

Quoted 10 lines
migration/ram.c +10 -0
+            }
+            rb->host_mirror = addr;
+
+            /*
+             * We need to make sure we pre-allocate the range with
+             * hugetlbfs pages before hand, so that all the page fault will
+             * be trapped as MINOR faults always, rather than MISSING
+             * faults in userfaultfd.
+             */
+            ret = qemu_madvise(addr, rb->mmap_length, QEMU_MADV_POPULATE_WRITE);
               int ret = qemu_madvise(addr, rb->mmap_length, QEMU_MADV_POPULATE_WRITE);
Quoted 16 lines
migration/ram.c +16 -0
+            if (ret) {
+                error_report("Failed to populate hugetlb ramblock '%s': "
+                             "%s", qemu_ram_get_idstr(rb), strerror(-ret));
+                return ret;
+            }
+        }
+    }
+
+    /*
+     * When reach here, it means we've setup the mirror mapping for all the
+     * hugetlbfs pages.  Hence when page fault happens, we'll be able to
+     * resolve page faults using UFFDIO_CONTINUE for hugetlbfs pages, but
+     * we'll keep using UFFDIO_COPY for anonymous pages.
+     */
+    return 0;
+}
author
#49 lore raw

Re: [PATCH RFC 14/21] migration: Map hugetlbfs ramblocks twice, and pre-allocate

Quoted 38 lines · Juan Quintela
Quoted 9 lines · Peter Xu

Add a RAMBlock.host_mirror for all the hugetlbfs backed guest memories. It'll be used to remap the same region twice and it'll be used to service page faults using UFFDIO_CONTINUE.

To make sure all accesses to these ranges will generate minor page faults not missing page faults, we need to pre-allocate the files to make sure page cache exist start from the beginning.

but what about this change

Quoted 10 lines
---
 include/exec/ramblock.h |  7 +++++
 migration/ram.c         | 59 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
include/exec/ramblock.h +0 -0
index 3f31ce1591..c76683c3c8 100644

include/exec/ramblock.h struct RAMBlock { · line 37

Quoted 10 lines
include/exec/ramblock.h +7 -0
@@ -28,6 +28,13 @@ struct RAMBlock {
     struct rcu_head rcu;
     struct MemoryRegion *mr;
     uint8_t *host;
+    /*
+     * This is only used for hugetlbfs ramblocks where doublemap is
+     * enabled.  The pointer is managed by dest host migration code, and
+     * should be NULL when migration is finished.  On src host, it should
+     * always be NULL.
+     */
+    uint8_t *host_mirror;

I would consider here:

Quoted 6 lines
    uint8_t *host_doublemap;

as I have not a small name that means

    uint8_t *host_map_smaller_size_pages;

That explains why we need it.

Sure, I can rename this one if it helps.

One thing worth mention is that, it's not mapping things in small page size here with host_doublemap but in huge page size only.

It's just that UFFDIO_CONTINUE needs another mapping to resolve the page faults. It'll be the guest hugetlb ramblocks that will be mapped in small pages during postcopy.

Quoted 13 lines
Quoted 13 lines
include/exec/ramblock.h +0 -0
     uint8_t *colo_cache; /* For colo, VM's ram cache */
     ram_addr_t offset;
     ram_addr_t used_length;
migration/ram.c +0 -0
index 2ebf414f5f..37d7b3553a 100644
@@ -3879,6 +3879,57 @@ void colo_release_ram_cache(void)
     ram_state_cleanup(&ram_state);
 }
 
Quoted 7 lines

migration/ram.c void colo_release_ram_cache(void) · line 3886

Quoted 5 lines
migration/ram.c +5 -0
+static int migrate_hugetlb_doublemap_init(void)
+{
+    RAMBlock *rb;
+    void *addr;
+    int ret;

Not initialized variables, remove the last two.

I can do this.

Quoted 7 lines

migration/ram.c void colo_release_ram_cache(void) · line 3891

Quoted 5 lines
migration/ram.c +4 -0
+    if (!migrate_hugetlb_doublemap()) {
+        return 0;
+    }
+

I would move the declaration of the RAMBlock here.

But isn't QEMU in most cases declaring variables at the start of any code block, rather than after or in the middle of any code segments? IIRC some compiler should start to fail with it, even though not on the modern ones.

Thanks,

Signature · 1 line
Peter Xu
#50 lore raw

Re: [PATCH RFC 14/21] migration: Map hugetlbfs ramblocks twice, and pre-allocate

Quoted 8 lines · Peter Xu
Quoted 7 lines · Juan Quintela

I would consider here:

    uint8_t *host_doublemap;

as I have not a small name that means

    uint8_t *host_map_smaller_size_pages;
Quoted 6 lines
Quoted 1 line

That explains why we need it.

Sure, I can rename this one if it helps.

One thing worth mention is that, it's not mapping things in small page size here with host_doublemap but in huge page size only.

Thanks.

Quoted 3 lines

It's just that UFFDIO_CONTINUE needs another mapping to resolve the page faults. It'll be the guest hugetlb ramblocks that will be mapped in small pages during postcopy.

ok

Quoted 10 lines
Quoted 1 line

Not initialized variables, remove the last two.

I can do this.

Quoted 6 lines
Quoted 5 lines
migration/ram.c +4 -0
+    if (!migrate_hugetlb_doublemap()) {
+        return 0;
+    }
+
Quoted 5 lines
Quoted 1 line

I would move the declaration of the RAMBlock here.

But isn't QEMU in most cases declaring variables at the start of any code block, rather than after or in the middle of any code segments? IIRC some compiler should start to fail with it, even though not on the modern ones.

We can declare variables since c99. Only 24 years have passed O:-)

Anyways:

Exhibit A: We already have that kind of code

static int nocomp_send_prepare(MultiFDSendParams *p, Error **errp)

{
    MultiFDPages_t *pages = p->pages;

    for (int i = 0; i < p->normal_num; i++) {
        p->iov[p->iovs_num].iov_base = pages->block->host + p->normal[i];
        p->iov[p->iovs_num].iov_len = p->page_size;
        p->iovs_num++;
    }

    p->next_packet_size = p->normal_num * p->page_size;
    p->flags |= MULTIFD_FLAG_NOCOMP;
    return nocomp;
}

Exhibit B:

from configure:

#if defined(__clang_major__) && defined(__clang_minor__)

# ifdef __apple_build_version__
#  if __clang_major__ < 10 || (__clang_major__ == 10 && __clang_minor__ < 0)
#   error You need at least XCode Clang v10.0 to compile QEMU
#  endif
# else
#  if __clang_major__ < 6 || (__clang_major__ == 6 && __clang_minor__ < 0)
#   error You need at least Clang v6.0 to compile QEMU
#  endif
# endif
#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
# if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 4)
#  error You need at least GCC v7.4.0 to compile QEMU
# endif
#else
# error You either need GCC or Clang to compiler QEMU
#endif
int main (void) { return 0; }
EOF

gcc-7.4.0: supports C11, so we are good here
https://gcc.gnu.org/onlinedocs/gcc-7.4.0/gcc/Standards.html#C-Language

clang 6.0: supports c11 and c17 standard
https://releases.llvm.org/6.0.0/tools/clang/docs/ReleaseNotes.html

So as far as I can see, we are good here.

Later, Juan.

author
#51 lore raw

Re: [PATCH RFC 14/21] migration: Map hugetlbfs ramblocks twice, and pre-allocate

Quoted 34 lines · Juan Quintela
Quoted 8 lines · Peter Xu
Quoted 7 lines · Juan Quintela

I would consider here:

    uint8_t *host_doublemap;

as I have not a small name that means

    uint8_t *host_map_smaller_size_pages;
Quoted 6 lines
Quoted 1 line

That explains why we need it.

Sure, I can rename this one if it helps.

One thing worth mention is that, it's not mapping things in small page size here with host_doublemap but in huge page size only.

Thanks.

Quoted 3 lines

It's just that UFFDIO_CONTINUE needs another mapping to resolve the page faults. It'll be the guest hugetlb ramblocks that will be mapped in small pages during postcopy.

ok

Quoted 10 lines
Quoted 1 line

Not initialized variables, remove the last two.

I can do this.

Quoted 6 lines
Quoted 5 lines
migration/ram.c +4 -0
+    if (!migrate_hugetlb_doublemap()) {
+        return 0;
+    }
+
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I would move the declaration of the RAMBlock here.

But isn't QEMU in most cases declaring variables at the start of any code block, rather than after or in the middle of any code segments? IIRC some compiler should start to fail with it, even though not on the modern ones.

We can declare variables since c99. Only 24 years have passed O:-)

Oh OK :)

Quoted 44 lines

Anyways:

Exhibit A: We already have that kind of code

static int nocomp_send_prepare(MultiFDSendParams *p, Error **errp)

{
    MultiFDPages_t *pages = p->pages;

    for (int i = 0; i < p->normal_num; i++) {
        p->iov[p->iovs_num].iov_base = pages->block->host + p->normal[i];
        p->iov[p->iovs_num].iov_len = p->page_size;
        p->iovs_num++;
    }

    p->next_packet_size = p->normal_num * p->page_size;
    p->flags |= MULTIFD_FLAG_NOCOMP;
    return nocomp;
}

Exhibit B:

from configure:

#if defined(__clang_major__) && defined(__clang_minor__)

# ifdef __apple_build_version__
#  if __clang_major__ < 10 || (__clang_major__ == 10 && __clang_minor__ < 0)
#   error You need at least XCode Clang v10.0 to compile QEMU
#  endif
# else
#  if __clang_major__ < 6 || (__clang_major__ == 6 && __clang_minor__ < 0)
#   error You need at least Clang v6.0 to compile QEMU
#  endif
# endif
#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
# if __GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 4)
#  error You need at least GCC v7.4.0 to compile QEMU
# endif
#else
# error You either need GCC or Clang to compiler QEMU
#endif
int main (void) { return 0; }
EOF
Quoted 8 lines

gcc-7.4.0: supports C11, so we are good here
https://gcc.gnu.org/onlinedocs/gcc-7.4.0/gcc/Standards.html#C-Language

clang 6.0: supports c11 and c17 standard
https://releases.llvm.org/6.0.0/tools/clang/docs/ReleaseNotes.html

So as far as I can see, we are good here.

Thanks, I'll switch over.

Signature · 1 line
Peter Xu
author
#52 lore raw

RFC PATCH 15/21 migration: Teach qemu about minor faults and doublemap

In reply to Peter Xu

When a ramblock is backed by hugetlbfs and the user specified using double-map feature, we trap the faults on these regions using minor mode. Teach QEMU about that.

Add some sanity check on the fault flags when receiving a uffd message. For minor fault trapped ranges, we should always see the MINOR flag set, while when using generic missing faults we should never see it.

---
 migration/postcopy-ram.c | 99 ++++++++++++++++++++++++++++++++--------
 migration/postcopy-ram.h |  1 +
 2 files changed, 81 insertions(+), 19 deletions(-)
migration/postcopy-ram.c +80 -19
index acae1dc6ae..86ff73c2c0 100644
@@ -325,12 +325,25 @@ static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
 
     if (qemu_real_host_page_size() != ram_pagesize_summary()) {
         bool have_hp = false;
-        /* We've got a huge page */
+
+        /*
+         * If we're using doublemap, we need MINOR fault, otherwise we need
+         * MISSING fault (which is the default).
+         */
+        if (migrate_hugetlb_doublemap()) {
+#ifdef UFFD_FEATURE_MINOR_HUGETLBFS
+            have_hp = supported_features & UFFD_FEATURE_MINOR_HUGETLBFS;
+#endif
+        } else {
 #ifdef UFFD_FEATURE_MISSING_HUGETLBFS
-        have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
+            have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
 #endif
+        }
+
         if (!have_hp) {
-            error_report("Userfault on this host does not support huge pages");
+            error_report("Userfault on this host does not support huge pages "
+                         "with %s fault traps", migrate_hugetlb_doublemap() ?
+                         "MINOR" : "MISSING");
             return false;
         }
     }
@@ -669,22 +682,43 @@ static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
 {
     MigrationIncomingState *mis = opaque;
     struct uffdio_register reg_struct;
+    bool minor_fault = postcopy_use_minor_fault(rb);
 
     reg_struct.range.start = (uintptr_t)qemu_ram_get_host_addr(rb);
     reg_struct.range.len = rb->postcopy_length;
+
+    /*
+     * For hugetlbfs with double-map enabled, we trap pages using minor
+     * mode, otherwise we use missing mode.  Note: we also register missing
+     * mode for doublemap, but we should never hit it.
+     */
     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
+    if (minor_fault) {
+        reg_struct.mode |= UFFDIO_REGISTER_MODE_MINOR;
+    }
 
     /* Now tell our userfault_fd that it's responsible for this area */
     if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, &reg_struct)) {
         error_report("%s userfault register: %s", __func__, strerror(errno));
         return -1;
     }
-    if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
-        error_report("%s userfault: Region doesn't support COPY", __func__);
-        return -1;
-    }
-    if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
-        qemu_ram_set_uf_zeroable(rb);
+
+    if (minor_fault) {
+        /* Using minor faults for this ramblock */
+        if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_CONTINUE))) {
+            error_report("%s userfault: Region doesn't support CONTINUE",
+                         __func__);
+            return -1;
+        }
+    } else {
+        /* Using missing faults for this ramblock */
+        if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
+            error_report("%s userfault: Region doesn't support COPY", __func__);
+            return -1;
+        }
+        if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
+            qemu_ram_set_uf_zeroable(rb);
+        }
     }
 
     return 0;
@@ -916,6 +950,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
 {
     MigrationIncomingState *mis = opaque;
     struct uffd_msg msg;
+    uint64_t address;
     int ret;
     size_t index;
     RAMBlock *rb = NULL;
@@ -945,6 +980,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
     }
 
     while (true) {
+        bool use_minor_fault, minor_flag;
         ram_addr_t rb_offset;
         int poll_result;
 
@@ -1022,22 +1058,37 @@ static void *postcopy_ram_fault_thread(void *opaque)
                 break;
             }
 
-            rb_offset = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
-            trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
-                                                qemu_ram_get_idstr(rb),
-                                                rb_offset,
-                                                msg.arg.pagefault.feat.ptid);
-            mark_postcopy_blocktime_begin(
-                    (uintptr_t)(msg.arg.pagefault.address),
-                                msg.arg.pagefault.feat.ptid, rb);
+            address = ROUND_DOWN(msg.arg.pagefault.address,
+                                 migration_ram_pagesize(rb));
+            use_minor_fault = postcopy_use_minor_fault(rb);
+            minor_flag = !!(msg.arg.pagefault.flags &
+                            UFFD_PAGEFAULT_FLAG_MINOR);
 
+            /*
+             * Do sanity check on the message flags to make sure this is
+             * the one we expect to receive.  When using minor fault on
+             * this ramblock, it should _always_ be set; when not using
+             * minor fault, it should _never_ be set.
+             */
+            if (use_minor_fault ^ minor_flag) {
+                error_report("%s: Unexpected page fault flags (0x%"PRIx64") "
+                             "for address 0x%"PRIx64" (mode=%s)", __func__,
+                             (uint64_t)msg.arg.pagefault.flags,
+                             (uint64_t)msg.arg.pagefault.address,
+                             use_minor_fault ? "MINOR" : "MISSING");
+            }
+
+            trace_postcopy_ram_fault_thread_request(
+                address, qemu_ram_get_idstr(rb), rb_offset,
+                msg.arg.pagefault.feat.ptid);
+            mark_postcopy_blocktime_begin(
+                    (uintptr_t)(address), msg.arg.pagefault.feat.ptid, rb);
 retry:
             /*
              * Send the request to the source - we want to request one
              * of our host page sizes (which is >= TPS)
              */
-            ret = postcopy_request_page(mis, rb, rb_offset,
-                                        msg.arg.pagefault.address);
+            ret = postcopy_request_page(mis, rb, rb_offset, address);
             if (ret) {
                 /* May be network failure, try to wait for recovery */
                 postcopy_pause_fault_thread(mis);
@@ -1694,3 +1745,13 @@ void *postcopy_preempt_thread(void *opaque)
 
     return NULL;
 }
+
+/*
+ * Whether we should use MINOR fault to trap page faults?  It will be used
+ * when doublemap is enabled on hugetlbfs.  The default value will be
+ * false, which means we'll keep using the legacy MISSING faults.
+ */
+bool postcopy_use_minor_fault(RAMBlock *rb)
+{
+    return migrate_hugetlb_doublemap() && qemu_ram_is_hugetlb(rb);
+}
migration/postcopy-ram.h +1 -0
index b4867a32d5..32734d2340 100644
@@ -193,5 +193,6 @@ enum PostcopyChannels {
 void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file);
 void postcopy_preempt_setup(MigrationState *s);
 int postcopy_preempt_establish_channel(MigrationState *s);
+bool postcopy_use_minor_fault(RAMBlock *rb);
 
 #endif
Signature · 1 line
2.37.3
Reviewed-by on v1 15/21
#53 lore raw

Re: [PATCH RFC 15/21] migration: Teach qemu about minor faults and doublemap

Quoted 9 lines · Peter Xu

When a ramblock is backed by hugetlbfs and the user specified using double-map feature, we trap the faults on these regions using minor mode. Teach QEMU about that.

Add some sanity check on the fault flags when receiving a uffd message. For minor fault trapped ranges, we should always see the MINOR flag set, while when using generic missing faults we should never see it.

Quoted 1 line

- if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {

Does qemu have a macro to do this bitmap handling?

migration/postcopy-ram.c static void *postcopy_ram_fault_thread(void *opaque) · line 983

Quoted 12 lines
 {
     MigrationIncomingState *mis = opaque;
     struct uffd_msg msg;
migration/postcopy-ram.c +2 -0
+    uint64_t address;
     int ret;
     size_t index;
     RAMBlock *rb = NULL;
@@ -945,6 +980,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
     }
 
     while (true) {
+        bool use_minor_fault, minor_flag;

I think that something on the lines of:

           bool src_minor_fault, dst_minor_fault;

will make things simpler. Reviewing, I have to go back to definition place to know which is which.

Quoted 38 lines
migration/postcopy-ram.c +22 -8
         ram_addr_t rb_offset;
         int poll_result;
 
@@ -1022,22 +1058,37 @@ static void *postcopy_ram_fault_thread(void *opaque)
                 break;
             }
 
-            rb_offset = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
-            trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
-                                                qemu_ram_get_idstr(rb),
-                                                rb_offset,
-                                                msg.arg.pagefault.feat.ptid);
-            mark_postcopy_blocktime_begin(
-                    (uintptr_t)(msg.arg.pagefault.address),
-                                msg.arg.pagefault.feat.ptid, rb);
+            address = ROUND_DOWN(msg.arg.pagefault.address,
+                                 migration_ram_pagesize(rb));
+            use_minor_fault = postcopy_use_minor_fault(rb);
+            minor_flag = !!(msg.arg.pagefault.flags &
+                            UFFD_PAGEFAULT_FLAG_MINOR);
 
+            /*
+             * Do sanity check on the message flags to make sure this is
+             * the one we expect to receive.  When using minor fault on
+             * this ramblock, it should _always_ be set; when not using
+             * minor fault, it should _never_ be set.
+             */
+            if (use_minor_fault ^ minor_flag) {
+                error_report("%s: Unexpected page fault flags (0x%"PRIx64") "
+                             "for address 0x%"PRIx64" (mode=%s)", __func__,
+                             (uint64_t)msg.arg.pagefault.flags,
+                             (uint64_t)msg.arg.pagefault.address,
+                             use_minor_fault ? "MINOR" : "MISSING");
+            }
+
+            trace_postcopy_ram_fault_thread_request(
+                address, qemu_ram_get_idstr(rb), rb_offset,
+                msg.arg.pagefault.feat.ptid);

migration/postcopy-ram.c static void *postcopy_ram_fault_thread(void *opaque) · line 1091

Quoted 10 lines
migration/postcopy-ram.c +3 -2
+            mark_postcopy_blocktime_begin(
+                    (uintptr_t)(address), msg.arg.pagefault.feat.ptid, rb);
 retry:
             /*
              * Send the request to the source - we want to request one
              * of our host page sizes (which is >= TPS)
              */
-            ret = postcopy_request_page(mis, rb, rb_offset,
-                                        msg.arg.pagefault.address);
+            ret = postcopy_request_page(mis, rb, rb_offset, address);

This is the only change that I find 'problematic'.
On old code, rb_offset has been ROUND_DOWN, on new code it is not. On old code we pass msg.arg.pagefault.address, now we use ROUND_DOW(msg.arg.pagefault.address, mighration_ram_pagesize(rb)).

Quoted 7 lines
migration/postcopy-ram.c +0 -0
             if (ret) {
                 /* May be network failure, try to wait for recovery */
                 postcopy_pause_fault_thread(mis);
@@ -1694,3 +1745,13 @@ void *postcopy_preempt_thread(void *opaque)
 
     return NULL;
 }

migration/postcopy-ram.c void *postcopy_preempt_thread(void *opaque) · line 1757

Quoted 10 lines
migration/postcopy-ram.c +10 -0
+
+/*
+ * Whether we should use MINOR fault to trap page faults?  It will be used
+ * when doublemap is enabled on hugetlbfs.  The default value will be
+ * false, which means we'll keep using the legacy MISSING faults.
+ */
+bool postcopy_use_minor_fault(RAMBlock *rb)
+{
+    return migrate_hugetlb_doublemap() && qemu_ram_is_hugetlb(rb);
+}

Are you planing using this function outside postocpy-ram.c? Otherwise if you move up its definition you can make it static and drop the header change.

Later, Juan.

author
#54 lore raw

Re: [PATCH RFC 15/21] migration: Teach qemu about minor faults and doublemap

Quoted 11 lines · Juan Quintela
Quoted 9 lines · Peter Xu

When a ramblock is backed by hugetlbfs and the user specified using double-map feature, we trap the faults on these regions using minor mode. Teach QEMU about that.

Add some sanity check on the fault flags when receiving a uffd message. For minor fault trapped ranges, we should always see the MINOR flag set, while when using generic missing faults we should never see it.

Quoted 7 lines
Quoted 1 line

- if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {

Does qemu have a macro to do this bitmap handling?

Not yet that's suitable. It's open coded like this in many places of postcopy. One thing close enough is bitmap_test_and_clear() but too heavy.

Quoted 11 lines
Quoted 11 lines
 {
     MigrationIncomingState *mis = opaque;
     struct uffd_msg msg;
migration/postcopy-ram.c +1 -0
+    uint64_t address;
     int ret;
     size_t index;
     RAMBlock *rb = NULL;
@@ -945,6 +980,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
     }
 
Quoted 8 lines

migration/postcopy-ram.c static void *postcopy_ram_fault_thread(void *opaque) · line 983

Quoted 2 lines
migration/postcopy-ram.c +1 -0
     while (true) {
+        bool use_minor_fault, minor_flag;

I think that something on the lines of:

           bool src_minor_fault, dst_minor_fault;

will make things simpler. Reviewing, I have to go back to definition place to know which is which.

These two values represents "what we expect" and "what we got from the message", so the only thing is I'm not sure whether src/dst matches the best here.

How about "expect_minor_fault" and "has_minor_fault" instead?

Quoted 46 lines
Quoted 46 lines
migration/postcopy-ram.c +24 -8
         ram_addr_t rb_offset;
         int poll_result;
 
@@ -1022,22 +1058,37 @@ static void *postcopy_ram_fault_thread(void *opaque)
                 break;
             }
 
-            rb_offset = ROUND_DOWN(rb_offset, migration_ram_pagesize(rb));
-            trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
-                                                qemu_ram_get_idstr(rb),
-                                                rb_offset,
-                                                msg.arg.pagefault.feat.ptid);
-            mark_postcopy_blocktime_begin(
-                    (uintptr_t)(msg.arg.pagefault.address),
-                                msg.arg.pagefault.feat.ptid, rb);
+            address = ROUND_DOWN(msg.arg.pagefault.address,
+                                 migration_ram_pagesize(rb));
+            use_minor_fault = postcopy_use_minor_fault(rb);
+            minor_flag = !!(msg.arg.pagefault.flags &
+                            UFFD_PAGEFAULT_FLAG_MINOR);
 
+            /*
+             * Do sanity check on the message flags to make sure this is
+             * the one we expect to receive.  When using minor fault on
+             * this ramblock, it should _always_ be set; when not using
+             * minor fault, it should _never_ be set.
+             */
+            if (use_minor_fault ^ minor_flag) {
+                error_report("%s: Unexpected page fault flags (0x%"PRIx64") "
+                             "for address 0x%"PRIx64" (mode=%s)", __func__,
+                             (uint64_t)msg.arg.pagefault.flags,
+                             (uint64_t)msg.arg.pagefault.address,
+                             use_minor_fault ? "MINOR" : "MISSING");
+            }
+
+            trace_postcopy_ram_fault_thread_request(
+                address, qemu_ram_get_idstr(rb), rb_offset,
+                msg.arg.pagefault.feat.ptid);
+            mark_postcopy_blocktime_begin(
+                    (uintptr_t)(address), msg.arg.pagefault.feat.ptid, rb);
 retry:
             /*
              * Send the request to the source - we want to request one
              * of our host page sizes (which is >= TPS)
              */
Quoted 8 lines

migration/postcopy-ram.c static void *postcopy_ram_fault_thread(void *opaque) · line 1091

Quoted 3 lines
migration/postcopy-ram.c +1 -2
-            ret = postcopy_request_page(mis, rb, rb_offset,
-                                        msg.arg.pagefault.address);
+            ret = postcopy_request_page(mis, rb, rb_offset, address);

This is the only change that I find 'problematic'.
On old code, rb_offset has been ROUND_DOWN, on new code it is not. On old code we pass msg.arg.pagefault.address, now we use ROUND_DOW(msg.arg.pagefault.address, mighration_ram_pagesize(rb)).

Thanks for spotting such a detail even for a RFC series. :)

It's actually rounded down to target psize, here since we're in postcopy we should require target psize equals to host psize (or I bet it won't really work at all). So the relevant rounddown was actually done here:

            rb = qemu_ram_block_from_host(
                     (void *)(uintptr_t)msg.arg.pagefault.address,
                     true, &rb_offset);

In which there's:

    *offset = (host - block->host);
    if (round_offset) {
        *offset &= TARGET_PAGE_MASK;
    }

So when I rework that chunk of code I directly dropped the ROUND_DOWN() because I find it duplicated.

Quoted 6 lines
Quoted 6 lines
migration/postcopy-ram.c +0 -0
             if (ret) {
                 /* May be network failure, try to wait for recovery */
                 postcopy_pause_fault_thread(mis);
@@ -1694,3 +1745,13 @@ void *postcopy_preempt_thread(void *opaque)
 
Quoted 16 lines

migration/postcopy-ram.c void *postcopy_preempt_thread(void *opaque) · line 1757

Quoted 12 lines
migration/postcopy-ram.c +10 -0
     return NULL;
 }
+
+/*
+ * Whether we should use MINOR fault to trap page faults?  It will be used
+ * when doublemap is enabled on hugetlbfs.  The default value will be
+ * false, which means we'll keep using the legacy MISSING faults.
+ */
+bool postcopy_use_minor_fault(RAMBlock *rb)
+{
+    return migrate_hugetlb_doublemap() && qemu_ram_is_hugetlb(rb);
+}

Are you planing using this function outside postocpy-ram.c? Otherwise if you move up its definition you can make it static and drop the header change.

Yes, it'll be further used in ram.c later in the patch "migration: Rework ram discard logic for hugetlb double-map" right below.

Thanks,

Signature · 1 line
Peter Xu
#55 lore raw

Re: [PATCH RFC 15/21] migration: Teach qemu about minor faults and doublemap

Quoted 40 lines · Peter Xu
Quoted 11 lines · Juan Quintela
Quoted 9 lines · Peter Xu

When a ramblock is backed by hugetlbfs and the user specified using double-map feature, we trap the faults on these regions using minor mode. Teach QEMU about that.

Add some sanity check on the fault flags when receiving a uffd message. For minor fault trapped ranges, we should always see the MINOR flag set, while when using generic missing faults we should never see it.

Quoted 7 lines
Quoted 1 line

- if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {

Does qemu have a macro to do this bitmap handling?

Not yet that's suitable. It's open coded like this in many places of postcopy. One thing close enough is bitmap_test_and_clear() but too heavy.

Quoted 17 lines

migration/postcopy-ram.c static void *postcopy_ram_fault_thread(void *opaque) · line 983

Quoted 13 lines
 {
     MigrationIncomingState *mis = opaque;
     struct uffd_msg msg;
migration/postcopy-ram.c +2 -0
+    uint64_t address;
     int ret;
     size_t index;
     RAMBlock *rb = NULL;
@@ -945,6 +980,7 @@ static void *postcopy_ram_fault_thread(void *opaque)
     }
 
     while (true) {
+        bool use_minor_fault, minor_flag;

I think that something on the lines of:

           bool src_minor_fault, dst_minor_fault;
Quoted 8 lines
Quoted 2 lines

will make things simpler. Reviewing, I have to go back to definition place to know which is which.

These two values represents "what we expect" and "what we got from the message", so the only thing is I'm not sure whether src/dst matches the best here.

How about "expect_minor_fault" and "has_minor_fault" instead?

Perfect with me.

Quoted 25 lines
Quoted 8 lines

migration/postcopy-ram.c static void *postcopy_ram_fault_thread(void *opaque) · line 1091

Quoted 7 lines
migration/postcopy-ram.c +0 -0
             /*
              * Send the request to the source - we want to request one
              * of our host page sizes (which is >= TPS)
              */
migration/postcopy-ram.c +1 -2
-            ret = postcopy_request_page(mis, rb, rb_offset,
-                                        msg.arg.pagefault.address);
+            ret = postcopy_request_page(mis, rb, rb_offset, address);
Quoted 4 lines

This is the only change that I find 'problematic'.
On old code, rb_offset has been ROUND_DOWN, on new code it is not. On old code we pass msg.arg.pagefault.address, now we use ROUND_DOW(msg.arg.pagefault.address, mighration_ram_pagesize(rb)).

Thanks for spotting such a detail even for a RFC series. :)

It's actually rounded down to target psize, here since we're in postcopy we should require target psize equals to host psize (or I bet it won't really work at all). So the relevant rounddown was actually done here:

            rb = qemu_ram_block_from_host(
                     (void *)(uintptr_t)msg.arg.pagefault.address,
                     true, &rb_offset);

In which there's:

Quoted 7 lines
    *offset = (host - block->host);
    if (round_offset) {
        *offset &= TARGET_PAGE_MASK;
    }

So when I rework that chunk of code I directly dropped the ROUND_DOWN() because I find it duplicated.

Ok.

Quoted 24 lines
Quoted 7 lines
Quoted 7 lines

             if (ret) {
                 /* May be network failure, try to wait for recovery */
                 postcopy_pause_fault_thread(mis);
(quoted diff) +0 -0
@@ -1694,3 +1745,13 @@ void *postcopy_preempt_thread(void *opaque)
 
Quoted 16 lines

migration/postcopy-ram.c void *postcopy_preempt_thread(void *opaque) · line 1757

Quoted 12 lines
migration/postcopy-ram.c +10 -0
     return NULL;
 }
+
+/*
+ * Whether we should use MINOR fault to trap page faults?  It will be used
+ * when doublemap is enabled on hugetlbfs.  The default value will be
+ * false, which means we'll keep using the legacy MISSING faults.
+ */
+bool postcopy_use_minor_fault(RAMBlock *rb)
+{
+    return migrate_hugetlb_doublemap() && qemu_ram_is_hugetlb(rb);
+}

Are you planing using this function outside postocpy-ram.c? Otherwise if you move up its definition you can make it static and drop the header change.

Quoted 2 lines

Yes, it'll be further used in ram.c later in the patch "migration: Rework ram discard logic for hugetlb double-map" right below.

Aha.

Thanks.

author
#56 lore raw

RFC PATCH 16/21 migration: Enable doublemap with MADV_SPLIT

In reply to Peter Xu

MADV_SPLIT enables doublemap on hugetlb. Do that if doublemap=true specified for the migration.

---
 migration/postcopy-ram.c | 16 ++++++++++++++++
 migration/ram.c          | 18 ++++++++++++++++++
 2 files changed, 34 insertions(+)
migration/postcopy-ram.c +16 -0
index 86ff73c2c0..dbc7e54e4a 100644
@@ -694,6 +694,22 @@ static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
      */
     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
     if (minor_fault) {
+        /*
+         * MADV_SPLIT implicitly enables doublemap mode for hugetlb.  If
+         * that fails (e.g. on old kernels) we need to fail the migration.
+         *
+         * It's a bit late to fail here as we could have migrated lots of
+         * pages in precopy, but early failure will require us to allocate
+         * hugetlb pages secretly in QEMU which is not friendly to admins
+         * and it may affect the global hugetlb pool.  Considering it is
+         * normally always limited, keep the failure late but tolerable.
+         */
+        if (qemu_madvise(qemu_ram_get_host_addr(rb), rb->postcopy_length,
+                         QEMU_MADV_SPLIT)) {
+            error_report("%s: madvise(MADV_SPLIT) failed (ret=%d) but "
+                         "required for doublemap.", __func__, -errno);
+            return -1;
+        }
         reg_struct.mode |= UFFDIO_REGISTER_MODE_MINOR;
     }
 
migration/ram.c +18 -0
index 37d7b3553a..4d786f4b97 100644
@@ -3891,6 +3891,19 @@ static int migrate_hugetlb_doublemap_init(void)
 
     RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
         if (qemu_ram_is_hugetlb(rb)) {
+            /*
+             * MADV_SPLIT implicitly enables doublemap mode for hugetlb on
+             * the guest mapped ranges.  If that fails (e.g. on old
+             * kernels) we need to fail the migration.  Note, the
+             * host_mirror mapping below can be kept as hugely mapped.
+             */
+            if (qemu_madvise(qemu_ram_get_host_addr(rb), rb->mmap_length,
+                             QEMU_MADV_SPLIT)) {
+                error_report("%s: madvise(MADV_SPLIT) required for doublemap",
+                             __func__);
+                return -1;
+            }
+
             /*
              * Firstly, we remap the same ramblock into another range of
              * virtual address, so that we can write to the pages without
@@ -3898,6 +3911,11 @@ static int migrate_hugetlb_doublemap_init(void)
              */
             addr = ramblock_file_map(rb);
             if (addr == MAP_FAILED) {
+                /*
+                 * No need to undo MADV_SPLIT because this is dest node and
+                 * we're going to bail out anyway.  Leave that for mm exit
+                 * to clean things up.
+                 */
                 ret = -errno;
                 error_report("%s: Duplicate mapping for hugetlb ramblock '%s'"
                              "failed: %s", __func__, qemu_ram_get_idstr(rb),
Signature · 1 line
2.37.3
Reviewed-by on v1 16/21
#57 lore raw

Re: [PATCH RFC 16/21] migration: Enable doublemap with MADV_SPLIT

Quoted 8 lines · Peter Xu

MADV_SPLIT enables doublemap on hugetlb. Do that if doublemap=true specified for the migration.

---
 migration/postcopy-ram.c | 16 ++++++++++++++++
 migration/ram.c          | 18 ++++++++++++++++++
 2 files changed, 34 insertions(+)
Quoted 13 lines
migration/postcopy-ram.c +4 -0
index 86ff73c2c0..dbc7e54e4a 100644
@@ -694,6 +694,22 @@ static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
      */
     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
     if (minor_fault) {
+        /*
+         * MADV_SPLIT implicitly enables doublemap mode for hugetlb.  If
+         * that fails (e.g. on old kernels) we need to fail the migration.
+         *

migration/postcopy-ram.c static int ram_block_enable_notify(RAMBlock *rb, void *opaque) · line 710

Quoted 10 lines
migration/postcopy-ram.c +10 -0
+         * It's a bit late to fail here as we could have migrated lots of
+         * pages in precopy, but early failure will require us to allocate
+         * hugetlb pages secretly in QEMU which is not friendly to admins
+         * and it may affect the global hugetlb pool.  Considering it is
+         * normally always limited, keep the failure late but tolerable.
+         */
+        if (qemu_madvise(qemu_ram_get_host_addr(rb), rb->postcopy_length,
+                         QEMU_MADV_SPLIT)) {
+            error_report("%s: madvise(MADV_SPLIT) failed (ret=%d) but "
+                         "required for doublemap.", __func__, -errno);

Here you write errno

Quoted 13 lines
migration/postcopy-ram.c +2 -0
+            return -1;
+        }
         reg_struct.mode |= UFFDIO_REGISTER_MODE_MINOR;
     }
 
migration/ram.c +0 -0
index 37d7b3553a..4d786f4b97 100644
@@ -3891,6 +3891,19 @@ static int migrate_hugetlb_doublemap_init(void)
 
     RAMBLOCK_FOREACH_NOT_IGNORED(rb) {
         if (qemu_ram_is_hugetlb(rb)) {

migration/ram.c static int migrate_hugetlb_doublemap_init(void) · line 3903

Quoted 10 lines
migration/ram.c +10 -0
+            /*
+             * MADV_SPLIT implicitly enables doublemap mode for hugetlb on
+             * the guest mapped ranges.  If that fails (e.g. on old
+             * kernels) we need to fail the migration.  Note, the
+             * host_mirror mapping below can be kept as hugely mapped.
+             */
+            if (qemu_madvise(qemu_ram_get_host_addr(rb), rb->mmap_length,
+                             QEMU_MADV_SPLIT)) {
+                error_report("%s: madvise(MADV_SPLIT) required for doublemap",
+                             __func__);

Here you don't.

So I think you could change it.

I was thinking about creating a function for this, but as comments are different I think it is overkill.

Quoted 18 lines
migration/ram.c +8 -0
+                return -1;
+            }
+
             /*
              * Firstly, we remap the same ramblock into another range of
              * virtual address, so that we can write to the pages without
@@ -3898,6 +3911,11 @@ static int migrate_hugetlb_doublemap_init(void)
              */
             addr = ramblock_file_map(rb);
             if (addr == MAP_FAILED) {
+                /*
+                 * No need to undo MADV_SPLIT because this is dest node and
+                 * we're going to bail out anyway.  Leave that for mm exit
+                 * to clean things up.
+                 */
                 ret = -errno;
                 error_report("%s: Duplicate mapping for hugetlb ramblock '%s'"
                              "failed: %s", __func__, qemu_ram_get_idstr(rb),
author
#58 lore raw

RFC PATCH 17/21 migration: Rework ram discard logic for hugetlb double-map

In reply to Peter Xu

Hugetlb double map will make the ram discard logic different.

The whole idea will still be the same: we need to a bitmap sync between src/dst before we switch to postcopy.

When discarding a range, we only erase the pgtables that were used to be mapped for the guest leveraging the semantics of MADV_DONTNEED on Linux. This guarantees us that when a guest access triggered we'll receive a MINOR fault message rather than a MISSING fault message.

---
 include/exec/cpu-common.h |  1 +
 migration/ram.c           | 16 +++++++++++++++-
 migration/trace-events    |  1 +
 softmmu/physmem.c         | 31 +++++++++++++++++++++++++++++++
 4 files changed, 48 insertions(+), 1 deletion(-)
include/exec/cpu-common.h +1 -0
index 4c394ccdfc..09378c6ada 100644
@@ -155,6 +155,7 @@ typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
 
 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
 int ram_block_discard_range(RAMBlock *rb, uint64_t start, size_t length);
+int ram_block_zap_range(RAMBlock *rb, uint64_t start, size_t length);
 
 #endif
 
migration/ram.c +15 -1
index 4d786f4b97..4da56d925c 100644
@@ -2770,6 +2770,12 @@ static void postcopy_each_ram_send_discard(MigrationState *ms)
          * host-page size chunks, mark any partially dirty host-page size
          * chunks as all dirty.  In this case the host-page is the host-page
          * for the particular RAMBlock, i.e. it might be a huge page.
+         *
+         * Note: we need to do huge page truncation when double-map is
+         * enabled too, _only_ because we use MADV_DONTNEED to drop
+         * pgtables on dest QEMU, and it (at least so far...) does not
+         * support dropping partial of the hugetlb pgtables.  If it can one
+         * day, we can skip this "chunk" operation as further optimization.
          */
         postcopy_chunk_hostpages_pass(ms, block);
 
@@ -2913,7 +2919,15 @@ int ram_discard_range(const char *rbname, uint64_t start, size_t length)
                      length >> qemu_target_page_bits());
     }
 
-    return ram_block_discard_range(rb, start, length);
+    if (postcopy_use_minor_fault(rb)) {
+        /*
+         * We need to keep the page cache exist, so as to trigger MINOR
+         * faults for every future page accesses on old pages.
+         */
+        return ram_block_zap_range(rb, start, length);
+    } else {
+        return ram_block_discard_range(rb, start, length);
+    }
 }
 
 /*
migration/trace-events +1 -0
index 57003edcbd..6b418a0e9e 100644
@@ -92,6 +92,7 @@ migration_bitmap_sync_end(uint64_t dirty_pages) "dirty_pages %" PRIu64
 migration_bitmap_clear_dirty(char *str, uint64_t start, uint64_t size, unsigned long page) "rb %s start 0x%"PRIx64" size 0x%"PRIx64" page 0x%lx"
 migration_throttle(void) ""
 ram_discard_range(const char *rbname, uint64_t start, size_t len) "%s: start: %" PRIx64 " %zx"
+postcopy_discard_range(const char *rbname, uint64_t start, void *host, size_t len) "%s: start=%" PRIx64 " haddr=%p len=%zx"
 ram_load_loop(const char *rbname, uint64_t addr, int flags, void *host) "%s: addr: 0x%" PRIx64 " flags: 0x%x host: %p"
 ram_load_postcopy_loop(int channel, uint64_t addr, int flags) "chan=%d addr=0x%" PRIx64 " flags=0x%x"
 ram_postcopy_send_discard_bitmap(void) ""
softmmu/physmem.c +31 -0
index 536c204811..12c0bc9aee 100644
@@ -3567,6 +3567,37 @@ int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
     return ret;
 }
 
+/*
+ * Zap page tables for specified range.  Only applicable for file-backed
+ * memory.  We're relying on Linux's MADV_DONTNEED behavior here for
+ * zapping the pgtables, it may or may not work on other OSes.  Before we
+ * know that, fail them.
+ */
+int ram_block_zap_range(RAMBlock *rb, uint64_t start, size_t length)
+{
+#ifdef CONFIG_LINUX
+    uint8_t *host_addr = rb->host + start;
+    int ret;
+
+    if (rb->fd == -1) {
+        /* The zap magic only works with file-backed */
+        return -EINVAL;
+    }
+
+    ret = madvise(host_addr, length, MADV_DONTNEED);
+    if (ret) {
+        ret = -errno;
+        error_report("%s: Failed to zap ramblock start=0x%"PRIx64
+                     " addr=0x%"PRIx64" length=0x%zx", __func__,
+                     start, (uint64_t)host_addr, length);
+    }
+
+    return ret;
+#else
+    return -EINVAL;
+#endif
+}
+
 /*
  * Unmap pages of memory from start to start+length such that
  * they a) read as 0, b) Trigger whatever fault mechanism
Signature · 1 line
2.37.3
Reviewed-by on v1 17/21
#59 lore raw

Re: [PATCH RFC 17/21] migration: Rework ram discard logic for hugetlb double-map

Quoted 11 lines · Peter Xu

Hugetlb double map will make the ram discard logic different.

The whole idea will still be the same: we need to a bitmap sync between src/dst before we switch to postcopy.

When discarding a range, we only erase the pgtables that were used to be mapped for the guest leveraging the semantics of MADV_DONTNEED on Linux. This guarantees us that when a guest access triggered we'll receive a MINOR fault message rather than a MISSING fault message.

migration/ram.c int ram_discard_range(const char *rbname, uint64_t start, size_t length) · line 2930

Quoted 13 lines
                      length >> qemu_target_page_bits());
     }
migration/ram.c +9 -1
-    return ram_block_discard_range(rb, start, length);
+    if (postcopy_use_minor_fault(rb)) {
+        /*
+         * We need to keep the page cache exist, so as to trigger MINOR
+         * faults for every future page accesses on old pages.
+         */
+        return ram_block_zap_range(rb, start, length);
+    } else {
+        return ram_block_discard_range(rb, start, length);
+    }

This is a question of style, so take it or leave as it as you wish.

You can change:

if (foo) {
    return X;
} else {
    return Y;
}

into:

if (foo) {
    return X;
}

return Y;

It is one line less of code, and in my eyes, makes it easier to see that one exits in all cases. But as said, this is a question of taste, and that is as personal as it gets.

Later, Juan.

author
#60 lore raw

RFC PATCH 18/21 migration: Allow postcopy_register_shared_ufd() to fail

In reply to Peter Xu

Let's fail double-map for vhost-user and any potential users that can have a remote userfaultfd for now.

---
 hw/virtio/vhost-user.c   | 9 ++++++++-
 migration/postcopy-ram.c | 9 +++++++--
 migration/postcopy-ram.h | 4 ++--
 3 files changed, 17 insertions(+), 5 deletions(-)
hw/virtio/vhost-user.c +8 -1
index d9ce0501b2..00351bd67a 100644
@@ -1952,7 +1952,14 @@ static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp)
     u->postcopy_fd.handler = vhost_user_postcopy_fault_handler;
     u->postcopy_fd.waker = vhost_user_postcopy_waker;
     u->postcopy_fd.idstr = "vhost-user"; /* Need to find unique name */
-    postcopy_register_shared_ufd(&u->postcopy_fd);
+
+    ret = postcopy_register_shared_ufd(&u->postcopy_fd);
+    if (ret) {
+        error_setg(errp, "%s: Register of shared userfaultfd failed: %s",
+                   __func__, strerror(ret));
+        return ret;
+    }
+
     return 0;
 #else
     error_setg(errp, "Postcopy not supported on non-Linux systems");
migration/postcopy-ram.c +7 -2
index dbc7e54e4a..0cfe5174a5 100644
@@ -1582,14 +1582,19 @@ PostcopyState postcopy_state_set(PostcopyState new_state)
 }
 
 /* Register a handler for external shared memory postcopy
- * called on the destination.
+ * called on the destination.  Returns 0 if success, <0 for err.
  */
-void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
+int postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
 {
     MigrationIncomingState *mis = migration_incoming_get_current();
 
+    if (migrate_hugetlb_doublemap()) {
+        return -EINVAL;
+    }
+
     mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
                                                   *pcfd);
+    return 0;
 }
 
 /* Unregister a handler for external shared memory postcopy
migration/postcopy-ram.h +2 -2
index 32734d2340..94adad6fb8 100644
@@ -161,9 +161,9 @@ struct PostCopyFD {
 };
 
 /* Register a userfaultfd owned by an external process for
- * shared memory.
+ * shared memory.  Returns 0 if succeeded, <0 if error.
  */
-void postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
+int postcopy_register_shared_ufd(struct PostCopyFD *pcfd);
 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd);
 /* Call each of the shared 'waker's registered telling them of
  * availability of a block.
Signature · 1 line
2.37.3
Reviewed-by on v1 18/21
#61 lore raw

Re: [PATCH RFC 18/21] migration: Allow postcopy_register_shared_ufd() to fail

Quoted 4 lines · Peter Xu

Let's fail double-map for vhost-user and any potential users that can have a remote userfaultfd for now.

But

migration/postcopy-ram.c PostcopyState postcopy_state_set(PostcopyState new_state) · line 1592

Quoted 7 lines
migration/postcopy-ram.c +3 -1
-void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
+int postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
 {
     MigrationIncomingState *mis = migration_incoming_get_current();
 
+    if (migrate_hugetlb_doublemap()) {
+        return -EINVAL;

I am not sure that -EINVAL is the best answer here. There is not a problem with the value. The problem is that both features together don't work.

As an alternative:

ENOSYS 38 Function not implemented

But I am not sure that this is much better :-(

Later, Juan.

author
#62 lore raw

RFC PATCH 19/21 migration: Add postcopy_mark_received()

In reply to Peter Xu

We have a few maintainance work to do after we UFFDIO_[ZERO]COPY a page before, e.g. on requested list of pages or when measuring page latencies.

Move those steps into a separate function so that it can be easily reused when we're going to support UFFDIO_CONTINUE.

---
 migration/postcopy-ram.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)
migration/postcopy-ram.c +21 -14
index 0cfe5174a5..8a2259581e 100644
@@ -1288,6 +1288,25 @@ int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
     return 0;
 }
 
+static void
+postcopy_mark_received(MigrationIncomingState *mis, RAMBlock *rb,
+                       void *host_addr, size_t npages)
+{
+        qemu_mutex_lock(&mis->page_request_mutex);
+        ramblock_recv_bitmap_set_range(rb, host_addr, npages);
+        /*
+         * If this page resolves a page fault for a previous recorded faulted
+         * address, take a special note to maintain the requested page list.
+         */
+        if (g_tree_lookup(mis->page_requested, host_addr)) {
+            g_tree_remove(mis->page_requested, host_addr);
+            mis->page_requested_count--;
+            trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
+        }
+        qemu_mutex_unlock(&mis->page_request_mutex);
+        mark_postcopy_blocktime_end((uintptr_t)host_addr);
+}
+
 static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
                                void *from_addr, uint64_t pagesize, RAMBlock *rb)
 {
@@ -1309,20 +1328,8 @@ static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
         ret = ioctl(userfault_fd, UFFDIO_ZEROPAGE, &zero_struct);
     }
     if (!ret) {
-        qemu_mutex_lock(&mis->page_request_mutex);
-        ramblock_recv_bitmap_set_range(rb, host_addr,
-                                       pagesize / qemu_target_page_size());
-        /*
-         * If this page resolves a page fault for a previous recorded faulted
-         * address, take a special note to maintain the requested page list.
-         */
-        if (g_tree_lookup(mis->page_requested, host_addr)) {
-            g_tree_remove(mis->page_requested, host_addr);
-            mis->page_requested_count--;
-            trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
-        }
-        qemu_mutex_unlock(&mis->page_request_mutex);
-        mark_postcopy_blocktime_end((uintptr_t)host_addr);
+        postcopy_mark_received(mis, rb, host_addr,
+                               pagesize / qemu_target_page_size());
     }
     return ret;
 }
Signature · 1 line
2.37.3
Reviewed-by on v1 19/21
#63 lore raw

Re: [PATCH RFC 19/21] migration: Add postcopy_mark_received()

Quoted 7 lines · Peter Xu

We have a few maintainance work to do after we UFFDIO_[ZERO]COPY a page before, e.g. on requested list of pages or when measuring page latencies.

Move those steps into a separate function so that it can be easily reused when we're going to support UFFDIO_CONTINUE.

author
#64 lore raw

RFC PATCH 20/21 migration: Handle page faults using UFFDIO_CONTINUE

In reply to Peter Xu

Teach QEMU to be able to handle page faults using UFFDIO_CONTINUE for hugetlbfs double mapped ranges.

To copy the data, we need to use the mirror buffer created per ramblock by a raw memcpy(), then we can kick the faulted threads using UFFDIO_CONTINUE by installing the pgtables.

Move trace_postcopy_place_page(host) upper so that it'll dump something for either UFFDIO_COPY or UFFDIO_CONTINUE.

---
 migration/postcopy-ram.c | 55 ++++++++++++++++++++++++++++++++++++++--
 migration/trace-events   |  4 +--
 2 files changed, 55 insertions(+), 4 deletions(-)
migration/postcopy-ram.c +53 -2
index 8a2259581e..c4bd338e22 100644
@@ -1350,6 +1350,43 @@ int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
     return 0;
 }
 
+/* Returns the mirror_host addr for a specific host address in ramblock */
+static inline void *migration_ram_get_mirror_addr(RAMBlock *rb, void *host)
+{
+    return (void *)((__u64)rb->host_mirror + ((__u64)host - (__u64)rb->host));
+}
+
+static int
+qemu_uffd_continue(MigrationIncomingState *mis, RAMBlock *rb, void *host,
+                   void *from)
+{
+    void *mirror_addr = migration_ram_get_mirror_addr(rb, host);
+    /* Doublemap uses small host page size */
+    uint64_t psize = qemu_real_host_page_size();
+    struct uffdio_continue req;
+
+    /*
+     * Copy data first into the mirror host pointer; we can't directly copy
+     * data into rb->host because otherwise our thread will get trapped too.
+     */
+    memcpy(mirror_addr, from, psize);
+
+    /* Kick off the faluted threads to fetch data from the page cache */
+    req.range.start = (__u64)host;
+    req.range.len = psize;
+    req.mode = 0;
+	if (ioctl(mis->userfault_fd, UFFDIO_CONTINUE, &req)) {
+        error_report("%s: UFFDIO_CONTINUE failed for start=%p"
+                     " len=0x%"PRIx64": %s\n", __func__, host,
+                     psize, strerror(-req.mapped));
+        return req.mapped;
+    }
+
+    postcopy_mark_received(mis, rb, host, psize / qemu_target_page_size());
+
+    return 0;
+}
+
 /*
  * Place a host page (from) at (host) atomically
  * returns 0 on success
@@ -1359,6 +1396,18 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
 {
     size_t pagesize = migration_ram_pagesize(rb);
 
+    trace_postcopy_place_page(rb->idstr, (uint8_t *)host - rb->host, host);
+
+    if (postcopy_use_minor_fault(rb)) {
+        /*
+         * If minor fault used, we use UFFDIO_CONTINUE instead.
+         *
+         * TODO: support shared uffds (e.g. vhost-user). Currently we're
+         * skipping them.
+         */
+        return qemu_uffd_continue(mis, rb, host, from);
+    }
+
     /* copy also acks to the kernel waking the stalled thread up
      * TODO: We can inhibit that ack and only do it if it was requested
      * which would be slightly cheaper, but we'd have to be careful
@@ -1372,7 +1421,6 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
         return -e;
     }
 
-    trace_postcopy_place_page(host);
     return postcopy_notify_shared_wake(rb,
                                        qemu_ram_block_host_offset(rb, host));
 }
@@ -1385,10 +1433,13 @@ int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
                              RAMBlock *rb)
 {
     size_t pagesize = migration_ram_pagesize(rb);
-    trace_postcopy_place_page_zero(host);
+    trace_postcopy_place_page_zero(rb->idstr, (uint8_t *)host - rb->host, host);
 
     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
      * but it's not available for everything (e.g. hugetlbpages)
+     *
+     * NOTE: when hugetlb double-map enabled, then this ramblock will never
+     * have RAM_UF_ZEROPAGE, so it'll always go to postcopy_place_page().
      */
     if (qemu_ram_is_uf_zeroable(rb)) {
         if (qemu_ufd_copy_ioctl(mis, host, NULL, pagesize, rb)) {
migration/trace-events +2 -2
index 6b418a0e9e..7baf235d22 100644
@@ -265,8 +265,8 @@ postcopy_discard_send_range(const char *ramblock, unsigned long start, unsigned
 postcopy_cleanup_range(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=0x%zx length=0x%zx"
 postcopy_init_range(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=0x%zx length=0x%zx"
 postcopy_nhp_range(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=0x%zx length=0x%zx"
-postcopy_place_page(void *host_addr) "host=%p"
-postcopy_place_page_zero(void *host_addr) "host=%p"
+postcopy_place_page(const char *id, size_t offset, void *host_addr) "id=%s offset=0x%zx host=%p"
+postcopy_place_page_zero(const char *id, size_t offset, void *host_addr) "id=%s offset=0x%zx host=%p"
 postcopy_ram_enable_notify(void) ""
 mark_postcopy_blocktime_begin(uint64_t addr, void *dd, uint32_t time, int cpu, int received) "addr: 0x%" PRIx64 ", dd: %p, time: %u, cpu: %d, already_received: %d"
 mark_postcopy_blocktime_end(uint64_t addr, void *dd, uint32_t time, int affected_cpu) "addr: 0x%" PRIx64 ", dd: %p, time: %u, affected_cpu: %d"
Signature · 1 line
2.37.3
#65 lore raw

Re: [PATCH RFC 20/21] migration: Handle page faults using UFFDIO_CONTINUE

Quoted 22 lines · Peter Xu

Teach QEMU to be able to handle page faults using UFFDIO_CONTINUE for hugetlbfs double mapped ranges.

To copy the data, we need to use the mirror buffer created per ramblock by a raw memcpy(), then we can kick the faulted threads using UFFDIO_CONTINUE by installing the pgtables.

Move trace_postcopy_place_page(host) upper so that it'll dump something for either UFFDIO_COPY or UFFDIO_CONTINUE.

---
 migration/postcopy-ram.c | 55 ++++++++++++++++++++++++++++++++++++++--
 migration/trace-events   |  4 +--
 2 files changed, 55 insertions(+), 4 deletions(-)
migration/postcopy-ram.c +0 -0
index 8a2259581e..c4bd338e22 100644

migration/postcopy-ram.c int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset) · line 1356

Quoted 7 lines
migration/postcopy-ram.c +4 -0
@@ -1350,6 +1350,43 @@ int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
     return 0;
 }
 
+/* Returns the mirror_host addr for a specific host address in ramblock */
+static inline void *migration_ram_get_mirror_addr(RAMBlock *rb, void *host)
+{
+    return (void *)((__u64)rb->host_mirror + ((__u64)host - (__u64)rb->host));

This is gross :-(
I hate this C miss-feature.

What about:

    return (char *)rb->host_mirror + (char*)host - (char*)rb->host;

But I don't know if it (much) clearer. And no, I don't remember if we ever need more parents.

gcc used to do "the right" thing on void * arithmetic, but it is not in the standard, and I don't know what is worse.

Quoted 8 lines
migration/postcopy-ram.c +8 -0
+}
+
+static int
+qemu_uffd_continue(MigrationIncomingState *mis, RAMBlock *rb, void *host,
+                   void *from)
+{
+    void *mirror_addr = migration_ram_get_mirror_addr(rb, host);
+    /* Doublemap uses small host page size */

migration/postcopy-ram.c int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)

Quoted 10 lines
migration/postcopy-ram.c +10 -0
+    uint64_t psize = qemu_real_host_page_size();
+    struct uffdio_continue req;
+
+    /*
+     * Copy data first into the mirror host pointer; we can't directly copy
+     * data into rb->host because otherwise our thread will get trapped too.
+     */
+    memcpy(mirror_addr, from, psize);
+
+    /* Kick off the faluted threads to fetch data from the page cache
                       ^^^^^^^

migration/postcopy-ram.c int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset) · line 1375

Quoted 1 line
migration/postcopy-ram.c +0 -0
 */

Faulted

Quoted 46 lines
migration/postcopy-ram.c +15 -0
+    req.range.start = (__u64)host;
+    req.range.len = psize;
+    req.mode = 0;
+	if (ioctl(mis->userfault_fd, UFFDIO_CONTINUE, &req)) {
+        error_report("%s: UFFDIO_CONTINUE failed for start=%p"
+                     " len=0x%"PRIx64": %s\n", __func__, host,
+                     psize, strerror(-req.mapped));
+        return req.mapped;
+    }
+
+    postcopy_mark_received(mis, rb, host, psize / qemu_target_page_size());
+
+    return 0;
+}
+
 /*
  * Place a host page (from) at (host) atomically

* returns 0 on success

migration/postcopy-ram.c +12 -1
@@ -1359,6 +1396,18 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
 {
     size_t pagesize = migration_ram_pagesize(rb);
 
+    trace_postcopy_place_page(rb->idstr, (uint8_t *)host - rb->host, host);
+
+    if (postcopy_use_minor_fault(rb)) {
+        /*
+         * If minor fault used, we use UFFDIO_CONTINUE instead.
+         *
+         * TODO: support shared uffds (e.g. vhost-user). Currently we're
+         * skipping them.
+         */
+        return qemu_uffd_continue(mis, rb, host, from);
+    }
+
     /* copy also acks to the kernel waking the stalled thread up
      * TODO: We can inhibit that ack and only do it if it was requested
      * which would be slightly cheaper, but we'd have to be careful
@@ -1372,7 +1421,6 @@ int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
         return -e;
     }
 
-    trace_postcopy_place_page(host);
     return postcopy_notify_shared_wake(rb,
                                        qemu_ram_block_host_offset(rb, host));
 }

migration/postcopy-ram.c int postcopy_place_page_zero(MigrationIncomingState *mis, void *host, · line 1436

Quoted 5 lines
migration/postcopy-ram.c +1 -1
@@ -1385,10 +1433,13 @@ int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
                              RAMBlock *rb)
 {
     size_t pagesize = migration_ram_pagesize(rb);
-    trace_postcopy_place_page_zero(host);
+    trace_postcopy_place_page_zero(rb->idstr, (uint8_t *)host - rb->host, host);

It is me, or to be standard compliant, you need to cast also rb->host?

Quoted 13 lines
migration/postcopy-ram.c +3 -0
     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
      * but it's not available for everything (e.g. hugetlbpages)
+     *
+     * NOTE: when hugetlb double-map enabled, then this ramblock will never
+     * have RAM_UF_ZEROPAGE, so it'll always go to postcopy_place_page().
      */
     if (qemu_ram_is_uf_zeroable(rb)) {
         if (qemu_ufd_copy_ioctl(mis, host, NULL, pagesize, rb)) {
migration/trace-events +0 -0
index 6b418a0e9e..7baf235d22 100644

migration/trace-events postcopy_discard_send_range(const char *ramblock, unsigned long start, unsigned · line 272

Quoted 10 lines
migration/trace-events +2 -2
@@ -265,8 +265,8 @@ postcopy_discard_send_range(const char *ramblock, unsigned long start, unsigned
 postcopy_cleanup_range(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=0x%zx length=0x%zx"
 postcopy_init_range(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=0x%zx length=0x%zx"
 postcopy_nhp_range(const char *ramblock, void *host_addr, size_t offset, size_t length) "%s: %p offset=0x%zx length=0x%zx"
-postcopy_place_page(void *host_addr) "host=%p"
-postcopy_place_page_zero(void *host_addr) "host=%p"
+postcopy_place_page(const char *id, size_t offset, void *host_addr) "id=%s offset=0x%zx host=%p"
+postcopy_place_page_zero(const char *id, size_t offset, void *host_addr) "id=%s offset=0x%zx host=%p"
 postcopy_ram_enable_notify(void) ""
 mark_postcopy_blocktime_begin(uint64_t addr, void *dd, uint32_t time, int cpu, int received) "addr: 0x%" PRIx64 ", dd: %p, time: %u, cpu: %d, already_received: %d"
 mark_postcopy_blocktime_end(uint64_t addr, void *dd, uint32_t time, int affected_cpu) "addr: 0x%" PRIx64 ", dd: %p, time: %u, affected_cpu: %d"

I think that you can split the part of the patch that changes the traces. But again, it is up to you.

#66 lore raw

Re: [PATCH RFC 20/21] migration: Handle page faults using UFFDIO_CONTINUE

Quoted 34 lines · Juan Quintela
Quoted 22 lines · Peter Xu

Teach QEMU to be able to handle page faults using UFFDIO_CONTINUE for hugetlbfs double mapped ranges.

To copy the data, we need to use the mirror buffer created per ramblock by a raw memcpy(), then we can kick the faulted threads using UFFDIO_CONTINUE by installing the pgtables.

Move trace_postcopy_place_page(host) upper so that it'll dump something for either UFFDIO_COPY or UFFDIO_CONTINUE.

---
 migration/postcopy-ram.c | 55 ++++++++++++++++++++++++++++++++++++++--
 migration/trace-events   |  4 +--
 2 files changed, 55 insertions(+), 4 deletions(-)
migration/postcopy-ram.c +0 -0
index 8a2259581e..c4bd338e22 100644

migration/postcopy-ram.c int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset) · line 1356

Quoted 7 lines
migration/postcopy-ram.c +4 -0
@@ -1350,6 +1350,43 @@ int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
     return 0;
 }
 
+/* Returns the mirror_host addr for a specific host address in ramblock */
+static inline void *migration_ram_get_mirror_addr(RAMBlock *rb, void *host)
+{
+    return (void *)((__u64)rb->host_mirror + ((__u64)host - (__u64)rb->host));

This is gross :-(
I hate this C miss-feature.

Quoted 2 lines

What about:

    return (char *)rb->host_mirror + (char*)host - (char*)rb->host;

This was a generic suggestion. But after looking at ramblock.h and realizing that rb->host is not void*.

    return (uint8_t *)rb->host_mirror + (uint8_t *)host - rb->host;

Sorry for looking too late.

BTW, once here, why is the type of host_mirror different than the one from host? I don't know what is more confusing anymore.

Later, Juan.

author
#67 lore raw

RFC PATCH 21/21 migration: Collapse huge pages again after postcopy finished

In reply to Peter Xu

When hugetlb-doublemap enabled, the pages will be migrated in small page sizes during postcopy. When the migration finishes, the pgtable needs to be rebuilt explicitly for these ranges to have huge page being mapped again.

---
 migration/ram.c        | 31 +++++++++++++++++++++++++++++++
 migration/trace-events |  1 +
 2 files changed, 32 insertions(+)
migration/ram.c +31 -0
index 4da56d925c..178739f8c3 100644
@@ -3986,6 +3986,31 @@ static int ram_load_setup(QEMUFile *f, void *opaque)
     return 0;
 }
 
+#define  MADV_COLLAPSE_CHUNK_SIZE  (1UL << 30) /* 1G */
+
+static void ramblock_rebuild_huge_mappings(RAMBlock *rb)
+{
+    unsigned long addr, size;
+
+    assert(qemu_ram_is_hugetlb(rb));
+
+    addr = (unsigned long)qemu_ram_get_host_addr(rb);
+    size = rb->mmap_length;
+
+    while (size) {
+        unsigned long chunk = MIN(size, MADV_COLLAPSE_CHUNK_SIZE);
+
+        if (qemu_madvise((void *)addr, chunk, QEMU_MADV_COLLAPSE)) {
+            error_report("%s: madvise(MADV_COLLAPSE) failed "
+                         "for ramblock '%s'", __func__, rb->idstr);
+        } else {
+            trace_ramblock_rebuild_huge_mappings(rb->idstr, addr, chunk);
+        }
+        addr += chunk;
+        size -= chunk;
+    }
+}
+
 static int ram_load_cleanup(void *opaque)
 {
     RAMBlock *rb;
@@ -4001,6 +4026,12 @@ static int ram_load_cleanup(void *opaque)
         g_free(rb->receivedmap);
         rb->receivedmap = NULL;
         if (rb->host_mirror) {
+            /*
+             * If host_mirror set, it means this is an hugetlb ramblock,
+             * and we've enabled double mappings for it.  Rebuild the huge
+             * page tables here.
+             */
+            ramblock_rebuild_huge_mappings(rb);
             munmap(rb->host_mirror, rb->mmap_length);
             rb->host_mirror = NULL;
         }
migration/trace-events +1 -0
index 7baf235d22..6b52bb691c 100644
@@ -119,6 +119,7 @@ postcopy_preempt_hit(char *str, uint64_t offset) "ramblock %s offset 0x%"PRIx64
 postcopy_preempt_send_host_page(char *str, uint64_t offset) "ramblock %s offset 0x%"PRIx64
 postcopy_preempt_switch_channel(int channel) "%d"
 postcopy_preempt_reset_channel(void) ""
+ramblock_rebuild_huge_mappings(char *str, unsigned long start, unsigned long size) "ramblock %s start 0x%lx size 0x%lx"
 
 # multifd.c
 multifd_new_send_channel_async(uint8_t id) "channel %u"
Signature · 1 line
2.37.3
#68 lore raw

Re: [PATCH RFC 21/21] migration: Collapse huge pages again after postcopy finished

Quoted 15 lines · Peter Xu

When hugetlb-doublemap enabled, the pages will be migrated in small page sizes during postcopy. When the migration finishes, the pgtable needs to be rebuilt explicitly for these ranges to have huge page being mapped again.

---
 migration/ram.c        | 31 +++++++++++++++++++++++++++++++
 migration/trace-events |  1 +
 2 files changed, 32 insertions(+)
migration/ram.c +0 -0
index 4da56d925c..178739f8c3 100644

migration/ram.c static int ram_load_setup(QEMUFile *f, void *opaque) · line 3993

Quoted 8 lines
migration/ram.c +5 -0
@@ -3986,6 +3986,31 @@ static int ram_load_setup(QEMUFile *f, void *opaque)
     return 0;
 }
 
+#define  MADV_COLLAPSE_CHUNK_SIZE  (1UL << 30) /* 1G */
+
+static void ramblock_rebuild_huge_mappings(RAMBlock *rb)
+{
+    unsigned long addr, size;

This makes my head explode.

We have:

unsigned long
__u64
uint64_t

Used and mixed all around.

migration/ram.c static int ram_load_setup(QEMUFile *f, void *opaque) · line 3997

Quoted 3 lines
migration/ram.c +3 -0
+    assert(qemu_ram_is_hugetlb(rb));
+
+    addr = (unsigned long)qemu_ram_get_host_addr(rb);

Don't this cast should be uintptr_t?
At least on win64 it should fail, no?

migration/ram.c static int ram_load_setup(QEMUFile *f, void *opaque) · line 3998

Quoted 1 line
migration/ram.c +1 -0
+    size = rb->mmap_length;

this is ram_addr_t. It is uint64_t except with xen. So it should fail on any 32 bit host.

Later, Juan.