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