]> git.proxmox.com Git - mirror_qemu.git/blame - migration/postcopy-ram.c
misc: Clean up includes
[mirror_qemu.git] / migration / postcopy-ram.c
CommitLineData
eb59db53
DDAG
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
1393a485 19#include "qemu/osdep.h"
b85ea5fa 20#include "qemu/madvise.h"
51180423 21#include "exec/target_page.h"
6666c96a 22#include "migration.h"
08a0aee1 23#include "qemu-file.h"
20a519a0 24#include "savevm.h"
be07b0ac 25#include "postcopy-ram.h"
7b1e1a22 26#include "ram.h"
1693c64c
DDAG
27#include "qapi/error.h"
28#include "qemu/notify.h"
d4842052 29#include "qemu/rcu.h"
eb59db53
DDAG
30#include "sysemu/sysemu.h"
31#include "qemu/error-report.h"
32#include "trace.h"
5cc8767d 33#include "hw/boards.h"
898ba906 34#include "exec/ramblock.h"
36f62f11 35#include "socket.h"
36f62f11 36#include "yank_functions.h"
f0afaf6c 37#include "tls.h"
d5890ea0 38#include "qemu/userfaultfd.h"
ae30b9b2 39#include "qemu/mmap-alloc.h"
1f0776f1 40#include "options.h"
eb59db53 41
e0b266f0
DDAG
42/* Arbitrary limit on size of each discard command,
43 * keeps them around ~200 bytes
44 */
45#define MAX_DISCARDS_PER_COMMAND 12
46
47struct PostcopyDiscardState {
48 const char *ramblock_name;
e0b266f0
DDAG
49 uint16_t cur_entry;
50 /*
51 * Start and length of a discard range (bytes)
52 */
53 uint64_t start_list[MAX_DISCARDS_PER_COMMAND];
54 uint64_t length_list[MAX_DISCARDS_PER_COMMAND];
55 unsigned int nsentwords;
56 unsigned int nsentcmds;
57};
58
1693c64c
DDAG
59static NotifierWithReturnList postcopy_notifier_list;
60
61void postcopy_infrastructure_init(void)
62{
63 notifier_with_return_list_init(&postcopy_notifier_list);
64}
65
66void postcopy_add_notifier(NotifierWithReturn *nn)
67{
68 notifier_with_return_list_add(&postcopy_notifier_list, nn);
69}
70
71void postcopy_remove_notifier(NotifierWithReturn *n)
72{
73 notifier_with_return_remove(n);
74}
75
76int postcopy_notify(enum PostcopyNotifyReason reason, Error **errp)
77{
78 struct PostcopyNotifyData pnd;
79 pnd.reason = reason;
80 pnd.errp = errp;
81
82 return notifier_with_return_list_notify(&postcopy_notifier_list,
83 &pnd);
84}
85
095c12a4
PX
86/*
87 * NOTE: this routine is not thread safe, we can't call it concurrently. But it
88 * should be good enough for migration's purposes.
89 */
90void postcopy_thread_create(MigrationIncomingState *mis,
91 QemuThread *thread, const char *name,
92 void *(*fn)(void *), int joinable)
93{
94 qemu_sem_init(&mis->thread_sync_sem, 0);
95 qemu_thread_create(thread, name, fn, mis, joinable);
96 qemu_sem_wait(&mis->thread_sync_sem);
97 qemu_sem_destroy(&mis->thread_sync_sem);
98}
99
eb59db53
DDAG
100/* Postcopy needs to detect accesses to pages that haven't yet been copied
101 * across, and efficiently map new pages in, the techniques for doing this
102 * are target OS specific.
103 */
104#if defined(__linux__)
c4faeed2 105#include <poll.h>
eb59db53
DDAG
106#include <sys/ioctl.h>
107#include <sys/syscall.h>
eb59db53
DDAG
108#endif
109
d8b9d771
MF
110#if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
111#include <sys/eventfd.h>
eb59db53
DDAG
112#include <linux/userfaultfd.h>
113
2a4c42f1
AP
114typedef struct PostcopyBlocktimeContext {
115 /* time when page fault initiated per vCPU */
116 uint32_t *page_fault_vcpu_time;
117 /* page address per vCPU */
118 uintptr_t *vcpu_addr;
119 uint32_t total_blocktime;
120 /* blocktime per vCPU */
121 uint32_t *vcpu_blocktime;
122 /* point in time when last page fault was initiated */
123 uint32_t last_begin;
124 /* number of vCPU are suspended */
125 int smp_cpus_down;
126 uint64_t start_time;
127
128 /*
129 * Handler for exit event, necessary for
130 * releasing whole blocktime_ctx
131 */
132 Notifier exit_notifier;
133} PostcopyBlocktimeContext;
134
135static void destroy_blocktime_context(struct PostcopyBlocktimeContext *ctx)
136{
137 g_free(ctx->page_fault_vcpu_time);
138 g_free(ctx->vcpu_addr);
139 g_free(ctx->vcpu_blocktime);
140 g_free(ctx);
141}
142
143static void migration_exit_cb(Notifier *n, void *data)
144{
145 PostcopyBlocktimeContext *ctx = container_of(n, PostcopyBlocktimeContext,
146 exit_notifier);
147 destroy_blocktime_context(ctx);
148}
149
150static struct PostcopyBlocktimeContext *blocktime_context_new(void)
151{
5cc8767d
LX
152 MachineState *ms = MACHINE(qdev_get_machine());
153 unsigned int smp_cpus = ms->smp.cpus;
2a4c42f1
AP
154 PostcopyBlocktimeContext *ctx = g_new0(PostcopyBlocktimeContext, 1);
155 ctx->page_fault_vcpu_time = g_new0(uint32_t, smp_cpus);
156 ctx->vcpu_addr = g_new0(uintptr_t, smp_cpus);
157 ctx->vcpu_blocktime = g_new0(uint32_t, smp_cpus);
158
159 ctx->exit_notifier.notify = migration_exit_cb;
160 ctx->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
161 qemu_add_exit_notifier(&ctx->exit_notifier);
162 return ctx;
163}
ca6011c2 164
65ace060
AP
165static uint32List *get_vcpu_blocktime_list(PostcopyBlocktimeContext *ctx)
166{
5cc8767d 167 MachineState *ms = MACHINE(qdev_get_machine());
54aa3de7 168 uint32List *list = NULL;
65ace060
AP
169 int i;
170
5cc8767d 171 for (i = ms->smp.cpus - 1; i >= 0; i--) {
54aa3de7 172 QAPI_LIST_PREPEND(list, ctx->vcpu_blocktime[i]);
65ace060
AP
173 }
174
175 return list;
176}
177
178/*
179 * This function just populates MigrationInfo from postcopy's
180 * blocktime context. It will not populate MigrationInfo,
181 * unless postcopy-blocktime capability was set.
182 *
183 * @info: pointer to MigrationInfo to populate
184 */
185void fill_destination_postcopy_migration_info(MigrationInfo *info)
186{
187 MigrationIncomingState *mis = migration_incoming_get_current();
188 PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
189
190 if (!bc) {
191 return;
192 }
193
194 info->has_postcopy_blocktime = true;
195 info->postcopy_blocktime = bc->total_blocktime;
196 info->has_postcopy_vcpu_blocktime = true;
197 info->postcopy_vcpu_blocktime = get_vcpu_blocktime_list(bc);
198}
199
200static uint32_t get_postcopy_total_blocktime(void)
201{
202 MigrationIncomingState *mis = migration_incoming_get_current();
203 PostcopyBlocktimeContext *bc = mis->blocktime_ctx;
204
205 if (!bc) {
206 return 0;
207 }
208
209 return bc->total_blocktime;
210}
211
54ae0886
AP
212/**
213 * receive_ufd_features: check userfault fd features, to request only supported
214 * features in the future.
215 *
216 * Returns: true on success
217 *
218 * __NR_userfaultfd - should be checked before
219 * @features: out parameter will contain uffdio_api.features provided by kernel
220 * in case of success
221 */
222static bool receive_ufd_features(uint64_t *features)
eb59db53 223{
54ae0886
AP
224 struct uffdio_api api_struct = {0};
225 int ufd;
226 bool ret = true;
227
d5890ea0 228 ufd = uffd_open(O_CLOEXEC);
54ae0886 229 if (ufd == -1) {
d5890ea0 230 error_report("%s: uffd_open() failed: %s", __func__, strerror(errno));
54ae0886
AP
231 return false;
232 }
eb59db53 233
54ae0886 234 /* ask features */
eb59db53
DDAG
235 api_struct.api = UFFD_API;
236 api_struct.features = 0;
237 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
5553499f 238 error_report("%s: UFFDIO_API failed: %s", __func__,
eb59db53 239 strerror(errno));
54ae0886
AP
240 ret = false;
241 goto release_ufd;
242 }
243
244 *features = api_struct.features;
245
246release_ufd:
247 close(ufd);
248 return ret;
249}
250
251/**
252 * request_ufd_features: this function should be called only once on a newly
253 * opened ufd, subsequent calls will lead to error.
254 *
3a4452d8 255 * Returns: true on success
54ae0886
AP
256 *
257 * @ufd: fd obtained from userfaultfd syscall
258 * @features: bit mask see UFFD_API_FEATURES
259 */
260static bool request_ufd_features(int ufd, uint64_t features)
261{
262 struct uffdio_api api_struct = {0};
263 uint64_t ioctl_mask;
264
265 api_struct.api = UFFD_API;
266 api_struct.features = features;
267 if (ioctl(ufd, UFFDIO_API, &api_struct)) {
268 error_report("%s failed: UFFDIO_API failed: %s", __func__,
269 strerror(errno));
eb59db53
DDAG
270 return false;
271 }
272
73b49878
PB
273 ioctl_mask = 1ULL << _UFFDIO_REGISTER |
274 1ULL << _UFFDIO_UNREGISTER;
eb59db53
DDAG
275 if ((api_struct.ioctls & ioctl_mask) != ioctl_mask) {
276 error_report("Missing userfault features: %" PRIx64,
277 (uint64_t)(~api_struct.ioctls & ioctl_mask));
278 return false;
279 }
280
54ae0886
AP
281 return true;
282}
283
74c38cf7
PX
284static bool ufd_check_and_apply(int ufd, MigrationIncomingState *mis,
285 Error **errp)
54ae0886
AP
286{
287 uint64_t asked_features = 0;
288 static uint64_t supported_features;
289
74c38cf7 290 ERRP_GUARD();
54ae0886
AP
291 /*
292 * it's not possible to
293 * request UFFD_API twice per one fd
294 * userfault fd features is persistent
295 */
296 if (!supported_features) {
297 if (!receive_ufd_features(&supported_features)) {
74c38cf7 298 error_setg(errp, "Userfault feature detection failed");
54ae0886
AP
299 return false;
300 }
301 }
302
2a4c42f1 303#ifdef UFFD_FEATURE_THREAD_ID
2d1c37c6 304 if (UFFD_FEATURE_THREAD_ID & supported_features) {
2a4c42f1 305 asked_features |= UFFD_FEATURE_THREAD_ID;
2d1c37c6
PX
306 if (migrate_postcopy_blocktime()) {
307 if (!mis->blocktime_ctx) {
308 mis->blocktime_ctx = blocktime_context_new();
309 }
310 }
2a4c42f1
AP
311 }
312#endif
313
54ae0886
AP
314 /*
315 * request features, even if asked_features is 0, due to
316 * kernel expects UFFD_API before UFFDIO_REGISTER, per
317 * userfault file descriptor
318 */
319 if (!request_ufd_features(ufd, asked_features)) {
74c38cf7 320 error_setg(errp, "Failed features %" PRIu64, asked_features);
54ae0886
AP
321 return false;
322 }
323
8e3b0cbb 324 if (qemu_real_host_page_size() != ram_pagesize_summary()) {
7e8cafb7
DDAG
325 bool have_hp = false;
326 /* We've got a huge page */
327#ifdef UFFD_FEATURE_MISSING_HUGETLBFS
54ae0886 328 have_hp = supported_features & UFFD_FEATURE_MISSING_HUGETLBFS;
7e8cafb7
DDAG
329#endif
330 if (!have_hp) {
74c38cf7
PX
331 error_setg(errp,
332 "Userfault on this host does not support huge pages");
7e8cafb7
DDAG
333 return false;
334 }
335 }
eb59db53
DDAG
336 return true;
337}
338
8679638b
DDAG
339/* Callback from postcopy_ram_supported_by_host block iterator.
340 */
74c38cf7 341static int test_ramblock_postcopiable(RAMBlock *rb, Error **errp)
8679638b 342{
754cb9c0
YK
343 const char *block_name = qemu_ram_get_idstr(rb);
344 ram_addr_t length = qemu_ram_get_used_length(rb);
5d214a92 345 size_t pagesize = qemu_ram_pagesize(rb);
ae30b9b2 346 QemuFsType fs;
5d214a92 347
5d214a92 348 if (length % pagesize) {
74c38cf7
PX
349 error_setg(errp,
350 "Postcopy requires RAM blocks to be a page size multiple,"
351 " block %s is 0x" RAM_ADDR_FMT " bytes with a "
352 "page size of 0x%zx", block_name, length, pagesize);
5d214a92
DDAG
353 return 1;
354 }
ae30b9b2
PX
355
356 if (rb->fd >= 0) {
357 fs = qemu_fd_getfs(rb->fd);
358 if (fs != QEMU_FS_TYPE_TMPFS && fs != QEMU_FS_TYPE_HUGETLBFS) {
74c38cf7
PX
359 error_setg(errp,
360 "Host backend files need to be TMPFS or HUGETLBFS only");
ae30b9b2
PX
361 return 1;
362 }
363 }
364
8679638b
DDAG
365 return 0;
366}
367
58b7c17e
DDAG
368/*
369 * Note: This has the side effect of munlock'ing all of RAM, that's
370 * normally fine since if the postcopy succeeds it gets turned back on at the
371 * end.
372 */
74c38cf7 373bool postcopy_ram_supported_by_host(MigrationIncomingState *mis, Error **errp)
eb59db53 374{
8e3b0cbb 375 long pagesize = qemu_real_host_page_size();
eb59db53
DDAG
376 int ufd = -1;
377 bool ret = false; /* Error unless we change it */
378 void *testarea = NULL;
379 struct uffdio_register reg_struct;
380 struct uffdio_range range_struct;
381 uint64_t feature_mask;
ae30b9b2 382 RAMBlock *block;
eb59db53 383
74c38cf7 384 ERRP_GUARD();
20afaed9 385 if (qemu_target_page_size() > pagesize) {
74c38cf7 386 error_setg(errp, "Target page size bigger than host page size");
eb59db53
DDAG
387 goto out;
388 }
389
d5890ea0 390 ufd = uffd_open(O_CLOEXEC);
eb59db53 391 if (ufd == -1) {
74c38cf7 392 error_setg(errp, "Userfaultfd not available: %s", strerror(errno));
eb59db53
DDAG
393 goto out;
394 }
395
1693c64c 396 /* Give devices a chance to object */
74c38cf7 397 if (postcopy_notify(POSTCOPY_NOTIFY_PROBE, errp)) {
1693c64c
DDAG
398 goto out;
399 }
400
eb59db53 401 /* Version and features check */
74c38cf7 402 if (!ufd_check_and_apply(ufd, mis, errp)) {
eb59db53
DDAG
403 goto out;
404 }
405
ae30b9b2
PX
406 /*
407 * We don't support postcopy with some type of ramblocks.
408 *
f161c88a 409 * NOTE: we explicitly ignored migrate_ram_is_ignored() instead we checked
ae30b9b2
PX
410 * all possible ramblocks. This is because this function can be called
411 * when creating the migration object, during the phase RAM_MIGRATABLE
412 * is not even properly set for all the ramblocks.
413 *
414 * A side effect of this is we'll also check against RAM_SHARED
415 * ramblocks even if migrate_ignore_shared() is set (in which case
416 * we'll never migrate RAM_SHARED at all), but normally this shouldn't
417 * affect in reality, or we can revisit.
418 */
419 RAMBLOCK_FOREACH(block) {
74c38cf7 420 if (test_ramblock_postcopiable(block, errp)) {
ae30b9b2
PX
421 goto out;
422 }
8679638b
DDAG
423 }
424
58b7c17e
DDAG
425 /*
426 * userfault and mlock don't go together; we'll put it back later if
427 * it was enabled.
428 */
429 if (munlockall()) {
74c38cf7 430 error_setg(errp, "munlockall() failed: %s", strerror(errno));
617a32f5 431 goto out;
58b7c17e
DDAG
432 }
433
eb59db53
DDAG
434 /*
435 * We need to check that the ops we need are supported on anon memory
436 * To do that we need to register a chunk and see the flags that
437 * are returned.
438 */
439 testarea = mmap(NULL, pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE |
440 MAP_ANONYMOUS, -1, 0);
441 if (testarea == MAP_FAILED) {
74c38cf7 442 error_setg(errp, "Failed to map test area: %s", strerror(errno));
eb59db53
DDAG
443 goto out;
444 }
7648297d 445 g_assert(QEMU_PTR_IS_ALIGNED(testarea, pagesize));
eb59db53
DDAG
446
447 reg_struct.range.start = (uintptr_t)testarea;
448 reg_struct.range.len = pagesize;
449 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
450
451 if (ioctl(ufd, UFFDIO_REGISTER, &reg_struct)) {
74c38cf7 452 error_setg(errp, "UFFDIO_REGISTER failed: %s", strerror(errno));
eb59db53
DDAG
453 goto out;
454 }
455
456 range_struct.start = (uintptr_t)testarea;
457 range_struct.len = pagesize;
458 if (ioctl(ufd, UFFDIO_UNREGISTER, &range_struct)) {
74c38cf7 459 error_setg(errp, "UFFDIO_UNREGISTER failed: %s", strerror(errno));
eb59db53
DDAG
460 goto out;
461 }
462
73b49878
PB
463 feature_mask = 1ULL << _UFFDIO_WAKE |
464 1ULL << _UFFDIO_COPY |
465 1ULL << _UFFDIO_ZEROPAGE;
eb59db53 466 if ((reg_struct.ioctls & feature_mask) != feature_mask) {
74c38cf7
PX
467 error_setg(errp, "Missing userfault map features: %" PRIx64,
468 (uint64_t)(~reg_struct.ioctls & feature_mask));
eb59db53
DDAG
469 goto out;
470 }
471
472 /* Success! */
473 ret = true;
474out:
475 if (testarea) {
476 munmap(testarea, pagesize);
477 }
478 if (ufd != -1) {
479 close(ufd);
480 }
481 return ret;
482}
483
1caddf8a
DDAG
484/*
485 * Setup an area of RAM so that it *can* be used for postcopy later; this
486 * must be done right at the start prior to pre-copy.
487 * opaque should be the MIS.
488 */
754cb9c0 489static int init_range(RAMBlock *rb, void *opaque)
1caddf8a 490{
754cb9c0
YK
491 const char *block_name = qemu_ram_get_idstr(rb);
492 void *host_addr = qemu_ram_get_host_addr(rb);
493 ram_addr_t offset = qemu_ram_get_offset(rb);
494 ram_addr_t length = qemu_ram_get_used_length(rb);
1caddf8a
DDAG
495 trace_postcopy_init_range(block_name, host_addr, offset, length);
496
898ba906
DH
497 /*
498 * Save the used_length before running the guest. In case we have to
499 * resize RAM blocks when syncing RAM block sizes from the source during
500 * precopy, we'll update it manually via the ram block notifier.
501 */
502 rb->postcopy_length = length;
503
1caddf8a
DDAG
504 /*
505 * We need the whole of RAM to be truly empty for postcopy, so things
506 * like ROMs and any data tables built during init must be zero'd
507 * - we're going to get the copy from the source anyway.
508 * (Precopy will just overwrite this data, so doesn't need the discard)
509 */
aaa2064c 510 if (ram_discard_range(block_name, 0, length)) {
1caddf8a
DDAG
511 return -1;
512 }
513
514 return 0;
515}
516
517/*
518 * At the end of migration, undo the effects of init_range
519 * opaque should be the MIS.
520 */
754cb9c0 521static int cleanup_range(RAMBlock *rb, void *opaque)
1caddf8a 522{
754cb9c0
YK
523 const char *block_name = qemu_ram_get_idstr(rb);
524 void *host_addr = qemu_ram_get_host_addr(rb);
525 ram_addr_t offset = qemu_ram_get_offset(rb);
898ba906 526 ram_addr_t length = rb->postcopy_length;
1caddf8a
DDAG
527 MigrationIncomingState *mis = opaque;
528 struct uffdio_range range_struct;
529 trace_postcopy_cleanup_range(block_name, host_addr, offset, length);
530
531 /*
532 * We turned off hugepage for the precopy stage with postcopy enabled
533 * we can turn it back on now.
534 */
1d741439 535 qemu_madvise(host_addr, length, QEMU_MADV_HUGEPAGE);
1caddf8a
DDAG
536
537 /*
538 * We can also turn off userfault now since we should have all the
539 * pages. It can be useful to leave it on to debug postcopy
540 * if you're not sure it's always getting every page.
541 */
542 range_struct.start = (uintptr_t)host_addr;
543 range_struct.len = length;
544
545 if (ioctl(mis->userfault_fd, UFFDIO_UNREGISTER, &range_struct)) {
546 error_report("%s: userfault unregister %s", __func__, strerror(errno));
547
548 return -1;
549 }
550
551 return 0;
552}
553
554/*
555 * Initialise postcopy-ram, setting the RAM to a state where we can go into
556 * postcopy later; must be called prior to any precopy.
557 * called from arch_init's similarly named ram_postcopy_incoming_init
558 */
c136180c 559int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1caddf8a 560{
fbd162e6 561 if (foreach_not_ignored_block(init_range, NULL)) {
1caddf8a
DDAG
562 return -1;
563 }
564
565 return 0;
566}
567
476ebf77
PX
568static void postcopy_temp_pages_cleanup(MigrationIncomingState *mis)
569{
77dadc3f
PX
570 int i;
571
572 if (mis->postcopy_tmp_pages) {
573 for (i = 0; i < mis->postcopy_channels; i++) {
574 if (mis->postcopy_tmp_pages[i].tmp_huge_page) {
575 munmap(mis->postcopy_tmp_pages[i].tmp_huge_page,
576 mis->largest_page_size);
577 mis->postcopy_tmp_pages[i].tmp_huge_page = NULL;
578 }
579 }
580 g_free(mis->postcopy_tmp_pages);
581 mis->postcopy_tmp_pages = NULL;
476ebf77
PX
582 }
583
584 if (mis->postcopy_tmp_zero_page) {
585 munmap(mis->postcopy_tmp_zero_page, mis->largest_page_size);
586 mis->postcopy_tmp_zero_page = NULL;
587 }
588}
589
1caddf8a
DDAG
590/*
591 * At the end of a migration where postcopy_ram_incoming_init was called.
592 */
593int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
594{
c4faeed2
DDAG
595 trace_postcopy_ram_incoming_cleanup_entry();
596
6621883f
PX
597 if (mis->preempt_thread_status == PREEMPT_THREAD_CREATED) {
598 /* Notify the fast load thread to quit */
599 mis->preempt_thread_status = PREEMPT_THREAD_QUIT;
cf02f29e
PX
600 /*
601 * Update preempt_thread_status before reading count. Note: mutex
602 * lock only provide ACQUIRE semantic, and it doesn't stops this
603 * write to be reordered after reading the count.
604 */
605 smp_mb();
606 /*
607 * It's possible that the preempt thread is still handling the last
608 * pages to arrive which were requested by guest page faults.
609 * Making sure nothing is left behind by waiting on the condvar if
610 * that unlikely case happened.
611 */
612 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
613 if (qatomic_read(&mis->page_requested_count)) {
614 /*
615 * It is guaranteed to receive a signal later, because the
616 * count>0 now, so it's destined to be decreased to zero
617 * very soon by the preempt thread.
618 */
619 qemu_cond_wait(&mis->page_request_cond,
620 &mis->page_request_mutex);
621 }
622 }
623 /* Notify the fast load thread to quit */
6621883f
PX
624 if (mis->postcopy_qemufile_dst) {
625 qemu_file_shutdown(mis->postcopy_qemufile_dst);
626 }
36f62f11 627 qemu_thread_join(&mis->postcopy_prio_thread);
6621883f 628 mis->preempt_thread_status = PREEMPT_THREAD_NONE;
36f62f11
PX
629 }
630
c4faeed2 631 if (mis->have_fault_thread) {
46343570
DDAG
632 Error *local_err = NULL;
633
55d0fe82 634 /* Let the fault thread quit */
d73415a3 635 qatomic_set(&mis->fault_thread_quit, 1);
55d0fe82
IM
636 postcopy_fault_thread_notify(mis);
637 trace_postcopy_ram_incoming_cleanup_join();
638 qemu_thread_join(&mis->fault_thread);
639
46343570
DDAG
640 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_END, &local_err)) {
641 error_report_err(local_err);
642 return -1;
643 }
644
fbd162e6 645 if (foreach_not_ignored_block(cleanup_range, mis)) {
c4faeed2
DDAG
646 return -1;
647 }
9ab7ef9b 648
c4faeed2
DDAG
649 trace_postcopy_ram_incoming_cleanup_closeuf();
650 close(mis->userfault_fd);
64f615fe 651 close(mis->userfault_event_fd);
c4faeed2 652 mis->have_fault_thread = false;
1caddf8a
DDAG
653 }
654
58b7c17e
DDAG
655 if (enable_mlock) {
656 if (os_mlock() < 0) {
657 error_report("mlock: %s", strerror(errno));
658 /*
659 * It doesn't feel right to fail at this point, we have a valid
660 * VM state.
661 */
662 }
663 }
664
476ebf77
PX
665 postcopy_temp_pages_cleanup(mis);
666
65ace060
AP
667 trace_postcopy_ram_incoming_cleanup_blocktime(
668 get_postcopy_total_blocktime());
669
c4faeed2 670 trace_postcopy_ram_incoming_cleanup_exit();
1caddf8a
DDAG
671 return 0;
672}
673
f9527107
DDAG
674/*
675 * Disable huge pages on an area
676 */
754cb9c0 677static int nhp_range(RAMBlock *rb, void *opaque)
f9527107 678{
754cb9c0
YK
679 const char *block_name = qemu_ram_get_idstr(rb);
680 void *host_addr = qemu_ram_get_host_addr(rb);
681 ram_addr_t offset = qemu_ram_get_offset(rb);
898ba906 682 ram_addr_t length = rb->postcopy_length;
f9527107
DDAG
683 trace_postcopy_nhp_range(block_name, host_addr, offset, length);
684
685 /*
686 * Before we do discards we need to ensure those discards really
687 * do delete areas of the page, even if THP thinks a hugepage would
688 * be a good idea, so force hugepages off.
689 */
1d741439 690 qemu_madvise(host_addr, length, QEMU_MADV_NOHUGEPAGE);
f9527107
DDAG
691
692 return 0;
693}
694
695/*
696 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
697 * however leaving it until after precopy means that most of the precopy
698 * data is still THPd
699 */
700int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
701{
fbd162e6 702 if (foreach_not_ignored_block(nhp_range, mis)) {
f9527107
DDAG
703 return -1;
704 }
705
706 postcopy_state_set(POSTCOPY_INCOMING_DISCARD);
707
708 return 0;
709}
710
f0a227ad
DDAG
711/*
712 * Mark the given area of RAM as requiring notification to unwritten areas
fbd162e6 713 * Used as a callback on foreach_not_ignored_block.
f0a227ad
DDAG
714 * host_addr: Base of area to mark
715 * offset: Offset in the whole ram arena
716 * length: Length of the section
717 * opaque: MigrationIncomingState pointer
718 * Returns 0 on success
719 */
754cb9c0 720static int ram_block_enable_notify(RAMBlock *rb, void *opaque)
f0a227ad
DDAG
721{
722 MigrationIncomingState *mis = opaque;
723 struct uffdio_register reg_struct;
724
754cb9c0 725 reg_struct.range.start = (uintptr_t)qemu_ram_get_host_addr(rb);
898ba906 726 reg_struct.range.len = rb->postcopy_length;
f0a227ad
DDAG
727 reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
728
729 /* Now tell our userfault_fd that it's responsible for this area */
730 if (ioctl(mis->userfault_fd, UFFDIO_REGISTER, &reg_struct)) {
731 error_report("%s userfault register: %s", __func__, strerror(errno));
732 return -1;
733 }
73b49878 734 if (!(reg_struct.ioctls & (1ULL << _UFFDIO_COPY))) {
665414ad
DDAG
735 error_report("%s userfault: Region doesn't support COPY", __func__);
736 return -1;
737 }
73b49878 738 if (reg_struct.ioctls & (1ULL << _UFFDIO_ZEROPAGE)) {
2ce16640
DDAG
739 qemu_ram_set_uf_zeroable(rb);
740 }
f0a227ad
DDAG
741
742 return 0;
743}
744
5efc3564
DDAG
745int postcopy_wake_shared(struct PostCopyFD *pcfd,
746 uint64_t client_addr,
747 RAMBlock *rb)
748{
749 size_t pagesize = qemu_ram_pagesize(rb);
750 struct uffdio_range range;
751 int ret;
752 trace_postcopy_wake_shared(client_addr, qemu_ram_get_idstr(rb));
7648297d 753 range.start = ROUND_DOWN(client_addr, pagesize);
5efc3564
DDAG
754 range.len = pagesize;
755 ret = ioctl(pcfd->fd, UFFDIO_WAKE, &range);
756 if (ret) {
757 error_report("%s: Failed to wake: %zx in %s (%s)",
758 __func__, (size_t)client_addr, qemu_ram_get_idstr(rb),
759 strerror(errno));
760 }
761 return ret;
762}
763
9470c5e0
DH
764static int postcopy_request_page(MigrationIncomingState *mis, RAMBlock *rb,
765 ram_addr_t start, uint64_t haddr)
766{
767 void *aligned = (void *)(uintptr_t)ROUND_DOWN(haddr, qemu_ram_pagesize(rb));
768
769 /*
770 * Discarded pages (via RamDiscardManager) are never migrated. On unlikely
771 * access, place a zeropage, which will also set the relevant bits in the
772 * recv_bitmap accordingly, so we won't try placing a zeropage twice.
773 *
774 * Checking a single bit is sufficient to handle pagesize > TPS as either
775 * all relevant bits are set or not.
776 */
777 assert(QEMU_IS_ALIGNED(start, qemu_ram_pagesize(rb)));
778 if (ramblock_page_is_discarded(rb, start)) {
779 bool received = ramblock_recv_bitmap_test_byte_offset(rb, start);
780
781 return received ? 0 : postcopy_place_page_zero(mis, aligned, rb);
782 }
783
784 return migrate_send_rp_req_pages(mis, rb, start, haddr);
785}
786
096bf4c8
DDAG
787/*
788 * Callback from shared fault handlers to ask for a page,
789 * the page must be specified by a RAMBlock and an offset in that rb
790 * Note: Only for use by shared fault handlers (in fault thread)
791 */
792int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
793 uint64_t client_addr, uint64_t rb_offset)
794{
7648297d 795 uint64_t aligned_rbo = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
096bf4c8
DDAG
796 MigrationIncomingState *mis = migration_incoming_get_current();
797
798 trace_postcopy_request_shared_page(pcfd->idstr, qemu_ram_get_idstr(rb),
799 rb_offset);
dedfb4b2
DDAG
800 if (ramblock_recv_bitmap_test_byte_offset(rb, aligned_rbo)) {
801 trace_postcopy_request_shared_page_present(pcfd->idstr,
802 qemu_ram_get_idstr(rb), rb_offset);
803 return postcopy_wake_shared(pcfd, client_addr, rb);
804 }
9470c5e0 805 postcopy_request_page(mis, rb, aligned_rbo, client_addr);
096bf4c8
DDAG
806 return 0;
807}
808
575b0b33
AP
809static int get_mem_fault_cpu_index(uint32_t pid)
810{
811 CPUState *cpu_iter;
812
813 CPU_FOREACH(cpu_iter) {
814 if (cpu_iter->thread_id == pid) {
815 trace_get_mem_fault_cpu_index(cpu_iter->cpu_index, pid);
816 return cpu_iter->cpu_index;
817 }
818 }
819 trace_get_mem_fault_cpu_index(-1, pid);
820 return -1;
821}
822
823static uint32_t get_low_time_offset(PostcopyBlocktimeContext *dc)
824{
825 int64_t start_time_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) -
826 dc->start_time;
827 return start_time_offset < 1 ? 1 : start_time_offset & UINT32_MAX;
828}
829
830/*
831 * This function is being called when pagefault occurs. It
832 * tracks down vCPU blocking time.
833 *
834 * @addr: faulted host virtual address
835 * @ptid: faulted process thread id
836 * @rb: ramblock appropriate to addr
837 */
838static void mark_postcopy_blocktime_begin(uintptr_t addr, uint32_t ptid,
839 RAMBlock *rb)
840{
841 int cpu, already_received;
842 MigrationIncomingState *mis = migration_incoming_get_current();
843 PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
844 uint32_t low_time_offset;
845
846 if (!dc || ptid == 0) {
847 return;
848 }
849 cpu = get_mem_fault_cpu_index(ptid);
850 if (cpu < 0) {
851 return;
852 }
853
854 low_time_offset = get_low_time_offset(dc);
855 if (dc->vcpu_addr[cpu] == 0) {
d73415a3 856 qatomic_inc(&dc->smp_cpus_down);
575b0b33
AP
857 }
858
d73415a3
SH
859 qatomic_xchg(&dc->last_begin, low_time_offset);
860 qatomic_xchg(&dc->page_fault_vcpu_time[cpu], low_time_offset);
861 qatomic_xchg(&dc->vcpu_addr[cpu], addr);
575b0b33 862
da1725d3
WY
863 /*
864 * check it here, not at the beginning of the function,
865 * due to, check could occur early than bitmap_set in
866 * qemu_ufd_copy_ioctl
867 */
575b0b33
AP
868 already_received = ramblock_recv_bitmap_test(rb, (void *)addr);
869 if (already_received) {
d73415a3
SH
870 qatomic_xchg(&dc->vcpu_addr[cpu], 0);
871 qatomic_xchg(&dc->page_fault_vcpu_time[cpu], 0);
872 qatomic_dec(&dc->smp_cpus_down);
575b0b33
AP
873 }
874 trace_mark_postcopy_blocktime_begin(addr, dc, dc->page_fault_vcpu_time[cpu],
875 cpu, already_received);
876}
877
878/*
879 * This function just provide calculated blocktime per cpu and trace it.
880 * Total blocktime is calculated in mark_postcopy_blocktime_end.
881 *
882 *
883 * Assume we have 3 CPU
884 *
885 * S1 E1 S1 E1
886 * -----***********------------xxx***************------------------------> CPU1
887 *
888 * S2 E2
889 * ------------****************xxx---------------------------------------> CPU2
890 *
891 * S3 E3
892 * ------------------------****xxx********-------------------------------> CPU3
893 *
894 * We have sequence S1,S2,E1,S3,S1,E2,E3,E1
895 * S2,E1 - doesn't match condition due to sequence S1,S2,E1 doesn't include CPU3
896 * S3,S1,E2 - sequence includes all CPUs, in this case overlap will be S1,E2 -
897 * it's a part of total blocktime.
898 * S1 - here is last_begin
899 * Legend of the picture is following:
900 * * - means blocktime per vCPU
901 * x - means overlapped blocktime (total blocktime)
902 *
903 * @addr: host virtual address
904 */
905static void mark_postcopy_blocktime_end(uintptr_t addr)
906{
907 MigrationIncomingState *mis = migration_incoming_get_current();
908 PostcopyBlocktimeContext *dc = mis->blocktime_ctx;
5cc8767d
LX
909 MachineState *ms = MACHINE(qdev_get_machine());
910 unsigned int smp_cpus = ms->smp.cpus;
575b0b33
AP
911 int i, affected_cpu = 0;
912 bool vcpu_total_blocktime = false;
913 uint32_t read_vcpu_time, low_time_offset;
914
915 if (!dc) {
916 return;
917 }
918
919 low_time_offset = get_low_time_offset(dc);
920 /* lookup cpu, to clear it,
3a4452d8 921 * that algorithm looks straightforward, but it's not
575b0b33
AP
922 * optimal, more optimal algorithm is keeping tree or hash
923 * where key is address value is a list of */
924 for (i = 0; i < smp_cpus; i++) {
925 uint32_t vcpu_blocktime = 0;
926
d73415a3
SH
927 read_vcpu_time = qatomic_fetch_add(&dc->page_fault_vcpu_time[i], 0);
928 if (qatomic_fetch_add(&dc->vcpu_addr[i], 0) != addr ||
575b0b33
AP
929 read_vcpu_time == 0) {
930 continue;
931 }
d73415a3 932 qatomic_xchg(&dc->vcpu_addr[i], 0);
575b0b33
AP
933 vcpu_blocktime = low_time_offset - read_vcpu_time;
934 affected_cpu += 1;
935 /* we need to know is that mark_postcopy_end was due to
936 * faulted page, another possible case it's prefetched
937 * page and in that case we shouldn't be here */
938 if (!vcpu_total_blocktime &&
d73415a3 939 qatomic_fetch_add(&dc->smp_cpus_down, 0) == smp_cpus) {
575b0b33
AP
940 vcpu_total_blocktime = true;
941 }
942 /* continue cycle, due to one page could affect several vCPUs */
943 dc->vcpu_blocktime[i] += vcpu_blocktime;
944 }
945
d73415a3 946 qatomic_sub(&dc->smp_cpus_down, affected_cpu);
575b0b33 947 if (vcpu_total_blocktime) {
d73415a3 948 dc->total_blocktime += low_time_offset - qatomic_fetch_add(
575b0b33
AP
949 &dc->last_begin, 0);
950 }
951 trace_mark_postcopy_blocktime_end(addr, dc, dc->total_blocktime,
952 affected_cpu);
953}
954
27dd21b4 955static void postcopy_pause_fault_thread(MigrationIncomingState *mis)
3a7804c3
PX
956{
957 trace_postcopy_pause_fault_thread();
3a7804c3 958 qemu_sem_wait(&mis->postcopy_pause_sem_fault);
3a7804c3 959 trace_postcopy_pause_fault_thread_continued();
3a7804c3
PX
960}
961
f0a227ad
DDAG
962/*
963 * Handle faults detected by the USERFAULT markings
964 */
965static void *postcopy_ram_fault_thread(void *opaque)
966{
967 MigrationIncomingState *mis = opaque;
c4faeed2
DDAG
968 struct uffd_msg msg;
969 int ret;
00fa4fc8 970 size_t index;
c4faeed2 971 RAMBlock *rb = NULL;
f0a227ad 972
c4faeed2 973 trace_postcopy_ram_fault_thread_entry();
74637e6f 974 rcu_register_thread();
096bf4c8 975 mis->last_rb = NULL; /* last RAMBlock we sent part of */
095c12a4 976 qemu_sem_post(&mis->thread_sync_sem);
f0a227ad 977
00fa4fc8
DDAG
978 struct pollfd *pfd;
979 size_t pfd_len = 2 + mis->postcopy_remote_fds->len;
980
981 pfd = g_new0(struct pollfd, pfd_len);
982
983 pfd[0].fd = mis->userfault_fd;
984 pfd[0].events = POLLIN;
985 pfd[1].fd = mis->userfault_event_fd;
986 pfd[1].events = POLLIN; /* Waiting for eventfd to go positive */
987 trace_postcopy_ram_fault_thread_fds_core(pfd[0].fd, pfd[1].fd);
988 for (index = 0; index < mis->postcopy_remote_fds->len; index++) {
989 struct PostCopyFD *pcfd = &g_array_index(mis->postcopy_remote_fds,
990 struct PostCopyFD, index);
991 pfd[2 + index].fd = pcfd->fd;
992 pfd[2 + index].events = POLLIN;
993 trace_postcopy_ram_fault_thread_fds_extra(2 + index, pcfd->idstr,
994 pcfd->fd);
995 }
996
c4faeed2
DDAG
997 while (true) {
998 ram_addr_t rb_offset;
00fa4fc8 999 int poll_result;
c4faeed2
DDAG
1000
1001 /*
1002 * We're mainly waiting for the kernel to give us a faulting HVA,
1003 * however we can be told to quit via userfault_quit_fd which is
1004 * an eventfd
1005 */
00fa4fc8
DDAG
1006
1007 poll_result = poll(pfd, pfd_len, -1 /* Wait forever */);
1008 if (poll_result == -1) {
c4faeed2
DDAG
1009 error_report("%s: userfault poll: %s", __func__, strerror(errno));
1010 break;
1011 }
1012
3a7804c3
PX
1013 if (!mis->to_src_file) {
1014 /*
1015 * Possibly someone tells us that the return path is
1016 * broken already using the event. We should hold until
1017 * the channel is rebuilt.
1018 */
27dd21b4 1019 postcopy_pause_fault_thread(mis);
3a7804c3
PX
1020 }
1021
c4faeed2 1022 if (pfd[1].revents) {
64f615fe
PX
1023 uint64_t tmp64 = 0;
1024
1025 /* Consume the signal */
1026 if (read(mis->userfault_event_fd, &tmp64, 8) != 8) {
1027 /* Nothing obviously nicer than posting this error. */
1028 error_report("%s: read() failed", __func__);
1029 }
1030
d73415a3 1031 if (qatomic_read(&mis->fault_thread_quit)) {
64f615fe
PX
1032 trace_postcopy_ram_fault_thread_quit();
1033 break;
1034 }
c4faeed2
DDAG
1035 }
1036
00fa4fc8
DDAG
1037 if (pfd[0].revents) {
1038 poll_result--;
1039 ret = read(mis->userfault_fd, &msg, sizeof(msg));
1040 if (ret != sizeof(msg)) {
1041 if (errno == EAGAIN) {
1042 /*
1043 * if a wake up happens on the other thread just after
1044 * the poll, there is nothing to read.
1045 */
1046 continue;
1047 }
1048 if (ret < 0) {
1049 error_report("%s: Failed to read full userfault "
1050 "message: %s",
1051 __func__, strerror(errno));
1052 break;
1053 } else {
1054 error_report("%s: Read %d bytes from userfaultfd "
1055 "expected %zd",
1056 __func__, ret, sizeof(msg));
1057 break; /* Lost alignment, don't know what we'd read next */
1058 }
c4faeed2 1059 }
00fa4fc8
DDAG
1060 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1061 error_report("%s: Read unexpected event %ud from userfaultfd",
1062 __func__, msg.event);
1063 continue; /* It's not a page fault, shouldn't happen */
c4faeed2 1064 }
c4faeed2 1065
00fa4fc8
DDAG
1066 rb = qemu_ram_block_from_host(
1067 (void *)(uintptr_t)msg.arg.pagefault.address,
1068 true, &rb_offset);
1069 if (!rb) {
1070 error_report("postcopy_ram_fault_thread: Fault outside guest: %"
1071 PRIx64, (uint64_t)msg.arg.pagefault.address);
1072 break;
1073 }
c4faeed2 1074
7648297d 1075 rb_offset = ROUND_DOWN(rb_offset, qemu_ram_pagesize(rb));
00fa4fc8 1076 trace_postcopy_ram_fault_thread_request(msg.arg.pagefault.address,
c4faeed2 1077 qemu_ram_get_idstr(rb),
575b0b33
AP
1078 rb_offset,
1079 msg.arg.pagefault.feat.ptid);
1080 mark_postcopy_blocktime_begin(
1081 (uintptr_t)(msg.arg.pagefault.address),
1082 msg.arg.pagefault.feat.ptid, rb);
1083
3a7804c3 1084retry:
00fa4fc8
DDAG
1085 /*
1086 * Send the request to the source - we want to request one
1087 * of our host page sizes (which is >= TPS)
1088 */
9470c5e0
DH
1089 ret = postcopy_request_page(mis, rb, rb_offset,
1090 msg.arg.pagefault.address);
3a7804c3
PX
1091 if (ret) {
1092 /* May be network failure, try to wait for recovery */
27dd21b4
PX
1093 postcopy_pause_fault_thread(mis);
1094 goto retry;
00fa4fc8
DDAG
1095 }
1096 }
c4faeed2 1097
00fa4fc8
DDAG
1098 /* Now handle any requests from external processes on shared memory */
1099 /* TODO: May need to handle devices deregistering during postcopy */
1100 for (index = 2; index < pfd_len && poll_result; index++) {
1101 if (pfd[index].revents) {
1102 struct PostCopyFD *pcfd =
1103 &g_array_index(mis->postcopy_remote_fds,
1104 struct PostCopyFD, index - 2);
1105
1106 poll_result--;
1107 if (pfd[index].revents & POLLERR) {
1108 error_report("%s: POLLERR on poll %zd fd=%d",
1109 __func__, index, pcfd->fd);
1110 pfd[index].events = 0;
1111 continue;
1112 }
1113
1114 ret = read(pcfd->fd, &msg, sizeof(msg));
1115 if (ret != sizeof(msg)) {
1116 if (errno == EAGAIN) {
1117 /*
1118 * if a wake up happens on the other thread just after
1119 * the poll, there is nothing to read.
1120 */
1121 continue;
1122 }
1123 if (ret < 0) {
1124 error_report("%s: Failed to read full userfault "
1125 "message: %s (shared) revents=%d",
1126 __func__, strerror(errno),
1127 pfd[index].revents);
1128 /*TODO: Could just disable this sharer */
1129 break;
1130 } else {
1131 error_report("%s: Read %d bytes from userfaultfd "
1132 "expected %zd (shared)",
1133 __func__, ret, sizeof(msg));
1134 /*TODO: Could just disable this sharer */
1135 break; /*Lost alignment,don't know what we'd read next*/
1136 }
1137 }
1138 if (msg.event != UFFD_EVENT_PAGEFAULT) {
1139 error_report("%s: Read unexpected event %ud "
1140 "from userfaultfd (shared)",
1141 __func__, msg.event);
1142 continue; /* It's not a page fault, shouldn't happen */
1143 }
1144 /* Call the device handler registered with us */
1145 ret = pcfd->handler(pcfd, &msg);
1146 if (ret) {
1147 error_report("%s: Failed to resolve shared fault on %zd/%s",
1148 __func__, index, pcfd->idstr);
1149 /* TODO: Fail? Disable this sharer? */
1150 }
1151 }
c4faeed2
DDAG
1152 }
1153 }
74637e6f 1154 rcu_unregister_thread();
c4faeed2 1155 trace_postcopy_ram_fault_thread_exit();
fc6008f3 1156 g_free(pfd);
f0a227ad
DDAG
1157 return NULL;
1158}
1159
476ebf77
PX
1160static int postcopy_temp_pages_setup(MigrationIncomingState *mis)
1161{
77dadc3f
PX
1162 PostcopyTmpPage *tmp_page;
1163 int err, i, channels;
1164 void *temp_page;
1165
36f62f11
PX
1166 if (migrate_postcopy_preempt()) {
1167 /* If preemption enabled, need extra channel for urgent requests */
1168 mis->postcopy_channels = RAM_CHANNEL_MAX;
1169 } else {
1170 /* Both precopy/postcopy on the same channel */
1171 mis->postcopy_channels = 1;
1172 }
77dadc3f
PX
1173
1174 channels = mis->postcopy_channels;
1175 mis->postcopy_tmp_pages = g_malloc0_n(sizeof(PostcopyTmpPage), channels);
1176
1177 for (i = 0; i < channels; i++) {
1178 tmp_page = &mis->postcopy_tmp_pages[i];
1179 temp_page = mmap(NULL, mis->largest_page_size, PROT_READ | PROT_WRITE,
1180 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1181 if (temp_page == MAP_FAILED) {
1182 err = errno;
1183 error_report("%s: Failed to map postcopy_tmp_pages[%d]: %s",
1184 __func__, i, strerror(err));
1185 /* Clean up will be done later */
1186 return -err;
1187 }
1188 tmp_page->tmp_huge_page = temp_page;
1189 /* Initialize default states for each tmp page */
1190 postcopy_temp_page_reset(tmp_page);
476ebf77
PX
1191 }
1192
1193 /*
1194 * Map large zero page when kernel can't use UFFDIO_ZEROPAGE for hugepages
1195 */
1196 mis->postcopy_tmp_zero_page = mmap(NULL, mis->largest_page_size,
1197 PROT_READ | PROT_WRITE,
1198 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
1199 if (mis->postcopy_tmp_zero_page == MAP_FAILED) {
1200 err = errno;
1201 mis->postcopy_tmp_zero_page = NULL;
1202 error_report("%s: Failed to map large zero page %s",
1203 __func__, strerror(err));
1204 return -err;
1205 }
1206
1207 memset(mis->postcopy_tmp_zero_page, '\0', mis->largest_page_size);
1208
1209 return 0;
1210}
1211
2a7eb148 1212int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
f0a227ad 1213{
74c38cf7
PX
1214 Error *local_err = NULL;
1215
c4faeed2 1216 /* Open the fd for the kernel to give us userfaults */
d5890ea0 1217 mis->userfault_fd = uffd_open(O_CLOEXEC | O_NONBLOCK);
c4faeed2
DDAG
1218 if (mis->userfault_fd == -1) {
1219 error_report("%s: Failed to open userfault fd: %s", __func__,
1220 strerror(errno));
1221 return -1;
1222 }
1223
1224 /*
1225 * Although the host check already tested the API, we need to
1226 * do the check again as an ABI handshake on the new fd.
1227 */
74c38cf7
PX
1228 if (!ufd_check_and_apply(mis->userfault_fd, mis, &local_err)) {
1229 error_report_err(local_err);
c4faeed2
DDAG
1230 return -1;
1231 }
1232
1233 /* Now an eventfd we use to tell the fault-thread to quit */
64f615fe
PX
1234 mis->userfault_event_fd = eventfd(0, EFD_CLOEXEC);
1235 if (mis->userfault_event_fd == -1) {
1236 error_report("%s: Opening userfault_event_fd: %s", __func__,
c4faeed2
DDAG
1237 strerror(errno));
1238 close(mis->userfault_fd);
1239 return -1;
1240 }
1241
36f62f11 1242 postcopy_thread_create(mis, &mis->fault_thread, "fault-default",
095c12a4 1243 postcopy_ram_fault_thread, QEMU_THREAD_JOINABLE);
c4faeed2 1244 mis->have_fault_thread = true;
f0a227ad
DDAG
1245
1246 /* Mark so that we get notified of accesses to unwritten areas */
fbd162e6 1247 if (foreach_not_ignored_block(ram_block_enable_notify, mis)) {
91b02dc7 1248 error_report("ram_block_enable_notify failed");
f0a227ad
DDAG
1249 return -1;
1250 }
1251
476ebf77
PX
1252 if (postcopy_temp_pages_setup(mis)) {
1253 /* Error dumped in the sub-function */
3414322a
WY
1254 return -1;
1255 }
1256
36f62f11
PX
1257 if (migrate_postcopy_preempt()) {
1258 /*
1259 * This thread needs to be created after the temp pages because
1260 * it'll fetch RAM_CHANNEL_POSTCOPY PostcopyTmpPage immediately.
1261 */
1262 postcopy_thread_create(mis, &mis->postcopy_prio_thread, "fault-fast",
1263 postcopy_preempt_thread, QEMU_THREAD_JOINABLE);
6621883f 1264 mis->preempt_thread_status = PREEMPT_THREAD_CREATED;
36f62f11
PX
1265 }
1266
c4faeed2
DDAG
1267 trace_postcopy_ram_enable_notify();
1268
f0a227ad
DDAG
1269 return 0;
1270}
1271
eef621c4 1272static int qemu_ufd_copy_ioctl(MigrationIncomingState *mis, void *host_addr,
f9494614 1273 void *from_addr, uint64_t pagesize, RAMBlock *rb)
727b9d7e 1274{
eef621c4 1275 int userfault_fd = mis->userfault_fd;
f9494614 1276 int ret;
eef621c4 1277
727b9d7e
AP
1278 if (from_addr) {
1279 struct uffdio_copy copy_struct;
1280 copy_struct.dst = (uint64_t)(uintptr_t)host_addr;
1281 copy_struct.src = (uint64_t)(uintptr_t)from_addr;
1282 copy_struct.len = pagesize;
1283 copy_struct.mode = 0;
f9494614 1284 ret = ioctl(userfault_fd, UFFDIO_COPY, &copy_struct);
727b9d7e
AP
1285 } else {
1286 struct uffdio_zeropage zero_struct;
1287 zero_struct.range.start = (uint64_t)(uintptr_t)host_addr;
1288 zero_struct.range.len = pagesize;
1289 zero_struct.mode = 0;
f9494614
AP
1290 ret = ioctl(userfault_fd, UFFDIO_ZEROPAGE, &zero_struct);
1291 }
1292 if (!ret) {
8f8bfffc 1293 qemu_mutex_lock(&mis->page_request_mutex);
f9494614
AP
1294 ramblock_recv_bitmap_set_range(rb, host_addr,
1295 pagesize / qemu_target_page_size());
8f8bfffc
PX
1296 /*
1297 * If this page resolves a page fault for a previous recorded faulted
1298 * address, take a special note to maintain the requested page list.
1299 */
1300 if (g_tree_lookup(mis->page_requested, host_addr)) {
1301 g_tree_remove(mis->page_requested, host_addr);
cf02f29e
PX
1302 int left_pages = qatomic_dec_fetch(&mis->page_requested_count);
1303
8f8bfffc 1304 trace_postcopy_page_req_del(host_addr, mis->page_requested_count);
cf02f29e
PX
1305 /* Order the update of count and read of preempt status */
1306 smp_mb();
1307 if (mis->preempt_thread_status == PREEMPT_THREAD_QUIT &&
1308 left_pages == 0) {
1309 /*
1310 * This probably means the main thread is waiting for us.
1311 * Notify that we've finished receiving the last requested
1312 * page.
1313 */
1314 qemu_cond_signal(&mis->page_request_cond);
1315 }
8f8bfffc
PX
1316 }
1317 qemu_mutex_unlock(&mis->page_request_mutex);
575b0b33 1318 mark_postcopy_blocktime_end((uintptr_t)host_addr);
727b9d7e 1319 }
f9494614 1320 return ret;
727b9d7e
AP
1321}
1322
d488b349
DDAG
1323int postcopy_notify_shared_wake(RAMBlock *rb, uint64_t offset)
1324{
1325 int i;
1326 MigrationIncomingState *mis = migration_incoming_get_current();
1327 GArray *pcrfds = mis->postcopy_remote_fds;
1328
1329 for (i = 0; i < pcrfds->len; i++) {
1330 struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1331 int ret = cur->waker(cur, rb, offset);
1332 if (ret) {
1333 return ret;
1334 }
1335 }
1336 return 0;
1337}
1338
696ed9a9
DDAG
1339/*
1340 * Place a host page (from) at (host) atomically
1341 * returns 0 on success
1342 */
df9ff5e1 1343int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
8be4620b 1344 RAMBlock *rb)
696ed9a9 1345{
8be4620b 1346 size_t pagesize = qemu_ram_pagesize(rb);
696ed9a9 1347
696ed9a9
DDAG
1348 /* copy also acks to the kernel waking the stalled thread up
1349 * TODO: We can inhibit that ack and only do it if it was requested
1350 * which would be slightly cheaper, but we'd have to be careful
1351 * of the order of updating our page state.
1352 */
eef621c4 1353 if (qemu_ufd_copy_ioctl(mis, host, from, pagesize, rb)) {
696ed9a9 1354 int e = errno;
df9ff5e1
DDAG
1355 error_report("%s: %s copy host: %p from: %p (size: %zd)",
1356 __func__, strerror(e), host, from, pagesize);
696ed9a9
DDAG
1357
1358 return -e;
1359 }
1360
1361 trace_postcopy_place_page(host);
dedfb4b2
DDAG
1362 return postcopy_notify_shared_wake(rb,
1363 qemu_ram_block_host_offset(rb, host));
696ed9a9
DDAG
1364}
1365
1366/*
1367 * Place a zero page at (host) atomically
1368 * returns 0 on success
1369 */
df9ff5e1 1370int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
8be4620b 1371 RAMBlock *rb)
696ed9a9 1372{
2ce16640 1373 size_t pagesize = qemu_ram_pagesize(rb);
df9ff5e1 1374 trace_postcopy_place_page_zero(host);
696ed9a9 1375
2ce16640
DDAG
1376 /* Normal RAMBlocks can zero a page using UFFDIO_ZEROPAGE
1377 * but it's not available for everything (e.g. hugetlbpages)
1378 */
1379 if (qemu_ram_is_uf_zeroable(rb)) {
eef621c4 1380 if (qemu_ufd_copy_ioctl(mis, host, NULL, pagesize, rb)) {
df9ff5e1
DDAG
1381 int e = errno;
1382 error_report("%s: %s zero host: %p",
1383 __func__, strerror(e), host);
696ed9a9 1384
df9ff5e1
DDAG
1385 return -e;
1386 }
dedfb4b2
DDAG
1387 return postcopy_notify_shared_wake(rb,
1388 qemu_ram_block_host_offset(rb,
1389 host));
df9ff5e1 1390 } else {
6629890d 1391 return postcopy_place_page(mis, host, mis->postcopy_tmp_zero_page, rb);
696ed9a9 1392 }
696ed9a9
DDAG
1393}
1394
eb59db53
DDAG
1395#else
1396/* No target OS support, stubs just fail */
65ace060
AP
1397void fill_destination_postcopy_migration_info(MigrationInfo *info)
1398{
1399}
1400
74c38cf7 1401bool postcopy_ram_supported_by_host(MigrationIncomingState *mis, Error **errp)
eb59db53
DDAG
1402{
1403 error_report("%s: No OS support", __func__);
1404 return false;
1405}
1406
c136180c 1407int postcopy_ram_incoming_init(MigrationIncomingState *mis)
1caddf8a
DDAG
1408{
1409 error_report("postcopy_ram_incoming_init: No OS support");
1410 return -1;
1411}
1412
1413int postcopy_ram_incoming_cleanup(MigrationIncomingState *mis)
1414{
1415 assert(0);
1416 return -1;
1417}
1418
f9527107
DDAG
1419int postcopy_ram_prepare_discard(MigrationIncomingState *mis)
1420{
1421 assert(0);
1422 return -1;
1423}
1424
c188c539
MT
1425int postcopy_request_shared_page(struct PostCopyFD *pcfd, RAMBlock *rb,
1426 uint64_t client_addr, uint64_t rb_offset)
1427{
1428 assert(0);
1429 return -1;
1430}
1431
2a7eb148 1432int postcopy_ram_incoming_setup(MigrationIncomingState *mis)
f0a227ad
DDAG
1433{
1434 assert(0);
1435 return -1;
1436}
696ed9a9 1437
df9ff5e1 1438int postcopy_place_page(MigrationIncomingState *mis, void *host, void *from,
8be4620b 1439 RAMBlock *rb)
696ed9a9
DDAG
1440{
1441 assert(0);
1442 return -1;
1443}
1444
df9ff5e1 1445int postcopy_place_page_zero(MigrationIncomingState *mis, void *host,
8be4620b 1446 RAMBlock *rb)
696ed9a9
DDAG
1447{
1448 assert(0);
1449 return -1;
1450}
1451
5efc3564
DDAG
1452int postcopy_wake_shared(struct PostCopyFD *pcfd,
1453 uint64_t client_addr,
1454 RAMBlock *rb)
1455{
1456 assert(0);
1457 return -1;
1458}
eb59db53
DDAG
1459#endif
1460
e0b266f0 1461/* ------------------------------------------------------------------------- */
77dadc3f
PX
1462void postcopy_temp_page_reset(PostcopyTmpPage *tmp_page)
1463{
1464 tmp_page->target_pages = 0;
1465 tmp_page->host_addr = NULL;
1466 /*
1467 * This is set to true when reset, and cleared as long as we received any
1468 * of the non-zero small page within this huge page.
1469 */
1470 tmp_page->all_zero = true;
1471}
e0b266f0 1472
9ab7ef9b
PX
1473void postcopy_fault_thread_notify(MigrationIncomingState *mis)
1474{
1475 uint64_t tmp64 = 1;
1476
1477 /*
1478 * Wakeup the fault_thread. It's an eventfd that should currently
1479 * be at 0, we're going to increment it to 1
1480 */
1481 if (write(mis->userfault_event_fd, &tmp64, 8) != 8) {
1482 /* Not much we can do here, but may as well report it */
1483 error_report("%s: incrementing failed: %s", __func__,
1484 strerror(errno));
1485 }
1486}
1487
e0b266f0
DDAG
1488/**
1489 * postcopy_discard_send_init: Called at the start of each RAMBlock before
1490 * asking to discard individual ranges.
1491 *
1492 * @ms: The current migration state.
810cf2bb 1493 * @offset: the bitmap offset of the named RAMBlock in the migration bitmap.
e0b266f0 1494 * @name: RAMBlock that discards will operate on.
e0b266f0 1495 */
810cf2bb
WY
1496static PostcopyDiscardState pds = {0};
1497void postcopy_discard_send_init(MigrationState *ms, const char *name)
e0b266f0 1498{
810cf2bb
WY
1499 pds.ramblock_name = name;
1500 pds.cur_entry = 0;
1501 pds.nsentwords = 0;
1502 pds.nsentcmds = 0;
e0b266f0
DDAG
1503}
1504
1505/**
1506 * postcopy_discard_send_range: Called by the bitmap code for each chunk to
1507 * discard. May send a discard message, may just leave it queued to
1508 * be sent later.
1509 *
1510 * @ms: Current migration state.
e0b266f0
DDAG
1511 * @start,@length: a range of pages in the migration bitmap in the
1512 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
1513 */
810cf2bb
WY
1514void postcopy_discard_send_range(MigrationState *ms, unsigned long start,
1515 unsigned long length)
e0b266f0 1516{
20afaed9 1517 size_t tp_size = qemu_target_page_size();
e0b266f0 1518 /* Convert to byte offsets within the RAM block */
810cf2bb
WY
1519 pds.start_list[pds.cur_entry] = start * tp_size;
1520 pds.length_list[pds.cur_entry] = length * tp_size;
1521 trace_postcopy_discard_send_range(pds.ramblock_name, start, length);
1522 pds.cur_entry++;
1523 pds.nsentwords++;
e0b266f0 1524
810cf2bb 1525 if (pds.cur_entry == MAX_DISCARDS_PER_COMMAND) {
e0b266f0 1526 /* Full set, ship it! */
89a02a9f 1527 qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
810cf2bb
WY
1528 pds.ramblock_name,
1529 pds.cur_entry,
1530 pds.start_list,
1531 pds.length_list);
1532 pds.nsentcmds++;
1533 pds.cur_entry = 0;
e0b266f0
DDAG
1534 }
1535}
1536
1537/**
1538 * postcopy_discard_send_finish: Called at the end of each RAMBlock by the
1539 * bitmap code. Sends any outstanding discard messages, frees the PDS
1540 *
1541 * @ms: Current migration state.
e0b266f0 1542 */
810cf2bb 1543void postcopy_discard_send_finish(MigrationState *ms)
e0b266f0
DDAG
1544{
1545 /* Anything unsent? */
810cf2bb 1546 if (pds.cur_entry) {
89a02a9f 1547 qemu_savevm_send_postcopy_ram_discard(ms->to_dst_file,
810cf2bb
WY
1548 pds.ramblock_name,
1549 pds.cur_entry,
1550 pds.start_list,
1551 pds.length_list);
1552 pds.nsentcmds++;
e0b266f0
DDAG
1553 }
1554
810cf2bb
WY
1555 trace_postcopy_discard_send_finish(pds.ramblock_name, pds.nsentwords,
1556 pds.nsentcmds);
e0b266f0 1557}
bac3b212
JQ
1558
1559/*
1560 * Current state of incoming postcopy; note this is not part of
1561 * MigrationIncomingState since it's state is used during cleanup
1562 * at the end as MIS is being freed.
1563 */
1564static PostcopyState incoming_postcopy_state;
1565
1566PostcopyState postcopy_state_get(void)
1567{
4592eaf3 1568 return qatomic_load_acquire(&incoming_postcopy_state);
bac3b212
JQ
1569}
1570
1571/* Set the state and return the old state */
1572PostcopyState postcopy_state_set(PostcopyState new_state)
1573{
d73415a3 1574 return qatomic_xchg(&incoming_postcopy_state, new_state);
bac3b212 1575}
00fa4fc8
DDAG
1576
1577/* Register a handler for external shared memory postcopy
1578 * called on the destination.
1579 */
1580void postcopy_register_shared_ufd(struct PostCopyFD *pcfd)
1581{
1582 MigrationIncomingState *mis = migration_incoming_get_current();
1583
1584 mis->postcopy_remote_fds = g_array_append_val(mis->postcopy_remote_fds,
1585 *pcfd);
1586}
1587
1588/* Unregister a handler for external shared memory postcopy
1589 */
1590void postcopy_unregister_shared_ufd(struct PostCopyFD *pcfd)
1591{
1592 guint i;
1593 MigrationIncomingState *mis = migration_incoming_get_current();
1594 GArray *pcrfds = mis->postcopy_remote_fds;
1595
56559980
JQ
1596 if (!pcrfds) {
1597 /* migration has already finished and freed the array */
1598 return;
1599 }
00fa4fc8
DDAG
1600 for (i = 0; i < pcrfds->len; i++) {
1601 struct PostCopyFD *cur = &g_array_index(pcrfds, struct PostCopyFD, i);
1602 if (cur->fd == pcfd->fd) {
1603 mis->postcopy_remote_fds = g_array_remove_index(pcrfds, i);
1604 return;
1605 }
1606 }
1607}
36f62f11 1608
6720c2b3 1609void postcopy_preempt_new_channel(MigrationIncomingState *mis, QEMUFile *file)
36f62f11
PX
1610{
1611 /*
1612 * The new loading channel has its own threads, so it needs to be
1613 * blocked too. It's by default true, just be explicit.
1614 */
1615 qemu_file_set_blocking(file, true);
1616 mis->postcopy_qemufile_dst = file;
5655aab0 1617 qemu_sem_post(&mis->postcopy_qemufile_dst_done);
36f62f11 1618 trace_postcopy_preempt_new_channel();
36f62f11
PX
1619}
1620
f0afaf6c
PX
1621/*
1622 * Setup the postcopy preempt channel with the IOC. If ERROR is specified,
1623 * setup the error instead. This helper will free the ERROR if specified.
1624 */
d0edb8a1 1625static void
f0afaf6c
PX
1626postcopy_preempt_send_channel_done(MigrationState *s,
1627 QIOChannel *ioc, Error *local_err)
36f62f11 1628{
f0afaf6c 1629 if (local_err) {
d0edb8a1
PX
1630 migrate_set_error(s, local_err);
1631 error_free(local_err);
1632 } else {
1633 migration_ioc_register_yank(ioc);
1634 s->postcopy_qemufile_src = qemu_file_new_output(ioc);
1635 trace_postcopy_preempt_new_channel();
1636 }
1637
1638 /*
1639 * Kick the waiter in all cases. The waiter should check upon
1640 * postcopy_qemufile_src to know whether it failed or not.
1641 */
1642 qemu_sem_post(&s->postcopy_qemufile_src_sem);
f0afaf6c
PX
1643}
1644
1645static void
1646postcopy_preempt_tls_handshake(QIOTask *task, gpointer opaque)
1647{
1648 g_autoptr(QIOChannel) ioc = QIO_CHANNEL(qio_task_get_source(task));
1649 MigrationState *s = opaque;
1650 Error *local_err = NULL;
1651
1652 qio_task_propagate_error(task, &local_err);
1653 postcopy_preempt_send_channel_done(s, ioc, local_err);
1654}
1655
1656static void
1657postcopy_preempt_send_channel_new(QIOTask *task, gpointer opaque)
1658{
1659 g_autoptr(QIOChannel) ioc = QIO_CHANNEL(qio_task_get_source(task));
1660 MigrationState *s = opaque;
1661 QIOChannelTLS *tioc;
1662 Error *local_err = NULL;
1663
1664 if (qio_task_propagate_error(task, &local_err)) {
1665 goto out;
1666 }
1667
1668 if (migrate_channel_requires_tls_upgrade(ioc)) {
0deb7e9b 1669 tioc = migration_tls_client_create(ioc, s->hostname, &local_err);
f0afaf6c
PX
1670 if (!tioc) {
1671 goto out;
1672 }
1673 trace_postcopy_preempt_tls_handshake();
1674 qio_channel_set_name(QIO_CHANNEL(tioc), "migration-tls-preempt");
1675 qio_channel_tls_handshake(tioc, postcopy_preempt_tls_handshake,
1676 s, NULL, NULL);
1677 /* Setup the channel until TLS handshake finished */
1678 return;
1679 }
1680
1681out:
1682 /* This handles both good and error cases */
1683 postcopy_preempt_send_channel_done(s, ioc, local_err);
d0edb8a1 1684}
36f62f11 1685
5655aab0
PX
1686/*
1687 * This function will kick off an async task to establish the preempt
1688 * channel, and wait until the connection setup completed. Returns 0 if
1689 * channel established, -1 for error.
1690 */
1691int postcopy_preempt_establish_channel(MigrationState *s)
d0edb8a1
PX
1692{
1693 /* If preempt not enabled, no need to wait */
1694 if (!migrate_postcopy_preempt()) {
1695 return 0;
1696 }
1697
06064a67
PX
1698 /*
1699 * Kick off async task to establish preempt channel. Only do so with
1700 * 8.0+ machines, because 7.1/7.2 require the channel to be created in
1701 * setup phase of migration (even if racy in an unreliable network).
1702 */
1703 if (!s->preempt_pre_7_2) {
1704 postcopy_preempt_setup(s);
1705 }
5655aab0 1706
d0edb8a1
PX
1707 /*
1708 * We need the postcopy preempt channel to be established before
1709 * starting doing anything.
1710 */
1711 qemu_sem_wait(&s->postcopy_qemufile_src_sem);
1712
1713 return s->postcopy_qemufile_src ? 0 : -1;
1714}
1715
fc063a7b 1716void postcopy_preempt_setup(MigrationState *s)
d0edb8a1 1717{
d0edb8a1
PX
1718 /* Kick an async task to connect */
1719 socket_send_channel_create(postcopy_preempt_send_channel_new, s);
36f62f11
PX
1720}
1721
60bb3c58
PX
1722static void postcopy_pause_ram_fast_load(MigrationIncomingState *mis)
1723{
1724 trace_postcopy_pause_fast_load();
1725 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
1726 qemu_sem_wait(&mis->postcopy_pause_sem_fast_load);
1727 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
1728 trace_postcopy_pause_fast_load_continued();
1729}
1730
6621883f
PX
1731static bool preempt_thread_should_run(MigrationIncomingState *mis)
1732{
1733 return mis->preempt_thread_status != PREEMPT_THREAD_QUIT;
1734}
1735
36f62f11
PX
1736void *postcopy_preempt_thread(void *opaque)
1737{
1738 MigrationIncomingState *mis = opaque;
1739 int ret;
1740
1741 trace_postcopy_preempt_thread_entry();
1742
1743 rcu_register_thread();
1744
1745 qemu_sem_post(&mis->thread_sync_sem);
1746
a5d35dc7
PX
1747 /*
1748 * The preempt channel is established in asynchronous way. Wait
1749 * for its completion.
1750 */
1751 qemu_sem_wait(&mis->postcopy_qemufile_dst_done);
1752
36f62f11 1753 /* Sending RAM_SAVE_FLAG_EOS to terminate this thread */
60bb3c58 1754 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
6621883f 1755 while (preempt_thread_should_run(mis)) {
60bb3c58
PX
1756 ret = ram_load_postcopy(mis->postcopy_qemufile_dst,
1757 RAM_CHANNEL_POSTCOPY);
1758 /* If error happened, go into recovery routine */
6621883f 1759 if (ret && preempt_thread_should_run(mis)) {
60bb3c58
PX
1760 postcopy_pause_ram_fast_load(mis);
1761 } else {
1762 /* We're done */
1763 break;
1764 }
1765 }
1766 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
36f62f11
PX
1767
1768 rcu_unregister_thread();
1769
1770 trace_postcopy_preempt_thread_exit();
1771
60bb3c58 1772 return NULL;
36f62f11 1773}