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