4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2011-2015 Red Hat Inc
8 * Juan Quintela <quintela@redhat.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 #include "qemu/osdep.h"
30 #include "qemu/cutils.h"
31 #include "qemu/bitops.h"
32 #include "qemu/bitmap.h"
33 #include "qemu/main-loop.h"
36 #include "migration.h"
37 #include "migration/register.h"
38 #include "migration/misc.h"
39 #include "qemu-file.h"
40 #include "postcopy-ram.h"
41 #include "page_cache.h"
42 #include "qemu/error-report.h"
43 #include "qapi/error.h"
44 #include "qapi/qapi-types-migration.h"
45 #include "qapi/qapi-events-migration.h"
46 #include "qapi/qmp/qerror.h"
48 #include "exec/ram_addr.h"
49 #include "exec/target_page.h"
50 #include "qemu/rcu_queue.h"
51 #include "migration/colo.h"
53 #include "sysemu/cpu-throttle.h"
57 #include "sysemu/runstate.h"
59 #if defined(__linux__)
60 #include "qemu/userfaultfd.h"
61 #endif /* defined(__linux__) */
63 /***********************************************************/
64 /* ram save/restore */
66 /* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it
67 * worked for pages that where filled with the same char. We switched
68 * it to only search for the zero value. And to avoid confusion with
69 * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it.
72 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
73 #define RAM_SAVE_FLAG_ZERO 0x02
74 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
75 #define RAM_SAVE_FLAG_PAGE 0x08
76 #define RAM_SAVE_FLAG_EOS 0x10
77 #define RAM_SAVE_FLAG_CONTINUE 0x20
78 #define RAM_SAVE_FLAG_XBZRLE 0x40
79 /* 0x80 is reserved in migration.h start with 0x100 next */
80 #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
82 static inline bool is_zero_range(uint8_t *p
, uint64_t size
)
84 return buffer_is_zero(p
, size
);
87 XBZRLECacheStats xbzrle_counters
;
89 /* struct contains XBZRLE cache and a static page
90 used by the compression */
92 /* buffer used for XBZRLE encoding */
94 /* buffer for storing page content */
96 /* Cache for XBZRLE, Protected by lock. */
99 /* it will store a page full of zeros */
100 uint8_t *zero_target_page
;
101 /* buffer used for XBZRLE decoding */
102 uint8_t *decoded_buf
;
105 static void XBZRLE_cache_lock(void)
107 if (migrate_use_xbzrle()) {
108 qemu_mutex_lock(&XBZRLE
.lock
);
112 static void XBZRLE_cache_unlock(void)
114 if (migrate_use_xbzrle()) {
115 qemu_mutex_unlock(&XBZRLE
.lock
);
120 * xbzrle_cache_resize: resize the xbzrle cache
122 * This function is called from migrate_params_apply in main
123 * thread, possibly while a migration is in progress. A running
124 * migration may be using the cache and might finish during this call,
125 * hence changes to the cache are protected by XBZRLE.lock().
127 * Returns 0 for success or -1 for error
129 * @new_size: new cache size
130 * @errp: set *errp if the check failed, with reason
132 int xbzrle_cache_resize(uint64_t new_size
, Error
**errp
)
134 PageCache
*new_cache
;
137 /* Check for truncation */
138 if (new_size
!= (size_t)new_size
) {
139 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cache size",
140 "exceeding address space");
144 if (new_size
== migrate_xbzrle_cache_size()) {
151 if (XBZRLE
.cache
!= NULL
) {
152 new_cache
= cache_init(new_size
, TARGET_PAGE_SIZE
, errp
);
158 cache_fini(XBZRLE
.cache
);
159 XBZRLE
.cache
= new_cache
;
162 XBZRLE_cache_unlock();
166 bool ramblock_is_ignored(RAMBlock
*block
)
168 return !qemu_ram_is_migratable(block
) ||
169 (migrate_ignore_shared() && qemu_ram_is_shared(block
));
172 #undef RAMBLOCK_FOREACH
174 int foreach_not_ignored_block(RAMBlockIterFunc func
, void *opaque
)
179 RCU_READ_LOCK_GUARD();
181 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
182 ret
= func(block
, opaque
);
190 static void ramblock_recv_map_init(void)
194 RAMBLOCK_FOREACH_NOT_IGNORED(rb
) {
195 assert(!rb
->receivedmap
);
196 rb
->receivedmap
= bitmap_new(rb
->max_length
>> qemu_target_page_bits());
200 int ramblock_recv_bitmap_test(RAMBlock
*rb
, void *host_addr
)
202 return test_bit(ramblock_recv_bitmap_offset(host_addr
, rb
),
206 bool ramblock_recv_bitmap_test_byte_offset(RAMBlock
*rb
, uint64_t byte_offset
)
208 return test_bit(byte_offset
>> TARGET_PAGE_BITS
, rb
->receivedmap
);
211 void ramblock_recv_bitmap_set(RAMBlock
*rb
, void *host_addr
)
213 set_bit_atomic(ramblock_recv_bitmap_offset(host_addr
, rb
), rb
->receivedmap
);
216 void ramblock_recv_bitmap_set_range(RAMBlock
*rb
, void *host_addr
,
219 bitmap_set_atomic(rb
->receivedmap
,
220 ramblock_recv_bitmap_offset(host_addr
, rb
),
224 #define RAMBLOCK_RECV_BITMAP_ENDING (0x0123456789abcdefULL)
227 * Format: bitmap_size (8 bytes) + whole_bitmap (N bytes).
229 * Returns >0 if success with sent bytes, or <0 if error.
231 int64_t ramblock_recv_bitmap_send(QEMUFile
*file
,
232 const char *block_name
)
234 RAMBlock
*block
= qemu_ram_block_by_name(block_name
);
235 unsigned long *le_bitmap
, nbits
;
239 error_report("%s: invalid block name: %s", __func__
, block_name
);
243 nbits
= block
->used_length
>> TARGET_PAGE_BITS
;
246 * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit
247 * machines we may need 4 more bytes for padding (see below
248 * comment). So extend it a bit before hand.
250 le_bitmap
= bitmap_new(nbits
+ BITS_PER_LONG
);
253 * Always use little endian when sending the bitmap. This is
254 * required that when source and destination VMs are not using the
255 * same endianness. (Note: big endian won't work.)
257 bitmap_to_le(le_bitmap
, block
->receivedmap
, nbits
);
259 /* Size of the bitmap, in bytes */
260 size
= DIV_ROUND_UP(nbits
, 8);
263 * size is always aligned to 8 bytes for 64bit machines, but it
264 * may not be true for 32bit machines. We need this padding to
265 * make sure the migration can survive even between 32bit and
268 size
= ROUND_UP(size
, 8);
270 qemu_put_be64(file
, size
);
271 qemu_put_buffer(file
, (const uint8_t *)le_bitmap
, size
);
273 * Mark as an end, in case the middle part is screwed up due to
274 * some "mysterious" reason.
276 qemu_put_be64(file
, RAMBLOCK_RECV_BITMAP_ENDING
);
281 if (qemu_file_get_error(file
)) {
282 return qemu_file_get_error(file
);
285 return size
+ sizeof(size
);
289 * An outstanding page request, on the source, having been received
292 struct RAMSrcPageRequest
{
297 QSIMPLEQ_ENTRY(RAMSrcPageRequest
) next_req
;
300 /* State of RAM for migration */
302 /* QEMUFile used for this migration */
304 /* UFFD file descriptor, used in 'write-tracking' migration */
306 /* Last block that we have visited searching for dirty pages */
307 RAMBlock
*last_seen_block
;
308 /* Last block from where we have sent data */
309 RAMBlock
*last_sent_block
;
310 /* Last dirty target page we have sent */
311 ram_addr_t last_page
;
312 /* last ram version we have seen */
313 uint32_t last_version
;
314 /* We are in the first round */
316 /* The free page optimization is enabled */
318 /* How many times we have dirty too many pages */
319 int dirty_rate_high_cnt
;
320 /* these variables are used for bitmap sync */
321 /* last time we did a full bitmap_sync */
322 int64_t time_last_bitmap_sync
;
323 /* bytes transferred at start_time */
324 uint64_t bytes_xfer_prev
;
325 /* number of dirty pages since start_time */
326 uint64_t num_dirty_pages_period
;
327 /* xbzrle misses since the beginning of the period */
328 uint64_t xbzrle_cache_miss_prev
;
329 /* Amount of xbzrle pages since the beginning of the period */
330 uint64_t xbzrle_pages_prev
;
331 /* Amount of xbzrle encoded bytes since the beginning of the period */
332 uint64_t xbzrle_bytes_prev
;
334 /* compression statistics since the beginning of the period */
335 /* amount of count that no free thread to compress data */
336 uint64_t compress_thread_busy_prev
;
337 /* amount bytes after compression */
338 uint64_t compressed_size_prev
;
339 /* amount of compressed pages */
340 uint64_t compress_pages_prev
;
342 /* total handled target pages at the beginning of period */
343 uint64_t target_page_count_prev
;
344 /* total handled target pages since start */
345 uint64_t target_page_count
;
346 /* number of dirty bits in the bitmap */
347 uint64_t migration_dirty_pages
;
348 /* Protects modification of the bitmap and migration dirty pages */
349 QemuMutex bitmap_mutex
;
350 /* The RAMBlock used in the last src_page_requests */
351 RAMBlock
*last_req_rb
;
352 /* Queue of outstanding page requests from the destination */
353 QemuMutex src_page_req_mutex
;
354 QSIMPLEQ_HEAD(, RAMSrcPageRequest
) src_page_requests
;
356 typedef struct RAMState RAMState
;
358 static RAMState
*ram_state
;
360 static NotifierWithReturnList precopy_notifier_list
;
362 void precopy_infrastructure_init(void)
364 notifier_with_return_list_init(&precopy_notifier_list
);
367 void precopy_add_notifier(NotifierWithReturn
*n
)
369 notifier_with_return_list_add(&precopy_notifier_list
, n
);
372 void precopy_remove_notifier(NotifierWithReturn
*n
)
374 notifier_with_return_remove(n
);
377 int precopy_notify(PrecopyNotifyReason reason
, Error
**errp
)
379 PrecopyNotifyData pnd
;
383 return notifier_with_return_list_notify(&precopy_notifier_list
, &pnd
);
386 void precopy_enable_free_page_optimization(void)
392 ram_state
->fpo_enabled
= true;
395 uint64_t ram_bytes_remaining(void)
397 return ram_state
? (ram_state
->migration_dirty_pages
* TARGET_PAGE_SIZE
) :
401 MigrationStats ram_counters
;
403 /* used by the search for pages to send */
404 struct PageSearchStatus
{
405 /* Current block being searched */
407 /* Current page to search from */
409 /* Set once we wrap around */
412 typedef struct PageSearchStatus PageSearchStatus
;
414 CompressionStats compression_counters
;
416 struct CompressParam
{
426 /* internally used fields */
430 typedef struct CompressParam CompressParam
;
432 struct DecompressParam
{
442 typedef struct DecompressParam DecompressParam
;
444 static CompressParam
*comp_param
;
445 static QemuThread
*compress_threads
;
446 /* comp_done_cond is used to wake up the migration thread when
447 * one of the compression threads has finished the compression.
448 * comp_done_lock is used to co-work with comp_done_cond.
450 static QemuMutex comp_done_lock
;
451 static QemuCond comp_done_cond
;
452 /* The empty QEMUFileOps will be used by file in CompressParam */
453 static const QEMUFileOps empty_ops
= { };
455 static QEMUFile
*decomp_file
;
456 static DecompressParam
*decomp_param
;
457 static QemuThread
*decompress_threads
;
458 static QemuMutex decomp_done_lock
;
459 static QemuCond decomp_done_cond
;
461 static bool do_compress_ram_page(QEMUFile
*f
, z_stream
*stream
, RAMBlock
*block
,
462 ram_addr_t offset
, uint8_t *source_buf
);
464 static void *do_data_compress(void *opaque
)
466 CompressParam
*param
= opaque
;
471 qemu_mutex_lock(¶m
->mutex
);
472 while (!param
->quit
) {
474 block
= param
->block
;
475 offset
= param
->offset
;
477 qemu_mutex_unlock(¶m
->mutex
);
479 zero_page
= do_compress_ram_page(param
->file
, ¶m
->stream
,
480 block
, offset
, param
->originbuf
);
482 qemu_mutex_lock(&comp_done_lock
);
484 param
->zero_page
= zero_page
;
485 qemu_cond_signal(&comp_done_cond
);
486 qemu_mutex_unlock(&comp_done_lock
);
488 qemu_mutex_lock(¶m
->mutex
);
490 qemu_cond_wait(¶m
->cond
, ¶m
->mutex
);
493 qemu_mutex_unlock(¶m
->mutex
);
498 static void compress_threads_save_cleanup(void)
502 if (!migrate_use_compression() || !comp_param
) {
506 thread_count
= migrate_compress_threads();
507 for (i
= 0; i
< thread_count
; i
++) {
509 * we use it as a indicator which shows if the thread is
510 * properly init'd or not
512 if (!comp_param
[i
].file
) {
516 qemu_mutex_lock(&comp_param
[i
].mutex
);
517 comp_param
[i
].quit
= true;
518 qemu_cond_signal(&comp_param
[i
].cond
);
519 qemu_mutex_unlock(&comp_param
[i
].mutex
);
521 qemu_thread_join(compress_threads
+ i
);
522 qemu_mutex_destroy(&comp_param
[i
].mutex
);
523 qemu_cond_destroy(&comp_param
[i
].cond
);
524 deflateEnd(&comp_param
[i
].stream
);
525 g_free(comp_param
[i
].originbuf
);
526 qemu_fclose(comp_param
[i
].file
);
527 comp_param
[i
].file
= NULL
;
529 qemu_mutex_destroy(&comp_done_lock
);
530 qemu_cond_destroy(&comp_done_cond
);
531 g_free(compress_threads
);
533 compress_threads
= NULL
;
537 static int compress_threads_save_setup(void)
541 if (!migrate_use_compression()) {
544 thread_count
= migrate_compress_threads();
545 compress_threads
= g_new0(QemuThread
, thread_count
);
546 comp_param
= g_new0(CompressParam
, thread_count
);
547 qemu_cond_init(&comp_done_cond
);
548 qemu_mutex_init(&comp_done_lock
);
549 for (i
= 0; i
< thread_count
; i
++) {
550 comp_param
[i
].originbuf
= g_try_malloc(TARGET_PAGE_SIZE
);
551 if (!comp_param
[i
].originbuf
) {
555 if (deflateInit(&comp_param
[i
].stream
,
556 migrate_compress_level()) != Z_OK
) {
557 g_free(comp_param
[i
].originbuf
);
561 /* comp_param[i].file is just used as a dummy buffer to save data,
562 * set its ops to empty.
564 comp_param
[i
].file
= qemu_fopen_ops(NULL
, &empty_ops
);
565 comp_param
[i
].done
= true;
566 comp_param
[i
].quit
= false;
567 qemu_mutex_init(&comp_param
[i
].mutex
);
568 qemu_cond_init(&comp_param
[i
].cond
);
569 qemu_thread_create(compress_threads
+ i
, "compress",
570 do_data_compress
, comp_param
+ i
,
571 QEMU_THREAD_JOINABLE
);
576 compress_threads_save_cleanup();
581 * save_page_header: write page header to wire
583 * If this is the 1st block, it also writes the block identification
585 * Returns the number of bytes written
587 * @f: QEMUFile where to send the data
588 * @block: block that contains the page we want to send
589 * @offset: offset inside the block for the page
590 * in the lower bits, it contains flags
592 static size_t save_page_header(RAMState
*rs
, QEMUFile
*f
, RAMBlock
*block
,
597 if (block
== rs
->last_sent_block
) {
598 offset
|= RAM_SAVE_FLAG_CONTINUE
;
600 qemu_put_be64(f
, offset
);
603 if (!(offset
& RAM_SAVE_FLAG_CONTINUE
)) {
604 len
= strlen(block
->idstr
);
605 qemu_put_byte(f
, len
);
606 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, len
);
608 rs
->last_sent_block
= block
;
614 * mig_throttle_guest_down: throotle down the guest
616 * Reduce amount of guest cpu execution to hopefully slow down memory
617 * writes. If guest dirty memory rate is reduced below the rate at
618 * which we can transfer pages to the destination then we should be
619 * able to complete migration. Some workloads dirty memory way too
620 * fast and will not effectively converge, even with auto-converge.
622 static void mig_throttle_guest_down(uint64_t bytes_dirty_period
,
623 uint64_t bytes_dirty_threshold
)
625 MigrationState
*s
= migrate_get_current();
626 uint64_t pct_initial
= s
->parameters
.cpu_throttle_initial
;
627 uint64_t pct_increment
= s
->parameters
.cpu_throttle_increment
;
628 bool pct_tailslow
= s
->parameters
.cpu_throttle_tailslow
;
629 int pct_max
= s
->parameters
.max_cpu_throttle
;
631 uint64_t throttle_now
= cpu_throttle_get_percentage();
632 uint64_t cpu_now
, cpu_ideal
, throttle_inc
;
634 /* We have not started throttling yet. Let's start it. */
635 if (!cpu_throttle_active()) {
636 cpu_throttle_set(pct_initial
);
638 /* Throttling already on, just increase the rate */
640 throttle_inc
= pct_increment
;
642 /* Compute the ideal CPU percentage used by Guest, which may
643 * make the dirty rate match the dirty rate threshold. */
644 cpu_now
= 100 - throttle_now
;
645 cpu_ideal
= cpu_now
* (bytes_dirty_threshold
* 1.0 /
647 throttle_inc
= MIN(cpu_now
- cpu_ideal
, pct_increment
);
649 cpu_throttle_set(MIN(throttle_now
+ throttle_inc
, pct_max
));
654 * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache
656 * @rs: current RAM state
657 * @current_addr: address for the zero page
659 * Update the xbzrle cache to reflect a page that's been sent as all 0.
660 * The important thing is that a stale (not-yet-0'd) page be replaced
662 * As a bonus, if the page wasn't in the cache it gets added so that
663 * when a small write is made into the 0'd page it gets XBZRLE sent.
665 static void xbzrle_cache_zero_page(RAMState
*rs
, ram_addr_t current_addr
)
667 if (rs
->ram_bulk_stage
|| !migrate_use_xbzrle()) {
671 /* We don't care if this fails to allocate a new cache page
672 * as long as it updated an old one */
673 cache_insert(XBZRLE
.cache
, current_addr
, XBZRLE
.zero_target_page
,
674 ram_counters
.dirty_sync_count
);
677 #define ENCODING_FLAG_XBZRLE 0x1
680 * save_xbzrle_page: compress and send current page
682 * Returns: 1 means that we wrote the page
683 * 0 means that page is identical to the one already sent
684 * -1 means that xbzrle would be longer than normal
686 * @rs: current RAM state
687 * @current_data: pointer to the address of the page contents
688 * @current_addr: addr of the page
689 * @block: block that contains the page we want to send
690 * @offset: offset inside the block for the page
691 * @last_stage: if we are at the completion stage
693 static int save_xbzrle_page(RAMState
*rs
, uint8_t **current_data
,
694 ram_addr_t current_addr
, RAMBlock
*block
,
695 ram_addr_t offset
, bool last_stage
)
697 int encoded_len
= 0, bytes_xbzrle
;
698 uint8_t *prev_cached_page
;
700 if (!cache_is_cached(XBZRLE
.cache
, current_addr
,
701 ram_counters
.dirty_sync_count
)) {
702 xbzrle_counters
.cache_miss
++;
704 if (cache_insert(XBZRLE
.cache
, current_addr
, *current_data
,
705 ram_counters
.dirty_sync_count
) == -1) {
708 /* update *current_data when the page has been
709 inserted into cache */
710 *current_data
= get_cached_data(XBZRLE
.cache
, current_addr
);
717 * Reaching here means the page has hit the xbzrle cache, no matter what
718 * encoding result it is (normal encoding, overflow or skipping the page),
719 * count the page as encoded. This is used to calculate the encoding rate.
721 * Example: 2 pages (8KB) being encoded, first page encoding generates 2KB,
722 * 2nd page turns out to be skipped (i.e. no new bytes written to the
723 * page), the overall encoding rate will be 8KB / 2KB = 4, which has the
724 * skipped page included. In this way, the encoding rate can tell if the
725 * guest page is good for xbzrle encoding.
727 xbzrle_counters
.pages
++;
728 prev_cached_page
= get_cached_data(XBZRLE
.cache
, current_addr
);
730 /* save current buffer into memory */
731 memcpy(XBZRLE
.current_buf
, *current_data
, TARGET_PAGE_SIZE
);
733 /* XBZRLE encoding (if there is no overflow) */
734 encoded_len
= xbzrle_encode_buffer(prev_cached_page
, XBZRLE
.current_buf
,
735 TARGET_PAGE_SIZE
, XBZRLE
.encoded_buf
,
739 * Update the cache contents, so that it corresponds to the data
740 * sent, in all cases except where we skip the page.
742 if (!last_stage
&& encoded_len
!= 0) {
743 memcpy(prev_cached_page
, XBZRLE
.current_buf
, TARGET_PAGE_SIZE
);
745 * In the case where we couldn't compress, ensure that the caller
746 * sends the data from the cache, since the guest might have
747 * changed the RAM since we copied it.
749 *current_data
= prev_cached_page
;
752 if (encoded_len
== 0) {
753 trace_save_xbzrle_page_skipping();
755 } else if (encoded_len
== -1) {
756 trace_save_xbzrle_page_overflow();
757 xbzrle_counters
.overflow
++;
758 xbzrle_counters
.bytes
+= TARGET_PAGE_SIZE
;
762 /* Send XBZRLE based compressed page */
763 bytes_xbzrle
= save_page_header(rs
, rs
->f
, block
,
764 offset
| RAM_SAVE_FLAG_XBZRLE
);
765 qemu_put_byte(rs
->f
, ENCODING_FLAG_XBZRLE
);
766 qemu_put_be16(rs
->f
, encoded_len
);
767 qemu_put_buffer(rs
->f
, XBZRLE
.encoded_buf
, encoded_len
);
768 bytes_xbzrle
+= encoded_len
+ 1 + 2;
770 * Like compressed_size (please see update_compress_thread_counts),
771 * the xbzrle encoded bytes don't count the 8 byte header with
772 * RAM_SAVE_FLAG_CONTINUE.
774 xbzrle_counters
.bytes
+= bytes_xbzrle
- 8;
775 ram_counters
.transferred
+= bytes_xbzrle
;
781 * migration_bitmap_find_dirty: find the next dirty page from start
783 * Returns the page offset within memory region of the start of a dirty page
785 * @rs: current RAM state
786 * @rb: RAMBlock where to search for dirty pages
787 * @start: page where we start the search
790 unsigned long migration_bitmap_find_dirty(RAMState
*rs
, RAMBlock
*rb
,
793 unsigned long size
= rb
->used_length
>> TARGET_PAGE_BITS
;
794 unsigned long *bitmap
= rb
->bmap
;
797 if (ramblock_is_ignored(rb
)) {
802 * When the free page optimization is enabled, we need to check the bitmap
803 * to send the non-free pages rather than all the pages in the bulk stage.
805 if (!rs
->fpo_enabled
&& rs
->ram_bulk_stage
&& start
> 0) {
808 next
= find_next_bit(bitmap
, size
, start
);
814 static inline bool migration_bitmap_clear_dirty(RAMState
*rs
,
820 QEMU_LOCK_GUARD(&rs
->bitmap_mutex
);
823 * Clear dirty bitmap if needed. This _must_ be called before we
824 * send any of the page in the chunk because we need to make sure
825 * we can capture further page content changes when we sync dirty
826 * log the next time. So as long as we are going to send any of
827 * the page in the chunk we clear the remote dirty bitmap for all.
828 * Clearing it earlier won't be a problem, but too late will.
830 if (rb
->clear_bmap
&& clear_bmap_test_and_clear(rb
, page
)) {
831 uint8_t shift
= rb
->clear_bmap_shift
;
832 hwaddr size
= 1ULL << (TARGET_PAGE_BITS
+ shift
);
833 hwaddr start
= (((ram_addr_t
)page
) << TARGET_PAGE_BITS
) & (-size
);
836 * CLEAR_BITMAP_SHIFT_MIN should always guarantee this... this
837 * can make things easier sometimes since then start address
838 * of the small chunk will always be 64 pages aligned so the
839 * bitmap will always be aligned to unsigned long. We should
840 * even be able to remove this restriction but I'm simply
844 trace_migration_bitmap_clear_dirty(rb
->idstr
, start
, size
, page
);
845 memory_region_clear_dirty_bitmap(rb
->mr
, start
, size
);
848 ret
= test_and_clear_bit(page
, rb
->bmap
);
851 rs
->migration_dirty_pages
--;
857 /* Called with RCU critical section */
858 static void ramblock_sync_dirty_bitmap(RAMState
*rs
, RAMBlock
*rb
)
860 uint64_t new_dirty_pages
=
861 cpu_physical_memory_sync_dirty_bitmap(rb
, 0, rb
->used_length
);
863 rs
->migration_dirty_pages
+= new_dirty_pages
;
864 rs
->num_dirty_pages_period
+= new_dirty_pages
;
868 * ram_pagesize_summary: calculate all the pagesizes of a VM
870 * Returns a summary bitmap of the page sizes of all RAMBlocks
872 * For VMs with just normal pages this is equivalent to the host page
873 * size. If it's got some huge pages then it's the OR of all the
874 * different page sizes.
876 uint64_t ram_pagesize_summary(void)
879 uint64_t summary
= 0;
881 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
882 summary
|= block
->page_size
;
888 uint64_t ram_get_total_transferred_pages(void)
890 return ram_counters
.normal
+ ram_counters
.duplicate
+
891 compression_counters
.pages
+ xbzrle_counters
.pages
;
894 static void migration_update_rates(RAMState
*rs
, int64_t end_time
)
896 uint64_t page_count
= rs
->target_page_count
- rs
->target_page_count_prev
;
897 double compressed_size
;
899 /* calculate period counters */
900 ram_counters
.dirty_pages_rate
= rs
->num_dirty_pages_period
* 1000
901 / (end_time
- rs
->time_last_bitmap_sync
);
907 if (migrate_use_xbzrle()) {
908 double encoded_size
, unencoded_size
;
910 xbzrle_counters
.cache_miss_rate
= (double)(xbzrle_counters
.cache_miss
-
911 rs
->xbzrle_cache_miss_prev
) / page_count
;
912 rs
->xbzrle_cache_miss_prev
= xbzrle_counters
.cache_miss
;
913 unencoded_size
= (xbzrle_counters
.pages
- rs
->xbzrle_pages_prev
) *
915 encoded_size
= xbzrle_counters
.bytes
- rs
->xbzrle_bytes_prev
;
916 if (xbzrle_counters
.pages
== rs
->xbzrle_pages_prev
|| !encoded_size
) {
917 xbzrle_counters
.encoding_rate
= 0;
919 xbzrle_counters
.encoding_rate
= unencoded_size
/ encoded_size
;
921 rs
->xbzrle_pages_prev
= xbzrle_counters
.pages
;
922 rs
->xbzrle_bytes_prev
= xbzrle_counters
.bytes
;
925 if (migrate_use_compression()) {
926 compression_counters
.busy_rate
= (double)(compression_counters
.busy
-
927 rs
->compress_thread_busy_prev
) / page_count
;
928 rs
->compress_thread_busy_prev
= compression_counters
.busy
;
930 compressed_size
= compression_counters
.compressed_size
-
931 rs
->compressed_size_prev
;
932 if (compressed_size
) {
933 double uncompressed_size
= (compression_counters
.pages
-
934 rs
->compress_pages_prev
) * TARGET_PAGE_SIZE
;
936 /* Compression-Ratio = Uncompressed-size / Compressed-size */
937 compression_counters
.compression_rate
=
938 uncompressed_size
/ compressed_size
;
940 rs
->compress_pages_prev
= compression_counters
.pages
;
941 rs
->compressed_size_prev
= compression_counters
.compressed_size
;
946 static void migration_trigger_throttle(RAMState
*rs
)
948 MigrationState
*s
= migrate_get_current();
949 uint64_t threshold
= s
->parameters
.throttle_trigger_threshold
;
951 uint64_t bytes_xfer_period
= ram_counters
.transferred
- rs
->bytes_xfer_prev
;
952 uint64_t bytes_dirty_period
= rs
->num_dirty_pages_period
* TARGET_PAGE_SIZE
;
953 uint64_t bytes_dirty_threshold
= bytes_xfer_period
* threshold
/ 100;
955 /* During block migration the auto-converge logic incorrectly detects
956 * that ram migration makes no progress. Avoid this by disabling the
957 * throttling logic during the bulk phase of block migration. */
958 if (migrate_auto_converge() && !blk_mig_bulk_active()) {
959 /* The following detection logic can be refined later. For now:
960 Check to see if the ratio between dirtied bytes and the approx.
961 amount of bytes that just got transferred since the last time
962 we were in this routine reaches the threshold. If that happens
963 twice, start or increase throttling. */
965 if ((bytes_dirty_period
> bytes_dirty_threshold
) &&
966 (++rs
->dirty_rate_high_cnt
>= 2)) {
967 trace_migration_throttle();
968 rs
->dirty_rate_high_cnt
= 0;
969 mig_throttle_guest_down(bytes_dirty_period
,
970 bytes_dirty_threshold
);
975 static void migration_bitmap_sync(RAMState
*rs
)
980 ram_counters
.dirty_sync_count
++;
982 if (!rs
->time_last_bitmap_sync
) {
983 rs
->time_last_bitmap_sync
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
986 trace_migration_bitmap_sync_start();
987 memory_global_dirty_log_sync();
989 qemu_mutex_lock(&rs
->bitmap_mutex
);
990 WITH_RCU_READ_LOCK_GUARD() {
991 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
992 ramblock_sync_dirty_bitmap(rs
, block
);
994 ram_counters
.remaining
= ram_bytes_remaining();
996 qemu_mutex_unlock(&rs
->bitmap_mutex
);
998 memory_global_after_dirty_log_sync();
999 trace_migration_bitmap_sync_end(rs
->num_dirty_pages_period
);
1001 end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
1003 /* more than 1 second = 1000 millisecons */
1004 if (end_time
> rs
->time_last_bitmap_sync
+ 1000) {
1005 migration_trigger_throttle(rs
);
1007 migration_update_rates(rs
, end_time
);
1009 rs
->target_page_count_prev
= rs
->target_page_count
;
1011 /* reset period counters */
1012 rs
->time_last_bitmap_sync
= end_time
;
1013 rs
->num_dirty_pages_period
= 0;
1014 rs
->bytes_xfer_prev
= ram_counters
.transferred
;
1016 if (migrate_use_events()) {
1017 qapi_event_send_migration_pass(ram_counters
.dirty_sync_count
);
1021 static void migration_bitmap_sync_precopy(RAMState
*rs
)
1023 Error
*local_err
= NULL
;
1026 * The current notifier usage is just an optimization to migration, so we
1027 * don't stop the normal migration process in the error case.
1029 if (precopy_notify(PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC
, &local_err
)) {
1030 error_report_err(local_err
);
1034 migration_bitmap_sync(rs
);
1036 if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC
, &local_err
)) {
1037 error_report_err(local_err
);
1042 * save_zero_page_to_file: send the zero page to the file
1044 * Returns the size of data written to the file, 0 means the page is not
1047 * @rs: current RAM state
1048 * @file: the file where the data is saved
1049 * @block: block that contains the page we want to send
1050 * @offset: offset inside the block for the page
1052 static int save_zero_page_to_file(RAMState
*rs
, QEMUFile
*file
,
1053 RAMBlock
*block
, ram_addr_t offset
)
1055 uint8_t *p
= block
->host
+ offset
;
1058 if (is_zero_range(p
, TARGET_PAGE_SIZE
)) {
1059 len
+= save_page_header(rs
, file
, block
, offset
| RAM_SAVE_FLAG_ZERO
);
1060 qemu_put_byte(file
, 0);
1067 * save_zero_page: send the zero page to the stream
1069 * Returns the number of pages written.
1071 * @rs: current RAM state
1072 * @block: block that contains the page we want to send
1073 * @offset: offset inside the block for the page
1075 static int save_zero_page(RAMState
*rs
, RAMBlock
*block
, ram_addr_t offset
)
1077 int len
= save_zero_page_to_file(rs
, rs
->f
, block
, offset
);
1080 ram_counters
.duplicate
++;
1081 ram_counters
.transferred
+= len
;
1087 static void ram_release_pages(const char *rbname
, uint64_t offset
, int pages
)
1089 if (!migrate_release_ram() || !migration_in_postcopy()) {
1093 ram_discard_range(rbname
, offset
, ((ram_addr_t
)pages
) << TARGET_PAGE_BITS
);
1097 * @pages: the number of pages written by the control path,
1099 * > 0 - number of pages written
1101 * Return true if the pages has been saved, otherwise false is returned.
1103 static bool control_save_page(RAMState
*rs
, RAMBlock
*block
, ram_addr_t offset
,
1106 uint64_t bytes_xmit
= 0;
1110 ret
= ram_control_save_page(rs
->f
, block
->offset
, offset
, TARGET_PAGE_SIZE
,
1112 if (ret
== RAM_SAVE_CONTROL_NOT_SUPP
) {
1117 ram_counters
.transferred
+= bytes_xmit
;
1121 if (ret
== RAM_SAVE_CONTROL_DELAYED
) {
1125 if (bytes_xmit
> 0) {
1126 ram_counters
.normal
++;
1127 } else if (bytes_xmit
== 0) {
1128 ram_counters
.duplicate
++;
1135 * directly send the page to the stream
1137 * Returns the number of pages written.
1139 * @rs: current RAM state
1140 * @block: block that contains the page we want to send
1141 * @offset: offset inside the block for the page
1142 * @buf: the page to be sent
1143 * @async: send to page asyncly
1145 static int save_normal_page(RAMState
*rs
, RAMBlock
*block
, ram_addr_t offset
,
1146 uint8_t *buf
, bool async
)
1148 ram_counters
.transferred
+= save_page_header(rs
, rs
->f
, block
,
1149 offset
| RAM_SAVE_FLAG_PAGE
);
1151 qemu_put_buffer_async(rs
->f
, buf
, TARGET_PAGE_SIZE
,
1152 migrate_release_ram() &
1153 migration_in_postcopy());
1155 qemu_put_buffer(rs
->f
, buf
, TARGET_PAGE_SIZE
);
1157 ram_counters
.transferred
+= TARGET_PAGE_SIZE
;
1158 ram_counters
.normal
++;
1163 * ram_save_page: send the given page to the stream
1165 * Returns the number of pages written.
1167 * >=0 - Number of pages written - this might legally be 0
1168 * if xbzrle noticed the page was the same.
1170 * @rs: current RAM state
1171 * @block: block that contains the page we want to send
1172 * @offset: offset inside the block for the page
1173 * @last_stage: if we are at the completion stage
1175 static int ram_save_page(RAMState
*rs
, PageSearchStatus
*pss
, bool last_stage
)
1179 bool send_async
= true;
1180 RAMBlock
*block
= pss
->block
;
1181 ram_addr_t offset
= ((ram_addr_t
)pss
->page
) << TARGET_PAGE_BITS
;
1182 ram_addr_t current_addr
= block
->offset
+ offset
;
1184 p
= block
->host
+ offset
;
1185 trace_ram_save_page(block
->idstr
, (uint64_t)offset
, p
);
1187 XBZRLE_cache_lock();
1188 if (!rs
->ram_bulk_stage
&& !migration_in_postcopy() &&
1189 migrate_use_xbzrle()) {
1190 pages
= save_xbzrle_page(rs
, &p
, current_addr
, block
,
1191 offset
, last_stage
);
1193 /* Can't send this cached data async, since the cache page
1194 * might get updated before it gets to the wire
1200 /* XBZRLE overflow or normal page */
1202 pages
= save_normal_page(rs
, block
, offset
, p
, send_async
);
1205 XBZRLE_cache_unlock();
1210 static int ram_save_multifd_page(RAMState
*rs
, RAMBlock
*block
,
1213 if (multifd_queue_page(rs
->f
, block
, offset
) < 0) {
1216 ram_counters
.normal
++;
1221 static bool do_compress_ram_page(QEMUFile
*f
, z_stream
*stream
, RAMBlock
*block
,
1222 ram_addr_t offset
, uint8_t *source_buf
)
1224 RAMState
*rs
= ram_state
;
1225 uint8_t *p
= block
->host
+ (offset
& TARGET_PAGE_MASK
);
1226 bool zero_page
= false;
1229 if (save_zero_page_to_file(rs
, f
, block
, offset
)) {
1234 save_page_header(rs
, f
, block
, offset
| RAM_SAVE_FLAG_COMPRESS_PAGE
);
1237 * copy it to a internal buffer to avoid it being modified by VM
1238 * so that we can catch up the error during compression and
1241 memcpy(source_buf
, p
, TARGET_PAGE_SIZE
);
1242 ret
= qemu_put_compression_data(f
, stream
, source_buf
, TARGET_PAGE_SIZE
);
1244 qemu_file_set_error(migrate_get_current()->to_dst_file
, ret
);
1245 error_report("compressed data failed!");
1250 ram_release_pages(block
->idstr
, offset
& TARGET_PAGE_MASK
, 1);
1255 update_compress_thread_counts(const CompressParam
*param
, int bytes_xmit
)
1257 ram_counters
.transferred
+= bytes_xmit
;
1259 if (param
->zero_page
) {
1260 ram_counters
.duplicate
++;
1264 /* 8 means a header with RAM_SAVE_FLAG_CONTINUE. */
1265 compression_counters
.compressed_size
+= bytes_xmit
- 8;
1266 compression_counters
.pages
++;
1269 static bool save_page_use_compression(RAMState
*rs
);
1271 static void flush_compressed_data(RAMState
*rs
)
1273 int idx
, len
, thread_count
;
1275 if (!save_page_use_compression(rs
)) {
1278 thread_count
= migrate_compress_threads();
1280 qemu_mutex_lock(&comp_done_lock
);
1281 for (idx
= 0; idx
< thread_count
; idx
++) {
1282 while (!comp_param
[idx
].done
) {
1283 qemu_cond_wait(&comp_done_cond
, &comp_done_lock
);
1286 qemu_mutex_unlock(&comp_done_lock
);
1288 for (idx
= 0; idx
< thread_count
; idx
++) {
1289 qemu_mutex_lock(&comp_param
[idx
].mutex
);
1290 if (!comp_param
[idx
].quit
) {
1291 len
= qemu_put_qemu_file(rs
->f
, comp_param
[idx
].file
);
1293 * it's safe to fetch zero_page without holding comp_done_lock
1294 * as there is no further request submitted to the thread,
1295 * i.e, the thread should be waiting for a request at this point.
1297 update_compress_thread_counts(&comp_param
[idx
], len
);
1299 qemu_mutex_unlock(&comp_param
[idx
].mutex
);
1303 static inline void set_compress_params(CompressParam
*param
, RAMBlock
*block
,
1306 param
->block
= block
;
1307 param
->offset
= offset
;
1310 static int compress_page_with_multi_thread(RAMState
*rs
, RAMBlock
*block
,
1313 int idx
, thread_count
, bytes_xmit
= -1, pages
= -1;
1314 bool wait
= migrate_compress_wait_thread();
1316 thread_count
= migrate_compress_threads();
1317 qemu_mutex_lock(&comp_done_lock
);
1319 for (idx
= 0; idx
< thread_count
; idx
++) {
1320 if (comp_param
[idx
].done
) {
1321 comp_param
[idx
].done
= false;
1322 bytes_xmit
= qemu_put_qemu_file(rs
->f
, comp_param
[idx
].file
);
1323 qemu_mutex_lock(&comp_param
[idx
].mutex
);
1324 set_compress_params(&comp_param
[idx
], block
, offset
);
1325 qemu_cond_signal(&comp_param
[idx
].cond
);
1326 qemu_mutex_unlock(&comp_param
[idx
].mutex
);
1328 update_compress_thread_counts(&comp_param
[idx
], bytes_xmit
);
1334 * wait for the free thread if the user specifies 'compress-wait-thread',
1335 * otherwise we will post the page out in the main thread as normal page.
1337 if (pages
< 0 && wait
) {
1338 qemu_cond_wait(&comp_done_cond
, &comp_done_lock
);
1341 qemu_mutex_unlock(&comp_done_lock
);
1347 * find_dirty_block: find the next dirty page and update any state
1348 * associated with the search process.
1350 * Returns true if a page is found
1352 * @rs: current RAM state
1353 * @pss: data about the state of the current dirty page scan
1354 * @again: set to false if the search has scanned the whole of RAM
1356 static bool find_dirty_block(RAMState
*rs
, PageSearchStatus
*pss
, bool *again
)
1358 pss
->page
= migration_bitmap_find_dirty(rs
, pss
->block
, pss
->page
);
1359 if (pss
->complete_round
&& pss
->block
== rs
->last_seen_block
&&
1360 pss
->page
>= rs
->last_page
) {
1362 * We've been once around the RAM and haven't found anything.
1368 if ((((ram_addr_t
)pss
->page
) << TARGET_PAGE_BITS
)
1369 >= pss
->block
->used_length
) {
1370 /* Didn't find anything in this RAM Block */
1372 pss
->block
= QLIST_NEXT_RCU(pss
->block
, next
);
1375 * If memory migration starts over, we will meet a dirtied page
1376 * which may still exists in compression threads's ring, so we
1377 * should flush the compressed data to make sure the new page
1378 * is not overwritten by the old one in the destination.
1380 * Also If xbzrle is on, stop using the data compression at this
1381 * point. In theory, xbzrle can do better than compression.
1383 flush_compressed_data(rs
);
1385 /* Hit the end of the list */
1386 pss
->block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
1387 /* Flag that we've looped */
1388 pss
->complete_round
= true;
1389 rs
->ram_bulk_stage
= false;
1391 /* Didn't find anything this time, but try again on the new block */
1395 /* Can go around again, but... */
1397 /* We've found something so probably don't need to */
1403 * unqueue_page: gets a page of the queue
1405 * Helper for 'get_queued_page' - gets a page off the queue
1407 * Returns the block of the page (or NULL if none available)
1409 * @rs: current RAM state
1410 * @offset: used to return the offset within the RAMBlock
1412 static RAMBlock
*unqueue_page(RAMState
*rs
, ram_addr_t
*offset
)
1414 RAMBlock
*block
= NULL
;
1416 if (QSIMPLEQ_EMPTY_ATOMIC(&rs
->src_page_requests
)) {
1420 QEMU_LOCK_GUARD(&rs
->src_page_req_mutex
);
1421 if (!QSIMPLEQ_EMPTY(&rs
->src_page_requests
)) {
1422 struct RAMSrcPageRequest
*entry
=
1423 QSIMPLEQ_FIRST(&rs
->src_page_requests
);
1425 *offset
= entry
->offset
;
1427 if (entry
->len
> TARGET_PAGE_SIZE
) {
1428 entry
->len
-= TARGET_PAGE_SIZE
;
1429 entry
->offset
+= TARGET_PAGE_SIZE
;
1431 memory_region_unref(block
->mr
);
1432 QSIMPLEQ_REMOVE_HEAD(&rs
->src_page_requests
, next_req
);
1434 migration_consume_urgent_request();
1441 #if defined(__linux__)
1443 * poll_fault_page: try to get next UFFD write fault page and, if pending fault
1444 * is found, return RAM block pointer and page offset
1446 * Returns pointer to the RAMBlock containing faulting page,
1447 * NULL if no write faults are pending
1449 * @rs: current RAM state
1450 * @offset: page offset from the beginning of the block
1452 static RAMBlock
*poll_fault_page(RAMState
*rs
, ram_addr_t
*offset
)
1454 struct uffd_msg uffd_msg
;
1459 if (!migrate_background_snapshot()) {
1463 res
= uffd_read_events(rs
->uffdio_fd
, &uffd_msg
, 1);
1468 page_address
= (void *)(uintptr_t) uffd_msg
.arg
.pagefault
.address
;
1469 block
= qemu_ram_block_from_host(page_address
, false, offset
);
1470 assert(block
&& (block
->flags
& RAM_UF_WRITEPROTECT
) != 0);
1475 * ram_save_release_protection: release UFFD write protection after
1476 * a range of pages has been saved
1478 * @rs: current RAM state
1479 * @pss: page-search-status structure
1480 * @start_page: index of the first page in the range relative to pss->block
1482 * Returns 0 on success, negative value in case of an error
1484 static int ram_save_release_protection(RAMState
*rs
, PageSearchStatus
*pss
,
1485 unsigned long start_page
)
1489 /* Check if page is from UFFD-managed region. */
1490 if (pss
->block
->flags
& RAM_UF_WRITEPROTECT
) {
1491 void *page_address
= pss
->block
->host
+ (start_page
<< TARGET_PAGE_BITS
);
1492 uint64_t run_length
= (pss
->page
- start_page
+ 1) << TARGET_PAGE_BITS
;
1494 /* Flush async buffers before un-protect. */
1496 /* Un-protect memory range. */
1497 res
= uffd_change_protection(rs
->uffdio_fd
, page_address
, run_length
,
1504 /* ram_write_tracking_available: check if kernel supports required UFFD features
1506 * Returns true if supports, false otherwise
1508 bool ram_write_tracking_available(void)
1510 uint64_t uffd_features
;
1513 res
= uffd_query_features(&uffd_features
);
1515 (uffd_features
& UFFD_FEATURE_PAGEFAULT_FLAG_WP
) != 0);
1518 /* ram_write_tracking_compatible: check if guest configuration is
1519 * compatible with 'write-tracking'
1521 * Returns true if compatible, false otherwise
1523 bool ram_write_tracking_compatible(void)
1525 const uint64_t uffd_ioctls_mask
= BIT(_UFFDIO_WRITEPROTECT
);
1530 /* Open UFFD file descriptor */
1531 uffd_fd
= uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP
, false);
1536 RCU_READ_LOCK_GUARD();
1538 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
1539 uint64_t uffd_ioctls
;
1541 /* Nothing to do with read-only and MMIO-writable regions */
1542 if (block
->mr
->readonly
|| block
->mr
->rom_device
) {
1545 /* Try to register block memory via UFFD-IO to track writes */
1546 if (uffd_register_memory(uffd_fd
, block
->host
, block
->max_length
,
1547 UFFDIO_REGISTER_MODE_WP
, &uffd_ioctls
)) {
1550 if ((uffd_ioctls
& uffd_ioctls_mask
) != uffd_ioctls_mask
) {
1557 uffd_close_fd(uffd_fd
);
1562 * ram_block_populate_pages: populate memory in the RAM block by reading
1563 * an integer from the beginning of each page.
1565 * Since it's solely used for userfault_fd WP feature, here we just
1566 * hardcode page size to qemu_real_host_page_size.
1568 * @block: RAM block to populate
1570 static void ram_block_populate_pages(RAMBlock
*block
)
1572 char *ptr
= (char *) block
->host
;
1574 for (ram_addr_t offset
= 0; offset
< block
->used_length
;
1575 offset
+= qemu_real_host_page_size
) {
1576 char tmp
= *(ptr
+ offset
);
1578 /* Don't optimize the read out */
1579 asm volatile("" : "+r" (tmp
));
1584 * ram_write_tracking_prepare: prepare for UFFD-WP memory tracking
1586 void ram_write_tracking_prepare(void)
1590 RCU_READ_LOCK_GUARD();
1592 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
1593 /* Nothing to do with read-only and MMIO-writable regions */
1594 if (block
->mr
->readonly
|| block
->mr
->rom_device
) {
1599 * Populate pages of the RAM block before enabling userfault_fd
1602 * This stage is required since ioctl(UFFDIO_WRITEPROTECT) with
1603 * UFFDIO_WRITEPROTECT_MODE_WP mode setting would silently skip
1604 * pages with pte_none() entries in page table.
1606 ram_block_populate_pages(block
);
1611 * ram_write_tracking_start: start UFFD-WP memory tracking
1613 * Returns 0 for success or negative value in case of error
1615 int ram_write_tracking_start(void)
1618 RAMState
*rs
= ram_state
;
1621 /* Open UFFD file descriptor */
1622 uffd_fd
= uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP
, true);
1626 rs
->uffdio_fd
= uffd_fd
;
1628 RCU_READ_LOCK_GUARD();
1630 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
1631 /* Nothing to do with read-only and MMIO-writable regions */
1632 if (block
->mr
->readonly
|| block
->mr
->rom_device
) {
1636 /* Register block memory with UFFD to track writes */
1637 if (uffd_register_memory(rs
->uffdio_fd
, block
->host
,
1638 block
->max_length
, UFFDIO_REGISTER_MODE_WP
, NULL
)) {
1641 /* Apply UFFD write protection to the block memory range */
1642 if (uffd_change_protection(rs
->uffdio_fd
, block
->host
,
1643 block
->max_length
, true, false)) {
1646 block
->flags
|= RAM_UF_WRITEPROTECT
;
1647 memory_region_ref(block
->mr
);
1649 trace_ram_write_tracking_ramblock_start(block
->idstr
, block
->page_size
,
1650 block
->host
, block
->max_length
);
1656 error_report("ram_write_tracking_start() failed: restoring initial memory state");
1658 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
1659 if ((block
->flags
& RAM_UF_WRITEPROTECT
) == 0) {
1663 * In case some memory block failed to be write-protected
1664 * remove protection and unregister all succeeded RAM blocks
1666 uffd_change_protection(rs
->uffdio_fd
, block
->host
, block
->max_length
,
1668 uffd_unregister_memory(rs
->uffdio_fd
, block
->host
, block
->max_length
);
1669 /* Cleanup flags and remove reference */
1670 block
->flags
&= ~RAM_UF_WRITEPROTECT
;
1671 memory_region_unref(block
->mr
);
1674 uffd_close_fd(uffd_fd
);
1680 * ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection
1682 void ram_write_tracking_stop(void)
1684 RAMState
*rs
= ram_state
;
1687 RCU_READ_LOCK_GUARD();
1689 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
1690 if ((block
->flags
& RAM_UF_WRITEPROTECT
) == 0) {
1693 /* Remove protection and unregister all affected RAM blocks */
1694 uffd_change_protection(rs
->uffdio_fd
, block
->host
, block
->max_length
,
1696 uffd_unregister_memory(rs
->uffdio_fd
, block
->host
, block
->max_length
);
1698 trace_ram_write_tracking_ramblock_stop(block
->idstr
, block
->page_size
,
1699 block
->host
, block
->max_length
);
1701 /* Cleanup flags and remove reference */
1702 block
->flags
&= ~RAM_UF_WRITEPROTECT
;
1703 memory_region_unref(block
->mr
);
1706 /* Finally close UFFD file descriptor */
1707 uffd_close_fd(rs
->uffdio_fd
);
1712 /* No target OS support, stubs just fail or ignore */
1714 static RAMBlock
*poll_fault_page(RAMState
*rs
, ram_addr_t
*offset
)
1722 static int ram_save_release_protection(RAMState
*rs
, PageSearchStatus
*pss
,
1723 unsigned long start_page
)
1732 bool ram_write_tracking_available(void)
1737 bool ram_write_tracking_compatible(void)
1743 int ram_write_tracking_start(void)
1749 void ram_write_tracking_stop(void)
1753 #endif /* defined(__linux__) */
1756 * get_queued_page: unqueue a page from the postcopy requests
1758 * Skips pages that are already sent (!dirty)
1760 * Returns true if a queued page is found
1762 * @rs: current RAM state
1763 * @pss: data about the state of the current dirty page scan
1765 static bool get_queued_page(RAMState
*rs
, PageSearchStatus
*pss
)
1772 block
= unqueue_page(rs
, &offset
);
1774 * We're sending this page, and since it's postcopy nothing else
1775 * will dirty it, and we must make sure it doesn't get sent again
1776 * even if this queue request was received after the background
1777 * search already sent it.
1782 page
= offset
>> TARGET_PAGE_BITS
;
1783 dirty
= test_bit(page
, block
->bmap
);
1785 trace_get_queued_page_not_dirty(block
->idstr
, (uint64_t)offset
,
1788 trace_get_queued_page(block
->idstr
, (uint64_t)offset
, page
);
1792 } while (block
&& !dirty
);
1796 * Poll write faults too if background snapshot is enabled; that's
1797 * when we have vcpus got blocked by the write protected pages.
1799 block
= poll_fault_page(rs
, &offset
);
1804 * As soon as we start servicing pages out of order, then we have
1805 * to kill the bulk stage, since the bulk stage assumes
1806 * in (migration_bitmap_find_and_reset_dirty) that every page is
1807 * dirty, that's no longer true.
1809 rs
->ram_bulk_stage
= false;
1812 * We want the background search to continue from the queued page
1813 * since the guest is likely to want other pages near to the page
1814 * it just requested.
1817 pss
->page
= offset
>> TARGET_PAGE_BITS
;
1820 * This unqueued page would break the "one round" check, even is
1823 pss
->complete_round
= false;
1830 * migration_page_queue_free: drop any remaining pages in the ram
1833 * It should be empty at the end anyway, but in error cases there may
1834 * be some left. in case that there is any page left, we drop it.
1837 static void migration_page_queue_free(RAMState
*rs
)
1839 struct RAMSrcPageRequest
*mspr
, *next_mspr
;
1840 /* This queue generally should be empty - but in the case of a failed
1841 * migration might have some droppings in.
1843 RCU_READ_LOCK_GUARD();
1844 QSIMPLEQ_FOREACH_SAFE(mspr
, &rs
->src_page_requests
, next_req
, next_mspr
) {
1845 memory_region_unref(mspr
->rb
->mr
);
1846 QSIMPLEQ_REMOVE_HEAD(&rs
->src_page_requests
, next_req
);
1852 * ram_save_queue_pages: queue the page for transmission
1854 * A request from postcopy destination for example.
1856 * Returns zero on success or negative on error
1858 * @rbname: Name of the RAMBLock of the request. NULL means the
1859 * same that last one.
1860 * @start: starting address from the start of the RAMBlock
1861 * @len: length (in bytes) to send
1863 int ram_save_queue_pages(const char *rbname
, ram_addr_t start
, ram_addr_t len
)
1866 RAMState
*rs
= ram_state
;
1868 ram_counters
.postcopy_requests
++;
1869 RCU_READ_LOCK_GUARD();
1872 /* Reuse last RAMBlock */
1873 ramblock
= rs
->last_req_rb
;
1877 * Shouldn't happen, we can't reuse the last RAMBlock if
1878 * it's the 1st request.
1880 error_report("ram_save_queue_pages no previous block");
1884 ramblock
= qemu_ram_block_by_name(rbname
);
1887 /* We shouldn't be asked for a non-existent RAMBlock */
1888 error_report("ram_save_queue_pages no block '%s'", rbname
);
1891 rs
->last_req_rb
= ramblock
;
1893 trace_ram_save_queue_pages(ramblock
->idstr
, start
, len
);
1894 if (start
+ len
> ramblock
->used_length
) {
1895 error_report("%s request overrun start=" RAM_ADDR_FMT
" len="
1896 RAM_ADDR_FMT
" blocklen=" RAM_ADDR_FMT
,
1897 __func__
, start
, len
, ramblock
->used_length
);
1901 struct RAMSrcPageRequest
*new_entry
=
1902 g_malloc0(sizeof(struct RAMSrcPageRequest
));
1903 new_entry
->rb
= ramblock
;
1904 new_entry
->offset
= start
;
1905 new_entry
->len
= len
;
1907 memory_region_ref(ramblock
->mr
);
1908 qemu_mutex_lock(&rs
->src_page_req_mutex
);
1909 QSIMPLEQ_INSERT_TAIL(&rs
->src_page_requests
, new_entry
, next_req
);
1910 migration_make_urgent_request();
1911 qemu_mutex_unlock(&rs
->src_page_req_mutex
);
1916 static bool save_page_use_compression(RAMState
*rs
)
1918 if (!migrate_use_compression()) {
1923 * If xbzrle is on, stop using the data compression after first
1924 * round of migration even if compression is enabled. In theory,
1925 * xbzrle can do better than compression.
1927 if (rs
->ram_bulk_stage
|| !migrate_use_xbzrle()) {
1935 * try to compress the page before posting it out, return true if the page
1936 * has been properly handled by compression, otherwise needs other
1937 * paths to handle it
1939 static bool save_compress_page(RAMState
*rs
, RAMBlock
*block
, ram_addr_t offset
)
1941 if (!save_page_use_compression(rs
)) {
1946 * When starting the process of a new block, the first page of
1947 * the block should be sent out before other pages in the same
1948 * block, and all the pages in last block should have been sent
1949 * out, keeping this order is important, because the 'cont' flag
1950 * is used to avoid resending the block name.
1952 * We post the fist page as normal page as compression will take
1953 * much CPU resource.
1955 if (block
!= rs
->last_sent_block
) {
1956 flush_compressed_data(rs
);
1960 if (compress_page_with_multi_thread(rs
, block
, offset
) > 0) {
1964 compression_counters
.busy
++;
1969 * ram_save_target_page: save one target page
1971 * Returns the number of pages written
1973 * @rs: current RAM state
1974 * @pss: data about the page we want to send
1975 * @last_stage: if we are at the completion stage
1977 static int ram_save_target_page(RAMState
*rs
, PageSearchStatus
*pss
,
1980 RAMBlock
*block
= pss
->block
;
1981 ram_addr_t offset
= ((ram_addr_t
)pss
->page
) << TARGET_PAGE_BITS
;
1984 if (control_save_page(rs
, block
, offset
, &res
)) {
1988 if (save_compress_page(rs
, block
, offset
)) {
1992 res
= save_zero_page(rs
, block
, offset
);
1994 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
1995 * page would be stale
1997 if (!save_page_use_compression(rs
)) {
1998 XBZRLE_cache_lock();
1999 xbzrle_cache_zero_page(rs
, block
->offset
+ offset
);
2000 XBZRLE_cache_unlock();
2002 ram_release_pages(block
->idstr
, offset
, res
);
2007 * Do not use multifd for:
2008 * 1. Compression as the first page in the new block should be posted out
2009 * before sending the compressed page
2010 * 2. In postcopy as one whole host page should be placed
2012 if (!save_page_use_compression(rs
) && migrate_use_multifd()
2013 && !migration_in_postcopy()) {
2014 return ram_save_multifd_page(rs
, block
, offset
);
2017 return ram_save_page(rs
, pss
, last_stage
);
2021 * ram_save_host_page: save a whole host page
2023 * Starting at *offset send pages up to the end of the current host
2024 * page. It's valid for the initial offset to point into the middle of
2025 * a host page in which case the remainder of the hostpage is sent.
2026 * Only dirty target pages are sent. Note that the host page size may
2027 * be a huge page for this block.
2028 * The saving stops at the boundary of the used_length of the block
2029 * if the RAMBlock isn't a multiple of the host page size.
2031 * Returns the number of pages written or negative on error
2033 * @rs: current RAM state
2034 * @ms: current migration state
2035 * @pss: data about the page we want to send
2036 * @last_stage: if we are at the completion stage
2038 static int ram_save_host_page(RAMState
*rs
, PageSearchStatus
*pss
,
2041 int tmppages
, pages
= 0;
2042 size_t pagesize_bits
=
2043 qemu_ram_pagesize(pss
->block
) >> TARGET_PAGE_BITS
;
2044 unsigned long start_page
= pss
->page
;
2047 if (ramblock_is_ignored(pss
->block
)) {
2048 error_report("block %s should not be migrated !", pss
->block
->idstr
);
2053 /* Check the pages is dirty and if it is send it */
2054 if (!migration_bitmap_clear_dirty(rs
, pss
->block
, pss
->page
)) {
2059 tmppages
= ram_save_target_page(rs
, pss
, last_stage
);
2066 /* Allow rate limiting to happen in the middle of huge pages */
2067 migration_rate_limit();
2068 } while ((pss
->page
& (pagesize_bits
- 1)) &&
2069 offset_in_ramblock(pss
->block
,
2070 ((ram_addr_t
)pss
->page
) << TARGET_PAGE_BITS
));
2071 /* The offset we leave with is the last one we looked at */
2074 res
= ram_save_release_protection(rs
, pss
, start_page
);
2075 return (res
< 0 ? res
: pages
);
2079 * ram_find_and_save_block: finds a dirty page and sends it to f
2081 * Called within an RCU critical section.
2083 * Returns the number of pages written where zero means no dirty pages,
2084 * or negative on error
2086 * @rs: current RAM state
2087 * @last_stage: if we are at the completion stage
2089 * On systems where host-page-size > target-page-size it will send all the
2090 * pages in a host page that are dirty.
2093 static int ram_find_and_save_block(RAMState
*rs
, bool last_stage
)
2095 PageSearchStatus pss
;
2099 /* No dirty page as there is zero RAM */
2100 if (!ram_bytes_total()) {
2104 pss
.block
= rs
->last_seen_block
;
2105 pss
.page
= rs
->last_page
;
2106 pss
.complete_round
= false;
2109 pss
.block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
2114 found
= get_queued_page(rs
, &pss
);
2117 /* priority queue empty, so just search for something dirty */
2118 found
= find_dirty_block(rs
, &pss
, &again
);
2122 pages
= ram_save_host_page(rs
, &pss
, last_stage
);
2124 } while (!pages
&& again
);
2126 rs
->last_seen_block
= pss
.block
;
2127 rs
->last_page
= pss
.page
;
2132 void acct_update_position(QEMUFile
*f
, size_t size
, bool zero
)
2134 uint64_t pages
= size
/ TARGET_PAGE_SIZE
;
2137 ram_counters
.duplicate
+= pages
;
2139 ram_counters
.normal
+= pages
;
2140 ram_counters
.transferred
+= size
;
2141 qemu_update_position(f
, size
);
2145 static uint64_t ram_bytes_total_common(bool count_ignored
)
2150 RCU_READ_LOCK_GUARD();
2152 if (count_ignored
) {
2153 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
2154 total
+= block
->used_length
;
2157 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2158 total
+= block
->used_length
;
2164 uint64_t ram_bytes_total(void)
2166 return ram_bytes_total_common(false);
2169 static void xbzrle_load_setup(void)
2171 XBZRLE
.decoded_buf
= g_malloc(TARGET_PAGE_SIZE
);
2174 static void xbzrle_load_cleanup(void)
2176 g_free(XBZRLE
.decoded_buf
);
2177 XBZRLE
.decoded_buf
= NULL
;
2180 static void ram_state_cleanup(RAMState
**rsp
)
2183 migration_page_queue_free(*rsp
);
2184 qemu_mutex_destroy(&(*rsp
)->bitmap_mutex
);
2185 qemu_mutex_destroy(&(*rsp
)->src_page_req_mutex
);
2191 static void xbzrle_cleanup(void)
2193 XBZRLE_cache_lock();
2195 cache_fini(XBZRLE
.cache
);
2196 g_free(XBZRLE
.encoded_buf
);
2197 g_free(XBZRLE
.current_buf
);
2198 g_free(XBZRLE
.zero_target_page
);
2199 XBZRLE
.cache
= NULL
;
2200 XBZRLE
.encoded_buf
= NULL
;
2201 XBZRLE
.current_buf
= NULL
;
2202 XBZRLE
.zero_target_page
= NULL
;
2204 XBZRLE_cache_unlock();
2207 static void ram_save_cleanup(void *opaque
)
2209 RAMState
**rsp
= opaque
;
2212 /* We don't use dirty log with background snapshots */
2213 if (!migrate_background_snapshot()) {
2214 /* caller have hold iothread lock or is in a bh, so there is
2215 * no writing race against the migration bitmap
2217 memory_global_dirty_log_stop();
2220 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2221 g_free(block
->clear_bmap
);
2222 block
->clear_bmap
= NULL
;
2223 g_free(block
->bmap
);
2228 compress_threads_save_cleanup();
2229 ram_state_cleanup(rsp
);
2232 static void ram_state_reset(RAMState
*rs
)
2234 rs
->last_seen_block
= NULL
;
2235 rs
->last_sent_block
= NULL
;
2237 rs
->last_version
= ram_list
.version
;
2238 rs
->ram_bulk_stage
= true;
2239 rs
->fpo_enabled
= false;
2242 #define MAX_WAIT 50 /* ms, half buffered_file limit */
2245 * 'expected' is the value you expect the bitmap mostly to be full
2246 * of; it won't bother printing lines that are all this value.
2247 * If 'todump' is null the migration bitmap is dumped.
2249 void ram_debug_dump_bitmap(unsigned long *todump
, bool expected
,
2250 unsigned long pages
)
2253 int64_t linelen
= 128;
2256 for (cur
= 0; cur
< pages
; cur
+= linelen
) {
2260 * Last line; catch the case where the line length
2261 * is longer than remaining ram
2263 if (cur
+ linelen
> pages
) {
2264 linelen
= pages
- cur
;
2266 for (curb
= 0; curb
< linelen
; curb
++) {
2267 bool thisbit
= test_bit(cur
+ curb
, todump
);
2268 linebuf
[curb
] = thisbit
? '1' : '.';
2269 found
= found
|| (thisbit
!= expected
);
2272 linebuf
[curb
] = '\0';
2273 fprintf(stderr
, "0x%08" PRIx64
" : %s\n", cur
, linebuf
);
2278 /* **** functions for postcopy ***** */
2280 void ram_postcopy_migrated_memory_release(MigrationState
*ms
)
2282 struct RAMBlock
*block
;
2284 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2285 unsigned long *bitmap
= block
->bmap
;
2286 unsigned long range
= block
->used_length
>> TARGET_PAGE_BITS
;
2287 unsigned long run_start
= find_next_zero_bit(bitmap
, range
, 0);
2289 while (run_start
< range
) {
2290 unsigned long run_end
= find_next_bit(bitmap
, range
, run_start
+ 1);
2291 ram_discard_range(block
->idstr
,
2292 ((ram_addr_t
)run_start
) << TARGET_PAGE_BITS
,
2293 ((ram_addr_t
)(run_end
- run_start
))
2294 << TARGET_PAGE_BITS
);
2295 run_start
= find_next_zero_bit(bitmap
, range
, run_end
+ 1);
2301 * postcopy_send_discard_bm_ram: discard a RAMBlock
2303 * Returns zero on success
2305 * Callback from postcopy_each_ram_send_discard for each RAMBlock
2307 * @ms: current migration state
2308 * @block: RAMBlock to discard
2310 static int postcopy_send_discard_bm_ram(MigrationState
*ms
, RAMBlock
*block
)
2312 unsigned long end
= block
->used_length
>> TARGET_PAGE_BITS
;
2313 unsigned long current
;
2314 unsigned long *bitmap
= block
->bmap
;
2316 for (current
= 0; current
< end
; ) {
2317 unsigned long one
= find_next_bit(bitmap
, end
, current
);
2318 unsigned long zero
, discard_length
;
2324 zero
= find_next_zero_bit(bitmap
, end
, one
+ 1);
2327 discard_length
= end
- one
;
2329 discard_length
= zero
- one
;
2331 postcopy_discard_send_range(ms
, one
, discard_length
);
2332 current
= one
+ discard_length
;
2339 * postcopy_each_ram_send_discard: discard all RAMBlocks
2341 * Returns 0 for success or negative for error
2343 * Utility for the outgoing postcopy code.
2344 * Calls postcopy_send_discard_bm_ram for each RAMBlock
2345 * passing it bitmap indexes and name.
2346 * (qemu_ram_foreach_block ends up passing unscaled lengths
2347 * which would mean postcopy code would have to deal with target page)
2349 * @ms: current migration state
2351 static int postcopy_each_ram_send_discard(MigrationState
*ms
)
2353 struct RAMBlock
*block
;
2356 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2357 postcopy_discard_send_init(ms
, block
->idstr
);
2360 * Postcopy sends chunks of bitmap over the wire, but it
2361 * just needs indexes at this point, avoids it having
2362 * target page specific code.
2364 ret
= postcopy_send_discard_bm_ram(ms
, block
);
2365 postcopy_discard_send_finish(ms
);
2375 * postcopy_chunk_hostpages_pass: canonicalize bitmap in hostpages
2377 * Helper for postcopy_chunk_hostpages; it's called twice to
2378 * canonicalize the two bitmaps, that are similar, but one is
2381 * Postcopy requires that all target pages in a hostpage are dirty or
2382 * clean, not a mix. This function canonicalizes the bitmaps.
2384 * @ms: current migration state
2385 * @block: block that contains the page we want to canonicalize
2387 static void postcopy_chunk_hostpages_pass(MigrationState
*ms
, RAMBlock
*block
)
2389 RAMState
*rs
= ram_state
;
2390 unsigned long *bitmap
= block
->bmap
;
2391 unsigned int host_ratio
= block
->page_size
/ TARGET_PAGE_SIZE
;
2392 unsigned long pages
= block
->used_length
>> TARGET_PAGE_BITS
;
2393 unsigned long run_start
;
2395 if (block
->page_size
== TARGET_PAGE_SIZE
) {
2396 /* Easy case - TPS==HPS for a non-huge page RAMBlock */
2400 /* Find a dirty page */
2401 run_start
= find_next_bit(bitmap
, pages
, 0);
2403 while (run_start
< pages
) {
2406 * If the start of this run of pages is in the middle of a host
2407 * page, then we need to fixup this host page.
2409 if (QEMU_IS_ALIGNED(run_start
, host_ratio
)) {
2410 /* Find the end of this run */
2411 run_start
= find_next_zero_bit(bitmap
, pages
, run_start
+ 1);
2413 * If the end isn't at the start of a host page, then the
2414 * run doesn't finish at the end of a host page
2415 * and we need to discard.
2419 if (!QEMU_IS_ALIGNED(run_start
, host_ratio
)) {
2421 unsigned long fixup_start_addr
= QEMU_ALIGN_DOWN(run_start
,
2423 run_start
= QEMU_ALIGN_UP(run_start
, host_ratio
);
2425 /* Clean up the bitmap */
2426 for (page
= fixup_start_addr
;
2427 page
< fixup_start_addr
+ host_ratio
; page
++) {
2429 * Remark them as dirty, updating the count for any pages
2430 * that weren't previously dirty.
2432 rs
->migration_dirty_pages
+= !test_and_set_bit(page
, bitmap
);
2436 /* Find the next dirty page for the next iteration */
2437 run_start
= find_next_bit(bitmap
, pages
, run_start
);
2442 * postcopy_chunk_hostpages: discard any partially sent host page
2444 * Utility for the outgoing postcopy code.
2446 * Discard any partially sent host-page size chunks, mark any partially
2447 * dirty host-page size chunks as all dirty. In this case the host-page
2448 * is the host-page for the particular RAMBlock, i.e. it might be a huge page
2450 * Returns zero on success
2452 * @ms: current migration state
2453 * @block: block we want to work with
2455 static int postcopy_chunk_hostpages(MigrationState
*ms
, RAMBlock
*block
)
2457 postcopy_discard_send_init(ms
, block
->idstr
);
2460 * Ensure that all partially dirty host pages are made fully dirty.
2462 postcopy_chunk_hostpages_pass(ms
, block
);
2464 postcopy_discard_send_finish(ms
);
2469 * ram_postcopy_send_discard_bitmap: transmit the discard bitmap
2471 * Returns zero on success
2473 * Transmit the set of pages to be discarded after precopy to the target
2474 * these are pages that:
2475 * a) Have been previously transmitted but are now dirty again
2476 * b) Pages that have never been transmitted, this ensures that
2477 * any pages on the destination that have been mapped by background
2478 * tasks get discarded (transparent huge pages is the specific concern)
2479 * Hopefully this is pretty sparse
2481 * @ms: current migration state
2483 int ram_postcopy_send_discard_bitmap(MigrationState
*ms
)
2485 RAMState
*rs
= ram_state
;
2489 RCU_READ_LOCK_GUARD();
2491 /* This should be our last sync, the src is now paused */
2492 migration_bitmap_sync(rs
);
2494 /* Easiest way to make sure we don't resume in the middle of a host-page */
2495 rs
->last_seen_block
= NULL
;
2496 rs
->last_sent_block
= NULL
;
2499 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2500 /* Deal with TPS != HPS and huge pages */
2501 ret
= postcopy_chunk_hostpages(ms
, block
);
2506 #ifdef DEBUG_POSTCOPY
2507 ram_debug_dump_bitmap(block
->bmap
, true,
2508 block
->used_length
>> TARGET_PAGE_BITS
);
2511 trace_ram_postcopy_send_discard_bitmap();
2513 return postcopy_each_ram_send_discard(ms
);
2517 * ram_discard_range: discard dirtied pages at the beginning of postcopy
2519 * Returns zero on success
2521 * @rbname: name of the RAMBlock of the request. NULL means the
2522 * same that last one.
2523 * @start: RAMBlock starting page
2524 * @length: RAMBlock size
2526 int ram_discard_range(const char *rbname
, uint64_t start
, size_t length
)
2528 trace_ram_discard_range(rbname
, start
, length
);
2530 RCU_READ_LOCK_GUARD();
2531 RAMBlock
*rb
= qemu_ram_block_by_name(rbname
);
2534 error_report("ram_discard_range: Failed to find block '%s'", rbname
);
2539 * On source VM, we don't need to update the received bitmap since
2540 * we don't even have one.
2542 if (rb
->receivedmap
) {
2543 bitmap_clear(rb
->receivedmap
, start
>> qemu_target_page_bits(),
2544 length
>> qemu_target_page_bits());
2547 return ram_block_discard_range(rb
, start
, length
);
2551 * For every allocation, we will try not to crash the VM if the
2552 * allocation failed.
2554 static int xbzrle_init(void)
2556 Error
*local_err
= NULL
;
2558 if (!migrate_use_xbzrle()) {
2562 XBZRLE_cache_lock();
2564 XBZRLE
.zero_target_page
= g_try_malloc0(TARGET_PAGE_SIZE
);
2565 if (!XBZRLE
.zero_target_page
) {
2566 error_report("%s: Error allocating zero page", __func__
);
2570 XBZRLE
.cache
= cache_init(migrate_xbzrle_cache_size(),
2571 TARGET_PAGE_SIZE
, &local_err
);
2572 if (!XBZRLE
.cache
) {
2573 error_report_err(local_err
);
2574 goto free_zero_page
;
2577 XBZRLE
.encoded_buf
= g_try_malloc0(TARGET_PAGE_SIZE
);
2578 if (!XBZRLE
.encoded_buf
) {
2579 error_report("%s: Error allocating encoded_buf", __func__
);
2583 XBZRLE
.current_buf
= g_try_malloc(TARGET_PAGE_SIZE
);
2584 if (!XBZRLE
.current_buf
) {
2585 error_report("%s: Error allocating current_buf", __func__
);
2586 goto free_encoded_buf
;
2589 /* We are all good */
2590 XBZRLE_cache_unlock();
2594 g_free(XBZRLE
.encoded_buf
);
2595 XBZRLE
.encoded_buf
= NULL
;
2597 cache_fini(XBZRLE
.cache
);
2598 XBZRLE
.cache
= NULL
;
2600 g_free(XBZRLE
.zero_target_page
);
2601 XBZRLE
.zero_target_page
= NULL
;
2603 XBZRLE_cache_unlock();
2607 static int ram_state_init(RAMState
**rsp
)
2609 *rsp
= g_try_new0(RAMState
, 1);
2612 error_report("%s: Init ramstate fail", __func__
);
2616 qemu_mutex_init(&(*rsp
)->bitmap_mutex
);
2617 qemu_mutex_init(&(*rsp
)->src_page_req_mutex
);
2618 QSIMPLEQ_INIT(&(*rsp
)->src_page_requests
);
2621 * Count the total number of pages used by ram blocks not including any
2622 * gaps due to alignment or unplugs.
2623 * This must match with the initial values of dirty bitmap.
2625 (*rsp
)->migration_dirty_pages
= ram_bytes_total() >> TARGET_PAGE_BITS
;
2626 ram_state_reset(*rsp
);
2631 static void ram_list_init_bitmaps(void)
2633 MigrationState
*ms
= migrate_get_current();
2635 unsigned long pages
;
2638 /* Skip setting bitmap if there is no RAM */
2639 if (ram_bytes_total()) {
2640 shift
= ms
->clear_bitmap_shift
;
2641 if (shift
> CLEAR_BITMAP_SHIFT_MAX
) {
2642 error_report("clear_bitmap_shift (%u) too big, using "
2643 "max value (%u)", shift
, CLEAR_BITMAP_SHIFT_MAX
);
2644 shift
= CLEAR_BITMAP_SHIFT_MAX
;
2645 } else if (shift
< CLEAR_BITMAP_SHIFT_MIN
) {
2646 error_report("clear_bitmap_shift (%u) too small, using "
2647 "min value (%u)", shift
, CLEAR_BITMAP_SHIFT_MIN
);
2648 shift
= CLEAR_BITMAP_SHIFT_MIN
;
2651 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2652 pages
= block
->max_length
>> TARGET_PAGE_BITS
;
2654 * The initial dirty bitmap for migration must be set with all
2655 * ones to make sure we'll migrate every guest RAM page to
2657 * Here we set RAMBlock.bmap all to 1 because when rebegin a
2658 * new migration after a failed migration, ram_list.
2659 * dirty_memory[DIRTY_MEMORY_MIGRATION] don't include the whole
2662 block
->bmap
= bitmap_new(pages
);
2663 bitmap_set(block
->bmap
, 0, pages
);
2664 block
->clear_bmap_shift
= shift
;
2665 block
->clear_bmap
= bitmap_new(clear_bmap_size(pages
, shift
));
2670 static void ram_init_bitmaps(RAMState
*rs
)
2672 /* For memory_global_dirty_log_start below. */
2673 qemu_mutex_lock_iothread();
2674 qemu_mutex_lock_ramlist();
2676 WITH_RCU_READ_LOCK_GUARD() {
2677 ram_list_init_bitmaps();
2678 /* We don't use dirty log with background snapshots */
2679 if (!migrate_background_snapshot()) {
2680 memory_global_dirty_log_start();
2681 migration_bitmap_sync_precopy(rs
);
2684 qemu_mutex_unlock_ramlist();
2685 qemu_mutex_unlock_iothread();
2688 static int ram_init_all(RAMState
**rsp
)
2690 if (ram_state_init(rsp
)) {
2694 if (xbzrle_init()) {
2695 ram_state_cleanup(rsp
);
2699 ram_init_bitmaps(*rsp
);
2704 static void ram_state_resume_prepare(RAMState
*rs
, QEMUFile
*out
)
2710 * Postcopy is not using xbzrle/compression, so no need for that.
2711 * Also, since source are already halted, we don't need to care
2712 * about dirty page logging as well.
2715 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
2716 pages
+= bitmap_count_one(block
->bmap
,
2717 block
->used_length
>> TARGET_PAGE_BITS
);
2720 /* This may not be aligned with current bitmaps. Recalculate. */
2721 rs
->migration_dirty_pages
= pages
;
2723 rs
->last_seen_block
= NULL
;
2724 rs
->last_sent_block
= NULL
;
2726 rs
->last_version
= ram_list
.version
;
2728 * Disable the bulk stage, otherwise we'll resend the whole RAM no
2729 * matter what we have sent.
2731 rs
->ram_bulk_stage
= false;
2733 /* Update RAMState cache of output QEMUFile */
2736 trace_ram_state_resume_prepare(pages
);
2740 * This function clears bits of the free pages reported by the caller from the
2741 * migration dirty bitmap. @addr is the host address corresponding to the
2742 * start of the continuous guest free pages, and @len is the total bytes of
2745 void qemu_guest_free_page_hint(void *addr
, size_t len
)
2749 size_t used_len
, start
, npages
;
2750 MigrationState
*s
= migrate_get_current();
2752 /* This function is currently expected to be used during live migration */
2753 if (!migration_is_setup_or_active(s
->state
)) {
2757 for (; len
> 0; len
-= used_len
, addr
+= used_len
) {
2758 block
= qemu_ram_block_from_host(addr
, false, &offset
);
2759 if (unlikely(!block
|| offset
>= block
->used_length
)) {
2761 * The implementation might not support RAMBlock resize during
2762 * live migration, but it could happen in theory with future
2763 * updates. So we add a check here to capture that case.
2765 error_report_once("%s unexpected error", __func__
);
2769 if (len
<= block
->used_length
- offset
) {
2772 used_len
= block
->used_length
- offset
;
2775 start
= offset
>> TARGET_PAGE_BITS
;
2776 npages
= used_len
>> TARGET_PAGE_BITS
;
2778 qemu_mutex_lock(&ram_state
->bitmap_mutex
);
2779 ram_state
->migration_dirty_pages
-=
2780 bitmap_count_one_with_offset(block
->bmap
, start
, npages
);
2781 bitmap_clear(block
->bmap
, start
, npages
);
2782 qemu_mutex_unlock(&ram_state
->bitmap_mutex
);
2787 * Each of ram_save_setup, ram_save_iterate and ram_save_complete has
2788 * long-running RCU critical section. When rcu-reclaims in the code
2789 * start to become numerous it will be necessary to reduce the
2790 * granularity of these critical sections.
2794 * ram_save_setup: Setup RAM for migration
2796 * Returns zero to indicate success and negative for error
2798 * @f: QEMUFile where to send the data
2799 * @opaque: RAMState pointer
2801 static int ram_save_setup(QEMUFile
*f
, void *opaque
)
2803 RAMState
**rsp
= opaque
;
2806 if (compress_threads_save_setup()) {
2810 /* migration has already setup the bitmap, reuse it. */
2811 if (!migration_in_colo_state()) {
2812 if (ram_init_all(rsp
) != 0) {
2813 compress_threads_save_cleanup();
2819 WITH_RCU_READ_LOCK_GUARD() {
2820 qemu_put_be64(f
, ram_bytes_total_common(true) | RAM_SAVE_FLAG_MEM_SIZE
);
2822 RAMBLOCK_FOREACH_MIGRATABLE(block
) {
2823 qemu_put_byte(f
, strlen(block
->idstr
));
2824 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, strlen(block
->idstr
));
2825 qemu_put_be64(f
, block
->used_length
);
2826 if (migrate_postcopy_ram() && block
->page_size
!=
2827 qemu_host_page_size
) {
2828 qemu_put_be64(f
, block
->page_size
);
2830 if (migrate_ignore_shared()) {
2831 qemu_put_be64(f
, block
->mr
->addr
);
2836 ram_control_before_iterate(f
, RAM_CONTROL_SETUP
);
2837 ram_control_after_iterate(f
, RAM_CONTROL_SETUP
);
2839 multifd_send_sync_main(f
);
2840 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2847 * ram_save_iterate: iterative stage for migration
2849 * Returns zero to indicate success and negative for error
2851 * @f: QEMUFile where to send the data
2852 * @opaque: RAMState pointer
2854 static int ram_save_iterate(QEMUFile
*f
, void *opaque
)
2856 RAMState
**temp
= opaque
;
2857 RAMState
*rs
= *temp
;
2863 if (blk_mig_bulk_active()) {
2864 /* Avoid transferring ram during bulk phase of block migration as
2865 * the bulk phase will usually take a long time and transferring
2866 * ram updates during that time is pointless. */
2870 WITH_RCU_READ_LOCK_GUARD() {
2871 if (ram_list
.version
!= rs
->last_version
) {
2872 ram_state_reset(rs
);
2875 /* Read version before ram_list.blocks */
2878 ram_control_before_iterate(f
, RAM_CONTROL_ROUND
);
2880 t0
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
2882 while ((ret
= qemu_file_rate_limit(f
)) == 0 ||
2883 !QSIMPLEQ_EMPTY(&rs
->src_page_requests
)) {
2886 if (qemu_file_get_error(f
)) {
2890 pages
= ram_find_and_save_block(rs
, false);
2891 /* no more pages to sent */
2898 qemu_file_set_error(f
, pages
);
2902 rs
->target_page_count
+= pages
;
2905 * During postcopy, it is necessary to make sure one whole host
2906 * page is sent in one chunk.
2908 if (migrate_postcopy_ram()) {
2909 flush_compressed_data(rs
);
2913 * we want to check in the 1st loop, just in case it was the 1st
2914 * time and we had to sync the dirty bitmap.
2915 * qemu_clock_get_ns() is a bit expensive, so we only check each
2918 if ((i
& 63) == 0) {
2919 uint64_t t1
= (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - t0
) /
2921 if (t1
> MAX_WAIT
) {
2922 trace_ram_save_iterate_big_wait(t1
, i
);
2931 * Must occur before EOS (or any QEMUFile operation)
2932 * because of RDMA protocol.
2934 ram_control_after_iterate(f
, RAM_CONTROL_ROUND
);
2938 && migration_is_setup_or_active(migrate_get_current()->state
)) {
2939 multifd_send_sync_main(rs
->f
);
2940 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2942 ram_counters
.transferred
+= 8;
2944 ret
= qemu_file_get_error(f
);
2954 * ram_save_complete: function called to send the remaining amount of ram
2956 * Returns zero to indicate success or negative on error
2958 * Called with iothread lock
2960 * @f: QEMUFile where to send the data
2961 * @opaque: RAMState pointer
2963 static int ram_save_complete(QEMUFile
*f
, void *opaque
)
2965 RAMState
**temp
= opaque
;
2966 RAMState
*rs
= *temp
;
2969 WITH_RCU_READ_LOCK_GUARD() {
2970 if (!migration_in_postcopy()) {
2971 migration_bitmap_sync_precopy(rs
);
2974 ram_control_before_iterate(f
, RAM_CONTROL_FINISH
);
2976 /* try transferring iterative blocks of memory */
2978 /* flush all remaining blocks regardless of rate limiting */
2982 pages
= ram_find_and_save_block(rs
, !migration_in_colo_state());
2983 /* no more blocks to sent */
2993 flush_compressed_data(rs
);
2994 ram_control_after_iterate(f
, RAM_CONTROL_FINISH
);
2998 multifd_send_sync_main(rs
->f
);
2999 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3006 static void ram_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
,
3007 uint64_t *res_precopy_only
,
3008 uint64_t *res_compatible
,
3009 uint64_t *res_postcopy_only
)
3011 RAMState
**temp
= opaque
;
3012 RAMState
*rs
= *temp
;
3013 uint64_t remaining_size
;
3015 remaining_size
= rs
->migration_dirty_pages
* TARGET_PAGE_SIZE
;
3017 if (!migration_in_postcopy() &&
3018 remaining_size
< max_size
) {
3019 qemu_mutex_lock_iothread();
3020 WITH_RCU_READ_LOCK_GUARD() {
3021 migration_bitmap_sync_precopy(rs
);
3023 qemu_mutex_unlock_iothread();
3024 remaining_size
= rs
->migration_dirty_pages
* TARGET_PAGE_SIZE
;
3027 if (migrate_postcopy_ram()) {
3028 /* We can do postcopy, and all the data is postcopiable */
3029 *res_compatible
+= remaining_size
;
3031 *res_precopy_only
+= remaining_size
;
3035 static int load_xbzrle(QEMUFile
*f
, ram_addr_t addr
, void *host
)
3037 unsigned int xh_len
;
3039 uint8_t *loaded_data
;
3041 /* extract RLE header */
3042 xh_flags
= qemu_get_byte(f
);
3043 xh_len
= qemu_get_be16(f
);
3045 if (xh_flags
!= ENCODING_FLAG_XBZRLE
) {
3046 error_report("Failed to load XBZRLE page - wrong compression!");
3050 if (xh_len
> TARGET_PAGE_SIZE
) {
3051 error_report("Failed to load XBZRLE page - len overflow!");
3054 loaded_data
= XBZRLE
.decoded_buf
;
3055 /* load data and decode */
3056 /* it can change loaded_data to point to an internal buffer */
3057 qemu_get_buffer_in_place(f
, &loaded_data
, xh_len
);
3060 if (xbzrle_decode_buffer(loaded_data
, xh_len
, host
,
3061 TARGET_PAGE_SIZE
) == -1) {
3062 error_report("Failed to load XBZRLE page - decode error!");
3070 * ram_block_from_stream: read a RAMBlock id from the migration stream
3072 * Must be called from within a rcu critical section.
3074 * Returns a pointer from within the RCU-protected ram_list.
3076 * @f: QEMUFile where to read the data from
3077 * @flags: Page flags (mostly to see if it's a continuation of previous block)
3079 static inline RAMBlock
*ram_block_from_stream(QEMUFile
*f
, int flags
)
3081 static RAMBlock
*block
;
3085 if (flags
& RAM_SAVE_FLAG_CONTINUE
) {
3087 error_report("Ack, bad migration stream!");
3093 len
= qemu_get_byte(f
);
3094 qemu_get_buffer(f
, (uint8_t *)id
, len
);
3097 block
= qemu_ram_block_by_name(id
);
3099 error_report("Can't find block %s", id
);
3103 if (ramblock_is_ignored(block
)) {
3104 error_report("block %s should not be migrated !", id
);
3111 static inline void *host_from_ram_block_offset(RAMBlock
*block
,
3114 if (!offset_in_ramblock(block
, offset
)) {
3118 return block
->host
+ offset
;
3121 static inline void *colo_cache_from_block_offset(RAMBlock
*block
,
3122 ram_addr_t offset
, bool record_bitmap
)
3124 if (!offset_in_ramblock(block
, offset
)) {
3127 if (!block
->colo_cache
) {
3128 error_report("%s: colo_cache is NULL in block :%s",
3129 __func__
, block
->idstr
);
3134 * During colo checkpoint, we need bitmap of these migrated pages.
3135 * It help us to decide which pages in ram cache should be flushed
3136 * into VM's RAM later.
3138 if (record_bitmap
&&
3139 !test_and_set_bit(offset
>> TARGET_PAGE_BITS
, block
->bmap
)) {
3140 ram_state
->migration_dirty_pages
++;
3142 return block
->colo_cache
+ offset
;
3146 * ram_handle_compressed: handle the zero page case
3148 * If a page (or a whole RDMA chunk) has been
3149 * determined to be zero, then zap it.
3151 * @host: host address for the zero page
3152 * @ch: what the page is filled from. We only support zero
3153 * @size: size of the zero page
3155 void ram_handle_compressed(void *host
, uint8_t ch
, uint64_t size
)
3157 if (ch
!= 0 || !is_zero_range(host
, size
)) {
3158 memset(host
, ch
, size
);
3162 /* return the size after decompression, or negative value on error */
3164 qemu_uncompress_data(z_stream
*stream
, uint8_t *dest
, size_t dest_len
,
3165 const uint8_t *source
, size_t source_len
)
3169 err
= inflateReset(stream
);
3174 stream
->avail_in
= source_len
;
3175 stream
->next_in
= (uint8_t *)source
;
3176 stream
->avail_out
= dest_len
;
3177 stream
->next_out
= dest
;
3179 err
= inflate(stream
, Z_NO_FLUSH
);
3180 if (err
!= Z_STREAM_END
) {
3184 return stream
->total_out
;
3187 static void *do_data_decompress(void *opaque
)
3189 DecompressParam
*param
= opaque
;
3190 unsigned long pagesize
;
3194 qemu_mutex_lock(¶m
->mutex
);
3195 while (!param
->quit
) {
3200 qemu_mutex_unlock(¶m
->mutex
);
3202 pagesize
= TARGET_PAGE_SIZE
;
3204 ret
= qemu_uncompress_data(¶m
->stream
, des
, pagesize
,
3205 param
->compbuf
, len
);
3206 if (ret
< 0 && migrate_get_current()->decompress_error_check
) {
3207 error_report("decompress data failed");
3208 qemu_file_set_error(decomp_file
, ret
);
3211 qemu_mutex_lock(&decomp_done_lock
);
3213 qemu_cond_signal(&decomp_done_cond
);
3214 qemu_mutex_unlock(&decomp_done_lock
);
3216 qemu_mutex_lock(¶m
->mutex
);
3218 qemu_cond_wait(¶m
->cond
, ¶m
->mutex
);
3221 qemu_mutex_unlock(¶m
->mutex
);
3226 static int wait_for_decompress_done(void)
3228 int idx
, thread_count
;
3230 if (!migrate_use_compression()) {
3234 thread_count
= migrate_decompress_threads();
3235 qemu_mutex_lock(&decomp_done_lock
);
3236 for (idx
= 0; idx
< thread_count
; idx
++) {
3237 while (!decomp_param
[idx
].done
) {
3238 qemu_cond_wait(&decomp_done_cond
, &decomp_done_lock
);
3241 qemu_mutex_unlock(&decomp_done_lock
);
3242 return qemu_file_get_error(decomp_file
);
3245 static void compress_threads_load_cleanup(void)
3247 int i
, thread_count
;
3249 if (!migrate_use_compression()) {
3252 thread_count
= migrate_decompress_threads();
3253 for (i
= 0; i
< thread_count
; i
++) {
3255 * we use it as a indicator which shows if the thread is
3256 * properly init'd or not
3258 if (!decomp_param
[i
].compbuf
) {
3262 qemu_mutex_lock(&decomp_param
[i
].mutex
);
3263 decomp_param
[i
].quit
= true;
3264 qemu_cond_signal(&decomp_param
[i
].cond
);
3265 qemu_mutex_unlock(&decomp_param
[i
].mutex
);
3267 for (i
= 0; i
< thread_count
; i
++) {
3268 if (!decomp_param
[i
].compbuf
) {
3272 qemu_thread_join(decompress_threads
+ i
);
3273 qemu_mutex_destroy(&decomp_param
[i
].mutex
);
3274 qemu_cond_destroy(&decomp_param
[i
].cond
);
3275 inflateEnd(&decomp_param
[i
].stream
);
3276 g_free(decomp_param
[i
].compbuf
);
3277 decomp_param
[i
].compbuf
= NULL
;
3279 g_free(decompress_threads
);
3280 g_free(decomp_param
);
3281 decompress_threads
= NULL
;
3282 decomp_param
= NULL
;
3286 static int compress_threads_load_setup(QEMUFile
*f
)
3288 int i
, thread_count
;
3290 if (!migrate_use_compression()) {
3294 thread_count
= migrate_decompress_threads();
3295 decompress_threads
= g_new0(QemuThread
, thread_count
);
3296 decomp_param
= g_new0(DecompressParam
, thread_count
);
3297 qemu_mutex_init(&decomp_done_lock
);
3298 qemu_cond_init(&decomp_done_cond
);
3300 for (i
= 0; i
< thread_count
; i
++) {
3301 if (inflateInit(&decomp_param
[i
].stream
) != Z_OK
) {
3305 decomp_param
[i
].compbuf
= g_malloc0(compressBound(TARGET_PAGE_SIZE
));
3306 qemu_mutex_init(&decomp_param
[i
].mutex
);
3307 qemu_cond_init(&decomp_param
[i
].cond
);
3308 decomp_param
[i
].done
= true;
3309 decomp_param
[i
].quit
= false;
3310 qemu_thread_create(decompress_threads
+ i
, "decompress",
3311 do_data_decompress
, decomp_param
+ i
,
3312 QEMU_THREAD_JOINABLE
);
3316 compress_threads_load_cleanup();
3320 static void decompress_data_with_multi_threads(QEMUFile
*f
,
3321 void *host
, int len
)
3323 int idx
, thread_count
;
3325 thread_count
= migrate_decompress_threads();
3326 QEMU_LOCK_GUARD(&decomp_done_lock
);
3328 for (idx
= 0; idx
< thread_count
; idx
++) {
3329 if (decomp_param
[idx
].done
) {
3330 decomp_param
[idx
].done
= false;
3331 qemu_mutex_lock(&decomp_param
[idx
].mutex
);
3332 qemu_get_buffer(f
, decomp_param
[idx
].compbuf
, len
);
3333 decomp_param
[idx
].des
= host
;
3334 decomp_param
[idx
].len
= len
;
3335 qemu_cond_signal(&decomp_param
[idx
].cond
);
3336 qemu_mutex_unlock(&decomp_param
[idx
].mutex
);
3340 if (idx
< thread_count
) {
3343 qemu_cond_wait(&decomp_done_cond
, &decomp_done_lock
);
3349 * we must set ram_bulk_stage to false, otherwise in
3350 * migation_bitmap_find_dirty the bitmap will be unused and
3351 * all the pages in ram cache wil be flushed to the ram of
3354 static void colo_init_ram_state(void)
3356 ram_state_init(&ram_state
);
3357 ram_state
->ram_bulk_stage
= false;
3361 * colo cache: this is for secondary VM, we cache the whole
3362 * memory of the secondary VM, it is need to hold the global lock
3363 * to call this helper.
3365 int colo_init_ram_cache(void)
3369 WITH_RCU_READ_LOCK_GUARD() {
3370 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3371 block
->colo_cache
= qemu_anon_ram_alloc(block
->used_length
,
3374 if (!block
->colo_cache
) {
3375 error_report("%s: Can't alloc memory for COLO cache of block %s,"
3376 "size 0x" RAM_ADDR_FMT
, __func__
, block
->idstr
,
3377 block
->used_length
);
3378 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3379 if (block
->colo_cache
) {
3380 qemu_anon_ram_free(block
->colo_cache
, block
->used_length
);
3381 block
->colo_cache
= NULL
;
3390 * Record the dirty pages that sent by PVM, we use this dirty bitmap together
3391 * with to decide which page in cache should be flushed into SVM's RAM. Here
3392 * we use the same name 'ram_bitmap' as for migration.
3394 if (ram_bytes_total()) {
3397 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3398 unsigned long pages
= block
->max_length
>> TARGET_PAGE_BITS
;
3399 block
->bmap
= bitmap_new(pages
);
3403 colo_init_ram_state();
3407 /* TODO: duplicated with ram_init_bitmaps */
3408 void colo_incoming_start_dirty_log(void)
3410 RAMBlock
*block
= NULL
;
3411 /* For memory_global_dirty_log_start below. */
3412 qemu_mutex_lock_iothread();
3413 qemu_mutex_lock_ramlist();
3415 memory_global_dirty_log_sync();
3416 WITH_RCU_READ_LOCK_GUARD() {
3417 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3418 ramblock_sync_dirty_bitmap(ram_state
, block
);
3419 /* Discard this dirty bitmap record */
3420 bitmap_zero(block
->bmap
, block
->max_length
>> TARGET_PAGE_BITS
);
3422 memory_global_dirty_log_start();
3424 ram_state
->migration_dirty_pages
= 0;
3425 qemu_mutex_unlock_ramlist();
3426 qemu_mutex_unlock_iothread();
3429 /* It is need to hold the global lock to call this helper */
3430 void colo_release_ram_cache(void)
3434 memory_global_dirty_log_stop();
3435 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3436 g_free(block
->bmap
);
3440 WITH_RCU_READ_LOCK_GUARD() {
3441 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3442 if (block
->colo_cache
) {
3443 qemu_anon_ram_free(block
->colo_cache
, block
->used_length
);
3444 block
->colo_cache
= NULL
;
3448 ram_state_cleanup(&ram_state
);
3452 * ram_load_setup: Setup RAM for migration incoming side
3454 * Returns zero to indicate success and negative for error
3456 * @f: QEMUFile where to receive the data
3457 * @opaque: RAMState pointer
3459 static int ram_load_setup(QEMUFile
*f
, void *opaque
)
3461 if (compress_threads_load_setup(f
)) {
3465 xbzrle_load_setup();
3466 ramblock_recv_map_init();
3471 static int ram_load_cleanup(void *opaque
)
3475 RAMBLOCK_FOREACH_NOT_IGNORED(rb
) {
3476 qemu_ram_block_writeback(rb
);
3479 xbzrle_load_cleanup();
3480 compress_threads_load_cleanup();
3482 RAMBLOCK_FOREACH_NOT_IGNORED(rb
) {
3483 g_free(rb
->receivedmap
);
3484 rb
->receivedmap
= NULL
;
3491 * ram_postcopy_incoming_init: allocate postcopy data structures
3493 * Returns 0 for success and negative if there was one error
3495 * @mis: current migration incoming state
3497 * Allocate data structures etc needed by incoming migration with
3498 * postcopy-ram. postcopy-ram's similarly names
3499 * postcopy_ram_incoming_init does the work.
3501 int ram_postcopy_incoming_init(MigrationIncomingState
*mis
)
3503 return postcopy_ram_incoming_init(mis
);
3507 * ram_load_postcopy: load a page in postcopy case
3509 * Returns 0 for success or -errno in case of error
3511 * Called in postcopy mode by ram_load().
3512 * rcu_read_lock is taken prior to this being called.
3514 * @f: QEMUFile where to send the data
3516 static int ram_load_postcopy(QEMUFile
*f
)
3518 int flags
= 0, ret
= 0;
3519 bool place_needed
= false;
3520 bool matches_target_page_size
= false;
3521 MigrationIncomingState
*mis
= migration_incoming_get_current();
3522 /* Temporary page that is later 'placed' */
3523 void *postcopy_host_page
= mis
->postcopy_tmp_page
;
3524 void *this_host
= NULL
;
3525 bool all_zero
= true;
3526 int target_pages
= 0;
3528 while (!ret
&& !(flags
& RAM_SAVE_FLAG_EOS
)) {
3531 void *page_buffer
= NULL
;
3532 void *place_source
= NULL
;
3533 RAMBlock
*block
= NULL
;
3537 addr
= qemu_get_be64(f
);
3540 * If qemu file error, we should stop here, and then "addr"
3543 ret
= qemu_file_get_error(f
);
3548 flags
= addr
& ~TARGET_PAGE_MASK
;
3549 addr
&= TARGET_PAGE_MASK
;
3551 trace_ram_load_postcopy_loop((uint64_t)addr
, flags
);
3552 if (flags
& (RAM_SAVE_FLAG_ZERO
| RAM_SAVE_FLAG_PAGE
|
3553 RAM_SAVE_FLAG_COMPRESS_PAGE
)) {
3554 block
= ram_block_from_stream(f
, flags
);
3556 host
= host_from_ram_block_offset(block
, addr
);
3558 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
3563 matches_target_page_size
= block
->page_size
== TARGET_PAGE_SIZE
;
3565 * Postcopy requires that we place whole host pages atomically;
3566 * these may be huge pages for RAMBlocks that are backed by
3568 * To make it atomic, the data is read into a temporary page
3569 * that's moved into place later.
3570 * The migration protocol uses, possibly smaller, target-pages
3571 * however the source ensures it always sends all the components
3572 * of a host page in one chunk.
3574 page_buffer
= postcopy_host_page
+
3575 ((uintptr_t)host
& (block
->page_size
- 1));
3576 if (target_pages
== 1) {
3577 this_host
= (void *)QEMU_ALIGN_DOWN((uintptr_t)host
,
3580 /* not the 1st TP within the HP */
3581 if (QEMU_ALIGN_DOWN((uintptr_t)host
, block
->page_size
) !=
3582 (uintptr_t)this_host
) {
3583 error_report("Non-same host page %p/%p",
3591 * If it's the last part of a host page then we place the host
3594 if (target_pages
== (block
->page_size
/ TARGET_PAGE_SIZE
)) {
3595 place_needed
= true;
3597 place_source
= postcopy_host_page
;
3600 switch (flags
& ~RAM_SAVE_FLAG_CONTINUE
) {
3601 case RAM_SAVE_FLAG_ZERO
:
3602 ch
= qemu_get_byte(f
);
3604 * Can skip to set page_buffer when
3605 * this is a zero page and (block->page_size == TARGET_PAGE_SIZE).
3607 if (ch
|| !matches_target_page_size
) {
3608 memset(page_buffer
, ch
, TARGET_PAGE_SIZE
);
3615 case RAM_SAVE_FLAG_PAGE
:
3617 if (!matches_target_page_size
) {
3618 /* For huge pages, we always use temporary buffer */
3619 qemu_get_buffer(f
, page_buffer
, TARGET_PAGE_SIZE
);
3622 * For small pages that matches target page size, we
3623 * avoid the qemu_file copy. Instead we directly use
3624 * the buffer of QEMUFile to place the page. Note: we
3625 * cannot do any QEMUFile operation before using that
3626 * buffer to make sure the buffer is valid when
3629 qemu_get_buffer_in_place(f
, (uint8_t **)&place_source
,
3633 case RAM_SAVE_FLAG_COMPRESS_PAGE
:
3635 len
= qemu_get_be32(f
);
3636 if (len
< 0 || len
> compressBound(TARGET_PAGE_SIZE
)) {
3637 error_report("Invalid compressed data length: %d", len
);
3641 decompress_data_with_multi_threads(f
, page_buffer
, len
);
3644 case RAM_SAVE_FLAG_EOS
:
3646 multifd_recv_sync_main();
3649 error_report("Unknown combination of migration flags: 0x%x"
3650 " (postcopy mode)", flags
);
3655 /* Got the whole host page, wait for decompress before placing. */
3657 ret
|= wait_for_decompress_done();
3660 /* Detect for any possible file errors */
3661 if (!ret
&& qemu_file_get_error(f
)) {
3662 ret
= qemu_file_get_error(f
);
3665 if (!ret
&& place_needed
) {
3666 /* This gets called at the last target page in the host page */
3667 void *place_dest
= (void *)QEMU_ALIGN_DOWN((uintptr_t)host
,
3671 ret
= postcopy_place_page_zero(mis
, place_dest
,
3674 ret
= postcopy_place_page(mis
, place_dest
,
3675 place_source
, block
);
3677 place_needed
= false;
3679 /* Assume we have a zero page until we detect something different */
3687 static bool postcopy_is_advised(void)
3689 PostcopyState ps
= postcopy_state_get();
3690 return ps
>= POSTCOPY_INCOMING_ADVISE
&& ps
< POSTCOPY_INCOMING_END
;
3693 static bool postcopy_is_running(void)
3695 PostcopyState ps
= postcopy_state_get();
3696 return ps
>= POSTCOPY_INCOMING_LISTENING
&& ps
< POSTCOPY_INCOMING_END
;
3700 * Flush content of RAM cache into SVM's memory.
3701 * Only flush the pages that be dirtied by PVM or SVM or both.
3703 void colo_flush_ram_cache(void)
3705 RAMBlock
*block
= NULL
;
3708 unsigned long offset
= 0;
3710 memory_global_dirty_log_sync();
3711 WITH_RCU_READ_LOCK_GUARD() {
3712 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
3713 ramblock_sync_dirty_bitmap(ram_state
, block
);
3717 trace_colo_flush_ram_cache_begin(ram_state
->migration_dirty_pages
);
3718 WITH_RCU_READ_LOCK_GUARD() {
3719 block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
3722 offset
= migration_bitmap_find_dirty(ram_state
, block
, offset
);
3724 if (((ram_addr_t
)offset
) << TARGET_PAGE_BITS
3725 >= block
->used_length
) {
3727 block
= QLIST_NEXT_RCU(block
, next
);
3729 migration_bitmap_clear_dirty(ram_state
, block
, offset
);
3730 dst_host
= block
->host
3731 + (((ram_addr_t
)offset
) << TARGET_PAGE_BITS
);
3732 src_host
= block
->colo_cache
3733 + (((ram_addr_t
)offset
) << TARGET_PAGE_BITS
);
3734 memcpy(dst_host
, src_host
, TARGET_PAGE_SIZE
);
3738 trace_colo_flush_ram_cache_end();
3742 * ram_load_precopy: load pages in precopy case
3744 * Returns 0 for success or -errno in case of error
3746 * Called in precopy mode by ram_load().
3747 * rcu_read_lock is taken prior to this being called.
3749 * @f: QEMUFile where to send the data
3751 static int ram_load_precopy(QEMUFile
*f
)
3753 int flags
= 0, ret
= 0, invalid_flags
= 0, len
= 0, i
= 0;
3754 /* ADVISE is earlier, it shows the source has the postcopy capability on */
3755 bool postcopy_advised
= postcopy_is_advised();
3756 if (!migrate_use_compression()) {
3757 invalid_flags
|= RAM_SAVE_FLAG_COMPRESS_PAGE
;
3760 while (!ret
&& !(flags
& RAM_SAVE_FLAG_EOS
)) {
3761 ram_addr_t addr
, total_ram_bytes
;
3762 void *host
= NULL
, *host_bak
= NULL
;
3766 * Yield periodically to let main loop run, but an iteration of
3767 * the main loop is expensive, so do it each some iterations
3769 if ((i
& 32767) == 0 && qemu_in_coroutine()) {
3770 aio_co_schedule(qemu_get_current_aio_context(),
3771 qemu_coroutine_self());
3772 qemu_coroutine_yield();
3776 addr
= qemu_get_be64(f
);
3777 flags
= addr
& ~TARGET_PAGE_MASK
;
3778 addr
&= TARGET_PAGE_MASK
;
3780 if (flags
& invalid_flags
) {
3781 if (flags
& invalid_flags
& RAM_SAVE_FLAG_COMPRESS_PAGE
) {
3782 error_report("Received an unexpected compressed page");
3789 if (flags
& (RAM_SAVE_FLAG_ZERO
| RAM_SAVE_FLAG_PAGE
|
3790 RAM_SAVE_FLAG_COMPRESS_PAGE
| RAM_SAVE_FLAG_XBZRLE
)) {
3791 RAMBlock
*block
= ram_block_from_stream(f
, flags
);
3793 host
= host_from_ram_block_offset(block
, addr
);
3795 * After going into COLO stage, we should not load the page
3796 * into SVM's memory directly, we put them into colo_cache firstly.
3797 * NOTE: We need to keep a copy of SVM's ram in colo_cache.
3798 * Previously, we copied all these memory in preparing stage of COLO
3799 * while we need to stop VM, which is a time-consuming process.
3800 * Here we optimize it by a trick, back-up every page while in
3801 * migration process while COLO is enabled, though it affects the
3802 * speed of the migration, but it obviously reduce the downtime of
3803 * back-up all SVM'S memory in COLO preparing stage.
3805 if (migration_incoming_colo_enabled()) {
3806 if (migration_incoming_in_colo_state()) {
3807 /* In COLO stage, put all pages into cache temporarily */
3808 host
= colo_cache_from_block_offset(block
, addr
, true);
3811 * In migration stage but before COLO stage,
3812 * Put all pages into both cache and SVM's memory.
3814 host_bak
= colo_cache_from_block_offset(block
, addr
, false);
3818 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
3822 if (!migration_incoming_in_colo_state()) {
3823 ramblock_recv_bitmap_set(block
, host
);
3826 trace_ram_load_loop(block
->idstr
, (uint64_t)addr
, flags
, host
);
3829 switch (flags
& ~RAM_SAVE_FLAG_CONTINUE
) {
3830 case RAM_SAVE_FLAG_MEM_SIZE
:
3831 /* Synchronize RAM block list */
3832 total_ram_bytes
= addr
;
3833 while (!ret
&& total_ram_bytes
) {
3838 len
= qemu_get_byte(f
);
3839 qemu_get_buffer(f
, (uint8_t *)id
, len
);
3841 length
= qemu_get_be64(f
);
3843 block
= qemu_ram_block_by_name(id
);
3844 if (block
&& !qemu_ram_is_migratable(block
)) {
3845 error_report("block %s should not be migrated !", id
);
3848 if (length
!= block
->used_length
) {
3849 Error
*local_err
= NULL
;
3851 ret
= qemu_ram_resize(block
, length
,
3854 error_report_err(local_err
);
3857 /* For postcopy we need to check hugepage sizes match */
3858 if (postcopy_advised
&& migrate_postcopy_ram() &&
3859 block
->page_size
!= qemu_host_page_size
) {
3860 uint64_t remote_page_size
= qemu_get_be64(f
);
3861 if (remote_page_size
!= block
->page_size
) {
3862 error_report("Mismatched RAM page size %s "
3863 "(local) %zd != %" PRId64
,
3864 id
, block
->page_size
,
3869 if (migrate_ignore_shared()) {
3870 hwaddr addr
= qemu_get_be64(f
);
3871 if (ramblock_is_ignored(block
) &&
3872 block
->mr
->addr
!= addr
) {
3873 error_report("Mismatched GPAs for block %s "
3874 "%" PRId64
"!= %" PRId64
,
3876 (uint64_t)block
->mr
->addr
);
3880 ram_control_load_hook(f
, RAM_CONTROL_BLOCK_REG
,
3883 error_report("Unknown ramblock \"%s\", cannot "
3884 "accept migration", id
);
3888 total_ram_bytes
-= length
;
3892 case RAM_SAVE_FLAG_ZERO
:
3893 ch
= qemu_get_byte(f
);
3894 ram_handle_compressed(host
, ch
, TARGET_PAGE_SIZE
);
3897 case RAM_SAVE_FLAG_PAGE
:
3898 qemu_get_buffer(f
, host
, TARGET_PAGE_SIZE
);
3901 case RAM_SAVE_FLAG_COMPRESS_PAGE
:
3902 len
= qemu_get_be32(f
);
3903 if (len
< 0 || len
> compressBound(TARGET_PAGE_SIZE
)) {
3904 error_report("Invalid compressed data length: %d", len
);
3908 decompress_data_with_multi_threads(f
, host
, len
);
3911 case RAM_SAVE_FLAG_XBZRLE
:
3912 if (load_xbzrle(f
, addr
, host
) < 0) {
3913 error_report("Failed to decompress XBZRLE page at "
3914 RAM_ADDR_FMT
, addr
);
3919 case RAM_SAVE_FLAG_EOS
:
3921 multifd_recv_sync_main();
3924 if (flags
& RAM_SAVE_FLAG_HOOK
) {
3925 ram_control_load_hook(f
, RAM_CONTROL_HOOK
, NULL
);
3927 error_report("Unknown combination of migration flags: 0x%x",
3933 ret
= qemu_file_get_error(f
);
3935 if (!ret
&& host_bak
) {
3936 memcpy(host_bak
, host
, TARGET_PAGE_SIZE
);
3940 ret
|= wait_for_decompress_done();
3944 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3947 static uint64_t seq_iter
;
3949 * If system is running in postcopy mode, page inserts to host memory must
3952 bool postcopy_running
= postcopy_is_running();
3956 if (version_id
!= 4) {
3961 * This RCU critical section can be very long running.
3962 * When RCU reclaims in the code start to become numerous,
3963 * it will be necessary to reduce the granularity of this
3966 WITH_RCU_READ_LOCK_GUARD() {
3967 if (postcopy_running
) {
3968 ret
= ram_load_postcopy(f
);
3970 ret
= ram_load_precopy(f
);
3973 trace_ram_load_complete(ret
, seq_iter
);
3978 static bool ram_has_postcopy(void *opaque
)
3981 RAMBLOCK_FOREACH_NOT_IGNORED(rb
) {
3982 if (ramblock_is_pmem(rb
)) {
3983 info_report("Block: %s, host: %p is a nvdimm memory, postcopy"
3984 "is not supported now!", rb
->idstr
, rb
->host
);
3989 return migrate_postcopy_ram();
3992 /* Sync all the dirty bitmap with destination VM. */
3993 static int ram_dirty_bitmap_sync_all(MigrationState
*s
, RAMState
*rs
)
3996 QEMUFile
*file
= s
->to_dst_file
;
3997 int ramblock_count
= 0;
3999 trace_ram_dirty_bitmap_sync_start();
4001 RAMBLOCK_FOREACH_NOT_IGNORED(block
) {
4002 qemu_savevm_send_recv_bitmap(file
, block
->idstr
);
4003 trace_ram_dirty_bitmap_request(block
->idstr
);
4007 trace_ram_dirty_bitmap_sync_wait();
4009 /* Wait until all the ramblocks' dirty bitmap synced */
4010 while (ramblock_count
--) {
4011 qemu_sem_wait(&s
->rp_state
.rp_sem
);
4014 trace_ram_dirty_bitmap_sync_complete();
4019 static void ram_dirty_bitmap_reload_notify(MigrationState
*s
)
4021 qemu_sem_post(&s
->rp_state
.rp_sem
);
4025 * Read the received bitmap, revert it as the initial dirty bitmap.
4026 * This is only used when the postcopy migration is paused but wants
4027 * to resume from a middle point.
4029 int ram_dirty_bitmap_reload(MigrationState
*s
, RAMBlock
*block
)
4032 QEMUFile
*file
= s
->rp_state
.from_dst_file
;
4033 unsigned long *le_bitmap
, nbits
= block
->used_length
>> TARGET_PAGE_BITS
;
4034 uint64_t local_size
= DIV_ROUND_UP(nbits
, 8);
4035 uint64_t size
, end_mark
;
4037 trace_ram_dirty_bitmap_reload_begin(block
->idstr
);
4039 if (s
->state
!= MIGRATION_STATUS_POSTCOPY_RECOVER
) {
4040 error_report("%s: incorrect state %s", __func__
,
4041 MigrationStatus_str(s
->state
));
4046 * Note: see comments in ramblock_recv_bitmap_send() on why we
4047 * need the endianness conversion, and the paddings.
4049 local_size
= ROUND_UP(local_size
, 8);
4052 le_bitmap
= bitmap_new(nbits
+ BITS_PER_LONG
);
4054 size
= qemu_get_be64(file
);
4056 /* The size of the bitmap should match with our ramblock */
4057 if (size
!= local_size
) {
4058 error_report("%s: ramblock '%s' bitmap size mismatch "
4059 "(0x%"PRIx64
" != 0x%"PRIx64
")", __func__
,
4060 block
->idstr
, size
, local_size
);
4065 size
= qemu_get_buffer(file
, (uint8_t *)le_bitmap
, local_size
);
4066 end_mark
= qemu_get_be64(file
);
4068 ret
= qemu_file_get_error(file
);
4069 if (ret
|| size
!= local_size
) {
4070 error_report("%s: read bitmap failed for ramblock '%s': %d"
4071 " (size 0x%"PRIx64
", got: 0x%"PRIx64
")",
4072 __func__
, block
->idstr
, ret
, local_size
, size
);
4077 if (end_mark
!= RAMBLOCK_RECV_BITMAP_ENDING
) {
4078 error_report("%s: ramblock '%s' end mark incorrect: 0x%"PRIx64
,
4079 __func__
, block
->idstr
, end_mark
);
4085 * Endianness conversion. We are during postcopy (though paused).
4086 * The dirty bitmap won't change. We can directly modify it.
4088 bitmap_from_le(block
->bmap
, le_bitmap
, nbits
);
4091 * What we received is "received bitmap". Revert it as the initial
4092 * dirty bitmap for this ramblock.
4094 bitmap_complement(block
->bmap
, block
->bmap
, nbits
);
4096 trace_ram_dirty_bitmap_reload_complete(block
->idstr
);
4099 * We succeeded to sync bitmap for current ramblock. If this is
4100 * the last one to sync, we need to notify the main send thread.
4102 ram_dirty_bitmap_reload_notify(s
);
4110 static int ram_resume_prepare(MigrationState
*s
, void *opaque
)
4112 RAMState
*rs
= *(RAMState
**)opaque
;
4115 ret
= ram_dirty_bitmap_sync_all(s
, rs
);
4120 ram_state_resume_prepare(rs
, s
->to_dst_file
);
4125 static SaveVMHandlers savevm_ram_handlers
= {
4126 .save_setup
= ram_save_setup
,
4127 .save_live_iterate
= ram_save_iterate
,
4128 .save_live_complete_postcopy
= ram_save_complete
,
4129 .save_live_complete_precopy
= ram_save_complete
,
4130 .has_postcopy
= ram_has_postcopy
,
4131 .save_live_pending
= ram_save_pending
,
4132 .load_state
= ram_load
,
4133 .save_cleanup
= ram_save_cleanup
,
4134 .load_setup
= ram_load_setup
,
4135 .load_cleanup
= ram_load_cleanup
,
4136 .resume_prepare
= ram_resume_prepare
,
4139 void ram_mig_init(void)
4141 qemu_mutex_init(&XBZRLE
.lock
);
4142 register_savevm_live("ram", 0, 4, &savevm_ram_handlers
, &ram_state
);