xref: /qemu/migration/postcopy-ram.c (revision 93c9aeed)
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 /* Postcopy needs to detect accesses to pages that haven't yet been copied
82  * across, and efficiently map new pages in, the techniques for doing this
83  * are target OS specific.
84  */
85 #if defined(__linux__)
86 
87 #include <poll.h>
88 #include <sys/ioctl.h>
89 #include <sys/syscall.h>
90 #include <asm/types.h> /* for __u64 */
91 #endif
92 
93 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
94 #include <sys/eventfd.h>
95 #include <linux/userfaultfd.h>
96 
97 typedef struct PostcopyBlocktimeContext {
98     /* time when page fault initiated per vCPU */
99     uint32_t *page_fault_vcpu_time;
100     /* page address per vCPU */
101     uintptr_t *vcpu_addr;
102     uint32_t total_blocktime;
103     /* blocktime per vCPU */
104     uint32_t *vcpu_blocktime;
105     /* point in time when last page fault was initiated */
106     uint32_t last_begin;
107     /* number of vCPU are suspended */
108     int smp_cpus_down;
109     uint64_t start_time;
110 
111     /*
112      * Handler for exit event, necessary for
113      * releasing whole blocktime_ctx
114      */
115     Notifier exit_notifier;
116 } PostcopyBlocktimeContext;
117 
118 static void destroy_blocktime_context(struct PostcopyBlocktimeContext *ctx)
119 {
120     g_free(ctx->page_fault_vcpu_time);
121     g_free(ctx->vcpu_addr);
122     g_free(ctx->vcpu_blocktime);
123     g_free(ctx);
124 }
125 
126 static void migration_exit_cb(Notifier *n, void *data)
127 {
128     PostcopyBlocktimeContext *ctx = container_of(n, PostcopyBlocktimeContext,
129                                                  exit_notifier);
130     destroy_blocktime_context(ctx);
131 }
132 
133 static struct PostcopyBlocktimeContext *blocktime_context_new(void)
134 {
135     MachineState *ms = MACHINE(qdev_get_machine());
136     unsigned int smp_cpus = ms->smp.cpus;
137     PostcopyBlocktimeContext *ctx = g_new0(PostcopyBlocktimeContext, 1);
138     ctx->page_fault_vcpu_time = g_new0(uint32_t, smp_cpus);
139     ctx->vcpu_addr = g_new0(uintptr_t, smp_cpus);
140     ctx->vcpu_blocktime = g_new0(uint32_t, smp_cpus);
141 
142     ctx->exit_notifier.notify = migration_exit_cb;
143     ctx->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
144     qemu_add_exit_notifier(&ctx->exit_notifier);
145     return ctx;
146 }
147 
148 static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
149 {
150     MachineState *ms = MACHINE(qdev_get_machine());
151     uint32List *list = NULL;
152     int i;
153 
154     for (i = ms->smp.cpus - 1; i >= 0; i--) {
155         QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
156     }
157 
158     return list;
159 }
160 
161 /*
162  * This function just populates MigrationInfo from postcopy's
163  * blocktime context. It will not populate MigrationInfo,
164  * unless postcopy-blocktime capability was set.
165  *
166  * @info: pointer to MigrationInfo to populate
167  */
168 void fill_destination_postcopy_migration_info(MigrationInfo *info)
169 {
170     MigrationIncomingState *mis = migration_incoming_get_current();
171     PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
172 
173     if (!bc) {
174         return;
175     }
176 
177     info->has_postcopy_blocktime = true;
178     info->postcopy_blocktime = bc->total_blocktime;
179     info->has_postcopy_vcpu_blocktime = true;
180     info->postcopy_vcpu_blocktime = get_vcpu_blocktime_list(bc);
181 }
182 
183 static uint32_t get_postcopy_total_blocktime(void)
184 {
185     MigrationIncomingState *mis = migration_incoming_get_current();
186     PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
187 
188     if (!bc) {
189         return 0;
190     }
191 
192     return bc->total_blocktime;
193 }
194 
195 /**
196  * receive_ufd_features: check userfault fd features, to request only supported
197  * features in the future.
198  *
199  * Returns: true on success
200  *
201  * __NR_userfaultfd - should be checked before
202  *  @features: out parameter will contain uffdio_api.features provided by kernel
203  *              in case of success
204  */
205 static bool receive_ufd_features(uint64_t *features)
206 {
207     struct uffdio_api api_struct = {0};
208     int ufd;
209     bool ret = true;
210 
211     /* if we are here __NR_userfaultfd should exists */
212     ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
213     if (ufd == -1) {
214         error_report("%s: syscall __NR_userfaultfd failed: %s", __func__,
215                      strerror(errno));
216         return false;
217     }
218 
219     /* ask features */
220     api_struct.api = UFFD_API;
221     api_struct.features = 0;
222     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
223         error_report("%s: UFFDIO_API failed: %s", __func__,
224                      strerror(errno));
225         ret = false;
226         goto release_ufd;
227     }
228 
229     *features = api_struct.features;
230 
231 release_ufd:
232     close(ufd);
233     return ret;
234 }
235 
236 /**
237  * request_ufd_features: this function should be called only once on a newly
238  * opened ufd, subsequent calls will lead to error.
239  *
240  * Returns: true on success
241  *
242  * @ufd: fd obtained from userfaultfd syscall
243  * @features: bit mask see UFFD_API_FEATURES
244  */
245 static bool request_ufd_features(int ufd, uint64_t features)
246 {
247     struct uffdio_api api_struct = {0};
248     uint64_t ioctl_mask;
249 
250     api_struct.api = UFFD_API;
251     api_struct.features = features;
252     if (ioctl(ufd, UFFDIO_API, &api_struct)) {
253         error_report("%s failed: UFFDIO_API failed: %s", __func__,
254                      strerror(errno));
255         return false;
256     }
257 
258     ioctl_mask = (__u64)1 << _UFFDIO_REGISTER |
259                  (__u64)1 << _UFFDIO_UNREGISTER;
260     if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
261         error_report("Missing userfault features: %" PRIx64,
262                      (uint64_t)(~api_struct.ioctls & ioctl_mask));
263         return false;
264     }
265 
266     return true;
267 }
268 
269 static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis)
270 {
271     uint64_t asked_features = 0;
272     static uint64_t supported_features;
273 
274     /*
275      * it's not possible to
276      * request UFFD_API twice per one fd
277      * userfault fd features is persistent
278      */
279     if (!supported_features) {
280         if (!receive_ufd_features(&supported_features)) {
281             error_report("%s failed", __func__);
282             return false;
283         }
284     }
285 
286 #ifdef UFFD_FEATURE_THREAD_ID
287     if (UFFD_FEATURE_THREAD_ID & supported_features) {
288         asked_features |= UFFD_FEATURE_THREAD_ID;
289         if (migrate_postcopy_blocktime()) {
290             if (!mis->blocktime_ctx) {
291                 mis->blocktime_ctx = blocktime_context_new();
292             }
293         }
294     }
295 #endif
296 
297     /*
298      * request features, even if asked_features is 0, due to
299      * kernel expects UFFD_API before UFFDIO_REGISTER, per
300      * userfault file descriptor
301      */
302     if (!request_ufd_features(ufd, asked_features)) {
303         error_report("%s failed: features %" PRIu64, __func__,
304                      asked_features);
305         return false;
306     }
307 
308     if (qemu_real_host_page_size != ram_pagesize_summary()) {
309         bool have_hp = false;
310         /* We've got a huge page */
311 #ifdef UFFD_FEATURE_MISSING_HUGETLBFS
312         have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
313 #endif
314         if (!have_hp) {
315             error_report("Userfault on this host does not support huge pages");
316             return false;
317         }
318     }
319     return true;
320 }
321 
322 /* Callback from postcopy_ram_supported_by_host block iterator.
323  */
324 static int test_ramblock_postcopiable(RAMBlock *rb, void *opaque)
325 {
326     const char *block_name = qemu_ram_get_idstr(rb);
327     ram_addr_t length = qemu_ram_get_used_length(rb);
328     size_t pagesize = qemu_ram_pagesize(rb);
329 
330     if (length % pagesize) {
331         error_report("Postcopy requires RAM blocks to be a page size multiple,"
332                      " block %s is 0x" RAM_ADDR_FMT " bytes with a "
333                      "page size of 0x%zx", block_name, length, pagesize);
334         return 1;
335     }
336     return 0;
337 }
338 
339 /*
340  * Note: This has the side effect of munlock'ing all of RAM, that's
341  * normally fine since if the postcopy succeeds it gets turned back on at the
342  * end.
343  */
344 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
345 {
346     long pagesize = qemu_real_host_page_size;
347     int ufd = -1;
348     bool ret = false; /* Error unless we change it */
349     void *testarea = NULL;
350     struct uffdio_register reg_struct;
351     struct uffdio_range range_struct;
352     uint64_t feature_mask;
353     Error *local_err = NULL;
354 
355     if (qemu_target_page_size() > pagesize) {
356         error_report("Target page size bigger than host page size");
357         goto out;
358     }
359 
360     ufd = syscall(__NR_userfaultfd, O_CLOEXEC);
361     if (ufd == -1) {
362         error_report("%s: userfaultfd not available: %s", __func__,
363                      strerror(errno));
364         goto out;
365     }
366 
367     /* Give devices a chance to object */
368     if (postcopy_notify(POSTCOPY_NOTIFY_PROBE, &local_err)) {
369         error_report_err(local_err);
370         goto out;
371     }
372 
373     /* Version and features check */
374     if (!ufd_check_and_apply(ufd, mis)) {
375         goto out;
376     }
377 
378     /* We don't support postcopy with shared RAM yet */
379     if (foreach_not_ignored_block(test_ramblock_postcopiable, NULL)) {
380         goto out;
381     }
382 
383     /*
384      * userfault and mlock don't go together; we'll put it back later if
385      * it was enabled.
386      */
387     if (munlockall()) {
388         error_report("%s: munlockall: %s", __func__,  strerror(errno));
389         goto out;
390     }
391 
392     /*
393      *  We need to check that the ops we need are supported on anon memory
394      *  To do that we need to register a chunk and see the flags that
395      *  are returned.
396      */
397     testarea = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE |
398                                     MAP_ANONYMOUS, -1, 0);
399     if (testarea == MAP_FAILED) {
400         error_report("%s: Failed to map test area: %s", __func__,
401                      strerror(errno));
402         goto out;
403     }
404     g_assert(QEMU_PTR_IS_ALIGNED(testarea, pagesize));
405 
406     reg_struct.range.start = (uintptr_t)testarea;
407     reg_struct.range.len = pagesize;
408     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
409 
410     if (ioctl(ufd, UFFDIO_REGISTER, &reg_struct)) {
411         error_report("%s userfault register: %s", __func__, strerror(errno));
412         goto out;
413     }
414 
415     range_struct.start = (uintptr_t)testarea;
416     range_struct.len = pagesize;
417     if (ioctl(ufd, UFFDIO_UNREGISTER, &range_struct)) {
418         error_report("%s userfault unregister: %s", __func__, strerror(errno));
419         goto out;
420     }
421 
422     feature_mask = (__u64)1 << _UFFDIO_WAKE |
423                    (__u64)1 << _UFFDIO_COPY |
424                    (__u64)1 << _UFFDIO_ZEROPAGE;
425     if ((reg_struct.ioctls & feature_mask) != feature_mask) {
426         error_report("Missing userfault map features: %" PRIx64,
427                      (uint64_t)(~reg_struct.ioctls & feature_mask));
428         goto out;
429     }
430 
431     /* Success! */
432     ret = true;
433 out:
434     if (testarea) {
435         munmap(testarea, pagesize);
436     }
437     if (ufd != -1) {
438         close(ufd);
439     }
440     return ret;
441 }
442 
443 /*
444  * Setup an area of RAM so that it *can* be used for postcopy later; this
445  * must be done right at the start prior to pre-copy.
446  * opaque should be the MIS.
447  */
448 static int init_range(RAMBlock *rb, void *opaque)
449 {
450     const char *block_name = qemu_ram_get_idstr(rb);
451     void *host_addr = qemu_ram_get_host_addr(rb);
452     ram_addr_t offset = qemu_ram_get_offset(rb);
453     ram_addr_t length = qemu_ram_get_used_length(rb);
454     trace_postcopy_init_range(block_name, host_addr, offset, length);
455 
456     /*
457      * Save the used_length before running the guest. In case we have to
458      * resize RAM blocks when syncing RAM block sizes from the source during
459      * precopy, we'll update it manually via the ram block notifier.
460      */
461     rb->postcopy_length = length;
462 
463     /*
464      * We need the whole of RAM to be truly empty for postcopy, so things
465      * like ROMs and any data tables built during init must be zero'd
466      * - we're going to get the copy from the source anyway.
467      * (Precopy will just overwrite this data, so doesn't need the discard)
468      */
469     if (ram_discard_range(block_name, 0, length)) {
470         return -1;
471     }
472 
473     return 0;
474 }
475 
476 /*
477  * At the end of migration, undo the effects of init_range
478  * opaque should be the MIS.
479  */
480 static int cleanup_range(RAMBlock *rb, void *opaque)
481 {
482     const char *block_name = qemu_ram_get_idstr(rb);
483     void *host_addr = qemu_ram_get_host_addr(rb);
484     ram_addr_t offset = qemu_ram_get_offset(rb);
485     ram_addr_t length = rb->postcopy_length;
486     MigrationIncomingState *mis = opaque;
487     struct uffdio_range range_struct;
488     trace_postcopy_cleanup_range(block_name, host_addr, offset, length);
489 
490     /*
491      * We turned off hugepage for the precopy stage with postcopy enabled
492      * we can turn it back on now.
493      */
494     qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE);
495 
496     /*
497      * We can also turn off userfault now since we should have all the
498      * pages.   It can be useful to leave it on to debug postcopy
499      * if you're not sure it's always getting every page.
500      */
501     range_struct.start = (uintptr_t)host_addr;
502     range_struct.len = length;
503 
504     if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) {
505         error_report("%s: userfault unregister %s", __func__, strerror(errno));
506 
507         return -1;
508     }
509 
510     return 0;
511 }
512 
513 /*
514  * Initialise postcopy-ram, setting the RAM to a state where we can go into
515  * postcopy later; must be called prior to any precopy.
516  * called from arch_init's similarly named ram_postcopy_incoming_init
517  */
518 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
519 {
520     if (foreach_not_ignored_block(init_range, NULL)) {
521         return -1;
522     }
523 
524     return 0;
525 }
526 
527 static void postcopy_temp_pages_cleanup(MigrationIncomingState *mis)
528 {
529     if (mis->postcopy_tmp_page) {
530         munmap(mis->postcopy_tmp_page, mis->largest_page_size);
531         mis->postcopy_tmp_page = NULL;
532     }
533 
534     if (mis->postcopy_tmp_zero_page) {
535         munmap(mis->postcopy_tmp_zero_page, mis->largest_page_size);
536         mis->postcopy_tmp_zero_page = NULL;
537     }
538 }
539 
540 /*
541  * At the end of a migration where postcopy_ram_incoming_init was called.
542  */
543 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
544 {
545     trace_postcopy_ram_incoming_cleanup_entry();
546 
547     if (mis->have_fault_thread) {
548         Error *local_err = NULL;
549 
550         /* Let the fault thread quit */
551         qatomic_set(&mis->fault_thread_quit, 1);
552         postcopy_fault_thread_notify(mis);
553         trace_postcopy_ram_incoming_cleanup_join();
554         qemu_thread_join(&mis->fault_thread);
555 
556         if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_END, &local_err)) {
557             error_report_err(local_err);
558             return -1;
559         }
560 
561         if (foreach_not_ignored_block(cleanup_range, mis)) {
562             return -1;
563         }
564 
565         trace_postcopy_ram_incoming_cleanup_closeuf();
566         close(mis->userfault_fd);
567         close(mis->userfault_event_fd);
568         mis->have_fault_thread = false;
569     }
570 
571     if (enable_mlock) {
572         if (os_mlock() < 0) {
573             error_report("mlock: %s", strerror(errno));
574             /*
575              * It doesn't feel right to fail at this point, we have a valid
576              * VM state.
577              */
578         }
579     }
580 
581     postcopy_temp_pages_cleanup(mis);
582 
583     trace_postcopy_ram_incoming_cleanup_blocktime(
584             get_postcopy_total_blocktime());
585 
586     trace_postcopy_ram_incoming_cleanup_exit();
587     return 0;
588 }
589 
590 /*
591  * Disable huge pages on an area
592  */
593 static int nhp_range(RAMBlock *rb, void *opaque)
594 {
595     const char *block_name = qemu_ram_get_idstr(rb);
596     void *host_addr = qemu_ram_get_host_addr(rb);
597     ram_addr_t offset = qemu_ram_get_offset(rb);
598     ram_addr_t length = rb->postcopy_length;
599     trace_postcopy_nhp_range(block_name, host_addr, offset, length);
600 
601     /*
602      * Before we do discards we need to ensure those discards really
603      * do delete areas of the page, even if THP thinks a hugepage would
604      * be a good idea, so force hugepages off.
605      */
606     qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE);
607 
608     return 0;
609 }
610 
611 /*
612  * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
613  * however leaving it until after precopy means that most of the precopy
614  * data is still THPd
615  */
616 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
617 {
618     if (foreach_not_ignored_block(nhp_range, mis)) {
619         return -1;
620     }
621 
622     postcopy_state_set(POSTCOPY_INCOMING_DISCARD);
623 
624     return 0;
625 }
626 
627 /*
628  * Mark the given area of RAM as requiring notification to unwritten areas
629  * Used as a  callback on foreach_not_ignored_block.
630  *   host_addr: Base of area to mark
631  *   offset: Offset in the whole ram arena
632  *   length: Length of the section
633  *   opaque: MigrationIncomingState pointer
634  * Returns 0 on success
635  */
636 static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
637 {
638     MigrationIncomingState *mis = opaque;
639     struct uffdio_register reg_struct;
640 
641     reg_struct.range.start = (uintptr_t)qemu_ram_get_host_addr(rb);
642     reg_struct.range.len = rb->postcopy_length;
643     reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
644 
645     /* Now tell our userfault_fd that it's responsible for this area */
646     if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, &reg_struct)) {
647         error_report("%s userfault register: %s", __func__, strerror(errno));
648         return -1;
649     }
650     if (!(reg_struct.ioctls & ((__u64)1 << _UFFDIO_COPY))) {
651         error_report("%s userfault: Region doesn't support COPY", __func__);
652         return -1;
653     }
654     if (reg_struct.ioctls & ((__u64)1 << _UFFDIO_ZEROPAGE)) {
655         qemu_ram_set_uf_zeroable(rb);
656     }
657 
658     return 0;
659 }
660 
661 int postcopy_wake_shared(struct PostCopyFD *pcfd,
662                          uint64_t client_addr,
663                          RAMBlock *rb)
664 {
665     size_t pagesize = qemu_ram_pagesize(rb);
666     struct uffdio_range range;
667     int ret;
668     trace_postcopy_wake_shared(client_addr, qemu_ram_get_idstr(rb));
669     range.start = ROUND_DOWN(client_addr, pagesize);
670     range.len = pagesize;
671     ret = ioctl(pcfd->fd, UFFDIO_WAKE, &range);
672     if (ret) {
673         error_report("%s: Failed to wake: %zx in %s (%s)",
674                      __func__, (size_t)client_addr, qemu_ram_get_idstr(rb),
675                      strerror(errno));
676     }
677     return ret;
678 }
679 
680 static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
681                                  ram_addr_t start, uint64_t haddr)
682 {
683     void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
684 
685     /*
686      * Discarded pages (via RamDiscardManager) are never migrated. On unlikely
687      * access, place a zeropage, which will also set the relevant bits in the
688      * recv_bitmap accordingly, so we won't try placing a zeropage twice.
689      *
690      * Checking a single bit is sufficient to handle pagesize > TPS as either
691      * all relevant bits are set or not.
692      */
693     assert(QEMU_IS_ALIGNED(start, qemu_ram_pagesize(rb)));
694     if (ramblock_page_is_discarded(rb, start)) {
695         bool received = ramblock_recv_bitmap_test_byte_offset(rb, start);
696 
697         return received ? 0 : postcopy_place_page_zero(mis, aligned, rb);
698     }
699 
700     return migrate_send_rp_req_pages(mis, rb, start, haddr);
701 }
702 
703 /*
704  * Callback from shared fault handlers to ask for a page,
705  * the page must be specified by a RAMBlock and an offset in that rb
706  * Note: Only for use by shared fault handlers (in fault thread)
707  */
708 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
709                                  uint64_t client_addr, uint64_t rb_offset)
710 {
711     uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
712     MigrationIncomingState *mis = migration_incoming_get_current();
713 
714     trace_postcopy_request_shared_page(pcfd->idstr, qemu_ram_get_idstr(rb),
715                                        rb_offset);
716     if (ramblock_recv_bitmap_test_byte_offset(rb, aligned_rbo)) {
717         trace_postcopy_request_shared_page_present(pcfd->idstr,
718                                         qemu_ram_get_idstr(rb), rb_offset);
719         return postcopy_wake_shared(pcfd, client_addr, rb);
720     }
721     postcopy_request_page(mis, rb, aligned_rbo, client_addr);
722     return 0;
723 }
724 
725 static int get_mem_fault_cpu_index(uint32_t pid)
726 {
727     CPUState *cpu_iter;
728 
729     CPU_FOREACH(cpu_iter) {
730         if (cpu_iter->thread_id == pid) {
731             trace_get_mem_fault_cpu_index(cpu_iter->cpu_index, pid);
732             return cpu_iter->cpu_index;
733         }
734     }
735     trace_get_mem_fault_cpu_index(-1, pid);
736     return -1;
737 }
738 
739 static uint32_t get_low_time_offset(PostcopyBlocktimeContext *dc)
740 {
741     int64_t start_time_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
742                                     dc->start_time;
743     return start_time_offset < 1 ? 1 : start_time_offset & UINT32_MAX;
744 }
745 
746 /*
747  * This function is being called when pagefault occurs. It
748  * tracks down vCPU blocking time.
749  *
750  * @addr: faulted host virtual address
751  * @ptid: faulted process thread id
752  * @rb: ramblock appropriate to addr
753  */
754 static void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
755                                           RAMBlock *rb)
756 {
757     int cpu, already_received;
758     MigrationIncomingState *mis = migration_incoming_get_current();
759     PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
760     uint32_t low_time_offset;
761 
762     if (!dc || ptid == 0) {
763         return;
764     }
765     cpu = get_mem_fault_cpu_index(ptid);
766     if (cpu < 0) {
767         return;
768     }
769 
770     low_time_offset = get_low_time_offset(dc);
771     if (dc->vcpu_addr[cpu] == 0) {
772         qatomic_inc(&dc->smp_cpus_down);
773     }
774 
775     qatomic_xchg(&dc->last_begin, low_time_offset);
776     qatomic_xchg(&dc->page_fault_vcpu_time[cpu], low_time_offset);
777     qatomic_xchg(&dc->vcpu_addr[cpu], addr);
778 
779     /*
780      * check it here, not at the beginning of the function,
781      * due to, check could occur early than bitmap_set in
782      * qemu_ufd_copy_ioctl
783      */
784     already_received = ramblock_recv_bitmap_test(rb, (void *)addr);
785     if (already_received) {
786         qatomic_xchg(&dc->vcpu_addr[cpu], 0);
787         qatomic_xchg(&dc->page_fault_vcpu_time[cpu], 0);
788         qatomic_dec(&dc->smp_cpus_down);
789     }
790     trace_mark_postcopy_blocktime_begin(addr, dc, dc->page_fault_vcpu_time[cpu],
791                                         cpu, already_received);
792 }
793 
794 /*
795  *  This function just provide calculated blocktime per cpu and trace it.
796  *  Total blocktime is calculated in mark_postcopy_blocktime_end.
797  *
798  *
799  * Assume we have 3 CPU
800  *
801  *      S1        E1           S1               E1
802  * -----***********------------xxx***************------------------------> CPU1
803  *
804  *             S2                E2
805  * ------------****************xxx---------------------------------------> CPU2
806  *
807  *                         S3            E3
808  * ------------------------****xxx********-------------------------------> CPU3
809  *
810  * We have sequence S1,S2,E1,S3,S1,E2,E3,E1
811  * S2,E1 - doesn't match condition due to sequence S1,S2,E1 doesn't include CPU3
812  * S3,S1,E2 - sequence includes all CPUs, in this case overlap will be S1,E2 -
813  *            it's a part of total blocktime.
814  * S1 - here is last_begin
815  * Legend of the picture is following:
816  *              * - means blocktime per vCPU
817  *              x - means overlapped blocktime (total blocktime)
818  *
819  * @addr: host virtual address
820  */
821 static void mark_postcopy_blocktime_end(uintptr_t addr)
822 {
823     MigrationIncomingState *mis = migration_incoming_get_current();
824     PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
825     MachineState *ms = MACHINE(qdev_get_machine());
826     unsigned int smp_cpus = ms->smp.cpus;
827     int i, affected_cpu = 0;
828     bool vcpu_total_blocktime = false;
829     uint32_t read_vcpu_time, low_time_offset;
830 
831     if (!dc) {
832         return;
833     }
834 
835     low_time_offset = get_low_time_offset(dc);
836     /* lookup cpu, to clear it,
837      * that algorithm looks straightforward, but it's not
838      * optimal, more optimal algorithm is keeping tree or hash
839      * where key is address value is a list of  */
840     for (i = 0; i < smp_cpus; i++) {
841         uint32_t vcpu_blocktime = 0;
842 
843         read_vcpu_time = qatomic_fetch_add(&dc->page_fault_vcpu_time[i], 0);
844         if (qatomic_fetch_add(&dc->vcpu_addr[i], 0) != addr ||
845             read_vcpu_time == 0) {
846             continue;
847         }
848         qatomic_xchg(&dc->vcpu_addr[i], 0);
849         vcpu_blocktime = low_time_offset - read_vcpu_time;
850         affected_cpu += 1;
851         /* we need to know is that mark_postcopy_end was due to
852          * faulted page, another possible case it's prefetched
853          * page and in that case we shouldn't be here */
854         if (!vcpu_total_blocktime &&
855             qatomic_fetch_add(&dc->smp_cpus_down, 0) == smp_cpus) {
856             vcpu_total_blocktime = true;
857         }
858         /* continue cycle, due to one page could affect several vCPUs */
859         dc->vcpu_blocktime[i] += vcpu_blocktime;
860     }
861 
862     qatomic_sub(&dc->smp_cpus_down, affected_cpu);
863     if (vcpu_total_blocktime) {
864         dc->total_blocktime += low_time_offset - qatomic_fetch_add(
865                 &dc->last_begin, 0);
866     }
867     trace_mark_postcopy_blocktime_end(addr, dc, dc->total_blocktime,
868                                       affected_cpu);
869 }
870 
871 static bool postcopy_pause_fault_thread(MigrationIncomingState *mis)
872 {
873     trace_postcopy_pause_fault_thread();
874 
875     qemu_sem_wait(&mis->postcopy_pause_sem_fault);
876 
877     trace_postcopy_pause_fault_thread_continued();
878 
879     return true;
880 }
881 
882 /*
883  * Handle faults detected by the USERFAULT markings
884  */
885 static void *postcopy_ram_fault_thread(void *opaque)
886 {
887     MigrationIncomingState *mis = opaque;
888     struct uffd_msg msg;
889     int ret;
890     size_t index;
891     RAMBlock *rb = NULL;
892 
893     trace_postcopy_ram_fault_thread_entry();
894     rcu_register_thread();
895     mis->last_rb = NULL; /* last RAMBlock we sent part of */
896     qemu_sem_post(&mis->fault_thread_sem);
897 
898     struct pollfd *pfd;
899     size_t pfd_len = 2 + mis->postcopy_remote_fds->len;
900 
901     pfd = g_new0(struct pollfd, pfd_len);
902 
903     pfd[0].fd = mis->userfault_fd;
904     pfd[0].events = POLLIN;
905     pfd[1].fd = mis->userfault_event_fd;
906     pfd[1].events = POLLIN; /* Waiting for eventfd to go positive */
907     trace_postcopy_ram_fault_thread_fds_core(pfd[0].fd, pfd[1].fd);
908     for (index = 0; index < mis->postcopy_remote_fds->len; index++) {
909         struct PostCopyFD *pcfd = &g_array_index(mis->postcopy_remote_fds,
910                                                  struct PostCopyFD, index);
911         pfd[2 + index].fd = pcfd->fd;
912         pfd[2 + index].events = POLLIN;
913         trace_postcopy_ram_fault_thread_fds_extra(2 + index, pcfd->idstr,
914                                                   pcfd->fd);
915     }
916 
917     while (true) {
918         ram_addr_t rb_offset;
919         int poll_result;
920 
921         /*
922          * We're mainly waiting for the kernel to give us a faulting HVA,
923          * however we can be told to quit via userfault_quit_fd which is
924          * an eventfd
925          */
926 
927         poll_result = poll(pfd, pfd_len, -1 /* Wait forever */);
928         if (poll_result == -1) {
929             error_report("%s: userfault poll: %s", __func__, strerror(errno));
930             break;
931         }
932 
933         if (!mis->to_src_file) {
934             /*
935              * Possibly someone tells us that the return path is
936              * broken already using the event. We should hold until
937              * the channel is rebuilt.
938              */
939             if (postcopy_pause_fault_thread(mis)) {
940                 /* Continue to read the userfaultfd */
941             } else {
942                 error_report("%s: paused but don't allow to continue",
943                              __func__);
944                 break;
945             }
946         }
947 
948         if (pfd[1].revents) {
949             uint64_t tmp64 = 0;
950 
951             /* Consume the signal */
952             if (read(mis->userfault_event_fd, &tmp64, 8) != 8) {
953                 /* Nothing obviously nicer than posting this error. */
954                 error_report("%s: read() failed", __func__);
955             }
956 
957             if (qatomic_read(&mis->fault_thread_quit)) {
958                 trace_postcopy_ram_fault_thread_quit();
959                 break;
960             }
961         }
962 
963         if (pfd[0].revents) {
964             poll_result--;
965             ret = read(mis->userfault_fd, &msg, sizeof(msg));
966             if (ret != sizeof(msg)) {
967                 if (errno == EAGAIN) {
968                     /*
969                      * if a wake up happens on the other thread just after
970                      * the poll, there is nothing to read.
971                      */
972                     continue;
973                 }
974                 if (ret < 0) {
975                     error_report("%s: Failed to read full userfault "
976                                  "message: %s",
977                                  __func__, strerror(errno));
978                     break;
979                 } else {
980                     error_report("%s: Read %d bytes from userfaultfd "
981                                  "expected %zd",
982                                  __func__, ret, sizeof(msg));
983                     break; /* Lost alignment, don't know what we'd read next */
984                 }
985             }
986             if (msg.event != UFFD_EVENT_PAGEFAULT) {
987                 error_report("%s: Read unexpected event %ud from userfaultfd",
988                              __func__, msg.event);
989                 continue; /* It's not a page fault, shouldn't happen */
990             }
991 
992             rb = qemu_ram_block_from_host(
993                      (void *)(uintptr_t)msg.arg.pagefault.address,
994                      true, &rb_offset);
995             if (!rb) {
996                 error_report("postcopy_ram_fault_thread: Fault outside guest: %"
997                              PRIx64, (uint64_t)msg.arg.pagefault.address);
998                 break;
999             }
1000 
1001             rb_offset = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
1002             trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
1003                                                 qemu_ram_get_idstr(rb),
1004                                                 rb_offset,
1005                                                 msg.arg.pagefault.feat.ptid);
1006             mark_postcopy_blocktime_begin(
1007                     (uintptr_t)(msg.arg.pagefault.address),
1008                                 msg.arg.pagefault.feat.ptid, rb);
1009 
1010 retry:
1011             /*
1012              * Send the request to the source - we want to request one
1013              * of our host page sizes (which is >= TPS)
1014              */
1015             ret = postcopy_request_page(mis, rb, rb_offset,
1016                                         msg.arg.pagefault.address);
1017             if (ret) {
1018                 /* May be network failure, try to wait for recovery */
1019                 if (ret == -EIO && postcopy_pause_fault_thread(mis)) {
1020                     /* We got reconnected somehow, try to continue */
1021                     goto retry;
1022                 } else {
1023                     /* This is a unavoidable fault */
1024                     error_report("%s: postcopy_request_page() get %d",
1025                                  __func__, ret);
1026                     break;
1027                 }
1028             }
1029         }
1030 
1031         /* Now handle any requests from external processes on shared memory */
1032         /* TODO: May need to handle devices deregistering during postcopy */
1033         for (index = 2; index < pfd_len && poll_result; index++) {
1034             if (pfd[index].revents) {
1035                 struct PostCopyFD *pcfd =
1036                     &g_array_index(mis->postcopy_remote_fds,
1037                                    struct PostCopyFD, index - 2);
1038 
1039                 poll_result--;
1040                 if (pfd[index].revents & POLLERR) {
1041                     error_report("%s: POLLERR on poll %zd fd=%d",
1042                                  __func__, index, pcfd->fd);
1043                     pfd[index].events = 0;
1044                     continue;
1045                 }
1046 
1047                 ret = read(pcfd->fd, &msg, sizeof(msg));
1048                 if (ret != sizeof(msg)) {
1049                     if (errno == EAGAIN) {
1050                         /*
1051                          * if a wake up happens on the other thread just after
1052                          * the poll, there is nothing to read.
1053                          */
1054                         continue;
1055                     }
1056                     if (ret < 0) {
1057                         error_report("%s: Failed to read full userfault "
1058                                      "message: %s (shared) revents=%d",
1059                                      __func__, strerror(errno),
1060                                      pfd[index].revents);
1061                         /*TODO: Could just disable this sharer */
1062                         break;
1063                     } else {
1064                         error_report("%s: Read %d bytes from userfaultfd "
1065                                      "expected %zd (shared)",
1066                                      __func__, ret, sizeof(msg));
1067                         /*TODO: Could just disable this sharer */
1068                         break; /*Lost alignment,don't know what we'd read next*/
1069                     }
1070                 }
1071                 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1072                     error_report("%s: Read unexpected event %ud "
1073                                  "from userfaultfd (shared)",
1074                                  __func__, msg.event);
1075                     continue; /* It's not a page fault, shouldn't happen */
1076                 }
1077                 /* Call the device handler registered with us */
1078                 ret = pcfd->handler(pcfd, &msg);
1079                 if (ret) {
1080                     error_report("%s: Failed to resolve shared fault on %zd/%s",
1081                                  __func__, index, pcfd->idstr);
1082                     /* TODO: Fail? Disable this sharer? */
1083                 }
1084             }
1085         }
1086     }
1087     rcu_unregister_thread();
1088     trace_postcopy_ram_fault_thread_exit();
1089     g_free(pfd);
1090     return NULL;
1091 }
1092 
1093 static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
1094 {
1095     int err;
1096 
1097     mis->postcopy_tmp_page = mmap(NULL, mis->largest_page_size,
1098                                   PROT_READ | PROT_WRITE,
1099                                   MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1100     if (mis->postcopy_tmp_page == MAP_FAILED) {
1101         err = errno;
1102         mis->postcopy_tmp_page = NULL;
1103         error_report("%s: Failed to map postcopy_tmp_page %s",
1104                      __func__, strerror(err));
1105         return -err;
1106     }
1107 
1108     /*
1109      * Map large zero page when kernel can't use UFFDIO_ZEROPAGE for hugepages
1110      */
1111     mis->postcopy_tmp_zero_page = mmap(NULL, mis->largest_page_size,
1112                                        PROT_READ | PROT_WRITE,
1113                                        MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1114     if (mis->postcopy_tmp_zero_page == MAP_FAILED) {
1115         err = errno;
1116         mis->postcopy_tmp_zero_page = NULL;
1117         error_report("%s: Failed to map large zero page %s",
1118                      __func__, strerror(err));
1119         return -err;
1120     }
1121 
1122     memset(mis->postcopy_tmp_zero_page, '\0', mis->largest_page_size);
1123 
1124     return 0;
1125 }
1126 
1127 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1128 {
1129     /* Open the fd for the kernel to give us userfaults */
1130     mis->userfault_fd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
1131     if (mis->userfault_fd == -1) {
1132         error_report("%s: Failed to open userfault fd: %s", __func__,
1133                      strerror(errno));
1134         return -1;
1135     }
1136 
1137     /*
1138      * Although the host check already tested the API, we need to
1139      * do the check again as an ABI handshake on the new fd.
1140      */
1141     if (!ufd_check_and_apply(mis->userfault_fd, mis)) {
1142         return -1;
1143     }
1144 
1145     /* Now an eventfd we use to tell the fault-thread to quit */
1146     mis->userfault_event_fd = eventfd(0, EFD_CLOEXEC);
1147     if (mis->userfault_event_fd == -1) {
1148         error_report("%s: Opening userfault_event_fd: %s", __func__,
1149                      strerror(errno));
1150         close(mis->userfault_fd);
1151         return -1;
1152     }
1153 
1154     qemu_sem_init(&mis->fault_thread_sem, 0);
1155     qemu_thread_create(&mis->fault_thread, "postcopy/fault",
1156                        postcopy_ram_fault_thread, mis, QEMU_THREAD_JOINABLE);
1157     qemu_sem_wait(&mis->fault_thread_sem);
1158     qemu_sem_destroy(&mis->fault_thread_sem);
1159     mis->have_fault_thread = true;
1160 
1161     /* Mark so that we get notified of accesses to unwritten areas */
1162     if (foreach_not_ignored_block(ram_block_enable_notify, mis)) {
1163         error_report("ram_block_enable_notify failed");
1164         return -1;
1165     }
1166 
1167     if (postcopy_temp_pages_setup(mis)) {
1168         /* Error dumped in the sub-function */
1169         return -1;
1170     }
1171 
1172     trace_postcopy_ram_enable_notify();
1173 
1174     return 0;
1175 }
1176 
1177 static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
1178                                void *from_addr, uint64_t pagesize, RAMBlock *rb)
1179 {
1180     int userfault_fd = mis->userfault_fd;
1181     int ret;
1182 
1183     if (from_addr) {
1184         struct uffdio_copy copy_struct;
1185         copy_struct.dst = (uint64_t)(uintptr_t)host_addr;
1186         copy_struct.src = (uint64_t)(uintptr_t)from_addr;
1187         copy_struct.len = pagesize;
1188         copy_struct.mode = 0;
1189         ret = ioctl(userfault_fd, UFFDIO_COPY, &copy_struct);
1190     } else {
1191         struct uffdio_zeropage zero_struct;
1192         zero_struct.range.start = (uint64_t)(uintptr_t)host_addr;
1193         zero_struct.range.len = pagesize;
1194         zero_struct.mode = 0;
1195         ret = ioctl(userfault_fd, UFFDIO_ZEROPAGE, &zero_struct);
1196     }
1197     if (!ret) {
1198         qemu_mutex_lock(&mis->page_request_mutex);
1199         ramblock_recv_bitmap_set_range(rb, host_addr,
1200                                        pagesize / qemu_target_page_size());
1201         /*
1202          * If this page resolves a page fault for a previous recorded faulted
1203          * address, take a special note to maintain the requested page list.
1204          */
1205         if (g_tree_lookup(mis->page_requested, host_addr)) {
1206             g_tree_remove(mis->page_requested, host_addr);
1207             mis->page_requested_count--;
1208             trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
1209         }
1210         qemu_mutex_unlock(&mis->page_request_mutex);
1211         mark_postcopy_blocktime_end((uintptr_t)host_addr);
1212     }
1213     return ret;
1214 }
1215 
1216 int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
1217 {
1218     int i;
1219     MigrationIncomingState *mis = migration_incoming_get_current();
1220     GArray *pcrfds = mis->postcopy_remote_fds;
1221 
1222     for (i = 0; i < pcrfds->len; i++) {
1223         struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1224         int ret = cur->waker(cur, rb, offset);
1225         if (ret) {
1226             return ret;
1227         }
1228     }
1229     return 0;
1230 }
1231 
1232 /*
1233  * Place a host page (from) at (host) atomically
1234  * returns 0 on success
1235  */
1236 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1237                         RAMBlock *rb)
1238 {
1239     size_t pagesize = qemu_ram_pagesize(rb);
1240 
1241     /* copy also acks to the kernel waking the stalled thread up
1242      * TODO: We can inhibit that ack and only do it if it was requested
1243      * which would be slightly cheaper, but we'd have to be careful
1244      * of the order of updating our page state.
1245      */
1246     if (qemu_ufd_copy_ioctl(mis, host, from, pagesize, rb)) {
1247         int e = errno;
1248         error_report("%s: %s copy host: %p from: %p (size: %zd)",
1249                      __func__, strerror(e), host, from, pagesize);
1250 
1251         return -e;
1252     }
1253 
1254     trace_postcopy_place_page(host);
1255     return postcopy_notify_shared_wake(rb,
1256                                        qemu_ram_block_host_offset(rb, host));
1257 }
1258 
1259 /*
1260  * Place a zero page at (host) atomically
1261  * returns 0 on success
1262  */
1263 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1264                              RAMBlock *rb)
1265 {
1266     size_t pagesize = qemu_ram_pagesize(rb);
1267     trace_postcopy_place_page_zero(host);
1268 
1269     /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
1270      * but it's not available for everything (e.g. hugetlbpages)
1271      */
1272     if (qemu_ram_is_uf_zeroable(rb)) {
1273         if (qemu_ufd_copy_ioctl(mis, host, NULL, pagesize, rb)) {
1274             int e = errno;
1275             error_report("%s: %s zero host: %p",
1276                          __func__, strerror(e), host);
1277 
1278             return -e;
1279         }
1280         return postcopy_notify_shared_wake(rb,
1281                                            qemu_ram_block_host_offset(rb,
1282                                                                       host));
1283     } else {
1284         return postcopy_place_page(mis, host, mis->postcopy_tmp_zero_page, rb);
1285     }
1286 }
1287 
1288 #else
1289 /* No target OS support, stubs just fail */
1290 void fill_destination_postcopy_migration_info(MigrationInfo *info)
1291 {
1292 }
1293 
1294 bool postcopy_ram_supported_by_host(MigrationIncomingState *mis)
1295 {
1296     error_report("%s: No OS support", __func__);
1297     return false;
1298 }
1299 
1300 int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1301 {
1302     error_report("postcopy_ram_incoming_init: No OS support");
1303     return -1;
1304 }
1305 
1306 int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
1307 {
1308     assert(0);
1309     return -1;
1310 }
1311 
1312 int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
1313 {
1314     assert(0);
1315     return -1;
1316 }
1317 
1318 int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
1319                                  uint64_t client_addr, uint64_t rb_offset)
1320 {
1321     assert(0);
1322     return -1;
1323 }
1324 
1325 int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
1326 {
1327     assert(0);
1328     return -1;
1329 }
1330 
1331 int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
1332                         RAMBlock *rb)
1333 {
1334     assert(0);
1335     return -1;
1336 }
1337 
1338 int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
1339                         RAMBlock *rb)
1340 {
1341     assert(0);
1342     return -1;
1343 }
1344 
1345 int postcopy_wake_shared(struct PostCopyFD *pcfd,
1346                          uint64_t client_addr,
1347                          RAMBlock *rb)
1348 {
1349     assert(0);
1350     return -1;
1351 }
1352 #endif
1353 
1354 /* ------------------------------------------------------------------------- */
1355 
1356 void postcopy_fault_thread_notify(MigrationIncomingState *mis)
1357 {
1358     uint64_t tmp64 = 1;
1359 
1360     /*
1361      * Wakeup the fault_thread.  It's an eventfd that should currently
1362      * be at 0, we're going to increment it to 1
1363      */
1364     if (write(mis->userfault_event_fd, &tmp64, 8) != 8) {
1365         /* Not much we can do here, but may as well report it */
1366         error_report("%s: incrementing failed: %s", __func__,
1367                      strerror(errno));
1368     }
1369 }
1370 
1371 /**
1372  * postcopy_discard_send_init: Called at the start of each RAMBlock before
1373  *   asking to discard individual ranges.
1374  *
1375  * @ms: The current migration state.
1376  * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
1377  * @name: RAMBlock that discards will operate on.
1378  */
1379 static PostcopyDiscardState pds = {0};
1380 void postcopy_discard_send_init(MigrationState *ms, const char *name)
1381 {
1382     pds.ramblock_name = name;
1383     pds.cur_entry = 0;
1384     pds.nsentwords = 0;
1385     pds.nsentcmds = 0;
1386 }
1387 
1388 /**
1389  * postcopy_discard_send_range: Called by the bitmap code for each chunk to
1390  *   discard. May send a discard message, may just leave it queued to
1391  *   be sent later.
1392  *
1393  * @ms: Current migration state.
1394  * @start,@length: a range of pages in the migration bitmap in the
1395  *   RAM block passed to postcopy_discard_send_init() (length=1 is one page)
1396  */
1397 void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
1398                                  unsigned long length)
1399 {
1400     size_t tp_size = qemu_target_page_size();
1401     /* Convert to byte offsets within the RAM block */
1402     pds.start_list[pds.cur_entry] = start  * tp_size;
1403     pds.length_list[pds.cur_entry] = length * tp_size;
1404     trace_postcopy_discard_send_range(pds.ramblock_name, start, length);
1405     pds.cur_entry++;
1406     pds.nsentwords++;
1407 
1408     if (pds.cur_entry == MAX_DISCARDS_PER_COMMAND) {
1409         /* Full set, ship it! */
1410         qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
1411                                               pds.ramblock_name,
1412                                               pds.cur_entry,
1413                                               pds.start_list,
1414                                               pds.length_list);
1415         pds.nsentcmds++;
1416         pds.cur_entry = 0;
1417     }
1418 }
1419 
1420 /**
1421  * postcopy_discard_send_finish: Called at the end of each RAMBlock by the
1422  * bitmap code. Sends any outstanding discard messages, frees the PDS
1423  *
1424  * @ms: Current migration state.
1425  */
1426 void postcopy_discard_send_finish(MigrationState *ms)
1427 {
1428     /* Anything unsent? */
1429     if (pds.cur_entry) {
1430         qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
1431                                               pds.ramblock_name,
1432                                               pds.cur_entry,
1433                                               pds.start_list,
1434                                               pds.length_list);
1435         pds.nsentcmds++;
1436     }
1437 
1438     trace_postcopy_discard_send_finish(pds.ramblock_name, pds.nsentwords,
1439                                        pds.nsentcmds);
1440 }
1441 
1442 /*
1443  * Current state of incoming postcopy; note this is not part of
1444  * MigrationIncomingState since it's state is used during cleanup
1445  * at the end as MIS is being freed.
1446  */
1447 static PostcopyState incoming_postcopy_state;
1448 
1449 PostcopyState  postcopy_state_get(void)
1450 {
1451     return qatomic_mb_read(&incoming_postcopy_state);
1452 }
1453 
1454 /* Set the state and return the old state */
1455 PostcopyState postcopy_state_set(PostcopyState new_state)
1456 {
1457     return qatomic_xchg(&incoming_postcopy_state, new_state);
1458 }
1459 
1460 /* Register a handler for external shared memory postcopy
1461  * called on the destination.
1462  */
1463 void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
1464 {
1465     MigrationIncomingState *mis = migration_incoming_get_current();
1466 
1467     mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
1468                                                   *pcfd);
1469 }
1470 
1471 /* Unregister a handler for external shared memory postcopy
1472  */
1473 void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
1474 {
1475     guint i;
1476     MigrationIncomingState *mis = migration_incoming_get_current();
1477     GArray *pcrfds = mis->postcopy_remote_fds;
1478 
1479     if (!pcrfds) {
1480         /* migration has already finished and freed the array */
1481         return;
1482     }
1483     for (i = 0; i < pcrfds->len; i++) {
1484         struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1485         if (cur->fd == pcfd->fd) {
1486             mis->postcopy_remote_fds = g_array_remove_index(pcrfds, i);
1487             return;
1488         }
1489     }
1490 }
1491