Skip to contentSkip to conversation

KVM: Dirty Quota-Based VM Live Migration Auto-Converge

PATCH New Shivam Kumar 2 participants 10 messages Oct 26, 2021 – Oct 27, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover KVM: Dirty Quota-Based VM Live Migration Auto-Converge

This patchset is the KVM-side implementation of a (new) dirty "quota" based throttling algorithm that selectively throttles vCPUs based on their individual contribution to overall memory dirtying and also dynamically adapts the throttle based on the available network bandwidth.

Overview
--------
--------

To throttle memory dirtying, we propose to set a limit on the number of pages a vCPU can dirty in given fixed microscopic size time intervals. This limit depends on the network throughput calculated over the last few intervals so as to throttle the vCPUs based on available network bandwidth. We are referring to this limit as the "dirty quota" of a vCPU and the fixed size intervals as the "dirty quota intervals".

One possible approach to distributing the overall scope of dirtying for a dirty quota interval is to equally distribute it among all the vCPUs. This approach to the distribution doesn't make sense if the distribution of workloads among vCPUs is skewed. So, to counter such skewed cases, we propose that if any vCPU doesn't need its quota for any given dirty quota interval, we add this quota to a common pool. This common pool (or "common quota") can be consumed on a first come first serve basis by all vCPUs in the upcoming dirty quota intervals.

Design
------
------

Initialization

vCPUDirtyQuotaContext keeps the dirty quota context for each vCPU. It keeps the number of pages the vCPU has dirtied (dirty_counter) in the ongoing dirty quota interval and the maximum number of dirties allowed for the vCPU (dirty_quota) in the ongoing dirty quota interval.

struct vCPUDirtyQuotaContext {
	u64 dirty_counter;
	u64 dirty_quota;
};

The flag dirty_quota_migration_enabled determines whether dirty quota-based throttling is enabled for an ongoing migration or not.

Handling page dirtying

When the guest tries to dirty a page, it leads to a vmexit as each page is write-protected. In the vmexit path, we increment the dirty_counter for the corresponding vCPU. Then, we check if the vCPU has exceeded its quota. If yes, we exit to userspace with a new exit reason KVM_EXIT_DIRTY_QUOTA_FULL. This "quota full" event is further handled on the userspace side.

Please find the KVM Forum presentation on dirty quota-based throttling
here: https://www.youtube.com/watch?v=ZBkkJf78zFA

Shivam Kumar (6):
Define data structures needed for dirty quota migration. Allocate memory for dirty quota context and initialize dirty quota
migration flag.
Add dirty quota migration capability and handle vCPU page fault for
dirty quota context.
Increment dirty counter for vmexit due to page write fault. Exit to userspace when dirty quota is full.
Free space allocated for the vCPU's dirty quota context upon destroy.

 arch/x86/kvm/Makefile                 |  3 ++-
 arch/x86/kvm/x86.c                    |  9 ++++++++
 include/linux/dirty_quota_migration.h | 21 ++++++++++++++++++
 include/linux/kvm_host.h              |  3 +++
 include/uapi/linux/kvm.h              |  2 ++
 virt/kvm/dirty_quota_migration.c      | 31 ++++++++++++++++++++++++++
 virt/kvm/kvm_main.c                   | 32 ++++++++++++++++++++++++++-
 7 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/dirty_quota_migration.h
 create mode 100644 virt/kvm/dirty_quota_migration.c
Signature · 1 line
2.22.3
#1 lore raw

PATCH 1/6 Define data structures needed for dirty quota migration.

Define the data structures to be used on the KVM side:

vCPUDirtyQuotaContext: stores the dirty quota context for individual vCPUs (shared between QEMU and KVM).
dirty_counter: number of pages dirtied by the vCPU
dirty_quota: limit on the number of pages the vCPU can dirty
dirty_quota_migration_enabled: flag to see if migration is on or off.

---
 include/linux/dirty_quota_migration.h | 11 +++++++++++
 include/linux/kvm_host.h              |  3 +++
 2 files changed, 14 insertions(+)
 create mode 100644 include/linux/dirty_quota_migration.h
