xref: /qemu/migration/postcopy-ram.c (revision ca61e750)
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 
37 /* Arbitrary limit on size of each discard command,
38  * keeps them around ~200 bytes
39  */
40 #define MAX_DISCARDS_PER_COMMAND 12
41 
42 struct PostcopyDiscardState {
43     const char *ramblock_name;
44     uint16_t cur_entry;
45     /*
46      * Start and length of a discard range (bytes)
47      */
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;
52 };
53 
54 static NotifierWithReturnList postcopy_notifier_list;
55 
56 void postcopy_infrastructure_init(void)
57 {
58     notifier_with_return_list_init(&postcopy_notifier_list);
59 }
60 
61 void postcopy_add_notifier(NotifierWithReturn *nn)
62 {
63     notifier_with_return_list_add(&postcopy_notifier_list, nn);
64 }
65 
66 void postcopy_remove_notifier(NotifierWithReturn *n)
67 {
68     notifier_with_return_remove(n);
69 }
70 
71 int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp)
72 {
73     struct PostcopyNotifyData pnd;
74     pnd.reason = reason;
75     pnd.errp = errp;
76 
77     return notifier_with_return_list_notify(&postcopy_notifier_list,
78                                             &pnd);
79 }
80 
81 /*
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.
84  */
85 void postcopy_thread_create(MigrationIncomingState *mis,
86                             QemuThread *thread, const char *name,
87                             void *(*fn)(void *), int joinable)
88 {
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);
93 }
94 
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.
98  */
99 #if defined(__linux__)
100 
101 #include <poll.h>
102 #include <sys/ioctl.h>
103 #include <sys/syscall.h>
104 #include <asm/types.h> /* for __u64 */
105 #endif
106 
107 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
108 #include <sys/eventfd.h>
109 #include <linux/userfaultfd.h>
110 
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;
124 
125     /*
126      * Handler for exit event, necessary for
127      * releasing whole blocktime_ctx
128      */
129     Notifier exit_notifier;
130 } PostcopyBlocktimeContext;
131 
132 static void destroy_blocktime_context(struct PostcopyBlocktimeContext *ctx)
133 {
134     g_free(ctx->page_fault_vcpu_time);
135     g_free(ctx->vcpu_addr);
136     g_free(ctx->vcpu_blocktime);
137     g_free(ctx);
138 }
139 
140 static void migration_exit_cb(Notifier *n, void *data)
141 {
142     PostcopyBlocktimeContext *ctx = container_of(n, PostcopyBlocktimeContext,
143                                                  exit_notifier);
144     destroy_blocktime_context(ctx);
145 }
146 
147 static struct PostcopyBlocktimeContext *blocktime_context_new(void)
148 {
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);
155 
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;
160 }
161 
162 static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
163 {
164     MachineState *ms = MACHINE(qdev_get_machine());
165     uint32List *list = NULL;
166     int i;
167 
168     for (i = ms->smp.cpus - 1; i >= 0; i--) {
169         QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
170     }
171 
172     return list;
173 }
174 
175 /*
176  * This function just populates MigrationInfo from postcopy's
177  * blocktime context. It will not populate MigrationInfo,
178  * unless postcopy-blocktime capability was set.
179  *
180  * @info: pointer to MigrationInfo to populate
181  */
182 void fill_destination_postcopy_migration_info(MigrationInfo *info)
183 {
184     MigrationIncomingState *mis = migration_incoming_get_current();
185     PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
186 
187     if (!bc) {
188         return;
189     }
190 
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);
195 }
196 
197 static uint32_t get_postcopy_total_blocktime(void)
198 {
199     MigrationIncomingState *mis = migration_incoming_get_current();
200     PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
201 
202     if (!bc) {
203         return 0;
204     }
205 
206     return bc->total_blocktime;
207 }
208 
209 /**
210  * receive_ufd_features: check userfault fd features, to request only supported
211  * features in the future.
212  *
213  * Returns: true on success
214  *
215  * __NR_userfaultfd - should be checked before
216  *  @features: out parameter will contain uffdio_api.features provided by kernel
217  *              in case of success
218  */
219 static bool receive_ufd_features(uint64_t *features)
220 {
221     struct uffdio_api api_struct = {0};
222     int ufd;
223     bool ret = true;
224 
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;
231     }
232 
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;
241     }
242 
243     *features = api_struct.features;
244 
245 release_ufd:
246     close(ufd);
247     return ret;
248 }
249 
250 /**
251  * request_ufd_features: this function should be called only once on a newly
252  * opened ufd, subsequent calls will lead to error.
253  *
254  * Returns: true on success
255  *
256  * @ufd: fd obtained from userfaultfd syscall
257  * @features: bit mask see UFFD_API_FEATURES
258  */
259 static bool request_ufd_features(int ufd, uint64_t features)
260 {
261     struct uffdio_api api_struct = {0};
262     uint64_t ioctl_mask;
263 
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;
270     }
271 
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;
278     }
279 
280     return true;
281 }
282 
283 static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
284 {
285     uint64_t asked_features = 0;
286     static uint64_t supported_features;
287 
288     /*
289      * it's not possible to
290      * request UFFD_API twice per one fd
291      * userfault fd features is persistent
292      */
293     if (!supported_features) {
294         if (!receive_ufd_features(&supported_features)) {
295             error_report("%s failed", __func__);
296             return false;
297         }
298     }
299 
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();
306             }
307         }
308     }
309 #endif
310 
311     /*
312      * request features, even if asked_features is 0, due to
313      * kernel expects UFFD_API before UFFDIO_REGISTER, per
314      * userfault file descriptor
315      */
316     if (!request_ufd_features(ufd, asked_features)) {
317         error_report("%s failed: features %" PRIu64, __func__,
318                      asked_features);
319         return false;
320     }
321 
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;
331         }
332     }
333     return true;
334 }
335 
336 /* Callback from postcopy_ram_supported_by_host block iterator.
337  */
338 static int test_ramblock_postcopiable(RAMBlock *rb, void *opaque)
339 {
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);
343 
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;
349     }
350     return 0;
351 }
352 
353 /*
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.
357  */
358 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
359 {
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;
368 
369     if (qemu_target_page_size() > pagesize) {
370         error_report("Target page size bigger than host page size");
371         goto out;
372     }
373 
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;
379     }
380 
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;
385     }
386 
387     /* Version and features check */
388     if (!ufd_check_and_apply(ufd, mis)) {
389         goto out;
390     }
391 
392     /* We don't support postcopy with shared RAM yet */
393     if (foreach_not_ignored_block(test_ramblock_postcopiable, NULL)) {
394         goto out;
395     }
396 
397     /*
398      * userfault and mlock don't go together; we'll put it back later if
399      * it was enabled.
400      */
401     if (munlockall()) {
402         error_report("%s: munlockall: %s", __func__,  strerror(errno));
403         goto out;
404     }
405 
406     /*
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.
410      */
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;
417     }
418     g_assert(QEMU_PTR_IS_ALIGNED(testarea, pagesize));
419 
420     reg_struct.range.start = (uintptr_t)testarea;
421     reg_struct.range.len = pagesize;
422     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
423 
424     if (ioctl(ufd, UFFDIO_REGISTER, &reg_struct)) {
425         error_report("%s userfault register: %s", __func__, strerror(errno));
426         goto out;
427     }
428 
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;
434     }
435 
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;
443     }
444 
445     /* Success! */
446     ret = true;
447 out:
448     if (testarea) {
449         munmap(testarea, pagesize);
450     }
451     if (ufd != -1) {
452         close(ufd);
453     }
454     return ret;
455 }
456 
457 /*
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.
461  */
462 static int init_range(RAMBlock *rb, void *opaque)
463 {
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);
469 
470     /*
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.
474      */
475     rb->postcopy_length = length;
476 
477     /*
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)
482      */
483     if (ram_discard_range(block_name, 0, length)) {
484         return -1;
485     }
486 
487     return 0;
488 }
489 
490 /*
491  * At the end of migration, undo the effects of init_range
492  * opaque should be the MIS.
493  */
494 static int cleanup_range(RAMBlock *rb, void *opaque)
495 {
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);
503 
504     /*
505      * We turned off hugepage for the precopy stage with postcopy enabled
506      * we can turn it back on now.
507      */
508     qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE);
509 
510     /*
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.
514      */
515     range_struct.start = (uintptr_t)host_addr;
516     range_struct.len = length;
517 
518     if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) {
519         error_report("%s: userfault unregister %s", __func__, strerror(errno));
520 
521         return -1;
522     }
523 
524     return 0;
525 }
526 
527 /*
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
531  */
532 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
533 {
534     if (foreach_not_ignored_block(init_range, NULL)) {
535         return -1;
536     }
537 
538     return 0;
539 }
540 
541 static void postcopy_temp_pages_cleanup(MigrationIncomingState *mis)
542 {
543     int i;
544 
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;
551             }
552         }
553         g_free(mis->postcopy_tmp_pages);
554         mis->postcopy_tmp_pages = NULL;
555     }
556 
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;
560     }
561 }
562 
563 /*
564  * At the end of a migration where postcopy_ram_incoming_init was called.
565  */
566 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
567 {
568     trace_postcopy_ram_incoming_cleanup_entry();
569 
570     if (mis->have_fault_thread) {
571         Error *local_err = NULL;
572 
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);
578 
579         if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_END, &local_err)) {
580             error_report_err(local_err);
581             return -1;
582         }
583 
584         if (foreach_not_ignored_block(cleanup_range, mis)) {
585             return -1;
586         }
587 
588         trace_postcopy_ram_incoming_cleanup_closeuf();
589         close(mis->userfault_fd);
590         close(mis->userfault_event_fd);
591         mis->have_fault_thread = false;
592     }
593 
594     if (enable_mlock) {
595         if (os_mlock() < 0) {
596             error_report("mlock: %s", strerror(errno));
597             /*
598              * It doesn't feel right to fail at this point, we have a valid
599              * VM state.
600              */
601         }
602     }
603 
604     postcopy_temp_pages_cleanup(mis);
605 
606     trace_postcopy_ram_incoming_cleanup_blocktime(
607             get_postcopy_total_blocktime());
608 
609     trace_postcopy_ram_incoming_cleanup_exit();
610     return 0;
611 }
612 
613 /*
614  * Disable huge pages on an area
615  */
616 static int nhp_range(RAMBlock *rb, void *opaque)
617 {
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);
623 
624     /*
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.
628      */
629     qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE);
630 
631     return 0;
632 }
633 
634 /*
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
638  */
639 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
640 {
641     if (foreach_not_ignored_block(nhp_range, mis)) {
642         return -1;
643     }
644 
645     postcopy_state_set(POSTCOPY_INCOMING_DISCARD);
646 
647     return 0;
648 }
649 
650 /*
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
658  */
659 static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
660 {
661     MigrationIncomingState *mis = opaque;
662     struct uffdio_register reg_struct;
663 
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;
667 
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;
672     }
673     if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
674         error_report("%s userfault: Region doesn't support COPY", __func__);
675         return -1;
676     }
677     if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
678         qemu_ram_set_uf_zeroable(rb);
679     }
680 
681     return 0;
682 }
683 
684 int postcopy_wake_shared(struct PostCopyFD *pcfd,
685                          uint64_t client_addr,
686                          RAMBlock *rb)
687 {
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));
699     }
700     return ret;
701 }
702 
703 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
704                                  ram_addr_t start, uint64_t haddr)
705 {
706     void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
707 
708     /*
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.
712      *
713      * Checking a single bit is sufficient to handle pagesize > TPS as either
714      * all relevant bits are set or not.
715      */
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);
719 
720         return received ? 0 : postcopy_place_page_zero(mis, aligned, rb);
721     }
722 
723     return migrate_send_rp_req_pages(mis, rb, start, haddr);
724 }
725 
726 /*
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)
730  */
731 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
732                                  uint64_t client_addr, uint64_t rb_offset)
733 {
734     uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
735     MigrationIncomingState *mis = migration_incoming_get_current();
736 
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);
743     }
744     postcopy_request_page(mis, rb, aligned_rbo, client_addr);
745     return 0;
746 }
747 
748 static int get_mem_fault_cpu_index(uint32_t pid)
749 {
750     CPUState *cpu_iter;
751 
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;
756         }
757     }
758     trace_get_mem_fault_cpu_index(-1, pid);
759     return -1;
760 }
761 
762 static uint32_t get_low_time_offset(PostcopyBlocktimeContext *dc)
763 {
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;
767 }
768 
769 /*
770  * This function is being called when pagefault occurs. It
771  * tracks down vCPU blocking time.
772  *
773  * @addr: faulted host virtual address
774  * @ptid: faulted process thread id
775  * @rb: ramblock appropriate to addr
776  */
777 static void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
778                                           RAMBlock *rb)
779 {
780     int cpu, already_received;
781     MigrationIncomingState *mis = migration_incoming_get_current();
782     PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
783     uint32_t low_time_offset;
784 
785     if (!dc || ptid == 0) {
786         return;
787     }
788     cpu = get_mem_fault_cpu_index(ptid);
789     if (cpu < 0) {
790         return;
791     }
792 
793     low_time_offset = get_low_time_offset(dc);
794     if (dc->vcpu_addr[cpu] == 0) {
795         qatomic_inc(&dc->smp_cpus_down);
796     }
797 
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);
801 
802     /*
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
806      */
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);
812     }
813     trace_mark_postcopy_blocktime_begin(addr, dc, dc->page_fault_vcpu_time[cpu],
814                                         cpu, already_received);
815 }
816 
817 /*
818  *  This function just provide calculated blocktime per cpu and trace it.
819  *  Total blocktime is calculated in mark_postcopy_blocktime_end.
820  *
821  *
822  * Assume we have 3 CPU
823  *
824  *      S1        E1           S1               E1
825  * -----***********------------xxx***************------------------------> CPU1
826  *
827  *             S2                E2
828  * ------------****************xxx---------------------------------------> CPU2
829  *
830  *                         S3            E3
831  * ------------------------****xxx********-------------------------------> CPU3
832  *
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)
841  *
842  * @addr: host virtual address
843  */
844 static void mark_postcopy_blocktime_end(uintptr_t addr)
845 {
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;
853 
854     if (!dc) {
855         return;
856     }
857 
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;
865 
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;
870         }
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;
880         }
881         /* continue cycle, due to one page could affect several vCPUs */
882         dc->vcpu_blocktime[i] += vcpu_blocktime;
883     }
884 
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);
889     }
890     trace_mark_postcopy_blocktime_end(addr, dc, dc->total_blocktime,
891                                       affected_cpu);
892 }
893 
894 static void postcopy_pause_fault_thread(MigrationIncomingState *mis)
895 {
896     trace_postcopy_pause_fault_thread();
897     qemu_sem_wait(&mis->postcopy_pause_sem_fault);
898     trace_postcopy_pause_fault_thread_continued();
899 }
900 
901 /*
902  * Handle faults detected by the USERFAULT markings
903  */
904 static void *postcopy_ram_fault_thread(void *opaque)
905 {
906     MigrationIncomingState *mis = opaque;
907     struct uffd_msg msg;
908     int ret;
909     size_t index;
910     RAMBlock *rb = NULL;
911 
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);
916 
917     struct pollfd *pfd;
918     size_t pfd_len = 2 + mis->postcopy_remote_fds->len;
919 
920     pfd = g_new0(struct pollfd, pfd_len);
921 
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);
934     }
935 
936     while (true) {
937         ram_addr_t rb_offset;
938         int poll_result;
939 
940         /*
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
944          */
945 
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;
950         }
951 
952         if (!mis->to_src_file) {
953             /*
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.
957              */
958             postcopy_pause_fault_thread(mis);
959         }
960 
961         if (pfd[1].revents) {
962             uint64_t tmp64 = 0;
963 
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__);
968             }
969 
970             if (qatomic_read(&mis->fault_thread_quit)) {
971                 trace_postcopy_ram_fault_thread_quit();
972                 break;
973             }
974         }
975 
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) {
981                     /*
982                      * if a wake up happens on the other thread just after
983                      * the poll, there is nothing to read.
984                      */
985                     continue;
986                 }
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 */
997                 }
998             }
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 */
1003             }
1004 
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;
1012             }
1013 
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);
1022 
1023 retry:
1024             /*
1025              * Send the request to the source - we want to request one
1026              * of our host page sizes (which is >= TPS)
1027              */
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;
1034             }
1035         }
1036 
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);
1044 
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;
1051                 }
1052 
1053                 ret = read(pcfd->fd, &msg, sizeof(msg));
1054                 if (ret != sizeof(msg)) {
1055                     if (errno == EAGAIN) {
1056                         /*
1057                          * if a wake up happens on the other thread just after
1058                          * the poll, there is nothing to read.
1059                          */
1060                         continue;
1061                     }
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*/
1075                     }
1076                 }
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 */
1082                 }
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? */
1089                 }
1090             }
1091         }
1092     }
1093     rcu_unregister_thread();
1094     trace_postcopy_ram_fault_thread_exit();
1095     g_free(pfd);
1096     return NULL;
1097 }
1098 
1099 static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
1100 {
1101     PostcopyTmpPage *tmp_page;
1102     int err, i, channels;
1103     void *temp_page;
1104 
1105     /* TODO: will be boosted when enable postcopy preemption */
1106     mis->postcopy_channels = 1;
1107 
1108     channels = mis->postcopy_channels;
1109     mis->postcopy_tmp_pages = g_malloc0_n(sizeof(PostcopyTmpPage), channels);
1110 
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;
1121         }
1122         tmp_page->tmp_huge_page = temp_page;
1123         /* Initialize default states for each tmp page */
1124         postcopy_temp_page_reset(tmp_page);
1125     }
1126 
1127     /*
1128      * Map large zero page when kernel can't use UFFDIO_ZEROPAGE for hugepages
1129      */
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;
1139     }
1140 
1141     memset(mis->postcopy_tmp_zero_page, '\0', mis->largest_page_size);
1142 
1143     return 0;
1144 }
1145 
1146 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1147 {
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;
1154     }
1155 
1156     /*
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.
1159      */
1160     if (!ufd_check_and_apply(mis->userfault_fd, mis)) {
1161         return -1;
1162     }
1163 
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;
1171     }
1172 
1173     postcopy_thread_create(mis, &mis->fault_thread, "postcopy/fault",
1174                            postcopy_ram_fault_thread, QEMU_THREAD_JOINABLE);
1175     mis->have_fault_thread = true;
1176 
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;
1181     }
1182 
1183     if (postcopy_temp_pages_setup(mis)) {
1184         /* Error dumped in the sub-function */
1185         return -1;
1186     }
1187 
1188     trace_postcopy_ram_enable_notify();
1189 
1190     return 0;
1191 }
1192 
1193 static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
1194                                void *from_addr, uint64_t pagesize, RAMBlock *rb)
1195 {
1196     int userfault_fd = mis->userfault_fd;
1197     int ret;
1198 
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);
1212     }
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());
1217         /*
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.
1220          */
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);
1225         }
1226         qemu_mutex_unlock(&mis->page_request_mutex);
1227         mark_postcopy_blocktime_end((uintptr_t)host_addr);
1228     }
1229     return ret;
1230 }
1231 
1232 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
1233 {
1234     int i;
1235     MigrationIncomingState *mis = migration_incoming_get_current();
1236     GArray *pcrfds = mis->postcopy_remote_fds;
1237 
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;
1243         }
1244     }
1245     return 0;
1246 }
1247 
1248 /*
1249  * Place a host page (from) at (host) atomically
1250  * returns 0 on success
1251  */
1252 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1253                         RAMBlock *rb)
1254 {
1255     size_t pagesize = qemu_ram_pagesize(rb);
1256 
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.
1261      */
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);
1266 
1267         return -e;
1268     }
1269 
1270     trace_postcopy_place_page(host);
1271     return postcopy_notify_shared_wake(rb,
1272                                        qemu_ram_block_host_offset(rb, host));
1273 }
1274 
1275 /*
1276  * Place a zero page at (host) atomically
1277  * returns 0 on success
1278  */
1279 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1280                              RAMBlock *rb)
1281 {
1282     size_t pagesize = qemu_ram_pagesize(rb);
1283     trace_postcopy_place_page_zero(host);
1284 
1285     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
1286      * but it's not available for everything (e.g. hugetlbpages)
1287      */
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);
1293 
1294             return -e;
1295         }
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);
1301     }
1302 }
1303 
1304 #else
1305 /* No target OS support, stubs just fail */
1306 void fill_destination_postcopy_migration_info(MigrationInfo *info)
1307 {
1308 }
1309 
1310 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
1311 {
1312     error_report("%s: No OS support", __func__);
1313     return false;
1314 }
1315 
1316 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1317 {
1318     error_report("postcopy_ram_incoming_init: No OS support");
1319     return -1;
1320 }
1321 
1322 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
1323 {
1324     assert(0);
1325     return -1;
1326 }
1327 
1328 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
1329 {
1330     assert(0);
1331     return -1;
1332 }
1333 
1334 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
1335                                  uint64_t client_addr, uint64_t rb_offset)
1336 {
1337     assert(0);
1338     return -1;
1339 }
1340 
1341 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1342 {
1343     assert(0);
1344     return -1;
1345 }
1346 
1347 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1348                         RAMBlock *rb)
1349 {
1350     assert(0);
1351     return -1;
1352 }
1353 
1354 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1355                         RAMBlock *rb)
1356 {
1357     assert(0);
1358     return -1;
1359 }
1360 
1361 int postcopy_wake_shared(struct PostCopyFD *pcfd,
1362                          uint64_t client_addr,
1363                          RAMBlock *rb)
1364 {
1365     assert(0);
1366     return -1;
1367 }
1368 #endif
1369 
1370 /* ------------------------------------------------------------------------- */
1371 void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page)
1372 {
1373     tmp_page->target_pages = 0;
1374     tmp_page->host_addr = NULL;
1375     /*
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.
1378      */
1379     tmp_page->all_zero = true;
1380 }
1381 
1382 void postcopy_fault_thread_notify(MigrationIncomingState *mis)
1383 {
1384     uint64_t tmp64 = 1;
1385 
1386     /*
1387      * Wakeup the fault_thread.  It's an eventfd that should currently
1388      * be at 0, we're going to increment it to 1
1389      */
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));
1394     }
1395 }
1396 
1397 /**
1398  * postcopy_discard_send_init: Called at the start of each RAMBlock before
1399  *   asking to discard individual ranges.
1400  *
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.
1404  */
1405 static PostcopyDiscardState pds = {0};
1406 void postcopy_discard_send_init(MigrationState *ms, const char *name)
1407 {
1408     pds.ramblock_name = name;
1409     pds.cur_entry = 0;
1410     pds.nsentwords = 0;
1411     pds.nsentcmds = 0;
1412 }
1413 
1414 /**
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.
1418  *
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)
1422  */
1423 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
1424                                  unsigned long length)
1425 {
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++;
1433 
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;
1443     }
1444 }
1445 
1446 /**
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
1449  *
1450  * @ms: Current migration state.
1451  */
1452 void postcopy_discard_send_finish(MigrationState *ms)
1453 {
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++;
1462     }
1463 
1464     trace_postcopy_discard_send_finish(pds.ramblock_name, pds.nsentwords,
1465                                        pds.nsentcmds);
1466 }
1467 
1468 /*
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.
1472  */
1473 static PostcopyState incoming_postcopy_state;
1474 
1475 PostcopyState  postcopy_state_get(void)
1476 {
1477     return qatomic_mb_read(&incoming_postcopy_state);
1478 }
1479 
1480 /* Set the state and return the old state */
1481 PostcopyState postcopy_state_set(PostcopyState new_state)
1482 {
1483     return qatomic_xchg(&incoming_postcopy_state, new_state);
1484 }
1485 
1486 /* Register a handler for external shared memory postcopy
1487  * called on the destination.
1488  */
1489 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
1490 {
1491     MigrationIncomingState *mis = migration_incoming_get_current();
1492 
1493     mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
1494                                                   *pcfd);
1495 }
1496 
1497 /* Unregister a handler for external shared memory postcopy
1498  */
1499 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
1500 {
1501     guint i;
1502     MigrationIncomingState *mis = migration_incoming_get_current();
1503     GArray *pcrfds = mis->postcopy_remote_fds;
1504 
1505     if (!pcrfds) {
1506         /* migration has already finished and freed the array */
1507         return;
1508     }
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;
1514         }
1515     }
1516 }
1517