]> git.proxmox.com Git - mirror_qemu.git/blame - migration/ram.c
ram: Move ZERO_TARGET_PAGE inside XBZRLE
[mirror_qemu.git] / migration / ram.c
CommitLineData
56e93d26
JQ
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
76cc7b58
JQ
5 * Copyright (c) 2011-2015 Red Hat Inc
6 *
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
56e93d26
JQ
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
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
26 * THE SOFTWARE.
27 */
1393a485 28#include "qemu/osdep.h"
33c11879
PB
29#include "qemu-common.h"
30#include "cpu.h"
56e93d26 31#include <zlib.h>
4addcd4f 32#include "qapi-event.h"
f348b6d1 33#include "qemu/cutils.h"
56e93d26
JQ
34#include "qemu/bitops.h"
35#include "qemu/bitmap.h"
7205c9ec
JQ
36#include "qemu/timer.h"
37#include "qemu/main-loop.h"
709e3fe8 38#include "xbzrle.h"
7b1e1a22 39#include "ram.h"
56e93d26 40#include "migration/migration.h"
7b1e1a22 41#include "migration/misc.h"
08a0aee1 42#include "qemu-file.h"
987772d9 43#include "migration/vmstate.h"
be07b0ac 44#include "postcopy-ram.h"
56e93d26
JQ
45#include "exec/address-spaces.h"
46#include "migration/page_cache.h"
56e93d26 47#include "qemu/error-report.h"
56e93d26 48#include "trace.h"
56e93d26 49#include "exec/ram_addr.h"
56e93d26 50#include "qemu/rcu_queue.h"
a91246c9 51#include "migration/colo.h"
56e93d26 52
56e93d26
JQ
53/***********************************************************/
54/* ram save/restore */
55
bb890ed5
JQ
56/* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it
57 * worked for pages that where filled with the same char. We switched
58 * it to only search for the zero value. And to avoid confusion with
59 * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it.
60 */
61
56e93d26 62#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
bb890ed5 63#define RAM_SAVE_FLAG_ZERO 0x02
56e93d26
JQ
64#define RAM_SAVE_FLAG_MEM_SIZE 0x04
65#define RAM_SAVE_FLAG_PAGE 0x08
66#define RAM_SAVE_FLAG_EOS 0x10
67#define RAM_SAVE_FLAG_CONTINUE 0x20
68#define RAM_SAVE_FLAG_XBZRLE 0x40
69/* 0x80 is reserved in migration.h start with 0x100 next */
70#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
71
56e93d26
JQ
72static inline bool is_zero_range(uint8_t *p, uint64_t size)
73{
a1febc49 74 return buffer_is_zero(p, size);
56e93d26
JQ
75}
76
77/* struct contains XBZRLE cache and a static page
78 used by the compression */
79static struct {
80 /* buffer used for XBZRLE encoding */
81 uint8_t *encoded_buf;
82 /* buffer for storing page content */
83 uint8_t *current_buf;
84 /* Cache for XBZRLE, Protected by lock. */
85 PageCache *cache;
86 QemuMutex lock;
c00e0928
JQ
87 /* it will store a page full of zeros */
88 uint8_t *zero_target_page;
56e93d26
JQ
89} XBZRLE;
90
91/* buffer used for XBZRLE decoding */
92static uint8_t *xbzrle_decoded_buf;
93
94static void XBZRLE_cache_lock(void)
95{
96 if (migrate_use_xbzrle())
97 qemu_mutex_lock(&XBZRLE.lock);
98}
99
100static void XBZRLE_cache_unlock(void)
101{
102 if (migrate_use_xbzrle())
103 qemu_mutex_unlock(&XBZRLE.lock);
104}
105
3d0684b2
JQ
106/**
107 * xbzrle_cache_resize: resize the xbzrle cache
108 *
109 * This function is called from qmp_migrate_set_cache_size in main
110 * thread, possibly while a migration is in progress. A running
111 * migration may be using the cache and might finish during this call,
112 * hence changes to the cache are protected by XBZRLE.lock().
113 *
114 * Returns the new_size or negative in case of error.
115 *
116 * @new_size: new cache size
56e93d26
JQ
117 */
118int64_t xbzrle_cache_resize(int64_t new_size)
119{
120 PageCache *new_cache;
121 int64_t ret;
122
123 if (new_size < TARGET_PAGE_SIZE) {
124 return -1;
125 }
126
127 XBZRLE_cache_lock();
128
129 if (XBZRLE.cache != NULL) {
130 if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
131 goto out_new_size;
132 }
133 new_cache = cache_init(new_size / TARGET_PAGE_SIZE,
134 TARGET_PAGE_SIZE);
135 if (!new_cache) {
136 error_report("Error creating cache");
137 ret = -1;
138 goto out;
139 }
140
141 cache_fini(XBZRLE.cache);
142 XBZRLE.cache = new_cache;
143 }
144
145out_new_size:
146 ret = pow2floor(new_size);
147out:
148 XBZRLE_cache_unlock();
149 return ret;
150}
151
ec481c6c
JQ
152/*
153 * An outstanding page request, on the source, having been received
154 * and queued
155 */
156struct RAMSrcPageRequest {
157 RAMBlock *rb;
158 hwaddr offset;
159 hwaddr len;
160
161 QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req;
162};
163
6f37bb8b
JQ
164/* State of RAM for migration */
165struct RAMState {
204b88b8
JQ
166 /* QEMUFile used for this migration */
167 QEMUFile *f;
6f37bb8b
JQ
168 /* Last block that we have visited searching for dirty pages */
169 RAMBlock *last_seen_block;
170 /* Last block from where we have sent data */
171 RAMBlock *last_sent_block;
269ace29
JQ
172 /* Last dirty target page we have sent */
173 ram_addr_t last_page;
6f37bb8b
JQ
174 /* last ram version we have seen */
175 uint32_t last_version;
176 /* We are in the first round */
177 bool ram_bulk_stage;
8d820d6f
JQ
178 /* How many times we have dirty too many pages */
179 int dirty_rate_high_cnt;
5a987738
JQ
180 /* How many times we have synchronized the bitmap */
181 uint64_t bitmap_sync_count;
f664da80
JQ
182 /* these variables are used for bitmap sync */
183 /* last time we did a full bitmap_sync */
184 int64_t time_last_bitmap_sync;
eac74159 185 /* bytes transferred at start_time */
c4bdf0cf 186 uint64_t bytes_xfer_prev;
a66cd90c 187 /* number of dirty pages since start_time */
68908ed6 188 uint64_t num_dirty_pages_period;
b5833fde
JQ
189 /* xbzrle misses since the beginning of the period */
190 uint64_t xbzrle_cache_miss_prev;
36040d9c
JQ
191 /* number of iterations at the beginning of period */
192 uint64_t iterations_prev;
f7ccd61b
JQ
193 /* Accounting fields */
194 /* number of zero pages. It used to be pages filled by the same char. */
195 uint64_t zero_pages;
b4d1c6e7
JQ
196 /* number of normal transferred pages */
197 uint64_t norm_pages;
23b28c3c
JQ
198 /* Iterations since start */
199 uint64_t iterations;
f36ada95
JQ
200 /* xbzrle transmitted bytes. Notice that this is with
201 * compression, they can't be calculated from the pages */
07ed50a2 202 uint64_t xbzrle_bytes;
f36ada95
JQ
203 /* xbzrle transmmited pages */
204 uint64_t xbzrle_pages;
544c36f1
JQ
205 /* xbzrle number of cache miss */
206 uint64_t xbzrle_cache_miss;
b07016b6
JQ
207 /* xbzrle miss rate */
208 double xbzrle_cache_miss_rate;
180f61f7
JQ
209 /* xbzrle number of overflows */
210 uint64_t xbzrle_overflows;
0d8ec885
JQ
211 /* number of dirty bits in the bitmap */
212 uint64_t migration_dirty_pages;
2f4fde93
JQ
213 /* total number of bytes transferred */
214 uint64_t bytes_transferred;
47ad8619
JQ
215 /* number of dirtied pages in the last second */
216 uint64_t dirty_pages_rate;
96506894
JQ
217 /* Count of requests incoming from destination */
218 uint64_t postcopy_requests;
108cfae0
JQ
219 /* protects modification of the bitmap */
220 QemuMutex bitmap_mutex;
68a098f3
JQ
221 /* The RAMBlock used in the last src_page_requests */
222 RAMBlock *last_req_rb;
ec481c6c
JQ
223 /* Queue of outstanding page requests from the destination */
224 QemuMutex src_page_req_mutex;
225 QSIMPLEQ_HEAD(src_page_requests, RAMSrcPageRequest) src_page_requests;
6f37bb8b
JQ
226};
227typedef struct RAMState RAMState;
228
229static RAMState ram_state;
230
56e93d26
JQ
231uint64_t dup_mig_pages_transferred(void)
232{
f7ccd61b 233 return ram_state.zero_pages;
56e93d26
JQ
234}
235
56e93d26
JQ
236uint64_t norm_mig_pages_transferred(void)
237{
b4d1c6e7 238 return ram_state.norm_pages;
56e93d26
JQ
239}
240
241uint64_t xbzrle_mig_bytes_transferred(void)
242{
07ed50a2 243 return ram_state.xbzrle_bytes;
56e93d26
JQ
244}
245
246uint64_t xbzrle_mig_pages_transferred(void)
247{
f36ada95 248 return ram_state.xbzrle_pages;
56e93d26
JQ
249}
250
251uint64_t xbzrle_mig_pages_cache_miss(void)
252{
544c36f1 253 return ram_state.xbzrle_cache_miss;
56e93d26
JQ
254}
255
256double xbzrle_mig_cache_miss_rate(void)
257{
b07016b6 258 return ram_state.xbzrle_cache_miss_rate;
56e93d26
JQ
259}
260
261uint64_t xbzrle_mig_pages_overflow(void)
262{
180f61f7 263 return ram_state.xbzrle_overflows;
56e93d26
JQ
264}
265
9edabd4d 266uint64_t ram_bytes_transferred(void)
0d8ec885 267{
9edabd4d 268 return ram_state.bytes_transferred;
0d8ec885
JQ
269}
270
9edabd4d 271uint64_t ram_bytes_remaining(void)
2f4fde93 272{
9edabd4d 273 return ram_state.migration_dirty_pages * TARGET_PAGE_SIZE;
2f4fde93
JQ
274}
275
42d219d3
JQ
276uint64_t ram_dirty_sync_count(void)
277{
278 return ram_state.bitmap_sync_count;
279}
280
47ad8619
JQ
281uint64_t ram_dirty_pages_rate(void)
282{
283 return ram_state.dirty_pages_rate;
284}
285
96506894
JQ
286uint64_t ram_postcopy_requests(void)
287{
288 return ram_state.postcopy_requests;
289}
290
b8fb8cb7
DDAG
291/* used by the search for pages to send */
292struct PageSearchStatus {
293 /* Current block being searched */
294 RAMBlock *block;
a935e30f
JQ
295 /* Current page to search from */
296 unsigned long page;
b8fb8cb7
DDAG
297 /* Set once we wrap around */
298 bool complete_round;
299};
300typedef struct PageSearchStatus PageSearchStatus;
301
56e93d26 302struct CompressParam {
56e93d26 303 bool done;
90e56fb4 304 bool quit;
56e93d26
JQ
305 QEMUFile *file;
306 QemuMutex mutex;
307 QemuCond cond;
308 RAMBlock *block;
309 ram_addr_t offset;
310};
311typedef struct CompressParam CompressParam;
312
313struct DecompressParam {
73a8912b 314 bool done;
90e56fb4 315 bool quit;
56e93d26
JQ
316 QemuMutex mutex;
317 QemuCond cond;
318 void *des;
d341d9f3 319 uint8_t *compbuf;
56e93d26
JQ
320 int len;
321};
322typedef struct DecompressParam DecompressParam;
323
324static CompressParam *comp_param;
325static QemuThread *compress_threads;
326/* comp_done_cond is used to wake up the migration thread when
327 * one of the compression threads has finished the compression.
328 * comp_done_lock is used to co-work with comp_done_cond.
329 */
0d9f9a5c
LL
330static QemuMutex comp_done_lock;
331static QemuCond comp_done_cond;
56e93d26
JQ
332/* The empty QEMUFileOps will be used by file in CompressParam */
333static const QEMUFileOps empty_ops = { };
334
56e93d26
JQ
335static DecompressParam *decomp_param;
336static QemuThread *decompress_threads;
73a8912b
LL
337static QemuMutex decomp_done_lock;
338static QemuCond decomp_done_cond;
56e93d26 339
a7a9a88f
LL
340static int do_compress_ram_page(QEMUFile *f, RAMBlock *block,
341 ram_addr_t offset);
56e93d26
JQ
342
343static void *do_data_compress(void *opaque)
344{
345 CompressParam *param = opaque;
a7a9a88f
LL
346 RAMBlock *block;
347 ram_addr_t offset;
56e93d26 348
a7a9a88f 349 qemu_mutex_lock(&param->mutex);
90e56fb4 350 while (!param->quit) {
a7a9a88f
LL
351 if (param->block) {
352 block = param->block;
353 offset = param->offset;
354 param->block = NULL;
355 qemu_mutex_unlock(&param->mutex);
356
357 do_compress_ram_page(param->file, block, offset);
358
0d9f9a5c 359 qemu_mutex_lock(&comp_done_lock);
a7a9a88f 360 param->done = true;
0d9f9a5c
LL
361 qemu_cond_signal(&comp_done_cond);
362 qemu_mutex_unlock(&comp_done_lock);
a7a9a88f
LL
363
364 qemu_mutex_lock(&param->mutex);
365 } else {
56e93d26
JQ
366 qemu_cond_wait(&param->cond, &param->mutex);
367 }
56e93d26 368 }
a7a9a88f 369 qemu_mutex_unlock(&param->mutex);
56e93d26
JQ
370
371 return NULL;
372}
373
374static inline void terminate_compression_threads(void)
375{
376 int idx, thread_count;
377
378 thread_count = migrate_compress_threads();
3d0684b2 379
56e93d26
JQ
380 for (idx = 0; idx < thread_count; idx++) {
381 qemu_mutex_lock(&comp_param[idx].mutex);
90e56fb4 382 comp_param[idx].quit = true;
56e93d26
JQ
383 qemu_cond_signal(&comp_param[idx].cond);
384 qemu_mutex_unlock(&comp_param[idx].mutex);
385 }
386}
387
388void migrate_compress_threads_join(void)
389{
390 int i, thread_count;
391
392 if (!migrate_use_compression()) {
393 return;
394 }
395 terminate_compression_threads();
396 thread_count = migrate_compress_threads();
397 for (i = 0; i < thread_count; i++) {
398 qemu_thread_join(compress_threads + i);
399 qemu_fclose(comp_param[i].file);
400 qemu_mutex_destroy(&comp_param[i].mutex);
401 qemu_cond_destroy(&comp_param[i].cond);
402 }
0d9f9a5c
LL
403 qemu_mutex_destroy(&comp_done_lock);
404 qemu_cond_destroy(&comp_done_cond);
56e93d26
JQ
405 g_free(compress_threads);
406 g_free(comp_param);
56e93d26
JQ
407 compress_threads = NULL;
408 comp_param = NULL;
56e93d26
JQ
409}
410
411void migrate_compress_threads_create(void)
412{
413 int i, thread_count;
414
415 if (!migrate_use_compression()) {
416 return;
417 }
56e93d26
JQ
418 thread_count = migrate_compress_threads();
419 compress_threads = g_new0(QemuThread, thread_count);
420 comp_param = g_new0(CompressParam, thread_count);
0d9f9a5c
LL
421 qemu_cond_init(&comp_done_cond);
422 qemu_mutex_init(&comp_done_lock);
56e93d26 423 for (i = 0; i < thread_count; i++) {
e110aa91
C
424 /* comp_param[i].file is just used as a dummy buffer to save data,
425 * set its ops to empty.
56e93d26
JQ
426 */
427 comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops);
428 comp_param[i].done = true;
90e56fb4 429 comp_param[i].quit = false;
56e93d26
JQ
430 qemu_mutex_init(&comp_param[i].mutex);
431 qemu_cond_init(&comp_param[i].cond);
432 qemu_thread_create(compress_threads + i, "compress",
433 do_data_compress, comp_param + i,
434 QEMU_THREAD_JOINABLE);
435 }
436}
437
438/**
3d0684b2 439 * save_page_header: write page header to wire
56e93d26
JQ
440 *
441 * If this is the 1st block, it also writes the block identification
442 *
3d0684b2 443 * Returns the number of bytes written
56e93d26
JQ
444 *
445 * @f: QEMUFile where to send the data
446 * @block: block that contains the page we want to send
447 * @offset: offset inside the block for the page
448 * in the lower bits, it contains flags
449 */
2bf3aa85
JQ
450static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block,
451 ram_addr_t offset)
56e93d26 452{
9f5f380b 453 size_t size, len;
56e93d26 454
24795694
JQ
455 if (block == rs->last_sent_block) {
456 offset |= RAM_SAVE_FLAG_CONTINUE;
457 }
2bf3aa85 458 qemu_put_be64(f, offset);
56e93d26
JQ
459 size = 8;
460
461 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
9f5f380b 462 len = strlen(block->idstr);
2bf3aa85
JQ
463 qemu_put_byte(f, len);
464 qemu_put_buffer(f, (uint8_t *)block->idstr, len);
9f5f380b 465 size += 1 + len;
24795694 466 rs->last_sent_block = block;
56e93d26
JQ
467 }
468 return size;
469}
470
3d0684b2
JQ
471/**
472 * mig_throttle_guest_down: throotle down the guest
473 *
474 * Reduce amount of guest cpu execution to hopefully slow down memory
475 * writes. If guest dirty memory rate is reduced below the rate at
476 * which we can transfer pages to the destination then we should be
477 * able to complete migration. Some workloads dirty memory way too
478 * fast and will not effectively converge, even with auto-converge.
070afca2
JH
479 */
480static void mig_throttle_guest_down(void)
481{
482 MigrationState *s = migrate_get_current();
2594f56d
DB
483 uint64_t pct_initial = s->parameters.cpu_throttle_initial;
484 uint64_t pct_icrement = s->parameters.cpu_throttle_increment;
070afca2
JH
485
486 /* We have not started throttling yet. Let's start it. */
487 if (!cpu_throttle_active()) {
488 cpu_throttle_set(pct_initial);
489 } else {
490 /* Throttling already on, just increase the rate */
491 cpu_throttle_set(cpu_throttle_get_percentage() + pct_icrement);
492 }
493}
494
3d0684b2
JQ
495/**
496 * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache
497 *
6f37bb8b 498 * @rs: current RAM state
3d0684b2
JQ
499 * @current_addr: address for the zero page
500 *
501 * Update the xbzrle cache to reflect a page that's been sent as all 0.
56e93d26
JQ
502 * The important thing is that a stale (not-yet-0'd) page be replaced
503 * by the new data.
504 * As a bonus, if the page wasn't in the cache it gets added so that
3d0684b2 505 * when a small write is made into the 0'd page it gets XBZRLE sent.
56e93d26 506 */
6f37bb8b 507static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr)
56e93d26 508{
6f37bb8b 509 if (rs->ram_bulk_stage || !migrate_use_xbzrle()) {
56e93d26
JQ
510 return;
511 }
512
513 /* We don't care if this fails to allocate a new cache page
514 * as long as it updated an old one */
c00e0928 515 cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page,
5a987738 516 rs->bitmap_sync_count);
56e93d26
JQ
517}
518
519#define ENCODING_FLAG_XBZRLE 0x1
520
521/**
522 * save_xbzrle_page: compress and send current page
523 *
524 * Returns: 1 means that we wrote the page
525 * 0 means that page is identical to the one already sent
526 * -1 means that xbzrle would be longer than normal
527 *
5a987738 528 * @rs: current RAM state
3d0684b2
JQ
529 * @current_data: pointer to the address of the page contents
530 * @current_addr: addr of the page
56e93d26
JQ
531 * @block: block that contains the page we want to send
532 * @offset: offset inside the block for the page
533 * @last_stage: if we are at the completion stage
56e93d26 534 */
204b88b8 535static int save_xbzrle_page(RAMState *rs, uint8_t **current_data,
56e93d26 536 ram_addr_t current_addr, RAMBlock *block,
072c2511 537 ram_addr_t offset, bool last_stage)
56e93d26
JQ
538{
539 int encoded_len = 0, bytes_xbzrle;
540 uint8_t *prev_cached_page;
541
5a987738 542 if (!cache_is_cached(XBZRLE.cache, current_addr, rs->bitmap_sync_count)) {
544c36f1 543 rs->xbzrle_cache_miss++;
56e93d26
JQ
544 if (!last_stage) {
545 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
5a987738 546 rs->bitmap_sync_count) == -1) {
56e93d26
JQ
547 return -1;
548 } else {
549 /* update *current_data when the page has been
550 inserted into cache */
551 *current_data = get_cached_data(XBZRLE.cache, current_addr);
552 }
553 }
554 return -1;
555 }
556
557 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
558
559 /* save current buffer into memory */
560 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
561
562 /* XBZRLE encoding (if there is no overflow) */
563 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
564 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
565 TARGET_PAGE_SIZE);
566 if (encoded_len == 0) {
55c4446b 567 trace_save_xbzrle_page_skipping();
56e93d26
JQ
568 return 0;
569 } else if (encoded_len == -1) {
55c4446b 570 trace_save_xbzrle_page_overflow();
180f61f7 571 rs->xbzrle_overflows++;
56e93d26
JQ
572 /* update data in the cache */
573 if (!last_stage) {
574 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
575 *current_data = prev_cached_page;
576 }
577 return -1;
578 }
579
580 /* we need to update the data in the cache, in order to get the same data */
581 if (!last_stage) {
582 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
583 }
584
585 /* Send XBZRLE based compressed page */
2bf3aa85 586 bytes_xbzrle = save_page_header(rs, rs->f, block,
204b88b8
JQ
587 offset | RAM_SAVE_FLAG_XBZRLE);
588 qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE);
589 qemu_put_be16(rs->f, encoded_len);
590 qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len);
56e93d26 591 bytes_xbzrle += encoded_len + 1 + 2;
f36ada95 592 rs->xbzrle_pages++;
07ed50a2 593 rs->xbzrle_bytes += bytes_xbzrle;
072c2511 594 rs->bytes_transferred += bytes_xbzrle;
56e93d26
JQ
595
596 return 1;
597}
598
3d0684b2
JQ
599/**
600 * migration_bitmap_find_dirty: find the next dirty page from start
f3f491fc 601 *
3d0684b2
JQ
602 * Called with rcu_read_lock() to protect migration_bitmap
603 *
604 * Returns the byte offset within memory region of the start of a dirty page
605 *
6f37bb8b 606 * @rs: current RAM state
3d0684b2 607 * @rb: RAMBlock where to search for dirty pages
a935e30f 608 * @start: page where we start the search
f3f491fc 609 */
56e93d26 610static inline
a935e30f 611unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb,
f20e2865 612 unsigned long start)
56e93d26 613{
6b6712ef
JQ
614 unsigned long size = rb->used_length >> TARGET_PAGE_BITS;
615 unsigned long *bitmap = rb->bmap;
56e93d26
JQ
616 unsigned long next;
617
6b6712ef
JQ
618 if (rs->ram_bulk_stage && start > 0) {
619 next = start + 1;
56e93d26 620 } else {
6b6712ef 621 next = find_next_bit(bitmap, size, start);
56e93d26
JQ
622 }
623
6b6712ef 624 return next;
56e93d26
JQ
625}
626
06b10688 627static inline bool migration_bitmap_clear_dirty(RAMState *rs,
f20e2865
JQ
628 RAMBlock *rb,
629 unsigned long page)
a82d593b
DDAG
630{
631 bool ret;
a82d593b 632
6b6712ef 633 ret = test_and_clear_bit(page, rb->bmap);
a82d593b
DDAG
634
635 if (ret) {
0d8ec885 636 rs->migration_dirty_pages--;
a82d593b
DDAG
637 }
638 return ret;
639}
640
15440dd5
JQ
641static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb,
642 ram_addr_t start, ram_addr_t length)
56e93d26 643{
0d8ec885 644 rs->migration_dirty_pages +=
6b6712ef 645 cpu_physical_memory_sync_dirty_bitmap(rb, start, length,
0d8ec885 646 &rs->num_dirty_pages_period);
56e93d26
JQ
647}
648
3d0684b2
JQ
649/**
650 * ram_pagesize_summary: calculate all the pagesizes of a VM
651 *
652 * Returns a summary bitmap of the page sizes of all RAMBlocks
653 *
654 * For VMs with just normal pages this is equivalent to the host page
655 * size. If it's got some huge pages then it's the OR of all the
656 * different page sizes.
e8ca1db2
DDAG
657 */
658uint64_t ram_pagesize_summary(void)
659{
660 RAMBlock *block;
661 uint64_t summary = 0;
662
99e15582 663 RAMBLOCK_FOREACH(block) {
e8ca1db2
DDAG
664 summary |= block->page_size;
665 }
666
667 return summary;
668}
669
8d820d6f 670static void migration_bitmap_sync(RAMState *rs)
56e93d26
JQ
671{
672 RAMBlock *block;
56e93d26 673 int64_t end_time;
c4bdf0cf 674 uint64_t bytes_xfer_now;
56e93d26 675
5a987738 676 rs->bitmap_sync_count++;
56e93d26 677
f664da80
JQ
678 if (!rs->time_last_bitmap_sync) {
679 rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
56e93d26
JQ
680 }
681
682 trace_migration_bitmap_sync_start();
9c1f8f44 683 memory_global_dirty_log_sync();
56e93d26 684
108cfae0 685 qemu_mutex_lock(&rs->bitmap_mutex);
56e93d26 686 rcu_read_lock();
99e15582 687 RAMBLOCK_FOREACH(block) {
15440dd5 688 migration_bitmap_sync_range(rs, block, 0, block->used_length);
56e93d26
JQ
689 }
690 rcu_read_unlock();
108cfae0 691 qemu_mutex_unlock(&rs->bitmap_mutex);
56e93d26 692
a66cd90c 693 trace_migration_bitmap_sync_end(rs->num_dirty_pages_period);
1ffb5dfd 694
56e93d26
JQ
695 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
696
697 /* more than 1 second = 1000 millisecons */
f664da80 698 if (end_time > rs->time_last_bitmap_sync + 1000) {
d693c6f1
FF
699 /* calculate period counters */
700 rs->dirty_pages_rate = rs->num_dirty_pages_period * 1000
701 / (end_time - rs->time_last_bitmap_sync);
d2a4d85a 702 bytes_xfer_now = ram_bytes_transferred();
d693c6f1 703
56e93d26
JQ
704 if (migrate_auto_converge()) {
705 /* The following detection logic can be refined later. For now:
706 Check to see if the dirtied bytes is 50% more than the approx.
707 amount of bytes that just got transferred since the last time we
070afca2
JH
708 were in this routine. If that happens twice, start or increase
709 throttling */
070afca2 710
d693c6f1 711 if ((rs->num_dirty_pages_period * TARGET_PAGE_SIZE >
eac74159 712 (bytes_xfer_now - rs->bytes_xfer_prev) / 2) &&
b4a3c64b 713 (++rs->dirty_rate_high_cnt >= 2)) {
56e93d26 714 trace_migration_throttle();
8d820d6f 715 rs->dirty_rate_high_cnt = 0;
070afca2 716 mig_throttle_guest_down();
d693c6f1 717 }
56e93d26 718 }
070afca2 719
56e93d26 720 if (migrate_use_xbzrle()) {
23b28c3c 721 if (rs->iterations_prev != rs->iterations) {
b07016b6 722 rs->xbzrle_cache_miss_rate =
544c36f1 723 (double)(rs->xbzrle_cache_miss -
b5833fde 724 rs->xbzrle_cache_miss_prev) /
23b28c3c 725 (rs->iterations - rs->iterations_prev);
56e93d26 726 }
23b28c3c 727 rs->iterations_prev = rs->iterations;
544c36f1 728 rs->xbzrle_cache_miss_prev = rs->xbzrle_cache_miss;
56e93d26 729 }
d693c6f1
FF
730
731 /* reset period counters */
f664da80 732 rs->time_last_bitmap_sync = end_time;
a66cd90c 733 rs->num_dirty_pages_period = 0;
d2a4d85a 734 rs->bytes_xfer_prev = bytes_xfer_now;
56e93d26 735 }
4addcd4f 736 if (migrate_use_events()) {
5a987738 737 qapi_event_send_migration_pass(rs->bitmap_sync_count, NULL);
4addcd4f 738 }
56e93d26
JQ
739}
740
741/**
3d0684b2 742 * save_zero_page: send the zero page to the stream
56e93d26 743 *
3d0684b2 744 * Returns the number of pages written.
56e93d26 745 *
f7ccd61b 746 * @rs: current RAM state
56e93d26
JQ
747 * @block: block that contains the page we want to send
748 * @offset: offset inside the block for the page
749 * @p: pointer to the page
56e93d26 750 */
ce25d337
JQ
751static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
752 uint8_t *p)
56e93d26
JQ
753{
754 int pages = -1;
755
756 if (is_zero_range(p, TARGET_PAGE_SIZE)) {
f7ccd61b 757 rs->zero_pages++;
072c2511 758 rs->bytes_transferred +=
bb890ed5 759 save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_ZERO);
ce25d337 760 qemu_put_byte(rs->f, 0);
072c2511 761 rs->bytes_transferred += 1;
56e93d26
JQ
762 pages = 1;
763 }
764
765 return pages;
766}
767
5727309d 768static void ram_release_pages(const char *rbname, uint64_t offset, int pages)
53f09a10 769{
5727309d 770 if (!migrate_release_ram() || !migration_in_postcopy()) {
53f09a10
PB
771 return;
772 }
773
aaa2064c 774 ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS);
53f09a10
PB
775}
776
56e93d26 777/**
3d0684b2 778 * ram_save_page: send the given page to the stream
56e93d26 779 *
3d0684b2 780 * Returns the number of pages written.
3fd3c4b3
DDAG
781 * < 0 - error
782 * >=0 - Number of pages written - this might legally be 0
783 * if xbzrle noticed the page was the same.
56e93d26 784 *
6f37bb8b 785 * @rs: current RAM state
56e93d26
JQ
786 * @block: block that contains the page we want to send
787 * @offset: offset inside the block for the page
788 * @last_stage: if we are at the completion stage
56e93d26 789 */
a0a8aa14 790static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
56e93d26
JQ
791{
792 int pages = -1;
793 uint64_t bytes_xmit;
794 ram_addr_t current_addr;
56e93d26
JQ
795 uint8_t *p;
796 int ret;
797 bool send_async = true;
a08f6890 798 RAMBlock *block = pss->block;
a935e30f 799 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
56e93d26 800
2f68e399 801 p = block->host + offset;
1db9d8e5 802 trace_ram_save_page(block->idstr, (uint64_t)offset, p);
56e93d26
JQ
803
804 /* In doubt sent page as normal */
805 bytes_xmit = 0;
ce25d337 806 ret = ram_control_save_page(rs->f, block->offset,
56e93d26
JQ
807 offset, TARGET_PAGE_SIZE, &bytes_xmit);
808 if (bytes_xmit) {
072c2511 809 rs->bytes_transferred += bytes_xmit;
56e93d26
JQ
810 pages = 1;
811 }
812
813 XBZRLE_cache_lock();
814
815 current_addr = block->offset + offset;
816
56e93d26
JQ
817 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
818 if (ret != RAM_SAVE_CONTROL_DELAYED) {
819 if (bytes_xmit > 0) {
b4d1c6e7 820 rs->norm_pages++;
56e93d26 821 } else if (bytes_xmit == 0) {
f7ccd61b 822 rs->zero_pages++;
56e93d26
JQ
823 }
824 }
825 } else {
ce25d337 826 pages = save_zero_page(rs, block, offset, p);
56e93d26
JQ
827 if (pages > 0) {
828 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
829 * page would be stale
830 */
6f37bb8b 831 xbzrle_cache_zero_page(rs, current_addr);
a935e30f 832 ram_release_pages(block->idstr, offset, pages);
6f37bb8b 833 } else if (!rs->ram_bulk_stage &&
5727309d 834 !migration_in_postcopy() && migrate_use_xbzrle()) {
204b88b8 835 pages = save_xbzrle_page(rs, &p, current_addr, block,
072c2511 836 offset, last_stage);
56e93d26
JQ
837 if (!last_stage) {
838 /* Can't send this cached data async, since the cache page
839 * might get updated before it gets to the wire
840 */
841 send_async = false;
842 }
843 }
844 }
845
846 /* XBZRLE overflow or normal page */
847 if (pages == -1) {
2bf3aa85 848 rs->bytes_transferred += save_page_header(rs, rs->f, block,
24795694 849 offset | RAM_SAVE_FLAG_PAGE);
56e93d26 850 if (send_async) {
ce25d337 851 qemu_put_buffer_async(rs->f, p, TARGET_PAGE_SIZE,
53f09a10 852 migrate_release_ram() &
5727309d 853 migration_in_postcopy());
56e93d26 854 } else {
ce25d337 855 qemu_put_buffer(rs->f, p, TARGET_PAGE_SIZE);
56e93d26 856 }
072c2511 857 rs->bytes_transferred += TARGET_PAGE_SIZE;
56e93d26 858 pages = 1;
b4d1c6e7 859 rs->norm_pages++;
56e93d26
JQ
860 }
861
862 XBZRLE_cache_unlock();
863
864 return pages;
865}
866
a7a9a88f
LL
867static int do_compress_ram_page(QEMUFile *f, RAMBlock *block,
868 ram_addr_t offset)
56e93d26 869{
24795694 870 RAMState *rs = &ram_state;
56e93d26 871 int bytes_sent, blen;
a7a9a88f 872 uint8_t *p = block->host + (offset & TARGET_PAGE_MASK);
56e93d26 873
2bf3aa85 874 bytes_sent = save_page_header(rs, f, block, offset |
56e93d26 875 RAM_SAVE_FLAG_COMPRESS_PAGE);
a7a9a88f 876 blen = qemu_put_compression_data(f, p, TARGET_PAGE_SIZE,
56e93d26 877 migrate_compress_level());
b3be2896
LL
878 if (blen < 0) {
879 bytes_sent = 0;
880 qemu_file_set_error(migrate_get_current()->to_dst_file, blen);
881 error_report("compressed data failed!");
882 } else {
883 bytes_sent += blen;
5727309d 884 ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1);
b3be2896 885 }
56e93d26
JQ
886
887 return bytes_sent;
888}
889
ce25d337 890static void flush_compressed_data(RAMState *rs)
56e93d26
JQ
891{
892 int idx, len, thread_count;
893
894 if (!migrate_use_compression()) {
895 return;
896 }
897 thread_count = migrate_compress_threads();
a7a9a88f 898
0d9f9a5c 899 qemu_mutex_lock(&comp_done_lock);
56e93d26 900 for (idx = 0; idx < thread_count; idx++) {
a7a9a88f 901 while (!comp_param[idx].done) {
0d9f9a5c 902 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
56e93d26 903 }
a7a9a88f 904 }
0d9f9a5c 905 qemu_mutex_unlock(&comp_done_lock);
a7a9a88f
LL
906
907 for (idx = 0; idx < thread_count; idx++) {
908 qemu_mutex_lock(&comp_param[idx].mutex);
90e56fb4 909 if (!comp_param[idx].quit) {
ce25d337 910 len = qemu_put_qemu_file(rs->f, comp_param[idx].file);
2f4fde93 911 rs->bytes_transferred += len;
56e93d26 912 }
a7a9a88f 913 qemu_mutex_unlock(&comp_param[idx].mutex);
56e93d26
JQ
914 }
915}
916
917static inline void set_compress_params(CompressParam *param, RAMBlock *block,
918 ram_addr_t offset)
919{
920 param->block = block;
921 param->offset = offset;
922}
923
ce25d337
JQ
924static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block,
925 ram_addr_t offset)
56e93d26
JQ
926{
927 int idx, thread_count, bytes_xmit = -1, pages = -1;
928
929 thread_count = migrate_compress_threads();
0d9f9a5c 930 qemu_mutex_lock(&comp_done_lock);
56e93d26
JQ
931 while (true) {
932 for (idx = 0; idx < thread_count; idx++) {
933 if (comp_param[idx].done) {
a7a9a88f 934 comp_param[idx].done = false;
ce25d337 935 bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file);
a7a9a88f 936 qemu_mutex_lock(&comp_param[idx].mutex);
56e93d26 937 set_compress_params(&comp_param[idx], block, offset);
a7a9a88f
LL
938 qemu_cond_signal(&comp_param[idx].cond);
939 qemu_mutex_unlock(&comp_param[idx].mutex);
56e93d26 940 pages = 1;
b4d1c6e7 941 rs->norm_pages++;
072c2511 942 rs->bytes_transferred += bytes_xmit;
56e93d26
JQ
943 break;
944 }
945 }
946 if (pages > 0) {
947 break;
948 } else {
0d9f9a5c 949 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
56e93d26
JQ
950 }
951 }
0d9f9a5c 952 qemu_mutex_unlock(&comp_done_lock);
56e93d26
JQ
953
954 return pages;
955}
956
957/**
958 * ram_save_compressed_page: compress the given page and send it to the stream
959 *
3d0684b2 960 * Returns the number of pages written.
56e93d26 961 *
6f37bb8b 962 * @rs: current RAM state
56e93d26
JQ
963 * @block: block that contains the page we want to send
964 * @offset: offset inside the block for the page
965 * @last_stage: if we are at the completion stage
56e93d26 966 */
a0a8aa14
JQ
967static int ram_save_compressed_page(RAMState *rs, PageSearchStatus *pss,
968 bool last_stage)
56e93d26
JQ
969{
970 int pages = -1;
fc50438e 971 uint64_t bytes_xmit = 0;
56e93d26 972 uint8_t *p;
fc50438e 973 int ret, blen;
a08f6890 974 RAMBlock *block = pss->block;
a935e30f 975 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
56e93d26 976
2f68e399 977 p = block->host + offset;
56e93d26 978
ce25d337 979 ret = ram_control_save_page(rs->f, block->offset,
56e93d26
JQ
980 offset, TARGET_PAGE_SIZE, &bytes_xmit);
981 if (bytes_xmit) {
072c2511 982 rs->bytes_transferred += bytes_xmit;
56e93d26
JQ
983 pages = 1;
984 }
56e93d26
JQ
985 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
986 if (ret != RAM_SAVE_CONTROL_DELAYED) {
987 if (bytes_xmit > 0) {
b4d1c6e7 988 rs->norm_pages++;
56e93d26 989 } else if (bytes_xmit == 0) {
f7ccd61b 990 rs->zero_pages++;
56e93d26
JQ
991 }
992 }
993 } else {
994 /* When starting the process of a new block, the first page of
995 * the block should be sent out before other pages in the same
996 * block, and all the pages in last block should have been sent
997 * out, keeping this order is important, because the 'cont' flag
998 * is used to avoid resending the block name.
999 */
6f37bb8b 1000 if (block != rs->last_sent_block) {
ce25d337
JQ
1001 flush_compressed_data(rs);
1002 pages = save_zero_page(rs, block, offset, p);
56e93d26 1003 if (pages == -1) {
fc50438e 1004 /* Make sure the first page is sent out before other pages */
2bf3aa85 1005 bytes_xmit = save_page_header(rs, rs->f, block, offset |
fc50438e 1006 RAM_SAVE_FLAG_COMPRESS_PAGE);
ce25d337 1007 blen = qemu_put_compression_data(rs->f, p, TARGET_PAGE_SIZE,
fc50438e
LL
1008 migrate_compress_level());
1009 if (blen > 0) {
072c2511 1010 rs->bytes_transferred += bytes_xmit + blen;
b4d1c6e7 1011 rs->norm_pages++;
b3be2896 1012 pages = 1;
fc50438e 1013 } else {
ce25d337 1014 qemu_file_set_error(rs->f, blen);
fc50438e 1015 error_report("compressed data failed!");
b3be2896 1016 }
56e93d26 1017 }
53f09a10 1018 if (pages > 0) {
a935e30f 1019 ram_release_pages(block->idstr, offset, pages);
53f09a10 1020 }
56e93d26 1021 } else {
ce25d337 1022 pages = save_zero_page(rs, block, offset, p);
56e93d26 1023 if (pages == -1) {
ce25d337 1024 pages = compress_page_with_multi_thread(rs, block, offset);
53f09a10 1025 } else {
a935e30f 1026 ram_release_pages(block->idstr, offset, pages);
56e93d26
JQ
1027 }
1028 }
1029 }
1030
1031 return pages;
1032}
1033
3d0684b2
JQ
1034/**
1035 * find_dirty_block: find the next dirty page and update any state
1036 * associated with the search process.
b9e60928 1037 *
3d0684b2 1038 * Returns if a page is found
b9e60928 1039 *
6f37bb8b 1040 * @rs: current RAM state
3d0684b2
JQ
1041 * @pss: data about the state of the current dirty page scan
1042 * @again: set to false if the search has scanned the whole of RAM
b9e60928 1043 */
f20e2865 1044static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
b9e60928 1045{
f20e2865 1046 pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
6f37bb8b 1047 if (pss->complete_round && pss->block == rs->last_seen_block &&
a935e30f 1048 pss->page >= rs->last_page) {
b9e60928
DDAG
1049 /*
1050 * We've been once around the RAM and haven't found anything.
1051 * Give up.
1052 */
1053 *again = false;
1054 return false;
1055 }
a935e30f 1056 if ((pss->page << TARGET_PAGE_BITS) >= pss->block->used_length) {
b9e60928 1057 /* Didn't find anything in this RAM Block */
a935e30f 1058 pss->page = 0;
b9e60928
DDAG
1059 pss->block = QLIST_NEXT_RCU(pss->block, next);
1060 if (!pss->block) {
1061 /* Hit the end of the list */
1062 pss->block = QLIST_FIRST_RCU(&ram_list.blocks);
1063 /* Flag that we've looped */
1064 pss->complete_round = true;
6f37bb8b 1065 rs->ram_bulk_stage = false;
b9e60928
DDAG
1066 if (migrate_use_xbzrle()) {
1067 /* If xbzrle is on, stop using the data compression at this
1068 * point. In theory, xbzrle can do better than compression.
1069 */
ce25d337 1070 flush_compressed_data(rs);
b9e60928
DDAG
1071 }
1072 }
1073 /* Didn't find anything this time, but try again on the new block */
1074 *again = true;
1075 return false;
1076 } else {
1077 /* Can go around again, but... */
1078 *again = true;
1079 /* We've found something so probably don't need to */
1080 return true;
1081 }
1082}
1083
3d0684b2
JQ
1084/**
1085 * unqueue_page: gets a page of the queue
1086 *
a82d593b 1087 * Helper for 'get_queued_page' - gets a page off the queue
a82d593b 1088 *
3d0684b2
JQ
1089 * Returns the block of the page (or NULL if none available)
1090 *
ec481c6c 1091 * @rs: current RAM state
3d0684b2 1092 * @offset: used to return the offset within the RAMBlock
a82d593b 1093 */
f20e2865 1094static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)
a82d593b
DDAG
1095{
1096 RAMBlock *block = NULL;
1097
ec481c6c
JQ
1098 qemu_mutex_lock(&rs->src_page_req_mutex);
1099 if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) {
1100 struct RAMSrcPageRequest *entry =
1101 QSIMPLEQ_FIRST(&rs->src_page_requests);
a82d593b
DDAG
1102 block = entry->rb;
1103 *offset = entry->offset;
a82d593b
DDAG
1104
1105 if (entry->len > TARGET_PAGE_SIZE) {
1106 entry->len -= TARGET_PAGE_SIZE;
1107 entry->offset += TARGET_PAGE_SIZE;
1108 } else {
1109 memory_region_unref(block->mr);
ec481c6c 1110 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
a82d593b
DDAG
1111 g_free(entry);
1112 }
1113 }
ec481c6c 1114 qemu_mutex_unlock(&rs->src_page_req_mutex);
a82d593b
DDAG
1115
1116 return block;
1117}
1118
3d0684b2
JQ
1119/**
1120 * get_queued_page: unqueue a page from the postocpy requests
1121 *
1122 * Skips pages that are already sent (!dirty)
a82d593b 1123 *
3d0684b2 1124 * Returns if a queued page is found
a82d593b 1125 *
6f37bb8b 1126 * @rs: current RAM state
3d0684b2 1127 * @pss: data about the state of the current dirty page scan
a82d593b 1128 */
f20e2865 1129static bool get_queued_page(RAMState *rs, PageSearchStatus *pss)
a82d593b
DDAG
1130{
1131 RAMBlock *block;
1132 ram_addr_t offset;
1133 bool dirty;
1134
1135 do {
f20e2865 1136 block = unqueue_page(rs, &offset);
a82d593b
DDAG
1137 /*
1138 * We're sending this page, and since it's postcopy nothing else
1139 * will dirty it, and we must make sure it doesn't get sent again
1140 * even if this queue request was received after the background
1141 * search already sent it.
1142 */
1143 if (block) {
f20e2865
JQ
1144 unsigned long page;
1145
6b6712ef
JQ
1146 page = offset >> TARGET_PAGE_BITS;
1147 dirty = test_bit(page, block->bmap);
a82d593b 1148 if (!dirty) {
06b10688 1149 trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset,
6b6712ef 1150 page, test_bit(page, block->unsentmap));
a82d593b 1151 } else {
f20e2865 1152 trace_get_queued_page(block->idstr, (uint64_t)offset, page);
a82d593b
DDAG
1153 }
1154 }
1155
1156 } while (block && !dirty);
1157
1158 if (block) {
1159 /*
1160 * As soon as we start servicing pages out of order, then we have
1161 * to kill the bulk stage, since the bulk stage assumes
1162 * in (migration_bitmap_find_and_reset_dirty) that every page is
1163 * dirty, that's no longer true.
1164 */
6f37bb8b 1165 rs->ram_bulk_stage = false;
a82d593b
DDAG
1166
1167 /*
1168 * We want the background search to continue from the queued page
1169 * since the guest is likely to want other pages near to the page
1170 * it just requested.
1171 */
1172 pss->block = block;
a935e30f 1173 pss->page = offset >> TARGET_PAGE_BITS;
a82d593b
DDAG
1174 }
1175
1176 return !!block;
1177}
1178
6c595cde 1179/**
5e58f968
JQ
1180 * migration_page_queue_free: drop any remaining pages in the ram
1181 * request queue
6c595cde 1182 *
3d0684b2
JQ
1183 * It should be empty at the end anyway, but in error cases there may
1184 * be some left. in case that there is any page left, we drop it.
1185 *
6c595cde 1186 */
83c13382 1187static void migration_page_queue_free(RAMState *rs)
6c595cde 1188{
ec481c6c 1189 struct RAMSrcPageRequest *mspr, *next_mspr;
6c595cde
DDAG
1190 /* This queue generally should be empty - but in the case of a failed
1191 * migration might have some droppings in.
1192 */
1193 rcu_read_lock();
ec481c6c 1194 QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) {
6c595cde 1195 memory_region_unref(mspr->rb->mr);
ec481c6c 1196 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
6c595cde
DDAG
1197 g_free(mspr);
1198 }
1199 rcu_read_unlock();
1200}
1201
1202/**
3d0684b2
JQ
1203 * ram_save_queue_pages: queue the page for transmission
1204 *
1205 * A request from postcopy destination for example.
1206 *
1207 * Returns zero on success or negative on error
1208 *
3d0684b2
JQ
1209 * @rbname: Name of the RAMBLock of the request. NULL means the
1210 * same that last one.
1211 * @start: starting address from the start of the RAMBlock
1212 * @len: length (in bytes) to send
6c595cde 1213 */
96506894 1214int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len)
6c595cde
DDAG
1215{
1216 RAMBlock *ramblock;
68a098f3 1217 RAMState *rs = &ram_state;
6c595cde 1218
96506894 1219 rs->postcopy_requests++;
6c595cde
DDAG
1220 rcu_read_lock();
1221 if (!rbname) {
1222 /* Reuse last RAMBlock */
68a098f3 1223 ramblock = rs->last_req_rb;
6c595cde
DDAG
1224
1225 if (!ramblock) {
1226 /*
1227 * Shouldn't happen, we can't reuse the last RAMBlock if
1228 * it's the 1st request.
1229 */
1230 error_report("ram_save_queue_pages no previous block");
1231 goto err;
1232 }
1233 } else {
1234 ramblock = qemu_ram_block_by_name(rbname);
1235
1236 if (!ramblock) {
1237 /* We shouldn't be asked for a non-existent RAMBlock */
1238 error_report("ram_save_queue_pages no block '%s'", rbname);
1239 goto err;
1240 }
68a098f3 1241 rs->last_req_rb = ramblock;
6c595cde
DDAG
1242 }
1243 trace_ram_save_queue_pages(ramblock->idstr, start, len);
1244 if (start+len > ramblock->used_length) {
9458ad6b
JQ
1245 error_report("%s request overrun start=" RAM_ADDR_FMT " len="
1246 RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT,
6c595cde
DDAG
1247 __func__, start, len, ramblock->used_length);
1248 goto err;
1249 }
1250
ec481c6c
JQ
1251 struct RAMSrcPageRequest *new_entry =
1252 g_malloc0(sizeof(struct RAMSrcPageRequest));
6c595cde
DDAG
1253 new_entry->rb = ramblock;
1254 new_entry->offset = start;
1255 new_entry->len = len;
1256
1257 memory_region_ref(ramblock->mr);
ec481c6c
JQ
1258 qemu_mutex_lock(&rs->src_page_req_mutex);
1259 QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req);
1260 qemu_mutex_unlock(&rs->src_page_req_mutex);
6c595cde
DDAG
1261 rcu_read_unlock();
1262
1263 return 0;
1264
1265err:
1266 rcu_read_unlock();
1267 return -1;
1268}
1269
a82d593b 1270/**
3d0684b2 1271 * ram_save_target_page: save one target page
a82d593b 1272 *
3d0684b2 1273 * Returns the number of pages written
a82d593b 1274 *
6f37bb8b 1275 * @rs: current RAM state
3d0684b2 1276 * @ms: current migration state
3d0684b2 1277 * @pss: data about the page we want to send
a82d593b 1278 * @last_stage: if we are at the completion stage
a82d593b 1279 */
a0a8aa14 1280static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,
f20e2865 1281 bool last_stage)
a82d593b
DDAG
1282{
1283 int res = 0;
1284
1285 /* Check the pages is dirty and if it is send it */
f20e2865 1286 if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
6d358d94
JQ
1287 /*
1288 * If xbzrle is on, stop using the data compression after first
1289 * round of migration even if compression is enabled. In theory,
1290 * xbzrle can do better than compression.
1291 */
6b6712ef
JQ
1292 if (migrate_use_compression() &&
1293 (rs->ram_bulk_stage || !migrate_use_xbzrle())) {
a0a8aa14 1294 res = ram_save_compressed_page(rs, pss, last_stage);
a82d593b 1295 } else {
a0a8aa14 1296 res = ram_save_page(rs, pss, last_stage);
a82d593b
DDAG
1297 }
1298
1299 if (res < 0) {
1300 return res;
1301 }
6b6712ef
JQ
1302 if (pss->block->unsentmap) {
1303 clear_bit(pss->page, pss->block->unsentmap);
a82d593b
DDAG
1304 }
1305 }
1306
1307 return res;
1308}
1309
1310/**
3d0684b2 1311 * ram_save_host_page: save a whole host page
a82d593b 1312 *
3d0684b2
JQ
1313 * Starting at *offset send pages up to the end of the current host
1314 * page. It's valid for the initial offset to point into the middle of
1315 * a host page in which case the remainder of the hostpage is sent.
1316 * Only dirty target pages are sent. Note that the host page size may
1317 * be a huge page for this block.
1eb3fc0a
DDAG
1318 * The saving stops at the boundary of the used_length of the block
1319 * if the RAMBlock isn't a multiple of the host page size.
a82d593b 1320 *
3d0684b2
JQ
1321 * Returns the number of pages written or negative on error
1322 *
6f37bb8b 1323 * @rs: current RAM state
3d0684b2 1324 * @ms: current migration state
3d0684b2 1325 * @pss: data about the page we want to send
a82d593b 1326 * @last_stage: if we are at the completion stage
a82d593b 1327 */
a0a8aa14 1328static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
f20e2865 1329 bool last_stage)
a82d593b
DDAG
1330{
1331 int tmppages, pages = 0;
a935e30f
JQ
1332 size_t pagesize_bits =
1333 qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
4c011c37 1334
a82d593b 1335 do {
f20e2865 1336 tmppages = ram_save_target_page(rs, pss, last_stage);
a82d593b
DDAG
1337 if (tmppages < 0) {
1338 return tmppages;
1339 }
1340
1341 pages += tmppages;
a935e30f 1342 pss->page++;
1eb3fc0a
DDAG
1343 } while ((pss->page & (pagesize_bits - 1)) &&
1344 offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS));
a82d593b
DDAG
1345
1346 /* The offset we leave with is the last one we looked at */
a935e30f 1347 pss->page--;
a82d593b
DDAG
1348 return pages;
1349}
6c595cde 1350
56e93d26 1351/**
3d0684b2 1352 * ram_find_and_save_block: finds a dirty page and sends it to f
56e93d26
JQ
1353 *
1354 * Called within an RCU critical section.
1355 *
3d0684b2 1356 * Returns the number of pages written where zero means no dirty pages
56e93d26 1357 *
6f37bb8b 1358 * @rs: current RAM state
56e93d26 1359 * @last_stage: if we are at the completion stage
a82d593b
DDAG
1360 *
1361 * On systems where host-page-size > target-page-size it will send all the
1362 * pages in a host page that are dirty.
56e93d26
JQ
1363 */
1364
ce25d337 1365static int ram_find_and_save_block(RAMState *rs, bool last_stage)
56e93d26 1366{
b8fb8cb7 1367 PageSearchStatus pss;
56e93d26 1368 int pages = 0;
b9e60928 1369 bool again, found;
56e93d26 1370
0827b9e9
AA
1371 /* No dirty page as there is zero RAM */
1372 if (!ram_bytes_total()) {
1373 return pages;
1374 }
1375
6f37bb8b 1376 pss.block = rs->last_seen_block;
a935e30f 1377 pss.page = rs->last_page;
b8fb8cb7
DDAG
1378 pss.complete_round = false;
1379
1380 if (!pss.block) {
1381 pss.block = QLIST_FIRST_RCU(&ram_list.blocks);
1382 }
56e93d26 1383
b9e60928 1384 do {
a82d593b 1385 again = true;
f20e2865 1386 found = get_queued_page(rs, &pss);
b9e60928 1387
a82d593b
DDAG
1388 if (!found) {
1389 /* priority queue empty, so just search for something dirty */
f20e2865 1390 found = find_dirty_block(rs, &pss, &again);
a82d593b 1391 }
f3f491fc 1392
a82d593b 1393 if (found) {
f20e2865 1394 pages = ram_save_host_page(rs, &pss, last_stage);
56e93d26 1395 }
b9e60928 1396 } while (!pages && again);
56e93d26 1397
6f37bb8b 1398 rs->last_seen_block = pss.block;
a935e30f 1399 rs->last_page = pss.page;
56e93d26
JQ
1400
1401 return pages;
1402}
1403
1404void acct_update_position(QEMUFile *f, size_t size, bool zero)
1405{
1406 uint64_t pages = size / TARGET_PAGE_SIZE;
f7ccd61b
JQ
1407 RAMState *rs = &ram_state;
1408
56e93d26 1409 if (zero) {
f7ccd61b 1410 rs->zero_pages += pages;
56e93d26 1411 } else {
b4d1c6e7 1412 rs->norm_pages += pages;
2f4fde93 1413 rs->bytes_transferred += size;
56e93d26
JQ
1414 qemu_update_position(f, size);
1415 }
1416}
1417
56e93d26
JQ
1418uint64_t ram_bytes_total(void)
1419{
1420 RAMBlock *block;
1421 uint64_t total = 0;
1422
1423 rcu_read_lock();
99e15582 1424 RAMBLOCK_FOREACH(block) {
56e93d26 1425 total += block->used_length;
99e15582 1426 }
56e93d26
JQ
1427 rcu_read_unlock();
1428 return total;
1429}
1430
1431void free_xbzrle_decoded_buf(void)
1432{
1433 g_free(xbzrle_decoded_buf);
1434 xbzrle_decoded_buf = NULL;
1435}
1436
6ad2a215 1437static void ram_migration_cleanup(void *opaque)
56e93d26 1438{
83c13382 1439 RAMState *rs = opaque;
6b6712ef 1440 RAMBlock *block;
eb859c53 1441
2ff64038
LZ
1442 /* caller have hold iothread lock or is in a bh, so there is
1443 * no writing race against this migration_bitmap
1444 */
6b6712ef
JQ
1445 memory_global_dirty_log_stop();
1446
1447 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1448 g_free(block->bmap);
1449 block->bmap = NULL;
1450 g_free(block->unsentmap);
1451 block->unsentmap = NULL;
56e93d26
JQ
1452 }
1453
1454 XBZRLE_cache_lock();
1455 if (XBZRLE.cache) {
1456 cache_fini(XBZRLE.cache);
1457 g_free(XBZRLE.encoded_buf);
1458 g_free(XBZRLE.current_buf);
c00e0928 1459 g_free(XBZRLE.zero_target_page);
56e93d26
JQ
1460 XBZRLE.cache = NULL;
1461 XBZRLE.encoded_buf = NULL;
1462 XBZRLE.current_buf = NULL;
c00e0928 1463 XBZRLE.zero_target_page = NULL;
56e93d26
JQ
1464 }
1465 XBZRLE_cache_unlock();
83c13382 1466 migration_page_queue_free(rs);
56e93d26
JQ
1467}
1468
6f37bb8b 1469static void ram_state_reset(RAMState *rs)
56e93d26 1470{
6f37bb8b
JQ
1471 rs->last_seen_block = NULL;
1472 rs->last_sent_block = NULL;
269ace29 1473 rs->last_page = 0;
6f37bb8b
JQ
1474 rs->last_version = ram_list.version;
1475 rs->ram_bulk_stage = true;
56e93d26
JQ
1476}
1477
1478#define MAX_WAIT 50 /* ms, half buffered_file limit */
1479
4f2e4252
DDAG
1480/*
1481 * 'expected' is the value you expect the bitmap mostly to be full
1482 * of; it won't bother printing lines that are all this value.
1483 * If 'todump' is null the migration bitmap is dumped.
1484 */
6b6712ef
JQ
1485void ram_debug_dump_bitmap(unsigned long *todump, bool expected,
1486 unsigned long pages)
4f2e4252 1487{
4f2e4252
DDAG
1488 int64_t cur;
1489 int64_t linelen = 128;
1490 char linebuf[129];
1491
6b6712ef 1492 for (cur = 0; cur < pages; cur += linelen) {
4f2e4252
DDAG
1493 int64_t curb;
1494 bool found = false;
1495 /*
1496 * Last line; catch the case where the line length
1497 * is longer than remaining ram
1498 */
6b6712ef
JQ
1499 if (cur + linelen > pages) {
1500 linelen = pages - cur;
4f2e4252
DDAG
1501 }
1502 for (curb = 0; curb < linelen; curb++) {
1503 bool thisbit = test_bit(cur + curb, todump);
1504 linebuf[curb] = thisbit ? '1' : '.';
1505 found = found || (thisbit != expected);
1506 }
1507 if (found) {
1508 linebuf[curb] = '\0';
1509 fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf);
1510 }
1511 }
1512}
1513
e0b266f0
DDAG
1514/* **** functions for postcopy ***** */
1515
ced1c616
PB
1516void ram_postcopy_migrated_memory_release(MigrationState *ms)
1517{
1518 struct RAMBlock *block;
ced1c616 1519
99e15582 1520 RAMBLOCK_FOREACH(block) {
6b6712ef
JQ
1521 unsigned long *bitmap = block->bmap;
1522 unsigned long range = block->used_length >> TARGET_PAGE_BITS;
1523 unsigned long run_start = find_next_zero_bit(bitmap, range, 0);
ced1c616
PB
1524
1525 while (run_start < range) {
1526 unsigned long run_end = find_next_bit(bitmap, range, run_start + 1);
aaa2064c 1527 ram_discard_range(block->idstr, run_start << TARGET_PAGE_BITS,
ced1c616
PB
1528 (run_end - run_start) << TARGET_PAGE_BITS);
1529 run_start = find_next_zero_bit(bitmap, range, run_end + 1);
1530 }
1531 }
1532}
1533
3d0684b2
JQ
1534/**
1535 * postcopy_send_discard_bm_ram: discard a RAMBlock
1536 *
1537 * Returns zero on success
1538 *
e0b266f0
DDAG
1539 * Callback from postcopy_each_ram_send_discard for each RAMBlock
1540 * Note: At this point the 'unsentmap' is the processed bitmap combined
1541 * with the dirtymap; so a '1' means it's either dirty or unsent.
3d0684b2
JQ
1542 *
1543 * @ms: current migration state
1544 * @pds: state for postcopy
1545 * @start: RAMBlock starting page
1546 * @length: RAMBlock size
e0b266f0
DDAG
1547 */
1548static int postcopy_send_discard_bm_ram(MigrationState *ms,
1549 PostcopyDiscardState *pds,
6b6712ef 1550 RAMBlock *block)
e0b266f0 1551{
6b6712ef 1552 unsigned long end = block->used_length >> TARGET_PAGE_BITS;
e0b266f0 1553 unsigned long current;
6b6712ef 1554 unsigned long *unsentmap = block->unsentmap;
e0b266f0 1555
6b6712ef 1556 for (current = 0; current < end; ) {
e0b266f0
DDAG
1557 unsigned long one = find_next_bit(unsentmap, end, current);
1558
1559 if (one <= end) {
1560 unsigned long zero = find_next_zero_bit(unsentmap, end, one + 1);
1561 unsigned long discard_length;
1562
1563 if (zero >= end) {
1564 discard_length = end - one;
1565 } else {
1566 discard_length = zero - one;
1567 }
d688c62d
DDAG
1568 if (discard_length) {
1569 postcopy_discard_send_range(ms, pds, one, discard_length);
1570 }
e0b266f0
DDAG
1571 current = one + discard_length;
1572 } else {
1573 current = one;
1574 }
1575 }
1576
1577 return 0;
1578}
1579
3d0684b2
JQ
1580/**
1581 * postcopy_each_ram_send_discard: discard all RAMBlocks
1582 *
1583 * Returns 0 for success or negative for error
1584 *
e0b266f0
DDAG
1585 * Utility for the outgoing postcopy code.
1586 * Calls postcopy_send_discard_bm_ram for each RAMBlock
1587 * passing it bitmap indexes and name.
e0b266f0
DDAG
1588 * (qemu_ram_foreach_block ends up passing unscaled lengths
1589 * which would mean postcopy code would have to deal with target page)
3d0684b2
JQ
1590 *
1591 * @ms: current migration state
e0b266f0
DDAG
1592 */
1593static int postcopy_each_ram_send_discard(MigrationState *ms)
1594{
1595 struct RAMBlock *block;
1596 int ret;
1597
99e15582 1598 RAMBLOCK_FOREACH(block) {
6b6712ef
JQ
1599 PostcopyDiscardState *pds =
1600 postcopy_discard_send_init(ms, block->idstr);
e0b266f0
DDAG
1601
1602 /*
1603 * Postcopy sends chunks of bitmap over the wire, but it
1604 * just needs indexes at this point, avoids it having
1605 * target page specific code.
1606 */
6b6712ef 1607 ret = postcopy_send_discard_bm_ram(ms, pds, block);
e0b266f0
DDAG
1608 postcopy_discard_send_finish(ms, pds);
1609 if (ret) {
1610 return ret;
1611 }
1612 }
1613
1614 return 0;
1615}
1616
3d0684b2
JQ
1617/**
1618 * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages
1619 *
1620 * Helper for postcopy_chunk_hostpages; it's called twice to
1621 * canonicalize the two bitmaps, that are similar, but one is
1622 * inverted.
99e314eb 1623 *
3d0684b2
JQ
1624 * Postcopy requires that all target pages in a hostpage are dirty or
1625 * clean, not a mix. This function canonicalizes the bitmaps.
99e314eb 1626 *
3d0684b2
JQ
1627 * @ms: current migration state
1628 * @unsent_pass: if true we need to canonicalize partially unsent host pages
1629 * otherwise we need to canonicalize partially dirty host pages
1630 * @block: block that contains the page we want to canonicalize
1631 * @pds: state for postcopy
99e314eb
DDAG
1632 */
1633static void postcopy_chunk_hostpages_pass(MigrationState *ms, bool unsent_pass,
1634 RAMBlock *block,
1635 PostcopyDiscardState *pds)
1636{
0d8ec885 1637 RAMState *rs = &ram_state;
6b6712ef
JQ
1638 unsigned long *bitmap = block->bmap;
1639 unsigned long *unsentmap = block->unsentmap;
29c59172 1640 unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE;
6b6712ef 1641 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
99e314eb
DDAG
1642 unsigned long run_start;
1643
29c59172
DDAG
1644 if (block->page_size == TARGET_PAGE_SIZE) {
1645 /* Easy case - TPS==HPS for a non-huge page RAMBlock */
1646 return;
1647 }
1648
99e314eb
DDAG
1649 if (unsent_pass) {
1650 /* Find a sent page */
6b6712ef 1651 run_start = find_next_zero_bit(unsentmap, pages, 0);
99e314eb
DDAG
1652 } else {
1653 /* Find a dirty page */
6b6712ef 1654 run_start = find_next_bit(bitmap, pages, 0);
99e314eb
DDAG
1655 }
1656
6b6712ef 1657 while (run_start < pages) {
99e314eb
DDAG
1658 bool do_fixup = false;
1659 unsigned long fixup_start_addr;
1660 unsigned long host_offset;
1661
1662 /*
1663 * If the start of this run of pages is in the middle of a host
1664 * page, then we need to fixup this host page.
1665 */
1666 host_offset = run_start % host_ratio;
1667 if (host_offset) {
1668 do_fixup = true;
1669 run_start -= host_offset;
1670 fixup_start_addr = run_start;
1671 /* For the next pass */
1672 run_start = run_start + host_ratio;
1673 } else {
1674 /* Find the end of this run */
1675 unsigned long run_end;
1676 if (unsent_pass) {
6b6712ef 1677 run_end = find_next_bit(unsentmap, pages, run_start + 1);
99e314eb 1678 } else {
6b6712ef 1679 run_end = find_next_zero_bit(bitmap, pages, run_start + 1);
99e314eb
DDAG
1680 }
1681 /*
1682 * If the end isn't at the start of a host page, then the
1683 * run doesn't finish at the end of a host page
1684 * and we need to discard.
1685 */
1686 host_offset = run_end % host_ratio;
1687 if (host_offset) {
1688 do_fixup = true;
1689 fixup_start_addr = run_end - host_offset;
1690 /*
1691 * This host page has gone, the next loop iteration starts
1692 * from after the fixup
1693 */
1694 run_start = fixup_start_addr + host_ratio;
1695 } else {
1696 /*
1697 * No discards on this iteration, next loop starts from
1698 * next sent/dirty page
1699 */
1700 run_start = run_end + 1;
1701 }
1702 }
1703
1704 if (do_fixup) {
1705 unsigned long page;
1706
1707 /* Tell the destination to discard this page */
1708 if (unsent_pass || !test_bit(fixup_start_addr, unsentmap)) {
1709 /* For the unsent_pass we:
1710 * discard partially sent pages
1711 * For the !unsent_pass (dirty) we:
1712 * discard partially dirty pages that were sent
1713 * (any partially sent pages were already discarded
1714 * by the previous unsent_pass)
1715 */
1716 postcopy_discard_send_range(ms, pds, fixup_start_addr,
1717 host_ratio);
1718 }
1719
1720 /* Clean up the bitmap */
1721 for (page = fixup_start_addr;
1722 page < fixup_start_addr + host_ratio; page++) {
1723 /* All pages in this host page are now not sent */
1724 set_bit(page, unsentmap);
1725
1726 /*
1727 * Remark them as dirty, updating the count for any pages
1728 * that weren't previously dirty.
1729 */
0d8ec885 1730 rs->migration_dirty_pages += !test_and_set_bit(page, bitmap);
99e314eb
DDAG
1731 }
1732 }
1733
1734 if (unsent_pass) {
1735 /* Find the next sent page for the next iteration */
6b6712ef 1736 run_start = find_next_zero_bit(unsentmap, pages, run_start);
99e314eb
DDAG
1737 } else {
1738 /* Find the next dirty page for the next iteration */
6b6712ef 1739 run_start = find_next_bit(bitmap, pages, run_start);
99e314eb
DDAG
1740 }
1741 }
1742}
1743
3d0684b2
JQ
1744/**
1745 * postcopy_chuck_hostpages: discrad any partially sent host page
1746 *
99e314eb
DDAG
1747 * Utility for the outgoing postcopy code.
1748 *
1749 * Discard any partially sent host-page size chunks, mark any partially
29c59172
DDAG
1750 * dirty host-page size chunks as all dirty. In this case the host-page
1751 * is the host-page for the particular RAMBlock, i.e. it might be a huge page
99e314eb 1752 *
3d0684b2
JQ
1753 * Returns zero on success
1754 *
1755 * @ms: current migration state
6b6712ef 1756 * @block: block we want to work with
99e314eb 1757 */
6b6712ef 1758static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block)
99e314eb 1759{
6b6712ef
JQ
1760 PostcopyDiscardState *pds =
1761 postcopy_discard_send_init(ms, block->idstr);
99e314eb 1762
6b6712ef
JQ
1763 /* First pass: Discard all partially sent host pages */
1764 postcopy_chunk_hostpages_pass(ms, true, block, pds);
1765 /*
1766 * Second pass: Ensure that all partially dirty host pages are made
1767 * fully dirty.
1768 */
1769 postcopy_chunk_hostpages_pass(ms, false, block, pds);
99e314eb 1770
6b6712ef 1771 postcopy_discard_send_finish(ms, pds);
99e314eb
DDAG
1772 return 0;
1773}
1774
3d0684b2
JQ
1775/**
1776 * ram_postcopy_send_discard_bitmap: transmit the discard bitmap
1777 *
1778 * Returns zero on success
1779 *
e0b266f0
DDAG
1780 * Transmit the set of pages to be discarded after precopy to the target
1781 * these are pages that:
1782 * a) Have been previously transmitted but are now dirty again
1783 * b) Pages that have never been transmitted, this ensures that
1784 * any pages on the destination that have been mapped by background
1785 * tasks get discarded (transparent huge pages is the specific concern)
1786 * Hopefully this is pretty sparse
3d0684b2
JQ
1787 *
1788 * @ms: current migration state
e0b266f0
DDAG
1789 */
1790int ram_postcopy_send_discard_bitmap(MigrationState *ms)
1791{
eb859c53 1792 RAMState *rs = &ram_state;
6b6712ef 1793 RAMBlock *block;
e0b266f0 1794 int ret;
e0b266f0
DDAG
1795
1796 rcu_read_lock();
1797
1798 /* This should be our last sync, the src is now paused */
eb859c53 1799 migration_bitmap_sync(rs);
e0b266f0 1800
6b6712ef
JQ
1801 /* Easiest way to make sure we don't resume in the middle of a host-page */
1802 rs->last_seen_block = NULL;
1803 rs->last_sent_block = NULL;
1804 rs->last_page = 0;
e0b266f0 1805
6b6712ef
JQ
1806 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1807 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
1808 unsigned long *bitmap = block->bmap;
1809 unsigned long *unsentmap = block->unsentmap;
1810
1811 if (!unsentmap) {
1812 /* We don't have a safe way to resize the sentmap, so
1813 * if the bitmap was resized it will be NULL at this
1814 * point.
1815 */
1816 error_report("migration ram resized during precopy phase");
1817 rcu_read_unlock();
1818 return -EINVAL;
1819 }
1820 /* Deal with TPS != HPS and huge pages */
1821 ret = postcopy_chunk_hostpages(ms, block);
1822 if (ret) {
1823 rcu_read_unlock();
1824 return ret;
1825 }
e0b266f0 1826
6b6712ef
JQ
1827 /*
1828 * Update the unsentmap to be unsentmap = unsentmap | dirty
1829 */
1830 bitmap_or(unsentmap, unsentmap, bitmap, pages);
e0b266f0 1831#ifdef DEBUG_POSTCOPY
6b6712ef 1832 ram_debug_dump_bitmap(unsentmap, true, pages);
e0b266f0 1833#endif
6b6712ef
JQ
1834 }
1835 trace_ram_postcopy_send_discard_bitmap();
e0b266f0
DDAG
1836
1837 ret = postcopy_each_ram_send_discard(ms);
1838 rcu_read_unlock();
1839
1840 return ret;
1841}
1842
3d0684b2
JQ
1843/**
1844 * ram_discard_range: discard dirtied pages at the beginning of postcopy
e0b266f0 1845 *
3d0684b2 1846 * Returns zero on success
e0b266f0 1847 *
36449157
JQ
1848 * @rbname: name of the RAMBlock of the request. NULL means the
1849 * same that last one.
3d0684b2
JQ
1850 * @start: RAMBlock starting page
1851 * @length: RAMBlock size
e0b266f0 1852 */
aaa2064c 1853int ram_discard_range(const char *rbname, uint64_t start, size_t length)
e0b266f0
DDAG
1854{
1855 int ret = -1;
1856
36449157 1857 trace_ram_discard_range(rbname, start, length);
d3a5038c 1858
e0b266f0 1859 rcu_read_lock();
36449157 1860 RAMBlock *rb = qemu_ram_block_by_name(rbname);
e0b266f0
DDAG
1861
1862 if (!rb) {
36449157 1863 error_report("ram_discard_range: Failed to find block '%s'", rbname);
e0b266f0
DDAG
1864 goto err;
1865 }
1866
d3a5038c 1867 ret = ram_block_discard_range(rb, start, length);
e0b266f0
DDAG
1868
1869err:
1870 rcu_read_unlock();
1871
1872 return ret;
1873}
1874
ceb4d168 1875static int ram_state_init(RAMState *rs)
56e93d26 1876{
ceb4d168 1877 memset(rs, 0, sizeof(*rs));
108cfae0 1878 qemu_mutex_init(&rs->bitmap_mutex);
ec481c6c
JQ
1879 qemu_mutex_init(&rs->src_page_req_mutex);
1880 QSIMPLEQ_INIT(&rs->src_page_requests);
56e93d26
JQ
1881
1882 if (migrate_use_xbzrle()) {
1883 XBZRLE_cache_lock();
c00e0928 1884 XBZRLE.zero_target_page = g_malloc0(TARGET_PAGE_SIZE);
56e93d26
JQ
1885 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
1886 TARGET_PAGE_SIZE,
1887 TARGET_PAGE_SIZE);
1888 if (!XBZRLE.cache) {
1889 XBZRLE_cache_unlock();
1890 error_report("Error creating cache");
1891 return -1;
1892 }
1893 XBZRLE_cache_unlock();
1894
1895 /* We prefer not to abort if there is no memory */
1896 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
1897 if (!XBZRLE.encoded_buf) {
1898 error_report("Error allocating encoded_buf");
1899 return -1;
1900 }
1901
1902 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
1903 if (!XBZRLE.current_buf) {
1904 error_report("Error allocating current_buf");
1905 g_free(XBZRLE.encoded_buf);
1906 XBZRLE.encoded_buf = NULL;
1907 return -1;
1908 }
56e93d26
JQ
1909 }
1910
49877834
PB
1911 /* For memory_global_dirty_log_start below. */
1912 qemu_mutex_lock_iothread();
1913
56e93d26
JQ
1914 qemu_mutex_lock_ramlist();
1915 rcu_read_lock();
6f37bb8b 1916 ram_state_reset(rs);
56e93d26 1917
0827b9e9
AA
1918 /* Skip setting bitmap if there is no RAM */
1919 if (ram_bytes_total()) {
6b6712ef
JQ
1920 RAMBlock *block;
1921
1922 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1923 unsigned long pages = block->max_length >> TARGET_PAGE_BITS;
0827b9e9 1924
6b6712ef
JQ
1925 block->bmap = bitmap_new(pages);
1926 bitmap_set(block->bmap, 0, pages);
1927 if (migrate_postcopy_ram()) {
1928 block->unsentmap = bitmap_new(pages);
1929 bitmap_set(block->unsentmap, 0, pages);
1930 }
0827b9e9 1931 }
f3f491fc
DDAG
1932 }
1933
56e93d26
JQ
1934 /*
1935 * Count the total number of pages used by ram blocks not including any
1936 * gaps due to alignment or unplugs.
1937 */
0d8ec885 1938 rs->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
56e93d26
JQ
1939
1940 memory_global_dirty_log_start();
8d820d6f 1941 migration_bitmap_sync(rs);
56e93d26 1942 qemu_mutex_unlock_ramlist();
49877834 1943 qemu_mutex_unlock_iothread();
a91246c9
HZ
1944 rcu_read_unlock();
1945
1946 return 0;
1947}
1948
3d0684b2
JQ
1949/*
1950 * Each of ram_save_setup, ram_save_iterate and ram_save_complete has
a91246c9
HZ
1951 * long-running RCU critical section. When rcu-reclaims in the code
1952 * start to become numerous it will be necessary to reduce the
1953 * granularity of these critical sections.
1954 */
1955
3d0684b2
JQ
1956/**
1957 * ram_save_setup: Setup RAM for migration
1958 *
1959 * Returns zero to indicate success and negative for error
1960 *
1961 * @f: QEMUFile where to send the data
1962 * @opaque: RAMState pointer
1963 */
a91246c9
HZ
1964static int ram_save_setup(QEMUFile *f, void *opaque)
1965{
6f37bb8b 1966 RAMState *rs = opaque;
a91246c9
HZ
1967 RAMBlock *block;
1968
1969 /* migration has already setup the bitmap, reuse it. */
1970 if (!migration_in_colo_state()) {
ceb4d168 1971 if (ram_state_init(rs) < 0) {
a91246c9
HZ
1972 return -1;
1973 }
1974 }
204b88b8 1975 rs->f = f;
a91246c9
HZ
1976
1977 rcu_read_lock();
56e93d26
JQ
1978
1979 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
1980
99e15582 1981 RAMBLOCK_FOREACH(block) {
56e93d26
JQ
1982 qemu_put_byte(f, strlen(block->idstr));
1983 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
1984 qemu_put_be64(f, block->used_length);
ef08fb38
DDAG
1985 if (migrate_postcopy_ram() && block->page_size != qemu_host_page_size) {
1986 qemu_put_be64(f, block->page_size);
1987 }
56e93d26
JQ
1988 }
1989
1990 rcu_read_unlock();
1991
1992 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
1993 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
1994
1995 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1996
1997 return 0;
1998}
1999
3d0684b2
JQ
2000/**
2001 * ram_save_iterate: iterative stage for migration
2002 *
2003 * Returns zero to indicate success and negative for error
2004 *
2005 * @f: QEMUFile where to send the data
2006 * @opaque: RAMState pointer
2007 */
56e93d26
JQ
2008static int ram_save_iterate(QEMUFile *f, void *opaque)
2009{
6f37bb8b 2010 RAMState *rs = opaque;
56e93d26
JQ
2011 int ret;
2012 int i;
2013 int64_t t0;
5c90308f 2014 int done = 0;
56e93d26
JQ
2015
2016 rcu_read_lock();
6f37bb8b
JQ
2017 if (ram_list.version != rs->last_version) {
2018 ram_state_reset(rs);
56e93d26
JQ
2019 }
2020
2021 /* Read version before ram_list.blocks */
2022 smp_rmb();
2023
2024 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
2025
2026 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2027 i = 0;
2028 while ((ret = qemu_file_rate_limit(f)) == 0) {
2029 int pages;
2030
ce25d337 2031 pages = ram_find_and_save_block(rs, false);
56e93d26
JQ
2032 /* no more pages to sent */
2033 if (pages == 0) {
5c90308f 2034 done = 1;
56e93d26
JQ
2035 break;
2036 }
23b28c3c 2037 rs->iterations++;
070afca2 2038
56e93d26
JQ
2039 /* we want to check in the 1st loop, just in case it was the 1st time
2040 and we had to sync the dirty bitmap.
2041 qemu_get_clock_ns() is a bit expensive, so we only check each some
2042 iterations
2043 */
2044 if ((i & 63) == 0) {
2045 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
2046 if (t1 > MAX_WAIT) {
55c4446b 2047 trace_ram_save_iterate_big_wait(t1, i);
56e93d26
JQ
2048 break;
2049 }
2050 }
2051 i++;
2052 }
ce25d337 2053 flush_compressed_data(rs);
56e93d26
JQ
2054 rcu_read_unlock();
2055
2056 /*
2057 * Must occur before EOS (or any QEMUFile operation)
2058 * because of RDMA protocol.
2059 */
2060 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
2061
2062 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2f4fde93 2063 rs->bytes_transferred += 8;
56e93d26
JQ
2064
2065 ret = qemu_file_get_error(f);
2066 if (ret < 0) {
2067 return ret;
2068 }
2069
5c90308f 2070 return done;
56e93d26
JQ
2071}
2072
3d0684b2
JQ
2073/**
2074 * ram_save_complete: function called to send the remaining amount of ram
2075 *
2076 * Returns zero to indicate success
2077 *
2078 * Called with iothread lock
2079 *
2080 * @f: QEMUFile where to send the data
2081 * @opaque: RAMState pointer
2082 */
56e93d26
JQ
2083static int ram_save_complete(QEMUFile *f, void *opaque)
2084{
6f37bb8b
JQ
2085 RAMState *rs = opaque;
2086
56e93d26
JQ
2087 rcu_read_lock();
2088
5727309d 2089 if (!migration_in_postcopy()) {
8d820d6f 2090 migration_bitmap_sync(rs);
663e6c1d 2091 }
56e93d26
JQ
2092
2093 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
2094
2095 /* try transferring iterative blocks of memory */
2096
2097 /* flush all remaining blocks regardless of rate limiting */
2098 while (true) {
2099 int pages;
2100
ce25d337 2101 pages = ram_find_and_save_block(rs, !migration_in_colo_state());
56e93d26
JQ
2102 /* no more blocks to sent */
2103 if (pages == 0) {
2104 break;
2105 }
2106 }
2107
ce25d337 2108 flush_compressed_data(rs);
56e93d26 2109 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
56e93d26
JQ
2110
2111 rcu_read_unlock();
d09a6fde 2112
56e93d26
JQ
2113 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2114
2115 return 0;
2116}
2117
c31b098f
DDAG
2118static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
2119 uint64_t *non_postcopiable_pending,
2120 uint64_t *postcopiable_pending)
56e93d26 2121{
8d820d6f 2122 RAMState *rs = opaque;
56e93d26
JQ
2123 uint64_t remaining_size;
2124
9edabd4d 2125 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
56e93d26 2126
5727309d 2127 if (!migration_in_postcopy() &&
663e6c1d 2128 remaining_size < max_size) {
56e93d26
JQ
2129 qemu_mutex_lock_iothread();
2130 rcu_read_lock();
8d820d6f 2131 migration_bitmap_sync(rs);
56e93d26
JQ
2132 rcu_read_unlock();
2133 qemu_mutex_unlock_iothread();
9edabd4d 2134 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
56e93d26 2135 }
c31b098f
DDAG
2136
2137 /* We can do postcopy, and all the data is postcopiable */
2138 *postcopiable_pending += remaining_size;
56e93d26
JQ
2139}
2140
2141static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
2142{
2143 unsigned int xh_len;
2144 int xh_flags;
063e760a 2145 uint8_t *loaded_data;
56e93d26
JQ
2146
2147 if (!xbzrle_decoded_buf) {
2148 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
2149 }
063e760a 2150 loaded_data = xbzrle_decoded_buf;
56e93d26
JQ
2151
2152 /* extract RLE header */
2153 xh_flags = qemu_get_byte(f);
2154 xh_len = qemu_get_be16(f);
2155
2156 if (xh_flags != ENCODING_FLAG_XBZRLE) {
2157 error_report("Failed to load XBZRLE page - wrong compression!");
2158 return -1;
2159 }
2160
2161 if (xh_len > TARGET_PAGE_SIZE) {
2162 error_report("Failed to load XBZRLE page - len overflow!");
2163 return -1;
2164 }
2165 /* load data and decode */
063e760a 2166 qemu_get_buffer_in_place(f, &loaded_data, xh_len);
56e93d26
JQ
2167
2168 /* decode RLE */
063e760a 2169 if (xbzrle_decode_buffer(loaded_data, xh_len, host,
56e93d26
JQ
2170 TARGET_PAGE_SIZE) == -1) {
2171 error_report("Failed to load XBZRLE page - decode error!");
2172 return -1;
2173 }
2174
2175 return 0;
2176}
2177
3d0684b2
JQ
2178/**
2179 * ram_block_from_stream: read a RAMBlock id from the migration stream
2180 *
2181 * Must be called from within a rcu critical section.
2182 *
56e93d26 2183 * Returns a pointer from within the RCU-protected ram_list.
a7180877 2184 *
3d0684b2
JQ
2185 * @f: QEMUFile where to read the data from
2186 * @flags: Page flags (mostly to see if it's a continuation of previous block)
a7180877 2187 */
3d0684b2 2188static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags)
56e93d26
JQ
2189{
2190 static RAMBlock *block = NULL;
2191 char id[256];
2192 uint8_t len;
2193
2194 if (flags & RAM_SAVE_FLAG_CONTINUE) {
4c4bad48 2195 if (!block) {
56e93d26
JQ
2196 error_report("Ack, bad migration stream!");
2197 return NULL;
2198 }
4c4bad48 2199 return block;
56e93d26
JQ
2200 }
2201
2202 len = qemu_get_byte(f);
2203 qemu_get_buffer(f, (uint8_t *)id, len);
2204 id[len] = 0;
2205
e3dd7493 2206 block = qemu_ram_block_by_name(id);
4c4bad48
HZ
2207 if (!block) {
2208 error_report("Can't find block %s", id);
2209 return NULL;
56e93d26
JQ
2210 }
2211
4c4bad48
HZ
2212 return block;
2213}
2214
2215static inline void *host_from_ram_block_offset(RAMBlock *block,
2216 ram_addr_t offset)
2217{
2218 if (!offset_in_ramblock(block, offset)) {
2219 return NULL;
2220 }
2221
2222 return block->host + offset;
56e93d26
JQ
2223}
2224
3d0684b2
JQ
2225/**
2226 * ram_handle_compressed: handle the zero page case
2227 *
56e93d26
JQ
2228 * If a page (or a whole RDMA chunk) has been
2229 * determined to be zero, then zap it.
3d0684b2
JQ
2230 *
2231 * @host: host address for the zero page
2232 * @ch: what the page is filled from. We only support zero
2233 * @size: size of the zero page
56e93d26
JQ
2234 */
2235void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
2236{
2237 if (ch != 0 || !is_zero_range(host, size)) {
2238 memset(host, ch, size);
2239 }
2240}
2241
2242static void *do_data_decompress(void *opaque)
2243{
2244 DecompressParam *param = opaque;
2245 unsigned long pagesize;
33d151f4
LL
2246 uint8_t *des;
2247 int len;
56e93d26 2248
33d151f4 2249 qemu_mutex_lock(&param->mutex);
90e56fb4 2250 while (!param->quit) {
33d151f4
LL
2251 if (param->des) {
2252 des = param->des;
2253 len = param->len;
2254 param->des = 0;
2255 qemu_mutex_unlock(&param->mutex);
2256
56e93d26 2257 pagesize = TARGET_PAGE_SIZE;
73a8912b
LL
2258 /* uncompress() will return failed in some case, especially
2259 * when the page is dirted when doing the compression, it's
2260 * not a problem because the dirty page will be retransferred
2261 * and uncompress() won't break the data in other pages.
2262 */
33d151f4
LL
2263 uncompress((Bytef *)des, &pagesize,
2264 (const Bytef *)param->compbuf, len);
73a8912b 2265
33d151f4
LL
2266 qemu_mutex_lock(&decomp_done_lock);
2267 param->done = true;
2268 qemu_cond_signal(&decomp_done_cond);
2269 qemu_mutex_unlock(&decomp_done_lock);
2270
2271 qemu_mutex_lock(&param->mutex);
2272 } else {
2273 qemu_cond_wait(&param->cond, &param->mutex);
2274 }
56e93d26 2275 }
33d151f4 2276 qemu_mutex_unlock(&param->mutex);
56e93d26
JQ
2277
2278 return NULL;
2279}
2280
5533b2e9
LL
2281static void wait_for_decompress_done(void)
2282{
2283 int idx, thread_count;
2284
2285 if (!migrate_use_compression()) {
2286 return;
2287 }
2288
2289 thread_count = migrate_decompress_threads();
2290 qemu_mutex_lock(&decomp_done_lock);
2291 for (idx = 0; idx < thread_count; idx++) {
2292 while (!decomp_param[idx].done) {
2293 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
2294 }
2295 }
2296 qemu_mutex_unlock(&decomp_done_lock);
2297}
2298
56e93d26
JQ
2299void migrate_decompress_threads_create(void)
2300{
2301 int i, thread_count;
2302
2303 thread_count = migrate_decompress_threads();
2304 decompress_threads = g_new0(QemuThread, thread_count);
2305 decomp_param = g_new0(DecompressParam, thread_count);
73a8912b
LL
2306 qemu_mutex_init(&decomp_done_lock);
2307 qemu_cond_init(&decomp_done_cond);
56e93d26
JQ
2308 for (i = 0; i < thread_count; i++) {
2309 qemu_mutex_init(&decomp_param[i].mutex);
2310 qemu_cond_init(&decomp_param[i].cond);
2311 decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
73a8912b 2312 decomp_param[i].done = true;
90e56fb4 2313 decomp_param[i].quit = false;
56e93d26
JQ
2314 qemu_thread_create(decompress_threads + i, "decompress",
2315 do_data_decompress, decomp_param + i,
2316 QEMU_THREAD_JOINABLE);
2317 }
2318}
2319
2320void migrate_decompress_threads_join(void)
2321{
2322 int i, thread_count;
2323
56e93d26
JQ
2324 thread_count = migrate_decompress_threads();
2325 for (i = 0; i < thread_count; i++) {
2326 qemu_mutex_lock(&decomp_param[i].mutex);
90e56fb4 2327 decomp_param[i].quit = true;
56e93d26
JQ
2328 qemu_cond_signal(&decomp_param[i].cond);
2329 qemu_mutex_unlock(&decomp_param[i].mutex);
2330 }
2331 for (i = 0; i < thread_count; i++) {
2332 qemu_thread_join(decompress_threads + i);
2333 qemu_mutex_destroy(&decomp_param[i].mutex);
2334 qemu_cond_destroy(&decomp_param[i].cond);
2335 g_free(decomp_param[i].compbuf);
2336 }
2337 g_free(decompress_threads);
2338 g_free(decomp_param);
56e93d26
JQ
2339 decompress_threads = NULL;
2340 decomp_param = NULL;
56e93d26
JQ
2341}
2342
c1bc6626 2343static void decompress_data_with_multi_threads(QEMUFile *f,
56e93d26
JQ
2344 void *host, int len)
2345{
2346 int idx, thread_count;
2347
2348 thread_count = migrate_decompress_threads();
73a8912b 2349 qemu_mutex_lock(&decomp_done_lock);
56e93d26
JQ
2350 while (true) {
2351 for (idx = 0; idx < thread_count; idx++) {
73a8912b 2352 if (decomp_param[idx].done) {
33d151f4
LL
2353 decomp_param[idx].done = false;
2354 qemu_mutex_lock(&decomp_param[idx].mutex);
c1bc6626 2355 qemu_get_buffer(f, decomp_param[idx].compbuf, len);
56e93d26
JQ
2356 decomp_param[idx].des = host;
2357 decomp_param[idx].len = len;
33d151f4
LL
2358 qemu_cond_signal(&decomp_param[idx].cond);
2359 qemu_mutex_unlock(&decomp_param[idx].mutex);
56e93d26
JQ
2360 break;
2361 }
2362 }
2363 if (idx < thread_count) {
2364 break;
73a8912b
LL
2365 } else {
2366 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
56e93d26
JQ
2367 }
2368 }
73a8912b 2369 qemu_mutex_unlock(&decomp_done_lock);
56e93d26
JQ
2370}
2371
3d0684b2
JQ
2372/**
2373 * ram_postcopy_incoming_init: allocate postcopy data structures
2374 *
2375 * Returns 0 for success and negative if there was one error
2376 *
2377 * @mis: current migration incoming state
2378 *
2379 * Allocate data structures etc needed by incoming migration with
2380 * postcopy-ram. postcopy-ram's similarly names
2381 * postcopy_ram_incoming_init does the work.
1caddf8a
DDAG
2382 */
2383int ram_postcopy_incoming_init(MigrationIncomingState *mis)
2384{
b8c48993 2385 unsigned long ram_pages = last_ram_page();
1caddf8a
DDAG
2386
2387 return postcopy_ram_incoming_init(mis, ram_pages);
2388}
2389
3d0684b2
JQ
2390/**
2391 * ram_load_postcopy: load a page in postcopy case
2392 *
2393 * Returns 0 for success or -errno in case of error
2394 *
a7180877
DDAG
2395 * Called in postcopy mode by ram_load().
2396 * rcu_read_lock is taken prior to this being called.
3d0684b2
JQ
2397 *
2398 * @f: QEMUFile where to send the data
a7180877
DDAG
2399 */
2400static int ram_load_postcopy(QEMUFile *f)
2401{
2402 int flags = 0, ret = 0;
2403 bool place_needed = false;
28abd200 2404 bool matching_page_sizes = false;
a7180877
DDAG
2405 MigrationIncomingState *mis = migration_incoming_get_current();
2406 /* Temporary page that is later 'placed' */
2407 void *postcopy_host_page = postcopy_get_tmp_page(mis);
c53b7ddc 2408 void *last_host = NULL;
a3b6ff6d 2409 bool all_zero = false;
a7180877
DDAG
2410
2411 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
2412 ram_addr_t addr;
2413 void *host = NULL;
2414 void *page_buffer = NULL;
2415 void *place_source = NULL;
df9ff5e1 2416 RAMBlock *block = NULL;
a7180877 2417 uint8_t ch;
a7180877
DDAG
2418
2419 addr = qemu_get_be64(f);
2420 flags = addr & ~TARGET_PAGE_MASK;
2421 addr &= TARGET_PAGE_MASK;
2422
2423 trace_ram_load_postcopy_loop((uint64_t)addr, flags);
2424 place_needed = false;
bb890ed5 2425 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE)) {
df9ff5e1 2426 block = ram_block_from_stream(f, flags);
4c4bad48
HZ
2427
2428 host = host_from_ram_block_offset(block, addr);
a7180877
DDAG
2429 if (!host) {
2430 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
2431 ret = -EINVAL;
2432 break;
2433 }
28abd200 2434 matching_page_sizes = block->page_size == TARGET_PAGE_SIZE;
a7180877 2435 /*
28abd200
DDAG
2436 * Postcopy requires that we place whole host pages atomically;
2437 * these may be huge pages for RAMBlocks that are backed by
2438 * hugetlbfs.
a7180877
DDAG
2439 * To make it atomic, the data is read into a temporary page
2440 * that's moved into place later.
2441 * The migration protocol uses, possibly smaller, target-pages
2442 * however the source ensures it always sends all the components
2443 * of a host page in order.
2444 */
2445 page_buffer = postcopy_host_page +
28abd200 2446 ((uintptr_t)host & (block->page_size - 1));
a7180877 2447 /* If all TP are zero then we can optimise the place */
28abd200 2448 if (!((uintptr_t)host & (block->page_size - 1))) {
a7180877 2449 all_zero = true;
c53b7ddc
DDAG
2450 } else {
2451 /* not the 1st TP within the HP */
2452 if (host != (last_host + TARGET_PAGE_SIZE)) {
9af9e0fe 2453 error_report("Non-sequential target page %p/%p",
c53b7ddc
DDAG
2454 host, last_host);
2455 ret = -EINVAL;
2456 break;
2457 }
a7180877
DDAG
2458 }
2459
c53b7ddc 2460
a7180877
DDAG
2461 /*
2462 * If it's the last part of a host page then we place the host
2463 * page
2464 */
2465 place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) &
28abd200 2466 (block->page_size - 1)) == 0;
a7180877
DDAG
2467 place_source = postcopy_host_page;
2468 }
c53b7ddc 2469 last_host = host;
a7180877
DDAG
2470
2471 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
bb890ed5 2472 case RAM_SAVE_FLAG_ZERO:
a7180877
DDAG
2473 ch = qemu_get_byte(f);
2474 memset(page_buffer, ch, TARGET_PAGE_SIZE);
2475 if (ch) {
2476 all_zero = false;
2477 }
2478 break;
2479
2480 case RAM_SAVE_FLAG_PAGE:
2481 all_zero = false;
2482 if (!place_needed || !matching_page_sizes) {
2483 qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE);
2484 } else {
2485 /* Avoids the qemu_file copy during postcopy, which is
2486 * going to do a copy later; can only do it when we
2487 * do this read in one go (matching page sizes)
2488 */
2489 qemu_get_buffer_in_place(f, (uint8_t **)&place_source,
2490 TARGET_PAGE_SIZE);
2491 }
2492 break;
2493 case RAM_SAVE_FLAG_EOS:
2494 /* normal exit */
2495 break;
2496 default:
2497 error_report("Unknown combination of migration flags: %#x"
2498 " (postcopy mode)", flags);
2499 ret = -EINVAL;
2500 }
2501
2502 if (place_needed) {
2503 /* This gets called at the last target page in the host page */
df9ff5e1
DDAG
2504 void *place_dest = host + TARGET_PAGE_SIZE - block->page_size;
2505
a7180877 2506 if (all_zero) {
df9ff5e1
DDAG
2507 ret = postcopy_place_page_zero(mis, place_dest,
2508 block->page_size);
a7180877 2509 } else {
df9ff5e1
DDAG
2510 ret = postcopy_place_page(mis, place_dest,
2511 place_source, block->page_size);
a7180877
DDAG
2512 }
2513 }
2514 if (!ret) {
2515 ret = qemu_file_get_error(f);
2516 }
2517 }
2518
2519 return ret;
2520}
2521
56e93d26
JQ
2522static int ram_load(QEMUFile *f, void *opaque, int version_id)
2523{
2524 int flags = 0, ret = 0;
2525 static uint64_t seq_iter;
2526 int len = 0;
a7180877
DDAG
2527 /*
2528 * If system is running in postcopy mode, page inserts to host memory must
2529 * be atomic
2530 */
2531 bool postcopy_running = postcopy_state_get() >= POSTCOPY_INCOMING_LISTENING;
ef08fb38
DDAG
2532 /* ADVISE is earlier, it shows the source has the postcopy capability on */
2533 bool postcopy_advised = postcopy_state_get() >= POSTCOPY_INCOMING_ADVISE;
56e93d26
JQ
2534
2535 seq_iter++;
2536
2537 if (version_id != 4) {
2538 ret = -EINVAL;
2539 }
2540
2541 /* This RCU critical section can be very long running.
2542 * When RCU reclaims in the code start to become numerous,
2543 * it will be necessary to reduce the granularity of this
2544 * critical section.
2545 */
2546 rcu_read_lock();
a7180877
DDAG
2547
2548 if (postcopy_running) {
2549 ret = ram_load_postcopy(f);
2550 }
2551
2552 while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) {
56e93d26 2553 ram_addr_t addr, total_ram_bytes;
a776aa15 2554 void *host = NULL;
56e93d26
JQ
2555 uint8_t ch;
2556
2557 addr = qemu_get_be64(f);
2558 flags = addr & ~TARGET_PAGE_MASK;
2559 addr &= TARGET_PAGE_MASK;
2560
bb890ed5 2561 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE |
a776aa15 2562 RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) {
4c4bad48
HZ
2563 RAMBlock *block = ram_block_from_stream(f, flags);
2564
2565 host = host_from_ram_block_offset(block, addr);
a776aa15
DDAG
2566 if (!host) {
2567 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
2568 ret = -EINVAL;
2569 break;
2570 }
1db9d8e5 2571 trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
a776aa15
DDAG
2572 }
2573
56e93d26
JQ
2574 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
2575 case RAM_SAVE_FLAG_MEM_SIZE:
2576 /* Synchronize RAM block list */
2577 total_ram_bytes = addr;
2578 while (!ret && total_ram_bytes) {
2579 RAMBlock *block;
56e93d26
JQ
2580 char id[256];
2581 ram_addr_t length;
2582
2583 len = qemu_get_byte(f);
2584 qemu_get_buffer(f, (uint8_t *)id, len);
2585 id[len] = 0;
2586 length = qemu_get_be64(f);
2587
e3dd7493
DDAG
2588 block = qemu_ram_block_by_name(id);
2589 if (block) {
2590 if (length != block->used_length) {
2591 Error *local_err = NULL;
56e93d26 2592
fa53a0e5 2593 ret = qemu_ram_resize(block, length,
e3dd7493
DDAG
2594 &local_err);
2595 if (local_err) {
2596 error_report_err(local_err);
56e93d26 2597 }
56e93d26 2598 }
ef08fb38
DDAG
2599 /* For postcopy we need to check hugepage sizes match */
2600 if (postcopy_advised &&
2601 block->page_size != qemu_host_page_size) {
2602 uint64_t remote_page_size = qemu_get_be64(f);
2603 if (remote_page_size != block->page_size) {
2604 error_report("Mismatched RAM page size %s "
2605 "(local) %zd != %" PRId64,
2606 id, block->page_size,
2607 remote_page_size);
2608 ret = -EINVAL;
2609 }
2610 }
e3dd7493
DDAG
2611 ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG,
2612 block->idstr);
2613 } else {
56e93d26
JQ
2614 error_report("Unknown ramblock \"%s\", cannot "
2615 "accept migration", id);
2616 ret = -EINVAL;
2617 }
2618
2619 total_ram_bytes -= length;
2620 }
2621 break;
a776aa15 2622
bb890ed5 2623 case RAM_SAVE_FLAG_ZERO:
56e93d26
JQ
2624 ch = qemu_get_byte(f);
2625 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
2626 break;
a776aa15 2627
56e93d26 2628 case RAM_SAVE_FLAG_PAGE:
56e93d26
JQ
2629 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
2630 break;
56e93d26 2631
a776aa15 2632 case RAM_SAVE_FLAG_COMPRESS_PAGE:
56e93d26
JQ
2633 len = qemu_get_be32(f);
2634 if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) {
2635 error_report("Invalid compressed data length: %d", len);
2636 ret = -EINVAL;
2637 break;
2638 }
c1bc6626 2639 decompress_data_with_multi_threads(f, host, len);
56e93d26 2640 break;
a776aa15 2641
56e93d26 2642 case RAM_SAVE_FLAG_XBZRLE:
56e93d26
JQ
2643 if (load_xbzrle(f, addr, host) < 0) {
2644 error_report("Failed to decompress XBZRLE page at "
2645 RAM_ADDR_FMT, addr);
2646 ret = -EINVAL;
2647 break;
2648 }
2649 break;
2650 case RAM_SAVE_FLAG_EOS:
2651 /* normal exit */
2652 break;
2653 default:
2654 if (flags & RAM_SAVE_FLAG_HOOK) {
632e3a5c 2655 ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
56e93d26
JQ
2656 } else {
2657 error_report("Unknown combination of migration flags: %#x",
2658 flags);
2659 ret = -EINVAL;
2660 }
2661 }
2662 if (!ret) {
2663 ret = qemu_file_get_error(f);
2664 }
2665 }
2666
5533b2e9 2667 wait_for_decompress_done();
56e93d26 2668 rcu_read_unlock();
55c4446b 2669 trace_ram_load_complete(ret, seq_iter);
56e93d26
JQ
2670 return ret;
2671}
2672
2673static SaveVMHandlers savevm_ram_handlers = {
2674 .save_live_setup = ram_save_setup,
2675 .save_live_iterate = ram_save_iterate,
763c906b 2676 .save_live_complete_postcopy = ram_save_complete,
a3e06c3d 2677 .save_live_complete_precopy = ram_save_complete,
56e93d26
JQ
2678 .save_live_pending = ram_save_pending,
2679 .load_state = ram_load,
6ad2a215 2680 .cleanup = ram_migration_cleanup,
56e93d26
JQ
2681};
2682
2683void ram_mig_init(void)
2684{
2685 qemu_mutex_init(&XBZRLE.lock);
6f37bb8b 2686 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, &ram_state);
56e93d26 2687}