include/linux/dirty_quota_migration.h new file +11 -0
new file mode 100644
index 000000000000..6338cb6984df
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef DIRTY_QUOTA_MIGRATION_H
+#define DIRTY_QUOTA_MIGRATION_H
+#include <linux/kvm.h>
+
+struct vCPUDirtyQuotaContext {
+	u64 dirty_counter;
+	u64 dirty_quota;
+};
+
+#endif  /* DIRTY_QUOTA_MIGRATION_H */
include/linux/kvm_host.h +3 -0
index 0f18df7fe874..9f6165617c38 100644
@@ -38,6 +38,7 @@
 
 #include <asm/kvm_host.h>
 #include <linux/kvm_dirty_ring.h>
+#include <linux/dirty_quota_migration.h>
 
 #ifndef KVM_MAX_VCPU_ID
 #define KVM_MAX_VCPU_ID KVM_MAX_VCPUS
@@ -361,6 +362,7 @@ struct kvm_vcpu {
 	 * it is a valid slot.
 	 */
 	int last_used_slot;
+	struct vCPUDirtyQuotaContext *vCPUdqctx;
 };
 
 /* must be called with irqs disabled */
@@ -618,6 +620,7 @@ struct kvm {
 	unsigned int max_halt_poll_ns;
 	u32 dirty_ring_size;
 	bool vm_bugged;
+	bool dirty_quota_migration_enabled;
 
 #ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
 	struct notifier_block pm_notifier;
Signature · 1 line
2.22.3
#2 lore raw

PATCH 2/6 Allocate memory for dirty quota context and initialize dirty quota migration flag.

In reply to Shivam Kumar

When the VM is created, we initialize the flag to track the start of a dirty quota migration as false. It is set to true when the dirty quota migration starts.
When a vCPU is created, we allocate memory for the dirty quota context of the vCPU. This dirty quota context is mmaped to QEMU when a dirty quota migration starts.

---
 arch/x86/kvm/Makefile                 |  3 ++-
 include/linux/dirty_quota_migration.h |  2 ++
 virt/kvm/dirty_quota_migration.c      | 14 ++++++++++++++
 virt/kvm/kvm_main.c                   |  6 ++++++
 4 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 virt/kvm/dirty_quota_migration.c
arch/x86/kvm/Makefile +2 -1
index 75dfd27b6e8a..a26fc0c94a83 100644
@@ -11,7 +11,8 @@ KVM := ../../../virt/kvm
 
 kvm-y			+= $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \
 				$(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o \
-				$(KVM)/dirty_ring.o $(KVM)/binary_stats.o
+				$(KVM)/dirty_ring.o $(KVM)/binary_stats.o \
+				$(KVM)/dirty_quota_migration.o
 kvm-$(CONFIG_KVM_ASYNC_PF)	+= $(KVM)/async_pf.o
 
 kvm-y			+= x86.o emulate.o i8259.o irq.o lapic.o \
include/linux/dirty_quota_migration.h +2 -0
index 6338cb6984df..2d6e5cd17be6 100644
@@ -8,4 +8,6 @@ struct vCPUDirtyQuotaContext {
 	u64 dirty_quota;
 };
 
+int kvm_vcpu_dirty_quota_alloc(struct vCPUDirtyQuotaContext **vCPUdqctx);
+
 #endif  /* DIRTY_QUOTA_MIGRATION_H */
virt/kvm/dirty_quota_migration.c new file +14 -0
new file mode 100644
index 000000000000..262f071aac0c
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <linux/mm.h>
+#include <linux/vmalloc.h>
+#include <linux/dirty_quota_migration.h>
+
+int kvm_vcpu_dirty_quota_alloc(struct vCPUDirtyQuotaContext **vCPUdqctx)
+{
+	u64 size = sizeof(struct vCPUDirtyQuotaContext);
+	*vCPUdqctx = vmalloc(size);
+	if (!(*vCPUdqctx))
+		return -ENOMEM;
+	memset((*vCPUdqctx), 0, size);
+	return 0;
+}
virt/kvm/kvm_main.c +6 -0
index 7851f3a1b5f7..f232a16a26e7 100644
@@ -66,6 +66,7 @@
 #include <trace/events/kvm.h>
 
 #include <linux/kvm_dirty_ring.h>
+#include <linux/dirty_quota_migration.h>
 
 /* Worst case buffer size needed for holding an integer. */
 #define ITOA_MAX_LEN 12
@@ -1071,6 +1072,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
 	}
 
 	kvm->max_halt_poll_ns = halt_poll_ns;
+	kvm->dirty_quota_migration_enabled = false;
 
 	r = kvm_arch_init_vm(kvm, type);
 	if (r)
@@ -3630,6 +3632,10 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 			goto arch_vcpu_destroy;
 	}
 
