Skip to contentSkip to conversation

KVM: For manual-protect GET_DIRTY_LOG, do not hold slots lock

PATCH In review James Houghton 3 participants 5 messages Sep 30, 2025 – Oct 8, 2025

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

PATCH 1/2 KVM: For manual-protect GET_DIRTY_LOG, do not hold slots lock

For users that have enabled manual-protect, holding the srcu lock instead of the slots lock allows KVM to copy the dirty bitmap for multiple memslots in parallel.

Userspace can take advantage of this by creating multiple memslots and calling GET_DIRTY_LOG on all of them at the same time, reducing the dirty log collection time.

For VM live migration, the final dirty memory state can only be collected after the VM has been paused (blackout). We can resume the VM on the target host without this bitmap, but doing so requires the post-copy implementation to assume that nothing is clean; this is very slow. By being able to receive the bitmap quicker, VM responsiveness improves dramatically.

On 12TiB Cascade Lake hosts, we observe GET_DIRTY_LOG times of about 25-40ms for each memslot when splitting the memslots 8 ways. This patch reduces the total wall time spent calling GET_DIRTY_LOG from ~300ms to ~40ms. This means that the dirty log can be transferred to the target ~250ms faster, which is a significant responsiveness improvement. It takes about 800ms to send the bitmap to the target today, so the 250ms improvement represents a ~20% reduction in total time spent without the dirty bitmap.

The bits that must be safe are:
1. The copy_to_user() to store the bitmap
2. kvm_arch_sync_dirty_log()

(1) is trivially safe.

(2) kvm_arch_sync_dirty_log() is non-trivially implemented for x86 and s390. s390 does not set KVM_GENERIC_DIRTYLOG_READ_PROTECT, so the optimization here does not apply. On x86, parallelization is safe. The extra vCPU kicks that come from having more memslots should not be an issue for the final dirty logging pass (the one I care about most here), as vCPUs will have been kicked out to userspace at that point.

$ ./dirty_log_perf_test -x 8 -b 512G -s anonymous_hugetlb_1gb # serial
Iteration 1 get dirty log time: 0.004699057s
Iteration 2 get dirty log time: 0.003918316s
Iteration 3 get dirty log time: 0.003903790s
Iteration 4 get dirty log time: 0.003944732s
Iteration 5 get dirty log time: 0.003885857s
$ ./dirty_log_perf_test -x 8 -b 512G -s anonymous_hugetlb_1gb # parallel
Iteration 1 get dirty log time: 0.002352174s
Iteration 2 get dirty log time: 0.001064265s
Iteration 3 get dirty log time: 0.001102144s
Iteration 4 get dirty log time: 0.000960649s
Iteration 5 get dirty log time: 0.000972533s

So with 8 memslots, we get about a 4x reduction on this platform (Skylake).

---
 include/linux/kvm_dirty_ring.h |  4 +-
 virt/kvm/dirty_ring.c          | 11 +++++-
 virt/kvm/kvm_main.c            | 68 +++++++++++++++++++++++-----------
 3 files changed, 58 insertions(+), 25 deletions(-)
include/linux/kvm_dirty_ring.h +2 -2
index eb10d87adf7d..b7d18abec4d0 100644
@@ -37,7 +37,7 @@ static inline u32 kvm_dirty_ring_get_rsvd_entries(struct kvm *kvm)
 	return 0;
 }
 
-static inline bool kvm_use_dirty_bitmap(struct kvm *kvm)
+static inline bool kvm_use_dirty_bitmap(struct kvm *kvm, bool shared)
 {
 	return true;
 }
@@ -73,7 +73,7 @@ static inline void kvm_dirty_ring_free(struct kvm_dirty_ring *ring)
 #else /* CONFIG_HAVE_KVM_DIRTY_RING */
 
 int kvm_cpu_dirty_log_size(struct kvm *kvm);
