2 * Postcopy migration for RAM
4 * Copyright 2013-2015 Red Hat, Inc. and/or its affiliates
7 * Dave Gilbert <dgilbert@redhat.com>
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.
15 * Postcopy is a migration technique where the execution flips from the
16 * source to the destination before all the data has been copied.
19 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "migration/migration.h"
23 #include "migration/postcopy-ram.h"
24 #include "sysemu/sysemu.h"
25 #include "sysemu/balloon.h"
26 #include "qemu/error-report.h"
29 /* Arbitrary limit on size of each discard command,
30 * keeps them around ~200 bytes
32 #define MAX_DISCARDS_PER_COMMAND 12
34 struct PostcopyDiscardState
{
35 const char *ramblock_name
;
36 uint64_t offset
; /* Bitmap entry for the 1st bit of this RAMBlock */
39 * Start and length of a discard range (bytes)
41 uint64_t start_list
[MAX_DISCARDS_PER_COMMAND
];
42 uint64_t length_list
[MAX_DISCARDS_PER_COMMAND
];
43 unsigned int nsentwords
;
44 unsigned int nsentcmds
;
47 /* Postcopy needs to detect accesses to pages that haven't yet been copied
48 * across, and efficiently map new pages in, the techniques for doing this
49 * are target OS specific.
51 #if defined(__linux__)
54 #include <sys/ioctl.h>
55 #include <sys/syscall.h>
56 #include <asm/types.h> /* for __u64 */
59 #if defined(__linux__) && defined(__NR_userfaultfd) && defined(CONFIG_EVENTFD)
60 #include <sys/eventfd.h>
61 #include <linux/userfaultfd.h>
63 static bool ufd_version_check(int ufd
)
65 struct uffdio_api api_struct
;
68 api_struct
.api
= UFFD_API
;
69 api_struct
.features
= 0;
70 if (ioctl(ufd
, UFFDIO_API
, &api_struct
)) {
71 error_report("postcopy_ram_supported_by_host: UFFDIO_API failed: %s",
76 ioctl_mask
= (__u64
)1 << _UFFDIO_REGISTER
|
77 (__u64
)1 << _UFFDIO_UNREGISTER
;
78 if ((api_struct
.ioctls
& ioctl_mask
) != ioctl_mask
) {
79 error_report("Missing userfault features: %" PRIx64
,
80 (uint64_t)(~api_struct
.ioctls
& ioctl_mask
));
88 * Check for things that postcopy won't support; returns 0 if the block
91 static int check_range(const char *block_name
, void *host_addr
,
92 ram_addr_t offset
, ram_addr_t length
, void *opaque
)
94 RAMBlock
*rb
= qemu_ram_block_by_name(block_name
);
96 if (qemu_ram_pagesize(rb
) > getpagesize()) {
97 error_report("Postcopy doesn't support large page sizes yet (%s)",
106 * Note: This has the side effect of munlock'ing all of RAM, that's
107 * normally fine since if the postcopy succeeds it gets turned back on at the
110 bool postcopy_ram_supported_by_host(void)
112 long pagesize
= getpagesize();
114 bool ret
= false; /* Error unless we change it */
115 void *testarea
= NULL
;
116 struct uffdio_register reg_struct
;
117 struct uffdio_range range_struct
;
118 uint64_t feature_mask
;
120 if ((1ul << qemu_target_page_bits()) > pagesize
) {
121 error_report("Target page size bigger than host page size");
125 /* Check for anything about the RAMBlocks we don't support */
126 if (qemu_ram_foreach_block(check_range
, NULL
)) {
127 /* check_range will have printed its own error */
131 ufd
= syscall(__NR_userfaultfd
, O_CLOEXEC
);
133 error_report("%s: userfaultfd not available: %s", __func__
,
138 /* Version and features check */
139 if (!ufd_version_check(ufd
)) {
144 * userfault and mlock don't go together; we'll put it back later if
148 error_report("%s: munlockall: %s", __func__
, strerror(errno
));
153 * We need to check that the ops we need are supported on anon memory
154 * To do that we need to register a chunk and see the flags that
157 testarea
= mmap(NULL
, pagesize
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
|
158 MAP_ANONYMOUS
, -1, 0);
159 if (testarea
== MAP_FAILED
) {
160 error_report("%s: Failed to map test area: %s", __func__
,
164 g_assert(((size_t)testarea
& (pagesize
-1)) == 0);
166 reg_struct
.range
.start
= (uintptr_t)testarea
;
167 reg_struct
.range
.len
= pagesize
;
168 reg_struct
.mode
= UFFDIO_REGISTER_MODE_MISSING
;
170 if (ioctl(ufd
, UFFDIO_REGISTER
, ®_struct
)) {
171 error_report("%s userfault register: %s", __func__
, strerror(errno
));
175 range_struct
.start
= (uintptr_t)testarea
;
176 range_struct
.len
= pagesize
;
177 if (ioctl(ufd
, UFFDIO_UNREGISTER
, &range_struct
)) {
178 error_report("%s userfault unregister: %s", __func__
, strerror(errno
));
182 feature_mask
= (__u64
)1 << _UFFDIO_WAKE
|
183 (__u64
)1 << _UFFDIO_COPY
|
184 (__u64
)1 << _UFFDIO_ZEROPAGE
;
185 if ((reg_struct
.ioctls
& feature_mask
) != feature_mask
) {
186 error_report("Missing userfault map features: %" PRIx64
,
187 (uint64_t)(~reg_struct
.ioctls
& feature_mask
));
195 munmap(testarea
, pagesize
);
204 * postcopy_ram_discard_range: Discard a range of memory.
205 * We can assume that if we've been called postcopy_ram_hosttest returned true.
207 * @mis: Current incoming migration state.
208 * @start, @length: range of memory to discard.
210 * returns: 0 on success.
212 int postcopy_ram_discard_range(MigrationIncomingState
*mis
, uint8_t *start
,
215 trace_postcopy_ram_discard_range(start
, length
);
216 if (madvise(start
, length
, MADV_DONTNEED
)) {
217 error_report("%s MADV_DONTNEED: %s", __func__
, strerror(errno
));
225 * Setup an area of RAM so that it *can* be used for postcopy later; this
226 * must be done right at the start prior to pre-copy.
227 * opaque should be the MIS.
229 static int init_range(const char *block_name
, void *host_addr
,
230 ram_addr_t offset
, ram_addr_t length
, void *opaque
)
232 MigrationIncomingState
*mis
= opaque
;
234 trace_postcopy_init_range(block_name
, host_addr
, offset
, length
);
237 * We need the whole of RAM to be truly empty for postcopy, so things
238 * like ROMs and any data tables built during init must be zero'd
239 * - we're going to get the copy from the source anyway.
240 * (Precopy will just overwrite this data, so doesn't need the discard)
242 if (postcopy_ram_discard_range(mis
, host_addr
, length
)) {
250 * At the end of migration, undo the effects of init_range
251 * opaque should be the MIS.
253 static int cleanup_range(const char *block_name
, void *host_addr
,
254 ram_addr_t offset
, ram_addr_t length
, void *opaque
)
256 MigrationIncomingState
*mis
= opaque
;
257 struct uffdio_range range_struct
;
258 trace_postcopy_cleanup_range(block_name
, host_addr
, offset
, length
);
261 * We turned off hugepage for the precopy stage with postcopy enabled
262 * we can turn it back on now.
264 qemu_madvise(host_addr
, length
, QEMU_MADV_HUGEPAGE
);
267 * We can also turn off userfault now since we should have all the
268 * pages. It can be useful to leave it on to debug postcopy
269 * if you're not sure it's always getting every page.
271 range_struct
.start
= (uintptr_t)host_addr
;
272 range_struct
.len
= length
;
274 if (ioctl(mis
->userfault_fd
, UFFDIO_UNREGISTER
, &range_struct
)) {
275 error_report("%s: userfault unregister %s", __func__
, strerror(errno
));
284 * Initialise postcopy-ram, setting the RAM to a state where we can go into
285 * postcopy later; must be called prior to any precopy.
286 * called from arch_init's similarly named ram_postcopy_incoming_init
288 int postcopy_ram_incoming_init(MigrationIncomingState
*mis
, size_t ram_pages
)
290 if (qemu_ram_foreach_block(init_range
, mis
)) {
298 * At the end of a migration where postcopy_ram_incoming_init was called.
300 int postcopy_ram_incoming_cleanup(MigrationIncomingState
*mis
)
302 trace_postcopy_ram_incoming_cleanup_entry();
304 if (mis
->have_fault_thread
) {
307 if (qemu_ram_foreach_block(cleanup_range
, mis
)) {
311 * Tell the fault_thread to exit, it's an eventfd that should
312 * currently be at 0, we're going to increment it to 1
315 if (write(mis
->userfault_quit_fd
, &tmp64
, 8) == 8) {
316 trace_postcopy_ram_incoming_cleanup_join();
317 qemu_thread_join(&mis
->fault_thread
);
319 /* Not much we can do here, but may as well report it */
320 error_report("%s: incrementing userfault_quit_fd: %s", __func__
,
323 trace_postcopy_ram_incoming_cleanup_closeuf();
324 close(mis
->userfault_fd
);
325 close(mis
->userfault_quit_fd
);
326 mis
->have_fault_thread
= false;
329 qemu_balloon_inhibit(false);
332 if (os_mlock() < 0) {
333 error_report("mlock: %s", strerror(errno
));
335 * It doesn't feel right to fail at this point, we have a valid
341 postcopy_state_set(POSTCOPY_INCOMING_END
);
342 migrate_send_rp_shut(mis
, qemu_file_get_error(mis
->from_src_file
) != 0);
344 if (mis
->postcopy_tmp_page
) {
345 munmap(mis
->postcopy_tmp_page
, getpagesize());
346 mis
->postcopy_tmp_page
= NULL
;
348 trace_postcopy_ram_incoming_cleanup_exit();
353 * Disable huge pages on an area
355 static int nhp_range(const char *block_name
, void *host_addr
,
356 ram_addr_t offset
, ram_addr_t length
, void *opaque
)
358 trace_postcopy_nhp_range(block_name
, host_addr
, offset
, length
);
361 * Before we do discards we need to ensure those discards really
362 * do delete areas of the page, even if THP thinks a hugepage would
363 * be a good idea, so force hugepages off.
365 qemu_madvise(host_addr
, length
, QEMU_MADV_NOHUGEPAGE
);
371 * Userfault requires us to mark RAM as NOHUGEPAGE prior to discard
372 * however leaving it until after precopy means that most of the precopy
375 int postcopy_ram_prepare_discard(MigrationIncomingState
*mis
)
377 if (qemu_ram_foreach_block(nhp_range
, mis
)) {
381 postcopy_state_set(POSTCOPY_INCOMING_DISCARD
);
387 * Mark the given area of RAM as requiring notification to unwritten areas
388 * Used as a callback on qemu_ram_foreach_block.
389 * host_addr: Base of area to mark
390 * offset: Offset in the whole ram arena
391 * length: Length of the section
392 * opaque: MigrationIncomingState pointer
393 * Returns 0 on success
395 static int ram_block_enable_notify(const char *block_name
, void *host_addr
,
396 ram_addr_t offset
, ram_addr_t length
,
399 MigrationIncomingState
*mis
= opaque
;
400 struct uffdio_register reg_struct
;
402 reg_struct
.range
.start
= (uintptr_t)host_addr
;
403 reg_struct
.range
.len
= length
;
404 reg_struct
.mode
= UFFDIO_REGISTER_MODE_MISSING
;
406 /* Now tell our userfault_fd that it's responsible for this area */
407 if (ioctl(mis
->userfault_fd
, UFFDIO_REGISTER
, ®_struct
)) {
408 error_report("%s userfault register: %s", __func__
, strerror(errno
));
416 * Handle faults detected by the USERFAULT markings
418 static void *postcopy_ram_fault_thread(void *opaque
)
420 MigrationIncomingState
*mis
= opaque
;
423 size_t hostpagesize
= getpagesize();
425 RAMBlock
*last_rb
= NULL
; /* last RAMBlock we sent part of */
427 trace_postcopy_ram_fault_thread_entry();
428 qemu_sem_post(&mis
->fault_thread_sem
);
431 ram_addr_t rb_offset
;
432 struct pollfd pfd
[2];
435 * We're mainly waiting for the kernel to give us a faulting HVA,
436 * however we can be told to quit via userfault_quit_fd which is
439 pfd
[0].fd
= mis
->userfault_fd
;
440 pfd
[0].events
= POLLIN
;
442 pfd
[1].fd
= mis
->userfault_quit_fd
;
443 pfd
[1].events
= POLLIN
; /* Waiting for eventfd to go positive */
446 if (poll(pfd
, 2, -1 /* Wait forever */) == -1) {
447 error_report("%s: userfault poll: %s", __func__
, strerror(errno
));
451 if (pfd
[1].revents
) {
452 trace_postcopy_ram_fault_thread_quit();
456 ret
= read(mis
->userfault_fd
, &msg
, sizeof(msg
));
457 if (ret
!= sizeof(msg
)) {
458 if (errno
== EAGAIN
) {
460 * if a wake up happens on the other thread just after
461 * the poll, there is nothing to read.
466 error_report("%s: Failed to read full userfault message: %s",
467 __func__
, strerror(errno
));
470 error_report("%s: Read %d bytes from userfaultfd expected %zd",
471 __func__
, ret
, sizeof(msg
));
472 break; /* Lost alignment, don't know what we'd read next */
475 if (msg
.event
!= UFFD_EVENT_PAGEFAULT
) {
476 error_report("%s: Read unexpected event %ud from userfaultfd",
477 __func__
, msg
.event
);
478 continue; /* It's not a page fault, shouldn't happen */
481 rb
= qemu_ram_block_from_host(
482 (void *)(uintptr_t)msg
.arg
.pagefault
.address
,
485 error_report("postcopy_ram_fault_thread: Fault outside guest: %"
486 PRIx64
, (uint64_t)msg
.arg
.pagefault
.address
);
490 rb_offset
&= ~(hostpagesize
- 1);
491 trace_postcopy_ram_fault_thread_request(msg
.arg
.pagefault
.address
,
492 qemu_ram_get_idstr(rb
),
496 * Send the request to the source - we want to request one
497 * of our host page sizes (which is >= TPS)
501 migrate_send_rp_req_pages(mis
, qemu_ram_get_idstr(rb
),
502 rb_offset
, hostpagesize
);
504 /* Save some space */
505 migrate_send_rp_req_pages(mis
, NULL
,
506 rb_offset
, hostpagesize
);
509 trace_postcopy_ram_fault_thread_exit();
513 int postcopy_ram_enable_notify(MigrationIncomingState
*mis
)
515 /* Open the fd for the kernel to give us userfaults */
516 mis
->userfault_fd
= syscall(__NR_userfaultfd
, O_CLOEXEC
| O_NONBLOCK
);
517 if (mis
->userfault_fd
== -1) {
518 error_report("%s: Failed to open userfault fd: %s", __func__
,
524 * Although the host check already tested the API, we need to
525 * do the check again as an ABI handshake on the new fd.
527 if (!ufd_version_check(mis
->userfault_fd
)) {
531 /* Now an eventfd we use to tell the fault-thread to quit */
532 mis
->userfault_quit_fd
= eventfd(0, EFD_CLOEXEC
);
533 if (mis
->userfault_quit_fd
== -1) {
534 error_report("%s: Opening userfault_quit_fd: %s", __func__
,
536 close(mis
->userfault_fd
);
540 qemu_sem_init(&mis
->fault_thread_sem
, 0);
541 qemu_thread_create(&mis
->fault_thread
, "postcopy/fault",
542 postcopy_ram_fault_thread
, mis
, QEMU_THREAD_JOINABLE
);
543 qemu_sem_wait(&mis
->fault_thread_sem
);
544 qemu_sem_destroy(&mis
->fault_thread_sem
);
545 mis
->have_fault_thread
= true;
547 /* Mark so that we get notified of accesses to unwritten areas */
548 if (qemu_ram_foreach_block(ram_block_enable_notify
, mis
)) {
553 * Ballooning can mark pages as absent while we're postcopying
554 * that would cause false userfaults.
556 qemu_balloon_inhibit(true);
558 trace_postcopy_ram_enable_notify();
564 * Place a host page (from) at (host) atomically
565 * returns 0 on success
567 int postcopy_place_page(MigrationIncomingState
*mis
, void *host
, void *from
)
569 struct uffdio_copy copy_struct
;
571 copy_struct
.dst
= (uint64_t)(uintptr_t)host
;
572 copy_struct
.src
= (uint64_t)(uintptr_t)from
;
573 copy_struct
.len
= getpagesize();
574 copy_struct
.mode
= 0;
576 /* copy also acks to the kernel waking the stalled thread up
577 * TODO: We can inhibit that ack and only do it if it was requested
578 * which would be slightly cheaper, but we'd have to be careful
579 * of the order of updating our page state.
581 if (ioctl(mis
->userfault_fd
, UFFDIO_COPY
, ©_struct
)) {
583 error_report("%s: %s copy host: %p from: %p",
584 __func__
, strerror(e
), host
, from
);
589 trace_postcopy_place_page(host
);
594 * Place a zero page at (host) atomically
595 * returns 0 on success
597 int postcopy_place_page_zero(MigrationIncomingState
*mis
, void *host
)
599 struct uffdio_zeropage zero_struct
;
601 zero_struct
.range
.start
= (uint64_t)(uintptr_t)host
;
602 zero_struct
.range
.len
= getpagesize();
603 zero_struct
.mode
= 0;
605 if (ioctl(mis
->userfault_fd
, UFFDIO_ZEROPAGE
, &zero_struct
)) {
607 error_report("%s: %s zero host: %p",
608 __func__
, strerror(e
), host
);
613 trace_postcopy_place_page_zero(host
);
618 * Returns a target page of memory that can be mapped at a later point in time
619 * using postcopy_place_page
620 * The same address is used repeatedly, postcopy_place_page just takes the
622 * Returns: Pointer to allocated page
625 void *postcopy_get_tmp_page(MigrationIncomingState
*mis
)
627 if (!mis
->postcopy_tmp_page
) {
628 mis
->postcopy_tmp_page
= mmap(NULL
, getpagesize(),
629 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
|
630 MAP_ANONYMOUS
, -1, 0);
631 if (mis
->postcopy_tmp_page
== MAP_FAILED
) {
632 mis
->postcopy_tmp_page
= NULL
;
633 error_report("%s: %s", __func__
, strerror(errno
));
638 return mis
->postcopy_tmp_page
;
642 /* No target OS support, stubs just fail */
643 bool postcopy_ram_supported_by_host(void)
645 error_report("%s: No OS support", __func__
);
649 int postcopy_ram_incoming_init(MigrationIncomingState
*mis
, size_t ram_pages
)
651 error_report("postcopy_ram_incoming_init: No OS support");
655 int postcopy_ram_incoming_cleanup(MigrationIncomingState
*mis
)
661 int postcopy_ram_discard_range(MigrationIncomingState
*mis
, uint8_t *start
,
668 int postcopy_ram_prepare_discard(MigrationIncomingState
*mis
)
674 int postcopy_ram_enable_notify(MigrationIncomingState
*mis
)
680 int postcopy_place_page(MigrationIncomingState
*mis
, void *host
, void *from
)
686 int postcopy_place_page_zero(MigrationIncomingState
*mis
, void *host
)
692 void *postcopy_get_tmp_page(MigrationIncomingState
*mis
)
700 /* ------------------------------------------------------------------------- */
703 * postcopy_discard_send_init: Called at the start of each RAMBlock before
704 * asking to discard individual ranges.
706 * @ms: The current migration state.
707 * @offset: the bitmap offset of the named RAMBlock in the migration
709 * @name: RAMBlock that discards will operate on.
711 * returns: a new PDS.
713 PostcopyDiscardState
*postcopy_discard_send_init(MigrationState
*ms
,
714 unsigned long offset
,
717 PostcopyDiscardState
*res
= g_malloc0(sizeof(PostcopyDiscardState
));
720 res
->ramblock_name
= name
;
721 res
->offset
= offset
;
728 * postcopy_discard_send_range: Called by the bitmap code for each chunk to
729 * discard. May send a discard message, may just leave it queued to
732 * @ms: Current migration state.
733 * @pds: Structure initialised by postcopy_discard_send_init().
734 * @start,@length: a range of pages in the migration bitmap in the
735 * RAM block passed to postcopy_discard_send_init() (length=1 is one page)
737 void postcopy_discard_send_range(MigrationState
*ms
, PostcopyDiscardState
*pds
,
738 unsigned long start
, unsigned long length
)
740 size_t tp_bits
= qemu_target_page_bits();
741 /* Convert to byte offsets within the RAM block */
742 pds
->start_list
[pds
->cur_entry
] = (start
- pds
->offset
) << tp_bits
;
743 pds
->length_list
[pds
->cur_entry
] = length
<< tp_bits
;
744 trace_postcopy_discard_send_range(pds
->ramblock_name
, start
, length
);
748 if (pds
->cur_entry
== MAX_DISCARDS_PER_COMMAND
) {
749 /* Full set, ship it! */
750 qemu_savevm_send_postcopy_ram_discard(ms
->to_dst_file
,
761 * postcopy_discard_send_finish: Called at the end of each RAMBlock by the
762 * bitmap code. Sends any outstanding discard messages, frees the PDS
764 * @ms: Current migration state.
765 * @pds: Structure initialised by postcopy_discard_send_init().
767 void postcopy_discard_send_finish(MigrationState
*ms
, PostcopyDiscardState
*pds
)
769 /* Anything unsent? */
770 if (pds
->cur_entry
) {
771 qemu_savevm_send_postcopy_ram_discard(ms
->to_dst_file
,
779 trace_postcopy_discard_send_finish(pds
->ramblock_name
, pds
->nsentwords
,