KVM: Dirty ring support (QEMU part)
v4 cover KVM: Dirty ring support (QEMU part)
This is v4 of the qemu dirty ring interface support.
It is merely the same as v3 content-wise, but there're a few things to mention besides the rebase itself:
- I picked up two patches from Eric Farman for the linux-header updates (from Eric's v3 series) for convenience just in case any of the series would got queued by any maintainer.
- One more patch is added as "KVM: Disable manual dirty log when dirty ring enabled". I found this when testing the branch after rebasing to latest qemu, that not only the manual dirty log capability is not needed for kvm dirty ring, but more importantly INITIALLY_ALL_SET is totally against kvm dirty ring and it could silently crash the guest after migration. For this new commit, I touched up "KVM: Add dirty-gfn-count property" a bit.
- A few more documentation lines in qemu-options.hx.
- I removed the RFC tag after kernel series got merged.
Again, this is only the 1st step to support dirty ring. Ideally dirty ring should grant QEMU the possibility to remove the whole layered dirty bitmap so that dirty ring will work similarly as auto-converge enabled but should better; we will just throttle vcpus with the dirty ring kvm exit rather than explicitly adding a timer to stop the vcpu thread from entering the guest again (like what we did with current migration auto-converge). Some more information could also be found in the kvm forum 2020 talk regarding kvm dirty ring (slides 21/22 [1]).
That next step (to remove all the dirty bitmaps, as mentioned above) is still
discussable: firstly I don't know whether there's anything I've overlooked in there. Meanwhile that's also only services huge VM cases, may not be extremely helpful with a lot major scenarios where VMs are not that huge.
There's probably other ways to fix huge VM migration issues, majorly focusing on responsiveness and convergence. For example, Google has proposed some new userfaultfd kernel capability called "minor modes" [2] to track page minor faults and that could be finally served for that purpose too using postcopy. That's another long story so I'll stop here, but just as a marker along with the dirty ring series so there'll still be a record to reference.
Said that, I still think this series is very worth merging even if we don't persue the next steps yet, since dirty ring is disabled by default, and we can always work upon this series.
Please review, thanks.
V3: https://lore.kernel.org/qemu-devel/[email protected]/
(V3 contains all the pre-v3 changelog)
QEMU branch for testing (requires kernel version 5.11-rc1+):
https://github.com/xzpeter/qemu/tree/kvm-dirty-ring
[1] https://static.sched.com/hosted_files/kvmforum2020/97/kvm_dirty_ring_peter.pdf
[2] https://lore.kernel.org/lkml/[email protected]/
---------------------------8<---------------------------------
Overview
========
KVM dirty ring is a new interface to pass over dirty bits from kernel to the userspace. Instead of using a bitmap for each memory region, the dirty ring contains an array of dirtied GPAs to fetch, one ring per vcpu.
There're a few major changes comparing to how the old dirty logging interface would work:
- Granularity of dirty bits
KVM dirty ring interface does not offer memory region level granularity to collect dirty bits (i.e., per KVM memory slot). Instead the dirty bit is collected globally for all the vcpus at once. The major effect is on VGA part because VGA dirty tracking is enabled as long as the device is created, also it was in memory region granularity. Now that operation will be amplified to a VM sync. Maybe there's smarter way to do the same thing in VGA with the new interface, but so far I don't see it affects much at least on regular VMs.
- Collection of dirty bits
The old dirty logging interface collects KVM dirty bits when synchronizing dirty bits. KVM dirty ring interface instead used a standalone thread to do that. So when the other thread (e.g., the migration thread) wants to synchronize the dirty bits, it simply kick the thread and wait until it flushes all the dirty bits to the ramblock dirty bitmap.
A new parameter "dirty-ring-size" is added to "-accel kvm". By default, dirty ring is still disabled (size==0). To enable it, we need to be with:
-accel kvm,dirty-ring-size=65536
This establishes a 64K dirty ring buffer per vcpu. Then if we migrate, it'll switch to dirty ring.
I gave it a shot with a 24G guest, 8 vcpus, using 10g NIC as migration channel. When idle or dirty workload small, I don't observe major difference on total migration time. When with higher random dirty workload (800MB/s dirty rate upon 20G memory, worse for kvm dirty ring). Total migration time is (ping pong migrate for 6 times, in seconds):
|-------------------------+---------------| | dirty ring (4k entries) | dirty logging | |-------------------------+---------------| | 70 | 58 | | 78 | 70 | | 72 | 48 | | 74 | 52 | | 83 | 49 | | 65 | 54 | |-------------------------+---------------|
Summary:
dirty ring average: 73s dirty logging average: 55s
The KVM dirty ring will be slower in above case. The number may show that the dirty logging is still preferred as a default value because small/medium VMs are still major cases, and high dirty workload happens frequently too. And that's what this series did.
TODO:
- Consider to drop the BQL dependency: then we can run the reaper thread in parallel of main thread. Needs some thought around the race conditions.
- Consider to drop the kvmslot bitmap: logically this can be dropped with kvm dirty ring, not only for space saving, but also it's still another layer linear to guest mem size which is against the whole idea of kvm dirty ring. This should make above number (of kvm dirty ring) even smaller (but still may not be as good as dirty logging when with such high workload).
Please refer to the code and comment itself for more information.
Thanks,
Eric Farman (2):
update-linux-headers: Include const.h
Update linux headers to 5.11-rc2
Peter Xu (11):
memory: Introduce log_sync_global() to memory listener
KVM: Fixup kvm_log_clear_one_slot() ioctl return check
KVM: Use a big lock to replace per-kml slots_lock
KVM: Create the KVMSlot dirty bitmap on flag changes
KVM: Provide helper to get kvm dirty log
KVM: Provide helper to sync dirty bitmap from slot to ramblock
KVM: Simplify dirty log sync in kvm_set_phys_mem
KVM: Cache kvm slot dirty bitmap size
KVM: Add dirty-gfn-count property
KVM: Disable manual dirty log when dirty ring enabled
KVM: Dirty ring support
accel/kvm/kvm-all.c | 577 +++++++++++++++--- accel/kvm/trace-events | 7 + include/exec/memory.h | 12 + include/hw/core/cpu.h | 8 + .../infiniband/hw/vmw_pvrdma/pvrdma_ring.h | 14 +- .../infiniband/hw/vmw_pvrdma/pvrdma_verbs.h | 2 +- include/standard-headers/drm/drm_fourcc.h | 175 +++++- include/standard-headers/linux/const.h | 36 ++ include/standard-headers/linux/ethtool.h | 2 +- include/standard-headers/linux/fuse.h | 30 +- include/standard-headers/linux/kernel.h | 9 +- include/standard-headers/linux/pci_regs.h | 16 + include/standard-headers/linux/vhost_types.h | 9 + include/standard-headers/linux/virtio_gpu.h | 82 +++ include/standard-headers/linux/virtio_ids.h | 44 +- include/sysemu/kvm_int.h | 7 +- linux-headers/asm-arm64/kvm.h | 3 - linux-headers/asm-generic/unistd.h | 6 +- linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 1 + linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 1 + linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/kvm.h | 56 +- linux-headers/linux/userfaultfd.h | 9 + linux-headers/linux/vfio.h | 1 + linux-headers/linux/vhost.h | 4 + qemu-options.hx | 12 + scripts/update-linux-headers.sh | 5 +- softmmu/memory.c | 33 +- 36 files changed, 1022 insertions(+), 148 deletions(-) create mode 100644 include/standard-headers/linux/const.h
Signature · 1 line
2.26.2
PATCH v4 1/13 update-linux-headers: Include const.h
From: Eric Farman <[email protected]>
Kernel commit a85cbe6159ff ("uapi: move constants from <linux/kernel.h> to <linux/const.h>") breaks our script because of the unrecognized include. Let's add that to our processing.
--- scripts/update-linux-headers.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/update-linux-headers.sh +4 -1
@@ -41,6 +41,7 @@ cp_portable() { -e 'pvrdma_verbs' \ -e 'drm.h' \ -e 'limits' \ + -e 'linux/const' \ -e 'linux/kernel' \ -e 'linux/sysinfo' \ -e 'asm-generic/kvm_para' \
@@ -190,7 +191,9 @@ for i in "$tmpdir"/include/linux/*virtio*.h \ "$tmpdir/include/linux/input.h" \ "$tmpdir/include/linux/input-event-codes.h" \ "$tmpdir/include/linux/pci_regs.h" \ - "$tmpdir/include/linux/ethtool.h" "$tmpdir/include/linux/kernel.h" \ + "$tmpdir/include/linux/ethtool.h" \ + "$tmpdir/include/linux/const.h" \ + "$tmpdir/include/linux/kernel.h" \ "$tmpdir/include/linux/vhost_types.h" \ "$tmpdir/include/linux/sysinfo.h"; do cp_portable "$i" "$output/include/standard-headers/linux"
Signature · 1 line
2.26.2
PATCH v4 2/13 Update linux headers to 5.11-rc2
In reply to Peter XuFrom: Eric Farman <[email protected]>
--- .../infiniband/hw/vmw_pvrdma/pvrdma_ring.h | 14 +- .../infiniband/hw/vmw_pvrdma/pvrdma_verbs.h | 2 +- include/standard-headers/drm/drm_fourcc.h | 175 +++++++++++++++++- include/standard-headers/linux/const.h | 36 ++++ include/standard-headers/linux/ethtool.h | 2 +- include/standard-headers/linux/fuse.h | 30 ++- include/standard-headers/linux/kernel.h | 9 +- include/standard-headers/linux/pci_regs.h | 16 ++ include/standard-headers/linux/vhost_types.h | 9 + include/standard-headers/linux/virtio_gpu.h | 82 ++++++++ include/standard-headers/linux/virtio_ids.h | 44 +++-- linux-headers/asm-arm64/kvm.h | 3 - linux-headers/asm-generic/unistd.h | 6 +- linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 1 + linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 1 + linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/kvm.h | 56 +++++- linux-headers/linux/userfaultfd.h | 9 + linux-headers/linux/vfio.h | 1 + linux-headers/linux/vhost.h | 4 + 28 files changed, 461 insertions(+), 48 deletions(-) create mode 100644 include/standard-headers/linux/const.h
include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h +7 -7
@@ -68,7 +68,7 @@ static inline int pvrdma_idx_valid(uint32_t idx, uint32_t max_elems)static inline int32_t pvrdma_idx (int *var,uint32_t max_elems) { -const unsigned int idx =qatomic_read (var); +const unsigned int idx =atomic_read (var);if (pvrdma_idx_valid (idx, max_elems))return idx & (max_elems -1 );
@@ -77,17 +77,17 @@ static inline int32_t pvrdma_idx(int *var, uint32_t max_elems)static inline void pvrdma_idx_ring_inc (int *var,uint32_t max_elems) { -uint32_t idx =qatomic_read (var) +1 ;/* Increment. */ +uint32_t idx =atomic_read (var) +1 ;/* Increment. */ idx &= (max_elems <<1 ) -1 ;/* Modulo size, flip gen. */ -qatomic_set (var, idx); +atomic_set (var, idx); }static inline int32_t pvrdma_idx_ring_has_space (const struct pvrdma_ring *r,uint32_t max_elems,uint32_t *out_tail) { -const uint32_t tail =qatomic_read (&r->prod_tail); -const uint32_t head =qatomic_read (&r->cons_head); +const uint32_t tail =atomic_read (&r->prod_tail); +const uint32_t head =atomic_read (&r->cons_head);if (pvrdma_idx_valid (tail, max_elems) &&pvrdma_idx_valid (head, max_elems)) {
@@ -100,8 +100,8 @@ static inline int32_t pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,static inline int32_t pvrdma_idx_ring_has_data (const struct pvrdma_ring *r,uint32_t max_elems,uint32_t *out_head) { -const uint32_t tail =qatomic_read (&r->prod_tail); -const uint32_t head =qatomic_read (&r->cons_head); +const uint32_t tail =atomic_read (&r->prod_tail); +const uint32_t head =atomic_read (&r->cons_head);if (pvrdma_idx_valid (tail, max_elems) &&pvrdma_idx_valid (head, max_elems)) {
include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_verbs.h +1 -1
@@ -176,7 +176,7 @@ struct pvrdma_port_attr {uint8_t subnet_timeout;uint8_t init_type_reply;uint8_t active_width; -uint16_t active_speed; +uint8_t active_speed;uint8_t phys_state;uint8_t reserved[2 ]; };
include/standard-headers/drm/drm_fourcc.h +174 -1
205 lines not on this page · raw message
include/standard-headers/linux/const.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* const.h: Macros for dealing with constants. */ + +#ifndef _LINUX_CONST_H +#define _LINUX_CONST_H + +/* Some constant macros are used in both assembler and +* C code. Therefore we cannot annotate them always with +* 'UL' and other type specifiers unilaterally. We +* use the following macros to deal with this. +* +* Similarly, _AT() will cast an expression with a type in C, but +* leave it unchanged in asm. +*/ + +#ifdef __ASSEMBLY__ +#define _AC (X ,Y ) X +#define _AT (T ,X ) X +#else +#define __AC (X ,Y ) (X##Y) +#define _AC (X ,Y ) __AC(X,Y) +#define _AT (T ,X ) ((T)(X)) +#endif + +#define _UL (x) (_AC(x, UL)) +#define _ULL (x) (_AC(x, ULL)) + +#define _BITUL (x) (_UL(1) << (x)) +#define _BITULL (x) (_ULL(1) << (x)) + +#define __ALIGN_KERNEL (x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1) +#define __ALIGN_KERNEL_MASK (x, mask) (((x) + (mask)) & ~(mask)) + +#define __KERNEL_DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d)) + +#endif /* _LINUX_CONST_H */
include/standard-headers/linux/ethtool.h +1 -1
@@ -16,7 +16,7 @@#include "net/eth.h" -#include "standard-headers/linux/kernel.h" +#include "standard-headers/linux/const.h" #include "standard-headers/linux/types.h" #include "standard-headers/linux/if_ether.h"
include/standard-headers/linux/fuse.h +25 -5
@@ -175,6 +175,10 @@* * 7.32 * - add flags to fuse_attr, add FUSE_ATTR_SUBMOUNT, add FUSE_SUBMOUNTS +* +* 7.33 +* - add FUSE_HANDLE_KILLPRIV_V2, FUSE_WRITE_KILL_SUIDGID, FATTR_KILL_SUIDGID +* - add FUSE_OPEN_KILL_SUIDGID */ #ifndef _LINUX_FUSE_H
@@ -206,7 +210,7 @@#define FUSE_KERNEL_VERSION 7/** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 32 +#define FUSE_KERNEL_MINOR_VERSION 33/** The node ID of the root inode */ #define FUSE_ROOT_ID 1
@@ -267,6 +271,7 @@ struct fuse_file_lock {#define FATTR_MTIME_NOW (1 << 8)#define FATTR_LOCKOWNER (1 << 9)#define FATTR_CTIME (1 << 10) +#define FATTR_KILL_SUIDGID (1 << 11)/** * Flags returned by the OPEN request
@@ -316,6 +321,11 @@ struct fuse_file_lock {* foffset and moffset fields in struct * fuse_setupmapping_out and fuse_removemapping_one. * FUSE_SUBMOUNTS: kernel supports auto-mounting directory submounts +* FUSE_HANDLE_KILLPRIV_V2: fs kills suid/sgid/cap on write/chown/trunc. +* Upon write/truncate suid/sgid is only killed if caller +* does not have CAP_FSETID. Additionally upon +* write/truncate sgid is killed only if file has group +* execute permission. (Same as Linux VFS behavior). */ #define FUSE_ASYNC_READ (1 << 0)#define FUSE_POSIX_LOCKS (1 << 1)
@@ -345,6 +355,7 @@ struct fuse_file_lock {#define FUSE_EXPLICIT_INVAL_DATA (1 << 25)#define FUSE_MAP_ALIGNMENT (1 << 26)#define FUSE_SUBMOUNTS (1 << 27) +#define FUSE_HANDLE_KILLPRIV_V2 (1 << 28)/** * CUSE INIT request/reply flags
@@ -374,11 +385,14 @@ struct fuse_file_lock {* * FUSE_WRITE_CACHE: delayed write from page cache, file handle is guessed * FUSE_WRITE_LOCKOWNER: lock_owner field is valid -* FUSE_WRITE_KILL_PRIV: kill suid and sgid bits +* FUSE_WRITE_KILL_SUIDGID: kill suid and sgid bits */ #define FUSE_WRITE_CACHE (1 << 0)#define FUSE_WRITE_LOCKOWNER (1 << 1) -#define FUSE_WRITE_KILL_PRIV (1 << 2) +#define FUSE_WRITE_KILL_SUIDGID (1 << 2) + +/* Obsolete alias; this flag implies killing suid/sgid only. */ +#define FUSE_WRITE_KILL_PRIV FUSE_WRITE_KILL_SUIDGID/** * Read flags
@@ -427,6 +441,12 @@ struct fuse_file_lock {*/ #define FUSE_ATTR_SUBMOUNT (1 << 0) +/** +* Open flags +* FUSE_OPEN_KILL_SUIDGID: Kill suid and sgid if executable +*/ +#define FUSE_OPEN_KILL_SUIDGID (1 << 0) +enum fuse_opcode {FUSE_LOOKUP =1 ,FUSE_FORGET =2 ,/* no reply */
@@ -588,14 +608,14 @@ struct fuse_setattr_in {struct fuse_open_in {uint32_t flags; -uint32_t unused; +uint32_t open_flags;/* FUSE_OPEN_... */ };struct fuse_create_in {uint32_t flags;uint32_t mode;uint32_t umask; -uint32_t padding; +uint32_t open_flags;/* FUSE_OPEN_... */ };struct fuse_open_out {
include/standard-headers/linux/kernel.h +1 -8
@@ -3,13 +3,6 @@#define _LINUX_KERNEL_H#include "standard-headers/linux/sysinfo.h" - -/* -* 'kernel.h' contains some often-used function prototypes etc -*/ -#define __ALIGN_KERNEL (x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1) -#define __ALIGN_KERNEL_MASK (x, mask) (((x) + (mask)) & ~(mask)) - -#define __KERNEL_DIV_ROUND_UP (n, d) (((n) + (d) - 1) / (d)) +#include "standard-headers/linux/const.h" #endif /* _LINUX_KERNEL_H */
include/standard-headers/linux/pci_regs.h +16 -0
@@ -531,6 +531,7 @@#define PCI_EXP_LNKCAP_SLS_8_0GB 0x00000003/* LNKCAP2 SLS Vector bit 2 */ #define PCI_EXP_LNKCAP_SLS_16_0GB 0x00000004/* LNKCAP2 SLS Vector bit 3 */ #define PCI_EXP_LNKCAP_SLS_32_0GB 0x00000005/* LNKCAP2 SLS Vector bit 4 */ +#define PCI_EXP_LNKCAP_SLS_64_0GB 0x00000006/* LNKCAP2 SLS Vector bit 5 */ #define PCI_EXP_LNKCAP_MLW 0x000003f0/* Maximum Link Width */ #define PCI_EXP_LNKCAP_ASPMS 0x00000c00/* ASPM Support */ #define PCI_EXP_LNKCAP_ASPM_L0S 0x00000400/* ASPM L0s Support */
@@ -562,6 +563,7 @@#define PCI_EXP_LNKSTA_CLS_8_0GB 0x0003/* Current Link Speed 8.0GT/s */ #define PCI_EXP_LNKSTA_CLS_16_0GB 0x0004/* Current Link Speed 16.0GT/s */ #define PCI_EXP_LNKSTA_CLS_32_0GB 0x0005/* Current Link Speed 32.0GT/s */ +#define PCI_EXP_LNKSTA_CLS_64_0GB 0x0006/* Current Link Speed 64.0GT/s */ #define PCI_EXP_LNKSTA_NLW 0x03f0/* Negotiated Link Width */ #define PCI_EXP_LNKSTA_NLW_X1 0x0010/* Current Link Width x1 */ #define PCI_EXP_LNKSTA_NLW_X2 0x0020/* Current Link Width x2 */
@@ -670,6 +672,7 @@#define PCI_EXP_LNKCAP2_SLS_8_0GB 0x00000008/* Supported Speed 8GT/s */ #define PCI_EXP_LNKCAP2_SLS_16_0GB 0x00000010/* Supported Speed 16GT/s */ #define PCI_EXP_LNKCAP2_SLS_32_0GB 0x00000020/* Supported Speed 32GT/s */ +#define PCI_EXP_LNKCAP2_SLS_64_0GB 0x00000040/* Supported Speed 64GT/s */ #define PCI_EXP_LNKCAP2_CROSSLINK 0x00000100/* Crosslink supported */ #define PCI_EXP_LNKCTL2 48/* Link Control 2 */ #define PCI_EXP_LNKCTL2_TLS 0x000f
@@ -678,6 +681,7 @@#define PCI_EXP_LNKCTL2_TLS_8_0GT 0x0003/* Supported Speed 8GT/s */ #define PCI_EXP_LNKCTL2_TLS_16_0GT 0x0004/* Supported Speed 16GT/s */ #define PCI_EXP_LNKCTL2_TLS_32_0GT 0x0005/* Supported Speed 32GT/s */ +#define PCI_EXP_LNKCTL2_TLS_64_0GT 0x0006/* Supported Speed 64GT/s */ #define PCI_EXP_LNKCTL2_ENTER_COMP 0x0010/* Enter Compliance */ #define PCI_EXP_LNKCTL2_TX_MARGIN 0x0380/* Transmit Margin */ #define PCI_EXP_LNKCTL2_HASD 0x0020/* HW Autonomous Speed Disable */
@@ -723,6 +727,7 @@#define PCI_EXT_CAP_ID_DPC 0x1D/* Downstream Port Containment */ #define PCI_EXT_CAP_ID_L1SS 0x1E/* L1 PM Substates */ #define PCI_EXT_CAP_ID_PTM 0x1F/* Precision Time Measurement */ +#define PCI_EXT_CAP_ID_DVSEC 0x23/* Designated Vendor-Specific */ #define PCI_EXT_CAP_ID_DLF 0x25/* Data Link Feature */ #define PCI_EXT_CAP_ID_PL_16GT 0x26/* Physical Layer 16.0 GT/s */ #define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PL_16GT
@@ -831,6 +836,13 @@#define PCI_PWR_CAP_BUDGET (x) ((x) & 1)/* Included in system budget */ #define PCI_EXT_CAP_PWR_SIZEOF 16 +/* Root Complex Event Collector Endpoint Association */ +#define PCI_RCEC_RCIEP_BITMAP 4/* Associated Bitmap for RCiEPs */ +#define PCI_RCEC_BUSN 8/* RCEC Associated Bus Numbers */ +#define PCI_RCEC_BUSN_REG_VER 0x02/* Least version with BUSN present */ +#define PCI_RCEC_BUSN_NEXT (x) (((x) >> 8) & 0xff) +#define PCI_RCEC_BUSN_LAST (x) (((x) >> 16) & 0xff) +/* Vendor-Specific (VSEC, PCI_EXT_CAP_ID_VNDR) */ #define PCI_VNDR_HEADER 4/* Vendor-Specific Header */ #define PCI_VNDR_HEADER_ID (x) ((x) & 0xffff)
@@ -1066,6 +1078,10 @@#define PCI_L1SS_CTL1_LTR_L12_TH_SCALE 0xe0000000/* LTR_L1.2_THRESHOLD_Scale */ #define PCI_L1SS_CTL2 0x0c/* Control 2 Register */ +/* Designated Vendor-Specific (DVSEC, PCI_EXT_CAP_ID_DVSEC) */ +#define PCI_DVSEC_HEADER1 0x4/* Designated Vendor-Specific Header1 */ +#define PCI_DVSEC_HEADER2 0x8/* Designated Vendor-Specific Header2 */ +/* Data Link Feature */ #define PCI_DLF_CAP 0x04/* Capabilities Register */ #define PCI_DLF_EXCHANGE_ENABLE 0x80000000/* Data Link Feature Exchange Enable */
include/standard-headers/linux/vhost_types.h +9 -0
@@ -138,6 +138,15 @@ struct vhost_vdpa_config {uint8_t buf[0 ]; }; +/* vhost vdpa IOVA range +* @first: First address that can be mapped by vhost-vDPA +* @last: Last address that can be mapped by vhost-vDPA +*/ +struct vhost_vdpa_iova_range { +uint64_t first; +uint64_t last; +}; +/* Feature bits */ /* Log all write descriptors. Can be changed while device is active. */ #define VHOST_F_LOG_ALL 26
include/standard-headers/linux/virtio_gpu.h +82 -0
116 lines not on this page · raw message
include/standard-headers/linux/virtio_ids.h +25 -19
49 lines not on this page · raw message
linux-headers/asm-arm64/kvm.h +0 -3
@@ -156,9 +156,6 @@ struct kvm_sync_regs {__u64 device_irq_level; }; -struct kvm_arch_memory_slot { -}; -/* * PMU filter structure. Describe a range of events with a particular * action. To be used with KVM_ARM_VCPU_PMU_V3_FILTER.
linux-headers/asm-generic/unistd.h +4 -2
@@ -517,7 +517,7 @@ __SC_COMP(__NR_settimeofday, sys_settimeofday, compat_sys_settimeofday) __SC_3264(__NR_adjtimex, sys_adjtimex_time32, sys_adjtimex)#endif -/* kernel/timer.c */ +/* kernel/sys.c */ #define __NR_getpid 172 __SYSCALL(__NR_getpid, sys_getpid)#define __NR_getppid 173
@@ -859,9 +859,11 @@ __SYSCALL(__NR_pidfd_getfd, sys_pidfd_getfd) __SYSCALL(__NR_faccessat2, sys_faccessat2)#define __NR_process_madvise 440 __SYSCALL(__NR_process_madvise, sys_process_madvise) +#define __NR_epoll_pwait2 441 +__SC_COMP(__NR_epoll_pwait2, sys_epoll_pwait2, compat_sys_epoll_pwait2)#undef __NR_syscalls -#define __NR_syscalls 441 +#define __NR_syscalls 442/* * 32 bit systems traditionally used different
linux-headers/asm-mips/unistd_n32.h +1 -0
@@ -370,6 +370,7 @@#define __NR_pidfd_getfd (__NR_Linux + 438)#define __NR_faccessat2 (__NR_Linux + 439)#define __NR_process_madvise (__NR_Linux + 440) +#define __NR_epoll_pwait2 (__NR_Linux + 441)#endif /* _ASM_MIPS_UNISTD_N32_H */
linux-headers/asm-mips/unistd_n64.h +1 -0
@@ -346,6 +346,7 @@#define __NR_pidfd_getfd (__NR_Linux + 438)#define __NR_faccessat2 (__NR_Linux + 439)#define __NR_process_madvise (__NR_Linux + 440) +#define __NR_epoll_pwait2 (__NR_Linux + 441)#endif /* _ASM_MIPS_UNISTD_N64_H */
linux-headers/asm-mips/unistd_o32.h +1 -0
@@ -416,6 +416,7 @@#define __NR_pidfd_getfd (__NR_Linux + 438)#define __NR_faccessat2 (__NR_Linux + 439)#define __NR_process_madvise (__NR_Linux + 440) +#define __NR_epoll_pwait2 (__NR_Linux + 441)#endif /* _ASM_MIPS_UNISTD_O32_H */
linux-headers/asm-powerpc/unistd_32.h +1 -0
@@ -423,6 +423,7 @@#define __NR_pidfd_getfd 438#define __NR_faccessat2 439#define __NR_process_madvise 440 +#define __NR_epoll_pwait2 441#endif /* _ASM_POWERPC_UNISTD_32_H */
linux-headers/asm-powerpc/unistd_64.h +1 -0
@@ -395,6 +395,7 @@#define __NR_pidfd_getfd 438#define __NR_faccessat2 439#define __NR_process_madvise 440 +#define __NR_epoll_pwait2 441#endif /* _ASM_POWERPC_UNISTD_64_H */
linux-headers/asm-s390/unistd_32.h +1 -0
@@ -413,5 +413,6 @@#define __NR_pidfd_getfd 438#define __NR_faccessat2 439#define __NR_process_madvise 440 +#define __NR_epoll_pwait2 441#endif /* _ASM_S390_UNISTD_32_H */
linux-headers/asm-s390/unistd_64.h +1 -0
@@ -361,5 +361,6 @@#define __NR_pidfd_getfd 438#define __NR_faccessat2 439#define __NR_process_madvise 440 +#define __NR_epoll_pwait2 441#endif /* _ASM_S390_UNISTD_64_H */
linux-headers/asm-x86/kvm.h +1 -0
@@ -12,6 +12,7 @@#define KVM_PIO_PAGE_OFFSET 1#define KVM_COALESCED_MMIO_PAGE_OFFSET 2 +#define KVM_DIRTY_LOG_PAGE_OFFSET 64#define DE_VECTOR 0#define DB_VECTOR 1
linux-headers/asm-x86/unistd_32.h +1 -0
@@ -431,6 +431,7 @@#define __NR_pidfd_getfd 438#define __NR_faccessat2 439#define __NR_process_madvise 440 +#define __NR_epoll_pwait2 441#endif /* _ASM_X86_UNISTD_32_H */
linux-headers/asm-x86/unistd_64.h +1 -0
@@ -353,6 +353,7 @@#define __NR_pidfd_getfd 438#define __NR_faccessat2 439#define __NR_process_madvise 440 +#define __NR_epoll_pwait2 441#endif /* _ASM_X86_UNISTD_64_H */
linux-headers/asm-x86/unistd_x32.h +1 -0
@@ -306,6 +306,7 @@#define __NR_pidfd_getfd (__X32_SYSCALL_BIT + 438)#define __NR_faccessat2 (__X32_SYSCALL_BIT + 439)#define __NR_process_madvise (__X32_SYSCALL_BIT + 440) +#define __NR_epoll_pwait2 (__X32_SYSCALL_BIT + 441)#define __NR_rt_sigaction (__X32_SYSCALL_BIT + 512)#define __NR_rt_sigreturn (__X32_SYSCALL_BIT + 513)#define __NR_ioctl (__X32_SYSCALL_BIT + 514)
linux-headers/linux/kvm.h +55 -1
84 lines not on this page · raw message
linux-headers/linux/userfaultfd.h +9 -0
@@ -257,4 +257,13 @@ struct uffdio_writeprotect { __u64 mode; }; +/* +* Flags for the userfaultfd(2) system call itself. +*/ + +/* +* Create a userfaultfd that can handle page faults only in user mode. +*/ +#define UFFD_USER_MODE_ONLY 1 +#endif /* _LINUX_USERFAULTFD_H */
linux-headers/linux/vfio.h +1 -0
@@ -820,6 +820,7 @@ enum {enum {VFIO_CCW_IO_IRQ_INDEX ,VFIO_CCW_CRW_IRQ_INDEX , +VFIO_CCW_REQ_IRQ_INDEX ,VFIO_CCW_NUM_IRQS };
linux-headers/linux/vhost.h +4 -0
@@ -146,4 +146,8 @@/* Set event fd for config interrupt*/ #define VHOST_VDPA_SET_CONFIG_CALL _IOW(VHOST_VIRTIO, 0x77, int) + +/* Get the valid iova range */ +#define VHOST_VDPA_GET_IOVA_RANGE _IOR(VHOST_VIRTIO, 0x78, \ + struct vhost_vdpa_iova_range)#endif
Signature · 1 line
2.26.2
PATCH v4 3/13 memory: Introduce log_sync_global() to memory listener
In reply to Peter XuSome of the memory listener may want to do log synchronization without being able to specify a range of memory to sync but always globally. Such a memory listener should provide this new method instead of the log_sync() method.
Obviously we can also achieve similar thing when we put the global sync logic into a log_sync() handler. However that's not efficient enough because otherwise memory_global_dirty_log_sync() may do the global sync N times, where N is the number of flat ranges in the address space.
Make this new method be exclusive to log_sync().
--- include/exec/memory.h | 12 ++++++++++++ softmmu/memory.c | 33 +++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 10 deletions(-)
include/exec/memory.h +12 -0
@@ -610,6 +610,18 @@ struct MemoryListener {*/ void (*log_sync)(MemoryListener *listener,MemoryRegionSection *section); +/** +* @log_sync_global: +* +* This is the global version of @log_sync when the listener does +* not have a way to synchronize the log with finer granularity. +* When the listener registers with @log_sync_global defined, then +* its @log_sync must be NULL. Vice versa. +* +* @listener: The #MemoryListener. +*/ +void (*log_sync_global)(MemoryListener *listener); +/** * @log_clear: *
softmmu/memory.c +23 -10
@@ -2052,6 +2052,10 @@ void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, memory_region_get_dirty_log_mask(mr)); } +/* +* If memory region `mr' is NULL, do global sync. Otherwise, sync +* dirty bitmap for the specified memory region. +*/ static void memory_region_sync_dirty_bitmap (MemoryRegion *mr) {MemoryListener *listener;
@@ -2065,18 +2069,24 @@ static void memory_region_sync_dirty_bitmap(MemoryRegion *mr)* address space once. */ QTAILQ_FOREACH(listener, &memory_listeners, link) { -if (!listener->log_sync) { -continue ; - } - as = listener->address_space; - view =address_space_get_flatview (as); - FOR_EACH_FLAT_RANGE(fr, view) { -if (fr->dirty_log_mask && (!mr || fr->mr == mr)) { -MemoryRegionSection mrs =section_from_flat_range (fr, view); - listener->log_sync (listener, &mrs); +if (listener->log_sync) { + as = listener->address_space; + view =address_space_get_flatview (as); + FOR_EACH_FLAT_RANGE(fr, view) { +if (fr->dirty_log_mask && (!mr || fr->mr == mr)) { +MemoryRegionSection mrs =section_from_flat_range (fr, view); + listener->log_sync (listener, &mrs); + } } +flatview_unref (view); + }else if (listener->log_sync_global) { +/* +* No matter whether MR is specified, what we can do here +* is to do a global sync, because we are not capable to +* sync in a finer granularity. +*/ + listener->log_sync_global (listener); } -flatview_unref (view); } }
@@ -2764,6 +2774,9 @@ void memory_listener_register(MemoryListener *listener, AddressSpace *as) {MemoryListener *other =NULL ; +/* Only one of them can be defined for a listener */ +assert (!(listener->log_sync && listener->log_sync_global)); + listener->address_space = as;if (QTAILQ_EMPTY (&memory_listeners) || listener->priority >=QTAILQ_LAST (&memory_listeners)->priority) {
Signature · 1 line
2.26.2
PATCH v4 4/13 KVM: Fixup kvm_log_clear_one_slot() ioctl return check
In reply to Peter Xukvm_vm_ioctl() handles the errno trick already for ioctl() on returning -1 for errors. Fix this.
--- accel/kvm/kvm-all.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
accel/kvm/kvm-all.c +2 -3
@@ -771,14 +771,13 @@ static int kvm_log_clear_one_slot(KVMSlot *mem, int as_id, uint64_t start, d.num_pages = bmap_npages; d.slot = mem->slot | (as_id <<16 ); -if (kvm_vm_ioctl (s,KVM_CLEAR_DIRTY_LOG , &d) ==-1 ) { - ret = -errno; + ret =kvm_vm_ioctl (s,KVM_CLEAR_DIRTY_LOG , &d); +if (ret) {error_report ("%s: KVM_CLEAR_DIRTY_LOG failed, slot=%d, " "start=0x%" PRIx64", size=0x%" PRIx32", errno=%d" , __func__, d.slot, (uint64_t )d.first_page, (uint32_t )d.num_pages, ret); }else { - ret =0 ;trace_kvm_clear_dirty_log (d.slot, d.first_page, d.num_pages); }
Signature · 1 line
2.26.2
PATCH v4 5/13 KVM: Use a big lock to replace per-kml slots_lock
Per-kml slots_lock will bring some trouble if we want to take all slots_lock of all the KMLs, especially when we're in a context that we could have taken some of the KML slots_lock, then we even need to figure out what we've taken and what we need to take.
Make this simple by merging all KML slots_lock into a single slots lock.
Per-kml slots_lock isn't anything that helpful anyway - so far only x86 has two address spaces (so, two slots_locks). All the rest archs will be having one address space always, which means there's actually one slots_lock so it will be the same as before.
--- accel/kvm/kvm-all.c | 32 +++++++++++++++++--------------- include/sysemu/kvm_int.h | 2 -- 2 files changed, 17 insertions(+), 17 deletions(-)
accel/kvm/kvm-all.c +17 -15
@@ -176,8 +176,10 @@ typedef struct KVMResampleFd KVMResampleFd; static QLIST_HEAD(, KVMResampleFd) kvm_resample_fd_list =QLIST_HEAD_INITIALIZER (kvm_resample_fd_list); -#define kvm_slots_lock (kml) qemu_mutex_lock(&(kml)->slots_lock) -#define kvm_slots_unlock (kml) qemu_mutex_unlock(&(kml)->slots_lock) +static QemuMutex kml_slots_lock; + +#define kvm_slots_lock () qemu_mutex_lock(&kml_slots_lock) +#define kvm_slots_unlock () qemu_mutex_unlock(&kml_slots_lock)static inline void kvm_resample_fd_remove (int gsi) {
@@ -263,9 +265,9 @@ bool kvm_has_free_slot(MachineState *ms)bool result;KVMMemoryListener *kml = &s->memory_listener; -kvm_slots_lock (kml); +kvm_slots_lock (); result = !!kvm_get_free_slot (kml); -kvm_slots_unlock (kml); +kvm_slots_unlock ();return result; }
@@ -331,7 +333,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,KVMMemoryListener *kml = &s->memory_listener;int i, ret =0 ; -kvm_slots_lock (kml); +kvm_slots_lock ();for (i =0 ; i < s->nr_slots; i++) {KVMSlot *mem = &kml->slots[i];
@@ -341,7 +343,7 @@ int kvm_physical_memory_addr_from_host(KVMState *s, void *ram,break ; } } -kvm_slots_unlock (kml); +kvm_slots_unlock ();return ret; }
@@ -537,7 +539,7 @@ static int kvm_section_update_flags(KVMMemoryListener *kml,return 0 ; } -kvm_slots_lock (kml); +kvm_slots_lock ();while (size && !ret) { slot_size =MIN (kvm_max_slot_size, size);
@@ -553,7 +555,7 @@ static int kvm_section_update_flags(KVMMemoryListener *kml, } out: -kvm_slots_unlock (kml); +kvm_slots_unlock ();return ret; }
@@ -826,7 +828,7 @@ static int kvm_physical_log_clear(KVMMemoryListener *kml,return ret; } -kvm_slots_lock (kml); +kvm_slots_lock ();for (i =0 ; i < s->nr_slots; i++) { mem = &kml->slots[i];
@@ -852,7 +854,7 @@ static int kvm_physical_log_clear(KVMMemoryListener *kml, } } -kvm_slots_unlock (kml); +kvm_slots_unlock ();return ret; }
@@ -1157,7 +1159,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, ram =memory_region_get_ram_ptr (mr) + section->offset_within_region + (start_addr - section->offset_within_address_space); -kvm_slots_lock (kml); +kvm_slots_lock ();if (!add) { do {
@@ -1215,7 +1217,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, }while (size); out: -kvm_slots_unlock (kml); +kvm_slots_unlock (); } static void kvm_region_add(MemoryListener *listener,
@@ -1242,9 +1244,9 @@ static void kvm_log_sync(MemoryListener *listener,KVMMemoryListener *kml =container_of (listener, KVMMemoryListener, listener);int r; -kvm_slots_lock (kml); +kvm_slots_lock (); r =kvm_physical_sync_dirty_bitmap (kml, section); -kvm_slots_unlock (kml); +kvm_slots_unlock ();if (r <0 ) {abort (); }
@@ -1344,7 +1346,7 @@ void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml, {int i; -qemu_mutex_init (&kml->slots_lock); +qemu_mutex_init (&kml_slots_lock); kml->slots =g_malloc0 (s->nr_slots *sizeof (KVMSlot)); kml->as_id = as_id;
include/sysemu/kvm_int.h +0 -2
@@ -27,8 +27,6 @@ typedef struct KVMSlottypedef struct KVMMemoryListener {MemoryListener listener; -/* Protects the slots and all inside them */ -QemuMutex slots_lock;KVMSlot *slots;int as_id; }KVMMemoryListener ;
Signature · 1 line
2.26.2
PATCH v4 6/13 KVM: Create the KVMSlot dirty bitmap on flag changes
In reply to Peter XuPreviously we have two places that will create the per KVMSlot dirty bitmap:
1. When a newly created KVMSlot has dirty logging enabled,
2. When the first log_sync() happens for a memory slot.
The 2nd case is lazy-init, while the 1st case is not (which is a fix of what the 2nd case missed).
To do explicit initialization of dirty bitmaps, what we're missing is to create the dirty bitmap when the slot changed from not-dirty-track to dirty-track. Do that in kvm_slot_update_flags().
With that, we can safely remove the 2nd lazy-init.
This change will be needed for kvm dirty ring because kvm dirty ring does not use the log_sync() interface at all.
Also move all the pre-checks into kvm_slot_init_dirty_bitmap().
--- accel/kvm/kvm-all.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
accel/kvm/kvm-all.c +9 -14
@@ -181,6 +181,8 @@ static QemuMutex kml_slots_lock;#define kvm_slots_lock () qemu_mutex_lock(&kml_slots_lock)#define kvm_slots_unlock () qemu_mutex_unlock(&kml_slots_lock) +static void kvm_slot_init_dirty_bitmap (KVMSlot *mem); +static inline void kvm_resample_fd_remove (int gsi) {KVMResampleFd *rfd;
@@ -524,6 +526,7 @@ static int kvm_slot_update_flags(KVMMemoryListener *kml, KVMSlot *mem,return 0 ; } +kvm_slot_init_dirty_bitmap (mem);return kvm_set_user_memory_region (kml, mem, false); }
@@ -608,8 +611,12 @@ static int kvm_get_dirty_pages_log_range(MemoryRegionSection *section,#define ALIGN (x, y) (((x)+(y)-1) & ~((y)-1))/* Allocate the dirty bitmap for a slot */ -static void kvm_memslot_init_dirty_bitmap (KVMSlot *mem) +static void kvm_slot_init_dirty_bitmap (KVMSlot *mem) { +if (!(mem->flags &KVM_MEM_LOG_DIRTY_PAGES ) || mem->dirty_bmap) { +return ; + } +/* * XXX bad kernel interface alert * For dirty bitmap, kernel allocates array of size aligned to
@@ -660,11 +667,6 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, goto out; } -if (!mem->dirty_bmap) { -/* Allocate on the first log_sync, once and for all */ -kvm_memslot_init_dirty_bitmap (mem); - } - d.dirty_bitmap = mem->dirty_bmap; d.slot = mem->slot | (kml->as_id <<16 );if (kvm_vm_ioctl (s,KVM_GET_DIRTY_LOG , &d) ==-1 ) {
@@ -1197,14 +1199,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->start_addr = start_addr; mem->ram = ram; mem->flags =kvm_mem_flags (mr); - -if (mem->flags &KVM_MEM_LOG_DIRTY_PAGES ) { -/* -* Reallocate the bmap; it means it doesn't disappear in -* middle of a migrate. -*/ -kvm_memslot_init_dirty_bitmap (mem); - } +kvm_slot_init_dirty_bitmap (mem); err =kvm_set_user_memory_region (kml, mem, true);if (err) { fprintf(stderr, "%s: error registering slot: %s\n", __func__,
Signature · 1 line
2.26.2
PATCH v4 7/13 KVM: Provide helper to get kvm dirty log
In reply to Peter XuProvide a helper kvm_slot_get_dirty_log() to make the function kvm_physical_sync_dirty_bitmap() clearer. We can even cache the as_id into KVMSlot when it is created, so that we don't even need to pass it down every time.
Since at it, remove return value of kvm_physical_sync_dirty_bitmap() because it should never fail.
--- accel/kvm/kvm-all.c | 42 +++++++++++++++++++++------------------- include/sysemu/kvm_int.h | 2 ++ 2 files changed, 24 insertions(+), 20 deletions(-)
accel/kvm/kvm-all.c +22 -20
@@ -635,6 +635,21 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem) mem->dirty_bmap =g_malloc0 (bitmap_size); } +/* Sync dirty bitmap from kernel to KVMSlot.dirty_bmap */ +static void kvm_slot_get_dirty_log (KVMState *s,KVMSlot *slot) +{ +struct kvm_dirty_log d = {}; +int ret; + + d.dirty_bitmap = slot->dirty_bmap; + d.slot = slot->slot | (slot->as_id <<16 ); + ret =kvm_vm_ioctl (s,KVM_GET_DIRTY_LOG , &d); +if (ret) { +error_report_once ("%s: KVM_GET_DIRTY_LOG failed with %d" , + __func__, ret); + } +} +/** * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space *
@@ -646,15 +661,13 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem)* @kml: the KVM memory listener object * @section: the memory section to sync the dirty bitmap with */ -static int kvm_physical_sync_dirty_bitmap (KVMMemoryListener *kml, -MemoryRegionSection *section) +static void kvm_physical_sync_dirty_bitmap (KVMMemoryListener *kml, +MemoryRegionSection *section) {KVMState *s = kvm_state; -struct kvm_dirty_log d = {};KVMSlot *mem;hwaddr start_addr, size;hwaddr slot_size, slot_offset =0 ; -int ret =0 ; size =kvm_align_section (section, &start_addr);while (size) {
@@ -664,27 +677,19 @@ static int kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, mem =kvm_lookup_matching_slot (kml, start_addr, slot_size);if (!mem) {/* We don't have a slot if we want to trap every access. */ - goto out; +return ; } - d.dirty_bitmap = mem->dirty_bmap; - d.slot = mem->slot | (kml->as_id <<16 ); -if (kvm_vm_ioctl (s,KVM_GET_DIRTY_LOG , &d) ==-1 ) { -DPRINTF ("ioctl failed %d\n" , errno); - ret =-1 ; - goto out; - } +kvm_slot_get_dirty_log (s, mem); subsection.offset_within_region += slot_offset; subsection.size =int128_make64 (slot_size); -kvm_get_dirty_pages_log_range (&subsection, d.dirty_bitmap); +kvm_get_dirty_pages_log_range (&subsection, mem->dirty_bmap); slot_offset += slot_size; start_addr += slot_size; size -= slot_size; } -out: -return ret; }/* Alignment requirement for KVM_CLEAR_DIRTY_LOG - 64 pages */
@@ -1195,6 +1200,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, do { slot_size = MIN(kvm_max_slot_size, size); mem = kvm_alloc_slot(kml); + mem->as_id = kml->as_id; mem->memory_size = slot_size; mem->start_addr = start_addr; mem->ram = ram;
@@ -1237,14 +1243,10 @@ static void kvm_log_sync(MemoryListener *listener, MemoryRegionSection *section) {KVMMemoryListener *kml =container_of (listener, KVMMemoryListener, listener); -int r;kvm_slots_lock (); - r =kvm_physical_sync_dirty_bitmap (kml, section); +kvm_physical_sync_dirty_bitmap (kml, section);kvm_slots_unlock (); -if (r <0 ) { -abort (); - } } static void kvm_log_clear(MemoryListener *listener,
include/sysemu/kvm_int.h +2 -0
@@ -23,6 +23,8 @@ typedef struct KVMSlotint old_flags;/* Dirty bitmap cache for the slot */ unsigned long *dirty_bmap; +/* Cache of the address space ID */ +int as_id; } KVMSlot; typedef struct KVMMemoryListener {
Signature · 1 line
2.26.2
PATCH v4 8/13 KVM: Provide helper to sync dirty bitmap from slot to ramblock
In reply to Peter Xukvm_physical_sync_dirty_bitmap() calculates the ramblock offset in an awkward way from the MemoryRegionSection that passed in from the caller. The truth is for each KVMSlot the ramblock offset never change for the lifecycle. Cache the ramblock offset for each KVMSlot into the structure when the KVMSlot is created.
With that, we can further simplify kvm_physical_sync_dirty_bitmap() with a helper to sync KVMSlot dirty bitmap to the ramblock dirty bitmap of a specific KVMSlot.
--- accel/kvm/kvm-all.c | 37 +++++++++++++++++-------------------- include/sysemu/kvm_int.h | 2 ++ 2 files changed, 19 insertions(+), 20 deletions(-)
accel/kvm/kvm-all.c +17 -20
@@ -597,15 +597,12 @@ static void kvm_log_stop(MemoryListener *listener, }/* get kvm's dirty pages bitmap and update qemu's */ -static int kvm_get_dirty_pages_log_range (MemoryRegionSection *section, -unsigned long *bitmap) +static void kvm_slot_sync_dirty_pages (KVMSlot *slot) { -ram_addr_t start = section->offset_within_region + -memory_region_get_ram_addr (section->mr); -ram_addr_t pages =int128_get64 (section->size) / qemu_real_host_page_size; +ram_addr_t start = slot->ram_start_offset; +ram_addr_t pages = slot->memory_size / qemu_real_host_page_size; -cpu_physical_memory_set_dirty_lebitmap (bitmap, start, pages); -return 0 ; +cpu_physical_memory_set_dirty_lebitmap (slot->dirty_bmap, start, pages); }#define ALIGN (x, y) (((x)+(y)-1) & ~((y)-1))
@@ -667,12 +664,10 @@ static void kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml,KVMState *s = kvm_state;KVMSlot *mem;hwaddr start_addr, size; -hwaddr slot_size, slot_offset =0 ; +hwaddr slot_size; size =kvm_align_section (section, &start_addr);while (size) { -MemoryRegionSection subsection = *section; - slot_size =MIN (kvm_max_slot_size, size); mem =kvm_lookup_matching_slot (kml, start_addr, slot_size);if (!mem) {
@@ -681,12 +676,7 @@ static void kvm_physical_sync_dirty_bitmap(KVMMemoryListener *kml, }kvm_slot_get_dirty_log (s, mem); - - subsection.offset_within_region += slot_offset; - subsection.size =int128_make64 (slot_size); -kvm_get_dirty_pages_log_range (&subsection, mem->dirty_bmap); - - slot_offset += slot_size; +kvm_slot_sync_dirty_pages (mem); start_addr += slot_size; size -= slot_size; }
@@ -1144,7 +1134,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,int err;MemoryRegion *mr = section->mr;bool writeable = !mr->readonly && !mr->rom_device; -hwaddr start_addr, size, slot_size; +hwaddr start_addr, size, slot_size, mr_offset; +ram_addr_t ram_start_offset;void *ram;if (!memory_region_is_ram (mr)) {
@@ -1162,9 +1153,13 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,return ; } -/* use aligned delta to align the ram address */ - ram =memory_region_get_ram_ptr (mr) + section->offset_within_region + - (start_addr - section->offset_within_address_space); +/* The offset of the kvmslot within the memory region */ + mr_offset = section->offset_within_region + start_addr - + section->offset_within_address_space; + +/* use aligned delta to align the ram address and offset */ + ram =memory_region_get_ram_ptr (mr) + mr_offset; + ram_start_offset =memory_region_get_ram_addr (mr) + mr_offset;kvm_slots_lock ();
@@ -1203,6 +1198,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, mem->as_id = kml->as_id; mem->memory_size = slot_size; mem->start_addr = start_addr; + mem->ram_start_offset = ram_start_offset; mem->ram = ram; mem->flags =kvm_mem_flags (mr);kvm_slot_init_dirty_bitmap (mem);
@@ -1213,6 +1209,7 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml,abort (); } start_addr += slot_size; + ram_start_offset += slot_size; ram += slot_size; size -= slot_size; }while (size);
include/sysemu/kvm_int.h +2 -0
@@ -25,6 +25,8 @@ typedef struct KVMSlotunsigned long *dirty_bmap;/* Cache of the address space ID */ int as_id; +/* Cache of the offset in ram address space */ +ram_addr_t ram_start_offset; } KVMSlot; typedef struct KVMMemoryListener {
Signature · 1 line
2.26.2
PATCH v4 9/13 KVM: Simplify dirty log sync in kvm_set_phys_mem
In reply to Peter Xukvm_physical_sync_dirty_bitmap() on the whole section is inaccurate, because the section can be a superset of the memslot that we're working on. The result is that if the section covers multiple kvm memslots, we could be doing the synchronization for multiple times for each kvmslot in the section.
With the two helpers that we just introduced, it's very easy to do it right now by calling the helpers.
--- accel/kvm/kvm-all.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +2 -1
@@ -1171,7 +1171,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, goto out; }if (mem->flags &KVM_MEM_LOG_DIRTY_PAGES ) { -kvm_physical_sync_dirty_bitmap (kml, section); +kvm_slot_get_dirty_log (kvm_state, mem); +kvm_slot_sync_dirty_pages (mem); }/* unregister the slot */
Signature · 1 line
2.26.2
PATCH v4 10/13 KVM: Cache kvm slot dirty bitmap size
In reply to Peter XuCache it too because we'll reference it more frequently in the future.
--- accel/kvm/kvm-all.c | 1 + include/sysemu/kvm_int.h | 1 + 2 files changed, 2 insertions(+)
accel/kvm/kvm-all.c +1 -0
@@ -630,6 +630,7 @@ static void kvm_slot_init_dirty_bitmap(KVMSlot *mem)hwaddr bitmap_size =ALIGN (((mem->memory_size) >>TARGET_PAGE_BITS ),/*HOST_LONG_BITS*/ 64 ) /8 ; mem->dirty_bmap =g_malloc0 (bitmap_size); + mem->dirty_bmap_size = bitmap_size; }/* Sync dirty bitmap from kernel to KVMSlot.dirty_bmap */
include/sysemu/kvm_int.h +1 -0
@@ -23,6 +23,7 @@ typedef struct KVMSlotint old_flags;/* Dirty bitmap cache for the slot */ unsigned long *dirty_bmap; +unsigned long dirty_bmap_size;/* Cache of the address space ID */ int as_id;/* Cache of the offset in ram address space */
Signature · 1 line
2.26.2
PATCH v4 11/13 KVM: Add dirty-gfn-count property
In reply to Peter XuAdd a parameter for dirty gfn count for dirty rings. If zero, dirty ring is disabled. Otherwise dirty ring will be enabled with the per-vcpu gfn count as specified. If dirty ring cannot be enabled due to unsupported kernel or illegal parameter, it'll fallback to dirty logging.
By default, dirty ring is not enabled (dirty-gfn-count default to 0).
--- accel/kvm/kvm-all.c | 72 +++++++++++++++++++++++++++++++++++++++++++++ qemu-options.hx | 12 ++++++++ 2 files changed, 84 insertions(+)
accel/kvm/kvm-all.c +72 -0
@@ -130,6 +130,9 @@ struct KVMStateKVMMemoryListener *ml;AddressSpace *as; } *as; +bool kvm_dirty_ring_enabled;/* Whether KVM dirty ring is enabled */ +uint64_t kvm_dirty_ring_size;/* Size of the per-vcpu dirty ring */ +uint32_t kvm_dirty_gfn_count;/* Number of dirty GFNs per ring */ };KVMState *kvm_state;
@@ -2137,6 +2140,40 @@ static int kvm_init(MachineState *ms) s->coalesced_pio = s->coalesced_mmio &&kvm_check_extension (s,KVM_CAP_COALESCED_PIO ); +/* +* Enable KVM dirty ring if supported, otherwise fall back to +* dirty logging mode +*/ +if (s->kvm_dirty_gfn_count >0 ) { +uint64_t ring_size; + + ring_size = s->kvm_dirty_gfn_count *sizeof (struct kvm_dirty_gfn ); + +/* Read the max supported pages */ + ret =kvm_vm_check_extension (s,KVM_CAP_DIRTY_LOG_RING ); +if (ret >0 ) { +if (ring_size > ret) { +error_report ("KVM dirty GFN count %" PRIu32" too big " +"(maximum is %ld). Please use a smaller value." , + s->kvm_dirty_gfn_count, + ret /sizeof (struct kvm_dirty_gfn )); + ret = -EINVAL ; + goto err; + } + + ret =kvm_vm_enable_cap (s,KVM_CAP_DIRTY_LOG_RING ,0 , ring_size); +if (ret) { +error_report ("Enabling of KVM dirty ring failed: %d. " +"Suggested mininum value is 1024. " +"Please also make sure it's a power of two." , ret); + goto err; + } + + s->kvm_dirty_ring_size = ring_size; + s->kvm_dirty_ring_enabled = true; + } + } + dirty_log_manual_caps =kvm_check_extension (s,KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ); dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE |
@@ -3191,6 +3228,33 @@ bool kvm_kernel_irqchip_split(void)return kvm_state->kernel_irqchip_split ==ON_OFF_AUTO_ON ; } +static void kvm_get_dirty_gfn_count (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +KVMState *s =KVM_STATE (obj); +uint32_t value = s->kvm_dirty_gfn_count; + +visit_type_uint32 (v, name, &value, errp); +} + +static void kvm_set_dirty_gfn_count (Object *obj,Visitor *v, +const char *name,void *opaque, +Error **errp) +{ +KVMState *s =KVM_STATE (obj); +Error *error =NULL ; +uint32_t value; + +visit_type_uint32 (v, name, &value, &error); +if (error) { +error_propagate (errp, error); +return ; + } + + s->kvm_dirty_gfn_count = value; +} +static void kvm_accel_instance_init (Object *obj) {KVMState *s =KVM_STATE (obj);
@@ -3198,6 +3262,8 @@ static void kvm_accel_instance_init(Object *obj) s->kvm_shadow_mem =-1 ; s->kernel_irqchip_allowed = true; s->kernel_irqchip_split =ON_OFF_AUTO_AUTO ; +/* KVM dirty ring is by default off */ + s->kvm_dirty_gfn_count =0 ; }static void kvm_accel_class_init (ObjectClass *oc,void *data)
@@ -3219,6 +3285,12 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data) NULL, NULL);object_class_property_set_description (oc,"kvm-shadow-mem" ,"KVM shadow MMU size" ); + +object_class_property_add (oc,"dirty-gfn-count" ,"uint32" , + kvm_get_dirty_gfn_count, kvm_set_dirty_gfn_count, +NULL ,NULL ); +object_class_property_set_description (oc,"dirty-gfn-count" , +"KVM dirty GFN count (=0 to disable dirty ring)" ); }static const TypeInfo kvm_accel_type = {
qemu-options.hx +12 -0
@@ -116,6 +116,7 @@ DEF("accel", HAS_ARG, QEMU_OPTION_accel, " kernel-irqchip=on|off|split controls accelerated irqchip support (default=on)\n" " kvm-shadow-mem=size of KVM shadow MMU in bytes\n" " tb-size=n (TCG translation block cache size)\n" + " dirty-gfn-count=n (KVM dirty ring GFN count, default 0)\n" " thread=single|multi (enable multi-threaded TCG)\n", QEMU_ARCH_ALL) SRST ``-accel name[,prop=value[,...]]``
@@ -150,6 +151,17 @@ SRST where both the back-end and front-ends support it and no incompatible TCG features have been enabled (e.g. icount/replay). + + ``dirty-gfn-count=n`` + When KVM accelerator is used, it controls the per-vcpu KVM dirty ring + size (number of entries one dirty ring contains, per-vcpu). It should + be a value that is power of two, and it should be 1024 or bigger (but + still less than the maximum value that the kernel supports). 4096 + could be a good initial value if you have no idea which is the best. + Set this value to 0 to disable the feature. By default, this feature + is disabled (dirty-gfn-count=0). When enabled, it'll automatically + replace the kvm get dirty log feature. + ERST DEF("smp", HAS_ARG, QEMU_OPTION_smp,
Signature · 1 line
2.26.2
PATCH v4 12/13 KVM: Disable manual dirty log when dirty ring enabled
In reply to Peter XuKVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is for KVM_CLEAR_DIRTY_LOG, which is only useful for KVM_GET_DIRTY_LOG. Skip enabling it for kvm dirty ring.
More importantly, KVM_DIRTY_LOG_INITIALLY_SET will not wr-protect all the pages initially, which is against how kvm dirty ring is used - there's no way for kvm dirty ring to re-protect a page before it's notified as being written first with a GFN entry in the ring! So when KVM_DIRTY_LOG_INITIALLY_SET is enabled with dirty ring, we'll see silent data loss after migration.
--- accel/kvm/kvm-all.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-)
accel/kvm/kvm-all.c +23 -14
@@ -2174,20 +2174,29 @@ static int kvm_init(MachineState *ms) } } - dirty_log_manual_caps = -kvm_check_extension (s,KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ); - dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | -KVM_DIRTY_LOG_INITIALLY_SET ); - s->manual_dirty_log_protect = dirty_log_manual_caps; -if (dirty_log_manual_caps) { - ret =kvm_vm_enable_cap (s,KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ,0 , - dirty_log_manual_caps); -if (ret) { -warn_report ("Trying to enable capability %" PRIu64" of " -"KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed. " -"Falling back to the legacy mode. " , - dirty_log_manual_caps); - s->manual_dirty_log_protect =0 ; +/* +* KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is not needed when dirty ring is +* enabled. More importantly, KVM_DIRTY_LOG_INITIALLY_SET will assume no +* page is wr-protected initially, which is against how kvm dirty ring is +* usage - kvm dirty ring requires all pages are wr-protected at the very +* beginning. Enabling this feature for dirty ring causes data corruption. +*/ +if (!s->kvm_dirty_ring_enabled) { + dirty_log_manual_caps = +kvm_check_extension (s,KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ); + dirty_log_manual_caps &= (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | +KVM_DIRTY_LOG_INITIALLY_SET ); + s->manual_dirty_log_protect = dirty_log_manual_caps; +if (dirty_log_manual_caps) { + ret =kvm_vm_enable_cap (s,KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 ,0 , + dirty_log_manual_caps); +if (ret) { +warn_report ("Trying to enable capability %" PRIu64" of " +"KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 but failed. " +"Falling back to the legacy mode. " , + dirty_log_manual_caps); + s->manual_dirty_log_protect =0 ; + } } }
Signature · 1 line
2.26.2
PATCH v4 13/13 KVM: Dirty ring support
In reply to Peter XuKVM dirty ring is a new interface to pass over dirty bits from kernel to the userspace. Instead of using a bitmap for each memory region, the dirty ring contains an array of dirtied GPAs to fetch (in the form of offset in slots). For each vcpu there will be one dirty ring that binds to it.
kvm_dirty_ring_reap() is the major function to collect dirty rings. It can be called either by a standalone reaper thread that runs in the background, collecting dirty pages for the whole VM. It can also be called directly by any thread that has BQL taken.
--- accel/kvm/kvm-all.c | 331 ++++++++++++++++++++++++++++++++++++++++- accel/kvm/trace-events | 7 + include/hw/core/cpu.h | 8 + 3 files changed, 343 insertions(+), 3 deletions(-)
accel/kvm/kvm-all.c +328 -3
@@ -15,6 +15,7 @@#include "qemu/osdep.h" #include <sys/ioctl.h> +#include <poll.h> #include <linux/kvm.h>
@@ -78,6 +79,25 @@ struct KVMParkedVcpu {QLIST_ENTRY (KVMParkedVcpu) node; }; +enum KVMDirtyRingReaperState { +KVM_DIRTY_RING_REAPER_NONE =0 , +/* The reaper is sleeping */ +KVM_DIRTY_RING_REAPER_WAIT , +/* The reaper is reaping for dirty pages */ +KVM_DIRTY_RING_REAPER_REAPING , +}; + +/* +* KVM reaper instance, responsible for collecting the KVM dirty bits +* via the dirty ring. +*/ +struct KVMDirtyRingReaper { +/* The reaper thread */ +QemuThread reaper_thr; +volatile uint64_t reaper_iteration;/* iteration number of reaper thr */ +volatile enum KVMDirtyRingReaperState reaper_state;/* reap thr state */ +}; +struct KVMState {AccelState parent_obj;
@@ -133,6 +153,7 @@ struct KVMStatebool kvm_dirty_ring_enabled;/* Whether KVM dirty ring is enabled */ uint64_t kvm_dirty_ring_size;/* Size of the per-vcpu dirty ring */ uint32_t kvm_dirty_gfn_count;/* Number of dirty GFNs per ring */ +struct KVMDirtyRingReaper reaper; };KVMState *kvm_state;
@@ -414,6 +435,13 @@ static int do_kvm_destroy_vcpu(CPUState *cpu) goto err; } +if (cpu->kvm_dirty_gfns) { + ret =munmap (cpu->kvm_dirty_gfns, s->kvm_dirty_ring_size); +if (ret <0 ) { + goto err; + } + } + vcpu =g_malloc0 (sizeof (*vcpu)); vcpu->vcpu_id =kvm_arch_vcpu_id (cpu); vcpu->kvm_fd = cpu->kvm_fd;
@@ -490,6 +518,19 @@ int kvm_init_vcpu(CPUState *cpu, Error **errp) (void *)cpu->kvm_run + s->coalesced_mmio *PAGE_SIZE ; } +if (s->kvm_dirty_ring_enabled) { +/* Use MAP_SHARED to share pages with the kernel */ + cpu->kvm_dirty_gfns =mmap (NULL , s->kvm_dirty_ring_size, +PROT_READ |PROT_WRITE ,MAP_SHARED , + cpu->kvm_fd, +PAGE_SIZE *KVM_DIRTY_LOG_PAGE_OFFSET ); +if (cpu->kvm_dirty_gfns ==MAP_FAILED ) { + ret = -errno; +DPRINTF ("mmap'ing vcpu dirty gfns failed: %d\n" , ret); + goto err; + } + } + ret =kvm_arch_init_vcpu (cpu);if (ret <0 ) { error_setg_errno(errp, -ret,
@@ -608,6 +649,11 @@ static void kvm_slot_sync_dirty_pages(KVMSlot *slot)cpu_physical_memory_set_dirty_lebitmap (slot->dirty_bmap, start, pages); } +static void kvm_slot_reset_dirty_pages (KVMSlot *slot) +{ +memset (slot->dirty_bmap,0 , slot->dirty_bmap_size); +} +#define ALIGN (x, y) (((x)+(y)-1) & ~((y)-1))/* Allocate the dirty bitmap for a slot */
@@ -651,6 +697,170 @@ static void kvm_slot_get_dirty_log(KVMState *s, KVMSlot *slot) } } +/* Should be with all slots_lock held for the address spaces. */ +static void kvm_dirty_ring_mark_page (KVMState *s,uint32_t as_id, +uint32_t slot_id,uint64_t offset) +{ +KVMMemoryListener *kml; +KVMSlot *mem; + +if (as_id >= s->nr_as) { +return ; + } + + kml = s->as[as_id].ml; + mem = &kml->slots[slot_id]; + +if (!mem->memory_size || offset >= (mem->memory_size /TARGET_PAGE_SIZE )) { +return ; + } + +set_bit (offset, mem->dirty_bmap); +} + +static bool dirty_gfn_is_dirtied (struct kvm_dirty_gfn *gfn) +{ +return gfn->flags ==KVM_DIRTY_GFN_F_DIRTY ; +} + +static void dirty_gfn_set_collected (struct kvm_dirty_gfn *gfn) +{ + gfn->flags =KVM_DIRTY_GFN_F_RESET ; +} + +/* +* Should be with all slots_lock held for the address spaces. It returns the +* dirty page we've collected on this dirty ring. +*/ +static uint32_t kvm_dirty_ring_reap_one (KVMState *s,CPUState *cpu) +{ +struct kvm_dirty_gfn *dirty_gfns = cpu->kvm_dirty_gfns, *cur; +uint32_t gfn_count = s->kvm_dirty_gfn_count; +uint32_t count =0 , fetch = cpu->kvm_fetch_index; + +assert (dirty_gfns && gfn_count); +trace_kvm_dirty_ring_reap_vcpu (cpu->cpu_index); + +while (true) { + cur = &dirty_gfns[fetch % gfn_count]; +if (!dirty_gfn_is_dirtied (cur)) { +break ; + } +kvm_dirty_ring_mark_page (s, cur->slot >>16 , cur->slot &0xffff , + cur->offset); +dirty_gfn_set_collected (cur); +trace_kvm_dirty_ring_page (cpu->cpu_index, fetch, cur->offset); + fetch++; + count++; + } + cpu->kvm_fetch_index = fetch; + +return count; +} + +/* Must be with slots_lock held */ +static uint64_t kvm_dirty_ring_reap_locked (KVMState *s) +{ +int ret; +CPUState *cpu; +uint64_t total =0 ; +int64_t stamp; + + stamp =get_clock (); + +CPU_FOREACH (cpu) { + total +=kvm_dirty_ring_reap_one (s, cpu); + } + +if (total) { + ret =kvm_vm_ioctl (s,KVM_RESET_DIRTY_RINGS ); +assert (ret == total); + } + + stamp =get_clock () - stamp; + +if (total) { +trace_kvm_dirty_ring_reap (total, stamp /1000 ); + } + +return total; +} + +/* +* Currently for simplicity, we must hold BQL before calling this. We can +* consider to drop the BQL if we're clear with all the race conditions. +*/ +static uint64_t kvm_dirty_ring_reap (KVMState *s) +{ +uint64_t total; + +/* +* We need to lock all kvm slots for all address spaces here, +* because: +* +* (1) We need to mark dirty for dirty bitmaps in multiple slots +* and for tons of pages, so it's better to take the lock here +* once rather than once per page. And more importantly, +* +* (2) We must _NOT_ publish dirty bits to the other threads +* (e.g., the migration thread) via the kvm memory slot dirty +* bitmaps before correctly re-protect those dirtied pages. +* Otherwise we can have potential risk of data corruption if +* the page data is read in the other thread before we do +* reset below. +*/ +kvm_slots_lock (); + total =kvm_dirty_ring_reap_locked (s); +kvm_slots_unlock (); + +return total; +} + +static void do_kvm_cpu_synchronize_kick (CPUState *cpu,run_on_cpu_data arg) +{ +/* No need to do anything */ +} + +/* +* Kick all vcpus out in a synchronized way. When returned, we +* guarantee that every vcpu has been kicked and at least returned to +* userspace once. +*/ +static void kvm_cpu_synchronize_kick_all (void ) +{ +CPUState *cpu; + +CPU_FOREACH (cpu) { +run_on_cpu (cpu, do_kvm_cpu_synchronize_kick,RUN_ON_CPU_NULL ); + } +} + +/* +* Flush all the existing dirty pages to the KVM slot buffers. When +* this call returns, we guarantee that all the touched dirty pages +* before calling this function have been put into the per-kvmslot +* dirty bitmap. +* +* This function must be called with BQL held. +*/ +static void kvm_dirty_ring_flush (struct KVMDirtyRingReaper *r) +{ +trace_kvm_dirty_ring_flush (0 ); +/* +* The function needs to be serialized. Since this function +* should always be with BQL held, serialization is guaranteed. +* However, let's be sure of it. +*/ +assert (qemu_mutex_iothread_locked ()); +/* +* First make sure to flush the hardware buffers by kicking all +* vcpus out in a synchronous way. +*/ +kvm_cpu_synchronize_kick_all (); +kvm_dirty_ring_reap (kvm_state); +trace_kvm_dirty_ring_flush (1 ); +} +/** * kvm_physical_sync_dirty_bitmap - Sync dirty bitmap from kernel space *
@@ -1175,7 +1385,24 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, goto out; }if (mem->flags &KVM_MEM_LOG_DIRTY_PAGES ) { -kvm_slot_get_dirty_log (kvm_state, mem); +/* +* NOTE: We should be aware of the fact that here we're only +* doing a best effort to sync dirty bits. No matter whether +* we're using dirty log or dirty ring, we ignored two facts: +* +* (1) dirty bits can reside in hardware buffers (PML) +* +* (2) after we collected dirty bits here, pages can be dirtied +* again before we do the final KVM_SET_USER_MEMORY_REGION to +* remove the slot. +* +* Not easy. Let's cross the fingers until it's fixed. +*/ +if (kvm_state->kvm_dirty_ring_enabled) { +kvm_dirty_ring_reap_locked (kvm_state); + }else { +kvm_slot_get_dirty_log (kvm_state, mem); + }kvm_slot_sync_dirty_pages (mem); }
@@ -1223,6 +1450,51 @@ out:kvm_slots_unlock (); } +static void *kvm_dirty_ring_reaper_thread (void *data) +{ +KVMState *s = data; +struct KVMDirtyRingReaper *r = &s->reaper; + +rcu_register_thread (); + +trace_kvm_dirty_ring_reaper ("init" ); + +while (true) { + r->reaper_state =KVM_DIRTY_RING_REAPER_WAIT ; +trace_kvm_dirty_ring_reaper ("wait" ); +/* +* TODO: provide a smarter timeout rather than a constant? +*/ +sleep (1 ); + +trace_kvm_dirty_ring_reaper ("wakeup" ); + r->reaper_state =KVM_DIRTY_RING_REAPER_REAPING ; + +qemu_mutex_lock_iothread (); +kvm_dirty_ring_reap (s); +qemu_mutex_unlock_iothread (); + + r->reaper_iteration++; + } + +trace_kvm_dirty_ring_reaper ("exit" ); + +rcu_unregister_thread (); + +return NULL ; +} + +static int kvm_dirty_ring_reaper_init (KVMState *s) +{ +struct KVMDirtyRingReaper *r = &s->reaper; + +qemu_thread_create (&r->reaper_thr,"kvm-reaper" , + kvm_dirty_ring_reaper_thread, + s,QEMU_THREAD_JOINABLE ); + +return 0 ; +} +static void kvm_region_add (MemoryListener *listener,MemoryRegionSection *section) {
@@ -1251,6 +1523,36 @@ static void kvm_log_sync(MemoryListener *listener,kvm_slots_unlock (); } +static void kvm_log_sync_global (MemoryListener *l) +{ +KVMMemoryListener *kml =container_of (l, KVMMemoryListener, listener); +KVMState *s = kvm_state; +KVMSlot *mem; +int i; + +/* Flush all kernel dirty addresses into KVMSlot dirty bitmap */ +kvm_dirty_ring_flush (&s->reaper); + +/* +* TODO: make this faster when nr_slots is big while there are +* only a few used slots (small VMs). +*/ +kvm_slots_lock (); +for (i =0 ; i < s->nr_slots; i++) { + mem = &kml->slots[i]; +if (mem->memory_size && mem->flags &KVM_MEM_LOG_DIRTY_PAGES ) { +kvm_slot_sync_dirty_pages (mem); +/* +* This is not needed by KVM_GET_DIRTY_LOG because the +* ioctl will unconditionally overwrite the whole region. +* However kvm dirty ring has no such side effect. +*/ +kvm_slot_reset_dirty_pages (mem); + } + } +kvm_slots_unlock (); +} +static void kvm_log_clear (MemoryListener *listener,MemoryRegionSection *section) {
@@ -1357,10 +1659,15 @@ void kvm_memory_listener_register(KVMState *s, KVMMemoryListener *kml, kml->listener.region_del = kvm_region_del; kml->listener.log_start = kvm_log_start; kml->listener.log_stop = kvm_log_stop; - kml->listener.log_sync = kvm_log_sync; - kml->listener.log_clear = kvm_log_clear; kml->listener.priority =10 ; +if (s->kvm_dirty_ring_enabled) { + kml->listener.log_sync_global = kvm_log_sync_global; + }else { + kml->listener.log_sync = kvm_log_sync; + kml->listener.log_clear = kvm_log_clear; + } +memory_listener_register (&kml->listener, as);for (i =0 ; i < s->nr_as; ++i) {
@@ -2297,6 +2604,13 @@ static int kvm_init(MachineState *ms)assert (!ret); } +if (s->kvm_dirty_ring_enabled) { + ret =kvm_dirty_ring_reaper_init (s); +if (ret) { + goto err; + } + } +cpus_register_accel (&kvm_cpus);return 0 ;
@@ -2605,6 +2919,17 @@ int kvm_cpu_exec(CPUState *cpu)case KVM_EXIT_INTERNAL_ERROR : ret =kvm_handle_internal_error (cpu, run);break ; +case KVM_EXIT_DIRTY_RING_FULL : +/* +* We shouldn't continue if the dirty ring of this vcpu is +* still full. Got kicked by KVM_RESET_DIRTY_RINGS. +*/ +trace_kvm_dirty_ring_full (cpu->cpu_index); +qemu_mutex_lock_iothread (); +kvm_dirty_ring_reap (kvm_state); +qemu_mutex_unlock_iothread (); + ret =0 ; +break ;case KVM_EXIT_SYSTEM_EVENT :switch (run->system_event.type) {case KVM_SYSTEM_EVENT_SHUTDOWN :
accel/kvm/trace-events +7 -0
@@ -18,4 +18,11 @@ kvm_set_ioeventfd_pio(int fd, uint16_t addr, uint32_t val, bool assign, uint32_t kvm_set_user_memory(uint32_t slot, uint32_t flags, uint64_t guest_phys_addr, uint64_t memory_size, uint64_t userspace_addr, int ret) "Slot#%d flags=0x%x gpa=0x%"PRIx64 " size=0x%"PRIx64 " ua=0x%"PRIx64 " ret=%d" kvm_clear_dirty_log(uint32_t slot, uint64_t start, uint32_t size) "slot#%"PRId32" start 0x%"PRIx64" size 0x%"PRIx32 kvm_resample_fd_notify(int gsi) "gsi %d" +kvm_dirty_ring_full(int id) "vcpu %d" +kvm_dirty_ring_reap_vcpu(int id) "vcpu %d" +kvm_dirty_ring_page(int vcpu, uint32_t slot, uint64_t offset) "vcpu %d fetch %"PRIu32" offset 0x%"PRIx64 +kvm_dirty_ring_reaper(const char *s) "%s" +kvm_dirty_ring_reap(uint64_t count, int64_t t) "reaped %"PRIu64" pages (took %"PRIi64" us)" +kvm_dirty_ring_reaper_kick(const char *reason) "%s" +kvm_dirty_ring_flush(int finished) "%d"
include/hw/core/cpu.h +8 -0
@@ -357,6 +357,11 @@ struct qemu_work_item;* @ignore_memory_transaction_failures: Cached copy of the MachineState * flag of the same name: allows the board to suppress calling of the * CPU do_transaction_failed hook function. +* @kvm_dirty_ring_full: +* Whether the kvm dirty ring of this vcpu is soft-full. +* @kvm_dirty_ring_avail: +* Semaphore to be posted when the kvm dirty ring of the vcpu is +* available again. * * State of one CPU core or thread. */
@@ -428,9 +433,12 @@ struct CPUState {*/ uintptr_t mem_io_pc; +/* Only used in KVM */ int kvm_fd;struct KVMState *kvm_state;struct kvm_run *kvm_run; +struct kvm_dirty_gfn *kvm_dirty_gfns; +uint32_t kvm_fetch_index;/* Used for events with 'vcpu' and *without* the 'disabled' properties */ DECLARE_BITMAP (trace_dstate_delayed,CPU_TRACE_DSTATE_MAX_EVENTS );
Signature · 1 line
2.26.2
Quoted 40 lines · Peter Xu
This is v4 of the qemu dirty ring interface support.
It is merely the same as v3 content-wise, but there're a few things to mention besides the rebase itself:
- I picked up two patches from Eric Farman for the linux-header updates (from Eric's v3 series) for convenience just in case any of the series would got queued by any maintainer.
- One more patch is added as "KVM: Disable manual dirty log when dirty ring enabled". I found this when testing the branch after rebasing to latest qemu, that not only the manual dirty log capability is not needed for kvm dirty ring, but more importantly
INITIALLY_ALL_SETis totally against kvm dirty ring and it could silently crash the guest after migration. For this new commit, I touched up "KVM: Add dirty-gfn-count property" a bit.- A few more documentation lines in qemu-options.hx.
- I removed the RFC tag after kernel series got merged.
Again, this is only the 1st step to support dirty ring. Ideally dirty ring should grant QEMU the possibility to remove the whole layered dirty bitmap so that dirty ring will work similarly as auto-converge enabled but should better; we will just throttle vcpus with the dirty ring kvm exit rather than explicitly adding a timer to stop the vcpu thread from entering the guest again (like what we did with current migration auto-converge). Some more information could also be found in the kvm forum 2020 talk regarding kvm dirty ring (slides 21/22 [1]).
That next step (to remove all the dirty bitmaps, as mentioned above) is still
discussable: firstly I don't know whether there's anything I've overlooked in there. Meanwhile that's also only services huge VM cases, may not be extremely helpful with a lot major scenarios where VMs are not that huge.There's probably other ways to fix huge VM migration issues, majorly focusing on responsiveness and convergence. For example, Google has proposed some new userfaultfd kernel capability called "minor modes" [2] to track page minor faults and that could be finally served for that purpose too using postcopy. That's another long story so I'll stop here, but just as a marker along with the dirty ring series so there'll still be a record to reference.
Quoted 5 lines
Said that, I still think this series is very worth merging even if we don't persue the next steps yet, since dirty ring is disabled by default, and we can always work upon this series.
Please review, thanks.
Ping - Paolo, what would be your take on this series?
Thanks,
Signature · 1 line
Peter Xu