-bool kvm_use_dirty_bitmap(struct kvm *kvm);
+bool kvm_use_dirty_bitmap(struct kvm *kvm, bool shared);
 bool kvm_arch_allow_write_without_running_vcpu(struct kvm *kvm);
 u32 kvm_dirty_ring_get_rsvd_entries(struct kvm *kvm);
 int kvm_dirty_ring_alloc(struct kvm *kvm, struct kvm_dirty_ring *ring,
virt/kvm/dirty_ring.c +9 -2
index 02bc6b00d76c..6662c302a3fa 100644
@@ -21,9 +21,16 @@ u32 kvm_dirty_ring_get_rsvd_entries(struct kvm *kvm)
 	return KVM_DIRTY_RING_RSVD_ENTRIES + kvm_cpu_dirty_log_size(kvm);
 }
 
-bool kvm_use_dirty_bitmap(struct kvm *kvm)
+bool kvm_use_dirty_bitmap(struct kvm *kvm, bool shared)
 {
-	lockdep_assert_held(&kvm->slots_lock);
+	if (shared)
+		/*
+		 * In the shared mode, racing with dirty log mode changes is
+		 * tolerated.
+		 */
+		lockdep_assert_held(&kvm->srcu);
+	else
+		lockdep_assert_held(&kvm->slots_lock);
 
 	return !kvm->dirty_ring_size || kvm->dirty_ring_with_bitmap;
 }
virt/kvm/kvm_main.c +47 -21

129 lines not on this page · raw message

base-commit: a6ad54137af92535cfe32e19e5f3bc1bb7dbd383

Signature · 1 line
2.51.0.618.g983fd99d29-goog
#1 lore raw

PATCH 2/2 KVM: selftests: Add parallel KVM_GET_DIRTY_LOG to dirty_log_perf_test

The parallelism is by memslot. This is useful because KVM no longer serializes KVM_GET_DIRTY_LOG if KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is enabled.

---
 .../selftests/kvm/dirty_log_perf_test.c       | 20 ++++++++--
 .../testing/selftests/kvm/include/memstress.h |  2 +
 tools/testing/selftests/kvm/lib/kvm_util.c    |  2 +
 tools/testing/selftests/kvm/lib/memstress.c   | 40 +++++++++++++++++++
 4 files changed, 61 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/dirty_log_perf_test.c +17 -3
index e79817bd0e29..8a5f289c4966 100644
@@ -131,8 +131,18 @@ struct test_params {
 	int slots;
 	uint32_t write_percent;
 	bool random_access;
+	bool parallel_get_dirty_log;
 };
 
+static void get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
+			  struct test_params *p)
+{
+	if (p->parallel_get_dirty_log)
+		memstress_get_dirty_log_parallel(vm, bitmaps, p->slots);
+	else
+		memstress_get_dirty_log(vm, bitmaps, p->slots);
+}
+
 static void run_test(enum vm_guest_mode mode, void *arg)
 {
 	struct test_params *p = arg;
@@ -230,7 +240,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 			iteration, ts_diff.tv_sec, ts_diff.tv_nsec);
 
 		clock_gettime(CLOCK_MONOTONIC, &start);
-		memstress_get_dirty_log(vm, bitmaps, p->slots);
+		get_dirty_log(vm, bitmaps, p);
 		ts_diff = timespec_elapsed(start);
 		get_dirty_log_total = timespec_add(get_dirty_log_total,
 						   ts_diff);