+	r = kvm_vcpu_dirty_quota_alloc(&vcpu->vCPUdqctx);
+	if (r)
+		goto arch_vcpu_destroy;
+
 	mutex_lock(&kvm->lock);
 	if (kvm_get_vcpu_by_id(kvm, id)) {
 		r = -EEXIST;
Signature · 1 line
2.22.3
#3 lore raw

Re: [PATCH 2/6] Allocate memory for dirty quota context and initialize dirty quota migration flag.

Thank you for the patch! Yet something to improve:

Hi Shivam,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on mst-vhost/linux-next]
[also build test ERROR on linus/master v5.15-rc7 next-20211026] [cannot apply to kvm/queue]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Shivam-Kumar/KVM-Dirty-Quota-Based-VM-Live-Migration-Auto-Converge/20211027-003852
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: s390-alldefconfig (attached as .config)
compiler: s390-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/96d8525a5ba81c559c00d6c0fa1f2b2e84fe74ce
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Shivam-Kumar/KVM-Dirty-Quota-Based-VM-Live-Migration-Auto-Converge/20211027-003852 git checkout 96d8525a5ba81c559c00d6c0fa1f2b2e84fe74ce
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=s390 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

s390-linux-ld: arch/s390/../../virt/kvm/kvm_main.o: in function `kvm_vm_ioctl_create_vcpu':

>> kvm_main.c:(.text+0x3766): undefined reference to `kvm_vcpu_dirty_quota_alloc'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw

Re: [PATCH 2/6] Allocate memory for dirty quota context and initialize dirty quota migration flag.

In reply to Shivam Kumar
Thank you for the patch! Yet something to improve:

Hi Shivam,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on mst-vhost/linux-next]
[also build test ERROR on linus/master v5.15-rc7 next-20211026] [cannot apply to kvm/queue]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Shivam-Kumar/KVM-Dirty-Quota-Based-VM-Live-Migration-Auto-Converge/20211027-003852
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: mips-malta_kvm_defconfig (attached as .config)
compiler: mipsel-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/96d8525a5ba81c559c00d6c0fa1f2b2e84fe74ce
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Shivam-Kumar/KVM-Dirty-Quota-Based-VM-Live-Migration-Auto-Converge/20211027-003852 git checkout 96d8525a5ba81c559c00d6c0fa1f2b2e84fe74ce
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=mips

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "kvm_vcpu_dirty_quota_alloc" [arch/mips/kvm/kvm.ko] undefined!

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#5 lore raw

PATCH 3/6 Add dirty quota migration capability and handle vCPU page fault for dirty quota context.

In reply to Shivam Kumar

When a dirty quota migration is initiated from QEMU side, the following things happen:

1. An mmap ioctl is called for each vCPU to mmap the dirty quota context. This results into vCPU page fault which needs to be handled.
2. An ioctl to start dirty quota migration is called from QEMU and must be handled. This happens once QEMU is ready to start the migration.

---
 include/linux/dirty_quota_migration.h |  6 ++++++
 include/uapi/linux/kvm.h              |  1 +
 virt/kvm/dirty_quota_migration.c      |  6 ++++++
 virt/kvm/kvm_main.c                   | 15 +++++++++++++++
 4 files changed, 28 insertions(+)
