accel/mshv: enable dirty page tracking
This change introduces the functions required to perform dirty page tracking to speed up migrations. We are using the sync, global_start, and global_stop hooks. The sync is implemented in batches. Before we can disable the dirty page tracking we have to set all dirty bits. Signed-off-by: Magnus Kulke <magnuskulke@linux.microsoft.com> Link: https://lore.kernel.org/r/20260417105618.3621-35-magnuskulke@linux.microsoft.com Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Magnus Kulke committed
Apr 17, 2026 at 12:56 UTC
f629431c09736cf1de900c0a1e08be157663f96d
3 files changed
+218
accel/mshv/mem.c
+211
@@ -12,10 +12,13 @@
12
13
#include "qemu/osdep.h"
14
#include "qemu/error-report.h"
15
+#include "qapi/error.h"
16
#include "linux/mshv.h"
17
#include "system/address-spaces.h"
18
#include "system/mshv.h"
19
#include "system/mshv_int.h"
20
+#include "hw/hyperv/hvhdk_mini.h"
21
+#include "system/physmem.h"
22
#include "exec/memattrs.h"
23
#include <sys/ioctl.h>
24
#include "trace.h"
@@ -211,3 +214,211 @@ void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *section,
214
abort();
215
}
216
}
217
+
218
+static int enable_dirty_page_tracking(int vm_fd)
219
+{
220
+ int ret;
221
+ struct hv_input_set_partition_property in = {0};
222
+ struct mshv_root_hvcall args = {0};
223
+
224
+ in.property_code = HV_PARTITION_PROPERTY_GPA_PAGE_ACCESS_TRACKING;
225
+ in.property_value = 1;
226
+
227
+ args.code = HVCALL_SET_PARTITION_PROPERTY;
228
+ args.in_sz = sizeof(in);
229
+ args.in_ptr = (uint64_t)∈
230
+
231
+ ret = mshv_hvcall(vm_fd, &args);
232
+ if (ret < 0) {
233
+ error_report("Failed to enable dirty page tracking: %s",
234
+ strerror(errno));
235
+ return -1;
236
+ }
237
+
238
+ return 0;
239
+}
240
+
241
+/*
242
+ * Retrieve dirty page bitmap for a GPA range, clearing the dirty bits
243
+ * atomically. Large ranges are handled in batches.
244
+ */
245
+static int get_dirty_log(int vm_fd, uint64_t base_pfn, uint64_t page_count,
246
+ unsigned long *bitmap, size_t bitmap_size)
247
+{
248
+ uint64_t batch, bitmap_offset, completed = 0;
249
+ struct mshv_gpap_access_bitmap args = {0};
250
+ int ret;
251
+
252
+ QEMU_BUILD_BUG_ON(MSHV_DIRTY_PAGES_BATCH_SIZE % BITS_PER_LONG != 0);
253
+ assert(bitmap_size >= ROUND_UP(page_count, BITS_PER_LONG) / 8);
254
+
255
+ while (completed < page_count) {
256
+ batch = MIN(MSHV_DIRTY_PAGES_BATCH_SIZE, page_count - completed);
257
+ bitmap_offset = completed / BITS_PER_LONG;
258
+
259
+ args.access_type = MSHV_GPAP_ACCESS_TYPE_DIRTY;
260
+ args.access_op = MSHV_GPAP_ACCESS_OP_CLEAR;
261
+ args.page_count = batch;
262
+ args.gpap_base = base_pfn + completed;
263
+ args.bitmap_ptr = (uint64_t)(bitmap + bitmap_offset);
264
+
265
+ ret = ioctl(vm_fd, MSHV_GET_GPAP_ACCESS_BITMAP, &args);
266
+ if (ret < 0) {
267
+ error_report("Failed to get dirty log (base_pfn=0x%" PRIx64
268
+ " batch=%" PRIu64 "): %s",
269
+ base_pfn + completed, batch, strerror(errno));
270
+ return -1;
271
+ }
272
+ completed += batch;
273
+ }
274
+
275
+ return 0;
276
+}
277
+
278
+bool mshv_log_global_start(MemoryListener *listener, Error **errp)
279
+{
280
+ int ret;
281
+
282
+ ret = enable_dirty_page_tracking(mshv_state->vm);
283
+ if (ret < 0) {
284
+ error_setg_errno(errp, -ret, "Failed to enable dirty page tracking");
285
+ return false;
286
+ }
287
+ return true;
288
+}
289
+
290
+static int disable_dirty_page_tracking(int vm_fd)
291
+{
292
+ int ret;
293
+ struct hv_input_set_partition_property in = {0};
294
+ struct mshv_root_hvcall args = {0};
295
+
296
+ in.property_code = HV_PARTITION_PROPERTY_GPA_PAGE_ACCESS_TRACKING;
297
+ in.property_value = 0;
298
+
299
+ args.code = HVCALL_SET_PARTITION_PROPERTY;
300
+ args.in_sz = sizeof(in);
301
+ args.in_ptr = (uint64_t)∈
302
+
303
+ ret = mshv_hvcall(vm_fd, &args);
304
+ if (ret < 0) {
305
+ error_report("Failed to disable dirty page tracking: %s",
306
+ strerror(errno));
307
+ return -1;
308
+ }
309
+
310
+ return 0;
311
+}
312
+
313
+static int set_dirty_pages(int vm_fd, uint64_t base_pfn, uint64_t page_count)
314
+{
315
+ uint64_t batch, completed = 0;
316
+ unsigned long bitmap[MSHV_DIRTY_PAGES_BATCH_SIZE / BITS_PER_LONG];
317
+ struct mshv_gpap_access_bitmap args = {0};
318
+ int ret;
319
+
320
+ while (completed < page_count) {
321
+ batch = MIN(MSHV_DIRTY_PAGES_BATCH_SIZE, page_count - completed);
322
+
323
+ args.access_type = MSHV_GPAP_ACCESS_TYPE_DIRTY;
324
+ args.access_op = MSHV_GPAP_ACCESS_OP_SET;
325
+ args.page_count = batch;
326
+ args.gpap_base = base_pfn + completed;
327
+ args.bitmap_ptr = (uint64_t)bitmap;
328
+
329
+ ret = ioctl(vm_fd, MSHV_GET_GPAP_ACCESS_BITMAP, &args);
330
+ if (ret < 0) {
331
+ error_report("Failed to set dirty pages (base_pfn=0x%" PRIx64
332
+ " batch=%" PRIu64 "): %s",
333
+ base_pfn + completed, batch, strerror(errno));
334
+ return -1;
335
+ }
336
+ completed += batch;
337
+ }
338
+
339
+ return 0;
340
+}
341
+
342
+static bool set_dirty_bits_cb(Int128 start, Int128 len, const MemoryRegion *mr,
343
+ hwaddr offset_in_region, void *opaque)
344
+{
345
+ int ret, *errp = opaque;
346
+ hwaddr gpa, size;
347
+ uint64_t page_count, base_pfn;
348
+
349
+ gpa = int128_get64(start);
350
+ size = int128_get64(len);
351
+ page_count = size >> MSHV_PAGE_SHIFT;
352
+ base_pfn = gpa >> MSHV_PAGE_SHIFT;
353
+
354
+ if (!mr->ram || mr->readonly) {
355
+ return false;
356
+ }
357
+
358
+ if (page_count == 0) {
359
+ return false;
360
+ }
361
+
362
+ ret = set_dirty_pages(mshv_state->vm, base_pfn, page_count);
363
+
364
+ /* true aborts the iteration, which is what we want if there's an error */
365
+ if (ret < 0) {
366
+ *errp = ret;
367
+ return true;
368
+ }
369
+
370
+ return false;
371
+}
372
+
373
+void mshv_log_global_stop(MemoryListener *listener)
374
+{
375
+ int err = 0;
376
+ /* MSHV requires all dirty bits to be set before disabling tracking. */
377
+ FlatView *fv = address_space_to_flatview(&address_space_memory);
378
+ flatview_for_each_range(fv, set_dirty_bits_cb, &err);
379
+
380
+ if (err < 0) {
381
+ error_report("Failed to set dirty bits before disabling tracking");
382
+ }
383
+
384
+ disable_dirty_page_tracking(mshv_state->vm);
385
+}
386
+
387
+void mshv_log_sync(MemoryListener *listener, MemoryRegionSection *section)
388
+{
389
+ hwaddr size, start_addr, mr_offset;
390
+ uint64_t page_count, base_pfn;
391
+ size_t bitmap_size;
392
+ unsigned long *bitmap;
393
+ ram_addr_t ram_addr;
394
+ int ret;
395
+ MemoryRegion *mr = section->mr;
396
+
397
+ if (!memory_region_is_ram(mr) || memory_region_is_rom(mr)) {
398
+ return;
399
+ }
400
+
401
+ size = align_section(section, &start_addr);
402
+ if (!size) {
403
+ return;
404
+ }
405
+
406
+ page_count = size >> MSHV_PAGE_SHIFT;
407
+ base_pfn = start_addr >> MSHV_PAGE_SHIFT;
408
+ bitmap_size = ROUND_UP(page_count, BITS_PER_LONG) / 8;
409
+ bitmap = g_malloc0(bitmap_size);
410
+
411
+ ret = get_dirty_log(mshv_state->vm, base_pfn, page_count, bitmap,
412
+ bitmap_size);
413
+ if (ret < 0) {
414
+ g_free(bitmap);
415
+ return;
416
+ }
417
+
418
+ mr_offset = section->offset_within_region + start_addr -
419
+ section->offset_within_address_space;
420
+ ram_addr = memory_region_get_ram_addr(mr) + mr_offset;
421
+
422
+ physical_memory_set_dirty_lebitmap(bitmap, ram_addr, page_count);
423
+ g_free(bitmap);
424
+}
accel/mshv/mshv-all.c
+3
@@ -477,6 +477,9 @@ static MemoryListener mshv_memory_listener = {
477
.region_del = mem_region_del,
478
.eventfd_add = mem_ioeventfd_add,
479
.eventfd_del = mem_ioeventfd_del,
480
+ .log_sync = mshv_log_sync,
481
+ .log_global_start = mshv_log_global_start,
482
+ .log_global_stop = mshv_log_global_stop,
483
};
484
485
static MemoryListener mshv_io_listener = {
include/system/mshv_int.h
+4
@@ -17,6 +17,7 @@
17
#include "hw/hyperv/hvhdk.h"
18
19
#define MSHV_MSR_ENTRIES_COUNT 64
20
+#define MSHV_DIRTY_PAGES_BATCH_SIZE 0x10000
21
22
typedef struct hyperv_message hv_message;
23
@@ -109,6 +110,9 @@ int mshv_guest_mem_write(uint64_t gpa, const uint8_t *data, uintptr_t size,
110
bool is_secure_mode);
111
void mshv_set_phys_mem(MshvMemoryListener *mml, MemoryRegionSection *section,
112
bool add);
113
+void mshv_log_sync(MemoryListener *listener, MemoryRegionSection *section);
114
+bool mshv_log_global_start(MemoryListener *listener, Error **errp);
115
+void mshv_log_global_stop(MemoryListener *listener);
116
117
/* msr */
118
typedef struct MshvMsrEntry {