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
28 #include "qemu/osdep.h"
31 #include "qapi-event.h"
32 #include "qemu/cutils.h"
33 #include "qemu/bitops.h"
34 #include "qemu/bitmap.h"
35 #include "qemu/main-loop.h"
38 #include "migration.h"
39 #include "migration/register.h"
40 #include "migration/misc.h"
41 #include "qemu-file.h"
42 #include "postcopy-ram.h"
43 #include "migration/page_cache.h"
44 #include "qemu/error-report.h"
46 #include "exec/ram_addr.h"
47 #include "qemu/rcu_queue.h"
48 #include "migration/colo.h"
50 /***********************************************************/
51 /* ram save/restore */
53 /* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it
54 * worked for pages that where filled with the same char. We switched
55 * it to only search for the zero value. And to avoid confusion with
56 * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it.
59 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
60 #define RAM_SAVE_FLAG_ZERO 0x02
61 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
62 #define RAM_SAVE_FLAG_PAGE 0x08
63 #define RAM_SAVE_FLAG_EOS 0x10
64 #define RAM_SAVE_FLAG_CONTINUE 0x20
65 #define RAM_SAVE_FLAG_XBZRLE 0x40
66 /* 0x80 is reserved in migration.h start with 0x100 next */
67 #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
69 static inline bool is_zero_range(uint8_t *p
, uint64_t size
)
71 return buffer_is_zero(p
, size
);
74 XBZRLECacheStats xbzrle_counters
;
76 /* struct contains XBZRLE cache and a static page
77 used by the compression */
79 /* buffer used for XBZRLE encoding */
81 /* buffer for storing page content */
83 /* Cache for XBZRLE, Protected by lock. */
86 /* it will store a page full of zeros */
87 uint8_t *zero_target_page
;
88 /* buffer used for XBZRLE decoding */
92 static void XBZRLE_cache_lock(void)
94 if (migrate_use_xbzrle())
95 qemu_mutex_lock(&XBZRLE
.lock
);
98 static void XBZRLE_cache_unlock(void)
100 if (migrate_use_xbzrle())
101 qemu_mutex_unlock(&XBZRLE
.lock
);
105 * xbzrle_cache_resize: resize the xbzrle cache
107 * This function is called from qmp_migrate_set_cache_size in main
108 * thread, possibly while a migration is in progress. A running
109 * migration may be using the cache and might finish during this call,
110 * hence changes to the cache are protected by XBZRLE.lock().
112 * Returns the new_size or negative in case of error.
114 * @new_size: new cache size
116 int64_t xbzrle_cache_resize(int64_t new_size
)
118 PageCache
*new_cache
;
121 if (new_size
< TARGET_PAGE_SIZE
) {
127 if (XBZRLE
.cache
!= NULL
) {
128 if (pow2floor(new_size
) == migrate_xbzrle_cache_size()) {
131 new_cache
= cache_init(new_size
/ TARGET_PAGE_SIZE
,
134 error_report("Error creating cache");
139 cache_fini(XBZRLE
.cache
);
140 XBZRLE
.cache
= new_cache
;
144 ret
= pow2floor(new_size
);
146 XBZRLE_cache_unlock();
151 * An outstanding page request, on the source, having been received
154 struct RAMSrcPageRequest
{
159 QSIMPLEQ_ENTRY(RAMSrcPageRequest
) next_req
;
162 /* State of RAM for migration */
164 /* QEMUFile used for this migration */
166 /* Last block that we have visited searching for dirty pages */
167 RAMBlock
*last_seen_block
;
168 /* Last block from where we have sent data */
169 RAMBlock
*last_sent_block
;
170 /* Last dirty target page we have sent */
171 ram_addr_t last_page
;
172 /* last ram version we have seen */
173 uint32_t last_version
;
174 /* We are in the first round */
176 /* How many times we have dirty too many pages */
177 int dirty_rate_high_cnt
;
178 /* these variables are used for bitmap sync */
179 /* last time we did a full bitmap_sync */
180 int64_t time_last_bitmap_sync
;
181 /* bytes transferred at start_time */
182 uint64_t bytes_xfer_prev
;
183 /* number of dirty pages since start_time */
184 uint64_t num_dirty_pages_period
;
185 /* xbzrle misses since the beginning of the period */
186 uint64_t xbzrle_cache_miss_prev
;
187 /* number of iterations at the beginning of period */
188 uint64_t iterations_prev
;
189 /* Iterations since start */
191 /* protects modification of the bitmap */
192 uint64_t migration_dirty_pages
;
193 /* number of dirty bits in the bitmap */
194 QemuMutex bitmap_mutex
;
195 /* The RAMBlock used in the last src_page_requests */
196 RAMBlock
*last_req_rb
;
197 /* Queue of outstanding page requests from the destination */
198 QemuMutex src_page_req_mutex
;
199 QSIMPLEQ_HEAD(src_page_requests
, RAMSrcPageRequest
) src_page_requests
;
201 typedef struct RAMState RAMState
;
203 static RAMState
*ram_state
;
205 uint64_t ram_bytes_remaining(void)
207 return ram_state
->migration_dirty_pages
* TARGET_PAGE_SIZE
;
210 MigrationStats ram_counters
;
212 /* used by the search for pages to send */
213 struct PageSearchStatus
{
214 /* Current block being searched */
216 /* Current page to search from */
218 /* Set once we wrap around */
221 typedef struct PageSearchStatus PageSearchStatus
;
223 struct CompressParam
{
232 typedef struct CompressParam CompressParam
;
234 struct DecompressParam
{
243 typedef struct DecompressParam DecompressParam
;
245 static CompressParam
*comp_param
;
246 static QemuThread
*compress_threads
;
247 /* comp_done_cond is used to wake up the migration thread when
248 * one of the compression threads has finished the compression.
249 * comp_done_lock is used to co-work with comp_done_cond.
251 static QemuMutex comp_done_lock
;
252 static QemuCond comp_done_cond
;
253 /* The empty QEMUFileOps will be used by file in CompressParam */
254 static const QEMUFileOps empty_ops
= { };
256 static DecompressParam
*decomp_param
;
257 static QemuThread
*decompress_threads
;
258 static QemuMutex decomp_done_lock
;
259 static QemuCond decomp_done_cond
;
261 static int do_compress_ram_page(QEMUFile
*f
, RAMBlock
*block
,
264 static void *do_data_compress(void *opaque
)
266 CompressParam
*param
= opaque
;
270 qemu_mutex_lock(¶m
->mutex
);
271 while (!param
->quit
) {
273 block
= param
->block
;
274 offset
= param
->offset
;
276 qemu_mutex_unlock(¶m
->mutex
);
278 do_compress_ram_page(param
->file
, block
, offset
);
280 qemu_mutex_lock(&comp_done_lock
);
282 qemu_cond_signal(&comp_done_cond
);
283 qemu_mutex_unlock(&comp_done_lock
);
285 qemu_mutex_lock(¶m
->mutex
);
287 qemu_cond_wait(¶m
->cond
, ¶m
->mutex
);
290 qemu_mutex_unlock(¶m
->mutex
);
295 static inline void terminate_compression_threads(void)
297 int idx
, thread_count
;
299 thread_count
= migrate_compress_threads();
301 for (idx
= 0; idx
< thread_count
; idx
++) {
302 qemu_mutex_lock(&comp_param
[idx
].mutex
);
303 comp_param
[idx
].quit
= true;
304 qemu_cond_signal(&comp_param
[idx
].cond
);
305 qemu_mutex_unlock(&comp_param
[idx
].mutex
);
309 static void compress_threads_save_cleanup(void)
313 if (!migrate_use_compression()) {
316 terminate_compression_threads();
317 thread_count
= migrate_compress_threads();
318 for (i
= 0; i
< thread_count
; i
++) {
319 qemu_thread_join(compress_threads
+ i
);
320 qemu_fclose(comp_param
[i
].file
);
321 qemu_mutex_destroy(&comp_param
[i
].mutex
);
322 qemu_cond_destroy(&comp_param
[i
].cond
);
324 qemu_mutex_destroy(&comp_done_lock
);
325 qemu_cond_destroy(&comp_done_cond
);
326 g_free(compress_threads
);
328 compress_threads
= NULL
;
332 static void compress_threads_save_setup(void)
336 if (!migrate_use_compression()) {
339 thread_count
= migrate_compress_threads();
340 compress_threads
= g_new0(QemuThread
, thread_count
);
341 comp_param
= g_new0(CompressParam
, thread_count
);
342 qemu_cond_init(&comp_done_cond
);
343 qemu_mutex_init(&comp_done_lock
);
344 for (i
= 0; i
< thread_count
; i
++) {
345 /* comp_param[i].file is just used as a dummy buffer to save data,
346 * set its ops to empty.
348 comp_param
[i
].file
= qemu_fopen_ops(NULL
, &empty_ops
);
349 comp_param
[i
].done
= true;
350 comp_param
[i
].quit
= false;
351 qemu_mutex_init(&comp_param
[i
].mutex
);
352 qemu_cond_init(&comp_param
[i
].cond
);
353 qemu_thread_create(compress_threads
+ i
, "compress",
354 do_data_compress
, comp_param
+ i
,
355 QEMU_THREAD_JOINABLE
);
360 * save_page_header: write page header to wire
362 * If this is the 1st block, it also writes the block identification
364 * Returns the number of bytes written
366 * @f: QEMUFile where to send the data
367 * @block: block that contains the page we want to send
368 * @offset: offset inside the block for the page
369 * in the lower bits, it contains flags
371 static size_t save_page_header(RAMState
*rs
, QEMUFile
*f
, RAMBlock
*block
,
376 if (block
== rs
->last_sent_block
) {
377 offset
|= RAM_SAVE_FLAG_CONTINUE
;
379 qemu_put_be64(f
, offset
);
382 if (!(offset
& RAM_SAVE_FLAG_CONTINUE
)) {
383 len
= strlen(block
->idstr
);
384 qemu_put_byte(f
, len
);
385 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, len
);
387 rs
->last_sent_block
= block
;
393 * mig_throttle_guest_down: throotle down the guest
395 * Reduce amount of guest cpu execution to hopefully slow down memory
396 * writes. If guest dirty memory rate is reduced below the rate at
397 * which we can transfer pages to the destination then we should be
398 * able to complete migration. Some workloads dirty memory way too
399 * fast and will not effectively converge, even with auto-converge.
401 static void mig_throttle_guest_down(void)
403 MigrationState
*s
= migrate_get_current();
404 uint64_t pct_initial
= s
->parameters
.cpu_throttle_initial
;
405 uint64_t pct_icrement
= s
->parameters
.cpu_throttle_increment
;
407 /* We have not started throttling yet. Let's start it. */
408 if (!cpu_throttle_active()) {
409 cpu_throttle_set(pct_initial
);
411 /* Throttling already on, just increase the rate */
412 cpu_throttle_set(cpu_throttle_get_percentage() + pct_icrement
);
417 * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache
419 * @rs: current RAM state
420 * @current_addr: address for the zero page
422 * Update the xbzrle cache to reflect a page that's been sent as all 0.
423 * The important thing is that a stale (not-yet-0'd) page be replaced
425 * As a bonus, if the page wasn't in the cache it gets added so that
426 * when a small write is made into the 0'd page it gets XBZRLE sent.
428 static void xbzrle_cache_zero_page(RAMState
*rs
, ram_addr_t current_addr
)
430 if (rs
->ram_bulk_stage
|| !migrate_use_xbzrle()) {
434 /* We don't care if this fails to allocate a new cache page
435 * as long as it updated an old one */
436 cache_insert(XBZRLE
.cache
, current_addr
, XBZRLE
.zero_target_page
,
437 ram_counters
.dirty_sync_count
);
440 #define ENCODING_FLAG_XBZRLE 0x1
443 * save_xbzrle_page: compress and send current page
445 * Returns: 1 means that we wrote the page
446 * 0 means that page is identical to the one already sent
447 * -1 means that xbzrle would be longer than normal
449 * @rs: current RAM state
450 * @current_data: pointer to the address of the page contents
451 * @current_addr: addr of the page
452 * @block: block that contains the page we want to send
453 * @offset: offset inside the block for the page
454 * @last_stage: if we are at the completion stage
456 static int save_xbzrle_page(RAMState
*rs
, uint8_t **current_data
,
457 ram_addr_t current_addr
, RAMBlock
*block
,
458 ram_addr_t offset
, bool last_stage
)
460 int encoded_len
= 0, bytes_xbzrle
;
461 uint8_t *prev_cached_page
;
463 if (!cache_is_cached(XBZRLE
.cache
, current_addr
,
464 ram_counters
.dirty_sync_count
)) {
465 xbzrle_counters
.cache_miss
++;
467 if (cache_insert(XBZRLE
.cache
, current_addr
, *current_data
,
468 ram_counters
.dirty_sync_count
) == -1) {
471 /* update *current_data when the page has been
472 inserted into cache */
473 *current_data
= get_cached_data(XBZRLE
.cache
, current_addr
);
479 prev_cached_page
= get_cached_data(XBZRLE
.cache
, current_addr
);
481 /* save current buffer into memory */
482 memcpy(XBZRLE
.current_buf
, *current_data
, TARGET_PAGE_SIZE
);
484 /* XBZRLE encoding (if there is no overflow) */
485 encoded_len
= xbzrle_encode_buffer(prev_cached_page
, XBZRLE
.current_buf
,
486 TARGET_PAGE_SIZE
, XBZRLE
.encoded_buf
,
488 if (encoded_len
== 0) {
489 trace_save_xbzrle_page_skipping();
491 } else if (encoded_len
== -1) {
492 trace_save_xbzrle_page_overflow();
493 xbzrle_counters
.overflow
++;
494 /* update data in the cache */
496 memcpy(prev_cached_page
, *current_data
, TARGET_PAGE_SIZE
);
497 *current_data
= prev_cached_page
;
502 /* we need to update the data in the cache, in order to get the same data */
504 memcpy(prev_cached_page
, XBZRLE
.current_buf
, TARGET_PAGE_SIZE
);
507 /* Send XBZRLE based compressed page */
508 bytes_xbzrle
= save_page_header(rs
, rs
->f
, block
,
509 offset
| RAM_SAVE_FLAG_XBZRLE
);
510 qemu_put_byte(rs
->f
, ENCODING_FLAG_XBZRLE
);
511 qemu_put_be16(rs
->f
, encoded_len
);
512 qemu_put_buffer(rs
->f
, XBZRLE
.encoded_buf
, encoded_len
);
513 bytes_xbzrle
+= encoded_len
+ 1 + 2;
514 xbzrle_counters
.pages
++;
515 xbzrle_counters
.bytes
+= bytes_xbzrle
;
516 ram_counters
.transferred
+= bytes_xbzrle
;
522 * migration_bitmap_find_dirty: find the next dirty page from start
524 * Called with rcu_read_lock() to protect migration_bitmap
526 * Returns the byte offset within memory region of the start of a dirty page
528 * @rs: current RAM state
529 * @rb: RAMBlock where to search for dirty pages
530 * @start: page where we start the search
533 unsigned long migration_bitmap_find_dirty(RAMState
*rs
, RAMBlock
*rb
,
536 unsigned long size
= rb
->used_length
>> TARGET_PAGE_BITS
;
537 unsigned long *bitmap
= rb
->bmap
;
540 if (rs
->ram_bulk_stage
&& start
> 0) {
543 next
= find_next_bit(bitmap
, size
, start
);
549 static inline bool migration_bitmap_clear_dirty(RAMState
*rs
,
555 ret
= test_and_clear_bit(page
, rb
->bmap
);
558 rs
->migration_dirty_pages
--;
563 static void migration_bitmap_sync_range(RAMState
*rs
, RAMBlock
*rb
,
564 ram_addr_t start
, ram_addr_t length
)
566 rs
->migration_dirty_pages
+=
567 cpu_physical_memory_sync_dirty_bitmap(rb
, start
, length
,
568 &rs
->num_dirty_pages_period
);
572 * ram_pagesize_summary: calculate all the pagesizes of a VM
574 * Returns a summary bitmap of the page sizes of all RAMBlocks
576 * For VMs with just normal pages this is equivalent to the host page
577 * size. If it's got some huge pages then it's the OR of all the
578 * different page sizes.
580 uint64_t ram_pagesize_summary(void)
583 uint64_t summary
= 0;
585 RAMBLOCK_FOREACH(block
) {
586 summary
|= block
->page_size
;
592 static void migration_bitmap_sync(RAMState
*rs
)
596 uint64_t bytes_xfer_now
;
598 ram_counters
.dirty_sync_count
++;
600 if (!rs
->time_last_bitmap_sync
) {
601 rs
->time_last_bitmap_sync
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
604 trace_migration_bitmap_sync_start();
605 memory_global_dirty_log_sync();
607 qemu_mutex_lock(&rs
->bitmap_mutex
);
609 RAMBLOCK_FOREACH(block
) {
610 migration_bitmap_sync_range(rs
, block
, 0, block
->used_length
);
613 qemu_mutex_unlock(&rs
->bitmap_mutex
);
615 trace_migration_bitmap_sync_end(rs
->num_dirty_pages_period
);
617 end_time
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
);
619 /* more than 1 second = 1000 millisecons */
620 if (end_time
> rs
->time_last_bitmap_sync
+ 1000) {
621 /* calculate period counters */
622 ram_counters
.dirty_pages_rate
= rs
->num_dirty_pages_period
* 1000
623 / (end_time
- rs
->time_last_bitmap_sync
);
624 bytes_xfer_now
= ram_counters
.transferred
;
626 if (migrate_auto_converge()) {
627 /* The following detection logic can be refined later. For now:
628 Check to see if the dirtied bytes is 50% more than the approx.
629 amount of bytes that just got transferred since the last time we
630 were in this routine. If that happens twice, start or increase
633 if ((rs
->num_dirty_pages_period
* TARGET_PAGE_SIZE
>
634 (bytes_xfer_now
- rs
->bytes_xfer_prev
) / 2) &&
635 (++rs
->dirty_rate_high_cnt
>= 2)) {
636 trace_migration_throttle();
637 rs
->dirty_rate_high_cnt
= 0;
638 mig_throttle_guest_down();
642 if (migrate_use_xbzrle()) {
643 if (rs
->iterations_prev
!= rs
->iterations
) {
644 xbzrle_counters
.cache_miss_rate
=
645 (double)(xbzrle_counters
.cache_miss
-
646 rs
->xbzrle_cache_miss_prev
) /
647 (rs
->iterations
- rs
->iterations_prev
);
649 rs
->iterations_prev
= rs
->iterations
;
650 rs
->xbzrle_cache_miss_prev
= xbzrle_counters
.cache_miss
;
653 /* reset period counters */
654 rs
->time_last_bitmap_sync
= end_time
;
655 rs
->num_dirty_pages_period
= 0;
656 rs
->bytes_xfer_prev
= bytes_xfer_now
;
658 if (migrate_use_events()) {
659 qapi_event_send_migration_pass(ram_counters
.dirty_sync_count
, NULL
);
664 * save_zero_page: send the zero page to the stream
666 * Returns the number of pages written.
668 * @rs: current RAM state
669 * @block: block that contains the page we want to send
670 * @offset: offset inside the block for the page
671 * @p: pointer to the page
673 static int save_zero_page(RAMState
*rs
, RAMBlock
*block
, ram_addr_t offset
,
678 if (is_zero_range(p
, TARGET_PAGE_SIZE
)) {
679 ram_counters
.duplicate
++;
680 ram_counters
.transferred
+=
681 save_page_header(rs
, rs
->f
, block
, offset
| RAM_SAVE_FLAG_ZERO
);
682 qemu_put_byte(rs
->f
, 0);
683 ram_counters
.transferred
+= 1;
690 static void ram_release_pages(const char *rbname
, uint64_t offset
, int pages
)
692 if (!migrate_release_ram() || !migration_in_postcopy()) {
696 ram_discard_range(rbname
, offset
, pages
<< TARGET_PAGE_BITS
);
700 * ram_save_page: send the given page to the stream
702 * Returns the number of pages written.
704 * >=0 - Number of pages written - this might legally be 0
705 * if xbzrle noticed the page was the same.
707 * @rs: current RAM state
708 * @block: block that contains the page we want to send
709 * @offset: offset inside the block for the page
710 * @last_stage: if we are at the completion stage
712 static int ram_save_page(RAMState
*rs
, PageSearchStatus
*pss
, bool last_stage
)
716 ram_addr_t current_addr
;
719 bool send_async
= true;
720 RAMBlock
*block
= pss
->block
;
721 ram_addr_t offset
= pss
->page
<< TARGET_PAGE_BITS
;
723 p
= block
->host
+ offset
;
724 trace_ram_save_page(block
->idstr
, (uint64_t)offset
, p
);
726 /* In doubt sent page as normal */
728 ret
= ram_control_save_page(rs
->f
, block
->offset
,
729 offset
, TARGET_PAGE_SIZE
, &bytes_xmit
);
731 ram_counters
.transferred
+= bytes_xmit
;
737 current_addr
= block
->offset
+ offset
;
739 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
740 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
741 if (bytes_xmit
> 0) {
742 ram_counters
.normal
++;
743 } else if (bytes_xmit
== 0) {
744 ram_counters
.duplicate
++;
748 pages
= save_zero_page(rs
, block
, offset
, p
);
750 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
751 * page would be stale
753 xbzrle_cache_zero_page(rs
, current_addr
);
754 ram_release_pages(block
->idstr
, offset
, pages
);
755 } else if (!rs
->ram_bulk_stage
&&
756 !migration_in_postcopy() && migrate_use_xbzrle()) {
757 pages
= save_xbzrle_page(rs
, &p
, current_addr
, block
,
760 /* Can't send this cached data async, since the cache page
761 * might get updated before it gets to the wire
768 /* XBZRLE overflow or normal page */
770 ram_counters
.transferred
+=
771 save_page_header(rs
, rs
->f
, block
, offset
| RAM_SAVE_FLAG_PAGE
);
773 qemu_put_buffer_async(rs
->f
, p
, TARGET_PAGE_SIZE
,
774 migrate_release_ram() &
775 migration_in_postcopy());
777 qemu_put_buffer(rs
->f
, p
, TARGET_PAGE_SIZE
);
779 ram_counters
.transferred
+= TARGET_PAGE_SIZE
;
781 ram_counters
.normal
++;
784 XBZRLE_cache_unlock();
789 static int do_compress_ram_page(QEMUFile
*f
, RAMBlock
*block
,
792 RAMState
*rs
= ram_state
;
793 int bytes_sent
, blen
;
794 uint8_t *p
= block
->host
+ (offset
& TARGET_PAGE_MASK
);
796 bytes_sent
= save_page_header(rs
, f
, block
, offset
|
797 RAM_SAVE_FLAG_COMPRESS_PAGE
);
798 blen
= qemu_put_compression_data(f
, p
, TARGET_PAGE_SIZE
,
799 migrate_compress_level());
802 qemu_file_set_error(migrate_get_current()->to_dst_file
, blen
);
803 error_report("compressed data failed!");
806 ram_release_pages(block
->idstr
, offset
& TARGET_PAGE_MASK
, 1);
812 static void flush_compressed_data(RAMState
*rs
)
814 int idx
, len
, thread_count
;
816 if (!migrate_use_compression()) {
819 thread_count
= migrate_compress_threads();
821 qemu_mutex_lock(&comp_done_lock
);
822 for (idx
= 0; idx
< thread_count
; idx
++) {
823 while (!comp_param
[idx
].done
) {
824 qemu_cond_wait(&comp_done_cond
, &comp_done_lock
);
827 qemu_mutex_unlock(&comp_done_lock
);
829 for (idx
= 0; idx
< thread_count
; idx
++) {
830 qemu_mutex_lock(&comp_param
[idx
].mutex
);
831 if (!comp_param
[idx
].quit
) {
832 len
= qemu_put_qemu_file(rs
->f
, comp_param
[idx
].file
);
833 ram_counters
.transferred
+= len
;
835 qemu_mutex_unlock(&comp_param
[idx
].mutex
);
839 static inline void set_compress_params(CompressParam
*param
, RAMBlock
*block
,
842 param
->block
= block
;
843 param
->offset
= offset
;
846 static int compress_page_with_multi_thread(RAMState
*rs
, RAMBlock
*block
,
849 int idx
, thread_count
, bytes_xmit
= -1, pages
= -1;
851 thread_count
= migrate_compress_threads();
852 qemu_mutex_lock(&comp_done_lock
);
854 for (idx
= 0; idx
< thread_count
; idx
++) {
855 if (comp_param
[idx
].done
) {
856 comp_param
[idx
].done
= false;
857 bytes_xmit
= qemu_put_qemu_file(rs
->f
, comp_param
[idx
].file
);
858 qemu_mutex_lock(&comp_param
[idx
].mutex
);
859 set_compress_params(&comp_param
[idx
], block
, offset
);
860 qemu_cond_signal(&comp_param
[idx
].cond
);
861 qemu_mutex_unlock(&comp_param
[idx
].mutex
);
863 ram_counters
.normal
++;
864 ram_counters
.transferred
+= bytes_xmit
;
871 qemu_cond_wait(&comp_done_cond
, &comp_done_lock
);
874 qemu_mutex_unlock(&comp_done_lock
);
880 * ram_save_compressed_page: compress the given page and send it to the stream
882 * Returns the number of pages written.
884 * @rs: current RAM state
885 * @block: block that contains the page we want to send
886 * @offset: offset inside the block for the page
887 * @last_stage: if we are at the completion stage
889 static int ram_save_compressed_page(RAMState
*rs
, PageSearchStatus
*pss
,
893 uint64_t bytes_xmit
= 0;
896 RAMBlock
*block
= pss
->block
;
897 ram_addr_t offset
= pss
->page
<< TARGET_PAGE_BITS
;
899 p
= block
->host
+ offset
;
901 ret
= ram_control_save_page(rs
->f
, block
->offset
,
902 offset
, TARGET_PAGE_SIZE
, &bytes_xmit
);
904 ram_counters
.transferred
+= bytes_xmit
;
907 if (ret
!= RAM_SAVE_CONTROL_NOT_SUPP
) {
908 if (ret
!= RAM_SAVE_CONTROL_DELAYED
) {
909 if (bytes_xmit
> 0) {
910 ram_counters
.normal
++;
911 } else if (bytes_xmit
== 0) {
912 ram_counters
.duplicate
++;
916 /* When starting the process of a new block, the first page of
917 * the block should be sent out before other pages in the same
918 * block, and all the pages in last block should have been sent
919 * out, keeping this order is important, because the 'cont' flag
920 * is used to avoid resending the block name.
922 if (block
!= rs
->last_sent_block
) {
923 flush_compressed_data(rs
);
924 pages
= save_zero_page(rs
, block
, offset
, p
);
926 /* Make sure the first page is sent out before other pages */
927 bytes_xmit
= save_page_header(rs
, rs
->f
, block
, offset
|
928 RAM_SAVE_FLAG_COMPRESS_PAGE
);
929 blen
= qemu_put_compression_data(rs
->f
, p
, TARGET_PAGE_SIZE
,
930 migrate_compress_level());
932 ram_counters
.transferred
+= bytes_xmit
+ blen
;
933 ram_counters
.normal
++;
936 qemu_file_set_error(rs
->f
, blen
);
937 error_report("compressed data failed!");
941 ram_release_pages(block
->idstr
, offset
, pages
);
944 pages
= save_zero_page(rs
, block
, offset
, p
);
946 pages
= compress_page_with_multi_thread(rs
, block
, offset
);
948 ram_release_pages(block
->idstr
, offset
, pages
);
957 * find_dirty_block: find the next dirty page and update any state
958 * associated with the search process.
960 * Returns if a page is found
962 * @rs: current RAM state
963 * @pss: data about the state of the current dirty page scan
964 * @again: set to false if the search has scanned the whole of RAM
966 static bool find_dirty_block(RAMState
*rs
, PageSearchStatus
*pss
, bool *again
)
968 pss
->page
= migration_bitmap_find_dirty(rs
, pss
->block
, pss
->page
);
969 if (pss
->complete_round
&& pss
->block
== rs
->last_seen_block
&&
970 pss
->page
>= rs
->last_page
) {
972 * We've been once around the RAM and haven't found anything.
978 if ((pss
->page
<< TARGET_PAGE_BITS
) >= pss
->block
->used_length
) {
979 /* Didn't find anything in this RAM Block */
981 pss
->block
= QLIST_NEXT_RCU(pss
->block
, next
);
983 /* Hit the end of the list */
984 pss
->block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
985 /* Flag that we've looped */
986 pss
->complete_round
= true;
987 rs
->ram_bulk_stage
= false;
988 if (migrate_use_xbzrle()) {
989 /* If xbzrle is on, stop using the data compression at this
990 * point. In theory, xbzrle can do better than compression.
992 flush_compressed_data(rs
);
995 /* Didn't find anything this time, but try again on the new block */
999 /* Can go around again, but... */
1001 /* We've found something so probably don't need to */
1007 * unqueue_page: gets a page of the queue
1009 * Helper for 'get_queued_page' - gets a page off the queue
1011 * Returns the block of the page (or NULL if none available)
1013 * @rs: current RAM state
1014 * @offset: used to return the offset within the RAMBlock
1016 static RAMBlock
*unqueue_page(RAMState
*rs
, ram_addr_t
*offset
)
1018 RAMBlock
*block
= NULL
;
1020 qemu_mutex_lock(&rs
->src_page_req_mutex
);
1021 if (!QSIMPLEQ_EMPTY(&rs
->src_page_requests
)) {
1022 struct RAMSrcPageRequest
*entry
=
1023 QSIMPLEQ_FIRST(&rs
->src_page_requests
);
1025 *offset
= entry
->offset
;
1027 if (entry
->len
> TARGET_PAGE_SIZE
) {
1028 entry
->len
-= TARGET_PAGE_SIZE
;
1029 entry
->offset
+= TARGET_PAGE_SIZE
;
1031 memory_region_unref(block
->mr
);
1032 QSIMPLEQ_REMOVE_HEAD(&rs
->src_page_requests
, next_req
);
1036 qemu_mutex_unlock(&rs
->src_page_req_mutex
);
1042 * get_queued_page: unqueue a page from the postocpy requests
1044 * Skips pages that are already sent (!dirty)
1046 * Returns if a queued page is found
1048 * @rs: current RAM state
1049 * @pss: data about the state of the current dirty page scan
1051 static bool get_queued_page(RAMState
*rs
, PageSearchStatus
*pss
)
1058 block
= unqueue_page(rs
, &offset
);
1060 * We're sending this page, and since it's postcopy nothing else
1061 * will dirty it, and we must make sure it doesn't get sent again
1062 * even if this queue request was received after the background
1063 * search already sent it.
1068 page
= offset
>> TARGET_PAGE_BITS
;
1069 dirty
= test_bit(page
, block
->bmap
);
1071 trace_get_queued_page_not_dirty(block
->idstr
, (uint64_t)offset
,
1072 page
, test_bit(page
, block
->unsentmap
));
1074 trace_get_queued_page(block
->idstr
, (uint64_t)offset
, page
);
1078 } while (block
&& !dirty
);
1082 * As soon as we start servicing pages out of order, then we have
1083 * to kill the bulk stage, since the bulk stage assumes
1084 * in (migration_bitmap_find_and_reset_dirty) that every page is
1085 * dirty, that's no longer true.
1087 rs
->ram_bulk_stage
= false;
1090 * We want the background search to continue from the queued page
1091 * since the guest is likely to want other pages near to the page
1092 * it just requested.
1095 pss
->page
= offset
>> TARGET_PAGE_BITS
;
1102 * migration_page_queue_free: drop any remaining pages in the ram
1105 * It should be empty at the end anyway, but in error cases there may
1106 * be some left. in case that there is any page left, we drop it.
1109 static void migration_page_queue_free(RAMState
*rs
)
1111 struct RAMSrcPageRequest
*mspr
, *next_mspr
;
1112 /* This queue generally should be empty - but in the case of a failed
1113 * migration might have some droppings in.
1116 QSIMPLEQ_FOREACH_SAFE(mspr
, &rs
->src_page_requests
, next_req
, next_mspr
) {
1117 memory_region_unref(mspr
->rb
->mr
);
1118 QSIMPLEQ_REMOVE_HEAD(&rs
->src_page_requests
, next_req
);
1125 * ram_save_queue_pages: queue the page for transmission
1127 * A request from postcopy destination for example.
1129 * Returns zero on success or negative on error
1131 * @rbname: Name of the RAMBLock of the request. NULL means the
1132 * same that last one.
1133 * @start: starting address from the start of the RAMBlock
1134 * @len: length (in bytes) to send
1136 int ram_save_queue_pages(const char *rbname
, ram_addr_t start
, ram_addr_t len
)
1139 RAMState
*rs
= ram_state
;
1141 ram_counters
.postcopy_requests
++;
1144 /* Reuse last RAMBlock */
1145 ramblock
= rs
->last_req_rb
;
1149 * Shouldn't happen, we can't reuse the last RAMBlock if
1150 * it's the 1st request.
1152 error_report("ram_save_queue_pages no previous block");
1156 ramblock
= qemu_ram_block_by_name(rbname
);
1159 /* We shouldn't be asked for a non-existent RAMBlock */
1160 error_report("ram_save_queue_pages no block '%s'", rbname
);
1163 rs
->last_req_rb
= ramblock
;
1165 trace_ram_save_queue_pages(ramblock
->idstr
, start
, len
);
1166 if (start
+len
> ramblock
->used_length
) {
1167 error_report("%s request overrun start=" RAM_ADDR_FMT
" len="
1168 RAM_ADDR_FMT
" blocklen=" RAM_ADDR_FMT
,
1169 __func__
, start
, len
, ramblock
->used_length
);
1173 struct RAMSrcPageRequest
*new_entry
=
1174 g_malloc0(sizeof(struct RAMSrcPageRequest
));
1175 new_entry
->rb
= ramblock
;
1176 new_entry
->offset
= start
;
1177 new_entry
->len
= len
;
1179 memory_region_ref(ramblock
->mr
);
1180 qemu_mutex_lock(&rs
->src_page_req_mutex
);
1181 QSIMPLEQ_INSERT_TAIL(&rs
->src_page_requests
, new_entry
, next_req
);
1182 qemu_mutex_unlock(&rs
->src_page_req_mutex
);
1193 * ram_save_target_page: save one target page
1195 * Returns the number of pages written
1197 * @rs: current RAM state
1198 * @ms: current migration state
1199 * @pss: data about the page we want to send
1200 * @last_stage: if we are at the completion stage
1202 static int ram_save_target_page(RAMState
*rs
, PageSearchStatus
*pss
,
1207 /* Check the pages is dirty and if it is send it */
1208 if (migration_bitmap_clear_dirty(rs
, pss
->block
, pss
->page
)) {
1210 * If xbzrle is on, stop using the data compression after first
1211 * round of migration even if compression is enabled. In theory,
1212 * xbzrle can do better than compression.
1214 if (migrate_use_compression() &&
1215 (rs
->ram_bulk_stage
|| !migrate_use_xbzrle())) {
1216 res
= ram_save_compressed_page(rs
, pss
, last_stage
);
1218 res
= ram_save_page(rs
, pss
, last_stage
);
1224 if (pss
->block
->unsentmap
) {
1225 clear_bit(pss
->page
, pss
->block
->unsentmap
);
1233 * ram_save_host_page: save a whole host page
1235 * Starting at *offset send pages up to the end of the current host
1236 * page. It's valid for the initial offset to point into the middle of
1237 * a host page in which case the remainder of the hostpage is sent.
1238 * Only dirty target pages are sent. Note that the host page size may
1239 * be a huge page for this block.
1240 * The saving stops at the boundary of the used_length of the block
1241 * if the RAMBlock isn't a multiple of the host page size.
1243 * Returns the number of pages written or negative on error
1245 * @rs: current RAM state
1246 * @ms: current migration state
1247 * @pss: data about the page we want to send
1248 * @last_stage: if we are at the completion stage
1250 static int ram_save_host_page(RAMState
*rs
, PageSearchStatus
*pss
,
1253 int tmppages
, pages
= 0;
1254 size_t pagesize_bits
=
1255 qemu_ram_pagesize(pss
->block
) >> TARGET_PAGE_BITS
;
1258 tmppages
= ram_save_target_page(rs
, pss
, last_stage
);
1265 } while ((pss
->page
& (pagesize_bits
- 1)) &&
1266 offset_in_ramblock(pss
->block
, pss
->page
<< TARGET_PAGE_BITS
));
1268 /* The offset we leave with is the last one we looked at */
1274 * ram_find_and_save_block: finds a dirty page and sends it to f
1276 * Called within an RCU critical section.
1278 * Returns the number of pages written where zero means no dirty pages
1280 * @rs: current RAM state
1281 * @last_stage: if we are at the completion stage
1283 * On systems where host-page-size > target-page-size it will send all the
1284 * pages in a host page that are dirty.
1287 static int ram_find_and_save_block(RAMState
*rs
, bool last_stage
)
1289 PageSearchStatus pss
;
1293 /* No dirty page as there is zero RAM */
1294 if (!ram_bytes_total()) {
1298 pss
.block
= rs
->last_seen_block
;
1299 pss
.page
= rs
->last_page
;
1300 pss
.complete_round
= false;
1303 pss
.block
= QLIST_FIRST_RCU(&ram_list
.blocks
);
1308 found
= get_queued_page(rs
, &pss
);
1311 /* priority queue empty, so just search for something dirty */
1312 found
= find_dirty_block(rs
, &pss
, &again
);
1316 pages
= ram_save_host_page(rs
, &pss
, last_stage
);
1318 } while (!pages
&& again
);
1320 rs
->last_seen_block
= pss
.block
;
1321 rs
->last_page
= pss
.page
;
1326 void acct_update_position(QEMUFile
*f
, size_t size
, bool zero
)
1328 uint64_t pages
= size
/ TARGET_PAGE_SIZE
;
1331 ram_counters
.duplicate
+= pages
;
1333 ram_counters
.normal
+= pages
;
1334 ram_counters
.transferred
+= size
;
1335 qemu_update_position(f
, size
);
1339 uint64_t ram_bytes_total(void)
1345 RAMBLOCK_FOREACH(block
) {
1346 total
+= block
->used_length
;
1352 static void xbzrle_load_setup(void)
1354 XBZRLE
.decoded_buf
= g_malloc(TARGET_PAGE_SIZE
);
1357 static void xbzrle_load_cleanup(void)
1359 g_free(XBZRLE
.decoded_buf
);
1360 XBZRLE
.decoded_buf
= NULL
;
1363 static void ram_save_cleanup(void *opaque
)
1365 RAMState
**rsp
= opaque
;
1368 /* caller have hold iothread lock or is in a bh, so there is
1369 * no writing race against this migration_bitmap
1371 memory_global_dirty_log_stop();
1373 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1374 g_free(block
->bmap
);
1376 g_free(block
->unsentmap
);
1377 block
->unsentmap
= NULL
;
1380 XBZRLE_cache_lock();
1382 cache_fini(XBZRLE
.cache
);
1383 g_free(XBZRLE
.encoded_buf
);
1384 g_free(XBZRLE
.current_buf
);
1385 g_free(XBZRLE
.zero_target_page
);
1386 XBZRLE
.cache
= NULL
;
1387 XBZRLE
.encoded_buf
= NULL
;
1388 XBZRLE
.current_buf
= NULL
;
1389 XBZRLE
.zero_target_page
= NULL
;
1391 XBZRLE_cache_unlock();
1392 migration_page_queue_free(*rsp
);
1393 compress_threads_save_cleanup();
1398 static void ram_state_reset(RAMState
*rs
)
1400 rs
->last_seen_block
= NULL
;
1401 rs
->last_sent_block
= NULL
;
1403 rs
->last_version
= ram_list
.version
;
1404 rs
->ram_bulk_stage
= true;
1407 #define MAX_WAIT 50 /* ms, half buffered_file limit */
1410 * 'expected' is the value you expect the bitmap mostly to be full
1411 * of; it won't bother printing lines that are all this value.
1412 * If 'todump' is null the migration bitmap is dumped.
1414 void ram_debug_dump_bitmap(unsigned long *todump
, bool expected
,
1415 unsigned long pages
)
1418 int64_t linelen
= 128;
1421 for (cur
= 0; cur
< pages
; cur
+= linelen
) {
1425 * Last line; catch the case where the line length
1426 * is longer than remaining ram
1428 if (cur
+ linelen
> pages
) {
1429 linelen
= pages
- cur
;
1431 for (curb
= 0; curb
< linelen
; curb
++) {
1432 bool thisbit
= test_bit(cur
+ curb
, todump
);
1433 linebuf
[curb
] = thisbit
? '1' : '.';
1434 found
= found
|| (thisbit
!= expected
);
1437 linebuf
[curb
] = '\0';
1438 fprintf(stderr
, "0x%08" PRIx64
" : %s\n", cur
, linebuf
);
1443 /* **** functions for postcopy ***** */
1445 void ram_postcopy_migrated_memory_release(MigrationState
*ms
)
1447 struct RAMBlock
*block
;
1449 RAMBLOCK_FOREACH(block
) {
1450 unsigned long *bitmap
= block
->bmap
;
1451 unsigned long range
= block
->used_length
>> TARGET_PAGE_BITS
;
1452 unsigned long run_start
= find_next_zero_bit(bitmap
, range
, 0);
1454 while (run_start
< range
) {
1455 unsigned long run_end
= find_next_bit(bitmap
, range
, run_start
+ 1);
1456 ram_discard_range(block
->idstr
, run_start
<< TARGET_PAGE_BITS
,
1457 (run_end
- run_start
) << TARGET_PAGE_BITS
);
1458 run_start
= find_next_zero_bit(bitmap
, range
, run_end
+ 1);
1464 * postcopy_send_discard_bm_ram: discard a RAMBlock
1466 * Returns zero on success
1468 * Callback from postcopy_each_ram_send_discard for each RAMBlock
1469 * Note: At this point the 'unsentmap' is the processed bitmap combined
1470 * with the dirtymap; so a '1' means it's either dirty or unsent.
1472 * @ms: current migration state
1473 * @pds: state for postcopy
1474 * @start: RAMBlock starting page
1475 * @length: RAMBlock size
1477 static int postcopy_send_discard_bm_ram(MigrationState
*ms
,
1478 PostcopyDiscardState
*pds
,
1481 unsigned long end
= block
->used_length
>> TARGET_PAGE_BITS
;
1482 unsigned long current
;
1483 unsigned long *unsentmap
= block
->unsentmap
;
1485 for (current
= 0; current
< end
; ) {
1486 unsigned long one
= find_next_bit(unsentmap
, end
, current
);
1489 unsigned long zero
= find_next_zero_bit(unsentmap
, end
, one
+ 1);
1490 unsigned long discard_length
;
1493 discard_length
= end
- one
;
1495 discard_length
= zero
- one
;
1497 if (discard_length
) {
1498 postcopy_discard_send_range(ms
, pds
, one
, discard_length
);
1500 current
= one
+ discard_length
;
1510 * postcopy_each_ram_send_discard: discard all RAMBlocks
1512 * Returns 0 for success or negative for error
1514 * Utility for the outgoing postcopy code.
1515 * Calls postcopy_send_discard_bm_ram for each RAMBlock
1516 * passing it bitmap indexes and name.
1517 * (qemu_ram_foreach_block ends up passing unscaled lengths
1518 * which would mean postcopy code would have to deal with target page)
1520 * @ms: current migration state
1522 static int postcopy_each_ram_send_discard(MigrationState
*ms
)
1524 struct RAMBlock
*block
;
1527 RAMBLOCK_FOREACH(block
) {
1528 PostcopyDiscardState
*pds
=
1529 postcopy_discard_send_init(ms
, block
->idstr
);
1532 * Postcopy sends chunks of bitmap over the wire, but it
1533 * just needs indexes at this point, avoids it having
1534 * target page specific code.
1536 ret
= postcopy_send_discard_bm_ram(ms
, pds
, block
);
1537 postcopy_discard_send_finish(ms
, pds
);
1547 * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages
1549 * Helper for postcopy_chunk_hostpages; it's called twice to
1550 * canonicalize the two bitmaps, that are similar, but one is
1553 * Postcopy requires that all target pages in a hostpage are dirty or
1554 * clean, not a mix. This function canonicalizes the bitmaps.
1556 * @ms: current migration state
1557 * @unsent_pass: if true we need to canonicalize partially unsent host pages
1558 * otherwise we need to canonicalize partially dirty host pages
1559 * @block: block that contains the page we want to canonicalize
1560 * @pds: state for postcopy
1562 static void postcopy_chunk_hostpages_pass(MigrationState
*ms
, bool unsent_pass
,
1564 PostcopyDiscardState
*pds
)
1566 RAMState
*rs
= ram_state
;
1567 unsigned long *bitmap
= block
->bmap
;
1568 unsigned long *unsentmap
= block
->unsentmap
;
1569 unsigned int host_ratio
= block
->page_size
/ TARGET_PAGE_SIZE
;
1570 unsigned long pages
= block
->used_length
>> TARGET_PAGE_BITS
;
1571 unsigned long run_start
;
1573 if (block
->page_size
== TARGET_PAGE_SIZE
) {
1574 /* Easy case - TPS==HPS for a non-huge page RAMBlock */
1579 /* Find a sent page */
1580 run_start
= find_next_zero_bit(unsentmap
, pages
, 0);
1582 /* Find a dirty page */
1583 run_start
= find_next_bit(bitmap
, pages
, 0);
1586 while (run_start
< pages
) {
1587 bool do_fixup
= false;
1588 unsigned long fixup_start_addr
;
1589 unsigned long host_offset
;
1592 * If the start of this run of pages is in the middle of a host
1593 * page, then we need to fixup this host page.
1595 host_offset
= run_start
% host_ratio
;
1598 run_start
-= host_offset
;
1599 fixup_start_addr
= run_start
;
1600 /* For the next pass */
1601 run_start
= run_start
+ host_ratio
;
1603 /* Find the end of this run */
1604 unsigned long run_end
;
1606 run_end
= find_next_bit(unsentmap
, pages
, run_start
+ 1);
1608 run_end
= find_next_zero_bit(bitmap
, pages
, run_start
+ 1);
1611 * If the end isn't at the start of a host page, then the
1612 * run doesn't finish at the end of a host page
1613 * and we need to discard.
1615 host_offset
= run_end
% host_ratio
;
1618 fixup_start_addr
= run_end
- host_offset
;
1620 * This host page has gone, the next loop iteration starts
1621 * from after the fixup
1623 run_start
= fixup_start_addr
+ host_ratio
;
1626 * No discards on this iteration, next loop starts from
1627 * next sent/dirty page
1629 run_start
= run_end
+ 1;
1636 /* Tell the destination to discard this page */
1637 if (unsent_pass
|| !test_bit(fixup_start_addr
, unsentmap
)) {
1638 /* For the unsent_pass we:
1639 * discard partially sent pages
1640 * For the !unsent_pass (dirty) we:
1641 * discard partially dirty pages that were sent
1642 * (any partially sent pages were already discarded
1643 * by the previous unsent_pass)
1645 postcopy_discard_send_range(ms
, pds
, fixup_start_addr
,
1649 /* Clean up the bitmap */
1650 for (page
= fixup_start_addr
;
1651 page
< fixup_start_addr
+ host_ratio
; page
++) {
1652 /* All pages in this host page are now not sent */
1653 set_bit(page
, unsentmap
);
1656 * Remark them as dirty, updating the count for any pages
1657 * that weren't previously dirty.
1659 rs
->migration_dirty_pages
+= !test_and_set_bit(page
, bitmap
);
1664 /* Find the next sent page for the next iteration */
1665 run_start
= find_next_zero_bit(unsentmap
, pages
, run_start
);
1667 /* Find the next dirty page for the next iteration */
1668 run_start
= find_next_bit(bitmap
, pages
, run_start
);
1674 * postcopy_chuck_hostpages: discrad any partially sent host page
1676 * Utility for the outgoing postcopy code.
1678 * Discard any partially sent host-page size chunks, mark any partially
1679 * dirty host-page size chunks as all dirty. In this case the host-page
1680 * is the host-page for the particular RAMBlock, i.e. it might be a huge page
1682 * Returns zero on success
1684 * @ms: current migration state
1685 * @block: block we want to work with
1687 static int postcopy_chunk_hostpages(MigrationState
*ms
, RAMBlock
*block
)
1689 PostcopyDiscardState
*pds
=
1690 postcopy_discard_send_init(ms
, block
->idstr
);
1692 /* First pass: Discard all partially sent host pages */
1693 postcopy_chunk_hostpages_pass(ms
, true, block
, pds
);
1695 * Second pass: Ensure that all partially dirty host pages are made
1698 postcopy_chunk_hostpages_pass(ms
, false, block
, pds
);
1700 postcopy_discard_send_finish(ms
, pds
);
1705 * ram_postcopy_send_discard_bitmap: transmit the discard bitmap
1707 * Returns zero on success
1709 * Transmit the set of pages to be discarded after precopy to the target
1710 * these are pages that:
1711 * a) Have been previously transmitted but are now dirty again
1712 * b) Pages that have never been transmitted, this ensures that
1713 * any pages on the destination that have been mapped by background
1714 * tasks get discarded (transparent huge pages is the specific concern)
1715 * Hopefully this is pretty sparse
1717 * @ms: current migration state
1719 int ram_postcopy_send_discard_bitmap(MigrationState
*ms
)
1721 RAMState
*rs
= ram_state
;
1727 /* This should be our last sync, the src is now paused */
1728 migration_bitmap_sync(rs
);
1730 /* Easiest way to make sure we don't resume in the middle of a host-page */
1731 rs
->last_seen_block
= NULL
;
1732 rs
->last_sent_block
= NULL
;
1735 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1736 unsigned long pages
= block
->used_length
>> TARGET_PAGE_BITS
;
1737 unsigned long *bitmap
= block
->bmap
;
1738 unsigned long *unsentmap
= block
->unsentmap
;
1741 /* We don't have a safe way to resize the sentmap, so
1742 * if the bitmap was resized it will be NULL at this
1745 error_report("migration ram resized during precopy phase");
1749 /* Deal with TPS != HPS and huge pages */
1750 ret
= postcopy_chunk_hostpages(ms
, block
);
1757 * Update the unsentmap to be unsentmap = unsentmap | dirty
1759 bitmap_or(unsentmap
, unsentmap
, bitmap
, pages
);
1760 #ifdef DEBUG_POSTCOPY
1761 ram_debug_dump_bitmap(unsentmap
, true, pages
);
1764 trace_ram_postcopy_send_discard_bitmap();
1766 ret
= postcopy_each_ram_send_discard(ms
);
1773 * ram_discard_range: discard dirtied pages at the beginning of postcopy
1775 * Returns zero on success
1777 * @rbname: name of the RAMBlock of the request. NULL means the
1778 * same that last one.
1779 * @start: RAMBlock starting page
1780 * @length: RAMBlock size
1782 int ram_discard_range(const char *rbname
, uint64_t start
, size_t length
)
1786 trace_ram_discard_range(rbname
, start
, length
);
1789 RAMBlock
*rb
= qemu_ram_block_by_name(rbname
);
1792 error_report("ram_discard_range: Failed to find block '%s'", rbname
);
1796 ret
= ram_block_discard_range(rb
, start
, length
);
1804 static int ram_state_init(RAMState
**rsp
)
1806 *rsp
= g_new0(RAMState
, 1);
1808 qemu_mutex_init(&(*rsp
)->bitmap_mutex
);
1809 qemu_mutex_init(&(*rsp
)->src_page_req_mutex
);
1810 QSIMPLEQ_INIT(&(*rsp
)->src_page_requests
);
1812 if (migrate_use_xbzrle()) {
1813 XBZRLE_cache_lock();
1814 XBZRLE
.zero_target_page
= g_malloc0(TARGET_PAGE_SIZE
);
1815 XBZRLE
.cache
= cache_init(migrate_xbzrle_cache_size() /
1818 if (!XBZRLE
.cache
) {
1819 XBZRLE_cache_unlock();
1820 error_report("Error creating cache");
1825 XBZRLE_cache_unlock();
1827 /* We prefer not to abort if there is no memory */
1828 XBZRLE
.encoded_buf
= g_try_malloc0(TARGET_PAGE_SIZE
);
1829 if (!XBZRLE
.encoded_buf
) {
1830 error_report("Error allocating encoded_buf");
1836 XBZRLE
.current_buf
= g_try_malloc(TARGET_PAGE_SIZE
);
1837 if (!XBZRLE
.current_buf
) {
1838 error_report("Error allocating current_buf");
1839 g_free(XBZRLE
.encoded_buf
);
1840 XBZRLE
.encoded_buf
= NULL
;
1847 /* For memory_global_dirty_log_start below. */
1848 qemu_mutex_lock_iothread();
1850 qemu_mutex_lock_ramlist();
1852 ram_state_reset(*rsp
);
1854 /* Skip setting bitmap if there is no RAM */
1855 if (ram_bytes_total()) {
1858 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1859 unsigned long pages
= block
->max_length
>> TARGET_PAGE_BITS
;
1861 block
->bmap
= bitmap_new(pages
);
1862 bitmap_set(block
->bmap
, 0, pages
);
1863 if (migrate_postcopy_ram()) {
1864 block
->unsentmap
= bitmap_new(pages
);
1865 bitmap_set(block
->unsentmap
, 0, pages
);
1871 * Count the total number of pages used by ram blocks not including any
1872 * gaps due to alignment or unplugs.
1874 (*rsp
)->migration_dirty_pages
= ram_bytes_total() >> TARGET_PAGE_BITS
;
1876 memory_global_dirty_log_start();
1877 migration_bitmap_sync(*rsp
);
1878 qemu_mutex_unlock_ramlist();
1879 qemu_mutex_unlock_iothread();
1886 * Each of ram_save_setup, ram_save_iterate and ram_save_complete has
1887 * long-running RCU critical section. When rcu-reclaims in the code
1888 * start to become numerous it will be necessary to reduce the
1889 * granularity of these critical sections.
1893 * ram_save_setup: Setup RAM for migration
1895 * Returns zero to indicate success and negative for error
1897 * @f: QEMUFile where to send the data
1898 * @opaque: RAMState pointer
1900 static int ram_save_setup(QEMUFile
*f
, void *opaque
)
1902 RAMState
**rsp
= opaque
;
1905 /* migration has already setup the bitmap, reuse it. */
1906 if (!migration_in_colo_state()) {
1907 if (ram_state_init(rsp
) != 0) {
1915 qemu_put_be64(f
, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE
);
1917 RAMBLOCK_FOREACH(block
) {
1918 qemu_put_byte(f
, strlen(block
->idstr
));
1919 qemu_put_buffer(f
, (uint8_t *)block
->idstr
, strlen(block
->idstr
));
1920 qemu_put_be64(f
, block
->used_length
);
1921 if (migrate_postcopy_ram() && block
->page_size
!= qemu_host_page_size
) {
1922 qemu_put_be64(f
, block
->page_size
);
1927 compress_threads_save_setup();
1929 ram_control_before_iterate(f
, RAM_CONTROL_SETUP
);
1930 ram_control_after_iterate(f
, RAM_CONTROL_SETUP
);
1932 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1938 * ram_save_iterate: iterative stage for migration
1940 * Returns zero to indicate success and negative for error
1942 * @f: QEMUFile where to send the data
1943 * @opaque: RAMState pointer
1945 static int ram_save_iterate(QEMUFile
*f
, void *opaque
)
1947 RAMState
**temp
= opaque
;
1948 RAMState
*rs
= *temp
;
1955 if (ram_list
.version
!= rs
->last_version
) {
1956 ram_state_reset(rs
);
1959 /* Read version before ram_list.blocks */
1962 ram_control_before_iterate(f
, RAM_CONTROL_ROUND
);
1964 t0
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
1966 while ((ret
= qemu_file_rate_limit(f
)) == 0) {
1969 pages
= ram_find_and_save_block(rs
, false);
1970 /* no more pages to sent */
1977 /* we want to check in the 1st loop, just in case it was the 1st time
1978 and we had to sync the dirty bitmap.
1979 qemu_get_clock_ns() is a bit expensive, so we only check each some
1982 if ((i
& 63) == 0) {
1983 uint64_t t1
= (qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - t0
) / 1000000;
1984 if (t1
> MAX_WAIT
) {
1985 trace_ram_save_iterate_big_wait(t1
, i
);
1991 flush_compressed_data(rs
);
1995 * Must occur before EOS (or any QEMUFile operation)
1996 * because of RDMA protocol.
1998 ram_control_after_iterate(f
, RAM_CONTROL_ROUND
);
2000 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2001 ram_counters
.transferred
+= 8;
2003 ret
= qemu_file_get_error(f
);
2012 * ram_save_complete: function called to send the remaining amount of ram
2014 * Returns zero to indicate success
2016 * Called with iothread lock
2018 * @f: QEMUFile where to send the data
2019 * @opaque: RAMState pointer
2021 static int ram_save_complete(QEMUFile
*f
, void *opaque
)
2023 RAMState
**temp
= opaque
;
2024 RAMState
*rs
= *temp
;
2028 if (!migration_in_postcopy()) {
2029 migration_bitmap_sync(rs
);
2032 ram_control_before_iterate(f
, RAM_CONTROL_FINISH
);
2034 /* try transferring iterative blocks of memory */
2036 /* flush all remaining blocks regardless of rate limiting */
2040 pages
= ram_find_and_save_block(rs
, !migration_in_colo_state());
2041 /* no more blocks to sent */
2047 flush_compressed_data(rs
);
2048 ram_control_after_iterate(f
, RAM_CONTROL_FINISH
);
2052 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2057 static void ram_save_pending(QEMUFile
*f
, void *opaque
, uint64_t max_size
,
2058 uint64_t *non_postcopiable_pending
,
2059 uint64_t *postcopiable_pending
)
2061 RAMState
**temp
= opaque
;
2062 RAMState
*rs
= *temp
;
2063 uint64_t remaining_size
;
2065 remaining_size
= rs
->migration_dirty_pages
* TARGET_PAGE_SIZE
;
2067 if (!migration_in_postcopy() &&
2068 remaining_size
< max_size
) {
2069 qemu_mutex_lock_iothread();
2071 migration_bitmap_sync(rs
);
2073 qemu_mutex_unlock_iothread();
2074 remaining_size
= rs
->migration_dirty_pages
* TARGET_PAGE_SIZE
;
2077 /* We can do postcopy, and all the data is postcopiable */
2078 *postcopiable_pending
+= remaining_size
;
2081 static int load_xbzrle(QEMUFile
*f
, ram_addr_t addr
, void *host
)
2083 unsigned int xh_len
;
2085 uint8_t *loaded_data
;
2087 /* extract RLE header */
2088 xh_flags
= qemu_get_byte(f
);
2089 xh_len
= qemu_get_be16(f
);
2091 if (xh_flags
!= ENCODING_FLAG_XBZRLE
) {
2092 error_report("Failed to load XBZRLE page - wrong compression!");
2096 if (xh_len
> TARGET_PAGE_SIZE
) {
2097 error_report("Failed to load XBZRLE page - len overflow!");
2100 loaded_data
= XBZRLE
.decoded_buf
;
2101 /* load data and decode */
2102 /* it can change loaded_data to point to an internal buffer */
2103 qemu_get_buffer_in_place(f
, &loaded_data
, xh_len
);
2106 if (xbzrle_decode_buffer(loaded_data
, xh_len
, host
,
2107 TARGET_PAGE_SIZE
) == -1) {
2108 error_report("Failed to load XBZRLE page - decode error!");
2116 * ram_block_from_stream: read a RAMBlock id from the migration stream
2118 * Must be called from within a rcu critical section.
2120 * Returns a pointer from within the RCU-protected ram_list.
2122 * @f: QEMUFile where to read the data from
2123 * @flags: Page flags (mostly to see if it's a continuation of previous block)
2125 static inline RAMBlock
*ram_block_from_stream(QEMUFile
*f
, int flags
)
2127 static RAMBlock
*block
= NULL
;
2131 if (flags
& RAM_SAVE_FLAG_CONTINUE
) {
2133 error_report("Ack, bad migration stream!");
2139 len
= qemu_get_byte(f
);
2140 qemu_get_buffer(f
, (uint8_t *)id
, len
);
2143 block
= qemu_ram_block_by_name(id
);
2145 error_report("Can't find block %s", id
);
2152 static inline void *host_from_ram_block_offset(RAMBlock
*block
,
2155 if (!offset_in_ramblock(block
, offset
)) {
2159 return block
->host
+ offset
;
2163 * ram_handle_compressed: handle the zero page case
2165 * If a page (or a whole RDMA chunk) has been
2166 * determined to be zero, then zap it.
2168 * @host: host address for the zero page
2169 * @ch: what the page is filled from. We only support zero
2170 * @size: size of the zero page
2172 void ram_handle_compressed(void *host
, uint8_t ch
, uint64_t size
)
2174 if (ch
!= 0 || !is_zero_range(host
, size
)) {
2175 memset(host
, ch
, size
);
2179 static void *do_data_decompress(void *opaque
)
2181 DecompressParam
*param
= opaque
;
2182 unsigned long pagesize
;
2186 qemu_mutex_lock(¶m
->mutex
);
2187 while (!param
->quit
) {
2192 qemu_mutex_unlock(¶m
->mutex
);
2194 pagesize
= TARGET_PAGE_SIZE
;
2195 /* uncompress() will return failed in some case, especially
2196 * when the page is dirted when doing the compression, it's
2197 * not a problem because the dirty page will be retransferred
2198 * and uncompress() won't break the data in other pages.
2200 uncompress((Bytef
*)des
, &pagesize
,
2201 (const Bytef
*)param
->compbuf
, len
);
2203 qemu_mutex_lock(&decomp_done_lock
);
2205 qemu_cond_signal(&decomp_done_cond
);
2206 qemu_mutex_unlock(&decomp_done_lock
);
2208 qemu_mutex_lock(¶m
->mutex
);
2210 qemu_cond_wait(¶m
->cond
, ¶m
->mutex
);
2213 qemu_mutex_unlock(¶m
->mutex
);
2218 static void wait_for_decompress_done(void)
2220 int idx
, thread_count
;
2222 if (!migrate_use_compression()) {
2226 thread_count
= migrate_decompress_threads();
2227 qemu_mutex_lock(&decomp_done_lock
);
2228 for (idx
= 0; idx
< thread_count
; idx
++) {
2229 while (!decomp_param
[idx
].done
) {
2230 qemu_cond_wait(&decomp_done_cond
, &decomp_done_lock
);
2233 qemu_mutex_unlock(&decomp_done_lock
);
2236 static void compress_threads_load_setup(void)
2238 int i
, thread_count
;
2240 if (!migrate_use_compression()) {
2243 thread_count
= migrate_decompress_threads();
2244 decompress_threads
= g_new0(QemuThread
, thread_count
);
2245 decomp_param
= g_new0(DecompressParam
, thread_count
);
2246 qemu_mutex_init(&decomp_done_lock
);
2247 qemu_cond_init(&decomp_done_cond
);
2248 for (i
= 0; i
< thread_count
; i
++) {
2249 qemu_mutex_init(&decomp_param
[i
].mutex
);
2250 qemu_cond_init(&decomp_param
[i
].cond
);
2251 decomp_param
[i
].compbuf
= g_malloc0(compressBound(TARGET_PAGE_SIZE
));
2252 decomp_param
[i
].done
= true;
2253 decomp_param
[i
].quit
= false;
2254 qemu_thread_create(decompress_threads
+ i
, "decompress",
2255 do_data_decompress
, decomp_param
+ i
,
2256 QEMU_THREAD_JOINABLE
);
2260 static void compress_threads_load_cleanup(void)
2262 int i
, thread_count
;
2264 if (!migrate_use_compression()) {
2267 thread_count
= migrate_decompress_threads();
2268 for (i
= 0; i
< thread_count
; i
++) {
2269 qemu_mutex_lock(&decomp_param
[i
].mutex
);
2270 decomp_param
[i
].quit
= true;
2271 qemu_cond_signal(&decomp_param
[i
].cond
);
2272 qemu_mutex_unlock(&decomp_param
[i
].mutex
);
2274 for (i
= 0; i
< thread_count
; i
++) {
2275 qemu_thread_join(decompress_threads
+ i
);
2276 qemu_mutex_destroy(&decomp_param
[i
].mutex
);
2277 qemu_cond_destroy(&decomp_param
[i
].cond
);
2278 g_free(decomp_param
[i
].compbuf
);
2280 g_free(decompress_threads
);
2281 g_free(decomp_param
);
2282 decompress_threads
= NULL
;
2283 decomp_param
= NULL
;
2286 static void decompress_data_with_multi_threads(QEMUFile
*f
,
2287 void *host
, int len
)
2289 int idx
, thread_count
;
2291 thread_count
= migrate_decompress_threads();
2292 qemu_mutex_lock(&decomp_done_lock
);
2294 for (idx
= 0; idx
< thread_count
; idx
++) {
2295 if (decomp_param
[idx
].done
) {
2296 decomp_param
[idx
].done
= false;
2297 qemu_mutex_lock(&decomp_param
[idx
].mutex
);
2298 qemu_get_buffer(f
, decomp_param
[idx
].compbuf
, len
);
2299 decomp_param
[idx
].des
= host
;
2300 decomp_param
[idx
].len
= len
;
2301 qemu_cond_signal(&decomp_param
[idx
].cond
);
2302 qemu_mutex_unlock(&decomp_param
[idx
].mutex
);
2306 if (idx
< thread_count
) {
2309 qemu_cond_wait(&decomp_done_cond
, &decomp_done_lock
);
2312 qemu_mutex_unlock(&decomp_done_lock
);
2316 * ram_load_setup: Setup RAM for migration incoming side
2318 * Returns zero to indicate success and negative for error
2320 * @f: QEMUFile where to receive the data
2321 * @opaque: RAMState pointer
2323 static int ram_load_setup(QEMUFile
*f
, void *opaque
)
2325 xbzrle_load_setup();
2326 compress_threads_load_setup();
2330 static int ram_load_cleanup(void *opaque
)
2332 xbzrle_load_cleanup();
2333 compress_threads_load_cleanup();
2338 * ram_postcopy_incoming_init: allocate postcopy data structures
2340 * Returns 0 for success and negative if there was one error
2342 * @mis: current migration incoming state
2344 * Allocate data structures etc needed by incoming migration with
2345 * postcopy-ram. postcopy-ram's similarly names
2346 * postcopy_ram_incoming_init does the work.
2348 int ram_postcopy_incoming_init(MigrationIncomingState
*mis
)
2350 unsigned long ram_pages
= last_ram_page();
2352 return postcopy_ram_incoming_init(mis
, ram_pages
);
2356 * ram_load_postcopy: load a page in postcopy case
2358 * Returns 0 for success or -errno in case of error
2360 * Called in postcopy mode by ram_load().
2361 * rcu_read_lock is taken prior to this being called.
2363 * @f: QEMUFile where to send the data
2365 static int ram_load_postcopy(QEMUFile
*f
)
2367 int flags
= 0, ret
= 0;
2368 bool place_needed
= false;
2369 bool matching_page_sizes
= false;
2370 MigrationIncomingState
*mis
= migration_incoming_get_current();
2371 /* Temporary page that is later 'placed' */
2372 void *postcopy_host_page
= postcopy_get_tmp_page(mis
);
2373 void *last_host
= NULL
;
2374 bool all_zero
= false;
2376 while (!ret
&& !(flags
& RAM_SAVE_FLAG_EOS
)) {
2379 void *page_buffer
= NULL
;
2380 void *place_source
= NULL
;
2381 RAMBlock
*block
= NULL
;
2384 addr
= qemu_get_be64(f
);
2385 flags
= addr
& ~TARGET_PAGE_MASK
;
2386 addr
&= TARGET_PAGE_MASK
;
2388 trace_ram_load_postcopy_loop((uint64_t)addr
, flags
);
2389 place_needed
= false;
2390 if (flags
& (RAM_SAVE_FLAG_ZERO
| RAM_SAVE_FLAG_PAGE
)) {
2391 block
= ram_block_from_stream(f
, flags
);
2393 host
= host_from_ram_block_offset(block
, addr
);
2395 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
2399 matching_page_sizes
= block
->page_size
== TARGET_PAGE_SIZE
;
2401 * Postcopy requires that we place whole host pages atomically;
2402 * these may be huge pages for RAMBlocks that are backed by
2404 * To make it atomic, the data is read into a temporary page
2405 * that's moved into place later.
2406 * The migration protocol uses, possibly smaller, target-pages
2407 * however the source ensures it always sends all the components
2408 * of a host page in order.
2410 page_buffer
= postcopy_host_page
+
2411 ((uintptr_t)host
& (block
->page_size
- 1));
2412 /* If all TP are zero then we can optimise the place */
2413 if (!((uintptr_t)host
& (block
->page_size
- 1))) {
2416 /* not the 1st TP within the HP */
2417 if (host
!= (last_host
+ TARGET_PAGE_SIZE
)) {
2418 error_report("Non-sequential target page %p/%p",
2427 * If it's the last part of a host page then we place the host
2430 place_needed
= (((uintptr_t)host
+ TARGET_PAGE_SIZE
) &
2431 (block
->page_size
- 1)) == 0;
2432 place_source
= postcopy_host_page
;
2436 switch (flags
& ~RAM_SAVE_FLAG_CONTINUE
) {
2437 case RAM_SAVE_FLAG_ZERO
:
2438 ch
= qemu_get_byte(f
);
2439 memset(page_buffer
, ch
, TARGET_PAGE_SIZE
);
2445 case RAM_SAVE_FLAG_PAGE
:
2447 if (!place_needed
|| !matching_page_sizes
) {
2448 qemu_get_buffer(f
, page_buffer
, TARGET_PAGE_SIZE
);
2450 /* Avoids the qemu_file copy during postcopy, which is
2451 * going to do a copy later; can only do it when we
2452 * do this read in one go (matching page sizes)
2454 qemu_get_buffer_in_place(f
, (uint8_t **)&place_source
,
2458 case RAM_SAVE_FLAG_EOS
:
2462 error_report("Unknown combination of migration flags: %#x"
2463 " (postcopy mode)", flags
);
2468 /* This gets called at the last target page in the host page */
2469 void *place_dest
= host
+ TARGET_PAGE_SIZE
- block
->page_size
;
2472 ret
= postcopy_place_page_zero(mis
, place_dest
,
2475 ret
= postcopy_place_page(mis
, place_dest
,
2476 place_source
, block
->page_size
);
2480 ret
= qemu_file_get_error(f
);
2487 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2489 int flags
= 0, ret
= 0, invalid_flags
= 0;
2490 static uint64_t seq_iter
;
2493 * If system is running in postcopy mode, page inserts to host memory must
2496 bool postcopy_running
= postcopy_state_get() >= POSTCOPY_INCOMING_LISTENING
;
2497 /* ADVISE is earlier, it shows the source has the postcopy capability on */
2498 bool postcopy_advised
= postcopy_state_get() >= POSTCOPY_INCOMING_ADVISE
;
2502 if (version_id
!= 4) {
2506 if (!migrate_use_compression()) {
2507 invalid_flags
|= RAM_SAVE_FLAG_COMPRESS_PAGE
;
2509 /* This RCU critical section can be very long running.
2510 * When RCU reclaims in the code start to become numerous,
2511 * it will be necessary to reduce the granularity of this
2516 if (postcopy_running
) {
2517 ret
= ram_load_postcopy(f
);
2520 while (!postcopy_running
&& !ret
&& !(flags
& RAM_SAVE_FLAG_EOS
)) {
2521 ram_addr_t addr
, total_ram_bytes
;
2525 addr
= qemu_get_be64(f
);
2526 flags
= addr
& ~TARGET_PAGE_MASK
;
2527 addr
&= TARGET_PAGE_MASK
;
2529 if (flags
& invalid_flags
) {
2530 if (flags
& invalid_flags
& RAM_SAVE_FLAG_COMPRESS_PAGE
) {
2531 error_report("Received an unexpected compressed page");
2538 if (flags
& (RAM_SAVE_FLAG_ZERO
| RAM_SAVE_FLAG_PAGE
|
2539 RAM_SAVE_FLAG_COMPRESS_PAGE
| RAM_SAVE_FLAG_XBZRLE
)) {
2540 RAMBlock
*block
= ram_block_from_stream(f
, flags
);
2542 host
= host_from_ram_block_offset(block
, addr
);
2544 error_report("Illegal RAM offset " RAM_ADDR_FMT
, addr
);
2548 trace_ram_load_loop(block
->idstr
, (uint64_t)addr
, flags
, host
);
2551 switch (flags
& ~RAM_SAVE_FLAG_CONTINUE
) {
2552 case RAM_SAVE_FLAG_MEM_SIZE
:
2553 /* Synchronize RAM block list */
2554 total_ram_bytes
= addr
;
2555 while (!ret
&& total_ram_bytes
) {
2560 len
= qemu_get_byte(f
);
2561 qemu_get_buffer(f
, (uint8_t *)id
, len
);
2563 length
= qemu_get_be64(f
);
2565 block
= qemu_ram_block_by_name(id
);
2567 if (length
!= block
->used_length
) {
2568 Error
*local_err
= NULL
;
2570 ret
= qemu_ram_resize(block
, length
,
2573 error_report_err(local_err
);
2576 /* For postcopy we need to check hugepage sizes match */
2577 if (postcopy_advised
&&
2578 block
->page_size
!= qemu_host_page_size
) {
2579 uint64_t remote_page_size
= qemu_get_be64(f
);
2580 if (remote_page_size
!= block
->page_size
) {
2581 error_report("Mismatched RAM page size %s "
2582 "(local) %zd != %" PRId64
,
2583 id
, block
->page_size
,
2588 ram_control_load_hook(f
, RAM_CONTROL_BLOCK_REG
,
2591 error_report("Unknown ramblock \"%s\", cannot "
2592 "accept migration", id
);
2596 total_ram_bytes
-= length
;
2600 case RAM_SAVE_FLAG_ZERO
:
2601 ch
= qemu_get_byte(f
);
2602 ram_handle_compressed(host
, ch
, TARGET_PAGE_SIZE
);
2605 case RAM_SAVE_FLAG_PAGE
:
2606 qemu_get_buffer(f
, host
, TARGET_PAGE_SIZE
);
2609 case RAM_SAVE_FLAG_COMPRESS_PAGE
:
2610 len
= qemu_get_be32(f
);
2611 if (len
< 0 || len
> compressBound(TARGET_PAGE_SIZE
)) {
2612 error_report("Invalid compressed data length: %d", len
);
2616 decompress_data_with_multi_threads(f
, host
, len
);
2619 case RAM_SAVE_FLAG_XBZRLE
:
2620 if (load_xbzrle(f
, addr
, host
) < 0) {
2621 error_report("Failed to decompress XBZRLE page at "
2622 RAM_ADDR_FMT
, addr
);
2627 case RAM_SAVE_FLAG_EOS
:
2631 if (flags
& RAM_SAVE_FLAG_HOOK
) {
2632 ram_control_load_hook(f
, RAM_CONTROL_HOOK
, NULL
);
2634 error_report("Unknown combination of migration flags: %#x",
2640 ret
= qemu_file_get_error(f
);
2644 wait_for_decompress_done();
2646 trace_ram_load_complete(ret
, seq_iter
);
2650 static SaveVMHandlers savevm_ram_handlers
= {
2651 .save_setup
= ram_save_setup
,
2652 .save_live_iterate
= ram_save_iterate
,
2653 .save_live_complete_postcopy
= ram_save_complete
,
2654 .save_live_complete_precopy
= ram_save_complete
,
2655 .save_live_pending
= ram_save_pending
,
2656 .load_state
= ram_load
,
2657 .save_cleanup
= ram_save_cleanup
,
2658 .load_setup
= ram_load_setup
,
2659 .load_cleanup
= ram_load_cleanup
,
2662 void ram_mig_init(void)
2664 qemu_mutex_init(&XBZRLE
.lock
);
2665 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, &ram_state
);