include/linux/dirty_quota_migration.h +6 -0
index 2d6e5cd17be6..a9a54c38ee54 100644
@@ -3,11 +3,17 @@
 #define DIRTY_QUOTA_MIGRATION_H
 #include <linux/kvm.h>
 
+#ifndef KVM_DIRTY_QUOTA_PAGE_OFFSET
+#define KVM_DIRTY_QUOTA_PAGE_OFFSET 64
+#endif
+
 struct vCPUDirtyQuotaContext {
 	u64 dirty_counter;
 	u64 dirty_quota;
 };
 
 int kvm_vcpu_dirty_quota_alloc(struct vCPUDirtyQuotaContext **vCPUdqctx);
+struct page *kvm_dirty_quota_context_get_page(
+		struct vCPUDirtyQuotaContext *vCPUdqctx, u32 offset);
 
 #endif  /* DIRTY_QUOTA_MIGRATION_H */
include/uapi/linux/kvm.h +1 -0
index a067410ebea5..3649a3bb9bb8 100644
@@ -1112,6 +1112,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_BINARY_STATS_FD 203
 #define KVM_CAP_EXIT_ON_EMULATION_FAILURE 204
 #define KVM_CAP_ARM_MTE 205
+#define KVM_CAP_DIRTY_QUOTA_MIGRATION 206
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
virt/kvm/dirty_quota_migration.c +6 -0
index 262f071aac0c..7e9ace760939 100644
@@ -12,3 +12,9 @@ int kvm_vcpu_dirty_quota_alloc(struct vCPUDirtyQuotaContext **vCPUdqctx)
 	memset((*vCPUdqctx), 0, size);
 	return 0;
 }
+
+struct page *kvm_dirty_quota_context_get_page(
+		struct vCPUDirtyQuotaContext *vCPUdqctx, u32 offset)
+{
+	return vmalloc_to_page((void *)vCPUdqctx + offset * PAGE_SIZE);
+}
virt/kvm/kvm_main.c +15 -0
index f232a16a26e7..95f857c50bf2 100644
@@ -3511,6 +3511,9 @@ static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
 		page = kvm_dirty_ring_get_page(
 		    &vcpu->dirty_ring,
 		    vmf->pgoff - KVM_DIRTY_LOG_PAGE_OFFSET);
+	else if (vmf->pgoff == KVM_DIRTY_QUOTA_PAGE_OFFSET)
+		page = kvm_dirty_quota_context_get_page(vcpu->vCPUdqctx,
+				vmf->pgoff - KVM_DIRTY_QUOTA_PAGE_OFFSET);
 	else
 		return kvm_arch_vcpu_fault(vcpu, vmf);
 	get_page(page);
@@ -4263,6 +4266,15 @@ static int kvm_vm_ioctl_reset_dirty_pages(struct kvm *kvm)
 	return cleared;
 }
 
+static int kvm_vm_ioctl_enable_dirty_quota_migration(struct kvm *kvm,
+		bool dirty_quota_migration_enabled)
+{
+	mutex_lock(&kvm->lock);
+	kvm->dirty_quota_migration_enabled = dirty_quota_migration_enabled;
+	mutex_unlock(&kvm->lock);
+	return 0;
+}
+
 int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm,
 						  struct kvm_enable_cap *cap)
 {
@@ -4295,6 +4307,9 @@ static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
 	}
 	case KVM_CAP_DIRTY_LOG_RING:
 		return kvm_vm_ioctl_enable_dirty_log_ring(kvm, cap->args[0]);
+	case KVM_CAP_DIRTY_QUOTA_MIGRATION:
+		return kvm_vm_ioctl_enable_dirty_quota_migration(kvm,
+				cap->args[0]);
 	default:
 		return kvm_vm_ioctl_enable_cap(kvm, cap);
 	}
Signature · 1 line
2.22.3
#6 lore raw

Re: [PATCH 3/6] Add dirty quota migration capability and handle vCPU page fault for dirty quota context.

Thank you for the patch! Yet something to improve:

Hi Shivam,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on mst-vhost/linux-next]
[also build test ERROR on linus/master v5.15-rc7 next-20211026] [cannot apply to kvm/queue]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Shivam-Kumar/KVM-Dirty-Quota-Based-VM-Live-Migration-Auto-Converge/20211027-003852
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next
config: s390-alldefconfig (attached as .config)
compiler: s390-linux-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/609ed7d1183e7cea180b18da8e7bf137cbcb22d2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Shivam-Kumar/KVM-Dirty-Quota-Based-VM-Live-Migration-Auto-Converge/20211027-003852 git checkout 609ed7d1183e7cea180b18da8e7bf137cbcb22d2
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=s390 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

s390-linux-ld: arch/s390/../../virt/kvm/kvm_main.o: in function `kvm_vcpu_fault':

>> kvm_main.c:(.text+0x4f0): undefined reference to `kvm_dirty_quota_context_get_page'

s390-linux-ld: arch/s390/../../virt/kvm/kvm_main.o: in function `kvm_vm_ioctl_create_vcpu':
kvm_main.c:(.text+0x37ae): undefined reference to `kvm_vcpu_dirty_quota_alloc'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

PATCH 4/6 Increment dirty counter for vmexit due to page write fault.

In reply to Shivam Kumar

For a page write fault or "page dirty", the dirty counter of the corresponding vCPU is incremented.

---
 virt/kvm/kvm_main.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +8 -1
index 95f857c50bf2..c41b85af8682 100644
@@ -3083,8 +3083,15 @@ void mark_page_dirty_in_slot(struct kvm *kvm,
 		if (kvm->dirty_ring_size)
 			kvm_dirty_ring_push(kvm_dirty_ring_get(kvm),
 					    slot, rel_gfn);
-		else
+		else {
+			struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+
+			if (vcpu && vcpu->kvm->dirty_quota_migration_enabled &&
+					vcpu->vCPUdqctx)
+				vcpu->vCPUdqctx->dirty_counter++;
+
 			set_bit_le(rel_gfn, memslot->dirty_bitmap);
+		}
 	}
 }
 EXPORT_SYMBOL_GPL(mark_page_dirty_in_slot);
Signature · 1 line
2.22.3
#8 lore raw

PATCH 5/6 Exit to userspace when dirty quota is full.

In reply to Shivam Kumar

Whenever dirty quota is full (i.e. dirty counter equals dirty quota), control is passed to the QEMU side, through a KVM exit with the custom exit reason KVM_EXIT_DIRTY_QUOTA_FULL, to handle the dirty quota full event.

---
 arch/x86/kvm/x86.c                    | 9 +++++++++
 include/linux/dirty_quota_migration.h | 1 +
 include/uapi/linux/kvm.h              | 1 +
 virt/kvm/dirty_quota_migration.c      | 5 +++++
 4 files changed, 16 insertions(+)
arch/x86/kvm/x86.c +9 -0
index b26647a5ea22..ee9464d71f01 100644
@@ -59,6 +59,7 @@
 #include <linux/mem_encrypt.h>
 #include <linux/entry-kvm.h>
 #include <linux/suspend.h>
+#include <linux/dirty_quota_migration.h>
 
 #include <trace/events/kvm.h>
 
@@ -9843,6 +9844,14 @@ static int vcpu_run(struct kvm_vcpu *vcpu)
 				return r;
 			vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
 		}
+
+		/* check for dirty quota migration exit condition if it is enabled */
+		if (vcpu->kvm->dirty_quota_migration_enabled &&
+				is_dirty_quota_full(vcpu->vCPUdqctx)) {
+			vcpu->run->exit_reason = KVM_EXIT_DIRTY_QUOTA_FULL;
+			r = 0;
+			break;
+		}
 	}
 
 	srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
