edk2: .git can be a file
[qemu.git] / migration / postcopy-ram.c
blob32c52f4b1d9ec7d8cb2a1d2679c40601bdef96c0
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"
37 /* Arbitrary limit on size of each discard command,
38 * keeps them around ~200 bytes
40 #define MAX_DISCARDS_PER_COMMAND 12
42 struct PostcopyDiscardState {
43 const char *ramblock_name;
44 uint16_t cur_entry;
46 * Start and length of a discard range (bytes)
48 uint64_t start_list[MAX_DISCARDS_PER_COMMAND];
49 uint64_t length_list[MAX_DISCARDS_PER_COMMAND];
50 unsigned int nsentwords;
51 unsigned int nsentcmds;
54 static NotifierWithReturnList postcopy_notifier_list;
56 void postcopy_infrastructure_init(void)
58 notifier_with_return_list_init(&postcopy_notifier_list);
61 void postcopy_add_notifier(NotifierWithReturn *nn)
63 notifier_with_return_list_add(&postcopy_notifier_list, nn);
66 void postcopy_remove_notifier(NotifierWithReturn *n)
68 notifier_with_return_remove(n);
71 int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp)
73 struct PostcopyNotifyData pnd;
74 pnd.reason = reason;
75 pnd.errp = errp;
77 return notifier_with_return_list_notify(&postcopy_notifier_list,
78 &pnd);
82 * NOTE: this routine is not thread safe, we can't call it concurrently. But it
83 * should be good enough for migration's purposes.
85 void postcopy_thread_create(MigrationIncomingState *mis,
86 QemuThread *thread, const char *name,
87 void *(*fn)(void *), int joinable)
89 qemu_sem_init(&mis->thread_sync_sem, 0);
90 qemu_thread_create(thread, name, fn, mis, joinable);
91 qemu_sem_wait(&mis->thread_sync_sem);
92 qemu_sem_destroy(&mis->thread_sync_sem);
95 /* Postcopy needs to detect accesses to pages that haven't yet been copied
96 * across, and efficiently map new pages in, the techniques for doing this
97 * are target OS specific.
99 #if defined(__linux__)
101 #include <poll.h>
102 #include <sys/ioctl.h>
103 #include <sys/syscall.h>
104 #include <asm/types.h> /* for __u64 */
105 #endif
107 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
108 #include <sys/eventfd.h>
109 #include <linux/userfaultfd.h>
111 typedef struct PostcopyBlocktimeContext {
112 /* time when page fault initiated per vCPU */
113 uint32_t *page_fault_vcpu_time;
114 /* page address per vCPU */
115 uintptr_t *vcpu_addr;
116 uint32_t total_blocktime;
117 /* blocktime per vCPU */
118 uint32_t *vcpu_blocktime;
119 /* point in time when last page fault was initiated */
120 uint32_t last_begin;
121 /* number of vCPU are suspended */
122 int smp_cpus_down;
123 uint64_t start_time;
126 * Handler for exit event, necessary for
127 * releasing whole blocktime_ctx
129 Notifier exit_notifier;
130 } PostcopyBlocktimeContext;
132 static void destroy_blocktime_context(struct PostcopyBlocktimeContext *ctx)
134 g_free(ctx->page_fault_vcpu_time);
135 g_free(ctx->vcpu_addr);
136 g_free(ctx->vcpu_blocktime);
137 g_free(ctx);
140 static void migration_exit_cb(Notifier *n, void *data)
142 PostcopyBlocktimeContext *ctx = container_of(n, PostcopyBlocktimeContext,
143 exit_notifier);
144 destroy_blocktime_context(ctx);
147 static struct PostcopyBlocktimeContext *blocktime_context_new(void)
149 MachineState *ms = MACHINE(qdev_get_machine());
150 unsigned int smp_cpus = ms->smp.cpus;
151 PostcopyBlocktimeContext *ctx = g_new0(PostcopyBlocktimeContext, 1);
152 ctx->page_fault_vcpu_time = g_new0(uint32_t, smp_cpus);
153 ctx->vcpu_addr = g_new0(uintptr_t, smp_cpus);
154 ctx->vcpu_blocktime = g_new0(uint32_t, smp_cpus);
156 ctx->exit_notifier.notify = migration_exit_cb;
157 ctx->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
158 qemu_add_exit_notifier(&ctx->exit_notifier);
159 return ctx;
162 static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
164 MachineState *ms = MACHINE(qdev_get_machine());
165 uint32List *list = NULL;
166 int i;
168 for (i = ms->smp.cpus - 1; i >= 0; i--) {
169 QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
172 return list;
176 * This function just populates MigrationInfo from postcopy's
177 * blocktime context. It will not populate MigrationInfo,
178 * unless postcopy-blocktime capability was set.
180 * @info: pointer to MigrationInfo to populate
182 void fill_destination_postcopy_migration_info(MigrationInfo *info)
184 MigrationIncomingState *mis = migration_incoming_get_current();
185 PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
187 if (!bc) {
188 return;
191 info->has_postcopy_blocktime = true;
192 info->postcopy_blocktime = bc->total_blocktime;
193 info->has_postcopy_vcpu_blocktime = true;
194 info->postcopy_vcpu_blocktime = get_vcpu_blocktime_list(bc);
197 static uint32_t get_postcopy_total_blocktime(void)
199 MigrationIncomingState *mis = migration_incoming_get_current();
200 PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
202 if (!bc) {
203 return 0;
206 return bc->total_blocktime;
210 * receive_ufd_features: check userfault fd features, to request only supported
211 * features in the future.
213 * Returns: true on success
215 * __NR_userfaultfd - should be checked before
216 * @features: out parameter will contain uffdio_api.features provided by kernel
217 * in case of success
219 static bool receive_ufd_features(uint64_t *features)
221 struct uffdio_api api_struct = {0};
222 int ufd;
223 bool ret = true;
225 /* if we are here __NR_userfaultfd should exists */
226 ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
227 if (ufd == -1) {
228 error_report("%s: syscall __NR_userfaultfd failed: %s", __func__,
229 strerror(errno));
230 return false;
233 /* ask features */
234 api_struct.api = UFFD_API;
235 api_struct.features = 0;
236 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
237 error_report("%s: UFFDIO_API failed: %s", __func__,
238 strerror(errno));
239 ret = false;
240 goto release_ufd;
243 *features = api_struct.features;
245 release_ufd:
246 close(ufd);
247 return ret;
251 * request_ufd_features: this function should be called only once on a newly
252 * opened ufd, subsequent calls will lead to error.
254 * Returns: true on success
256 * @ufd: fd obtained from userfaultfd syscall
257 * @features: bit mask see UFFD_API_FEATURES
259 static bool request_ufd_features(int ufd, uint64_t features)
261 struct uffdio_api api_struct = {0};
262 uint64_t ioctl_mask;
264 api_struct.api = UFFD_API;
265 api_struct.features = features;
266 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
267 error_report("%s failed: UFFDIO_API failed: %s", __func__,
268 strerror(errno));
269 return false;
272 ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
273 (__u64)1 << _UFFDIO_UNREGISTER;
274 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
275 error_report("Missing userfault features: %" PRIx64,
276 (uint64_t)(~api_struct.ioctls & ioctl_mask));
277 return false;
280 return true;
283 static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
285 uint64_t asked_features = 0;
286 static uint64_t supported_features;
289 * it's not possible to
290 * request UFFD_API twice per one fd
291 * userfault fd features is persistent
293 if (!supported_features) {
294 if (!receive_ufd_features(&supported_features)) {
295 error_report("%s failed", __func__);
296 return false;
300 #ifdef UFFD_FEATURE_THREAD_ID
301 if (UFFD_FEATURE_THREAD_ID & supported_features) {
302 asked_features |= UFFD_FEATURE_THREAD_ID;
303 if (migrate_postcopy_blocktime()) {
304 if (!mis->blocktime_ctx) {
305 mis->blocktime_ctx = blocktime_context_new();
309 #endif
312 * request features, even if asked_features is 0, due to
313 * kernel expects UFFD_API before UFFDIO_REGISTER, per
314 * userfault file descriptor
316 if (!request_ufd_features(ufd, asked_features)) {
317 error_report("%s failed: features %" PRIu64, __func__,
318 asked_features);
319 return false;
322 if (qemu_real_host_page_size != ram_pagesize_summary()) {
323 bool have_hp = false;
324 /* We've got a huge page */
325 #ifdef UFFD_FEATURE_MISSING_HUGETLBFS
326 have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
327 #endif
328 if (!have_hp) {
329 error_report("Userfault on this host does not support huge pages");
330 return false;
333 return true;
336 /* Callback from postcopy_ram_supported_by_host block iterator.
338 static int test_ramblock_postcopiable(RAMBlock *rb, void *opaque)
340 const char *block_name = qemu_ram_get_idstr(rb);
341 ram_addr_t length = qemu_ram_get_used_length(rb);
342 size_t pagesize = qemu_ram_pagesize(rb);
344 if (length % pagesize) {
345 error_report("Postcopy requires RAM blocks to be a page size multiple,"
346 " block %s is 0x" RAM_ADDR_FMT " bytes with a "
347 "page size of 0x%zx", block_name, length, pagesize);
348 return 1;
350 return 0;
354 * Note: This has the side effect of munlock'ing all of RAM, that's
355 * normally fine since if the postcopy succeeds it gets turned back on at the
356 * end.
358 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
360 long pagesize = qemu_real_host_page_size;
361 int ufd = -1;
362 bool ret = false; /* Error unless we change it */
363 void *testarea = NULL;
364 struct uffdio_register reg_struct;
365 struct uffdio_range range_struct;
366 uint64_t feature_mask;
367 Error *local_err = NULL;
369 if (qemu_target_page_size() > pagesize) {
370 error_report("Target page size bigger than host page size");
371 goto out;
374 ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
375 if (ufd == -1) {
376 error_report("%s: userfaultfd not available: %s", __func__,
377 strerror(errno));
378 goto out;
381 /* Give devices a chance to object */
382 if (postcopy_notify(POSTCOPY_NOTIFY_PROBE, &local_err)) {
383 error_report_err(local_err);
384 goto out;
387 /* Version and features check */
388 if (!ufd_check_and_apply(ufd, mis)) {
389 goto out;
392 /* We don't support postcopy with shared RAM yet */
393 if (foreach_not_ignored_block(test_ramblock_postcopiable, NULL)) {
394 goto out;
398 * userfault and mlock don't go together; we'll put it back later if
399 * it was enabled.
401 if (munlockall()) {
402 error_report("%s: munlockall: %s", __func__, strerror(errno));
403 goto out;
407 * We need to check that the ops we need are supported on anon memory
408 * To do that we need to register a chunk and see the flags that
409 * are returned.
411 testarea = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE |
412 MAP_ANONYMOUS, -1, 0);
413 if (testarea == MAP_FAILED) {
414 error_report("%s: Failed to map test area: %s", __func__,
415 strerror(errno));
416 goto out;
418 g_assert(QEMU_PTR_IS_ALIGNED(testarea, pagesize));
420 reg_struct.range.start = (uintptr_t)testarea;
421 reg_struct.range.len = pagesize;
422 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
424 if (ioctl(ufd, UFFDIO_REGISTER, &reg_struct)) {
425 error_report("%s userfault register: %s", __func__, strerror(errno));
426 goto out;
429 range_struct.start = (uintptr_t)testarea;
430 range_struct.len = pagesize;
431 if (ioctl(ufd, UFFDIO_UNREGISTER, &range_struct)) {
432 error_report("%s userfault unregister: %s", __func__, strerror(errno));
433 goto out;
436 feature_mask = (__u64)1 << _UFFDIO_WAKE |
437 (__u64)1 << _UFFDIO_COPY |
438 (__u64)1 << _UFFDIO_ZEROPAGE;
439 if ((reg_struct.ioctls & feature_mask) != feature_mask) {
440 error_report("Missing userfault map features: %" PRIx64,
441 (uint64_t)(~reg_struct.ioctls & feature_mask));
442 goto out;
445 /* Success! */
446 ret = true;
447 out:
448 if (testarea) {
449 munmap(testarea, pagesize);
451 if (ufd != -1) {
452 close(ufd);
454 return ret;
458 * Setup an area of RAM so that it *can* be used for postcopy later; this
459 * must be done right at the start prior to pre-copy.
460 * opaque should be the MIS.
462 static int init_range(RAMBlock *rb, void *opaque)
464 const char *block_name = qemu_ram_get_idstr(rb);
465 void *host_addr = qemu_ram_get_host_addr(rb);
466 ram_addr_t offset = qemu_ram_get_offset(rb);
467 ram_addr_t length = qemu_ram_get_used_length(rb);
468 trace_postcopy_init_range(block_name, host_addr, offset, length);
471 * Save the used_length before running the guest. In case we have to
472 * resize RAM blocks when syncing RAM block sizes from the source during
473 * precopy, we'll update it manually via the ram block notifier.
475 rb->postcopy_length = length;
478 * We need the whole of RAM to be truly empty for postcopy, so things
479 * like ROMs and any data tables built during init must be zero'd
480 * - we're going to get the copy from the source anyway.
481 * (Precopy will just overwrite this data, so doesn't need the discard)
483 if (ram_discard_range(block_name, 0, length)) {
484 return -1;
487 return 0;
491 * At the end of migration, undo the effects of init_range
492 * opaque should be the MIS.
494 static int cleanup_range(RAMBlock *rb, void *opaque)
496 const char *block_name = qemu_ram_get_idstr(rb);
497 void *host_addr = qemu_ram_get_host_addr(rb);
498 ram_addr_t offset = qemu_ram_get_offset(rb);
499 ram_addr_t length = rb->postcopy_length;
500 MigrationIncomingState *mis = opaque;
501 struct uffdio_range range_struct;
502 trace_postcopy_cleanup_range(block_name, host_addr, offset, length);
505 * We turned off hugepage for the precopy stage with postcopy enabled
506 * we can turn it back on now.
508 qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE);
511 * We can also turn off userfault now since we should have all the
512 * pages. It can be useful to leave it on to debug postcopy
513 * if you're not sure it's always getting every page.
515 range_struct.start = (uintptr_t)host_addr;
516 range_struct.len = length;
518 if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) {
519 error_report("%s: userfault unregister %s", __func__, strerror(errno));
521 return -1;
524 return 0;
528 * Initialise postcopy-ram, setting the RAM to a state where we can go into
529 * postcopy later; must be called prior to any precopy.
530 * called from arch_init's similarly named ram_postcopy_incoming_init
532 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
534 if (foreach_not_ignored_block(init_range, NULL)) {
535 return -1;
538 return 0;
541 static void postcopy_temp_pages_cleanup(MigrationIncomingState *mis)
543 int i;
545 if (mis->postcopy_tmp_pages) {
546 for (i = 0; i < mis->postcopy_channels; i++) {
547 if (mis->postcopy_tmp_pages[i].tmp_huge_page) {
548 munmap(mis->postcopy_tmp_pages[i].tmp_huge_page,
549 mis->largest_page_size);
550 mis->postcopy_tmp_pages[i].tmp_huge_page = NULL;
553 g_free(mis->postcopy_tmp_pages);
554 mis->postcopy_tmp_pages = NULL;
557 if (mis->postcopy_tmp_zero_page) {
558 munmap(mis->postcopy_tmp_zero_page, mis->largest_page_size);
559 mis->postcopy_tmp_zero_page = NULL;
564 * At the end of a migration where postcopy_ram_incoming_init was called.
566 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
568 trace_postcopy_ram_incoming_cleanup_entry();
570 if (mis->have_fault_thread) {
571 Error *local_err = NULL;
573 /* Let the fault thread quit */
574 qatomic_set(&mis->fault_thread_quit, 1);
575 postcopy_fault_thread_notify(mis);
576 trace_postcopy_ram_incoming_cleanup_join();
577 qemu_thread_join(&mis->fault_thread);
579 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_END, &local_err)) {
580 error_report_err(local_err);
581 return -1;
584 if (foreach_not_ignored_block(cleanup_range, mis)) {
585 return -1;
588 trace_postcopy_ram_incoming_cleanup_closeuf();
589 close(mis->userfault_fd);
590 close(mis->userfault_event_fd);
591 mis->have_fault_thread = false;
594 if (enable_mlock) {
595 if (os_mlock() < 0) {
596 error_report("mlock: %s", strerror(errno));
598 * It doesn't feel right to fail at this point, we have a valid
599 * VM state.
604 postcopy_temp_pages_cleanup(mis);
606 trace_postcopy_ram_incoming_cleanup_blocktime(
607 get_postcopy_total_blocktime());
609 trace_postcopy_ram_incoming_cleanup_exit();
610 return 0;
614 * Disable huge pages on an area
616 static int nhp_range(RAMBlock *rb, void *opaque)
618 const char *block_name = qemu_ram_get_idstr(rb);
619 void *host_addr = qemu_ram_get_host_addr(rb);
620 ram_addr_t offset = qemu_ram_get_offset(rb);
621 ram_addr_t length = rb->postcopy_length;
622 trace_postcopy_nhp_range(block_name, host_addr, offset, length);
625 * Before we do discards we need to ensure those discards really
626 * do delete areas of the page, even if THP thinks a hugepage would
627 * be a good idea, so force hugepages off.
629 qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE);
631 return 0;
635 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
636 * however leaving it until after precopy means that most of the precopy
637 * data is still THPd
639 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
641 if (foreach_not_ignored_block(nhp_range, mis)) {
642 return -1;
645 postcopy_state_set(POSTCOPY_INCOMING_DISCARD);
647 return 0;
651 * Mark the given area of RAM as requiring notification to unwritten areas
652 * Used as a callback on foreach_not_ignored_block.
653 * host_addr: Base of area to mark
654 * offset: Offset in the whole ram arena
655 * length: Length of the section
656 * opaque: MigrationIncomingState pointer
657 * Returns 0 on success
659 static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
661 MigrationIncomingState *mis = opaque;
662 struct uffdio_register reg_struct;
664 reg_struct.range.start = (uintptr_t)qemu_ram_get_host_addr(rb);
665 reg_struct.range.len = rb->postcopy_length;
666 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
668 /* Now tell our userfault_fd that it's responsible for this area */
669 if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, &reg_struct)) {
670 error_report("%s userfault register: %s", __func__, strerror(errno));
671 return -1;
673 if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
674 error_report("%s userfault: Region doesn't support COPY", __func__);
675 return -1;
677 if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
678 qemu_ram_set_uf_zeroable(rb);
681 return 0;
684 int postcopy_wake_shared(struct PostCopyFD *pcfd,
685 uint64_t client_addr,
686 RAMBlock *rb)
688 size_t pagesize = qemu_ram_pagesize(rb);
689 struct uffdio_range range;
690 int ret;
691 trace_postcopy_wake_shared(client_addr, qemu_ram_get_idstr(rb));
692 range.start = ROUND_DOWN(client_addr, pagesize);
693 range.len = pagesize;
694 ret = ioctl(pcfd->fd, UFFDIO_WAKE, &range);
695 if (ret) {
696 error_report("%s: Failed to wake: %zx in %s (%s)",
697 __func__, (size_t)client_addr, qemu_ram_get_idstr(rb),
698 strerror(errno));
700 return ret;
703 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
704 ram_addr_t start, uint64_t haddr)
706 void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
709 * Discarded pages (via RamDiscardManager) are never migrated. On unlikely
710 * access, place a zeropage, which will also set the relevant bits in the
711 * recv_bitmap accordingly, so we won't try placing a zeropage twice.
713 * Checking a single bit is sufficient to handle pagesize > TPS as either
714 * all relevant bits are set or not.
716 assert(QEMU_IS_ALIGNED(start, qemu_ram_pagesize(rb)));
717 if (ramblock_page_is_discarded(rb, start)) {
718 bool received = ramblock_recv_bitmap_test_byte_offset(rb, start);
720 return received ? 0 : postcopy_place_page_zero(mis, aligned, rb);
723 return migrate_send_rp_req_pages(mis, rb, start, haddr);
727 * Callback from shared fault handlers to ask for a page,
728 * the page must be specified by a RAMBlock and an offset in that rb
729 * Note: Only for use by shared fault handlers (in fault thread)
731 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
732 uint64_t client_addr, uint64_t rb_offset)
734 uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
735 MigrationIncomingState *mis = migration_incoming_get_current();
737 trace_postcopy_request_shared_page(pcfd->idstr, qemu_ram_get_idstr(rb),
738 rb_offset);
739 if (ramblock_recv_bitmap_test_byte_offset(rb, aligned_rbo)) {
740 trace_postcopy_request_shared_page_present(pcfd->idstr,
741 qemu_ram_get_idstr(rb), rb_offset);
742 return postcopy_wake_shared(pcfd, client_addr, rb);
744 postcopy_request_page(mis, rb, aligned_rbo, client_addr);
745 return 0;
748 static int get_mem_fault_cpu_index(uint32_t pid)
750 CPUState *cpu_iter;
752 CPU_FOREACH(cpu_iter) {
753 if (cpu_iter->thread_id == pid) {
754 trace_get_mem_fault_cpu_index(cpu_iter->cpu_index, pid);
755 return cpu_iter->cpu_index;
758 trace_get_mem_fault_cpu_index(-1, pid);
759 return -1;
762 static uint32_t get_low_time_offset(PostcopyBlocktimeContext *dc)
764 int64_t start_time_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
765 dc->start_time;
766 return start_time_offset < 1 ? 1 : start_time_offset & UINT32_MAX;
770 * This function is being called when pagefault occurs. It
771 * tracks down vCPU blocking time.
773 * @addr: faulted host virtual address
774 * @ptid: faulted process thread id
775 * @rb: ramblock appropriate to addr
777 static void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
778 RAMBlock *rb)
780 int cpu, already_received;
781 MigrationIncomingState *mis = migration_incoming_get_current();
782 PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
783 uint32_t low_time_offset;
785 if (!dc || ptid == 0) {
786 return;
788 cpu = get_mem_fault_cpu_index(ptid);
789 if (cpu < 0) {
790 return;
793 low_time_offset = get_low_time_offset(dc);
794 if (dc->vcpu_addr[cpu] == 0) {
795 qatomic_inc(&dc->smp_cpus_down);
798 qatomic_xchg(&dc->last_begin, low_time_offset);
799 qatomic_xchg(&dc->page_fault_vcpu_time[cpu], low_time_offset);
800 qatomic_xchg(&dc->vcpu_addr[cpu], addr);
803 * check it here, not at the beginning of the function,
804 * due to, check could occur early than bitmap_set in
805 * qemu_ufd_copy_ioctl
807 already_received = ramblock_recv_bitmap_test(rb, (void *)addr);
808 if (already_received) {
809 qatomic_xchg(&dc->vcpu_addr[cpu], 0);
810 qatomic_xchg(&dc->page_fault_vcpu_time[cpu], 0);
811 qatomic_dec(&dc->smp_cpus_down);
813 trace_mark_postcopy_blocktime_begin(addr, dc, dc->page_fault_vcpu_time[cpu],
814 cpu, already_received);
818 * This function just provide calculated blocktime per cpu and trace it.
819 * Total blocktime is calculated in mark_postcopy_blocktime_end.
822 * Assume we have 3 CPU
824 * S1 E1 S1 E1
825 * -----***********------------xxx***************------------------------> CPU1
827 * S2 E2
828 * ------------****************xxx---------------------------------------> CPU2
830 * S3 E3
831 * ------------------------****xxx********-------------------------------> CPU3
833 * We have sequence S1,S2,E1,S3,S1,E2,E3,E1
834 * S2,E1 - doesn't match condition due to sequence S1,S2,E1 doesn't include CPU3
835 * S3,S1,E2 - sequence includes all CPUs, in this case overlap will be S1,E2 -
836 * it's a part of total blocktime.
837 * S1 - here is last_begin
838 * Legend of the picture is following:
839 * * - means blocktime per vCPU
840 * x - means overlapped blocktime (total blocktime)
842 * @addr: host virtual address
844 static void mark_postcopy_blocktime_end(uintptr_t addr)
846 MigrationIncomingState *mis = migration_incoming_get_current();
847 PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
848 MachineState *ms = MACHINE(qdev_get_machine());
849 unsigned int smp_cpus = ms->smp.cpus;
850 int i, affected_cpu = 0;
851 bool vcpu_total_blocktime = false;
852 uint32_t read_vcpu_time, low_time_offset;
854 if (!dc) {
855 return;
858 low_time_offset = get_low_time_offset(dc);
859 /* lookup cpu, to clear it,
860 * that algorithm looks straightforward, but it's not
861 * optimal, more optimal algorithm is keeping tree or hash
862 * where key is address value is a list of */
863 for (i = 0; i < smp_cpus; i++) {
864 uint32_t vcpu_blocktime = 0;
866 read_vcpu_time = qatomic_fetch_add(&dc->page_fault_vcpu_time[i], 0);
867 if (qatomic_fetch_add(&dc->vcpu_addr[i], 0) != addr ||
868 read_vcpu_time == 0) {
869 continue;
871 qatomic_xchg(&dc->vcpu_addr[i], 0);
872 vcpu_blocktime = low_time_offset - read_vcpu_time;
873 affected_cpu += 1;
874 /* we need to know is that mark_postcopy_end was due to
875 * faulted page, another possible case it's prefetched
876 * page and in that case we shouldn't be here */
877 if (!vcpu_total_blocktime &&
878 qatomic_fetch_add(&dc->smp_cpus_down, 0) == smp_cpus) {
879 vcpu_total_blocktime = true;
881 /* continue cycle, due to one page could affect several vCPUs */
882 dc->vcpu_blocktime[i] += vcpu_blocktime;
885 qatomic_sub(&dc->smp_cpus_down, affected_cpu);
886 if (vcpu_total_blocktime) {
887 dc->total_blocktime += low_time_offset - qatomic_fetch_add(
888 &dc->last_begin, 0);
890 trace_mark_postcopy_blocktime_end(addr, dc, dc->total_blocktime,
891 affected_cpu);
894 static void postcopy_pause_fault_thread(MigrationIncomingState *mis)
896 trace_postcopy_pause_fault_thread();
897 qemu_sem_wait(&mis->postcopy_pause_sem_fault);
898 trace_postcopy_pause_fault_thread_continued();
902 * Handle faults detected by the USERFAULT markings
904 static void *postcopy_ram_fault_thread(void *opaque)
906 MigrationIncomingState *mis = opaque;
907 struct uffd_msg msg;
908 int ret;
909 size_t index;
910 RAMBlock *rb = NULL;
912 trace_postcopy_ram_fault_thread_entry();
913 rcu_register_thread();
914 mis->last_rb = NULL; /* last RAMBlock we sent part of */
915 qemu_sem_post(&mis->thread_sync_sem);
917 struct pollfd *pfd;
918 size_t pfd_len = 2 + mis->postcopy_remote_fds->len;
920 pfd = g_new0(struct pollfd, pfd_len);
922 pfd[0].fd = mis->userfault_fd;
923 pfd[0].events = POLLIN;
924 pfd[1].fd = mis->userfault_event_fd;
925 pfd[1].events = POLLIN; /* Waiting for eventfd to go positive */
926 trace_postcopy_ram_fault_thread_fds_core(pfd[0].fd, pfd[1].fd);
927 for (index = 0; index < mis->postcopy_remote_fds->len; index++) {
928 struct PostCopyFD *pcfd = &g_array_index(mis->postcopy_remote_fds,
929 struct PostCopyFD, index);
930 pfd[2 + index].fd = pcfd->fd;
931 pfd[2 + index].events = POLLIN;
932 trace_postcopy_ram_fault_thread_fds_extra(2 + index, pcfd->idstr,
933 pcfd->fd);
936 while (true) {
937 ram_addr_t rb_offset;
938 int poll_result;
941 * We're mainly waiting for the kernel to give us a faulting HVA,
942 * however we can be told to quit via userfault_quit_fd which is
943 * an eventfd
946 poll_result = poll(pfd, pfd_len, -1 /* Wait forever */);
947 if (poll_result == -1) {
948 error_report("%s: userfault poll: %s", __func__, strerror(errno));
949 break;
952 if (!mis->to_src_file) {
954 * Possibly someone tells us that the return path is
955 * broken already using the event. We should hold until
956 * the channel is rebuilt.
958 postcopy_pause_fault_thread(mis);
961 if (pfd[1].revents) {
962 uint64_t tmp64 = 0;
964 /* Consume the signal */
965 if (read(mis->userfault_event_fd, &tmp64, 8) != 8) {
966 /* Nothing obviously nicer than posting this error. */
967 error_report("%s: read() failed", __func__);
970 if (qatomic_read(&mis->fault_thread_quit)) {
971 trace_postcopy_ram_fault_thread_quit();
972 break;
976 if (pfd[0].revents) {
977 poll_result--;
978 ret = read(mis->userfault_fd, &msg, sizeof(msg));
979 if (ret != sizeof(msg)) {
980 if (errno == EAGAIN) {
982 * if a wake up happens on the other thread just after
983 * the poll, there is nothing to read.
985 continue;
987 if (ret < 0) {
988 error_report("%s: Failed to read full userfault "
989 "message: %s",
990 __func__, strerror(errno));
991 break;
992 } else {
993 error_report("%s: Read %d bytes from userfaultfd "
994 "expected %zd",
995 __func__, ret, sizeof(msg));
996 break; /* Lost alignment, don't know what we'd read next */
999 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1000 error_report("%s: Read unexpected event %ud from userfaultfd",
1001 __func__, msg.event);
1002 continue; /* It's not a page fault, shouldn't happen */
1005 rb = qemu_ram_block_from_host(
1006 (void *)(uintptr_t)msg.arg.pagefault.address,
1007 true, &rb_offset);
1008 if (!rb) {
1009 error_report("postcopy_ram_fault_thread: Fault outside guest: %"
1010 PRIx64, (uint64_t)msg.arg.pagefault.address);
1011 break;
1014 rb_offset = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
1015 trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
1016 qemu_ram_get_idstr(rb),
1017 rb_offset,
1018 msg.arg.pagefault.feat.ptid);
1019 mark_postcopy_blocktime_begin(
1020 (uintptr_t)(msg.arg.pagefault.address),
1021 msg.arg.pagefault.feat.ptid, rb);
1023 retry:
1025 * Send the request to the source - we want to request one
1026 * of our host page sizes (which is >= TPS)
1028 ret = postcopy_request_page(mis, rb, rb_offset,
1029 msg.arg.pagefault.address);
1030 if (ret) {
1031 /* May be network failure, try to wait for recovery */
1032 postcopy_pause_fault_thread(mis);
1033 goto retry;
1037 /* Now handle any requests from external processes on shared memory */
1038 /* TODO: May need to handle devices deregistering during postcopy */
1039 for (index = 2; index < pfd_len && poll_result; index++) {
1040 if (pfd[index].revents) {
1041 struct PostCopyFD *pcfd =
1042 &g_array_index(mis->postcopy_remote_fds,
1043 struct PostCopyFD, index - 2);
1045 poll_result--;
1046 if (pfd[index].revents & POLLERR) {
1047 error_report("%s: POLLERR on poll %zd fd=%d",
1048 __func__, index, pcfd->fd);
1049 pfd[index].events = 0;
1050 continue;
1053 ret = read(pcfd->fd, &msg, sizeof(msg));
1054 if (ret != sizeof(msg)) {
1055 if (errno == EAGAIN) {
1057 * if a wake up happens on the other thread just after
1058 * the poll, there is nothing to read.
1060 continue;
1062 if (ret < 0) {
1063 error_report("%s: Failed to read full userfault "
1064 "message: %s (shared) revents=%d",
1065 __func__, strerror(errno),
1066 pfd[index].revents);
1067 /*TODO: Could just disable this sharer */
1068 break;
1069 } else {
1070 error_report("%s: Read %d bytes from userfaultfd "
1071 "expected %zd (shared)",
1072 __func__, ret, sizeof(msg));
1073 /*TODO: Could just disable this sharer */
1074 break; /*Lost alignment,don't know what we'd read next*/
1077 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1078 error_report("%s: Read unexpected event %ud "
1079 "from userfaultfd (shared)",
1080 __func__, msg.event);
1081 continue; /* It's not a page fault, shouldn't happen */
1083 /* Call the device handler registered with us */
1084 ret = pcfd->handler(pcfd, &msg);
1085 if (ret) {
1086 error_report("%s: Failed to resolve shared fault on %zd/%s",
1087 __func__, index, pcfd->idstr);
1088 /* TODO: Fail? Disable this sharer? */
1093 rcu_unregister_thread();
1094 trace_postcopy_ram_fault_thread_exit();
1095 g_free(pfd);
1096 return NULL;
1099 static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
1101 PostcopyTmpPage *tmp_page;
1102 int err, i, channels;
1103 void *temp_page;
1105 /* TODO: will be boosted when enable postcopy preemption */
1106 mis->postcopy_channels = 1;
1108 channels = mis->postcopy_channels;
1109 mis->postcopy_tmp_pages = g_malloc0_n(sizeof(PostcopyTmpPage), channels);
1111 for (i = 0; i < channels; i++) {
1112 tmp_page = &mis->postcopy_tmp_pages[i];
1113 temp_page = mmap(NULL, mis->largest_page_size, PROT_READ | PROT_WRITE,
1114 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1115 if (temp_page == MAP_FAILED) {
1116 err = errno;
1117 error_report("%s: Failed to map postcopy_tmp_pages[%d]: %s",
1118 __func__, i, strerror(err));
1119 /* Clean up will be done later */
1120 return -err;
1122 tmp_page->tmp_huge_page = temp_page;
1123 /* Initialize default states for each tmp page */
1124 postcopy_temp_page_reset(tmp_page);
1128 * Map large zero page when kernel can't use UFFDIO_ZEROPAGE for hugepages
1130 mis->postcopy_tmp_zero_page = mmap(NULL, mis->largest_page_size,
1131 PROT_READ | PROT_WRITE,
1132 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1133 if (mis->postcopy_tmp_zero_page == MAP_FAILED) {
1134 err = errno;
1135 mis->postcopy_tmp_zero_page = NULL;
1136 error_report("%s: Failed to map large zero page %s",
1137 __func__, strerror(err));
1138 return -err;
1141 memset(mis->postcopy_tmp_zero_page, '\0', mis->largest_page_size);
1143 return 0;
1146 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1148 /* Open the fd for the kernel to give us userfaults */
1149 mis->userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
1150 if (mis->userfault_fd == -1) {
1151 error_report("%s: Failed to open userfault fd: %s", __func__,
1152 strerror(errno));
1153 return -1;
1157 * Although the host check already tested the API, we need to
1158 * do the check again as an ABI handshake on the new fd.
1160 if (!ufd_check_and_apply(mis->userfault_fd, mis)) {
1161 return -1;
1164 /* Now an eventfd we use to tell the fault-thread to quit */
1165 mis->userfault_event_fd = eventfd(0, EFD_CLOEXEC);
1166 if (mis->userfault_event_fd == -1) {
1167 error_report("%s: Opening userfault_event_fd: %s", __func__,
1168 strerror(errno));
1169 close(mis->userfault_fd);
1170 return -1;
1173 postcopy_thread_create(mis, &mis->fault_thread, "postcopy/fault",
1174 postcopy_ram_fault_thread, QEMU_THREAD_JOINABLE);
1175 mis->have_fault_thread = true;
1177 /* Mark so that we get notified of accesses to unwritten areas */
1178 if (foreach_not_ignored_block(ram_block_enable_notify, mis)) {
1179 error_report("ram_block_enable_notify failed");
1180 return -1;
1183 if (postcopy_temp_pages_setup(mis)) {
1184 /* Error dumped in the sub-function */
1185 return -1;
1188 trace_postcopy_ram_enable_notify();
1190 return 0;
1193 static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
1194 void *from_addr, uint64_t pagesize, RAMBlock *rb)
1196 int userfault_fd = mis->userfault_fd;
1197 int ret;
1199 if (from_addr) {
1200 struct uffdio_copy copy_struct;
1201 copy_struct.dst = (uint64_t)(uintptr_t)host_addr;
1202 copy_struct.src = (uint64_t)(uintptr_t)from_addr;
1203 copy_struct.len = pagesize;
1204 copy_struct.mode = 0;
1205 ret = ioctl(userfault_fd, UFFDIO_COPY, &copy_struct);
1206 } else {
1207 struct uffdio_zeropage zero_struct;
1208 zero_struct.range.start = (uint64_t)(uintptr_t)host_addr;
1209 zero_struct.range.len = pagesize;
1210 zero_struct.mode = 0;
1211 ret = ioctl(userfault_fd, UFFDIO_ZEROPAGE, &zero_struct);
1213 if (!ret) {
1214 qemu_mutex_lock(&mis->page_request_mutex);
1215 ramblock_recv_bitmap_set_range(rb, host_addr,
1216 pagesize / qemu_target_page_size());
1218 * If this page resolves a page fault for a previous recorded faulted
1219 * address, take a special note to maintain the requested page list.
1221 if (g_tree_lookup(mis->page_requested, host_addr)) {
1222 g_tree_remove(mis->page_requested, host_addr);
1223 mis->page_requested_count--;
1224 trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
1226 qemu_mutex_unlock(&mis->page_request_mutex);
1227 mark_postcopy_blocktime_end((uintptr_t)host_addr);
1229 return ret;
1232 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
1234 int i;
1235 MigrationIncomingState *mis = migration_incoming_get_current();
1236 GArray *pcrfds = mis->postcopy_remote_fds;
1238 for (i = 0; i < pcrfds->len; i++) {
1239 struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1240 int ret = cur->waker(cur, rb, offset);
1241 if (ret) {
1242 return ret;
1245 return 0;
1249 * Place a host page (from) at (host) atomically
1250 * returns 0 on success
1252 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1253 RAMBlock *rb)
1255 size_t pagesize = qemu_ram_pagesize(rb);
1257 /* copy also acks to the kernel waking the stalled thread up
1258 * TODO: We can inhibit that ack and only do it if it was requested
1259 * which would be slightly cheaper, but we'd have to be careful
1260 * of the order of updating our page state.
1262 if (qemu_ufd_copy_ioctl(mis, host, from, pagesize, rb)) {
1263 int e = errno;
1264 error_report("%s: %s copy host: %p from: %p (size: %zd)",
1265 __func__, strerror(e), host, from, pagesize);
1267 return -e;
1270 trace_postcopy_place_page(host);
1271 return postcopy_notify_shared_wake(rb,
1272 qemu_ram_block_host_offset(rb, host));
1276 * Place a zero page at (host) atomically
1277 * returns 0 on success
1279 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1280 RAMBlock *rb)
1282 size_t pagesize = qemu_ram_pagesize(rb);
1283 trace_postcopy_place_page_zero(host);
1285 /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
1286 * but it's not available for everything (e.g. hugetlbpages)
1288 if (qemu_ram_is_uf_zeroable(rb)) {
1289 if (qemu_ufd_copy_ioctl(mis, host, NULL, pagesize, rb)) {
1290 int e = errno;
1291 error_report("%s: %s zero host: %p",
1292 __func__, strerror(e), host);
1294 return -e;
1296 return postcopy_notify_shared_wake(rb,
1297 qemu_ram_block_host_offset(rb,
1298 host));
1299 } else {
1300 return postcopy_place_page(mis, host, mis->postcopy_tmp_zero_page, rb);
1304 #else
1305 /* No target OS support, stubs just fail */
1306 void fill_destination_postcopy_migration_info(MigrationInfo *info)
1310 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
1312 error_report("%s: No OS support", __func__);
1313 return false;
1316 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1318 error_report("postcopy_ram_incoming_init: No OS support");
1319 return -1;
1322 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
1324 assert(0);
1325 return -1;
1328 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
1330 assert(0);
1331 return -1;
1334 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
1335 uint64_t client_addr, uint64_t rb_offset)
1337 assert(0);
1338 return -1;
1341 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1343 assert(0);
1344 return -1;
1347 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1348 RAMBlock *rb)
1350 assert(0);
1351 return -1;
1354 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1355 RAMBlock *rb)
1357 assert(0);
1358 return -1;
1361 int postcopy_wake_shared(struct PostCopyFD *pcfd,
1362 uint64_t client_addr,
1363 RAMBlock *rb)
1365 assert(0);
1366 return -1;
1368 #endif
1370 /* ------------------------------------------------------------------------- */
1371 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page)
1373 tmp_page->target_pages = 0;
1374 tmp_page->host_addr = NULL;
1376 * This is set to true when reset, and cleared as long as we received any
1377 * of the non-zero small page within this huge page.
1379 tmp_page->all_zero = true;
1382 void postcopy_fault_thread_notify(MigrationIncomingState *mis)
1384 uint64_t tmp64 = 1;
1387 * Wakeup the fault_thread. It's an eventfd that should currently
1388 * be at 0, we're going to increment it to 1
1390 if (write(mis->userfault_event_fd, &tmp64, 8) != 8) {
1391 /* Not much we can do here, but may as well report it */
1392 error_report("%s: incrementing failed: %s", __func__,
1393 strerror(errno));
1398 * postcopy_discard_send_init: Called at the start of each RAMBlock before
1399 * asking to discard individual ranges.
1401 * @ms: The current migration state.
1402 * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
1403 * @name: RAMBlock that discards will operate on.
1405 static PostcopyDiscardState pds = {0};
1406 void postcopy_discard_send_init(MigrationState *ms, const char *name)
1408 pds.ramblock_name = name;
1409 pds.cur_entry = 0;
1410 pds.nsentwords = 0;
1411 pds.nsentcmds = 0;
1415 * postcopy_discard_send_range: Called by the bitmap code for each chunk to
1416 * discard. May send a discard message, may just leave it queued to
1417 * be sent later.
1419 * @ms: Current migration state.
1420 * @start,@length: a range of pages in the migration bitmap in the
1421 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
1423 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
1424 unsigned long length)
1426 size_t tp_size = qemu_target_page_size();
1427 /* Convert to byte offsets within the RAM block */
1428 pds.start_list[pds.cur_entry] = start * tp_size;
1429 pds.length_list[pds.cur_entry] = length * tp_size;
1430 trace_postcopy_discard_send_range(pds.ramblock_name, start, length);
1431 pds.cur_entry++;
1432 pds.nsentwords++;
1434 if (pds.cur_entry == MAX_DISCARDS_PER_COMMAND) {
1435 /* Full set, ship it! */
1436 qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
1437 pds.ramblock_name,
1438 pds.cur_entry,
1439 pds.start_list,
1440 pds.length_list);
1441 pds.nsentcmds++;
1442 pds.cur_entry = 0;
1447 * postcopy_discard_send_finish: Called at the end of each RAMBlock by the
1448 * bitmap code. Sends any outstanding discard messages, frees the PDS
1450 * @ms: Current migration state.
1452 void postcopy_discard_send_finish(MigrationState *ms)
1454 /* Anything unsent? */
1455 if (pds.cur_entry) {
1456 qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
1457 pds.ramblock_name,
1458 pds.cur_entry,
1459 pds.start_list,
1460 pds.length_list);
1461 pds.nsentcmds++;
1464 trace_postcopy_discard_send_finish(pds.ramblock_name, pds.nsentwords,
1465 pds.nsentcmds);
1469 * Current state of incoming postcopy; note this is not part of
1470 * MigrationIncomingState since it's state is used during cleanup
1471 * at the end as MIS is being freed.
1473 static PostcopyState incoming_postcopy_state;
1475 PostcopyState postcopy_state_get(void)
1477 return qatomic_mb_read(&incoming_postcopy_state);
1480 /* Set the state and return the old state */
1481 PostcopyState postcopy_state_set(PostcopyState new_state)
1483 return qatomic_xchg(&incoming_postcopy_state, new_state);
1486 /* Register a handler for external shared memory postcopy
1487 * called on the destination.
1489 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
1491 MigrationIncomingState *mis = migration_incoming_get_current();
1493 mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
1494 *pcfd);
1497 /* Unregister a handler for external shared memory postcopy
1499 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
1501 guint i;
1502 MigrationIncomingState *mis = migration_incoming_get_current();
1503 GArray *pcrfds = mis->postcopy_remote_fds;
1505 if (!pcrfds) {
1506 /* migration has already finished and freed the array */
1507 return;
1509 for (i = 0; i < pcrfds->len; i++) {
1510 struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1511 if (cur->fd == pcfd->fd) {
1512 mis->postcopy_remote_fds = g_array_remove_index(pcrfds, i);
1513 return;