Merge tag 'net-pull-request' of https://github.com/jasowang/qemu into staging
[qemu/ar7.git] / migration / postcopy-ram.c
blobb9a37ef25581e499c456be7341b44ec702d3ac14
1 /*
2 * Postcopy migration for RAM
4 * Copyright 2013-2015 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Dave Gilbert <dgilbert@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
15 * Postcopy is a migration technique where the execution flips from the
16 * source to the destination before all the data has been copied.
19 #include "qemu/osdep.h"
20 #include "qemu/rcu.h"
21 #include "qemu/madvise.h"
22 #include "exec/target_page.h"
23 #include "migration.h"
24 #include "qemu-file.h"
25 #include "savevm.h"
26 #include "postcopy-ram.h"
27 #include "ram.h"
28 #include "qapi/error.h"
29 #include "qemu/notify.h"
30 #include "qemu/rcu.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/error-report.h"
33 #include "trace.h"
34 #include "hw/boards.h"
35 #include "exec/ramblock.h"
36 #include "socket.h"
37 #include "qemu-file.h"
38 #include "yank_functions.h"
39 #include "tls.h"
41 /* Arbitrary limit on size of each discard command,
42 * keeps them around ~200 bytes
44 #define MAX_DISCARDS_PER_COMMAND 12
46 struct PostcopyDiscardState {
47 const char *ramblock_name;
48 uint16_t cur_entry;
50 * Start and length of a discard range (bytes)
52 uint64_t start_list[MAX_DISCARDS_PER_COMMAND];
53 uint64_t length_list[MAX_DISCARDS_PER_COMMAND];
54 unsigned int nsentwords;
55 unsigned int nsentcmds;
58 static NotifierWithReturnList postcopy_notifier_list;
60 void postcopy_infrastructure_init(void)
62 notifier_with_return_list_init(&postcopy_notifier_list);
65 void postcopy_add_notifier(NotifierWithReturn *nn)
67 notifier_with_return_list_add(&postcopy_notifier_list, nn);
70 void postcopy_remove_notifier(NotifierWithReturn *n)
72 notifier_with_return_remove(n);
75 int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp)
77 struct PostcopyNotifyData pnd;
78 pnd.reason = reason;
79 pnd.errp = errp;
81 return notifier_with_return_list_notify(&postcopy_notifier_list,
82 &pnd);
86 * NOTE: this routine is not thread safe, we can't call it concurrently. But it
87 * should be good enough for migration's purposes.
89 void postcopy_thread_create(MigrationIncomingState *mis,
90 QemuThread *thread, const char *name,
91 void *(*fn)(void *), int joinable)
93 qemu_sem_init(&mis->thread_sync_sem, 0);
94 qemu_thread_create(thread, name, fn, mis, joinable);
95 qemu_sem_wait(&mis->thread_sync_sem);
96 qemu_sem_destroy(&mis->thread_sync_sem);
99 /* Postcopy needs to detect accesses to pages that haven't yet been copied
100 * across, and efficiently map new pages in, the techniques for doing this
101 * are target OS specific.
103 #if defined(__linux__)
105 #include <poll.h>
106 #include <sys/ioctl.h>
107 #include <sys/syscall.h>
108 #include <asm/types.h> /* for __u64 */
109 #endif
111 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
112 #include <sys/eventfd.h>
113 #include <linux/userfaultfd.h>
115 typedef struct PostcopyBlocktimeContext {
116 /* time when page fault initiated per vCPU */
117 uint32_t *page_fault_vcpu_time;
118 /* page address per vCPU */
119 uintptr_t *vcpu_addr;
120 uint32_t total_blocktime;
121 /* blocktime per vCPU */
122 uint32_t *vcpu_blocktime;
123 /* point in time when last page fault was initiated */
124 uint32_t last_begin;
125 /* number of vCPU are suspended */
126 int smp_cpus_down;
127 uint64_t start_time;
130 * Handler for exit event, necessary for
131 * releasing whole blocktime_ctx
133 Notifier exit_notifier;
134 } PostcopyBlocktimeContext;
136 static void destroy_blocktime_context(struct PostcopyBlocktimeContext *ctx)
138 g_free(ctx->page_fault_vcpu_time);
139 g_free(ctx->vcpu_addr);
140 g_free(ctx->vcpu_blocktime);
141 g_free(ctx);
144 static void migration_exit_cb(Notifier *n, void *data)
146 PostcopyBlocktimeContext *ctx = container_of(n, PostcopyBlocktimeContext,
147 exit_notifier);
148 destroy_blocktime_context(ctx);
151 static struct PostcopyBlocktimeContext *blocktime_context_new(void)
153 MachineState *ms = MACHINE(qdev_get_machine());
154 unsigned int smp_cpus = ms->smp.cpus;
155 PostcopyBlocktimeContext *ctx = g_new0(PostcopyBlocktimeContext, 1);
156 ctx->page_fault_vcpu_time = g_new0(uint32_t, smp_cpus);
157 ctx->vcpu_addr = g_new0(uintptr_t, smp_cpus);
158 ctx->vcpu_blocktime = g_new0(uint32_t, smp_cpus);
160 ctx->exit_notifier.notify = migration_exit_cb;
161 ctx->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
162 qemu_add_exit_notifier(&ctx->exit_notifier);
163 return ctx;
166 static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
168 MachineState *ms = MACHINE(qdev_get_machine());
169 uint32List *list = NULL;
170 int i;
172 for (i = ms->smp.cpus - 1; i >= 0; i--) {
173 QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
176 return list;
180 * This function just populates MigrationInfo from postcopy's
181 * blocktime context. It will not populate MigrationInfo,
182 * unless postcopy-blocktime capability was set.
184 * @info: pointer to MigrationInfo to populate
186 void fill_destination_postcopy_migration_info(MigrationInfo *info)
188 MigrationIncomingState *mis = migration_incoming_get_current();
189 PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
191 if (!bc) {
192 return;
195 info->has_postcopy_blocktime = true;
196 info->postcopy_blocktime = bc->total_blocktime;
197 info->has_postcopy_vcpu_blocktime = true;
198 info->postcopy_vcpu_blocktime = get_vcpu_blocktime_list(bc);
201 static uint32_t get_postcopy_total_blocktime(void)
203 MigrationIncomingState *mis = migration_incoming_get_current();
204 PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
206 if (!bc) {
207 return 0;
210 return bc->total_blocktime;
214 * receive_ufd_features: check userfault fd features, to request only supported
215 * features in the future.
217 * Returns: true on success
219 * __NR_userfaultfd - should be checked before
220 * @features: out parameter will contain uffdio_api.features provided by kernel
221 * in case of success
223 static bool receive_ufd_features(uint64_t *features)
225 struct uffdio_api api_struct = {0};
226 int ufd;
227 bool ret = true;
229 /* if we are here __NR_userfaultfd should exists */
230 ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
231 if (ufd == -1) {
232 error_report("%s: syscall __NR_userfaultfd failed: %s", __func__,
233 strerror(errno));
234 return false;
237 /* ask features */
238 api_struct.api = UFFD_API;
239 api_struct.features = 0;
240 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
241 error_report("%s: UFFDIO_API failed: %s", __func__,
242 strerror(errno));
243 ret = false;
244 goto release_ufd;
247 *features = api_struct.features;
249 release_ufd:
250 close(ufd);
251 return ret;
255 * request_ufd_features: this function should be called only once on a newly
256 * opened ufd, subsequent calls will lead to error.
258 * Returns: true on success
260 * @ufd: fd obtained from userfaultfd syscall
261 * @features: bit mask see UFFD_API_FEATURES
263 static bool request_ufd_features(int ufd, uint64_t features)
265 struct uffdio_api api_struct = {0};
266 uint64_t ioctl_mask;
268 api_struct.api = UFFD_API;
269 api_struct.features = features;
270 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
271 error_report("%s failed: UFFDIO_API failed: %s", __func__,
272 strerror(errno));
273 return false;
276 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
277 (__u64)1 << _UFFDIO_UNREGISTER;
278 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
279 error_report("Missing userfault features: %" PRIx64,
280 (uint64_t)(~api_struct.ioctls & ioctl_mask));
281 return false;
284 return true;
287 static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
289 uint64_t asked_features = 0;
290 static uint64_t supported_features;
293 * it's not possible to
294 * request UFFD_API twice per one fd
295 * userfault fd features is persistent
297 if (!supported_features) {
298 if (!receive_ufd_features(&supported_features)) {
299 error_report("%s failed", __func__);
300 return false;
304 #ifdef UFFD_FEATURE_THREAD_ID
305 if (UFFD_FEATURE_THREAD_ID & supported_features) {
306 asked_features |= UFFD_FEATURE_THREAD_ID;
307 if (migrate_postcopy_blocktime()) {
308 if (!mis->blocktime_ctx) {
309 mis->blocktime_ctx = blocktime_context_new();
313 #endif
316 * request features, even if asked_features is 0, due to
317 * kernel expects UFFD_API before UFFDIO_REGISTER, per
318 * userfault file descriptor
320 if (!request_ufd_features(ufd, asked_features)) {
321 error_report("%s failed: features %" PRIu64, __func__,
322 asked_features);
323 return false;
326 if (qemu_real_host_page_size() != ram_pagesize_summary()) {
327 bool have_hp = false;
328 /* We've got a huge page */
329 #ifdef UFFD_FEATURE_MISSING_HUGETLBFS
330 have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
331 #endif
332 if (!have_hp) {
333 error_report("Userfault on this host does not support huge pages");
334 return false;
337 return true;
340 /* Callback from postcopy_ram_supported_by_host block iterator.
342 static int test_ramblock_postcopiable(RAMBlock *rb, void *opaque)
344 const char *block_name = qemu_ram_get_idstr(rb);
345 ram_addr_t length = qemu_ram_get_used_length(rb);
346 size_t pagesize = qemu_ram_pagesize(rb);
348 if (length % pagesize) {
349 error_report("Postcopy requires RAM blocks to be a page size multiple,"
350 " block %s is 0x" RAM_ADDR_FMT " bytes with a "
351 "page size of 0x%zx", block_name, length, pagesize);
352 return 1;
354 return 0;
358 * Note: This has the side effect of munlock'ing all of RAM, that's
359 * normally fine since if the postcopy succeeds it gets turned back on at the
360 * end.
362 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
364 long pagesize = qemu_real_host_page_size();
365 int ufd = -1;
366 bool ret = false; /* Error unless we change it */
367 void *testarea = NULL;
368 struct uffdio_register reg_struct;
369 struct uffdio_range range_struct;
370 uint64_t feature_mask;
371 Error *local_err = NULL;
373 if (qemu_target_page_size() > pagesize) {
374 error_report("Target page size bigger than host page size");
375 goto out;
378 ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
379 if (ufd == -1) {
380 error_report("%s: userfaultfd not available: %s", __func__,
381 strerror(errno));
382 goto out;
385 /* Give devices a chance to object */
386 if (postcopy_notify(POSTCOPY_NOTIFY_PROBE, &local_err)) {
387 error_report_err(local_err);
388 goto out;
391 /* Version and features check */
392 if (!ufd_check_and_apply(ufd, mis)) {
393 goto out;
396 /* We don't support postcopy with shared RAM yet */
397 if (foreach_not_ignored_block(test_ramblock_postcopiable, NULL)) {
398 goto out;
402 * userfault and mlock don't go together; we'll put it back later if
403 * it was enabled.
405 if (munlockall()) {
406 error_report("%s: munlockall: %s", __func__, strerror(errno));
407 goto out;
411 * We need to check that the ops we need are supported on anon memory
412 * To do that we need to register a chunk and see the flags that
413 * are returned.
415 testarea = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE |
416 MAP_ANONYMOUS, -1, 0);
417 if (testarea == MAP_FAILED) {
418 error_report("%s: Failed to map test area: %s", __func__,
419 strerror(errno));
420 goto out;
422 g_assert(QEMU_PTR_IS_ALIGNED(testarea, pagesize));
424 reg_struct.range.start = (uintptr_t)testarea;
425 reg_struct.range.len = pagesize;
426 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
428 if (ioctl(ufd, UFFDIO_REGISTER, &reg_struct)) {
429 error_report("%s userfault register: %s", __func__, strerror(errno));
430 goto out;
433 range_struct.start = (uintptr_t)testarea;
434 range_struct.len = pagesize;
435 if (ioctl(ufd, UFFDIO_UNREGISTER, &range_struct)) {
436 error_report("%s userfault unregister: %s", __func__, strerror(errno));
437 goto out;
440 feature_mask = (__u64)1 << _UFFDIO_WAKE |
441 (__u64)1 << _UFFDIO_COPY |
442 (__u64)1 << _UFFDIO_ZEROPAGE;
443 if ((reg_struct.ioctls & feature_mask) != feature_mask) {
444 error_report("Missing userfault map features: %" PRIx64,
445 (uint64_t)(~reg_struct.ioctls & feature_mask));
446 goto out;
449 /* Success! */
450 ret = true;
451 out:
452 if (testarea) {
453 munmap(testarea, pagesize);
455 if (ufd != -1) {
456 close(ufd);
458 return ret;
462 * Setup an area of RAM so that it *can* be used for postcopy later; this
463 * must be done right at the start prior to pre-copy.
464 * opaque should be the MIS.
466 static int init_range(RAMBlock *rb, void *opaque)
468 const char *block_name = qemu_ram_get_idstr(rb);
469 void *host_addr = qemu_ram_get_host_addr(rb);
470 ram_addr_t offset = qemu_ram_get_offset(rb);
471 ram_addr_t length = qemu_ram_get_used_length(rb);
472 trace_postcopy_init_range(block_name, host_addr, offset, length);
475 * Save the used_length before running the guest. In case we have to
476 * resize RAM blocks when syncing RAM block sizes from the source during
477 * precopy, we'll update it manually via the ram block notifier.
479 rb->postcopy_length = length;
482 * We need the whole of RAM to be truly empty for postcopy, so things
483 * like ROMs and any data tables built during init must be zero'd
484 * - we're going to get the copy from the source anyway.
485 * (Precopy will just overwrite this data, so doesn't need the discard)
487 if (ram_discard_range(block_name, 0, length)) {
488 return -1;
491 return 0;
495 * At the end of migration, undo the effects of init_range
496 * opaque should be the MIS.
498 static int cleanup_range(RAMBlock *rb, void *opaque)
500 const char *block_name = qemu_ram_get_idstr(rb);
501 void *host_addr = qemu_ram_get_host_addr(rb);
502 ram_addr_t offset = qemu_ram_get_offset(rb);
503 ram_addr_t length = rb->postcopy_length;
504 MigrationIncomingState *mis = opaque;
505 struct uffdio_range range_struct;
506 trace_postcopy_cleanup_range(block_name, host_addr, offset, length);
509 * We turned off hugepage for the precopy stage with postcopy enabled
510 * we can turn it back on now.
512 qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE);
515 * We can also turn off userfault now since we should have all the
516 * pages. It can be useful to leave it on to debug postcopy
517 * if you're not sure it's always getting every page.
519 range_struct.start = (uintptr_t)host_addr;
520 range_struct.len = length;
522 if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) {
523 error_report("%s: userfault unregister %s", __func__, strerror(errno));
525 return -1;
528 return 0;
532 * Initialise postcopy-ram, setting the RAM to a state where we can go into
533 * postcopy later; must be called prior to any precopy.
534 * called from arch_init's similarly named ram_postcopy_incoming_init
536 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
538 if (foreach_not_ignored_block(init_range, NULL)) {
539 return -1;
542 return 0;
545 static void postcopy_temp_pages_cleanup(MigrationIncomingState *mis)
547 int i;
549 if (mis->postcopy_tmp_pages) {
550 for (i = 0; i < mis->postcopy_channels; i++) {
551 if (mis->postcopy_tmp_pages[i].tmp_huge_page) {
552 munmap(mis->postcopy_tmp_pages[i].tmp_huge_page,
553 mis->largest_page_size);
554 mis->postcopy_tmp_pages[i].tmp_huge_page = NULL;
557 g_free(mis->postcopy_tmp_pages);
558 mis->postcopy_tmp_pages = NULL;
561 if (mis->postcopy_tmp_zero_page) {
562 munmap(mis->postcopy_tmp_zero_page, mis->largest_page_size);
563 mis->postcopy_tmp_zero_page = NULL;
568 * At the end of a migration where postcopy_ram_incoming_init was called.
570 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
572 trace_postcopy_ram_incoming_cleanup_entry();
574 if (mis->postcopy_prio_thread_created) {
575 qemu_thread_join(&mis->postcopy_prio_thread);
576 mis->postcopy_prio_thread_created = false;
579 if (mis->have_fault_thread) {
580 Error *local_err = NULL;
582 /* Let the fault thread quit */
583 qatomic_set(&mis->fault_thread_quit, 1);
584 postcopy_fault_thread_notify(mis);
585 trace_postcopy_ram_incoming_cleanup_join();
586 qemu_thread_join(&mis->fault_thread);
588 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_END, &local_err)) {
589 error_report_err(local_err);
590 return -1;
593 if (foreach_not_ignored_block(cleanup_range, mis)) {
594 return -1;
597 trace_postcopy_ram_incoming_cleanup_closeuf();
598 close(mis->userfault_fd);
599 close(mis->userfault_event_fd);
600 mis->have_fault_thread = false;
603 if (enable_mlock) {
604 if (os_mlock() < 0) {
605 error_report("mlock: %s", strerror(errno));
607 * It doesn't feel right to fail at this point, we have a valid
608 * VM state.
613 postcopy_temp_pages_cleanup(mis);
615 trace_postcopy_ram_incoming_cleanup_blocktime(
616 get_postcopy_total_blocktime());
618 trace_postcopy_ram_incoming_cleanup_exit();
619 return 0;
623 * Disable huge pages on an area
625 static int nhp_range(RAMBlock *rb, void *opaque)
627 const char *block_name = qemu_ram_get_idstr(rb);
628 void *host_addr = qemu_ram_get_host_addr(rb);
629 ram_addr_t offset = qemu_ram_get_offset(rb);
630 ram_addr_t length = rb->postcopy_length;
631 trace_postcopy_nhp_range(block_name, host_addr, offset, length);
634 * Before we do discards we need to ensure those discards really
635 * do delete areas of the page, even if THP thinks a hugepage would
636 * be a good idea, so force hugepages off.
638 qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE);
640 return 0;
644 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
645 * however leaving it until after precopy means that most of the precopy
646 * data is still THPd
648 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
650 if (foreach_not_ignored_block(nhp_range, mis)) {
651 return -1;
654 postcopy_state_set(POSTCOPY_INCOMING_DISCARD);
656 return 0;
660 * Mark the given area of RAM as requiring notification to unwritten areas
661 * Used as a callback on foreach_not_ignored_block.
662 * host_addr: Base of area to mark
663 * offset: Offset in the whole ram arena
664 * length: Length of the section
665 * opaque: MigrationIncomingState pointer
666 * Returns 0 on success
668 static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
670 MigrationIncomingState *mis = opaque;
671 struct uffdio_register reg_struct;
673 reg_struct.range.start = (uintptr_t)qemu_ram_get_host_addr(rb);
674 reg_struct.range.len = rb->postcopy_length;
675 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
677 /* Now tell our userfault_fd that it's responsible for this area */
678 if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, &reg_struct)) {
679 error_report("%s userfault register: %s", __func__, strerror(errno));
680 return -1;
682 if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
683 error_report("%s userfault: Region doesn't support COPY", __func__);
684 return -1;
686 if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
687 qemu_ram_set_uf_zeroable(rb);
690 return 0;
693 int postcopy_wake_shared(struct PostCopyFD *pcfd,
694 uint64_t client_addr,
695 RAMBlock *rb)
697 size_t pagesize = qemu_ram_pagesize(rb);
698 struct uffdio_range range;
699 int ret;
700 trace_postcopy_wake_shared(client_addr, qemu_ram_get_idstr(rb));
701 range.start = ROUND_DOWN(client_addr, pagesize);
702 range.len = pagesize;
703 ret = ioctl(pcfd->fd, UFFDIO_WAKE, &range);
704 if (ret) {
705 error_report("%s: Failed to wake: %zx in %s (%s)",
706 __func__, (size_t)client_addr, qemu_ram_get_idstr(rb),
707 strerror(errno));
709 return ret;
712 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
713 ram_addr_t start, uint64_t haddr)
715 void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
718 * Discarded pages (via RamDiscardManager) are never migrated. On unlikely
719 * access, place a zeropage, which will also set the relevant bits in the
720 * recv_bitmap accordingly, so we won't try placing a zeropage twice.
722 * Checking a single bit is sufficient to handle pagesize > TPS as either
723 * all relevant bits are set or not.
725 assert(QEMU_IS_ALIGNED(start, qemu_ram_pagesize(rb)));
726 if (ramblock_page_is_discarded(rb, start)) {
727 bool received = ramblock_recv_bitmap_test_byte_offset(rb, start);
729 return received ? 0 : postcopy_place_page_zero(mis, aligned, rb);
732 return migrate_send_rp_req_pages(mis, rb, start, haddr);
736 * Callback from shared fault handlers to ask for a page,
737 * the page must be specified by a RAMBlock and an offset in that rb
738 * Note: Only for use by shared fault handlers (in fault thread)
740 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
741 uint64_t client_addr, uint64_t rb_offset)
743 uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
744 MigrationIncomingState *mis = migration_incoming_get_current();
746 trace_postcopy_request_shared_page(pcfd->idstr, qemu_ram_get_idstr(rb),
747 rb_offset);
748 if (ramblock_recv_bitmap_test_byte_offset(rb, aligned_rbo)) {
749 trace_postcopy_request_shared_page_present(pcfd->idstr,
750 qemu_ram_get_idstr(rb), rb_offset);
751 return postcopy_wake_shared(pcfd, client_addr, rb);
753 postcopy_request_page(mis, rb, aligned_rbo, client_addr);
754 return 0;
757 static int get_mem_fault_cpu_index(uint32_t pid)
759 CPUState *cpu_iter;
761 CPU_FOREACH(cpu_iter) {
762 if (cpu_iter->thread_id == pid) {
763 trace_get_mem_fault_cpu_index(cpu_iter->cpu_index, pid);
764 return cpu_iter->cpu_index;
767 trace_get_mem_fault_cpu_index(-1, pid);
768 return -1;
771 static uint32_t get_low_time_offset(PostcopyBlocktimeContext *dc)
773 int64_t start_time_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
774 dc->start_time;
775 return start_time_offset < 1 ? 1 : start_time_offset & UINT32_MAX;
779 * This function is being called when pagefault occurs. It
780 * tracks down vCPU blocking time.
782 * @addr: faulted host virtual address
783 * @ptid: faulted process thread id
784 * @rb: ramblock appropriate to addr
786 static void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
787 RAMBlock *rb)
789 int cpu, already_received;
790 MigrationIncomingState *mis = migration_incoming_get_current();
791 PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
792 uint32_t low_time_offset;
794 if (!dc || ptid == 0) {
795 return;
797 cpu = get_mem_fault_cpu_index(ptid);
798 if (cpu < 0) {
799 return;
802 low_time_offset = get_low_time_offset(dc);
803 if (dc->vcpu_addr[cpu] == 0) {
804 qatomic_inc(&dc->smp_cpus_down);
807 qatomic_xchg(&dc->last_begin, low_time_offset);
808 qatomic_xchg(&dc->page_fault_vcpu_time[cpu], low_time_offset);
809 qatomic_xchg(&dc->vcpu_addr[cpu], addr);
812 * check it here, not at the beginning of the function,
813 * due to, check could occur early than bitmap_set in
814 * qemu_ufd_copy_ioctl
816 already_received = ramblock_recv_bitmap_test(rb, (void *)addr);
817 if (already_received) {
818 qatomic_xchg(&dc->vcpu_addr[cpu], 0);
819 qatomic_xchg(&dc->page_fault_vcpu_time[cpu], 0);
820 qatomic_dec(&dc->smp_cpus_down);
822 trace_mark_postcopy_blocktime_begin(addr, dc, dc->page_fault_vcpu_time[cpu],
823 cpu, already_received);
827 * This function just provide calculated blocktime per cpu and trace it.
828 * Total blocktime is calculated in mark_postcopy_blocktime_end.
831 * Assume we have 3 CPU
833 * S1 E1 S1 E1
834 * -----***********------------xxx***************------------------------> CPU1
836 * S2 E2
837 * ------------****************xxx---------------------------------------> CPU2
839 * S3 E3
840 * ------------------------****xxx********-------------------------------> CPU3
842 * We have sequence S1,S2,E1,S3,S1,E2,E3,E1
843 * S2,E1 - doesn't match condition due to sequence S1,S2,E1 doesn't include CPU3
844 * S3,S1,E2 - sequence includes all CPUs, in this case overlap will be S1,E2 -
845 * it's a part of total blocktime.
846 * S1 - here is last_begin
847 * Legend of the picture is following:
848 * * - means blocktime per vCPU
849 * x - means overlapped blocktime (total blocktime)
851 * @addr: host virtual address
853 static void mark_postcopy_blocktime_end(uintptr_t addr)
855 MigrationIncomingState *mis = migration_incoming_get_current();
856 PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
857 MachineState *ms = MACHINE(qdev_get_machine());
858 unsigned int smp_cpus = ms->smp.cpus;
859 int i, affected_cpu = 0;
860 bool vcpu_total_blocktime = false;
861 uint32_t read_vcpu_time, low_time_offset;
863 if (!dc) {
864 return;
867 low_time_offset = get_low_time_offset(dc);
868 /* lookup cpu, to clear it,
869 * that algorithm looks straightforward, but it's not
870 * optimal, more optimal algorithm is keeping tree or hash
871 * where key is address value is a list of */
872 for (i = 0; i < smp_cpus; i++) {
873 uint32_t vcpu_blocktime = 0;
875 read_vcpu_time = qatomic_fetch_add(&dc->page_fault_vcpu_time[i], 0);
876 if (qatomic_fetch_add(&dc->vcpu_addr[i], 0) != addr ||
877 read_vcpu_time == 0) {
878 continue;
880 qatomic_xchg(&dc->vcpu_addr[i], 0);
881 vcpu_blocktime = low_time_offset - read_vcpu_time;
882 affected_cpu += 1;
883 /* we need to know is that mark_postcopy_end was due to
884 * faulted page, another possible case it's prefetched
885 * page and in that case we shouldn't be here */
886 if (!vcpu_total_blocktime &&
887 qatomic_fetch_add(&dc->smp_cpus_down, 0) == smp_cpus) {
888 vcpu_total_blocktime = true;
890 /* continue cycle, due to one page could affect several vCPUs */
891 dc->vcpu_blocktime[i] += vcpu_blocktime;
894 qatomic_sub(&dc->smp_cpus_down, affected_cpu);
895 if (vcpu_total_blocktime) {
896 dc->total_blocktime += low_time_offset - qatomic_fetch_add(
897 &dc->last_begin, 0);
899 trace_mark_postcopy_blocktime_end(addr, dc, dc->total_blocktime,
900 affected_cpu);
903 static void postcopy_pause_fault_thread(MigrationIncomingState *mis)
905 trace_postcopy_pause_fault_thread();
906 qemu_sem_wait(&mis->postcopy_pause_sem_fault);
907 trace_postcopy_pause_fault_thread_continued();
911 * Handle faults detected by the USERFAULT markings
913 static void *postcopy_ram_fault_thread(void *opaque)
915 MigrationIncomingState *mis = opaque;
916 struct uffd_msg msg;
917 int ret;
918 size_t index;
919 RAMBlock *rb = NULL;
921 trace_postcopy_ram_fault_thread_entry();
922 rcu_register_thread();
923 mis->last_rb = NULL; /* last RAMBlock we sent part of */
924 qemu_sem_post(&mis->thread_sync_sem);
926 struct pollfd *pfd;
927 size_t pfd_len = 2 + mis->postcopy_remote_fds->len;
929 pfd = g_new0(struct pollfd, pfd_len);
931 pfd[0].fd = mis->userfault_fd;
932 pfd[0].events = POLLIN;
933 pfd[1].fd = mis->userfault_event_fd;
934 pfd[1].events = POLLIN; /* Waiting for eventfd to go positive */
935 trace_postcopy_ram_fault_thread_fds_core(pfd[0].fd, pfd[1].fd);
936 for (index = 0; index < mis->postcopy_remote_fds->len; index++) {
937 struct PostCopyFD *pcfd = &g_array_index(mis->postcopy_remote_fds,
938 struct PostCopyFD, index);
939 pfd[2 + index].fd = pcfd->fd;
940 pfd[2 + index].events = POLLIN;
941 trace_postcopy_ram_fault_thread_fds_extra(2 + index, pcfd->idstr,
942 pcfd->fd);
945 while (true) {
946 ram_addr_t rb_offset;
947 int poll_result;
950 * We're mainly waiting for the kernel to give us a faulting HVA,
951 * however we can be told to quit via userfault_quit_fd which is
952 * an eventfd
955 poll_result = poll(pfd, pfd_len, -1 /* Wait forever */);
956 if (poll_result == -1) {
957 error_report("%s: userfault poll: %s", __func__, strerror(errno));
958 break;
961 if (!mis->to_src_file) {
963 * Possibly someone tells us that the return path is
964 * broken already using the event. We should hold until
965 * the channel is rebuilt.
967 postcopy_pause_fault_thread(mis);
970 if (pfd[1].revents) {
971 uint64_t tmp64 = 0;
973 /* Consume the signal */
974 if (read(mis->userfault_event_fd, &tmp64, 8) != 8) {
975 /* Nothing obviously nicer than posting this error. */
976 error_report("%s: read() failed", __func__);
979 if (qatomic_read(&mis->fault_thread_quit)) {
980 trace_postcopy_ram_fault_thread_quit();
981 break;
985 if (pfd[0].revents) {
986 poll_result--;
987 ret = read(mis->userfault_fd, &msg, sizeof(msg));
988 if (ret != sizeof(msg)) {
989 if (errno == EAGAIN) {
991 * if a wake up happens on the other thread just after
992 * the poll, there is nothing to read.
994 continue;
996 if (ret < 0) {
997 error_report("%s: Failed to read full userfault "
998 "message: %s",
999 __func__, strerror(errno));
1000 break;
1001 } else {
1002 error_report("%s: Read %d bytes from userfaultfd "
1003 "expected %zd",
1004 __func__, ret, sizeof(msg));
1005 break; /* Lost alignment, don't know what we'd read next */
1008 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1009 error_report("%s: Read unexpected event %ud from userfaultfd",
1010 __func__, msg.event);
1011 continue; /* It's not a page fault, shouldn't happen */
1014 rb = qemu_ram_block_from_host(
1015 (void *)(uintptr_t)msg.arg.pagefault.address,
1016 true, &rb_offset);
1017 if (!rb) {
1018 error_report("postcopy_ram_fault_thread: Fault outside guest: %"
1019 PRIx64, (uint64_t)msg.arg.pagefault.address);
1020 break;
1023 rb_offset = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
1024 trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
1025 qemu_ram_get_idstr(rb),
1026 rb_offset,
1027 msg.arg.pagefault.feat.ptid);
1028 mark_postcopy_blocktime_begin(
1029 (uintptr_t)(msg.arg.pagefault.address),
1030 msg.arg.pagefault.feat.ptid, rb);
1032 retry:
1034 * Send the request to the source - we want to request one
1035 * of our host page sizes (which is >= TPS)
1037 ret = postcopy_request_page(mis, rb, rb_offset,
1038 msg.arg.pagefault.address);
1039 if (ret) {
1040 /* May be network failure, try to wait for recovery */
1041 postcopy_pause_fault_thread(mis);
1042 goto retry;
1046 /* Now handle any requests from external processes on shared memory */
1047 /* TODO: May need to handle devices deregistering during postcopy */
1048 for (index = 2; index < pfd_len && poll_result; index++) {
1049 if (pfd[index].revents) {
1050 struct PostCopyFD *pcfd =
1051 &g_array_index(mis->postcopy_remote_fds,
1052 struct PostCopyFD, index - 2);
1054 poll_result--;
1055 if (pfd[index].revents & POLLERR) {
1056 error_report("%s: POLLERR on poll %zd fd=%d",
1057 __func__, index, pcfd->fd);
1058 pfd[index].events = 0;
1059 continue;
1062 ret = read(pcfd->fd, &msg, sizeof(msg));
1063 if (ret != sizeof(msg)) {
1064 if (errno == EAGAIN) {
1066 * if a wake up happens on the other thread just after
1067 * the poll, there is nothing to read.
1069 continue;
1071 if (ret < 0) {
1072 error_report("%s: Failed to read full userfault "
1073 "message: %s (shared) revents=%d",
1074 __func__, strerror(errno),
1075 pfd[index].revents);
1076 /*TODO: Could just disable this sharer */
1077 break;
1078 } else {
1079 error_report("%s: Read %d bytes from userfaultfd "
1080 "expected %zd (shared)",
1081 __func__, ret, sizeof(msg));
1082 /*TODO: Could just disable this sharer */
1083 break; /*Lost alignment,don't know what we'd read next*/
1086 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1087 error_report("%s: Read unexpected event %ud "
1088 "from userfaultfd (shared)",
1089 __func__, msg.event);
1090 continue; /* It's not a page fault, shouldn't happen */
1092 /* Call the device handler registered with us */
1093 ret = pcfd->handler(pcfd, &msg);
1094 if (ret) {
1095 error_report("%s: Failed to resolve shared fault on %zd/%s",
1096 __func__, index, pcfd->idstr);
1097 /* TODO: Fail? Disable this sharer? */
1102 rcu_unregister_thread();
1103 trace_postcopy_ram_fault_thread_exit();
1104 g_free(pfd);
1105 return NULL;
1108 static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
1110 PostcopyTmpPage *tmp_page;
1111 int err, i, channels;
1112 void *temp_page;
1114 if (migrate_postcopy_preempt()) {
1115 /* If preemption enabled, need extra channel for urgent requests */
1116 mis->postcopy_channels = RAM_CHANNEL_MAX;
1117 } else {
1118 /* Both precopy/postcopy on the same channel */
1119 mis->postcopy_channels = 1;
1122 channels = mis->postcopy_channels;
1123 mis->postcopy_tmp_pages = g_malloc0_n(sizeof(PostcopyTmpPage), channels);
1125 for (i = 0; i < channels; i++) {
1126 tmp_page = &mis->postcopy_tmp_pages[i];
1127 temp_page = mmap(NULL, mis->largest_page_size, PROT_READ | PROT_WRITE,
1128 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1129 if (temp_page == MAP_FAILED) {
1130 err = errno;
1131 error_report("%s: Failed to map postcopy_tmp_pages[%d]: %s",
1132 __func__, i, strerror(err));
1133 /* Clean up will be done later */
1134 return -err;
1136 tmp_page->tmp_huge_page = temp_page;
1137 /* Initialize default states for each tmp page */
1138 postcopy_temp_page_reset(tmp_page);
1142 * Map large zero page when kernel can't use UFFDIO_ZEROPAGE for hugepages
1144 mis->postcopy_tmp_zero_page = mmap(NULL, mis->largest_page_size,
1145 PROT_READ | PROT_WRITE,
1146 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1147 if (mis->postcopy_tmp_zero_page == MAP_FAILED) {
1148 err = errno;
1149 mis->postcopy_tmp_zero_page = NULL;
1150 error_report("%s: Failed to map large zero page %s",
1151 __func__, strerror(err));
1152 return -err;
1155 memset(mis->postcopy_tmp_zero_page, '\0', mis->largest_page_size);
1157 return 0;
1160 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1162 /* Open the fd for the kernel to give us userfaults */
1163 mis->userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
1164 if (mis->userfault_fd == -1) {
1165 error_report("%s: Failed to open userfault fd: %s", __func__,
1166 strerror(errno));
1167 return -1;
1171 * Although the host check already tested the API, we need to
1172 * do the check again as an ABI handshake on the new fd.
1174 if (!ufd_check_and_apply(mis->userfault_fd, mis)) {
1175 return -1;
1178 /* Now an eventfd we use to tell the fault-thread to quit */
1179 mis->userfault_event_fd = eventfd(0, EFD_CLOEXEC);
1180 if (mis->userfault_event_fd == -1) {
1181 error_report("%s: Opening userfault_event_fd: %s", __func__,
1182 strerror(errno));
1183 close(mis->userfault_fd);
1184 return -1;
1187 postcopy_thread_create(mis, &mis->fault_thread, "fault-default",
1188 postcopy_ram_fault_thread, QEMU_THREAD_JOINABLE);
1189 mis->have_fault_thread = true;
1191 /* Mark so that we get notified of accesses to unwritten areas */
1192 if (foreach_not_ignored_block(ram_block_enable_notify, mis)) {
1193 error_report("ram_block_enable_notify failed");
1194 return -1;
1197 if (postcopy_temp_pages_setup(mis)) {
1198 /* Error dumped in the sub-function */
1199 return -1;
1202 if (migrate_postcopy_preempt()) {
1204 * This thread needs to be created after the temp pages because
1205 * it'll fetch RAM_CHANNEL_POSTCOPY PostcopyTmpPage immediately.
1207 postcopy_thread_create(mis, &mis->postcopy_prio_thread, "fault-fast",
1208 postcopy_preempt_thread, QEMU_THREAD_JOINABLE);
1209 mis->postcopy_prio_thread_created = true;
1212 trace_postcopy_ram_enable_notify();
1214 return 0;
1217 static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
1218 void *from_addr, uint64_t pagesize, RAMBlock *rb)
1220 int userfault_fd = mis->userfault_fd;
1221 int ret;
1223 if (from_addr) {
1224 struct uffdio_copy copy_struct;
1225 copy_struct.dst = (uint64_t)(uintptr_t)host_addr;
1226 copy_struct.src = (uint64_t)(uintptr_t)from_addr;
1227 copy_struct.len = pagesize;
1228 copy_struct.mode = 0;
1229 ret = ioctl(userfault_fd, UFFDIO_COPY, &copy_struct);
1230 } else {
1231 struct uffdio_zeropage zero_struct;
1232 zero_struct.range.start = (uint64_t)(uintptr_t)host_addr;
1233 zero_struct.range.len = pagesize;
1234 zero_struct.mode = 0;
1235 ret = ioctl(userfault_fd, UFFDIO_ZEROPAGE, &zero_struct);
1237 if (!ret) {
1238 qemu_mutex_lock(&mis->page_request_mutex);
1239 ramblock_recv_bitmap_set_range(rb, host_addr,
1240 pagesize / qemu_target_page_size());
1242 * If this page resolves a page fault for a previous recorded faulted
1243 * address, take a special note to maintain the requested page list.
1245 if (g_tree_lookup(mis->page_requested, host_addr)) {
1246 g_tree_remove(mis->page_requested, host_addr);
1247 mis->page_requested_count--;
1248 trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
1250 qemu_mutex_unlock(&mis->page_request_mutex);
1251 mark_postcopy_blocktime_end((uintptr_t)host_addr);
1253 return ret;
1256 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
1258 int i;
1259 MigrationIncomingState *mis = migration_incoming_get_current();
1260 GArray *pcrfds = mis->postcopy_remote_fds;
1262 for (i = 0; i < pcrfds->len; i++) {
1263 struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1264 int ret = cur->waker(cur, rb, offset);
1265 if (ret) {
1266 return ret;
1269 return 0;
1273 * Place a host page (from) at (host) atomically
1274 * returns 0 on success
1276 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1277 RAMBlock *rb)
1279 size_t pagesize = qemu_ram_pagesize(rb);
1281 /* copy also acks to the kernel waking the stalled thread up
1282 * TODO: We can inhibit that ack and only do it if it was requested
1283 * which would be slightly cheaper, but we'd have to be careful
1284 * of the order of updating our page state.
1286 if (qemu_ufd_copy_ioctl(mis, host, from, pagesize, rb)) {
1287 int e = errno;
1288 error_report("%s: %s copy host: %p from: %p (size: %zd)",
1289 __func__, strerror(e), host, from, pagesize);
1291 return -e;
1294 trace_postcopy_place_page(host);
1295 return postcopy_notify_shared_wake(rb,
1296 qemu_ram_block_host_offset(rb, host));
1300 * Place a zero page at (host) atomically
1301 * returns 0 on success
1303 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1304 RAMBlock *rb)
1306 size_t pagesize = qemu_ram_pagesize(rb);
1307 trace_postcopy_place_page_zero(host);
1309 /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
1310 * but it's not available for everything (e.g. hugetlbpages)
1312 if (qemu_ram_is_uf_zeroable(rb)) {
1313 if (qemu_ufd_copy_ioctl(mis, host, NULL, pagesize, rb)) {
1314 int e = errno;
1315 error_report("%s: %s zero host: %p",
1316 __func__, strerror(e), host);
1318 return -e;
1320 return postcopy_notify_shared_wake(rb,
1321 qemu_ram_block_host_offset(rb,
1322 host));
1323 } else {
1324 return postcopy_place_page(mis, host, mis->postcopy_tmp_zero_page, rb);
1328 #else
1329 /* No target OS support, stubs just fail */
1330 void fill_destination_postcopy_migration_info(MigrationInfo *info)
1334 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
1336 error_report("%s: No OS support", __func__);
1337 return false;
1340 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1342 error_report("postcopy_ram_incoming_init: No OS support");
1343 return -1;
1346 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
1348 assert(0);
1349 return -1;
1352 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
1354 assert(0);
1355 return -1;
1358 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
1359 uint64_t client_addr, uint64_t rb_offset)
1361 assert(0);
1362 return -1;
1365 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1367 assert(0);
1368 return -1;
1371 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1372 RAMBlock *rb)
1374 assert(0);
1375 return -1;
1378 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1379 RAMBlock *rb)
1381 assert(0);
1382 return -1;
1385 int postcopy_wake_shared(struct PostCopyFD *pcfd,
1386 uint64_t client_addr,
1387 RAMBlock *rb)
1389 assert(0);
1390 return -1;
1392 #endif
1394 /* ------------------------------------------------------------------------- */
1395 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page)
1397 tmp_page->target_pages = 0;
1398 tmp_page->host_addr = NULL;
1400 * This is set to true when reset, and cleared as long as we received any
1401 * of the non-zero small page within this huge page.
1403 tmp_page->all_zero = true;
1406 void postcopy_fault_thread_notify(MigrationIncomingState *mis)
1408 uint64_t tmp64 = 1;
1411 * Wakeup the fault_thread. It's an eventfd that should currently
1412 * be at 0, we're going to increment it to 1
1414 if (write(mis->userfault_event_fd, &tmp64, 8) != 8) {
1415 /* Not much we can do here, but may as well report it */
1416 error_report("%s: incrementing failed: %s", __func__,
1417 strerror(errno));
1422 * postcopy_discard_send_init: Called at the start of each RAMBlock before
1423 * asking to discard individual ranges.
1425 * @ms: The current migration state.
1426 * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
1427 * @name: RAMBlock that discards will operate on.
1429 static PostcopyDiscardState pds = {0};
1430 void postcopy_discard_send_init(MigrationState *ms, const char *name)
1432 pds.ramblock_name = name;
1433 pds.cur_entry = 0;
1434 pds.nsentwords = 0;
1435 pds.nsentcmds = 0;
1439 * postcopy_discard_send_range: Called by the bitmap code for each chunk to
1440 * discard. May send a discard message, may just leave it queued to
1441 * be sent later.
1443 * @ms: Current migration state.
1444 * @start,@length: a range of pages in the migration bitmap in the
1445 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
1447 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
1448 unsigned long length)
1450 size_t tp_size = qemu_target_page_size();
1451 /* Convert to byte offsets within the RAM block */
1452 pds.start_list[pds.cur_entry] = start * tp_size;
1453 pds.length_list[pds.cur_entry] = length * tp_size;
1454 trace_postcopy_discard_send_range(pds.ramblock_name, start, length);
1455 pds.cur_entry++;
1456 pds.nsentwords++;
1458 if (pds.cur_entry == MAX_DISCARDS_PER_COMMAND) {
1459 /* Full set, ship it! */
1460 qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
1461 pds.ramblock_name,
1462 pds.cur_entry,
1463 pds.start_list,
1464 pds.length_list);
1465 pds.nsentcmds++;
1466 pds.cur_entry = 0;
1471 * postcopy_discard_send_finish: Called at the end of each RAMBlock by the
1472 * bitmap code. Sends any outstanding discard messages, frees the PDS
1474 * @ms: Current migration state.
1476 void postcopy_discard_send_finish(MigrationState *ms)
1478 /* Anything unsent? */
1479 if (pds.cur_entry) {
1480 qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
1481 pds.ramblock_name,
1482 pds.cur_entry,
1483 pds.start_list,
1484 pds.length_list);
1485 pds.nsentcmds++;
1488 trace_postcopy_discard_send_finish(pds.ramblock_name, pds.nsentwords,
1489 pds.nsentcmds);
1493 * Current state of incoming postcopy; note this is not part of
1494 * MigrationIncomingState since it's state is used during cleanup
1495 * at the end as MIS is being freed.
1497 static PostcopyState incoming_postcopy_state;
1499 PostcopyState postcopy_state_get(void)
1501 return qatomic_mb_read(&incoming_postcopy_state);
1504 /* Set the state and return the old state */
1505 PostcopyState postcopy_state_set(PostcopyState new_state)
1507 return qatomic_xchg(&incoming_postcopy_state, new_state);
1510 /* Register a handler for external shared memory postcopy
1511 * called on the destination.
1513 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
1515 MigrationIncomingState *mis = migration_incoming_get_current();
1517 mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
1518 *pcfd);
1521 /* Unregister a handler for external shared memory postcopy
1523 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
1525 guint i;
1526 MigrationIncomingState *mis = migration_incoming_get_current();
1527 GArray *pcrfds = mis->postcopy_remote_fds;
1529 if (!pcrfds) {
1530 /* migration has already finished and freed the array */
1531 return;
1533 for (i = 0; i < pcrfds->len; i++) {
1534 struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1535 if (cur->fd == pcfd->fd) {
1536 mis->postcopy_remote_fds = g_array_remove_index(pcrfds, i);
1537 return;
1542 bool postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file)
1545 * The new loading channel has its own threads, so it needs to be
1546 * blocked too. It's by default true, just be explicit.
1548 qemu_file_set_blocking(file, true);
1549 mis->postcopy_qemufile_dst = file;
1550 trace_postcopy_preempt_new_channel();
1552 /* Start the migration immediately */
1553 return true;
1557 * Setup the postcopy preempt channel with the IOC. If ERROR is specified,
1558 * setup the error instead. This helper will free the ERROR if specified.
1560 static void
1561 postcopy_preempt_send_channel_done(MigrationState *s,
1562 QIOChannel *ioc, Error *local_err)
1564 if (local_err) {
1565 migrate_set_error(s, local_err);
1566 error_free(local_err);
1567 } else {
1568 migration_ioc_register_yank(ioc);
1569 s->postcopy_qemufile_src = qemu_file_new_output(ioc);
1570 trace_postcopy_preempt_new_channel();
1574 * Kick the waiter in all cases. The waiter should check upon
1575 * postcopy_qemufile_src to know whether it failed or not.
1577 qemu_sem_post(&s->postcopy_qemufile_src_sem);
1580 static void
1581 postcopy_preempt_tls_handshake(QIOTask *task, gpointer opaque)
1583 g_autoptr(QIOChannel) ioc = QIO_CHANNEL(qio_task_get_source(task));
1584 MigrationState *s = opaque;
1585 Error *local_err = NULL;
1587 qio_task_propagate_error(task, &local_err);
1588 postcopy_preempt_send_channel_done(s, ioc, local_err);
1591 static void
1592 postcopy_preempt_send_channel_new(QIOTask *task, gpointer opaque)
1594 g_autoptr(QIOChannel) ioc = QIO_CHANNEL(qio_task_get_source(task));
1595 MigrationState *s = opaque;
1596 QIOChannelTLS *tioc;
1597 Error *local_err = NULL;
1599 if (qio_task_propagate_error(task, &local_err)) {
1600 goto out;
1603 if (migrate_channel_requires_tls_upgrade(ioc)) {
1604 tioc = migration_tls_client_create(s, ioc, s->hostname, &local_err);
1605 if (!tioc) {
1606 goto out;
1608 trace_postcopy_preempt_tls_handshake();
1609 qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-preempt");
1610 qio_channel_tls_handshake(tioc, postcopy_preempt_tls_handshake,
1611 s, NULL, NULL);
1612 /* Setup the channel until TLS handshake finished */
1613 return;
1616 out:
1617 /* This handles both good and error cases */
1618 postcopy_preempt_send_channel_done(s, ioc, local_err);
1621 /* Returns 0 if channel established, -1 for error. */
1622 int postcopy_preempt_wait_channel(MigrationState *s)
1624 /* If preempt not enabled, no need to wait */
1625 if (!migrate_postcopy_preempt()) {
1626 return 0;
1630 * We need the postcopy preempt channel to be established before
1631 * starting doing anything.
1633 qemu_sem_wait(&s->postcopy_qemufile_src_sem);
1635 return s->postcopy_qemufile_src ? 0 : -1;
1638 int postcopy_preempt_setup(MigrationState *s, Error **errp)
1640 if (!migrate_postcopy_preempt()) {
1641 return 0;
1644 if (!migrate_multi_channels_is_allowed()) {
1645 error_setg(errp, "Postcopy preempt is not supported as current "
1646 "migration stream does not support multi-channels.");
1647 return -1;
1650 /* Kick an async task to connect */
1651 socket_send_channel_create(postcopy_preempt_send_channel_new, s);
1653 return 0;
1656 static void postcopy_pause_ram_fast_load(MigrationIncomingState *mis)
1658 trace_postcopy_pause_fast_load();
1659 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
1660 qemu_sem_wait(&mis->postcopy_pause_sem_fast_load);
1661 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
1662 trace_postcopy_pause_fast_load_continued();
1665 void *postcopy_preempt_thread(void *opaque)
1667 MigrationIncomingState *mis = opaque;
1668 int ret;
1670 trace_postcopy_preempt_thread_entry();
1672 rcu_register_thread();
1674 qemu_sem_post(&mis->thread_sync_sem);
1676 /* Sending RAM_SAVE_FLAG_EOS to terminate this thread */
1677 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
1678 while (1) {
1679 ret = ram_load_postcopy(mis->postcopy_qemufile_dst,
1680 RAM_CHANNEL_POSTCOPY);
1681 /* If error happened, go into recovery routine */
1682 if (ret) {
1683 postcopy_pause_ram_fast_load(mis);
1684 } else {
1685 /* We're done */
1686 break;
1689 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
1691 rcu_unregister_thread();
1693 trace_postcopy_preempt_thread_exit();
1695 return NULL;