include/linux/dirty_quota_migration.h +1 -0
index a9a54c38ee54..f343c073f38d 100644
@@ -15,5 +15,6 @@ struct vCPUDirtyQuotaContext {
 int kvm_vcpu_dirty_quota_alloc(struct vCPUDirtyQuotaContext **vCPUdqctx);
 struct page *kvm_dirty_quota_context_get_page(
 		struct vCPUDirtyQuotaContext *vCPUdqctx, u32 offset);
+bool is_dirty_quota_full(struct vCPUDirtyQuotaContext *vCPUdqctx);
 
 #endif  /* DIRTY_QUOTA_MIGRATION_H */
include/uapi/linux/kvm.h +1 -0
index 3649a3bb9bb8..0f04cd99fc8d 100644
@@ -269,6 +269,7 @@ struct kvm_xen_exit {
 #define KVM_EXIT_AP_RESET_HOLD    32
 #define KVM_EXIT_X86_BUS_LOCK     33
 #define KVM_EXIT_XEN              34
+#define KVM_EXIT_DIRTY_QUOTA_FULL 35
 
 /* For KVM_EXIT_INTERNAL_ERROR */
 /* Emulate instruction failed. */
virt/kvm/dirty_quota_migration.c +5 -0
index 7e9ace760939..eeef19347af4 100644
@@ -18,3 +18,8 @@ struct page *kvm_dirty_quota_context_get_page(
 {
 	return vmalloc_to_page((void *)vCPUdqctx + offset * PAGE_SIZE);
 }
+
+bool is_dirty_quota_full(struct vCPUDirtyQuotaContext *vCPUdqctx)
+{
+	return (vCPUdqctx->dirty_counter >= vCPUdqctx->dirty_quota);
+}
Signature · 1 line
2.22.3
#9 lore raw

PATCH 6/6 Free space allocated for the vCPU's dirty quota context upon destroy.

In reply to Shivam Kumar

When the vCPU is destroyed, we must free the space allocated to the dirty quota context for the vCPU.

---
 include/linux/dirty_quota_migration.h | 1 +
 virt/kvm/dirty_quota_migration.c      | 6 ++++++
 virt/kvm/kvm_main.c                   | 2 ++
 3 files changed, 9 insertions(+)
include/linux/dirty_quota_migration.h +1 -0
index f343c073f38d..d3ccab153d44 100644
@@ -16,5 +16,6 @@ int kvm_vcpu_dirty_quota_alloc(struct vCPUDirtyQuotaContext **vCPUdqctx);
 struct page *kvm_dirty_quota_context_get_page(
 		struct vCPUDirtyQuotaContext *vCPUdqctx, u32 offset);
 bool is_dirty_quota_full(struct vCPUDirtyQuotaContext *vCPUdqctx);
+void kvm_vcpu_dirty_quota_free(struct vCPUDirtyQuotaContext **vCPUdqctx);
 
 #endif  /* DIRTY_QUOTA_MIGRATION_H */
virt/kvm/dirty_quota_migration.c +6 -0
index eeef19347af4..3f74af2ccab9 100644
@@ -23,3 +23,9 @@ bool is_dirty_quota_full(struct vCPUDirtyQuotaContext *vCPUdqctx)
 {
 	return (vCPUdqctx->dirty_counter >= vCPUdqctx->dirty_quota);
 }
+
+void kvm_vcpu_dirty_quota_free(struct vCPUDirtyQuotaContext **vCPUdqctx)
+{
+	vfree(*vCPUdqctx);
+	*vCPUdqctx = NULL;
+}
virt/kvm/kvm_main.c +2 -0
index c41b85af8682..30fce3f93ce0 100644
@@ -430,6 +430,7 @@ static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
 
 void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
+	kvm_vcpu_dirty_quota_free(&vcpu->vCPUdqctx);
 	kvm_dirty_ring_free(&vcpu->dirty_ring);
 	kvm_arch_vcpu_destroy(vcpu);
 
@@ -3683,6 +3684,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 
 unlock_vcpu_destroy:
 	mutex_unlock(&kvm->lock);
+	kvm_vcpu_dirty_quota_free(&vcpu->vCPUdqctx);
 	kvm_dirty_ring_free(&vcpu->dirty_ring);
 arch_vcpu_destroy:
 	kvm_arch_vcpu_destroy(vcpu);
Signature · 1 line
2.22.3