]> git.proxmox.com Git - mirror_qemu.git/blame - migration/ram.c
migration: introduce decompress-error-check
[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 */
e688df6b 28
1393a485 29#include "qemu/osdep.h"
33c11879 30#include "cpu.h"
56e93d26 31#include <zlib.h>
f348b6d1 32#include "qemu/cutils.h"
56e93d26
JQ
33#include "qemu/bitops.h"
34#include "qemu/bitmap.h"
7205c9ec 35#include "qemu/main-loop.h"
709e3fe8 36#include "xbzrle.h"
7b1e1a22 37#include "ram.h"
6666c96a 38#include "migration.h"
71bb07db 39#include "socket.h"
f2a8f0a6 40#include "migration/register.h"
7b1e1a22 41#include "migration/misc.h"
08a0aee1 42#include "qemu-file.h"
be07b0ac 43#include "postcopy-ram.h"
56e93d26 44#include "migration/page_cache.h"
56e93d26 45#include "qemu/error-report.h"
e688df6b 46#include "qapi/error.h"
9af23989 47#include "qapi/qapi-events-migration.h"
8acabf69 48#include "qapi/qmp/qerror.h"
56e93d26 49#include "trace.h"
56e93d26 50#include "exec/ram_addr.h"
f9494614 51#include "exec/target_page.h"
56e93d26 52#include "qemu/rcu_queue.h"
a91246c9 53#include "migration/colo.h"
9ac78b61 54#include "migration/block.h"
af8b7d2b
JQ
55#include "sysemu/sysemu.h"
56#include "qemu/uuid.h"
edd090c7 57#include "savevm.h"
56e93d26 58
56e93d26
JQ
59/***********************************************************/
60/* ram save/restore */
61
bb890ed5
JQ
62/* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it
63 * worked for pages that where filled with the same char. We switched
64 * it to only search for the zero value. And to avoid confusion with
65 * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it.
66 */
67
56e93d26 68#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
bb890ed5 69#define RAM_SAVE_FLAG_ZERO 0x02
56e93d26
JQ
70#define RAM_SAVE_FLAG_MEM_SIZE 0x04
71#define RAM_SAVE_FLAG_PAGE 0x08
72#define RAM_SAVE_FLAG_EOS 0x10
73#define RAM_SAVE_FLAG_CONTINUE 0x20
74#define RAM_SAVE_FLAG_XBZRLE 0x40
75/* 0x80 is reserved in migration.h start with 0x100 next */
76#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
77
56e93d26
JQ
78static inline bool is_zero_range(uint8_t *p, uint64_t size)
79{
a1febc49 80 return buffer_is_zero(p, size);
56e93d26
JQ
81}
82
9360447d
JQ
83XBZRLECacheStats xbzrle_counters;
84
56e93d26
JQ
85/* struct contains XBZRLE cache and a static page
86 used by the compression */
87static struct {
88 /* buffer used for XBZRLE encoding */
89 uint8_t *encoded_buf;
90 /* buffer for storing page content */
91 uint8_t *current_buf;
92 /* Cache for XBZRLE, Protected by lock. */
93 PageCache *cache;
94 QemuMutex lock;
c00e0928
JQ
95 /* it will store a page full of zeros */
96 uint8_t *zero_target_page;
f265e0e4
JQ
97 /* buffer used for XBZRLE decoding */
98 uint8_t *decoded_buf;
56e93d26
JQ
99} XBZRLE;
100
56e93d26
JQ
101static void XBZRLE_cache_lock(void)
102{
103 if (migrate_use_xbzrle())
104 qemu_mutex_lock(&XBZRLE.lock);
105}
106
107static void XBZRLE_cache_unlock(void)
108{
109 if (migrate_use_xbzrle())
110 qemu_mutex_unlock(&XBZRLE.lock);
111}
112
3d0684b2
JQ
113/**
114 * xbzrle_cache_resize: resize the xbzrle cache
115 *
116 * This function is called from qmp_migrate_set_cache_size in main
117 * thread, possibly while a migration is in progress. A running
118 * migration may be using the cache and might finish during this call,
119 * hence changes to the cache are protected by XBZRLE.lock().
120 *
c9dede2d 121 * Returns 0 for success or -1 for error
3d0684b2
JQ
122 *
123 * @new_size: new cache size
8acabf69 124 * @errp: set *errp if the check failed, with reason
56e93d26 125 */
c9dede2d 126int xbzrle_cache_resize(int64_t new_size, Error **errp)
56e93d26
JQ
127{
128 PageCache *new_cache;
c9dede2d 129 int64_t ret = 0;
56e93d26 130
8acabf69
JQ
131 /* Check for truncation */
132 if (new_size != (size_t)new_size) {
133 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
134 "exceeding address space");
135 return -1;
136 }
137
2a313e5c
JQ
138 if (new_size == migrate_xbzrle_cache_size()) {
139 /* nothing to do */
c9dede2d 140 return 0;
2a313e5c
JQ
141 }
142
56e93d26
JQ
143 XBZRLE_cache_lock();
144
145 if (XBZRLE.cache != NULL) {
80f8dfde 146 new_cache = cache_init(new_size, TARGET_PAGE_SIZE, errp);
56e93d26 147 if (!new_cache) {
56e93d26
JQ
148 ret = -1;
149 goto out;
150 }
151
152 cache_fini(XBZRLE.cache);
153 XBZRLE.cache = new_cache;
154 }
56e93d26
JQ
155out:
156 XBZRLE_cache_unlock();
157 return ret;
158}
159
f9494614
AP
160static void ramblock_recv_map_init(void)
161{
162 RAMBlock *rb;
163
164 RAMBLOCK_FOREACH(rb) {
165 assert(!rb->receivedmap);
166 rb->receivedmap = bitmap_new(rb->max_length >> qemu_target_page_bits());
167 }
168}
169
170int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_addr)
171{
172 return test_bit(ramblock_recv_bitmap_offset(host_addr, rb),
173 rb->receivedmap);
174}
175
1cba9f6e
DDAG
176bool ramblock_recv_bitmap_test_byte_offset(RAMBlock *rb, uint64_t byte_offset)
177{
178 return test_bit(byte_offset >> TARGET_PAGE_BITS, rb->receivedmap);
179}
180
f9494614
AP
181void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr)
182{
183 set_bit_atomic(ramblock_recv_bitmap_offset(host_addr, rb), rb->receivedmap);
184}
185
186void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr,
187 size_t nr)
188{
189 bitmap_set_atomic(rb->receivedmap,
190 ramblock_recv_bitmap_offset(host_addr, rb),
191 nr);
192}
193
a335debb
PX
194#define RAMBLOCK_RECV_BITMAP_ENDING (0x0123456789abcdefULL)
195
196/*
197 * Format: bitmap_size (8 bytes) + whole_bitmap (N bytes).
198 *
199 * Returns >0 if success with sent bytes, or <0 if error.
200 */
201int64_t ramblock_recv_bitmap_send(QEMUFile *file,
202 const char *block_name)
203{
204 RAMBlock *block = qemu_ram_block_by_name(block_name);
205 unsigned long *le_bitmap, nbits;
206 uint64_t size;
207
208 if (!block) {
209 error_report("%s: invalid block name: %s", __func__, block_name);
210 return -1;
211 }
212
213 nbits = block->used_length >> TARGET_PAGE_BITS;
214
215 /*
216 * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit
217 * machines we may need 4 more bytes for padding (see below
218 * comment). So extend it a bit before hand.
219 */
220 le_bitmap = bitmap_new(nbits + BITS_PER_LONG);
221
222 /*
223 * Always use little endian when sending the bitmap. This is
224 * required that when source and destination VMs are not using the
225 * same endianess. (Note: big endian won't work.)
226 */
227 bitmap_to_le(le_bitmap, block->receivedmap, nbits);
228
229 /* Size of the bitmap, in bytes */
230 size = nbits / 8;
231
232 /*
233 * size is always aligned to 8 bytes for 64bit machines, but it
234 * may not be true for 32bit machines. We need this padding to
235 * make sure the migration can survive even between 32bit and
236 * 64bit machines.
237 */
238 size = ROUND_UP(size, 8);
239
240 qemu_put_be64(file, size);
241 qemu_put_buffer(file, (const uint8_t *)le_bitmap, size);
242 /*
243 * Mark as an end, in case the middle part is screwed up due to
244 * some "misterious" reason.
245 */
246 qemu_put_be64(file, RAMBLOCK_RECV_BITMAP_ENDING);
247 qemu_fflush(file);
248
bf269906 249 g_free(le_bitmap);
a335debb
PX
250
251 if (qemu_file_get_error(file)) {
252 return qemu_file_get_error(file);
253 }
254
255 return size + sizeof(size);
256}
257
ec481c6c
JQ
258/*
259 * An outstanding page request, on the source, having been received
260 * and queued
261 */
262struct RAMSrcPageRequest {
263 RAMBlock *rb;
264 hwaddr offset;
265 hwaddr len;
266
267 QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req;
268};
269
6f37bb8b
JQ
270/* State of RAM for migration */
271struct RAMState {
204b88b8
JQ
272 /* QEMUFile used for this migration */
273 QEMUFile *f;
6f37bb8b
JQ
274 /* Last block that we have visited searching for dirty pages */
275 RAMBlock *last_seen_block;
276 /* Last block from where we have sent data */
277 RAMBlock *last_sent_block;
269ace29
JQ
278 /* Last dirty target page we have sent */
279 ram_addr_t last_page;
6f37bb8b
JQ
280 /* last ram version we have seen */
281 uint32_t last_version;
282 /* We are in the first round */
283 bool ram_bulk_stage;
8d820d6f
JQ
284 /* How many times we have dirty too many pages */
285 int dirty_rate_high_cnt;
f664da80
JQ
286 /* these variables are used for bitmap sync */
287 /* last time we did a full bitmap_sync */
288 int64_t time_last_bitmap_sync;
eac74159 289 /* bytes transferred at start_time */
c4bdf0cf 290 uint64_t bytes_xfer_prev;
a66cd90c 291 /* number of dirty pages since start_time */
68908ed6 292 uint64_t num_dirty_pages_period;
b5833fde
JQ
293 /* xbzrle misses since the beginning of the period */
294 uint64_t xbzrle_cache_miss_prev;
36040d9c
JQ
295 /* number of iterations at the beginning of period */
296 uint64_t iterations_prev;
23b28c3c
JQ
297 /* Iterations since start */
298 uint64_t iterations;
9360447d 299 /* number of dirty bits in the bitmap */
2dfaf12e
PX
300 uint64_t migration_dirty_pages;
301 /* protects modification of the bitmap */
108cfae0 302 QemuMutex bitmap_mutex;
68a098f3
JQ
303 /* The RAMBlock used in the last src_page_requests */
304 RAMBlock *last_req_rb;
ec481c6c
JQ
305 /* Queue of outstanding page requests from the destination */
306 QemuMutex src_page_req_mutex;
307 QSIMPLEQ_HEAD(src_page_requests, RAMSrcPageRequest) src_page_requests;
6f37bb8b
JQ
308};
309typedef struct RAMState RAMState;
310
53518d94 311static RAMState *ram_state;
6f37bb8b 312
9edabd4d 313uint64_t ram_bytes_remaining(void)
2f4fde93 314{
bae416e5
DDAG
315 return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) :
316 0;
2f4fde93
JQ
317}
318
9360447d 319MigrationStats ram_counters;
96506894 320
b8fb8cb7
DDAG
321/* used by the search for pages to send */
322struct PageSearchStatus {
323 /* Current block being searched */
324 RAMBlock *block;
a935e30f
JQ
325 /* Current page to search from */
326 unsigned long page;
b8fb8cb7
DDAG
327 /* Set once we wrap around */
328 bool complete_round;
329};
330typedef struct PageSearchStatus PageSearchStatus;
331
56e93d26 332struct CompressParam {
56e93d26 333 bool done;
90e56fb4 334 bool quit;
56e93d26
JQ
335 QEMUFile *file;
336 QemuMutex mutex;
337 QemuCond cond;
338 RAMBlock *block;
339 ram_addr_t offset;
34ab9e97
XG
340
341 /* internally used fields */
dcaf446e 342 z_stream stream;
34ab9e97 343 uint8_t *originbuf;
56e93d26
JQ
344};
345typedef struct CompressParam CompressParam;
346
347struct DecompressParam {
73a8912b 348 bool done;
90e56fb4 349 bool quit;
56e93d26
JQ
350 QemuMutex mutex;
351 QemuCond cond;
352 void *des;
d341d9f3 353 uint8_t *compbuf;
56e93d26 354 int len;
797ca154 355 z_stream stream;
56e93d26
JQ
356};
357typedef struct DecompressParam DecompressParam;
358
359static CompressParam *comp_param;
360static QemuThread *compress_threads;
361/* comp_done_cond is used to wake up the migration thread when
362 * one of the compression threads has finished the compression.
363 * comp_done_lock is used to co-work with comp_done_cond.
364 */
0d9f9a5c
LL
365static QemuMutex comp_done_lock;
366static QemuCond comp_done_cond;
56e93d26
JQ
367/* The empty QEMUFileOps will be used by file in CompressParam */
368static const QEMUFileOps empty_ops = { };
369
34ab9e97 370static QEMUFile *decomp_file;
56e93d26
JQ
371static DecompressParam *decomp_param;
372static QemuThread *decompress_threads;
73a8912b
LL
373static QemuMutex decomp_done_lock;
374static QemuCond decomp_done_cond;
56e93d26 375
dcaf446e 376static int do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block,
34ab9e97 377 ram_addr_t offset, uint8_t *source_buf);
56e93d26
JQ
378
379static void *do_data_compress(void *opaque)
380{
381 CompressParam *param = opaque;
a7a9a88f
LL
382 RAMBlock *block;
383 ram_addr_t offset;
56e93d26 384
a7a9a88f 385 qemu_mutex_lock(&param->mutex);
90e56fb4 386 while (!param->quit) {
a7a9a88f
LL
387 if (param->block) {
388 block = param->block;
389 offset = param->offset;
390 param->block = NULL;
391 qemu_mutex_unlock(&param->mutex);
392
34ab9e97
XG
393 do_compress_ram_page(param->file, &param->stream, block, offset,
394 param->originbuf);
a7a9a88f 395
0d9f9a5c 396 qemu_mutex_lock(&comp_done_lock);
a7a9a88f 397 param->done = true;
0d9f9a5c
LL
398 qemu_cond_signal(&comp_done_cond);
399 qemu_mutex_unlock(&comp_done_lock);
a7a9a88f
LL
400
401 qemu_mutex_lock(&param->mutex);
402 } else {
56e93d26
JQ
403 qemu_cond_wait(&param->cond, &param->mutex);
404 }
56e93d26 405 }
a7a9a88f 406 qemu_mutex_unlock(&param->mutex);
56e93d26
JQ
407
408 return NULL;
409}
410
411static inline void terminate_compression_threads(void)
412{
413 int idx, thread_count;
414
415 thread_count = migrate_compress_threads();
3d0684b2 416
56e93d26
JQ
417 for (idx = 0; idx < thread_count; idx++) {
418 qemu_mutex_lock(&comp_param[idx].mutex);
90e56fb4 419 comp_param[idx].quit = true;
56e93d26
JQ
420 qemu_cond_signal(&comp_param[idx].cond);
421 qemu_mutex_unlock(&comp_param[idx].mutex);
422 }
423}
424
f0afa331 425static void compress_threads_save_cleanup(void)
56e93d26
JQ
426{
427 int i, thread_count;
428
429 if (!migrate_use_compression()) {
430 return;
431 }
432 terminate_compression_threads();
433 thread_count = migrate_compress_threads();
434 for (i = 0; i < thread_count; i++) {
dcaf446e
XG
435 /*
436 * we use it as a indicator which shows if the thread is
437 * properly init'd or not
438 */
439 if (!comp_param[i].file) {
440 break;
441 }
56e93d26 442 qemu_thread_join(compress_threads + i);
56e93d26
JQ
443 qemu_mutex_destroy(&comp_param[i].mutex);
444 qemu_cond_destroy(&comp_param[i].cond);
dcaf446e 445 deflateEnd(&comp_param[i].stream);
34ab9e97 446 g_free(comp_param[i].originbuf);
dcaf446e
XG
447 qemu_fclose(comp_param[i].file);
448 comp_param[i].file = NULL;
56e93d26 449 }
0d9f9a5c
LL
450 qemu_mutex_destroy(&comp_done_lock);
451 qemu_cond_destroy(&comp_done_cond);
56e93d26
JQ
452 g_free(compress_threads);
453 g_free(comp_param);
56e93d26
JQ
454 compress_threads = NULL;
455 comp_param = NULL;
56e93d26
JQ
456}
457
dcaf446e 458static int compress_threads_save_setup(void)
56e93d26
JQ
459{
460 int i, thread_count;
461
462 if (!migrate_use_compression()) {
dcaf446e 463 return 0;
56e93d26 464 }
56e93d26
JQ
465 thread_count = migrate_compress_threads();
466 compress_threads = g_new0(QemuThread, thread_count);
467 comp_param = g_new0(CompressParam, thread_count);
0d9f9a5c
LL
468 qemu_cond_init(&comp_done_cond);
469 qemu_mutex_init(&comp_done_lock);
56e93d26 470 for (i = 0; i < thread_count; i++) {
34ab9e97
XG
471 comp_param[i].originbuf = g_try_malloc(TARGET_PAGE_SIZE);
472 if (!comp_param[i].originbuf) {
473 goto exit;
474 }
475
dcaf446e
XG
476 if (deflateInit(&comp_param[i].stream,
477 migrate_compress_level()) != Z_OK) {
34ab9e97 478 g_free(comp_param[i].originbuf);
dcaf446e
XG
479 goto exit;
480 }
481
e110aa91
C
482 /* comp_param[i].file is just used as a dummy buffer to save data,
483 * set its ops to empty.
56e93d26
JQ
484 */
485 comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops);
486 comp_param[i].done = true;
90e56fb4 487 comp_param[i].quit = false;
56e93d26
JQ
488 qemu_mutex_init(&comp_param[i].mutex);
489 qemu_cond_init(&comp_param[i].cond);
490 qemu_thread_create(compress_threads + i, "compress",
491 do_data_compress, comp_param + i,
492 QEMU_THREAD_JOINABLE);
493 }
dcaf446e
XG
494 return 0;
495
496exit:
497 compress_threads_save_cleanup();
498 return -1;
56e93d26
JQ
499}
500
f986c3d2
JQ
501/* Multiple fd's */
502
af8b7d2b
JQ
503#define MULTIFD_MAGIC 0x11223344U
504#define MULTIFD_VERSION 1
505
506typedef struct {
507 uint32_t magic;
508 uint32_t version;
509 unsigned char uuid[16]; /* QemuUUID */
510 uint8_t id;
511} __attribute__((packed)) MultiFDInit_t;
512
8c4598f2
JQ
513typedef struct {
514 /* this fields are not changed once the thread is created */
515 /* channel number */
f986c3d2 516 uint8_t id;
8c4598f2 517 /* channel thread name */
f986c3d2 518 char *name;
8c4598f2 519 /* channel thread id */
f986c3d2 520 QemuThread thread;
8c4598f2 521 /* communication channel */
60df2d4a 522 QIOChannel *c;
8c4598f2 523 /* sem where to wait for more work */
f986c3d2 524 QemuSemaphore sem;
8c4598f2 525 /* this mutex protects the following parameters */
f986c3d2 526 QemuMutex mutex;
8c4598f2 527 /* is this channel thread running */
66770707 528 bool running;
8c4598f2 529 /* should this thread finish */
f986c3d2 530 bool quit;
8c4598f2
JQ
531} MultiFDSendParams;
532
533typedef struct {
534 /* this fields are not changed once the thread is created */
535 /* channel number */
536 uint8_t id;
537 /* channel thread name */
538 char *name;
539 /* channel thread id */
540 QemuThread thread;
541 /* communication channel */
542 QIOChannel *c;
543 /* sem where to wait for more work */
544 QemuSemaphore sem;
545 /* this mutex protects the following parameters */
546 QemuMutex mutex;
547 /* is this channel thread running */
548 bool running;
549 /* should this thread finish */
550 bool quit;
551} MultiFDRecvParams;
f986c3d2 552
af8b7d2b
JQ
553static int multifd_send_initial_packet(MultiFDSendParams *p, Error **errp)
554{
555 MultiFDInit_t msg;
556 int ret;
557
558 msg.magic = cpu_to_be32(MULTIFD_MAGIC);
559 msg.version = cpu_to_be32(MULTIFD_VERSION);
560 msg.id = p->id;
561 memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid));
562
563 ret = qio_channel_write_all(p->c, (char *)&msg, sizeof(msg), errp);
564 if (ret != 0) {
565 return -1;
566 }
567 return 0;
568}
569
570static int multifd_recv_initial_packet(QIOChannel *c, Error **errp)
571{
572 MultiFDInit_t msg;
573 int ret;
574
575 ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp);
576 if (ret != 0) {
577 return -1;
578 }
579
580 be32_to_cpus(&msg.magic);
581 be32_to_cpus(&msg.version);
582
583 if (msg.magic != MULTIFD_MAGIC) {
584 error_setg(errp, "multifd: received packet magic %x "
585 "expected %x", msg.magic, MULTIFD_MAGIC);
586 return -1;
587 }
588
589 if (msg.version != MULTIFD_VERSION) {
590 error_setg(errp, "multifd: received packet version %d "
591 "expected %d", msg.version, MULTIFD_VERSION);
592 return -1;
593 }
594
595 if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) {
596 char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid);
597 char *msg_uuid = qemu_uuid_unparse_strdup((const QemuUUID *)msg.uuid);
598
599 error_setg(errp, "multifd: received uuid '%s' and expected "
600 "uuid '%s' for channel %hhd", msg_uuid, uuid, msg.id);
601 g_free(uuid);
602 g_free(msg_uuid);
603 return -1;
604 }
605
606 if (msg.id > migrate_multifd_channels()) {
607 error_setg(errp, "multifd: received channel version %d "
608 "expected %d", msg.version, MULTIFD_VERSION);
609 return -1;
610 }
611
612 return msg.id;
613}
614
f986c3d2
JQ
615struct {
616 MultiFDSendParams *params;
617 /* number of created threads */
618 int count;
619} *multifd_send_state;
620
66770707 621static void multifd_send_terminate_threads(Error *err)
f986c3d2
JQ
622{
623 int i;
624
7a169d74
JQ
625 if (err) {
626 MigrationState *s = migrate_get_current();
627 migrate_set_error(s, err);
628 if (s->state == MIGRATION_STATUS_SETUP ||
629 s->state == MIGRATION_STATUS_PRE_SWITCHOVER ||
630 s->state == MIGRATION_STATUS_DEVICE ||
631 s->state == MIGRATION_STATUS_ACTIVE) {
632 migrate_set_state(&s->state, s->state,
633 MIGRATION_STATUS_FAILED);
634 }
635 }
636
66770707 637 for (i = 0; i < migrate_multifd_channels(); i++) {
f986c3d2
JQ
638 MultiFDSendParams *p = &multifd_send_state->params[i];
639
640 qemu_mutex_lock(&p->mutex);
641 p->quit = true;
642 qemu_sem_post(&p->sem);
643 qemu_mutex_unlock(&p->mutex);
644 }
645}
646
647int multifd_save_cleanup(Error **errp)
648{
649 int i;
650 int ret = 0;
651
652 if (!migrate_use_multifd()) {
653 return 0;
654 }
66770707
JQ
655 multifd_send_terminate_threads(NULL);
656 for (i = 0; i < migrate_multifd_channels(); i++) {
f986c3d2
JQ
657 MultiFDSendParams *p = &multifd_send_state->params[i];
658
66770707
JQ
659 if (p->running) {
660 qemu_thread_join(&p->thread);
661 }
60df2d4a
JQ
662 socket_send_channel_destroy(p->c);
663 p->c = NULL;
f986c3d2
JQ
664 qemu_mutex_destroy(&p->mutex);
665 qemu_sem_destroy(&p->sem);
666 g_free(p->name);
667 p->name = NULL;
668 }
669 g_free(multifd_send_state->params);
670 multifd_send_state->params = NULL;
671 g_free(multifd_send_state);
672 multifd_send_state = NULL;
673 return ret;
674}
675
676static void *multifd_send_thread(void *opaque)
677{
678 MultiFDSendParams *p = opaque;
af8b7d2b
JQ
679 Error *local_err = NULL;
680
681 if (multifd_send_initial_packet(p, &local_err) < 0) {
682 goto out;
683 }
f986c3d2
JQ
684
685 while (true) {
686 qemu_mutex_lock(&p->mutex);
687 if (p->quit) {
688 qemu_mutex_unlock(&p->mutex);
689 break;
690 }
691 qemu_mutex_unlock(&p->mutex);
692 qemu_sem_wait(&p->sem);
693 }
694
af8b7d2b
JQ
695out:
696 if (local_err) {
697 multifd_send_terminate_threads(local_err);
698 }
699
66770707
JQ
700 qemu_mutex_lock(&p->mutex);
701 p->running = false;
702 qemu_mutex_unlock(&p->mutex);
703
f986c3d2
JQ
704 return NULL;
705}
706
60df2d4a
JQ
707static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque)
708{
709 MultiFDSendParams *p = opaque;
710 QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task));
711 Error *local_err = NULL;
712
713 if (qio_task_propagate_error(task, &local_err)) {
714 if (multifd_save_cleanup(&local_err) != 0) {
715 migrate_set_error(migrate_get_current(), local_err);
716 }
717 } else {
718 p->c = QIO_CHANNEL(sioc);
719 qio_channel_set_delay(p->c, false);
720 p->running = true;
721 qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
722 QEMU_THREAD_JOINABLE);
723
724 atomic_inc(&multifd_send_state->count);
725 }
726}
727
f986c3d2
JQ
728int multifd_save_setup(void)
729{
730 int thread_count;
731 uint8_t i;
732
733 if (!migrate_use_multifd()) {
734 return 0;
735 }
736 thread_count = migrate_multifd_channels();
737 multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
738 multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
66770707 739 atomic_set(&multifd_send_state->count, 0);
f986c3d2
JQ
740 for (i = 0; i < thread_count; i++) {
741 MultiFDSendParams *p = &multifd_send_state->params[i];
742
743 qemu_mutex_init(&p->mutex);
744 qemu_sem_init(&p->sem, 0);
745 p->quit = false;
746 p->id = i;
747 p->name = g_strdup_printf("multifdsend_%d", i);
60df2d4a 748 socket_send_channel_create(multifd_new_send_channel_async, p);
f986c3d2
JQ
749 }
750 return 0;
751}
752
f986c3d2
JQ
753struct {
754 MultiFDRecvParams *params;
755 /* number of created threads */
756 int count;
757} *multifd_recv_state;
758
66770707 759static void multifd_recv_terminate_threads(Error *err)
f986c3d2
JQ
760{
761 int i;
762
7a169d74
JQ
763 if (err) {
764 MigrationState *s = migrate_get_current();
765 migrate_set_error(s, err);
766 if (s->state == MIGRATION_STATUS_SETUP ||
767 s->state == MIGRATION_STATUS_ACTIVE) {
768 migrate_set_state(&s->state, s->state,
769 MIGRATION_STATUS_FAILED);
770 }
771 }
772
66770707 773 for (i = 0; i < migrate_multifd_channels(); i++) {
f986c3d2
JQ
774 MultiFDRecvParams *p = &multifd_recv_state->params[i];
775
776 qemu_mutex_lock(&p->mutex);
777 p->quit = true;
778 qemu_sem_post(&p->sem);
779 qemu_mutex_unlock(&p->mutex);
780 }
781}
782
783int multifd_load_cleanup(Error **errp)
784{
785 int i;
786 int ret = 0;
787
788 if (!migrate_use_multifd()) {
789 return 0;
790 }
66770707
JQ
791 multifd_recv_terminate_threads(NULL);
792 for (i = 0; i < migrate_multifd_channels(); i++) {
f986c3d2
JQ
793 MultiFDRecvParams *p = &multifd_recv_state->params[i];
794
66770707
JQ
795 if (p->running) {
796 qemu_thread_join(&p->thread);
797 }
60df2d4a
JQ
798 object_unref(OBJECT(p->c));
799 p->c = NULL;
f986c3d2
JQ
800 qemu_mutex_destroy(&p->mutex);
801 qemu_sem_destroy(&p->sem);
802 g_free(p->name);
803 p->name = NULL;
804 }
805 g_free(multifd_recv_state->params);
806 multifd_recv_state->params = NULL;
807 g_free(multifd_recv_state);
808 multifd_recv_state = NULL;
809
810 return ret;
811}
812
813static void *multifd_recv_thread(void *opaque)
814{
815 MultiFDRecvParams *p = opaque;
816
817 while (true) {
818 qemu_mutex_lock(&p->mutex);
819 if (p->quit) {
820 qemu_mutex_unlock(&p->mutex);
821 break;
822 }
823 qemu_mutex_unlock(&p->mutex);
824 qemu_sem_wait(&p->sem);
825 }
826
66770707
JQ
827 qemu_mutex_lock(&p->mutex);
828 p->running = false;
829 qemu_mutex_unlock(&p->mutex);
830
f986c3d2
JQ
831 return NULL;
832}
833
834int multifd_load_setup(void)
835{
836 int thread_count;
837 uint8_t i;
838
839 if (!migrate_use_multifd()) {
840 return 0;
841 }
842 thread_count = migrate_multifd_channels();
843 multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
844 multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
66770707 845 atomic_set(&multifd_recv_state->count, 0);
f986c3d2
JQ
846 for (i = 0; i < thread_count; i++) {
847 MultiFDRecvParams *p = &multifd_recv_state->params[i];
848
849 qemu_mutex_init(&p->mutex);
850 qemu_sem_init(&p->sem, 0);
851 p->quit = false;
852 p->id = i;
853 p->name = g_strdup_printf("multifdrecv_%d", i);
f986c3d2
JQ
854 }
855 return 0;
856}
857
62c1e0ca
JQ
858bool multifd_recv_all_channels_created(void)
859{
860 int thread_count = migrate_multifd_channels();
861
862 if (!migrate_use_multifd()) {
863 return true;
864 }
865
866 return thread_count == atomic_read(&multifd_recv_state->count);
867}
868
71bb07db
JQ
869void multifd_recv_new_channel(QIOChannel *ioc)
870{
60df2d4a 871 MultiFDRecvParams *p;
af8b7d2b
JQ
872 Error *local_err = NULL;
873 int id;
60df2d4a 874
af8b7d2b
JQ
875 id = multifd_recv_initial_packet(ioc, &local_err);
876 if (id < 0) {
877 multifd_recv_terminate_threads(local_err);
878 return;
879 }
880
881 p = &multifd_recv_state->params[id];
882 if (p->c != NULL) {
883 error_setg(&local_err, "multifd: received id '%d' already setup'",
884 id);
885 multifd_recv_terminate_threads(local_err);
886 return;
887 }
60df2d4a
JQ
888 p->c = ioc;
889 object_ref(OBJECT(ioc));
890
891 p->running = true;
892 qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
893 QEMU_THREAD_JOINABLE);
894 atomic_inc(&multifd_recv_state->count);
36c2f8be
JQ
895 if (multifd_recv_state->count == migrate_multifd_channels()) {
896 migration_incoming_process();
897 }
71bb07db
JQ
898}
899
56e93d26 900/**
3d0684b2 901 * save_page_header: write page header to wire
56e93d26
JQ
902 *
903 * If this is the 1st block, it also writes the block identification
904 *
3d0684b2 905 * Returns the number of bytes written
56e93d26
JQ
906 *
907 * @f: QEMUFile where to send the data
908 * @block: block that contains the page we want to send
909 * @offset: offset inside the block for the page
910 * in the lower bits, it contains flags
911 */
2bf3aa85
JQ
912static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block,
913 ram_addr_t offset)
56e93d26 914{
9f5f380b 915 size_t size, len;
56e93d26 916
24795694
JQ
917 if (block == rs->last_sent_block) {
918 offset |= RAM_SAVE_FLAG_CONTINUE;
919 }
2bf3aa85 920 qemu_put_be64(f, offset);
56e93d26
JQ
921 size = 8;
922
923 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
9f5f380b 924 len = strlen(block->idstr);
2bf3aa85
JQ
925 qemu_put_byte(f, len);
926 qemu_put_buffer(f, (uint8_t *)block->idstr, len);
9f5f380b 927 size += 1 + len;
24795694 928 rs->last_sent_block = block;
56e93d26
JQ
929 }
930 return size;
931}
932
3d0684b2
JQ
933/**
934 * mig_throttle_guest_down: throotle down the guest
935 *
936 * Reduce amount of guest cpu execution to hopefully slow down memory
937 * writes. If guest dirty memory rate is reduced below the rate at
938 * which we can transfer pages to the destination then we should be
939 * able to complete migration. Some workloads dirty memory way too
940 * fast and will not effectively converge, even with auto-converge.
070afca2
JH
941 */
942static void mig_throttle_guest_down(void)
943{
944 MigrationState *s = migrate_get_current();
2594f56d
DB
945 uint64_t pct_initial = s->parameters.cpu_throttle_initial;
946 uint64_t pct_icrement = s->parameters.cpu_throttle_increment;
070afca2
JH
947
948 /* We have not started throttling yet. Let's start it. */
949 if (!cpu_throttle_active()) {
950 cpu_throttle_set(pct_initial);
951 } else {
952 /* Throttling already on, just increase the rate */
953 cpu_throttle_set(cpu_throttle_get_percentage() + pct_icrement);
954 }
955}
956
3d0684b2
JQ
957/**
958 * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache
959 *
6f37bb8b 960 * @rs: current RAM state
3d0684b2
JQ
961 * @current_addr: address for the zero page
962 *
963 * Update the xbzrle cache to reflect a page that's been sent as all 0.
56e93d26
JQ
964 * The important thing is that a stale (not-yet-0'd) page be replaced
965 * by the new data.
966 * As a bonus, if the page wasn't in the cache it gets added so that
3d0684b2 967 * when a small write is made into the 0'd page it gets XBZRLE sent.
56e93d26 968 */
6f37bb8b 969static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr)
56e93d26 970{
6f37bb8b 971 if (rs->ram_bulk_stage || !migrate_use_xbzrle()) {
56e93d26
JQ
972 return;
973 }
974
975 /* We don't care if this fails to allocate a new cache page
976 * as long as it updated an old one */
c00e0928 977 cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page,
9360447d 978 ram_counters.dirty_sync_count);
56e93d26
JQ
979}
980
981#define ENCODING_FLAG_XBZRLE 0x1
982
983/**
984 * save_xbzrle_page: compress and send current page
985 *
986 * Returns: 1 means that we wrote the page
987 * 0 means that page is identical to the one already sent
988 * -1 means that xbzrle would be longer than normal
989 *
5a987738 990 * @rs: current RAM state
3d0684b2
JQ
991 * @current_data: pointer to the address of the page contents
992 * @current_addr: addr of the page
56e93d26
JQ
993 * @block: block that contains the page we want to send
994 * @offset: offset inside the block for the page
995 * @last_stage: if we are at the completion stage
56e93d26 996 */
204b88b8 997static int save_xbzrle_page(RAMState *rs, uint8_t **current_data,
56e93d26 998 ram_addr_t current_addr, RAMBlock *block,
072c2511 999 ram_addr_t offset, bool last_stage)
56e93d26
JQ
1000{
1001 int encoded_len = 0, bytes_xbzrle;
1002 uint8_t *prev_cached_page;
1003
9360447d
JQ
1004 if (!cache_is_cached(XBZRLE.cache, current_addr,
1005 ram_counters.dirty_sync_count)) {
1006 xbzrle_counters.cache_miss++;
56e93d26
JQ
1007 if (!last_stage) {
1008 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
9360447d 1009 ram_counters.dirty_sync_count) == -1) {
56e93d26
JQ
1010 return -1;
1011 } else {
1012 /* update *current_data when the page has been
1013 inserted into cache */
1014 *current_data = get_cached_data(XBZRLE.cache, current_addr);
1015 }
1016 }
1017 return -1;
1018 }
1019
1020 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
1021
1022 /* save current buffer into memory */
1023 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
1024
1025 /* XBZRLE encoding (if there is no overflow) */
1026 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
1027 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
1028 TARGET_PAGE_SIZE);
1029 if (encoded_len == 0) {
55c4446b 1030 trace_save_xbzrle_page_skipping();
56e93d26
JQ
1031 return 0;
1032 } else if (encoded_len == -1) {
55c4446b 1033 trace_save_xbzrle_page_overflow();
9360447d 1034 xbzrle_counters.overflow++;
56e93d26
JQ
1035 /* update data in the cache */
1036 if (!last_stage) {
1037 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
1038 *current_data = prev_cached_page;
1039 }
1040 return -1;
1041 }
1042
1043 /* we need to update the data in the cache, in order to get the same data */
1044 if (!last_stage) {
1045 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
1046 }
1047
1048 /* Send XBZRLE based compressed page */
2bf3aa85 1049 bytes_xbzrle = save_page_header(rs, rs->f, block,
204b88b8
JQ
1050 offset | RAM_SAVE_FLAG_XBZRLE);
1051 qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE);
1052 qemu_put_be16(rs->f, encoded_len);
1053 qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len);
56e93d26 1054 bytes_xbzrle += encoded_len + 1 + 2;
9360447d
JQ
1055 xbzrle_counters.pages++;
1056 xbzrle_counters.bytes += bytes_xbzrle;
1057 ram_counters.transferred += bytes_xbzrle;
56e93d26
JQ
1058
1059 return 1;
1060}
1061
3d0684b2
JQ
1062/**
1063 * migration_bitmap_find_dirty: find the next dirty page from start
f3f491fc 1064 *
3d0684b2
JQ
1065 * Called with rcu_read_lock() to protect migration_bitmap
1066 *
1067 * Returns the byte offset within memory region of the start of a dirty page
1068 *
6f37bb8b 1069 * @rs: current RAM state
3d0684b2 1070 * @rb: RAMBlock where to search for dirty pages
a935e30f 1071 * @start: page where we start the search
f3f491fc 1072 */
56e93d26 1073static inline
a935e30f 1074unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb,
f20e2865 1075 unsigned long start)
56e93d26 1076{
6b6712ef
JQ
1077 unsigned long size = rb->used_length >> TARGET_PAGE_BITS;
1078 unsigned long *bitmap = rb->bmap;
56e93d26
JQ
1079 unsigned long next;
1080
6b6712ef
JQ
1081 if (rs->ram_bulk_stage && start > 0) {
1082 next = start + 1;
56e93d26 1083 } else {
6b6712ef 1084 next = find_next_bit(bitmap, size, start);
56e93d26
JQ
1085 }
1086
6b6712ef 1087 return next;
56e93d26
JQ
1088}
1089
06b10688 1090static inline bool migration_bitmap_clear_dirty(RAMState *rs,
f20e2865
JQ
1091 RAMBlock *rb,
1092 unsigned long page)
a82d593b
DDAG
1093{
1094 bool ret;
a82d593b 1095
6b6712ef 1096 ret = test_and_clear_bit(page, rb->bmap);
a82d593b
DDAG
1097
1098 if (ret) {
0d8ec885 1099 rs->migration_dirty_pages--;
a82d593b
DDAG
1100 }
1101 return ret;
1102}
1103
15440dd5
JQ
1104static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb,
1105 ram_addr_t start, ram_addr_t length)
56e93d26 1106{
0d8ec885 1107 rs->migration_dirty_pages +=
6b6712ef 1108 cpu_physical_memory_sync_dirty_bitmap(rb, start, length,
0d8ec885 1109 &rs->num_dirty_pages_period);
56e93d26
JQ
1110}
1111
3d0684b2
JQ
1112/**
1113 * ram_pagesize_summary: calculate all the pagesizes of a VM
1114 *
1115 * Returns a summary bitmap of the page sizes of all RAMBlocks
1116 *
1117 * For VMs with just normal pages this is equivalent to the host page
1118 * size. If it's got some huge pages then it's the OR of all the
1119 * different page sizes.
e8ca1db2
DDAG
1120 */
1121uint64_t ram_pagesize_summary(void)
1122{
1123 RAMBlock *block;
1124 uint64_t summary = 0;
1125
99e15582 1126 RAMBLOCK_FOREACH(block) {
e8ca1db2
DDAG
1127 summary |= block->page_size;
1128 }
1129
1130 return summary;
1131}
1132
8d820d6f 1133static void migration_bitmap_sync(RAMState *rs)
56e93d26
JQ
1134{
1135 RAMBlock *block;
56e93d26 1136 int64_t end_time;
c4bdf0cf 1137 uint64_t bytes_xfer_now;
56e93d26 1138
9360447d 1139 ram_counters.dirty_sync_count++;
56e93d26 1140
f664da80
JQ
1141 if (!rs->time_last_bitmap_sync) {
1142 rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
56e93d26
JQ
1143 }
1144
1145 trace_migration_bitmap_sync_start();
9c1f8f44 1146 memory_global_dirty_log_sync();
56e93d26 1147
108cfae0 1148 qemu_mutex_lock(&rs->bitmap_mutex);
56e93d26 1149 rcu_read_lock();
99e15582 1150 RAMBLOCK_FOREACH(block) {
15440dd5 1151 migration_bitmap_sync_range(rs, block, 0, block->used_length);
56e93d26
JQ
1152 }
1153 rcu_read_unlock();
108cfae0 1154 qemu_mutex_unlock(&rs->bitmap_mutex);
56e93d26 1155
a66cd90c 1156 trace_migration_bitmap_sync_end(rs->num_dirty_pages_period);
1ffb5dfd 1157
56e93d26
JQ
1158 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
1159
1160 /* more than 1 second = 1000 millisecons */
f664da80 1161 if (end_time > rs->time_last_bitmap_sync + 1000) {
d693c6f1 1162 /* calculate period counters */
9360447d 1163 ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000
d693c6f1 1164 / (end_time - rs->time_last_bitmap_sync);
9360447d 1165 bytes_xfer_now = ram_counters.transferred;
d693c6f1 1166
9ac78b61
PL
1167 /* During block migration the auto-converge logic incorrectly detects
1168 * that ram migration makes no progress. Avoid this by disabling the
1169 * throttling logic during the bulk phase of block migration. */
1170 if (migrate_auto_converge() && !blk_mig_bulk_active()) {
56e93d26
JQ
1171 /* The following detection logic can be refined later. For now:
1172 Check to see if the dirtied bytes is 50% more than the approx.
1173 amount of bytes that just got transferred since the last time we
070afca2
JH
1174 were in this routine. If that happens twice, start or increase
1175 throttling */
070afca2 1176
d693c6f1 1177 if ((rs->num_dirty_pages_period * TARGET_PAGE_SIZE >
eac74159 1178 (bytes_xfer_now - rs->bytes_xfer_prev) / 2) &&
b4a3c64b 1179 (++rs->dirty_rate_high_cnt >= 2)) {
56e93d26 1180 trace_migration_throttle();
8d820d6f 1181 rs->dirty_rate_high_cnt = 0;
070afca2 1182 mig_throttle_guest_down();
d693c6f1 1183 }
56e93d26 1184 }
070afca2 1185
56e93d26 1186 if (migrate_use_xbzrle()) {
23b28c3c 1187 if (rs->iterations_prev != rs->iterations) {
9360447d
JQ
1188 xbzrle_counters.cache_miss_rate =
1189 (double)(xbzrle_counters.cache_miss -
b5833fde 1190 rs->xbzrle_cache_miss_prev) /
23b28c3c 1191 (rs->iterations - rs->iterations_prev);
56e93d26 1192 }
23b28c3c 1193 rs->iterations_prev = rs->iterations;
9360447d 1194 rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss;
56e93d26 1195 }
d693c6f1
FF
1196
1197 /* reset period counters */
f664da80 1198 rs->time_last_bitmap_sync = end_time;
a66cd90c 1199 rs->num_dirty_pages_period = 0;
d2a4d85a 1200 rs->bytes_xfer_prev = bytes_xfer_now;
56e93d26 1201 }
4addcd4f 1202 if (migrate_use_events()) {
9360447d 1203 qapi_event_send_migration_pass(ram_counters.dirty_sync_count, NULL);
4addcd4f 1204 }
56e93d26
JQ
1205}
1206
1207/**
3d0684b2 1208 * save_zero_page: send the zero page to the stream
56e93d26 1209 *
3d0684b2 1210 * Returns the number of pages written.
56e93d26 1211 *
f7ccd61b 1212 * @rs: current RAM state
56e93d26
JQ
1213 * @block: block that contains the page we want to send
1214 * @offset: offset inside the block for the page
56e93d26 1215 */
7faccdc3 1216static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset)
56e93d26 1217{
7faccdc3 1218 uint8_t *p = block->host + offset;
56e93d26
JQ
1219 int pages = -1;
1220
1221 if (is_zero_range(p, TARGET_PAGE_SIZE)) {
9360447d
JQ
1222 ram_counters.duplicate++;
1223 ram_counters.transferred +=
bb890ed5 1224 save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_ZERO);
ce25d337 1225 qemu_put_byte(rs->f, 0);
9360447d 1226 ram_counters.transferred += 1;
56e93d26
JQ
1227 pages = 1;
1228 }
1229
1230 return pages;
1231}
1232
5727309d 1233static void ram_release_pages(const char *rbname, uint64_t offset, int pages)
53f09a10 1234{
5727309d 1235 if (!migrate_release_ram() || !migration_in_postcopy()) {
53f09a10
PB
1236 return;
1237 }
1238
aaa2064c 1239 ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS);
53f09a10
PB
1240}
1241
059ff0fb
XG
1242/*
1243 * @pages: the number of pages written by the control path,
1244 * < 0 - error
1245 * > 0 - number of pages written
1246 *
1247 * Return true if the pages has been saved, otherwise false is returned.
1248 */
1249static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
1250 int *pages)
1251{
1252 uint64_t bytes_xmit = 0;
1253 int ret;
1254
1255 *pages = -1;
1256 ret = ram_control_save_page(rs->f, block->offset, offset, TARGET_PAGE_SIZE,
1257 &bytes_xmit);
1258 if (ret == RAM_SAVE_CONTROL_NOT_SUPP) {
1259 return false;
1260 }
1261
1262 if (bytes_xmit) {
1263 ram_counters.transferred += bytes_xmit;
1264 *pages = 1;
1265 }
1266
1267 if (ret == RAM_SAVE_CONTROL_DELAYED) {
1268 return true;
1269 }
1270
1271 if (bytes_xmit > 0) {
1272 ram_counters.normal++;
1273 } else if (bytes_xmit == 0) {
1274 ram_counters.duplicate++;
1275 }
1276
1277 return true;
1278}
1279
65dacaa0
XG
1280/*
1281 * directly send the page to the stream
1282 *
1283 * Returns the number of pages written.
1284 *
1285 * @rs: current RAM state
1286 * @block: block that contains the page we want to send
1287 * @offset: offset inside the block for the page
1288 * @buf: the page to be sent
1289 * @async: send to page asyncly
1290 */
1291static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
1292 uint8_t *buf, bool async)
1293{
1294 ram_counters.transferred += save_page_header(rs, rs->f, block,
1295 offset | RAM_SAVE_FLAG_PAGE);
1296 if (async) {
1297 qemu_put_buffer_async(rs->f, buf, TARGET_PAGE_SIZE,
1298 migrate_release_ram() &
1299 migration_in_postcopy());
1300 } else {
1301 qemu_put_buffer(rs->f, buf, TARGET_PAGE_SIZE);
1302 }
1303 ram_counters.transferred += TARGET_PAGE_SIZE;
1304 ram_counters.normal++;
1305 return 1;
1306}
1307
56e93d26 1308/**
3d0684b2 1309 * ram_save_page: send the given page to the stream
56e93d26 1310 *
3d0684b2 1311 * Returns the number of pages written.
3fd3c4b3
DDAG
1312 * < 0 - error
1313 * >=0 - Number of pages written - this might legally be 0
1314 * if xbzrle noticed the page was the same.
56e93d26 1315 *
6f37bb8b 1316 * @rs: current RAM state
56e93d26
JQ
1317 * @block: block that contains the page we want to send
1318 * @offset: offset inside the block for the page
1319 * @last_stage: if we are at the completion stage
56e93d26 1320 */
a0a8aa14 1321static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
56e93d26
JQ
1322{
1323 int pages = -1;
56e93d26 1324 uint8_t *p;
56e93d26 1325 bool send_async = true;
a08f6890 1326 RAMBlock *block = pss->block;
a935e30f 1327 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
059ff0fb 1328 ram_addr_t current_addr = block->offset + offset;
56e93d26 1329
2f68e399 1330 p = block->host + offset;
1db9d8e5 1331 trace_ram_save_page(block->idstr, (uint64_t)offset, p);
56e93d26 1332
56e93d26 1333 XBZRLE_cache_lock();
d7400a34
XG
1334 if (!rs->ram_bulk_stage && !migration_in_postcopy() &&
1335 migrate_use_xbzrle()) {
059ff0fb
XG
1336 pages = save_xbzrle_page(rs, &p, current_addr, block,
1337 offset, last_stage);
1338 if (!last_stage) {
1339 /* Can't send this cached data async, since the cache page
1340 * might get updated before it gets to the wire
56e93d26 1341 */
059ff0fb 1342 send_async = false;
56e93d26
JQ
1343 }
1344 }
1345
1346 /* XBZRLE overflow or normal page */
1347 if (pages == -1) {
65dacaa0 1348 pages = save_normal_page(rs, block, offset, p, send_async);
56e93d26
JQ
1349 }
1350
1351 XBZRLE_cache_unlock();
1352
1353 return pages;
1354}
1355
dcaf446e 1356static int do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block,
34ab9e97 1357 ram_addr_t offset, uint8_t *source_buf)
56e93d26 1358{
53518d94 1359 RAMState *rs = ram_state;
56e93d26 1360 int bytes_sent, blen;
a7a9a88f 1361 uint8_t *p = block->host + (offset & TARGET_PAGE_MASK);
56e93d26 1362
2bf3aa85 1363 bytes_sent = save_page_header(rs, f, block, offset |
56e93d26 1364 RAM_SAVE_FLAG_COMPRESS_PAGE);
34ab9e97
XG
1365
1366 /*
1367 * copy it to a internal buffer to avoid it being modified by VM
1368 * so that we can catch up the error during compression and
1369 * decompression
1370 */
1371 memcpy(source_buf, p, TARGET_PAGE_SIZE);
1372 blen = qemu_put_compression_data(f, stream, source_buf, TARGET_PAGE_SIZE);
b3be2896
LL
1373 if (blen < 0) {
1374 bytes_sent = 0;
1375 qemu_file_set_error(migrate_get_current()->to_dst_file, blen);
1376 error_report("compressed data failed!");
1377 } else {
1378 bytes_sent += blen;
5727309d 1379 ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1);
b3be2896 1380 }
56e93d26
JQ
1381
1382 return bytes_sent;
1383}
1384
ce25d337 1385static void flush_compressed_data(RAMState *rs)
56e93d26
JQ
1386{
1387 int idx, len, thread_count;
1388
1389 if (!migrate_use_compression()) {
1390 return;
1391 }
1392 thread_count = migrate_compress_threads();
a7a9a88f 1393
0d9f9a5c 1394 qemu_mutex_lock(&comp_done_lock);
56e93d26 1395 for (idx = 0; idx < thread_count; idx++) {
a7a9a88f 1396 while (!comp_param[idx].done) {
0d9f9a5c 1397 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
56e93d26 1398 }
a7a9a88f 1399 }
0d9f9a5c 1400 qemu_mutex_unlock(&comp_done_lock);
a7a9a88f
LL
1401
1402 for (idx = 0; idx < thread_count; idx++) {
1403 qemu_mutex_lock(&comp_param[idx].mutex);
90e56fb4 1404 if (!comp_param[idx].quit) {
ce25d337 1405 len = qemu_put_qemu_file(rs->f, comp_param[idx].file);
9360447d 1406 ram_counters.transferred += len;
56e93d26 1407 }
a7a9a88f 1408 qemu_mutex_unlock(&comp_param[idx].mutex);
56e93d26
JQ
1409 }
1410}
1411
1412static inline void set_compress_params(CompressParam *param, RAMBlock *block,
1413 ram_addr_t offset)
1414{
1415 param->block = block;
1416 param->offset = offset;
1417}
1418
ce25d337
JQ
1419static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block,
1420 ram_addr_t offset)
56e93d26
JQ
1421{
1422 int idx, thread_count, bytes_xmit = -1, pages = -1;
1423
1424 thread_count = migrate_compress_threads();
0d9f9a5c 1425 qemu_mutex_lock(&comp_done_lock);
56e93d26
JQ
1426 while (true) {
1427 for (idx = 0; idx < thread_count; idx++) {
1428 if (comp_param[idx].done) {
a7a9a88f 1429 comp_param[idx].done = false;
ce25d337 1430 bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file);
a7a9a88f 1431 qemu_mutex_lock(&comp_param[idx].mutex);
56e93d26 1432 set_compress_params(&comp_param[idx], block, offset);
a7a9a88f
LL
1433 qemu_cond_signal(&comp_param[idx].cond);
1434 qemu_mutex_unlock(&comp_param[idx].mutex);
56e93d26 1435 pages = 1;
9360447d
JQ
1436 ram_counters.normal++;
1437 ram_counters.transferred += bytes_xmit;
56e93d26
JQ
1438 break;
1439 }
1440 }
1441 if (pages > 0) {
1442 break;
1443 } else {
0d9f9a5c 1444 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
56e93d26
JQ
1445 }
1446 }
0d9f9a5c 1447 qemu_mutex_unlock(&comp_done_lock);
56e93d26
JQ
1448
1449 return pages;
1450}
1451
3d0684b2
JQ
1452/**
1453 * find_dirty_block: find the next dirty page and update any state
1454 * associated with the search process.
b9e60928 1455 *
3d0684b2 1456 * Returns if a page is found
b9e60928 1457 *
6f37bb8b 1458 * @rs: current RAM state
3d0684b2
JQ
1459 * @pss: data about the state of the current dirty page scan
1460 * @again: set to false if the search has scanned the whole of RAM
b9e60928 1461 */
f20e2865 1462static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
b9e60928 1463{
f20e2865 1464 pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
6f37bb8b 1465 if (pss->complete_round && pss->block == rs->last_seen_block &&
a935e30f 1466 pss->page >= rs->last_page) {
b9e60928
DDAG
1467 /*
1468 * We've been once around the RAM and haven't found anything.
1469 * Give up.
1470 */
1471 *again = false;
1472 return false;
1473 }
a935e30f 1474 if ((pss->page << TARGET_PAGE_BITS) >= pss->block->used_length) {
b9e60928 1475 /* Didn't find anything in this RAM Block */
a935e30f 1476 pss->page = 0;
b9e60928
DDAG
1477 pss->block = QLIST_NEXT_RCU(pss->block, next);
1478 if (!pss->block) {
1479 /* Hit the end of the list */
1480 pss->block = QLIST_FIRST_RCU(&ram_list.blocks);
1481 /* Flag that we've looped */
1482 pss->complete_round = true;
6f37bb8b 1483 rs->ram_bulk_stage = false;
b9e60928
DDAG
1484 if (migrate_use_xbzrle()) {
1485 /* If xbzrle is on, stop using the data compression at this
1486 * point. In theory, xbzrle can do better than compression.
1487 */
ce25d337 1488 flush_compressed_data(rs);
b9e60928
DDAG
1489 }
1490 }
1491 /* Didn't find anything this time, but try again on the new block */
1492 *again = true;
1493 return false;
1494 } else {
1495 /* Can go around again, but... */
1496 *again = true;
1497 /* We've found something so probably don't need to */
1498 return true;
1499 }
1500}
1501
3d0684b2
JQ
1502/**
1503 * unqueue_page: gets a page of the queue
1504 *
a82d593b 1505 * Helper for 'get_queued_page' - gets a page off the queue
a82d593b 1506 *
3d0684b2
JQ
1507 * Returns the block of the page (or NULL if none available)
1508 *
ec481c6c 1509 * @rs: current RAM state
3d0684b2 1510 * @offset: used to return the offset within the RAMBlock
a82d593b 1511 */
f20e2865 1512static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)
a82d593b
DDAG
1513{
1514 RAMBlock *block = NULL;
1515
ec481c6c
JQ
1516 qemu_mutex_lock(&rs->src_page_req_mutex);
1517 if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) {
1518 struct RAMSrcPageRequest *entry =
1519 QSIMPLEQ_FIRST(&rs->src_page_requests);
a82d593b
DDAG
1520 block = entry->rb;
1521 *offset = entry->offset;
a82d593b
DDAG
1522
1523 if (entry->len > TARGET_PAGE_SIZE) {
1524 entry->len -= TARGET_PAGE_SIZE;
1525 entry->offset += TARGET_PAGE_SIZE;
1526 } else {
1527 memory_region_unref(block->mr);
ec481c6c 1528 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
a82d593b
DDAG
1529 g_free(entry);
1530 }
1531 }
ec481c6c 1532 qemu_mutex_unlock(&rs->src_page_req_mutex);
a82d593b
DDAG
1533
1534 return block;
1535}
1536
3d0684b2
JQ
1537/**
1538 * get_queued_page: unqueue a page from the postocpy requests
1539 *
1540 * Skips pages that are already sent (!dirty)
a82d593b 1541 *
3d0684b2 1542 * Returns if a queued page is found
a82d593b 1543 *
6f37bb8b 1544 * @rs: current RAM state
3d0684b2 1545 * @pss: data about the state of the current dirty page scan
a82d593b 1546 */
f20e2865 1547static bool get_queued_page(RAMState *rs, PageSearchStatus *pss)
a82d593b
DDAG
1548{
1549 RAMBlock *block;
1550 ram_addr_t offset;
1551 bool dirty;
1552
1553 do {
f20e2865 1554 block = unqueue_page(rs, &offset);
a82d593b
DDAG
1555 /*
1556 * We're sending this page, and since it's postcopy nothing else
1557 * will dirty it, and we must make sure it doesn't get sent again
1558 * even if this queue request was received after the background
1559 * search already sent it.
1560 */
1561 if (block) {
f20e2865
JQ
1562 unsigned long page;
1563
6b6712ef
JQ
1564 page = offset >> TARGET_PAGE_BITS;
1565 dirty = test_bit(page, block->bmap);
a82d593b 1566 if (!dirty) {
06b10688 1567 trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset,
6b6712ef 1568 page, test_bit(page, block->unsentmap));
a82d593b 1569 } else {
f20e2865 1570 trace_get_queued_page(block->idstr, (uint64_t)offset, page);
a82d593b
DDAG
1571 }
1572 }
1573
1574 } while (block && !dirty);
1575
1576 if (block) {
1577 /*
1578 * As soon as we start servicing pages out of order, then we have
1579 * to kill the bulk stage, since the bulk stage assumes
1580 * in (migration_bitmap_find_and_reset_dirty) that every page is
1581 * dirty, that's no longer true.
1582 */
6f37bb8b 1583 rs->ram_bulk_stage = false;
a82d593b
DDAG
1584
1585 /*
1586 * We want the background search to continue from the queued page
1587 * since the guest is likely to want other pages near to the page
1588 * it just requested.
1589 */
1590 pss->block = block;
a935e30f 1591 pss->page = offset >> TARGET_PAGE_BITS;
a82d593b
DDAG
1592 }
1593
1594 return !!block;
1595}
1596
6c595cde 1597/**
5e58f968
JQ
1598 * migration_page_queue_free: drop any remaining pages in the ram
1599 * request queue
6c595cde 1600 *
3d0684b2
JQ
1601 * It should be empty at the end anyway, but in error cases there may
1602 * be some left. in case that there is any page left, we drop it.
1603 *
6c595cde 1604 */
83c13382 1605static void migration_page_queue_free(RAMState *rs)
6c595cde 1606{
ec481c6c 1607 struct RAMSrcPageRequest *mspr, *next_mspr;
6c595cde
DDAG
1608 /* This queue generally should be empty - but in the case of a failed
1609 * migration might have some droppings in.
1610 */
1611 rcu_read_lock();
ec481c6c 1612 QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) {
6c595cde 1613 memory_region_unref(mspr->rb->mr);
ec481c6c 1614 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
6c595cde
DDAG
1615 g_free(mspr);
1616 }
1617 rcu_read_unlock();
1618}
1619
1620/**
3d0684b2
JQ
1621 * ram_save_queue_pages: queue the page for transmission
1622 *
1623 * A request from postcopy destination for example.
1624 *
1625 * Returns zero on success or negative on error
1626 *
3d0684b2
JQ
1627 * @rbname: Name of the RAMBLock of the request. NULL means the
1628 * same that last one.
1629 * @start: starting address from the start of the RAMBlock
1630 * @len: length (in bytes) to send
6c595cde 1631 */
96506894 1632int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len)
6c595cde
DDAG
1633{
1634 RAMBlock *ramblock;
53518d94 1635 RAMState *rs = ram_state;
6c595cde 1636
9360447d 1637 ram_counters.postcopy_requests++;
6c595cde
DDAG
1638 rcu_read_lock();
1639 if (!rbname) {
1640 /* Reuse last RAMBlock */
68a098f3 1641 ramblock = rs->last_req_rb;
6c595cde
DDAG
1642
1643 if (!ramblock) {
1644 /*
1645 * Shouldn't happen, we can't reuse the last RAMBlock if
1646 * it's the 1st request.
1647 */
1648 error_report("ram_save_queue_pages no previous block");
1649 goto err;
1650 }
1651 } else {
1652 ramblock = qemu_ram_block_by_name(rbname);
1653
1654 if (!ramblock) {
1655 /* We shouldn't be asked for a non-existent RAMBlock */
1656 error_report("ram_save_queue_pages no block '%s'", rbname);
1657 goto err;
1658 }
68a098f3 1659 rs->last_req_rb = ramblock;
6c595cde
DDAG
1660 }
1661 trace_ram_save_queue_pages(ramblock->idstr, start, len);
1662 if (start+len > ramblock->used_length) {
9458ad6b
JQ
1663 error_report("%s request overrun start=" RAM_ADDR_FMT " len="
1664 RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT,
6c595cde
DDAG
1665 __func__, start, len, ramblock->used_length);
1666 goto err;
1667 }
1668
ec481c6c
JQ
1669 struct RAMSrcPageRequest *new_entry =
1670 g_malloc0(sizeof(struct RAMSrcPageRequest));
6c595cde
DDAG
1671 new_entry->rb = ramblock;
1672 new_entry->offset = start;
1673 new_entry->len = len;
1674
1675 memory_region_ref(ramblock->mr);
ec481c6c
JQ
1676 qemu_mutex_lock(&rs->src_page_req_mutex);
1677 QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req);
1678 qemu_mutex_unlock(&rs->src_page_req_mutex);
6c595cde
DDAG
1679 rcu_read_unlock();
1680
1681 return 0;
1682
1683err:
1684 rcu_read_unlock();
1685 return -1;
1686}
1687
d7400a34
XG
1688static bool save_page_use_compression(RAMState *rs)
1689{
1690 if (!migrate_use_compression()) {
1691 return false;
1692 }
1693
1694 /*
1695 * If xbzrle is on, stop using the data compression after first
1696 * round of migration even if compression is enabled. In theory,
1697 * xbzrle can do better than compression.
1698 */
1699 if (rs->ram_bulk_stage || !migrate_use_xbzrle()) {
1700 return true;
1701 }
1702
1703 return false;
1704}
1705
a82d593b 1706/**
3d0684b2 1707 * ram_save_target_page: save one target page
a82d593b 1708 *
3d0684b2 1709 * Returns the number of pages written
a82d593b 1710 *
6f37bb8b 1711 * @rs: current RAM state
3d0684b2 1712 * @pss: data about the page we want to send
a82d593b 1713 * @last_stage: if we are at the completion stage
a82d593b 1714 */
a0a8aa14 1715static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,
f20e2865 1716 bool last_stage)
a82d593b 1717{
a8ec91f9
XG
1718 RAMBlock *block = pss->block;
1719 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
1720 int res;
1721
1722 if (control_save_page(rs, block, offset, &res)) {
1723 return res;
1724 }
1725
1faa5665 1726 /*
d7400a34
XG
1727 * When starting the process of a new block, the first page of
1728 * the block should be sent out before other pages in the same
1729 * block, and all the pages in last block should have been sent
1730 * out, keeping this order is important, because the 'cont' flag
1731 * is used to avoid resending the block name.
1faa5665 1732 */
d7400a34
XG
1733 if (block != rs->last_sent_block && save_page_use_compression(rs)) {
1734 flush_compressed_data(rs);
1735 }
1736
1737 res = save_zero_page(rs, block, offset);
1738 if (res > 0) {
1739 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
1740 * page would be stale
1741 */
1742 if (!save_page_use_compression(rs)) {
1743 XBZRLE_cache_lock();
1744 xbzrle_cache_zero_page(rs, block->offset + offset);
1745 XBZRLE_cache_unlock();
1746 }
1747 ram_release_pages(block->idstr, offset, res);
1748 return res;
1749 }
1750
da3f56cb
XG
1751 /*
1752 * Make sure the first page is sent out before other pages.
1753 *
1754 * we post it as normal page as compression will take much
1755 * CPU resource.
1756 */
1757 if (block == rs->last_sent_block && save_page_use_compression(rs)) {
701b1876 1758 return compress_page_with_multi_thread(rs, block, offset);
a82d593b
DDAG
1759 }
1760
1faa5665 1761 return ram_save_page(rs, pss, last_stage);
a82d593b
DDAG
1762}
1763
1764/**
3d0684b2 1765 * ram_save_host_page: save a whole host page
a82d593b 1766 *
3d0684b2
JQ
1767 * Starting at *offset send pages up to the end of the current host
1768 * page. It's valid for the initial offset to point into the middle of
1769 * a host page in which case the remainder of the hostpage is sent.
1770 * Only dirty target pages are sent. Note that the host page size may
1771 * be a huge page for this block.
1eb3fc0a
DDAG
1772 * The saving stops at the boundary of the used_length of the block
1773 * if the RAMBlock isn't a multiple of the host page size.
a82d593b 1774 *
3d0684b2
JQ
1775 * Returns the number of pages written or negative on error
1776 *
6f37bb8b 1777 * @rs: current RAM state
3d0684b2 1778 * @ms: current migration state
3d0684b2 1779 * @pss: data about the page we want to send
a82d593b 1780 * @last_stage: if we are at the completion stage
a82d593b 1781 */
a0a8aa14 1782static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
f20e2865 1783 bool last_stage)
a82d593b
DDAG
1784{
1785 int tmppages, pages = 0;
a935e30f
JQ
1786 size_t pagesize_bits =
1787 qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
4c011c37 1788
a82d593b 1789 do {
1faa5665
XG
1790 /* Check the pages is dirty and if it is send it */
1791 if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
1792 pss->page++;
1793 continue;
1794 }
1795
f20e2865 1796 tmppages = ram_save_target_page(rs, pss, last_stage);
a82d593b
DDAG
1797 if (tmppages < 0) {
1798 return tmppages;
1799 }
1800
1801 pages += tmppages;
1faa5665
XG
1802 if (pss->block->unsentmap) {
1803 clear_bit(pss->page, pss->block->unsentmap);
1804 }
1805
a935e30f 1806 pss->page++;
1eb3fc0a
DDAG
1807 } while ((pss->page & (pagesize_bits - 1)) &&
1808 offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS));
a82d593b
DDAG
1809
1810 /* The offset we leave with is the last one we looked at */
a935e30f 1811 pss->page--;
a82d593b
DDAG
1812 return pages;
1813}
6c595cde 1814
56e93d26 1815/**
3d0684b2 1816 * ram_find_and_save_block: finds a dirty page and sends it to f
56e93d26
JQ
1817 *
1818 * Called within an RCU critical section.
1819 *
3d0684b2 1820 * Returns the number of pages written where zero means no dirty pages
56e93d26 1821 *
6f37bb8b 1822 * @rs: current RAM state
56e93d26 1823 * @last_stage: if we are at the completion stage
a82d593b
DDAG
1824 *
1825 * On systems where host-page-size > target-page-size it will send all the
1826 * pages in a host page that are dirty.
56e93d26
JQ
1827 */
1828
ce25d337 1829static int ram_find_and_save_block(RAMState *rs, bool last_stage)
56e93d26 1830{
b8fb8cb7 1831 PageSearchStatus pss;
56e93d26 1832 int pages = 0;
b9e60928 1833 bool again, found;
56e93d26 1834
0827b9e9
AA
1835 /* No dirty page as there is zero RAM */
1836 if (!ram_bytes_total()) {
1837 return pages;
1838 }
1839
6f37bb8b 1840 pss.block = rs->last_seen_block;
a935e30f 1841 pss.page = rs->last_page;
b8fb8cb7
DDAG
1842 pss.complete_round = false;
1843
1844 if (!pss.block) {
1845 pss.block = QLIST_FIRST_RCU(&ram_list.blocks);
1846 }
56e93d26 1847
b9e60928 1848 do {
a82d593b 1849 again = true;
f20e2865 1850 found = get_queued_page(rs, &pss);
b9e60928 1851
a82d593b
DDAG
1852 if (!found) {
1853 /* priority queue empty, so just search for something dirty */
f20e2865 1854 found = find_dirty_block(rs, &pss, &again);
a82d593b 1855 }
f3f491fc 1856
a82d593b 1857 if (found) {
f20e2865 1858 pages = ram_save_host_page(rs, &pss, last_stage);
56e93d26 1859 }
b9e60928 1860 } while (!pages && again);
56e93d26 1861
6f37bb8b 1862 rs->last_seen_block = pss.block;
a935e30f 1863 rs->last_page = pss.page;
56e93d26
JQ
1864
1865 return pages;
1866}
1867
1868void acct_update_position(QEMUFile *f, size_t size, bool zero)
1869{
1870 uint64_t pages = size / TARGET_PAGE_SIZE;
f7ccd61b 1871
56e93d26 1872 if (zero) {
9360447d 1873 ram_counters.duplicate += pages;
56e93d26 1874 } else {
9360447d
JQ
1875 ram_counters.normal += pages;
1876 ram_counters.transferred += size;
56e93d26
JQ
1877 qemu_update_position(f, size);
1878 }
1879}
1880
56e93d26
JQ
1881uint64_t ram_bytes_total(void)
1882{
1883 RAMBlock *block;
1884 uint64_t total = 0;
1885
1886 rcu_read_lock();
99e15582 1887 RAMBLOCK_FOREACH(block) {
56e93d26 1888 total += block->used_length;
99e15582 1889 }
56e93d26
JQ
1890 rcu_read_unlock();
1891 return total;
1892}
1893
f265e0e4 1894static void xbzrle_load_setup(void)
56e93d26 1895{
f265e0e4 1896 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
56e93d26
JQ
1897}
1898
f265e0e4
JQ
1899static void xbzrle_load_cleanup(void)
1900{
1901 g_free(XBZRLE.decoded_buf);
1902 XBZRLE.decoded_buf = NULL;
1903}
1904
7d7c96be
PX
1905static void ram_state_cleanup(RAMState **rsp)
1906{
b9ccaf6d
DDAG
1907 if (*rsp) {
1908 migration_page_queue_free(*rsp);
1909 qemu_mutex_destroy(&(*rsp)->bitmap_mutex);
1910 qemu_mutex_destroy(&(*rsp)->src_page_req_mutex);
1911 g_free(*rsp);
1912 *rsp = NULL;
1913 }
7d7c96be
PX
1914}
1915
84593a08
PX
1916static void xbzrle_cleanup(void)
1917{
1918 XBZRLE_cache_lock();
1919 if (XBZRLE.cache) {
1920 cache_fini(XBZRLE.cache);
1921 g_free(XBZRLE.encoded_buf);
1922 g_free(XBZRLE.current_buf);
1923 g_free(XBZRLE.zero_target_page);
1924 XBZRLE.cache = NULL;
1925 XBZRLE.encoded_buf = NULL;
1926 XBZRLE.current_buf = NULL;
1927 XBZRLE.zero_target_page = NULL;
1928 }
1929 XBZRLE_cache_unlock();
1930}
1931
f265e0e4 1932static void ram_save_cleanup(void *opaque)
56e93d26 1933{
53518d94 1934 RAMState **rsp = opaque;
6b6712ef 1935 RAMBlock *block;
eb859c53 1936
2ff64038
LZ
1937 /* caller have hold iothread lock or is in a bh, so there is
1938 * no writing race against this migration_bitmap
1939 */
6b6712ef
JQ
1940 memory_global_dirty_log_stop();
1941
1942 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1943 g_free(block->bmap);
1944 block->bmap = NULL;
1945 g_free(block->unsentmap);
1946 block->unsentmap = NULL;
56e93d26
JQ
1947 }
1948
84593a08 1949 xbzrle_cleanup();
f0afa331 1950 compress_threads_save_cleanup();
7d7c96be 1951 ram_state_cleanup(rsp);
56e93d26
JQ
1952}
1953
6f37bb8b 1954static void ram_state_reset(RAMState *rs)
56e93d26 1955{
6f37bb8b
JQ
1956 rs->last_seen_block = NULL;
1957 rs->last_sent_block = NULL;
269ace29 1958 rs->last_page = 0;
6f37bb8b
JQ
1959 rs->last_version = ram_list.version;
1960 rs->ram_bulk_stage = true;
56e93d26
JQ
1961}
1962
1963#define MAX_WAIT 50 /* ms, half buffered_file limit */
1964
4f2e4252
DDAG
1965/*
1966 * 'expected' is the value you expect the bitmap mostly to be full
1967 * of; it won't bother printing lines that are all this value.
1968 * If 'todump' is null the migration bitmap is dumped.
1969 */
6b6712ef
JQ
1970void ram_debug_dump_bitmap(unsigned long *todump, bool expected,
1971 unsigned long pages)
4f2e4252 1972{
4f2e4252
DDAG
1973 int64_t cur;
1974 int64_t linelen = 128;
1975 char linebuf[129];
1976
6b6712ef 1977 for (cur = 0; cur < pages; cur += linelen) {
4f2e4252
DDAG
1978 int64_t curb;
1979 bool found = false;
1980 /*
1981 * Last line; catch the case where the line length
1982 * is longer than remaining ram
1983 */
6b6712ef
JQ
1984 if (cur + linelen > pages) {
1985 linelen = pages - cur;
4f2e4252
DDAG
1986 }
1987 for (curb = 0; curb < linelen; curb++) {
1988 bool thisbit = test_bit(cur + curb, todump);
1989 linebuf[curb] = thisbit ? '1' : '.';
1990 found = found || (thisbit != expected);
1991 }
1992 if (found) {
1993 linebuf[curb] = '\0';
1994 fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf);
1995 }
1996 }
1997}
1998
e0b266f0
DDAG
1999/* **** functions for postcopy ***** */
2000
ced1c616
PB
2001void ram_postcopy_migrated_memory_release(MigrationState *ms)
2002{
2003 struct RAMBlock *block;
ced1c616 2004
99e15582 2005 RAMBLOCK_FOREACH(block) {
6b6712ef
JQ
2006 unsigned long *bitmap = block->bmap;
2007 unsigned long range = block->used_length >> TARGET_PAGE_BITS;
2008 unsigned long run_start = find_next_zero_bit(bitmap, range, 0);
ced1c616
PB
2009
2010 while (run_start < range) {
2011 unsigned long run_end = find_next_bit(bitmap, range, run_start + 1);
aaa2064c 2012 ram_discard_range(block->idstr, run_start << TARGET_PAGE_BITS,
ced1c616
PB
2013 (run_end - run_start) << TARGET_PAGE_BITS);
2014 run_start = find_next_zero_bit(bitmap, range, run_end + 1);
2015 }
2016 }
2017}
2018
3d0684b2
JQ
2019/**
2020 * postcopy_send_discard_bm_ram: discard a RAMBlock
2021 *
2022 * Returns zero on success
2023 *
e0b266f0
DDAG
2024 * Callback from postcopy_each_ram_send_discard for each RAMBlock
2025 * Note: At this point the 'unsentmap' is the processed bitmap combined
2026 * with the dirtymap; so a '1' means it's either dirty or unsent.
3d0684b2
JQ
2027 *
2028 * @ms: current migration state
2029 * @pds: state for postcopy
2030 * @start: RAMBlock starting page
2031 * @length: RAMBlock size
e0b266f0
DDAG
2032 */
2033static int postcopy_send_discard_bm_ram(MigrationState *ms,
2034 PostcopyDiscardState *pds,
6b6712ef 2035 RAMBlock *block)
e0b266f0 2036{
6b6712ef 2037 unsigned long end = block->used_length >> TARGET_PAGE_BITS;
e0b266f0 2038 unsigned long current;
6b6712ef 2039 unsigned long *unsentmap = block->unsentmap;
e0b266f0 2040
6b6712ef 2041 for (current = 0; current < end; ) {
e0b266f0
DDAG
2042 unsigned long one = find_next_bit(unsentmap, end, current);
2043
2044 if (one <= end) {
2045 unsigned long zero = find_next_zero_bit(unsentmap, end, one + 1);
2046 unsigned long discard_length;
2047
2048 if (zero >= end) {
2049 discard_length = end - one;
2050 } else {
2051 discard_length = zero - one;
2052 }
d688c62d
DDAG
2053 if (discard_length) {
2054 postcopy_discard_send_range(ms, pds, one, discard_length);
2055 }
e0b266f0
DDAG
2056 current = one + discard_length;
2057 } else {
2058 current = one;
2059 }
2060 }
2061
2062 return 0;
2063}
2064
3d0684b2
JQ
2065/**
2066 * postcopy_each_ram_send_discard: discard all RAMBlocks
2067 *
2068 * Returns 0 for success or negative for error
2069 *
e0b266f0
DDAG
2070 * Utility for the outgoing postcopy code.
2071 * Calls postcopy_send_discard_bm_ram for each RAMBlock
2072 * passing it bitmap indexes and name.
e0b266f0
DDAG
2073 * (qemu_ram_foreach_block ends up passing unscaled lengths
2074 * which would mean postcopy code would have to deal with target page)
3d0684b2
JQ
2075 *
2076 * @ms: current migration state
e0b266f0
DDAG
2077 */
2078static int postcopy_each_ram_send_discard(MigrationState *ms)
2079{
2080 struct RAMBlock *block;
2081 int ret;
2082
99e15582 2083 RAMBLOCK_FOREACH(block) {
6b6712ef
JQ
2084 PostcopyDiscardState *pds =
2085 postcopy_discard_send_init(ms, block->idstr);
e0b266f0
DDAG
2086
2087 /*
2088 * Postcopy sends chunks of bitmap over the wire, but it
2089 * just needs indexes at this point, avoids it having
2090 * target page specific code.
2091 */
6b6712ef 2092 ret = postcopy_send_discard_bm_ram(ms, pds, block);
e0b266f0
DDAG
2093 postcopy_discard_send_finish(ms, pds);
2094 if (ret) {
2095 return ret;
2096 }
2097 }
2098
2099 return 0;
2100}
2101
3d0684b2
JQ
2102/**
2103 * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages
2104 *
2105 * Helper for postcopy_chunk_hostpages; it's called twice to
2106 * canonicalize the two bitmaps, that are similar, but one is
2107 * inverted.
99e314eb 2108 *
3d0684b2
JQ
2109 * Postcopy requires that all target pages in a hostpage are dirty or
2110 * clean, not a mix. This function canonicalizes the bitmaps.
99e314eb 2111 *
3d0684b2
JQ
2112 * @ms: current migration state
2113 * @unsent_pass: if true we need to canonicalize partially unsent host pages
2114 * otherwise we need to canonicalize partially dirty host pages
2115 * @block: block that contains the page we want to canonicalize
2116 * @pds: state for postcopy
99e314eb
DDAG
2117 */
2118static void postcopy_chunk_hostpages_pass(MigrationState *ms, bool unsent_pass,
2119 RAMBlock *block,
2120 PostcopyDiscardState *pds)
2121{
53518d94 2122 RAMState *rs = ram_state;
6b6712ef
JQ
2123 unsigned long *bitmap = block->bmap;
2124 unsigned long *unsentmap = block->unsentmap;
29c59172 2125 unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE;
6b6712ef 2126 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
99e314eb
DDAG
2127 unsigned long run_start;
2128
29c59172
DDAG
2129 if (block->page_size == TARGET_PAGE_SIZE) {
2130 /* Easy case - TPS==HPS for a non-huge page RAMBlock */
2131 return;
2132 }
2133
99e314eb
DDAG
2134 if (unsent_pass) {
2135 /* Find a sent page */
6b6712ef 2136 run_start = find_next_zero_bit(unsentmap, pages, 0);
99e314eb
DDAG
2137 } else {
2138 /* Find a dirty page */
6b6712ef 2139 run_start = find_next_bit(bitmap, pages, 0);
99e314eb
DDAG
2140 }
2141
6b6712ef 2142 while (run_start < pages) {
99e314eb
DDAG
2143 bool do_fixup = false;
2144 unsigned long fixup_start_addr;
2145 unsigned long host_offset;
2146
2147 /*
2148 * If the start of this run of pages is in the middle of a host
2149 * page, then we need to fixup this host page.
2150 */
2151 host_offset = run_start % host_ratio;
2152 if (host_offset) {
2153 do_fixup = true;
2154 run_start -= host_offset;
2155 fixup_start_addr = run_start;
2156 /* For the next pass */
2157 run_start = run_start + host_ratio;
2158 } else {
2159 /* Find the end of this run */
2160 unsigned long run_end;
2161 if (unsent_pass) {
6b6712ef 2162 run_end = find_next_bit(unsentmap, pages, run_start + 1);
99e314eb 2163 } else {
6b6712ef 2164 run_end = find_next_zero_bit(bitmap, pages, run_start + 1);
99e314eb
DDAG
2165 }
2166 /*
2167 * If the end isn't at the start of a host page, then the
2168 * run doesn't finish at the end of a host page
2169 * and we need to discard.
2170 */
2171 host_offset = run_end % host_ratio;
2172 if (host_offset) {
2173 do_fixup = true;
2174 fixup_start_addr = run_end - host_offset;
2175 /*
2176 * This host page has gone, the next loop iteration starts
2177 * from after the fixup
2178 */
2179 run_start = fixup_start_addr + host_ratio;
2180 } else {
2181 /*
2182 * No discards on this iteration, next loop starts from
2183 * next sent/dirty page
2184 */
2185 run_start = run_end + 1;
2186 }
2187 }
2188
2189 if (do_fixup) {
2190 unsigned long page;
2191
2192 /* Tell the destination to discard this page */
2193 if (unsent_pass || !test_bit(fixup_start_addr, unsentmap)) {
2194 /* For the unsent_pass we:
2195 * discard partially sent pages
2196 * For the !unsent_pass (dirty) we:
2197 * discard partially dirty pages that were sent
2198 * (any partially sent pages were already discarded
2199 * by the previous unsent_pass)
2200 */
2201 postcopy_discard_send_range(ms, pds, fixup_start_addr,
2202 host_ratio);
2203 }
2204
2205 /* Clean up the bitmap */
2206 for (page = fixup_start_addr;
2207 page < fixup_start_addr + host_ratio; page++) {
2208 /* All pages in this host page are now not sent */
2209 set_bit(page, unsentmap);
2210
2211 /*
2212 * Remark them as dirty, updating the count for any pages
2213 * that weren't previously dirty.
2214 */
0d8ec885 2215 rs->migration_dirty_pages += !test_and_set_bit(page, bitmap);
99e314eb
DDAG
2216 }
2217 }
2218
2219 if (unsent_pass) {
2220 /* Find the next sent page for the next iteration */
6b6712ef 2221 run_start = find_next_zero_bit(unsentmap, pages, run_start);
99e314eb
DDAG
2222 } else {
2223 /* Find the next dirty page for the next iteration */
6b6712ef 2224 run_start = find_next_bit(bitmap, pages, run_start);
99e314eb
DDAG
2225 }
2226 }
2227}
2228
3d0684b2
JQ
2229/**
2230 * postcopy_chuck_hostpages: discrad any partially sent host page
2231 *
99e314eb
DDAG
2232 * Utility for the outgoing postcopy code.
2233 *
2234 * Discard any partially sent host-page size chunks, mark any partially
29c59172
DDAG
2235 * dirty host-page size chunks as all dirty. In this case the host-page
2236 * is the host-page for the particular RAMBlock, i.e. it might be a huge page
99e314eb 2237 *
3d0684b2
JQ
2238 * Returns zero on success
2239 *
2240 * @ms: current migration state
6b6712ef 2241 * @block: block we want to work with
99e314eb 2242 */
6b6712ef 2243static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block)
99e314eb 2244{
6b6712ef
JQ
2245 PostcopyDiscardState *pds =
2246 postcopy_discard_send_init(ms, block->idstr);
99e314eb 2247
6b6712ef
JQ
2248 /* First pass: Discard all partially sent host pages */
2249 postcopy_chunk_hostpages_pass(ms, true, block, pds);
2250 /*
2251 * Second pass: Ensure that all partially dirty host pages are made
2252 * fully dirty.
2253 */
2254 postcopy_chunk_hostpages_pass(ms, false, block, pds);
99e314eb 2255
6b6712ef 2256 postcopy_discard_send_finish(ms, pds);
99e314eb
DDAG
2257 return 0;
2258}
2259
3d0684b2
JQ
2260/**
2261 * ram_postcopy_send_discard_bitmap: transmit the discard bitmap
2262 *
2263 * Returns zero on success
2264 *
e0b266f0
DDAG
2265 * Transmit the set of pages to be discarded after precopy to the target
2266 * these are pages that:
2267 * a) Have been previously transmitted but are now dirty again
2268 * b) Pages that have never been transmitted, this ensures that
2269 * any pages on the destination that have been mapped by background
2270 * tasks get discarded (transparent huge pages is the specific concern)
2271 * Hopefully this is pretty sparse
3d0684b2
JQ
2272 *
2273 * @ms: current migration state
e0b266f0
DDAG
2274 */
2275int ram_postcopy_send_discard_bitmap(MigrationState *ms)
2276{
53518d94 2277 RAMState *rs = ram_state;
6b6712ef 2278 RAMBlock *block;
e0b266f0 2279 int ret;
e0b266f0
DDAG
2280
2281 rcu_read_lock();
2282
2283 /* This should be our last sync, the src is now paused */
eb859c53 2284 migration_bitmap_sync(rs);
e0b266f0 2285
6b6712ef
JQ
2286 /* Easiest way to make sure we don't resume in the middle of a host-page */
2287 rs->last_seen_block = NULL;
2288 rs->last_sent_block = NULL;
2289 rs->last_page = 0;
e0b266f0 2290
6b6712ef
JQ
2291 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
2292 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
2293 unsigned long *bitmap = block->bmap;
2294 unsigned long *unsentmap = block->unsentmap;
2295
2296 if (!unsentmap) {
2297 /* We don't have a safe way to resize the sentmap, so
2298 * if the bitmap was resized it will be NULL at this
2299 * point.
2300 */
2301 error_report("migration ram resized during precopy phase");
2302 rcu_read_unlock();
2303 return -EINVAL;
2304 }
2305 /* Deal with TPS != HPS and huge pages */
2306 ret = postcopy_chunk_hostpages(ms, block);
2307 if (ret) {
2308 rcu_read_unlock();
2309 return ret;
2310 }
e0b266f0 2311
6b6712ef
JQ
2312 /*
2313 * Update the unsentmap to be unsentmap = unsentmap | dirty
2314 */
2315 bitmap_or(unsentmap, unsentmap, bitmap, pages);
e0b266f0 2316#ifdef DEBUG_POSTCOPY
6b6712ef 2317 ram_debug_dump_bitmap(unsentmap, true, pages);
e0b266f0 2318#endif
6b6712ef
JQ
2319 }
2320 trace_ram_postcopy_send_discard_bitmap();
e0b266f0
DDAG
2321
2322 ret = postcopy_each_ram_send_discard(ms);
2323 rcu_read_unlock();
2324
2325 return ret;
2326}
2327
3d0684b2
JQ
2328/**
2329 * ram_discard_range: discard dirtied pages at the beginning of postcopy
e0b266f0 2330 *
3d0684b2 2331 * Returns zero on success
e0b266f0 2332 *
36449157
JQ
2333 * @rbname: name of the RAMBlock of the request. NULL means the
2334 * same that last one.
3d0684b2
JQ
2335 * @start: RAMBlock starting page
2336 * @length: RAMBlock size
e0b266f0 2337 */
aaa2064c 2338int ram_discard_range(const char *rbname, uint64_t start, size_t length)
e0b266f0
DDAG
2339{
2340 int ret = -1;
2341
36449157 2342 trace_ram_discard_range(rbname, start, length);
d3a5038c 2343
e0b266f0 2344 rcu_read_lock();
36449157 2345 RAMBlock *rb = qemu_ram_block_by_name(rbname);
e0b266f0
DDAG
2346
2347 if (!rb) {
36449157 2348 error_report("ram_discard_range: Failed to find block '%s'", rbname);
e0b266f0
DDAG
2349 goto err;
2350 }
2351
f9494614
AP
2352 bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(),
2353 length >> qemu_target_page_bits());
d3a5038c 2354 ret = ram_block_discard_range(rb, start, length);
e0b266f0
DDAG
2355
2356err:
2357 rcu_read_unlock();
2358
2359 return ret;
2360}
2361
84593a08
PX
2362/*
2363 * For every allocation, we will try not to crash the VM if the
2364 * allocation failed.
2365 */
2366static int xbzrle_init(void)
2367{
2368 Error *local_err = NULL;
2369
2370 if (!migrate_use_xbzrle()) {
2371 return 0;
2372 }
2373
2374 XBZRLE_cache_lock();
2375
2376 XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE);
2377 if (!XBZRLE.zero_target_page) {
2378 error_report("%s: Error allocating zero page", __func__);
2379 goto err_out;
2380 }
2381
2382 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(),
2383 TARGET_PAGE_SIZE, &local_err);
2384 if (!XBZRLE.cache) {
2385 error_report_err(local_err);
2386 goto free_zero_page;
2387 }
2388
2389 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
2390 if (!XBZRLE.encoded_buf) {
2391 error_report("%s: Error allocating encoded_buf", __func__);
2392 goto free_cache;
2393 }
2394
2395 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
2396 if (!XBZRLE.current_buf) {
2397 error_report("%s: Error allocating current_buf", __func__);
2398 goto free_encoded_buf;
2399 }
2400
2401 /* We are all good */
2402 XBZRLE_cache_unlock();
2403 return 0;
2404
2405free_encoded_buf:
2406 g_free(XBZRLE.encoded_buf);
2407 XBZRLE.encoded_buf = NULL;
2408free_cache:
2409 cache_fini(XBZRLE.cache);
2410 XBZRLE.cache = NULL;
2411free_zero_page:
2412 g_free(XBZRLE.zero_target_page);
2413 XBZRLE.zero_target_page = NULL;
2414err_out:
2415 XBZRLE_cache_unlock();
2416 return -ENOMEM;
2417}
2418
53518d94 2419static int ram_state_init(RAMState **rsp)
56e93d26 2420{
7d00ee6a
PX
2421 *rsp = g_try_new0(RAMState, 1);
2422
2423 if (!*rsp) {
2424 error_report("%s: Init ramstate fail", __func__);
2425 return -1;
2426 }
53518d94
JQ
2427
2428 qemu_mutex_init(&(*rsp)->bitmap_mutex);
2429 qemu_mutex_init(&(*rsp)->src_page_req_mutex);
2430 QSIMPLEQ_INIT(&(*rsp)->src_page_requests);
56e93d26 2431
7d00ee6a
PX
2432 /*
2433 * Count the total number of pages used by ram blocks not including any
2434 * gaps due to alignment or unplugs.
2435 */
2436 (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
2437
2438 ram_state_reset(*rsp);
2439
2440 return 0;
2441}
2442
d6eff5d7 2443static void ram_list_init_bitmaps(void)
7d00ee6a 2444{
d6eff5d7
PX
2445 RAMBlock *block;
2446 unsigned long pages;
56e93d26 2447
0827b9e9
AA
2448 /* Skip setting bitmap if there is no RAM */
2449 if (ram_bytes_total()) {
6b6712ef 2450 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
d6eff5d7 2451 pages = block->max_length >> TARGET_PAGE_BITS;
6b6712ef
JQ
2452 block->bmap = bitmap_new(pages);
2453 bitmap_set(block->bmap, 0, pages);
2454 if (migrate_postcopy_ram()) {
2455 block->unsentmap = bitmap_new(pages);
2456 bitmap_set(block->unsentmap, 0, pages);
2457 }
0827b9e9 2458 }
f3f491fc 2459 }
d6eff5d7
PX
2460}
2461
2462static void ram_init_bitmaps(RAMState *rs)
2463{
2464 /* For memory_global_dirty_log_start below. */
2465 qemu_mutex_lock_iothread();
2466 qemu_mutex_lock_ramlist();
2467 rcu_read_lock();
f3f491fc 2468
d6eff5d7 2469 ram_list_init_bitmaps();
56e93d26 2470 memory_global_dirty_log_start();
d6eff5d7
PX
2471 migration_bitmap_sync(rs);
2472
2473 rcu_read_unlock();
56e93d26 2474 qemu_mutex_unlock_ramlist();
49877834 2475 qemu_mutex_unlock_iothread();
d6eff5d7
PX
2476}
2477
2478static int ram_init_all(RAMState **rsp)
2479{
2480 if (ram_state_init(rsp)) {
2481 return -1;
2482 }
2483
2484 if (xbzrle_init()) {
2485 ram_state_cleanup(rsp);
2486 return -1;
2487 }
2488
2489 ram_init_bitmaps(*rsp);
a91246c9
HZ
2490
2491 return 0;
2492}
2493
08614f34
PX
2494static void ram_state_resume_prepare(RAMState *rs, QEMUFile *out)
2495{
2496 RAMBlock *block;
2497 uint64_t pages = 0;
2498
2499 /*
2500 * Postcopy is not using xbzrle/compression, so no need for that.
2501 * Also, since source are already halted, we don't need to care
2502 * about dirty page logging as well.
2503 */
2504
2505 RAMBLOCK_FOREACH(block) {
2506 pages += bitmap_count_one(block->bmap,
2507 block->used_length >> TARGET_PAGE_BITS);
2508 }
2509
2510 /* This may not be aligned with current bitmaps. Recalculate. */
2511 rs->migration_dirty_pages = pages;
2512
2513 rs->last_seen_block = NULL;
2514 rs->last_sent_block = NULL;
2515 rs->last_page = 0;
2516 rs->last_version = ram_list.version;
2517 /*
2518 * Disable the bulk stage, otherwise we'll resend the whole RAM no
2519 * matter what we have sent.
2520 */
2521 rs->ram_bulk_stage = false;
2522
2523 /* Update RAMState cache of output QEMUFile */
2524 rs->f = out;
2525
2526 trace_ram_state_resume_prepare(pages);
2527}
2528
3d0684b2
JQ
2529/*
2530 * Each of ram_save_setup, ram_save_iterate and ram_save_complete has
a91246c9
HZ
2531 * long-running RCU critical section. When rcu-reclaims in the code
2532 * start to become numerous it will be necessary to reduce the
2533 * granularity of these critical sections.
2534 */
2535
3d0684b2
JQ
2536/**
2537 * ram_save_setup: Setup RAM for migration
2538 *
2539 * Returns zero to indicate success and negative for error
2540 *
2541 * @f: QEMUFile where to send the data
2542 * @opaque: RAMState pointer
2543 */
a91246c9
HZ
2544static int ram_save_setup(QEMUFile *f, void *opaque)
2545{
53518d94 2546 RAMState **rsp = opaque;
a91246c9
HZ
2547 RAMBlock *block;
2548
dcaf446e
XG
2549 if (compress_threads_save_setup()) {
2550 return -1;
2551 }
2552
a91246c9
HZ
2553 /* migration has already setup the bitmap, reuse it. */
2554 if (!migration_in_colo_state()) {
7d00ee6a 2555 if (ram_init_all(rsp) != 0) {
dcaf446e 2556 compress_threads_save_cleanup();
a91246c9 2557 return -1;
53518d94 2558 }
a91246c9 2559 }
53518d94 2560 (*rsp)->f = f;
a91246c9
HZ
2561
2562 rcu_read_lock();
56e93d26
JQ
2563
2564 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
2565
99e15582 2566 RAMBLOCK_FOREACH(block) {
56e93d26
JQ
2567 qemu_put_byte(f, strlen(block->idstr));
2568 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
2569 qemu_put_be64(f, block->used_length);
ef08fb38
DDAG
2570 if (migrate_postcopy_ram() && block->page_size != qemu_host_page_size) {
2571 qemu_put_be64(f, block->page_size);
2572 }
56e93d26
JQ
2573 }
2574
2575 rcu_read_unlock();
2576
2577 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
2578 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
2579
2580 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2581
2582 return 0;
2583}
2584
3d0684b2
JQ
2585/**
2586 * ram_save_iterate: iterative stage for migration
2587 *
2588 * Returns zero to indicate success and negative for error
2589 *
2590 * @f: QEMUFile where to send the data
2591 * @opaque: RAMState pointer
2592 */
56e93d26
JQ
2593static int ram_save_iterate(QEMUFile *f, void *opaque)
2594{
53518d94
JQ
2595 RAMState **temp = opaque;
2596 RAMState *rs = *temp;
56e93d26
JQ
2597 int ret;
2598 int i;
2599 int64_t t0;
5c90308f 2600 int done = 0;
56e93d26 2601
b2557345
PL
2602 if (blk_mig_bulk_active()) {
2603 /* Avoid transferring ram during bulk phase of block migration as
2604 * the bulk phase will usually take a long time and transferring
2605 * ram updates during that time is pointless. */
2606 goto out;
2607 }
2608
56e93d26 2609 rcu_read_lock();
6f37bb8b
JQ
2610 if (ram_list.version != rs->last_version) {
2611 ram_state_reset(rs);
56e93d26
JQ
2612 }
2613
2614 /* Read version before ram_list.blocks */
2615 smp_rmb();
2616
2617 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
2618
2619 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2620 i = 0;
2621 while ((ret = qemu_file_rate_limit(f)) == 0) {
2622 int pages;
2623
ce25d337 2624 pages = ram_find_and_save_block(rs, false);
56e93d26
JQ
2625 /* no more pages to sent */
2626 if (pages == 0) {
5c90308f 2627 done = 1;
56e93d26
JQ
2628 break;
2629 }
23b28c3c 2630 rs->iterations++;
070afca2 2631
56e93d26
JQ
2632 /* we want to check in the 1st loop, just in case it was the 1st time
2633 and we had to sync the dirty bitmap.
2634 qemu_get_clock_ns() is a bit expensive, so we only check each some
2635 iterations
2636 */
2637 if ((i & 63) == 0) {
2638 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
2639 if (t1 > MAX_WAIT) {
55c4446b 2640 trace_ram_save_iterate_big_wait(t1, i);
56e93d26
JQ
2641 break;
2642 }
2643 }
2644 i++;
2645 }
ce25d337 2646 flush_compressed_data(rs);
56e93d26
JQ
2647 rcu_read_unlock();
2648
2649 /*
2650 * Must occur before EOS (or any QEMUFile operation)
2651 * because of RDMA protocol.
2652 */
2653 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
2654
b2557345 2655out:
56e93d26 2656 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
9360447d 2657 ram_counters.transferred += 8;
56e93d26
JQ
2658
2659 ret = qemu_file_get_error(f);
2660 if (ret < 0) {
2661 return ret;
2662 }
2663
5c90308f 2664 return done;
56e93d26
JQ
2665}
2666
3d0684b2
JQ
2667/**
2668 * ram_save_complete: function called to send the remaining amount of ram
2669 *
2670 * Returns zero to indicate success
2671 *
2672 * Called with iothread lock
2673 *
2674 * @f: QEMUFile where to send the data
2675 * @opaque: RAMState pointer
2676 */
56e93d26
JQ
2677static int ram_save_complete(QEMUFile *f, void *opaque)
2678{
53518d94
JQ
2679 RAMState **temp = opaque;
2680 RAMState *rs = *temp;
6f37bb8b 2681
56e93d26
JQ
2682 rcu_read_lock();
2683
5727309d 2684 if (!migration_in_postcopy()) {
8d820d6f 2685 migration_bitmap_sync(rs);
663e6c1d 2686 }
56e93d26
JQ
2687
2688 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
2689
2690 /* try transferring iterative blocks of memory */
2691
2692 /* flush all remaining blocks regardless of rate limiting */
2693 while (true) {
2694 int pages;
2695
ce25d337 2696 pages = ram_find_and_save_block(rs, !migration_in_colo_state());
56e93d26
JQ
2697 /* no more blocks to sent */
2698 if (pages == 0) {
2699 break;
2700 }
2701 }
2702
ce25d337 2703 flush_compressed_data(rs);
56e93d26 2704 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
56e93d26
JQ
2705
2706 rcu_read_unlock();
d09a6fde 2707
56e93d26
JQ
2708 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2709
2710 return 0;
2711}
2712
c31b098f 2713static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
47995026
VSO
2714 uint64_t *res_precopy_only,
2715 uint64_t *res_compatible,
2716 uint64_t *res_postcopy_only)
56e93d26 2717{
53518d94
JQ
2718 RAMState **temp = opaque;
2719 RAMState *rs = *temp;
56e93d26
JQ
2720 uint64_t remaining_size;
2721
9edabd4d 2722 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
56e93d26 2723
5727309d 2724 if (!migration_in_postcopy() &&
663e6c1d 2725 remaining_size < max_size) {
56e93d26
JQ
2726 qemu_mutex_lock_iothread();
2727 rcu_read_lock();
8d820d6f 2728 migration_bitmap_sync(rs);
56e93d26
JQ
2729 rcu_read_unlock();
2730 qemu_mutex_unlock_iothread();
9edabd4d 2731 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
56e93d26 2732 }
c31b098f 2733
86e1167e
VSO
2734 if (migrate_postcopy_ram()) {
2735 /* We can do postcopy, and all the data is postcopiable */
47995026 2736 *res_compatible += remaining_size;
86e1167e 2737 } else {
47995026 2738 *res_precopy_only += remaining_size;
86e1167e 2739 }
56e93d26
JQ
2740}
2741
2742static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
2743{
2744 unsigned int xh_len;
2745 int xh_flags;
063e760a 2746 uint8_t *loaded_data;
56e93d26 2747
56e93d26
JQ
2748 /* extract RLE header */
2749 xh_flags = qemu_get_byte(f);
2750 xh_len = qemu_get_be16(f);
2751
2752 if (xh_flags != ENCODING_FLAG_XBZRLE) {
2753 error_report("Failed to load XBZRLE page - wrong compression!");
2754 return -1;
2755 }
2756
2757 if (xh_len > TARGET_PAGE_SIZE) {
2758 error_report("Failed to load XBZRLE page - len overflow!");
2759 return -1;
2760 }
f265e0e4 2761 loaded_data = XBZRLE.decoded_buf;
56e93d26 2762 /* load data and decode */
f265e0e4 2763 /* it can change loaded_data to point to an internal buffer */
063e760a 2764 qemu_get_buffer_in_place(f, &loaded_data, xh_len);
56e93d26
JQ
2765
2766 /* decode RLE */
063e760a 2767 if (xbzrle_decode_buffer(loaded_data, xh_len, host,
56e93d26
JQ
2768 TARGET_PAGE_SIZE) == -1) {
2769 error_report("Failed to load XBZRLE page - decode error!");
2770 return -1;
2771 }
2772
2773 return 0;
2774}
2775
3d0684b2
JQ
2776/**
2777 * ram_block_from_stream: read a RAMBlock id from the migration stream
2778 *
2779 * Must be called from within a rcu critical section.
2780 *
56e93d26 2781 * Returns a pointer from within the RCU-protected ram_list.
a7180877 2782 *
3d0684b2
JQ
2783 * @f: QEMUFile where to read the data from
2784 * @flags: Page flags (mostly to see if it's a continuation of previous block)
a7180877 2785 */
3d0684b2 2786static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags)
56e93d26
JQ
2787{
2788 static RAMBlock *block = NULL;
2789 char id[256];
2790 uint8_t len;
2791
2792 if (flags & RAM_SAVE_FLAG_CONTINUE) {
4c4bad48 2793 if (!block) {
56e93d26
JQ
2794 error_report("Ack, bad migration stream!");
2795 return NULL;
2796 }
4c4bad48 2797 return block;
56e93d26
JQ
2798 }
2799
2800 len = qemu_get_byte(f);
2801 qemu_get_buffer(f, (uint8_t *)id, len);
2802 id[len] = 0;
2803
e3dd7493 2804 block = qemu_ram_block_by_name(id);
4c4bad48
HZ
2805 if (!block) {
2806 error_report("Can't find block %s", id);
2807 return NULL;
56e93d26
JQ
2808 }
2809
4c4bad48
HZ
2810 return block;
2811}
2812
2813static inline void *host_from_ram_block_offset(RAMBlock *block,
2814 ram_addr_t offset)
2815{
2816 if (!offset_in_ramblock(block, offset)) {
2817 return NULL;
2818 }
2819
2820 return block->host + offset;
56e93d26
JQ
2821}
2822
3d0684b2
JQ
2823/**
2824 * ram_handle_compressed: handle the zero page case
2825 *
56e93d26
JQ
2826 * If a page (or a whole RDMA chunk) has been
2827 * determined to be zero, then zap it.
3d0684b2
JQ
2828 *
2829 * @host: host address for the zero page
2830 * @ch: what the page is filled from. We only support zero
2831 * @size: size of the zero page
56e93d26
JQ
2832 */
2833void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
2834{
2835 if (ch != 0 || !is_zero_range(host, size)) {
2836 memset(host, ch, size);
2837 }
2838}
2839
797ca154
XG
2840/* return the size after decompression, or negative value on error */
2841static int
2842qemu_uncompress_data(z_stream *stream, uint8_t *dest, size_t dest_len,
2843 const uint8_t *source, size_t source_len)
2844{
2845 int err;
2846
2847 err = inflateReset(stream);
2848 if (err != Z_OK) {
2849 return -1;
2850 }
2851
2852 stream->avail_in = source_len;
2853 stream->next_in = (uint8_t *)source;
2854 stream->avail_out = dest_len;
2855 stream->next_out = dest;
2856
2857 err = inflate(stream, Z_NO_FLUSH);
2858 if (err != Z_STREAM_END) {
2859 return -1;
2860 }
2861
2862 return stream->total_out;
2863}
2864
56e93d26
JQ
2865static void *do_data_decompress(void *opaque)
2866{
2867 DecompressParam *param = opaque;
2868 unsigned long pagesize;
33d151f4 2869 uint8_t *des;
34ab9e97 2870 int len, ret;
56e93d26 2871
33d151f4 2872 qemu_mutex_lock(&param->mutex);
90e56fb4 2873 while (!param->quit) {
33d151f4
LL
2874 if (param->des) {
2875 des = param->des;
2876 len = param->len;
2877 param->des = 0;
2878 qemu_mutex_unlock(&param->mutex);
2879
56e93d26 2880 pagesize = TARGET_PAGE_SIZE;
34ab9e97
XG
2881
2882 ret = qemu_uncompress_data(&param->stream, des, pagesize,
2883 param->compbuf, len);
f548222c 2884 if (ret < 0 && migrate_get_current()->decompress_error_check) {
34ab9e97
XG
2885 error_report("decompress data failed");
2886 qemu_file_set_error(decomp_file, ret);
2887 }
73a8912b 2888
33d151f4
LL
2889 qemu_mutex_lock(&decomp_done_lock);
2890 param->done = true;
2891 qemu_cond_signal(&decomp_done_cond);
2892 qemu_mutex_unlock(&decomp_done_lock);
2893
2894 qemu_mutex_lock(&param->mutex);
2895 } else {
2896 qemu_cond_wait(&param->cond, &param->mutex);
2897 }
56e93d26 2898 }
33d151f4 2899 qemu_mutex_unlock(&param->mutex);
56e93d26
JQ
2900
2901 return NULL;
2902}
2903
34ab9e97 2904static int wait_for_decompress_done(void)
5533b2e9
LL
2905{
2906 int idx, thread_count;
2907
2908 if (!migrate_use_compression()) {
34ab9e97 2909 return 0;
5533b2e9
LL
2910 }
2911
2912 thread_count = migrate_decompress_threads();
2913 qemu_mutex_lock(&decomp_done_lock);
2914 for (idx = 0; idx < thread_count; idx++) {
2915 while (!decomp_param[idx].done) {
2916 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
2917 }
2918 }
2919 qemu_mutex_unlock(&decomp_done_lock);
34ab9e97 2920 return qemu_file_get_error(decomp_file);
5533b2e9
LL
2921}
2922
f0afa331 2923static void compress_threads_load_cleanup(void)
56e93d26
JQ
2924{
2925 int i, thread_count;
2926
3416ab5b
JQ
2927 if (!migrate_use_compression()) {
2928 return;
2929 }
56e93d26
JQ
2930 thread_count = migrate_decompress_threads();
2931 for (i = 0; i < thread_count; i++) {
797ca154
XG
2932 /*
2933 * we use it as a indicator which shows if the thread is
2934 * properly init'd or not
2935 */
2936 if (!decomp_param[i].compbuf) {
2937 break;
2938 }
2939
56e93d26 2940 qemu_mutex_lock(&decomp_param[i].mutex);
90e56fb4 2941 decomp_param[i].quit = true;
56e93d26
JQ
2942 qemu_cond_signal(&decomp_param[i].cond);
2943 qemu_mutex_unlock(&decomp_param[i].mutex);
2944 }
2945 for (i = 0; i < thread_count; i++) {
797ca154
XG
2946 if (!decomp_param[i].compbuf) {
2947 break;
2948 }
2949
56e93d26
JQ
2950 qemu_thread_join(decompress_threads + i);
2951 qemu_mutex_destroy(&decomp_param[i].mutex);
2952 qemu_cond_destroy(&decomp_param[i].cond);
797ca154 2953 inflateEnd(&decomp_param[i].stream);
56e93d26 2954 g_free(decomp_param[i].compbuf);
797ca154 2955 decomp_param[i].compbuf = NULL;
56e93d26
JQ
2956 }
2957 g_free(decompress_threads);
2958 g_free(decomp_param);
56e93d26
JQ
2959 decompress_threads = NULL;
2960 decomp_param = NULL;
34ab9e97 2961 decomp_file = NULL;
56e93d26
JQ
2962}
2963
34ab9e97 2964static int compress_threads_load_setup(QEMUFile *f)
797ca154
XG
2965{
2966 int i, thread_count;
2967
2968 if (!migrate_use_compression()) {
2969 return 0;
2970 }
2971
2972 thread_count = migrate_decompress_threads();
2973 decompress_threads = g_new0(QemuThread, thread_count);
2974 decomp_param = g_new0(DecompressParam, thread_count);
2975 qemu_mutex_init(&decomp_done_lock);
2976 qemu_cond_init(&decomp_done_cond);
34ab9e97 2977 decomp_file = f;
797ca154
XG
2978 for (i = 0; i < thread_count; i++) {
2979 if (inflateInit(&decomp_param[i].stream) != Z_OK) {
2980 goto exit;
2981 }
2982
2983 decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
2984 qemu_mutex_init(&decomp_param[i].mutex);
2985 qemu_cond_init(&decomp_param[i].cond);
2986 decomp_param[i].done = true;
2987 decomp_param[i].quit = false;
2988 qemu_thread_create(decompress_threads + i, "decompress",
2989 do_data_decompress, decomp_param + i,
2990 QEMU_THREAD_JOINABLE);
2991 }
2992 return 0;
2993exit:
2994 compress_threads_load_cleanup();
2995 return -1;
2996}
2997
c1bc6626 2998static void decompress_data_with_multi_threads(QEMUFile *f,
56e93d26
JQ
2999 void *host, int len)
3000{
3001 int idx, thread_count;
3002
3003 thread_count = migrate_decompress_threads();
73a8912b 3004 qemu_mutex_lock(&decomp_done_lock);
56e93d26
JQ
3005 while (true) {
3006 for (idx = 0; idx < thread_count; idx++) {
73a8912b 3007 if (decomp_param[idx].done) {
33d151f4
LL
3008 decomp_param[idx].done = false;
3009 qemu_mutex_lock(&decomp_param[idx].mutex);
c1bc6626 3010 qemu_get_buffer(f, decomp_param[idx].compbuf, len);
56e93d26
JQ
3011 decomp_param[idx].des = host;
3012 decomp_param[idx].len = len;
33d151f4
LL
3013 qemu_cond_signal(&decomp_param[idx].cond);
3014 qemu_mutex_unlock(&decomp_param[idx].mutex);
56e93d26
JQ
3015 break;
3016 }
3017 }
3018 if (idx < thread_count) {
3019 break;
73a8912b
LL
3020 } else {
3021 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
56e93d26
JQ
3022 }
3023 }
73a8912b 3024 qemu_mutex_unlock(&decomp_done_lock);
56e93d26
JQ
3025}
3026
f265e0e4
JQ
3027/**
3028 * ram_load_setup: Setup RAM for migration incoming side
3029 *
3030 * Returns zero to indicate success and negative for error
3031 *
3032 * @f: QEMUFile where to receive the data
3033 * @opaque: RAMState pointer
3034 */
3035static int ram_load_setup(QEMUFile *f, void *opaque)
3036{
34ab9e97 3037 if (compress_threads_load_setup(f)) {
797ca154
XG
3038 return -1;
3039 }
3040
f265e0e4 3041 xbzrle_load_setup();
f9494614 3042 ramblock_recv_map_init();
f265e0e4
JQ
3043 return 0;
3044}
3045
3046static int ram_load_cleanup(void *opaque)
3047{
f9494614 3048 RAMBlock *rb;
f265e0e4 3049 xbzrle_load_cleanup();
f0afa331 3050 compress_threads_load_cleanup();
f9494614
AP
3051
3052 RAMBLOCK_FOREACH(rb) {
3053 g_free(rb->receivedmap);
3054 rb->receivedmap = NULL;
3055 }
f265e0e4
JQ
3056 return 0;
3057}
3058
3d0684b2
JQ
3059/**
3060 * ram_postcopy_incoming_init: allocate postcopy data structures
3061 *
3062 * Returns 0 for success and negative if there was one error
3063 *
3064 * @mis: current migration incoming state
3065 *
3066 * Allocate data structures etc needed by incoming migration with
3067 * postcopy-ram. postcopy-ram's similarly names
3068 * postcopy_ram_incoming_init does the work.
1caddf8a
DDAG
3069 */
3070int ram_postcopy_incoming_init(MigrationIncomingState *mis)
3071{
b8c48993 3072 unsigned long ram_pages = last_ram_page();
1caddf8a
DDAG
3073
3074 return postcopy_ram_incoming_init(mis, ram_pages);
3075}
3076
3d0684b2
JQ
3077/**
3078 * ram_load_postcopy: load a page in postcopy case
3079 *
3080 * Returns 0 for success or -errno in case of error
3081 *
a7180877
DDAG
3082 * Called in postcopy mode by ram_load().
3083 * rcu_read_lock is taken prior to this being called.
3d0684b2
JQ
3084 *
3085 * @f: QEMUFile where to send the data
a7180877
DDAG
3086 */
3087static int ram_load_postcopy(QEMUFile *f)
3088{
3089 int flags = 0, ret = 0;
3090 bool place_needed = false;
28abd200 3091 bool matching_page_sizes = false;
a7180877
DDAG
3092 MigrationIncomingState *mis = migration_incoming_get_current();
3093 /* Temporary page that is later 'placed' */
3094 void *postcopy_host_page = postcopy_get_tmp_page(mis);
c53b7ddc 3095 void *last_host = NULL;
a3b6ff6d 3096 bool all_zero = false;
a7180877
DDAG
3097
3098 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
3099 ram_addr_t addr;
3100 void *host = NULL;
3101 void *page_buffer = NULL;
3102 void *place_source = NULL;
df9ff5e1 3103 RAMBlock *block = NULL;
a7180877 3104 uint8_t ch;
a7180877
DDAG
3105
3106 addr = qemu_get_be64(f);
7a9ddfbf
PX
3107
3108 /*
3109 * If qemu file error, we should stop here, and then "addr"
3110 * may be invalid
3111 */
3112 ret = qemu_file_get_error(f);
3113 if (ret) {
3114 break;
3115 }
3116
a7180877
DDAG
3117 flags = addr & ~TARGET_PAGE_MASK;
3118 addr &= TARGET_PAGE_MASK;
3119
3120 trace_ram_load_postcopy_loop((uint64_t)addr, flags);
3121 place_needed = false;
bb890ed5 3122 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE)) {
df9ff5e1 3123 block = ram_block_from_stream(f, flags);
4c4bad48
HZ
3124
3125 host = host_from_ram_block_offset(block, addr);
a7180877
DDAG
3126 if (!host) {
3127 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
3128 ret = -EINVAL;
3129 break;
3130 }
28abd200 3131 matching_page_sizes = block->page_size == TARGET_PAGE_SIZE;
a7180877 3132 /*
28abd200
DDAG
3133 * Postcopy requires that we place whole host pages atomically;
3134 * these may be huge pages for RAMBlocks that are backed by
3135 * hugetlbfs.
a7180877
DDAG
3136 * To make it atomic, the data is read into a temporary page
3137 * that's moved into place later.
3138 * The migration protocol uses, possibly smaller, target-pages
3139 * however the source ensures it always sends all the components
3140 * of a host page in order.
3141 */
3142 page_buffer = postcopy_host_page +
28abd200 3143 ((uintptr_t)host & (block->page_size - 1));
a7180877 3144 /* If all TP are zero then we can optimise the place */
28abd200 3145 if (!((uintptr_t)host & (block->page_size - 1))) {
a7180877 3146 all_zero = true;
c53b7ddc
DDAG
3147 } else {
3148 /* not the 1st TP within the HP */
3149 if (host != (last_host + TARGET_PAGE_SIZE)) {
9af9e0fe 3150 error_report("Non-sequential target page %p/%p",
c53b7ddc
DDAG
3151 host, last_host);
3152 ret = -EINVAL;
3153 break;
3154 }
a7180877
DDAG
3155 }
3156
c53b7ddc 3157
a7180877
DDAG
3158 /*
3159 * If it's the last part of a host page then we place the host
3160 * page
3161 */
3162 place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) &
28abd200 3163 (block->page_size - 1)) == 0;
a7180877
DDAG
3164 place_source = postcopy_host_page;
3165 }
c53b7ddc 3166 last_host = host;
a7180877
DDAG
3167
3168 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
bb890ed5 3169 case RAM_SAVE_FLAG_ZERO:
a7180877
DDAG
3170 ch = qemu_get_byte(f);
3171 memset(page_buffer, ch, TARGET_PAGE_SIZE);
3172 if (ch) {
3173 all_zero = false;
3174 }
3175 break;
3176
3177 case RAM_SAVE_FLAG_PAGE:
3178 all_zero = false;
3179 if (!place_needed || !matching_page_sizes) {
3180 qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE);
3181 } else {
3182 /* Avoids the qemu_file copy during postcopy, which is
3183 * going to do a copy later; can only do it when we
3184 * do this read in one go (matching page sizes)
3185 */
3186 qemu_get_buffer_in_place(f, (uint8_t **)&place_source,
3187 TARGET_PAGE_SIZE);
3188 }
3189 break;
3190 case RAM_SAVE_FLAG_EOS:
3191 /* normal exit */
3192 break;
3193 default:
3194 error_report("Unknown combination of migration flags: %#x"
3195 " (postcopy mode)", flags);
3196 ret = -EINVAL;
7a9ddfbf
PX
3197 break;
3198 }
3199
3200 /* Detect for any possible file errors */
3201 if (!ret && qemu_file_get_error(f)) {
3202 ret = qemu_file_get_error(f);
a7180877
DDAG
3203 }
3204
7a9ddfbf 3205 if (!ret && place_needed) {
a7180877 3206 /* This gets called at the last target page in the host page */
df9ff5e1
DDAG
3207 void *place_dest = host + TARGET_PAGE_SIZE - block->page_size;
3208
a7180877 3209 if (all_zero) {
df9ff5e1 3210 ret = postcopy_place_page_zero(mis, place_dest,
8be4620b 3211 block);
a7180877 3212 } else {
df9ff5e1 3213 ret = postcopy_place_page(mis, place_dest,
8be4620b 3214 place_source, block);
a7180877
DDAG
3215 }
3216 }
a7180877
DDAG
3217 }
3218
3219 return ret;
3220}
3221
acab30b8
DHB
3222static bool postcopy_is_advised(void)
3223{
3224 PostcopyState ps = postcopy_state_get();
3225 return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END;
3226}
3227
3228static bool postcopy_is_running(void)
3229{
3230 PostcopyState ps = postcopy_state_get();
3231 return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END;
3232}
3233
56e93d26
JQ
3234static int ram_load(QEMUFile *f, void *opaque, int version_id)
3235{
edc60127 3236 int flags = 0, ret = 0, invalid_flags = 0;
56e93d26
JQ
3237 static uint64_t seq_iter;
3238 int len = 0;
a7180877
DDAG
3239 /*
3240 * If system is running in postcopy mode, page inserts to host memory must
3241 * be atomic
3242 */
acab30b8 3243 bool postcopy_running = postcopy_is_running();
ef08fb38 3244 /* ADVISE is earlier, it shows the source has the postcopy capability on */
acab30b8 3245 bool postcopy_advised = postcopy_is_advised();
56e93d26
JQ
3246
3247 seq_iter++;
3248
3249 if (version_id != 4) {
3250 ret = -EINVAL;
3251 }
3252
edc60127
JQ
3253 if (!migrate_use_compression()) {
3254 invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE;
3255 }
56e93d26
JQ
3256 /* This RCU critical section can be very long running.
3257 * When RCU reclaims in the code start to become numerous,
3258 * it will be necessary to reduce the granularity of this
3259 * critical section.
3260 */
3261 rcu_read_lock();
a7180877
DDAG
3262
3263 if (postcopy_running) {
3264 ret = ram_load_postcopy(f);
3265 }
3266
3267 while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) {
56e93d26 3268 ram_addr_t addr, total_ram_bytes;
a776aa15 3269 void *host = NULL;
56e93d26
JQ
3270 uint8_t ch;
3271
3272 addr = qemu_get_be64(f);
3273 flags = addr & ~TARGET_PAGE_MASK;
3274 addr &= TARGET_PAGE_MASK;
3275
edc60127
JQ
3276 if (flags & invalid_flags) {
3277 if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) {
3278 error_report("Received an unexpected compressed page");
3279 }
3280
3281 ret = -EINVAL;
3282 break;
3283 }
3284
bb890ed5 3285 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE |
a776aa15 3286 RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) {
4c4bad48
HZ
3287 RAMBlock *block = ram_block_from_stream(f, flags);
3288
3289 host = host_from_ram_block_offset(block, addr);
a776aa15
DDAG
3290 if (!host) {
3291 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
3292 ret = -EINVAL;
3293 break;
3294 }
f9494614 3295 ramblock_recv_bitmap_set(block, host);
1db9d8e5 3296 trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
a776aa15
DDAG
3297 }
3298
56e93d26
JQ
3299 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
3300 case RAM_SAVE_FLAG_MEM_SIZE:
3301 /* Synchronize RAM block list */
3302 total_ram_bytes = addr;
3303 while (!ret && total_ram_bytes) {
3304 RAMBlock *block;
56e93d26
JQ
3305 char id[256];
3306 ram_addr_t length;
3307
3308 len = qemu_get_byte(f);
3309 qemu_get_buffer(f, (uint8_t *)id, len);
3310 id[len] = 0;
3311 length = qemu_get_be64(f);
3312
e3dd7493
DDAG
3313 block = qemu_ram_block_by_name(id);
3314 if (block) {
3315 if (length != block->used_length) {
3316 Error *local_err = NULL;
56e93d26 3317
fa53a0e5 3318 ret = qemu_ram_resize(block, length,
e3dd7493
DDAG
3319 &local_err);
3320 if (local_err) {
3321 error_report_err(local_err);
56e93d26 3322 }
56e93d26 3323 }
ef08fb38
DDAG
3324 /* For postcopy we need to check hugepage sizes match */
3325 if (postcopy_advised &&
3326 block->page_size != qemu_host_page_size) {
3327 uint64_t remote_page_size = qemu_get_be64(f);
3328 if (remote_page_size != block->page_size) {
3329 error_report("Mismatched RAM page size %s "
3330 "(local) %zd != %" PRId64,
3331 id, block->page_size,
3332 remote_page_size);
3333 ret = -EINVAL;
3334 }
3335 }
e3dd7493
DDAG
3336 ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG,
3337 block->idstr);
3338 } else {
56e93d26
JQ
3339 error_report("Unknown ramblock \"%s\", cannot "
3340 "accept migration", id);
3341 ret = -EINVAL;
3342 }
3343
3344 total_ram_bytes -= length;
3345 }
3346 break;
a776aa15 3347
bb890ed5 3348 case RAM_SAVE_FLAG_ZERO:
56e93d26
JQ
3349 ch = qemu_get_byte(f);
3350 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
3351 break;
a776aa15 3352
56e93d26 3353 case RAM_SAVE_FLAG_PAGE:
56e93d26
JQ
3354 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
3355 break;
56e93d26 3356
a776aa15 3357 case RAM_SAVE_FLAG_COMPRESS_PAGE:
56e93d26
JQ
3358 len = qemu_get_be32(f);
3359 if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) {
3360 error_report("Invalid compressed data length: %d", len);
3361 ret = -EINVAL;
3362 break;
3363 }
c1bc6626 3364 decompress_data_with_multi_threads(f, host, len);
56e93d26 3365 break;
a776aa15 3366
56e93d26 3367 case RAM_SAVE_FLAG_XBZRLE:
56e93d26
JQ
3368 if (load_xbzrle(f, addr, host) < 0) {
3369 error_report("Failed to decompress XBZRLE page at "
3370 RAM_ADDR_FMT, addr);
3371 ret = -EINVAL;
3372 break;
3373 }
3374 break;
3375 case RAM_SAVE_FLAG_EOS:
3376 /* normal exit */
3377 break;
3378 default:
3379 if (flags & RAM_SAVE_FLAG_HOOK) {
632e3a5c 3380 ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
56e93d26
JQ
3381 } else {
3382 error_report("Unknown combination of migration flags: %#x",
3383 flags);
3384 ret = -EINVAL;
3385 }
3386 }
3387 if (!ret) {
3388 ret = qemu_file_get_error(f);
3389 }
3390 }
3391
34ab9e97 3392 ret |= wait_for_decompress_done();
56e93d26 3393 rcu_read_unlock();
55c4446b 3394 trace_ram_load_complete(ret, seq_iter);
56e93d26
JQ
3395 return ret;
3396}
3397
c6467627
VSO
3398static bool ram_has_postcopy(void *opaque)
3399{
3400 return migrate_postcopy_ram();
3401}
3402
edd090c7
PX
3403/* Sync all the dirty bitmap with destination VM. */
3404static int ram_dirty_bitmap_sync_all(MigrationState *s, RAMState *rs)
3405{
3406 RAMBlock *block;
3407 QEMUFile *file = s->to_dst_file;
3408 int ramblock_count = 0;
3409
3410 trace_ram_dirty_bitmap_sync_start();
3411
3412 RAMBLOCK_FOREACH(block) {
3413 qemu_savevm_send_recv_bitmap(file, block->idstr);
3414 trace_ram_dirty_bitmap_request(block->idstr);
3415 ramblock_count++;
3416 }
3417
3418 trace_ram_dirty_bitmap_sync_wait();
3419
3420 /* Wait until all the ramblocks' dirty bitmap synced */
3421 while (ramblock_count--) {
3422 qemu_sem_wait(&s->rp_state.rp_sem);
3423 }
3424
3425 trace_ram_dirty_bitmap_sync_complete();
3426
3427 return 0;
3428}
3429
3430static void ram_dirty_bitmap_reload_notify(MigrationState *s)
3431{
3432 qemu_sem_post(&s->rp_state.rp_sem);
3433}
3434
a335debb
PX
3435/*
3436 * Read the received bitmap, revert it as the initial dirty bitmap.
3437 * This is only used when the postcopy migration is paused but wants
3438 * to resume from a middle point.
3439 */
3440int ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *block)
3441{
3442 int ret = -EINVAL;
3443 QEMUFile *file = s->rp_state.from_dst_file;
3444 unsigned long *le_bitmap, nbits = block->used_length >> TARGET_PAGE_BITS;
3445 uint64_t local_size = nbits / 8;
3446 uint64_t size, end_mark;
3447
3448 trace_ram_dirty_bitmap_reload_begin(block->idstr);
3449
3450 if (s->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
3451 error_report("%s: incorrect state %s", __func__,
3452 MigrationStatus_str(s->state));
3453 return -EINVAL;
3454 }
3455
3456 /*
3457 * Note: see comments in ramblock_recv_bitmap_send() on why we
3458 * need the endianess convertion, and the paddings.
3459 */
3460 local_size = ROUND_UP(local_size, 8);
3461
3462 /* Add paddings */
3463 le_bitmap = bitmap_new(nbits + BITS_PER_LONG);
3464
3465 size = qemu_get_be64(file);
3466
3467 /* The size of the bitmap should match with our ramblock */
3468 if (size != local_size) {
3469 error_report("%s: ramblock '%s' bitmap size mismatch "
3470 "(0x%"PRIx64" != 0x%"PRIx64")", __func__,
3471 block->idstr, size, local_size);
3472 ret = -EINVAL;
3473 goto out;
3474 }
3475
3476 size = qemu_get_buffer(file, (uint8_t *)le_bitmap, local_size);
3477 end_mark = qemu_get_be64(file);
3478
3479 ret = qemu_file_get_error(file);
3480 if (ret || size != local_size) {
3481 error_report("%s: read bitmap failed for ramblock '%s': %d"
3482 " (size 0x%"PRIx64", got: 0x%"PRIx64")",
3483 __func__, block->idstr, ret, local_size, size);
3484 ret = -EIO;
3485 goto out;
3486 }
3487
3488 if (end_mark != RAMBLOCK_RECV_BITMAP_ENDING) {
3489 error_report("%s: ramblock '%s' end mark incorrect: 0x%"PRIu64,
3490 __func__, block->idstr, end_mark);
3491 ret = -EINVAL;
3492 goto out;
3493 }
3494
3495 /*
3496 * Endianess convertion. We are during postcopy (though paused).
3497 * The dirty bitmap won't change. We can directly modify it.
3498 */
3499 bitmap_from_le(block->bmap, le_bitmap, nbits);
3500
3501 /*
3502 * What we received is "received bitmap". Revert it as the initial
3503 * dirty bitmap for this ramblock.
3504 */
3505 bitmap_complement(block->bmap, block->bmap, nbits);
3506
3507 trace_ram_dirty_bitmap_reload_complete(block->idstr);
3508
edd090c7
PX
3509 /*
3510 * We succeeded to sync bitmap for current ramblock. If this is
3511 * the last one to sync, we need to notify the main send thread.
3512 */
3513 ram_dirty_bitmap_reload_notify(s);
3514
a335debb
PX
3515 ret = 0;
3516out:
bf269906 3517 g_free(le_bitmap);
a335debb
PX
3518 return ret;
3519}
3520
edd090c7
PX
3521static int ram_resume_prepare(MigrationState *s, void *opaque)
3522{
3523 RAMState *rs = *(RAMState **)opaque;
08614f34 3524 int ret;
edd090c7 3525
08614f34
PX
3526 ret = ram_dirty_bitmap_sync_all(s, rs);
3527 if (ret) {
3528 return ret;
3529 }
3530
3531 ram_state_resume_prepare(rs, s->to_dst_file);
3532
3533 return 0;
edd090c7
PX
3534}
3535
56e93d26 3536static SaveVMHandlers savevm_ram_handlers = {
9907e842 3537 .save_setup = ram_save_setup,
56e93d26 3538 .save_live_iterate = ram_save_iterate,
763c906b 3539 .save_live_complete_postcopy = ram_save_complete,
a3e06c3d 3540 .save_live_complete_precopy = ram_save_complete,
c6467627 3541 .has_postcopy = ram_has_postcopy,
56e93d26
JQ
3542 .save_live_pending = ram_save_pending,
3543 .load_state = ram_load,
f265e0e4
JQ
3544 .save_cleanup = ram_save_cleanup,
3545 .load_setup = ram_load_setup,
3546 .load_cleanup = ram_load_cleanup,
edd090c7 3547 .resume_prepare = ram_resume_prepare,
56e93d26
JQ
3548};
3549
3550void ram_mig_init(void)
3551{
3552 qemu_mutex_init(&XBZRLE.lock);
6f37bb8b 3553 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, &ram_state);
56e93d26 3554}