@@ -292,7 +302,7 @@ static void run_test(enum vm_guest_mode mode, void *arg)
 static void help(char *name)
 {
 	puts("");
-	printf("usage: %s [-h] [-a] [-i iterations] [-p offset] [-g] "
+	printf("usage: %s [-h] [-a] [-i iterations] [-p offset] [-g] [-l] "
 	       "[-m mode] [-n] [-b vcpu bytes] [-v vcpus] [-o] [-r random seed ] [-s mem type]"
 	       "[-x memslots] [-w percentage] [-c physical cpus to run test on]\n", name);
 	puts("");
@@ -305,6 +315,7 @@ static void help(char *name)
 	       "     and writes will be tracked as soon as dirty logging is\n"
 	       "     enabled on the memslot (i.e. KVM_DIRTY_LOG_INITIALLY_SET\n"
 	       "     is not enabled).\n");
+	printf(" -l: Do KVM_GET_DIRTY_LOG calls for each memslot in parallel.\n");
 	printf(" -p: specify guest physical test memory offset\n"
 	       "     Warning: a low offset can conflict with the loaded test code.\n");
 	guest_modes_help();
@@ -355,7 +366,7 @@ int main(int argc, char *argv[])
 
 	guest_modes_append_default();
 
-	while ((opt = getopt(argc, argv, "ab:c:eghi:m:nop:r:s:v:x:w:")) != -1) {
+	while ((opt = getopt(argc, argv, "ab:c:eghi:lm:nop:r:s:v:x:w:")) != -1) {
 		switch (opt) {
 		case 'a':
 			p.random_access = true;
@@ -379,6 +390,9 @@ int main(int argc, char *argv[])
 		case 'i':
 			p.iterations = atoi_positive("Number of iterations", optarg);
 			break;
+		case 'l':
+			p.parallel_get_dirty_log = true;
+			break;
 		case 'm':
 			guest_modes_cmdline(optarg);
 			break;
tools/testing/selftests/kvm/include/memstress.h +2 -0
index 9071eb6dea60..3e6ad2cdec80 100644
@@ -74,6 +74,8 @@ void memstress_setup_nested(struct kvm_vm *vm, int nr_vcpus, struct kvm_vcpu *vc
 void memstress_enable_dirty_logging(struct kvm_vm *vm, int slots);
 void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots);
 void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots);
+void memstress_get_dirty_log_parallel(struct kvm_vm *vm, unsigned long *bitmaps[],
+				      int slots);
 void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
 			       int slots, uint64_t pages_per_slot);
 unsigned long **memstress_alloc_bitmaps(int slots, uint64_t pages_per_slot);
tools/testing/selftests/kvm/lib/memstress.c +40 -0
index 557c0a0a5658..abbd96a1c3ba 100644
@@ -40,6 +40,12 @@ static bool all_vcpu_threads_running;
 
 static struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
 
+struct get_dirty_log_args {
+	struct kvm_vm *vm;
+	unsigned long *bitmap;
+	int slot;
+};
+
 /*
  * Continuously write to the first 8 bytes of each page in the
  * specified region.
@@ -341,6 +347,15 @@ void memstress_disable_dirty_logging(struct kvm_vm *vm, int slots)
 	toggle_dirty_logging(vm, slots, false);
 }
 
+static void *get_dirty_log_worker(void *arg)
+{
+	struct get_dirty_log_args *args = arg;
+
+	kvm_vm_get_dirty_log(args->vm, args->slot, args->bitmap);
+
+	return NULL;
+}
+
 void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int slots)
 {
 	int i;
@@ -352,6 +367,31 @@ void memstress_get_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[], int sl
 	}
 }
 
+void memstress_get_dirty_log_parallel(struct kvm_vm *vm, unsigned long *bitmaps[],
+				      int slots)
+{
+	struct {
+		pthread_t thd;
+		struct get_dirty_log_args args;
+	} *threads;
+	int i;
+
+	threads = malloc(slots * sizeof(*threads));
+
+	for (i = 0; i < slots; i++) {
+		threads[i].args.vm = vm;
+		threads[i].args.slot = MEMSTRESS_MEM_SLOT_INDEX + i;
+		threads[i].args.bitmap = bitmaps[i];
+		pthread_create(&threads[i].thd, NULL, get_dirty_log_worker,
+			       &threads[i].args);
+	}
+
+	for (i = 0; i < slots; i++)
+		pthread_join(threads[i].thd, NULL);
+
+	free(threads);
+}
+
 void memstress_clear_dirty_log(struct kvm_vm *vm, unsigned long *bitmaps[],
 			       int slots, uint64_t pages_per_slot)
 {
Signature · 1 line
2.51.0.618.g983fd99d29-goog
#2 lore raw
In reply to James Houghton
kernel test robot noticed the following build warnings:

Hi James,

kernel test robot noticed the following build warnings:

[auto build test WARNING on a6ad54137af92535cfe32e19e5f3bc1bb7dbd383]

url:    https://github.com/intel-lab-lkp/linux/commits/James-Houghton/KVM-selftests-Add-parallel-KVM_GET_DIRTY_LOG-to-dirty_log_perf_test/20251001-013306
base:   a6ad54137af92535cfe32e19e5f3bc1bb7dbd383
patch link:    https://lore.kernel.org/r/20250930172850.598938-1-jthoughton%40google.com
patch subject: [PATCH 1/2] KVM: For manual-protect GET_DIRTY_LOG, do not hold slots lock
config: x86_64-buildonly-randconfig-005-20251001 (https://download.01.org/0day-ci/archive/20251001/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251001/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: arch/x86/kvm/../../../virt/kvm/kvm_main.c:2220 function parameter 'protect' not described in 'kvm_get_dirty_log_protect'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
build bot
#3 lore raw
In reply to James Houghton
BCC: [email protected] CC: [email protected] In-Reply-To: <[email protected]> References: <[email protected]> TO: James Houghton…

BCC: [email protected]
CC: [email protected]
In-Reply-To: <[email protected]>
References: <[email protected]>
TO: James Houghton <[email protected]>
TO: Paolo Bonzini <[email protected]>
TO: Sean Christopherson <[email protected]>
CC: James Houghton <[email protected]>
CC: [email protected]
CC: [email protected]

Hi James,

kernel test robot noticed the following build warnings:

[auto build test WARNING on a6ad54137af92535cfe32e19e5f3bc1bb7dbd383]

url:    https://github.com/intel-lab-lkp/linux/commits/James-Houghton/KVM-selftests-Add-parallel-KVM_GET_DIRTY_LOG-to-dirty_log_perf_test/20251001-013306
base:   a6ad54137af92535cfe32e19e5f3bc1bb7dbd383
patch link:    https://lore.kernel.org/r/20250930172850.598938-1-jthoughton%40google.com
patch subject: [PATCH 1/2] KVM: For manual-protect GET_DIRTY_LOG, do not hold slots lock

:::::: branch date: 4 days ago
:::::: commit date: 4 days ago
config: x86_64-randconfig-161-20251004 (https://download.01.org/0day-ci/archive/20251004/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
arch/x86/kvm/../../../virt/kvm/kvm_main.c:2290 kvm_get_dirty_log_protect() error: uninitialized symbol 'flush'.

Old smatch warnings:
arch/x86/kvm/../../../virt/kvm/kvm_main.c:283 kvm_make_all_cpus_request() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:487 kvm_destroy_vcpus() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:1378 kvm_trylock_all_vcpus() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:1384 kvm_trylock_all_vcpus() warn: iterator 'j' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:1401 kvm_lock_all_vcpus() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:1409 kvm_lock_all_vcpus() warn: iterator 'j' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:1425 kvm_unlock_all_vcpus() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:1721 kvm_commit_memory_region() error: we previously assumed 'old' could be null (see line 1714) arch/x86/kvm/../../../virt/kvm/kvm_main.c:1723 kvm_commit_memory_region() error: we previously assumed 'new' could be null (see line 1715) include/linux/kvm_host.h:1012 kvm_get_vcpu_by_id() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:5005 kvm_vm_ioctl_reset_dirty_pages() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:6136 kvm_get_stat_per_vcpu() warn: iterator 'i' not incremented arch/x86/kvm/../../../virt/kvm/kvm_main.c:6147 kvm_clear_stat_per_vcpu() warn: iterator 'i' not incremented

vim +/flush +2290 arch/x86/kvm/../../../virt/kvm/kvm_main.c

6aa8b732ca01c3d drivers/kvm/kvm_main.c Avi Kivity          2006-12-10  2196  
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2197  #else /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2198  /**
b8b002209c06127 virt/kvm/kvm_main.c    Jiang Biao          2019-04-23  2199   * kvm_get_dirty_log_protect - get a snapshot of dirty pages
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2200   *	and reenable dirty page tracking for the corresponding pages.
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2201   * @kvm:	pointer to kvm instance
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2202   * @log:	slot id and address to which we copy the log
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2203   *
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2204   * We need to keep it in mind that VCPU threads can write to the bitmap
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2205   * concurrently. So, to avoid losing track of dirty pages we keep the
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2206   * following order:
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2207   *
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2208   *    1. Take a snapshot of the bit and clear it if needed.
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2209   *    2. Write protect the corresponding page.
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2210   *    3. Copy the snapshot to the userspace.
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2211   *    4. Upon return caller flushes TLB's if needed.
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2212   *
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2213   * Between 2 and 4, the guest may write to the page using the remaining TLB
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2214   * entry.  This is not a problem because the page is reported dirty using
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2215   * the snapshot taken before and step 4 ensures that writes done after
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2216   * exiting to userspace will be logged for the next call.
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2217   *
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2218   */
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2219  static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log,
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2220  				     bool protect)
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2221  {
9f6b8029787bb37 virt/kvm/kvm_main.c    Paolo Bonzini       2015-05-17  2222  	struct kvm_memslots *slots;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2223  	struct kvm_memory_slot *memslot;
58d6db349172786 virt/kvm/kvm_main.c    Markus Elfring      2017-01-22  2224  	int i, as_id, id;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2225  	unsigned long n;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2226  	unsigned long *dirty_bitmap;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2227  	unsigned long *dirty_bitmap_buffer;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2228  
86bdf3ebcfe1ded virt/kvm/kvm_main.c    Gavin Shan          2022-11-10  2229  	/* Dirty ring tracking may be exclusive to dirty log tracking */
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2230  	if (!kvm_use_dirty_bitmap(kvm, !protect))
b2cc64c4f3829c2 virt/kvm/kvm_main.c    Peter Xu            2020-09-30  2231  		return -ENXIO;
b2cc64c4f3829c2 virt/kvm/kvm_main.c    Peter Xu            2020-09-30  2232  
f481b069e674378 virt/kvm/kvm_main.c    Paolo Bonzini       2015-05-17  2233  	as_id = log->slot >> 16;
f481b069e674378 virt/kvm/kvm_main.c    Paolo Bonzini       2015-05-17  2234  	id = (u16)log->slot;
eed52e434bc3360 virt/kvm/kvm_main.c    Sean Christopherson 2023-10-27  2235  	if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS)
58d6db349172786 virt/kvm/kvm_main.c    Markus Elfring      2017-01-22  2236  		return -EINVAL;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2237  
f481b069e674378 virt/kvm/kvm_main.c    Paolo Bonzini       2015-05-17  2238  	slots = __kvm_memslots(kvm, as_id);
f481b069e674378 virt/kvm/kvm_main.c    Paolo Bonzini       2015-05-17  2239  	memslot = id_to_memslot(slots, id);
0577d1abe704c31 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2240  	if (!memslot || !memslot->dirty_bitmap)
0577d1abe704c31 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2241  		return -ENOENT;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2242  
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2243  	dirty_bitmap = memslot->dirty_bitmap;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2244  
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2245  	if (protect)
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2246  		lockdep_assert_held(&kvm->slots_lock);
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2247  	else
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2248  		lockdep_assert_held(&kvm->srcu);
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2249  	/*
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2250  	 * kvm_arch_sync_dirty_log() must be safe to call with either kvm->srcu
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2251  	 * held OR the slots lock held.
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2252  	 */
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2253  	kvm_arch_sync_dirty_log(kvm, memslot);
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2254  
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2255  	n = kvm_dirty_bitmap_bytes(memslot);
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2256  	if (!protect) {
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2257  		/*
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2258  		 * Unlike kvm_get_dirty_log, we never flush, because no flush is
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2259  		 * needed until KVM_CLEAR_DIRTY_LOG.  There is some code
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2260  		 * duplication between this function and kvm_get_dirty_log, but
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2261  		 * hopefully all architecture transition to
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2262  		 * kvm_get_dirty_log_protect and kvm_get_dirty_log can be
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2263  		 * eliminated.
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2264  		 */
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2265  		dirty_bitmap_buffer = dirty_bitmap;
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2266  	} else {
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2267  		bool flush;
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2268  
03133347b4452ef virt/kvm/kvm_main.c    Claudio Imbrenda    2018-04-30  2269  		dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2270  		memset(dirty_bitmap_buffer, 0, n);
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2271  
531810caa9f4bc9 virt/kvm/kvm_main.c    Ben Gardon          2021-02-02  2272  		KVM_MMU_LOCK(kvm);
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2273  		for (i = 0; i < n / sizeof(long); i++) {
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2274  			unsigned long mask;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2275  			gfn_t offset;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2276  
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2277  			if (!dirty_bitmap[i])
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2278  				continue;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2279  
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2280  			flush = true;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2281  			mask = xchg(&dirty_bitmap[i], 0);
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2282  			dirty_bitmap_buffer[i] = mask;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2283  
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2284  			offset = i * BITS_PER_LONG;
58d2930f4ee335a virt/kvm/kvm_main.c    Takuya Yoshikawa    2015-03-17  2285  			kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
58d2930f4ee335a virt/kvm/kvm_main.c    Takuya Yoshikawa    2015-03-17  2286  								offset, mask);
58d2930f4ee335a virt/kvm/kvm_main.c    Takuya Yoshikawa    2015-03-17  2287  		}
531810caa9f4bc9 virt/kvm/kvm_main.c    Ben Gardon          2021-02-02  2288  		KVM_MMU_UNLOCK(kvm);
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2289  
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18 @2290  		if (flush)
619b5072443c05c virt/kvm/kvm_main.c    David Matlack       2023-08-11  2291  			kvm_flush_remote_tlbs_memslot(kvm, memslot);
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2292  	}
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2293  
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2294  	if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
58d6db349172786 virt/kvm/kvm_main.c    Markus Elfring      2017-01-22  2295  		return -EFAULT;
58d6db349172786 virt/kvm/kvm_main.c    Markus Elfring      2017-01-22  2296  	return 0;
ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2297  }
0dff084607bd555 virt/kvm/kvm_main.c    Sean Christopherson 2020-02-18  2298  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#4 lore raw
Quoted 37 lines · Dan Carpenter

