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