xref: /openbmc/qemu/migration/postcopy-ram.c (revision bb7e03cb)
1 /*
2  * Postcopy migration for RAM
3  *
4  * Copyright 2013-2015 Red Hat, Inc. and/or its affiliates
5  *
6  * Authors:
7  *  Dave Gilbert  <dgilbert@redhat.com>
8  *
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.
11  *
12  */
13 
14 /*
15  * Postcopy is a migration technique where the execution flips from the
16  * source to the destination before all the data has been copied.
17  */
18 
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"
40 
41 /* Arbitrary limit on size of each discard command,
42  * keeps them around ~200 bytes
43  */
44 #define MAX_DISCARDS_PER_COMMAND 12
45 
46 struct PostcopyDiscardState {
47     const char *ramblock_name;
48     uint16_t cur_entry;
49     /*
50      * Start and length of a discard range (bytes)
51      */
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;
56 };
57 
58 static NotifierWithReturnList postcopy_notifier_list;
59 
60 void postcopy_infrastructure_init(void)
61 {
62     notifier_with_return_list_init(&postcopy_notifier_list);
63 }
64 
65 void postcopy_add_notifier(NotifierWithReturn *nn)
66 {
67     notifier_with_return_list_add(&postcopy_notifier_list, nn);
68 }
69 
70 void postcopy_remove_notifier(NotifierWithReturn *n)
71 {
72     notifier_with_return_remove(n);
73 }
74 
75 int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp)
76 {
77     struct PostcopyNotifyData pnd;
78     pnd.reason = reason;
79     pnd.errp = errp;
80 
81     return notifier_with_return_list_notify(&postcopy_notifier_list,
82                                             &pnd);
83 }
84 
85 /*
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.
88  */
89 void postcopy_thread_create(MigrationIncomingState *mis,
90                             QemuThread *thread, const char *name,
91                             void *(*fn)(void *), int joinable)
92 {
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);
97 }
98 
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.
102  */
103 #if defined(__linux__)
104 
105 #include <poll.h>
106 #include <sys/ioctl.h>
107 #include <sys/syscall.h>
108 #include <asm/types.h> /* for __u64 */
109 #endif
110 
111 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
112 #include <sys/eventfd.h>
113 #include <linux/userfaultfd.h>
114 
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;
128 
129     /*
130      * Handler for exit event, necessary for
131      * releasing whole blocktime_ctx
132      */
133     Notifier exit_notifier;
134 } PostcopyBlocktimeContext;
135 
136 static void destroy_blocktime_context(struct PostcopyBlocktimeContext *ctx)
137 {
138     g_free(ctx->page_fault_vcpu_time);
139     g_free(ctx->vcpu_addr);
140     g_free(ctx->vcpu_blocktime);
141     g_free(ctx);
142 }
143 
144 static void migration_exit_cb(Notifier *n, void *data)
145 {
146     PostcopyBlocktimeContext *ctx = container_of(n, PostcopyBlocktimeContext,
147                                                  exit_notifier);
148     destroy_blocktime_context(ctx);
149 }
150 
151 static struct PostcopyBlocktimeContext *blocktime_context_new(void)
152 {
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);
159 
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;
164 }
165 
166 static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
167 {
168     MachineState *ms = MACHINE(qdev_get_machine());
169     uint32List *list = NULL;
170     int i;
171 
172     for (i = ms->smp.cpus - 1; i >= 0; i--) {
173         QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
174     }
175 
176     return list;
177 }
178 
179 /*
180  * This function just populates MigrationInfo from postcopy's
181  * blocktime context. It will not populate MigrationInfo,
182  * unless postcopy-blocktime capability was set.
183  *
184  * @info: pointer to MigrationInfo to populate
185  */
186 void fill_destination_postcopy_migration_info(MigrationInfo *info)
187 {
188     MigrationIncomingState *mis = migration_incoming_get_current();
189     PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
190 
191     if (!bc) {
192         return;
193     }
194 
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);
199 }
200 
201 static uint32_t get_postcopy_total_blocktime(void)
202 {
203     MigrationIncomingState *mis = migration_incoming_get_current();
204     PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
205 
206     if (!bc) {
207         return 0;
208     }
209 
210     return bc->total_blocktime;
211 }
212 
213 /**
214  * receive_ufd_features: check userfault fd features, to request only supported
215  * features in the future.
216  *
217  * Returns: true on success
218  *
219  * __NR_userfaultfd - should be checked before
220  *  @features: out parameter will contain uffdio_api.features provided by kernel
221  *              in case of success
222  */
223 static bool receive_ufd_features(uint64_t *features)
224 {
225     struct uffdio_api api_struct = {0};
226     int ufd;
227     bool ret = true;
228 
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;
235     }
236 
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;
245     }
246 
247     *features = api_struct.features;
248 
249 release_ufd:
250     close(ufd);
251     return ret;
252 }
253 
254 /**
255  * request_ufd_features: this function should be called only once on a newly
256  * opened ufd, subsequent calls will lead to error.
257  *
258  * Returns: true on success
259  *
260  * @ufd: fd obtained from userfaultfd syscall
261  * @features: bit mask see UFFD_API_FEATURES
262  */
263 static bool request_ufd_features(int ufd, uint64_t features)
264 {
265     struct uffdio_api api_struct = {0};
266     uint64_t ioctl_mask;
267 
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;
274     }
275 
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;
282     }
283 
284     return true;
285 }
286 
287 static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
288 {
289     uint64_t asked_features = 0;
290     static uint64_t supported_features;
291 
292     /*
293      * it's not possible to
294      * request UFFD_API twice per one fd
295      * userfault fd features is persistent
296      */
297     if (!supported_features) {
298         if (!receive_ufd_features(&supported_features)) {
299             error_report("%s failed", __func__);
300             return false;
301         }
302     }
303 
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();
310             }
311         }
312     }
313 #endif
314 
315     /*
316      * request features, even if asked_features is 0, due to
317      * kernel expects UFFD_API before UFFDIO_REGISTER, per
318      * userfault file descriptor
319      */
320     if (!request_ufd_features(ufd, asked_features)) {
321         error_report("%s failed: features %" PRIu64, __func__,
322                      asked_features);
323         return false;
324     }
325 
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;
335         }
336     }
337     return true;
338 }
339 
340 /* Callback from postcopy_ram_supported_by_host block iterator.
341  */
342 static int test_ramblock_postcopiable(RAMBlock *rb, void *opaque)
343 {
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);
347 
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;
353     }
354     return 0;
355 }
356 
357 /*
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.
361  */
362 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
363 {
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;
372 
373     if (qemu_target_page_size() > pagesize) {
374         error_report("Target page size bigger than host page size");
375         goto out;
376     }
377 
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;
383     }
384 
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;
389     }
390 
391     /* Version and features check */
392     if (!ufd_check_and_apply(ufd, mis)) {
393         goto out;
394     }
395 
396     /* We don't support postcopy with shared RAM yet */
397     if (foreach_not_ignored_block(test_ramblock_postcopiable, NULL)) {
398         goto out;
399     }
400 
401     /*
402      * userfault and mlock don't go together; we'll put it back later if
403      * it was enabled.
404      */
405     if (munlockall()) {
406         error_report("%s: munlockall: %s", __func__,  strerror(errno));
407         goto out;
408     }
409 
410     /*
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.
414      */
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;
421     }
422     g_assert(QEMU_PTR_IS_ALIGNED(testarea, pagesize));
423 
424     reg_struct.range.start = (uintptr_t)testarea;
425     reg_struct.range.len = pagesize;
426     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
427 
428     if (ioctl(ufd, UFFDIO_REGISTER, &reg_struct)) {
429         error_report("%s userfault register: %s", __func__, strerror(errno));
430         goto out;
431     }
432 
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;
438     }
439 
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;
447     }
448 
449     /* Success! */
450     ret = true;
451 out:
452     if (testarea) {
453         munmap(testarea, pagesize);
454     }
455     if (ufd != -1) {
456         close(ufd);
457     }
458     return ret;
459 }
460 
461 /*
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.
465  */
466 static int init_range(RAMBlock *rb, void *opaque)
467 {
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);
473 
474     /*
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.
478      */
479     rb->postcopy_length = length;
480 
481     /*
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)
486      */
487     if (ram_discard_range(block_name, 0, length)) {
488         return -1;
489     }
490 
491     return 0;
492 }
493 
494 /*
495  * At the end of migration, undo the effects of init_range
496  * opaque should be the MIS.
497  */
498 static int cleanup_range(RAMBlock *rb, void *opaque)
499 {
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);
507 
508     /*
509      * We turned off hugepage for the precopy stage with postcopy enabled
510      * we can turn it back on now.
511      */
512     qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE);
513 
514     /*
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.
518      */
519     range_struct.start = (uintptr_t)host_addr;
520     range_struct.len = length;
521 
522     if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) {
523         error_report("%s: userfault unregister %s", __func__, strerror(errno));
524 
525         return -1;
526     }
527 
528     return 0;
529 }
530 
531 /*
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
535  */
536 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
537 {
538     if (foreach_not_ignored_block(init_range, NULL)) {
539         return -1;
540     }
541 
542     return 0;
543 }
544 
545 static void postcopy_temp_pages_cleanup(MigrationIncomingState *mis)
546 {
547     int i;
548 
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;
555             }
556         }
557         g_free(mis->postcopy_tmp_pages);
558         mis->postcopy_tmp_pages = NULL;
559     }
560 
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;
564     }
565 }
566 
567 /*
568  * At the end of a migration where postcopy_ram_incoming_init was called.
569  */
570 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
571 {
572     trace_postcopy_ram_incoming_cleanup_entry();
573 
574     if (mis->postcopy_prio_thread_created) {
575         qemu_thread_join(&mis->postcopy_prio_thread);
576         mis->postcopy_prio_thread_created = false;
577     }
578 
579     if (mis->have_fault_thread) {
580         Error *local_err = NULL;
581 
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);
587 
588         if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_END, &local_err)) {
589             error_report_err(local_err);
590             return -1;
591         }
592 
593         if (foreach_not_ignored_block(cleanup_range, mis)) {
594             return -1;
595         }
596 
597         trace_postcopy_ram_incoming_cleanup_closeuf();
598         close(mis->userfault_fd);
599         close(mis->userfault_event_fd);
600         mis->have_fault_thread = false;
601     }
602 
603     if (enable_mlock) {
604         if (os_mlock() < 0) {
605             error_report("mlock: %s", strerror(errno));
606             /*
607              * It doesn't feel right to fail at this point, we have a valid
608              * VM state.
609              */
610         }
611     }
612 
613     postcopy_temp_pages_cleanup(mis);
614 
615     trace_postcopy_ram_incoming_cleanup_blocktime(
616             get_postcopy_total_blocktime());
617 
618     trace_postcopy_ram_incoming_cleanup_exit();
619     return 0;
620 }
621 
622 /*
623  * Disable huge pages on an area
624  */
625 static int nhp_range(RAMBlock *rb, void *opaque)
626 {
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);
632 
633     /*
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.
637      */
638     qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE);
639 
640     return 0;
641 }
642 
643 /*
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
647  */
648 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
649 {
650     if (foreach_not_ignored_block(nhp_range, mis)) {
651         return -1;
652     }
653 
654     postcopy_state_set(POSTCOPY_INCOMING_DISCARD);
655 
656     return 0;
657 }
658 
659 /*
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
667  */
668 static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
669 {
670     MigrationIncomingState *mis = opaque;
671     struct uffdio_register reg_struct;
672 
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;
676 
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;
681     }
682     if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
683         error_report("%s userfault: Region doesn't support COPY", __func__);
684         return -1;
685     }
686     if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
687         qemu_ram_set_uf_zeroable(rb);
688     }
689 
690     return 0;
691 }
692 
693 int postcopy_wake_shared(struct PostCopyFD *pcfd,
694                          uint64_t client_addr,
695                          RAMBlock *rb)
696 {
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));
708     }
709     return ret;
710 }
711 
712 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
713                                  ram_addr_t start, uint64_t haddr)
714 {
715     void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
716 
717     /*
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.
721      *
722      * Checking a single bit is sufficient to handle pagesize > TPS as either
723      * all relevant bits are set or not.
724      */
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);
728 
729         return received ? 0 : postcopy_place_page_zero(mis, aligned, rb);
730     }
731 
732     return migrate_send_rp_req_pages(mis, rb, start, haddr);
733 }
734 
735 /*
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)
739  */
740 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
741                                  uint64_t client_addr, uint64_t rb_offset)
742 {
743     uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
744     MigrationIncomingState *mis = migration_incoming_get_current();
745 
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);
752     }
753     postcopy_request_page(mis, rb, aligned_rbo, client_addr);
754     return 0;
755 }
756 
757 static int get_mem_fault_cpu_index(uint32_t pid)
758 {
759     CPUState *cpu_iter;
760 
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;
765         }
766     }
767     trace_get_mem_fault_cpu_index(-1, pid);
768     return -1;
769 }
770 
771 static uint32_t get_low_time_offset(PostcopyBlocktimeContext *dc)
772 {
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;
776 }
777 
778 /*
779  * This function is being called when pagefault occurs. It
780  * tracks down vCPU blocking time.
781  *
782  * @addr: faulted host virtual address
783  * @ptid: faulted process thread id
784  * @rb: ramblock appropriate to addr
785  */
786 static void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
787                                           RAMBlock *rb)
788 {
789     int cpu, already_received;
790     MigrationIncomingState *mis = migration_incoming_get_current();
791     PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
792     uint32_t low_time_offset;
793 
794     if (!dc || ptid == 0) {
795         return;
796     }
797     cpu = get_mem_fault_cpu_index(ptid);
798     if (cpu < 0) {
799         return;
800     }
801 
802     low_time_offset = get_low_time_offset(dc);
803     if (dc->vcpu_addr[cpu] == 0) {
804         qatomic_inc(&dc->smp_cpus_down);
805     }
806 
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);
810 
811     /*
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
815      */
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);
821     }
822     trace_mark_postcopy_blocktime_begin(addr, dc, dc->page_fault_vcpu_time[cpu],
823                                         cpu, already_received);
824 }
825 
826 /*
827  *  This function just provide calculated blocktime per cpu and trace it.
828  *  Total blocktime is calculated in mark_postcopy_blocktime_end.
829  *
830  *
831  * Assume we have 3 CPU
832  *
833  *      S1        E1           S1               E1
834  * -----***********------------xxx***************------------------------> CPU1
835  *
836  *             S2                E2
837  * ------------****************xxx---------------------------------------> CPU2
838  *
839  *                         S3            E3
840  * ------------------------****xxx********-------------------------------> CPU3
841  *
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)
850  *
851  * @addr: host virtual address
852  */
853 static void mark_postcopy_blocktime_end(uintptr_t addr)
854 {
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;
862 
863     if (!dc) {
864         return;
865     }
866 
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;
874 
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;
879         }
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;
889         }
890         /* continue cycle, due to one page could affect several vCPUs */
891         dc->vcpu_blocktime[i] += vcpu_blocktime;
892     }
893 
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);
898     }
899     trace_mark_postcopy_blocktime_end(addr, dc, dc->total_blocktime,
900                                       affected_cpu);
901 }
902 
903 static void postcopy_pause_fault_thread(MigrationIncomingState *mis)
904 {
905     trace_postcopy_pause_fault_thread();
906     qemu_sem_wait(&mis->postcopy_pause_sem_fault);
907     trace_postcopy_pause_fault_thread_continued();
908 }
909 
910 /*
911  * Handle faults detected by the USERFAULT markings
912  */
913 static void *postcopy_ram_fault_thread(void *opaque)
914 {
915     MigrationIncomingState *mis = opaque;
916     struct uffd_msg msg;
917     int ret;
918     size_t index;
919     RAMBlock *rb = NULL;
920 
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);
925 
926     struct pollfd *pfd;
927     size_t pfd_len = 2 + mis->postcopy_remote_fds->len;
928 
929     pfd = g_new0(struct pollfd, pfd_len);
930 
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);
943     }
944 
945     while (true) {
946         ram_addr_t rb_offset;
947         int poll_result;
948 
949         /*
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
953          */
954 
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;
959         }
960 
961         if (!mis->to_src_file) {
962             /*
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.
966              */
967             postcopy_pause_fault_thread(mis);
968         }
969 
970         if (pfd[1].revents) {
971             uint64_t tmp64 = 0;
972 
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__);
977             }
978 
979             if (qatomic_read(&mis->fault_thread_quit)) {
980                 trace_postcopy_ram_fault_thread_quit();
981                 break;
982             }
983         }
984 
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) {
990                     /*
991                      * if a wake up happens on the other thread just after
992                      * the poll, there is nothing to read.
993                      */
994                     continue;
995                 }
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 */
1006                 }
1007             }
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 */
1012             }
1013 
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;
1021             }
1022 
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);
1031 
1032 retry:
1033             /*
1034              * Send the request to the source - we want to request one
1035              * of our host page sizes (which is >= TPS)
1036              */
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;
1043             }
1044         }
1045 
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);
1053 
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;
1060                 }
1061 
1062                 ret = read(pcfd->fd, &msg, sizeof(msg));
1063                 if (ret != sizeof(msg)) {
1064                     if (errno == EAGAIN) {
1065                         /*
1066                          * if a wake up happens on the other thread just after
1067                          * the poll, there is nothing to read.
1068                          */
1069                         continue;
1070                     }
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*/
1084                     }
1085                 }
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 */
1091                 }
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? */
1098                 }
1099             }
1100         }
1101     }
1102     rcu_unregister_thread();
1103     trace_postcopy_ram_fault_thread_exit();
1104     g_free(pfd);
1105     return NULL;
1106 }
1107 
1108 static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
1109 {
1110     PostcopyTmpPage *tmp_page;
1111     int err, i, channels;
1112     void *temp_page;
1113 
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;
1120     }
1121 
1122     channels = mis->postcopy_channels;
1123     mis->postcopy_tmp_pages = g_malloc0_n(sizeof(PostcopyTmpPage), channels);
1124 
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;
1135         }
1136         tmp_page->tmp_huge_page = temp_page;
1137         /* Initialize default states for each tmp page */
1138         postcopy_temp_page_reset(tmp_page);
1139     }
1140 
1141     /*
1142      * Map large zero page when kernel can't use UFFDIO_ZEROPAGE for hugepages
1143      */
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;
1153     }
1154 
1155     memset(mis->postcopy_tmp_zero_page, '\0', mis->largest_page_size);
1156 
1157     return 0;
1158 }
1159 
1160 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1161 {
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;
1168     }
1169 
1170     /*
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.
1173      */
1174     if (!ufd_check_and_apply(mis->userfault_fd, mis)) {
1175         return -1;
1176     }
1177 
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;
1185     }
1186 
1187     postcopy_thread_create(mis, &mis->fault_thread, "fault-default",
1188                            postcopy_ram_fault_thread, QEMU_THREAD_JOINABLE);
1189     mis->have_fault_thread = true;
1190 
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;
1195     }
1196 
1197     if (postcopy_temp_pages_setup(mis)) {
1198         /* Error dumped in the sub-function */
1199         return -1;
1200     }
1201 
1202     if (migrate_postcopy_preempt()) {
1203         /*
1204          * This thread needs to be created after the temp pages because
1205          * it'll fetch RAM_CHANNEL_POSTCOPY PostcopyTmpPage immediately.
1206          */
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;
1210     }
1211 
1212     trace_postcopy_ram_enable_notify();
1213 
1214     return 0;
1215 }
1216 
1217 static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
1218                                void *from_addr, uint64_t pagesize, RAMBlock *rb)
1219 {
1220     int userfault_fd = mis->userfault_fd;
1221     int ret;
1222 
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);
1236     }
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());
1241         /*
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.
1244          */
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);
1249         }
1250         qemu_mutex_unlock(&mis->page_request_mutex);
1251         mark_postcopy_blocktime_end((uintptr_t)host_addr);
1252     }
1253     return ret;
1254 }
1255 
1256 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
1257 {
1258     int i;
1259     MigrationIncomingState *mis = migration_incoming_get_current();
1260     GArray *pcrfds = mis->postcopy_remote_fds;
1261 
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;
1267         }
1268     }
1269     return 0;
1270 }
1271 
1272 /*
1273  * Place a host page (from) at (host) atomically
1274  * returns 0 on success
1275  */
1276 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1277                         RAMBlock *rb)
1278 {
1279     size_t pagesize = qemu_ram_pagesize(rb);
1280 
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.
1285      */
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);
1290 
1291         return -e;
1292     }
1293 
1294     trace_postcopy_place_page(host);
1295     return postcopy_notify_shared_wake(rb,
1296                                        qemu_ram_block_host_offset(rb, host));
1297 }
1298 
1299 /*
1300  * Place a zero page at (host) atomically
1301  * returns 0 on success
1302  */
1303 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1304                              RAMBlock *rb)
1305 {
1306     size_t pagesize = qemu_ram_pagesize(rb);
1307     trace_postcopy_place_page_zero(host);
1308 
1309     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
1310      * but it's not available for everything (e.g. hugetlbpages)
1311      */
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);
1317 
1318             return -e;
1319         }
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);
1325     }
1326 }
1327 
1328 #else
1329 /* No target OS support, stubs just fail */
1330 void fill_destination_postcopy_migration_info(MigrationInfo *info)
1331 {
1332 }
1333 
1334 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
1335 {
1336     error_report("%s: No OS support", __func__);
1337     return false;
1338 }
1339 
1340 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1341 {
1342     error_report("postcopy_ram_incoming_init: No OS support");
1343     return -1;
1344 }
1345 
1346 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
1347 {
1348     assert(0);
1349     return -1;
1350 }
1351 
1352 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
1353 {
1354     assert(0);
1355     return -1;
1356 }
1357 
1358 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
1359                                  uint64_t client_addr, uint64_t rb_offset)
1360 {
1361     assert(0);
1362     return -1;
1363 }
1364 
1365 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1366 {
1367     assert(0);
1368     return -1;
1369 }
1370 
1371 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1372                         RAMBlock *rb)
1373 {
1374     assert(0);
1375     return -1;
1376 }
1377 
1378 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1379                         RAMBlock *rb)
1380 {
1381     assert(0);
1382     return -1;
1383 }
1384 
1385 int postcopy_wake_shared(struct PostCopyFD *pcfd,
1386                          uint64_t client_addr,
1387                          RAMBlock *rb)
1388 {
1389     assert(0);
1390     return -1;
1391 }
1392 #endif
1393 
1394 /* ------------------------------------------------------------------------- */
1395 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page)
1396 {
1397     tmp_page->target_pages = 0;
1398     tmp_page->host_addr = NULL;
1399     /*
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.
1402      */
1403     tmp_page->all_zero = true;
1404 }
1405 
1406 void postcopy_fault_thread_notify(MigrationIncomingState *mis)
1407 {
1408     uint64_t tmp64 = 1;
1409 
1410     /*
1411      * Wakeup the fault_thread.  It's an eventfd that should currently
1412      * be at 0, we're going to increment it to 1
1413      */
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));
1418     }
1419 }
1420 
1421 /**
1422  * postcopy_discard_send_init: Called at the start of each RAMBlock before
1423  *   asking to discard individual ranges.
1424  *
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.
1428  */
1429 static PostcopyDiscardState pds = {0};
1430 void postcopy_discard_send_init(MigrationState *ms, const char *name)
1431 {
1432     pds.ramblock_name = name;
1433     pds.cur_entry = 0;
1434     pds.nsentwords = 0;
1435     pds.nsentcmds = 0;
1436 }
1437 
1438 /**
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.
1442  *
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)
1446  */
1447 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
1448                                  unsigned long length)
1449 {
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++;
1457 
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;
1467     }
1468 }
1469 
1470 /**
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
1473  *
1474  * @ms: Current migration state.
1475  */
1476 void postcopy_discard_send_finish(MigrationState *ms)
1477 {
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++;
1486     }
1487 
1488     trace_postcopy_discard_send_finish(pds.ramblock_name, pds.nsentwords,
1489                                        pds.nsentcmds);
1490 }
1491 
1492 /*
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.
1496  */
1497 static PostcopyState incoming_postcopy_state;
1498 
1499 PostcopyState  postcopy_state_get(void)
1500 {
1501     return qatomic_mb_read(&incoming_postcopy_state);
1502 }
1503 
1504 /* Set the state and return the old state */
1505 PostcopyState postcopy_state_set(PostcopyState new_state)
1506 {
1507     return qatomic_xchg(&incoming_postcopy_state, new_state);
1508 }
1509 
1510 /* Register a handler for external shared memory postcopy
1511  * called on the destination.
1512  */
1513 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
1514 {
1515     MigrationIncomingState *mis = migration_incoming_get_current();
1516 
1517     mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
1518                                                   *pcfd);
1519 }
1520 
1521 /* Unregister a handler for external shared memory postcopy
1522  */
1523 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
1524 {
1525     guint i;
1526     MigrationIncomingState *mis = migration_incoming_get_current();
1527     GArray *pcrfds = mis->postcopy_remote_fds;
1528 
1529     if (!pcrfds) {
1530         /* migration has already finished and freed the array */
1531         return;
1532     }
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;
1538         }
1539     }
1540 }
1541 
1542 bool postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file)
1543 {
1544     /*
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.
1547      */
1548     qemu_file_set_blocking(file, true);
1549     mis->postcopy_qemufile_dst = file;
1550     trace_postcopy_preempt_new_channel();
1551 
1552     /* Start the migration immediately */
1553     return true;
1554 }
1555 
1556 /*
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.
1559  */
1560 static void
1561 postcopy_preempt_send_channel_done(MigrationState *s,
1562                                    QIOChannel *ioc, Error *local_err)
1563 {
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();
1571     }
1572 
1573     /*
1574      * Kick the waiter in all cases.  The waiter should check upon
1575      * postcopy_qemufile_src to know whether it failed or not.
1576      */
1577     qemu_sem_post(&s->postcopy_qemufile_src_sem);
1578 }
1579 
1580 static void
1581 postcopy_preempt_tls_handshake(QIOTask *task, gpointer opaque)
1582 {
1583     g_autoptr(QIOChannel) ioc = QIO_CHANNEL(qio_task_get_source(task));
1584     MigrationState *s = opaque;
1585     Error *local_err = NULL;
1586 
1587     qio_task_propagate_error(task, &local_err);
1588     postcopy_preempt_send_channel_done(s, ioc, local_err);
1589 }
1590 
1591 static void
1592 postcopy_preempt_send_channel_new(QIOTask *task, gpointer opaque)
1593 {
1594     g_autoptr(QIOChannel) ioc = QIO_CHANNEL(qio_task_get_source(task));
1595     MigrationState *s = opaque;
1596     QIOChannelTLS *tioc;
1597     Error *local_err = NULL;
1598 
1599     if (qio_task_propagate_error(task, &local_err)) {
1600         goto out;
1601     }
1602 
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;
1607         }
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;
1614     }
1615 
1616 out:
1617     /* This handles both good and error cases */
1618     postcopy_preempt_send_channel_done(s, ioc, local_err);
1619 }
1620 
1621 /* Returns 0 if channel established, -1 for error. */
1622 int postcopy_preempt_wait_channel(MigrationState *s)
1623 {
1624     /* If preempt not enabled, no need to wait */
1625     if (!migrate_postcopy_preempt()) {
1626         return 0;
1627     }
1628 
1629     /*
1630      * We need the postcopy preempt channel to be established before
1631      * starting doing anything.
1632      */
1633     qemu_sem_wait(&s->postcopy_qemufile_src_sem);
1634 
1635     return s->postcopy_qemufile_src ? 0 : -1;
1636 }
1637 
1638 int postcopy_preempt_setup(MigrationState *s, Error **errp)
1639 {
1640     if (!migrate_postcopy_preempt()) {
1641         return 0;
1642     }
1643 
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;
1648     }
1649 
1650     /* Kick an async task to connect */
1651     socket_send_channel_create(postcopy_preempt_send_channel_new, s);
1652 
1653     return 0;
1654 }
1655 
1656 static void postcopy_pause_ram_fast_load(MigrationIncomingState *mis)
1657 {
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();
1663 }
1664 
1665 void *postcopy_preempt_thread(void *opaque)
1666 {
1667     MigrationIncomingState *mis = opaque;
1668     int ret;
1669 
1670     trace_postcopy_preempt_thread_entry();
1671 
1672     rcu_register_thread();
1673 
1674     qemu_sem_post(&mis->thread_sync_sem);
1675 
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;
1687         }
1688     }
1689     qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
1690 
1691     rcu_unregister_thread();
1692 
1693     trace_postcopy_preempt_thread_exit();
1694 
1695     return NULL;
1696 }
1697