Hi James,

kernel test robot noticed the following build warnings:

url:    https://github.com/intel-lab-lkp/linux/commits/James-Houghton/KVM-selftests-Add-parallel-KVM_GET_DIRTY_LOG-to-dirty_log_perf_test/20251001-013306
base:   a6ad54137af92535cfe32e19e5f3bc1bb7dbd383
patch link:    https://lore.kernel.org/r/20250930172850.598938-1-jthoughton%40google.com
patch subject: [PATCH 1/2] KVM: For manual-protect GET_DIRTY_LOG, do not hold slots lock
config: x86_64-randconfig-161-20251004 (https://download.01.org/0day-ci/archive/20251004/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags

| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/

New smatch warnings:
arch/x86/kvm/../../../virt/kvm/kvm_main.c:2290 kvm_get_dirty_log_protect() error: uninitialized symbol 'flush'.

vim +/flush +2290 arch/x86/kvm/../../../virt/kvm/kvm_main.c

ba0513b5b8ffbcb virt/kvm/kvm_main.c    Mario Smarduch      2015-01-15  2255     n = kvm_dirty_bitmap_bytes(memslot);
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2256     if (!protect) {
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2257             /*
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2258              * Unlike kvm_get_dirty_log, we never flush, because no flush is
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2259              * needed until KVM_CLEAR_DIRTY_LOG.  There is some code
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2260              * duplication between this function and kvm_get_dirty_log, but
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2261              * hopefully all architecture transition to
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2262              * kvm_get_dirty_log_protect and kvm_get_dirty_log can be
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2263              * eliminated.
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2264              */
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2265             dirty_bitmap_buffer = dirty_bitmap;
2a31b9db153530d virt/kvm/kvm_main.c    Paolo Bonzini       2018-10-23  2266     } else {
82fb1294f7ad3ee virt/kvm/kvm_main.c    James Houghton      2025-09-30  2267             bool flush;
Quoted 1 line

flush needs to be initialized to false.

I'll fix this and the other bug about not documenting the new parameter, my mistake. :(

I think in a v2 I'll also merge kvm_get_dirty_log() into kvm_get_dirty_log_protect(); might as well.