blob: 42f3b7cb2842bd1142ae61efa8c410c7fc029c13 [file] [log] [blame]
Juan Quintela56e93d22015-05-07 19:33:31 +02001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
Juan Quintela76cc7b52015-05-08 13:20:21 +02005 * Copyright (c) 2011-2015 Red Hat Inc
6 *
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
Juan Quintela56e93d22015-05-07 19:33:31 +02009 *
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 */
Peter Maydell1393a482016-01-26 18:16:54 +000028#include "qemu/osdep.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010029#include "cpu.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020030#include <zlib.h>
Dr. David Alan Gilbert4addcd42015-12-16 11:47:36 +000031#include "qapi-event.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020032#include "qemu/cutils.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020033#include "qemu/bitops.h"
34#include "qemu/bitmap.h"
Juan Quintela7205c9e2015-05-08 13:54:36 +020035#include "qemu/main-loop.h"
Juan Quintela709e3fe2017-04-05 21:47:50 +020036#include "xbzrle.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020037#include "ram.h"
Juan Quintela6666c962017-04-24 20:07:27 +020038#include "migration.h"
Juan Quintelaf2a8f0a2017-04-24 13:42:55 +020039#include "migration/register.h"
Juan Quintela7b1e1a22017-04-17 20:26:27 +020040#include "migration/misc.h"
Juan Quintela08a0aee2017-04-20 18:52:18 +020041#include "qemu-file.h"
Juan Quintelabe07b0a2017-04-20 13:12:24 +020042#include "postcopy-ram.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020043#include "migration/page_cache.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020044#include "qemu/error-report.h"
Juan Quintela8acabf62017-10-05 22:00:31 +020045#include "qapi/qmp/qerror.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020046#include "trace.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020047#include "exec/ram_addr.h"
Alexey Perevalovf9494612017-10-05 14:13:20 +030048#include "exec/target_page.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020049#include "qemu/rcu_queue.h"
zhanghailianga91246c2016-10-27 14:42:59 +080050#include "migration/colo.h"
Peter Lieven9ac78b62017-09-26 12:33:16 +020051#include "migration/block.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020052
Juan Quintela56e93d22015-05-07 19:33:31 +020053/***********************************************************/
54/* ram save/restore */
55
Juan Quintelabb890ed2017-04-28 09:39:55 +020056/* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it
57 * worked for pages that where filled with the same char. We switched
58 * it to only search for the zero value. And to avoid confusion with
59 * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it.
60 */
61
Juan Quintela56e93d22015-05-07 19:33:31 +020062#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
Juan Quintelabb890ed2017-04-28 09:39:55 +020063#define RAM_SAVE_FLAG_ZERO 0x02
Juan Quintela56e93d22015-05-07 19:33:31 +020064#define RAM_SAVE_FLAG_MEM_SIZE 0x04
65#define RAM_SAVE_FLAG_PAGE 0x08
66#define RAM_SAVE_FLAG_EOS 0x10
67#define RAM_SAVE_FLAG_CONTINUE 0x20
68#define RAM_SAVE_FLAG_XBZRLE 0x40
69/* 0x80 is reserved in migration.h start with 0x100 next */
70#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
71
Juan Quintela56e93d22015-05-07 19:33:31 +020072static inline bool is_zero_range(uint8_t *p, uint64_t size)
73{
Richard Hendersona1febc42016-08-29 11:46:14 -070074 return buffer_is_zero(p, size);
Juan Quintela56e93d22015-05-07 19:33:31 +020075}
76
Juan Quintela93604472017-06-06 19:49:03 +020077XBZRLECacheStats xbzrle_counters;
78
Juan Quintela56e93d22015-05-07 19:33:31 +020079/* struct contains XBZRLE cache and a static page
80 used by the compression */
81static struct {
82 /* buffer used for XBZRLE encoding */
83 uint8_t *encoded_buf;
84 /* buffer for storing page content */
85 uint8_t *current_buf;
86 /* Cache for XBZRLE, Protected by lock. */
87 PageCache *cache;
88 QemuMutex lock;
Juan Quintelac00e0922017-05-09 16:22:01 +020089 /* it will store a page full of zeros */
90 uint8_t *zero_target_page;
Juan Quintelaf265e0e2017-06-28 11:52:27 +020091 /* buffer used for XBZRLE decoding */
92 uint8_t *decoded_buf;
Juan Quintela56e93d22015-05-07 19:33:31 +020093} XBZRLE;
94
Juan Quintela56e93d22015-05-07 19:33:31 +020095static void XBZRLE_cache_lock(void)
96{
97 if (migrate_use_xbzrle())
98 qemu_mutex_lock(&XBZRLE.lock);
99}
100
101static void XBZRLE_cache_unlock(void)
102{
103 if (migrate_use_xbzrle())
104 qemu_mutex_unlock(&XBZRLE.lock);
105}
106
Juan Quintela3d0684b2017-03-23 15:06:39 +0100107/**
108 * xbzrle_cache_resize: resize the xbzrle cache
109 *
110 * This function is called from qmp_migrate_set_cache_size in main
111 * thread, possibly while a migration is in progress. A running
112 * migration may be using the cache and might finish during this call,
113 * hence changes to the cache are protected by XBZRLE.lock().
114 *
115 * Returns the new_size or negative in case of error.
116 *
117 * @new_size: new cache size
Juan Quintela8acabf62017-10-05 22:00:31 +0200118 * @errp: set *errp if the check failed, with reason
Juan Quintela56e93d22015-05-07 19:33:31 +0200119 */
Juan Quintela8acabf62017-10-05 22:00:31 +0200120int64_t xbzrle_cache_resize(int64_t new_size, Error **errp)
Juan Quintela56e93d22015-05-07 19:33:31 +0200121{
122 PageCache *new_cache;
123 int64_t ret;
124
Juan Quintela8acabf62017-10-05 22:00:31 +0200125 /* Check for truncation */
126 if (new_size != (size_t)new_size) {
127 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
128 "exceeding address space");
129 return -1;
130 }
131
132 /* Cache should not be larger than guest ram size */
133 if (new_size > ram_bytes_total()) {
134 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size",
135 "exceeds guest ram size");
136 return -1;
137 }
138
Juan Quintela2a313e52017-10-06 23:00:12 +0200139 if (new_size == migrate_xbzrle_cache_size()) {
140 /* nothing to do */
141 return new_size;
142 }
143
Juan Quintela56e93d22015-05-07 19:33:31 +0200144 XBZRLE_cache_lock();
145
146 if (XBZRLE.cache != NULL) {
Juan Quintela80f8dfd2017-10-06 22:30:45 +0200147 new_cache = cache_init(new_size, TARGET_PAGE_SIZE, errp);
Juan Quintela56e93d22015-05-07 19:33:31 +0200148 if (!new_cache) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200149 ret = -1;
150 goto out;
151 }
152
153 cache_fini(XBZRLE.cache);
154 XBZRLE.cache = new_cache;
155 }
156
Juan Quintela2a313e52017-10-06 23:00:12 +0200157 ret = new_size;
Juan Quintela56e93d22015-05-07 19:33:31 +0200158out:
159 XBZRLE_cache_unlock();
160 return ret;
161}
162
Alexey Perevalovf9494612017-10-05 14:13:20 +0300163static void ramblock_recv_map_init(void)
164{
165 RAMBlock *rb;
166
167 RAMBLOCK_FOREACH(rb) {
168 assert(!rb->receivedmap);
169 rb->receivedmap = bitmap_new(rb->max_length >> qemu_target_page_bits());
170 }
171}
172
173int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_addr)
174{
175 return test_bit(ramblock_recv_bitmap_offset(host_addr, rb),
176 rb->receivedmap);
177}
178
179void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr)
180{
181 set_bit_atomic(ramblock_recv_bitmap_offset(host_addr, rb), rb->receivedmap);
182}
183
184void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr,
185 size_t nr)
186{
187 bitmap_set_atomic(rb->receivedmap,
188 ramblock_recv_bitmap_offset(host_addr, rb),
189 nr);
190}
191
Juan Quintelaec481c62017-03-20 22:12:40 +0100192/*
193 * An outstanding page request, on the source, having been received
194 * and queued
195 */
196struct RAMSrcPageRequest {
197 RAMBlock *rb;
198 hwaddr offset;
199 hwaddr len;
200
201 QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req;
202};
203
Juan Quintela6f37bb82017-03-13 19:26:29 +0100204/* State of RAM for migration */
205struct RAMState {
Juan Quintela204b88b2017-03-15 09:16:57 +0100206 /* QEMUFile used for this migration */
207 QEMUFile *f;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100208 /* Last block that we have visited searching for dirty pages */
209 RAMBlock *last_seen_block;
210 /* Last block from where we have sent data */
211 RAMBlock *last_sent_block;
Juan Quintela269ace22017-03-21 15:23:31 +0100212 /* Last dirty target page we have sent */
213 ram_addr_t last_page;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100214 /* last ram version we have seen */
215 uint32_t last_version;
216 /* We are in the first round */
217 bool ram_bulk_stage;
Juan Quintela8d820d62017-03-13 19:35:50 +0100218 /* How many times we have dirty too many pages */
219 int dirty_rate_high_cnt;
Juan Quintelaf664da82017-03-13 19:44:57 +0100220 /* these variables are used for bitmap sync */
221 /* last time we did a full bitmap_sync */
222 int64_t time_last_bitmap_sync;
Juan Quintelaeac74152017-03-28 14:59:01 +0200223 /* bytes transferred at start_time */
Juan Quintelac4bdf0c2017-03-28 14:59:54 +0200224 uint64_t bytes_xfer_prev;
Juan Quintelaa66cd902017-03-28 15:02:43 +0200225 /* number of dirty pages since start_time */
Juan Quintela68908ed2017-03-28 15:05:53 +0200226 uint64_t num_dirty_pages_period;
Juan Quintelab5833fd2017-03-13 19:49:19 +0100227 /* xbzrle misses since the beginning of the period */
228 uint64_t xbzrle_cache_miss_prev;
Juan Quintela36040d92017-03-13 19:51:13 +0100229 /* number of iterations at the beginning of period */
230 uint64_t iterations_prev;
Juan Quintela23b28c32017-03-13 20:51:34 +0100231 /* Iterations since start */
232 uint64_t iterations;
Juan Quintela93604472017-06-06 19:49:03 +0200233 /* number of dirty bits in the bitmap */
Peter Xu2dfaf122017-08-02 17:41:19 +0800234 uint64_t migration_dirty_pages;
235 /* protects modification of the bitmap */
Juan Quintela108cfae2017-03-13 21:38:09 +0100236 QemuMutex bitmap_mutex;
Juan Quintela68a098f2017-03-14 13:48:42 +0100237 /* The RAMBlock used in the last src_page_requests */
238 RAMBlock *last_req_rb;
Juan Quintelaec481c62017-03-20 22:12:40 +0100239 /* Queue of outstanding page requests from the destination */
240 QemuMutex src_page_req_mutex;
241 QSIMPLEQ_HEAD(src_page_requests, RAMSrcPageRequest) src_page_requests;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100242};
243typedef struct RAMState RAMState;
244
Juan Quintela53518d92017-05-04 11:46:24 +0200245static RAMState *ram_state;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100246
Juan Quintela9edabd42017-03-14 12:02:16 +0100247uint64_t ram_bytes_remaining(void)
248{
Juan Quintela53518d92017-05-04 11:46:24 +0200249 return ram_state->migration_dirty_pages * TARGET_PAGE_SIZE;
Juan Quintela9edabd42017-03-14 12:02:16 +0100250}
251
Juan Quintela93604472017-06-06 19:49:03 +0200252MigrationStats ram_counters;
Juan Quintela96506892017-03-14 18:41:03 +0100253
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +0100254/* used by the search for pages to send */
255struct PageSearchStatus {
256 /* Current block being searched */
257 RAMBlock *block;
Juan Quintelaa935e302017-03-21 15:36:51 +0100258 /* Current page to search from */
259 unsigned long page;
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +0100260 /* Set once we wrap around */
261 bool complete_round;
262};
263typedef struct PageSearchStatus PageSearchStatus;
264
Juan Quintela56e93d22015-05-07 19:33:31 +0200265struct CompressParam {
Juan Quintela56e93d22015-05-07 19:33:31 +0200266 bool done;
Liang Li90e56fb2016-05-05 15:32:56 +0800267 bool quit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200268 QEMUFile *file;
269 QemuMutex mutex;
270 QemuCond cond;
271 RAMBlock *block;
272 ram_addr_t offset;
273};
274typedef struct CompressParam CompressParam;
275
276struct DecompressParam {
Liang Li73a89122016-05-05 15:32:51 +0800277 bool done;
Liang Li90e56fb2016-05-05 15:32:56 +0800278 bool quit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200279 QemuMutex mutex;
280 QemuCond cond;
281 void *des;
Peter Maydelld341d9f2016-01-22 15:09:21 +0000282 uint8_t *compbuf;
Juan Quintela56e93d22015-05-07 19:33:31 +0200283 int len;
284};
285typedef struct DecompressParam DecompressParam;
286
287static CompressParam *comp_param;
288static QemuThread *compress_threads;
289/* comp_done_cond is used to wake up the migration thread when
290 * one of the compression threads has finished the compression.
291 * comp_done_lock is used to co-work with comp_done_cond.
292 */
Liang Li0d9f9a52016-05-05 15:32:59 +0800293static QemuMutex comp_done_lock;
294static QemuCond comp_done_cond;
Juan Quintela56e93d22015-05-07 19:33:31 +0200295/* The empty QEMUFileOps will be used by file in CompressParam */
296static const QEMUFileOps empty_ops = { };
297
Juan Quintela56e93d22015-05-07 19:33:31 +0200298static DecompressParam *decomp_param;
299static QemuThread *decompress_threads;
Liang Li73a89122016-05-05 15:32:51 +0800300static QemuMutex decomp_done_lock;
301static QemuCond decomp_done_cond;
Juan Quintela56e93d22015-05-07 19:33:31 +0200302
Liang Lia7a9a882016-05-05 15:32:57 +0800303static int do_compress_ram_page(QEMUFile *f, RAMBlock *block,
304 ram_addr_t offset);
Juan Quintela56e93d22015-05-07 19:33:31 +0200305
306static void *do_data_compress(void *opaque)
307{
308 CompressParam *param = opaque;
Liang Lia7a9a882016-05-05 15:32:57 +0800309 RAMBlock *block;
310 ram_addr_t offset;
Juan Quintela56e93d22015-05-07 19:33:31 +0200311
Liang Lia7a9a882016-05-05 15:32:57 +0800312 qemu_mutex_lock(&param->mutex);
Liang Li90e56fb2016-05-05 15:32:56 +0800313 while (!param->quit) {
Liang Lia7a9a882016-05-05 15:32:57 +0800314 if (param->block) {
315 block = param->block;
316 offset = param->offset;
317 param->block = NULL;
318 qemu_mutex_unlock(&param->mutex);
319
320 do_compress_ram_page(param->file, block, offset);
321
Liang Li0d9f9a52016-05-05 15:32:59 +0800322 qemu_mutex_lock(&comp_done_lock);
Liang Lia7a9a882016-05-05 15:32:57 +0800323 param->done = true;
Liang Li0d9f9a52016-05-05 15:32:59 +0800324 qemu_cond_signal(&comp_done_cond);
325 qemu_mutex_unlock(&comp_done_lock);
Liang Lia7a9a882016-05-05 15:32:57 +0800326
327 qemu_mutex_lock(&param->mutex);
328 } else {
Juan Quintela56e93d22015-05-07 19:33:31 +0200329 qemu_cond_wait(&param->cond, &param->mutex);
330 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200331 }
Liang Lia7a9a882016-05-05 15:32:57 +0800332 qemu_mutex_unlock(&param->mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200333
334 return NULL;
335}
336
337static inline void terminate_compression_threads(void)
338{
339 int idx, thread_count;
340
341 thread_count = migrate_compress_threads();
Juan Quintela3d0684b2017-03-23 15:06:39 +0100342
Juan Quintela56e93d22015-05-07 19:33:31 +0200343 for (idx = 0; idx < thread_count; idx++) {
344 qemu_mutex_lock(&comp_param[idx].mutex);
Liang Li90e56fb2016-05-05 15:32:56 +0800345 comp_param[idx].quit = true;
Juan Quintela56e93d22015-05-07 19:33:31 +0200346 qemu_cond_signal(&comp_param[idx].cond);
347 qemu_mutex_unlock(&comp_param[idx].mutex);
348 }
349}
350
Juan Quintelaf0afa332017-06-28 11:52:28 +0200351static void compress_threads_save_cleanup(void)
Juan Quintela56e93d22015-05-07 19:33:31 +0200352{
353 int i, thread_count;
354
355 if (!migrate_use_compression()) {
356 return;
357 }
358 terminate_compression_threads();
359 thread_count = migrate_compress_threads();
360 for (i = 0; i < thread_count; i++) {
361 qemu_thread_join(compress_threads + i);
362 qemu_fclose(comp_param[i].file);
363 qemu_mutex_destroy(&comp_param[i].mutex);
364 qemu_cond_destroy(&comp_param[i].cond);
365 }
Liang Li0d9f9a52016-05-05 15:32:59 +0800366 qemu_mutex_destroy(&comp_done_lock);
367 qemu_cond_destroy(&comp_done_cond);
Juan Quintela56e93d22015-05-07 19:33:31 +0200368 g_free(compress_threads);
369 g_free(comp_param);
Juan Quintela56e93d22015-05-07 19:33:31 +0200370 compress_threads = NULL;
371 comp_param = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +0200372}
373
Juan Quintelaf0afa332017-06-28 11:52:28 +0200374static void compress_threads_save_setup(void)
Juan Quintela56e93d22015-05-07 19:33:31 +0200375{
376 int i, thread_count;
377
378 if (!migrate_use_compression()) {
379 return;
380 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200381 thread_count = migrate_compress_threads();
382 compress_threads = g_new0(QemuThread, thread_count);
383 comp_param = g_new0(CompressParam, thread_count);
Liang Li0d9f9a52016-05-05 15:32:59 +0800384 qemu_cond_init(&comp_done_cond);
385 qemu_mutex_init(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200386 for (i = 0; i < thread_count; i++) {
Cao jine110aa92016-07-29 15:10:31 +0800387 /* comp_param[i].file is just used as a dummy buffer to save data,
388 * set its ops to empty.
Juan Quintela56e93d22015-05-07 19:33:31 +0200389 */
390 comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops);
391 comp_param[i].done = true;
Liang Li90e56fb2016-05-05 15:32:56 +0800392 comp_param[i].quit = false;
Juan Quintela56e93d22015-05-07 19:33:31 +0200393 qemu_mutex_init(&comp_param[i].mutex);
394 qemu_cond_init(&comp_param[i].cond);
395 qemu_thread_create(compress_threads + i, "compress",
396 do_data_compress, comp_param + i,
397 QEMU_THREAD_JOINABLE);
398 }
399}
400
Juan Quintelaf986c3d2016-01-14 16:52:55 +0100401/* Multiple fd's */
402
403struct MultiFDSendParams {
404 uint8_t id;
405 char *name;
406 QemuThread thread;
407 QemuSemaphore sem;
408 QemuMutex mutex;
409 bool quit;
410};
411typedef struct MultiFDSendParams MultiFDSendParams;
412
413struct {
414 MultiFDSendParams *params;
415 /* number of created threads */
416 int count;
417} *multifd_send_state;
418
419static void terminate_multifd_send_threads(Error *errp)
420{
421 int i;
422
423 for (i = 0; i < multifd_send_state->count; i++) {
424 MultiFDSendParams *p = &multifd_send_state->params[i];
425
426 qemu_mutex_lock(&p->mutex);
427 p->quit = true;
428 qemu_sem_post(&p->sem);
429 qemu_mutex_unlock(&p->mutex);
430 }
431}
432
433int multifd_save_cleanup(Error **errp)
434{
435 int i;
436 int ret = 0;
437
438 if (!migrate_use_multifd()) {
439 return 0;
440 }
441 terminate_multifd_send_threads(NULL);
442 for (i = 0; i < multifd_send_state->count; i++) {
443 MultiFDSendParams *p = &multifd_send_state->params[i];
444
445 qemu_thread_join(&p->thread);
446 qemu_mutex_destroy(&p->mutex);
447 qemu_sem_destroy(&p->sem);
448 g_free(p->name);
449 p->name = NULL;
450 }
451 g_free(multifd_send_state->params);
452 multifd_send_state->params = NULL;
453 g_free(multifd_send_state);
454 multifd_send_state = NULL;
455 return ret;
456}
457
458static void *multifd_send_thread(void *opaque)
459{
460 MultiFDSendParams *p = opaque;
461
462 while (true) {
463 qemu_mutex_lock(&p->mutex);
464 if (p->quit) {
465 qemu_mutex_unlock(&p->mutex);
466 break;
467 }
468 qemu_mutex_unlock(&p->mutex);
469 qemu_sem_wait(&p->sem);
470 }
471
472 return NULL;
473}
474
475int multifd_save_setup(void)
476{
477 int thread_count;
478 uint8_t i;
479
480 if (!migrate_use_multifd()) {
481 return 0;
482 }
483 thread_count = migrate_multifd_channels();
484 multifd_send_state = g_malloc0(sizeof(*multifd_send_state));
485 multifd_send_state->params = g_new0(MultiFDSendParams, thread_count);
486 multifd_send_state->count = 0;
487 for (i = 0; i < thread_count; i++) {
488 MultiFDSendParams *p = &multifd_send_state->params[i];
489
490 qemu_mutex_init(&p->mutex);
491 qemu_sem_init(&p->sem, 0);
492 p->quit = false;
493 p->id = i;
494 p->name = g_strdup_printf("multifdsend_%d", i);
495 qemu_thread_create(&p->thread, p->name, multifd_send_thread, p,
496 QEMU_THREAD_JOINABLE);
497
498 multifd_send_state->count++;
499 }
500 return 0;
501}
502
503struct MultiFDRecvParams {
504 uint8_t id;
505 char *name;
506 QemuThread thread;
507 QemuSemaphore sem;
508 QemuMutex mutex;
509 bool quit;
510};
511typedef struct MultiFDRecvParams MultiFDRecvParams;
512
513struct {
514 MultiFDRecvParams *params;
515 /* number of created threads */
516 int count;
517} *multifd_recv_state;
518
519static void terminate_multifd_recv_threads(Error *errp)
520{
521 int i;
522
523 for (i = 0; i < multifd_recv_state->count; i++) {
524 MultiFDRecvParams *p = &multifd_recv_state->params[i];
525
526 qemu_mutex_lock(&p->mutex);
527 p->quit = true;
528 qemu_sem_post(&p->sem);
529 qemu_mutex_unlock(&p->mutex);
530 }
531}
532
533int multifd_load_cleanup(Error **errp)
534{
535 int i;
536 int ret = 0;
537
538 if (!migrate_use_multifd()) {
539 return 0;
540 }
541 terminate_multifd_recv_threads(NULL);
542 for (i = 0; i < multifd_recv_state->count; i++) {
543 MultiFDRecvParams *p = &multifd_recv_state->params[i];
544
545 qemu_thread_join(&p->thread);
546 qemu_mutex_destroy(&p->mutex);
547 qemu_sem_destroy(&p->sem);
548 g_free(p->name);
549 p->name = NULL;
550 }
551 g_free(multifd_recv_state->params);
552 multifd_recv_state->params = NULL;
553 g_free(multifd_recv_state);
554 multifd_recv_state = NULL;
555
556 return ret;
557}
558
559static void *multifd_recv_thread(void *opaque)
560{
561 MultiFDRecvParams *p = opaque;
562
563 while (true) {
564 qemu_mutex_lock(&p->mutex);
565 if (p->quit) {
566 qemu_mutex_unlock(&p->mutex);
567 break;
568 }
569 qemu_mutex_unlock(&p->mutex);
570 qemu_sem_wait(&p->sem);
571 }
572
573 return NULL;
574}
575
576int multifd_load_setup(void)
577{
578 int thread_count;
579 uint8_t i;
580
581 if (!migrate_use_multifd()) {
582 return 0;
583 }
584 thread_count = migrate_multifd_channels();
585 multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state));
586 multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count);
587 multifd_recv_state->count = 0;
588 for (i = 0; i < thread_count; i++) {
589 MultiFDRecvParams *p = &multifd_recv_state->params[i];
590
591 qemu_mutex_init(&p->mutex);
592 qemu_sem_init(&p->sem, 0);
593 p->quit = false;
594 p->id = i;
595 p->name = g_strdup_printf("multifdrecv_%d", i);
596 qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p,
597 QEMU_THREAD_JOINABLE);
598 multifd_recv_state->count++;
599 }
600 return 0;
601}
602
Juan Quintela56e93d22015-05-07 19:33:31 +0200603/**
Juan Quintela3d0684b2017-03-23 15:06:39 +0100604 * save_page_header: write page header to wire
Juan Quintela56e93d22015-05-07 19:33:31 +0200605 *
606 * If this is the 1st block, it also writes the block identification
607 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100608 * Returns the number of bytes written
Juan Quintela56e93d22015-05-07 19:33:31 +0200609 *
610 * @f: QEMUFile where to send the data
611 * @block: block that contains the page we want to send
612 * @offset: offset inside the block for the page
613 * in the lower bits, it contains flags
614 */
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200615static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block,
616 ram_addr_t offset)
Juan Quintela56e93d22015-05-07 19:33:31 +0200617{
Liang Li9f5f3802015-07-13 17:34:10 +0800618 size_t size, len;
Juan Quintela56e93d22015-05-07 19:33:31 +0200619
Juan Quintela24795692017-03-21 11:45:01 +0100620 if (block == rs->last_sent_block) {
621 offset |= RAM_SAVE_FLAG_CONTINUE;
622 }
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200623 qemu_put_be64(f, offset);
Juan Quintela56e93d22015-05-07 19:33:31 +0200624 size = 8;
625
626 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
Liang Li9f5f3802015-07-13 17:34:10 +0800627 len = strlen(block->idstr);
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200628 qemu_put_byte(f, len);
629 qemu_put_buffer(f, (uint8_t *)block->idstr, len);
Liang Li9f5f3802015-07-13 17:34:10 +0800630 size += 1 + len;
Juan Quintela24795692017-03-21 11:45:01 +0100631 rs->last_sent_block = block;
Juan Quintela56e93d22015-05-07 19:33:31 +0200632 }
633 return size;
634}
635
Juan Quintela3d0684b2017-03-23 15:06:39 +0100636/**
637 * mig_throttle_guest_down: throotle down the guest
638 *
639 * Reduce amount of guest cpu execution to hopefully slow down memory
640 * writes. If guest dirty memory rate is reduced below the rate at
641 * which we can transfer pages to the destination then we should be
642 * able to complete migration. Some workloads dirty memory way too
643 * fast and will not effectively converge, even with auto-converge.
Jason J. Herne070afca2015-09-08 13:12:35 -0400644 */
645static void mig_throttle_guest_down(void)
646{
647 MigrationState *s = migrate_get_current();
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100648 uint64_t pct_initial = s->parameters.cpu_throttle_initial;
649 uint64_t pct_icrement = s->parameters.cpu_throttle_increment;
Jason J. Herne070afca2015-09-08 13:12:35 -0400650
651 /* We have not started throttling yet. Let's start it. */
652 if (!cpu_throttle_active()) {
653 cpu_throttle_set(pct_initial);
654 } else {
655 /* Throttling already on, just increase the rate */
656 cpu_throttle_set(cpu_throttle_get_percentage() + pct_icrement);
657 }
658}
659
Juan Quintela3d0684b2017-03-23 15:06:39 +0100660/**
661 * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache
662 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100663 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100664 * @current_addr: address for the zero page
665 *
666 * Update the xbzrle cache to reflect a page that's been sent as all 0.
Juan Quintela56e93d22015-05-07 19:33:31 +0200667 * The important thing is that a stale (not-yet-0'd) page be replaced
668 * by the new data.
669 * As a bonus, if the page wasn't in the cache it gets added so that
Juan Quintela3d0684b2017-03-23 15:06:39 +0100670 * when a small write is made into the 0'd page it gets XBZRLE sent.
Juan Quintela56e93d22015-05-07 19:33:31 +0200671 */
Juan Quintela6f37bb82017-03-13 19:26:29 +0100672static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr)
Juan Quintela56e93d22015-05-07 19:33:31 +0200673{
Juan Quintela6f37bb82017-03-13 19:26:29 +0100674 if (rs->ram_bulk_stage || !migrate_use_xbzrle()) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200675 return;
676 }
677
678 /* We don't care if this fails to allocate a new cache page
679 * as long as it updated an old one */
Juan Quintelac00e0922017-05-09 16:22:01 +0200680 cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page,
Juan Quintela93604472017-06-06 19:49:03 +0200681 ram_counters.dirty_sync_count);
Juan Quintela56e93d22015-05-07 19:33:31 +0200682}
683
684#define ENCODING_FLAG_XBZRLE 0x1
685
686/**
687 * save_xbzrle_page: compress and send current page
688 *
689 * Returns: 1 means that we wrote the page
690 * 0 means that page is identical to the one already sent
691 * -1 means that xbzrle would be longer than normal
692 *
Juan Quintela5a987732017-03-13 19:39:02 +0100693 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100694 * @current_data: pointer to the address of the page contents
695 * @current_addr: addr of the page
Juan Quintela56e93d22015-05-07 19:33:31 +0200696 * @block: block that contains the page we want to send
697 * @offset: offset inside the block for the page
698 * @last_stage: if we are at the completion stage
Juan Quintela56e93d22015-05-07 19:33:31 +0200699 */
Juan Quintela204b88b2017-03-15 09:16:57 +0100700static int save_xbzrle_page(RAMState *rs, uint8_t **current_data,
Juan Quintela56e93d22015-05-07 19:33:31 +0200701 ram_addr_t current_addr, RAMBlock *block,
Juan Quintela072c2512017-03-14 10:27:31 +0100702 ram_addr_t offset, bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +0200703{
704 int encoded_len = 0, bytes_xbzrle;
705 uint8_t *prev_cached_page;
706
Juan Quintela93604472017-06-06 19:49:03 +0200707 if (!cache_is_cached(XBZRLE.cache, current_addr,
708 ram_counters.dirty_sync_count)) {
709 xbzrle_counters.cache_miss++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200710 if (!last_stage) {
711 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
Juan Quintela93604472017-06-06 19:49:03 +0200712 ram_counters.dirty_sync_count) == -1) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200713 return -1;
714 } else {
715 /* update *current_data when the page has been
716 inserted into cache */
717 *current_data = get_cached_data(XBZRLE.cache, current_addr);
718 }
719 }
720 return -1;
721 }
722
723 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
724
725 /* save current buffer into memory */
726 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
727
728 /* XBZRLE encoding (if there is no overflow) */
729 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
730 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
731 TARGET_PAGE_SIZE);
732 if (encoded_len == 0) {
Juan Quintela55c44462017-01-23 22:32:05 +0100733 trace_save_xbzrle_page_skipping();
Juan Quintela56e93d22015-05-07 19:33:31 +0200734 return 0;
735 } else if (encoded_len == -1) {
Juan Quintela55c44462017-01-23 22:32:05 +0100736 trace_save_xbzrle_page_overflow();
Juan Quintela93604472017-06-06 19:49:03 +0200737 xbzrle_counters.overflow++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200738 /* update data in the cache */
739 if (!last_stage) {
740 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
741 *current_data = prev_cached_page;
742 }
743 return -1;
744 }
745
746 /* we need to update the data in the cache, in order to get the same data */
747 if (!last_stage) {
748 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
749 }
750
751 /* Send XBZRLE based compressed page */
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200752 bytes_xbzrle = save_page_header(rs, rs->f, block,
Juan Quintela204b88b2017-03-15 09:16:57 +0100753 offset | RAM_SAVE_FLAG_XBZRLE);
754 qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE);
755 qemu_put_be16(rs->f, encoded_len);
756 qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len);
Juan Quintela56e93d22015-05-07 19:33:31 +0200757 bytes_xbzrle += encoded_len + 1 + 2;
Juan Quintela93604472017-06-06 19:49:03 +0200758 xbzrle_counters.pages++;
759 xbzrle_counters.bytes += bytes_xbzrle;
760 ram_counters.transferred += bytes_xbzrle;
Juan Quintela56e93d22015-05-07 19:33:31 +0200761
762 return 1;
763}
764
Juan Quintela3d0684b2017-03-23 15:06:39 +0100765/**
766 * migration_bitmap_find_dirty: find the next dirty page from start
Dr. David Alan Gilbertf3f491f2015-11-05 18:11:01 +0000767 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100768 * Called with rcu_read_lock() to protect migration_bitmap
769 *
770 * Returns the byte offset within memory region of the start of a dirty page
771 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100772 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100773 * @rb: RAMBlock where to search for dirty pages
Juan Quintelaa935e302017-03-21 15:36:51 +0100774 * @start: page where we start the search
Dr. David Alan Gilbertf3f491f2015-11-05 18:11:01 +0000775 */
Juan Quintela56e93d22015-05-07 19:33:31 +0200776static inline
Juan Quintelaa935e302017-03-21 15:36:51 +0100777unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb,
Juan Quintelaf20e2862017-03-21 16:19:05 +0100778 unsigned long start)
Juan Quintela56e93d22015-05-07 19:33:31 +0200779{
Juan Quintela6b6712e2017-03-22 15:18:04 +0100780 unsigned long size = rb->used_length >> TARGET_PAGE_BITS;
781 unsigned long *bitmap = rb->bmap;
Juan Quintela56e93d22015-05-07 19:33:31 +0200782 unsigned long next;
783
Juan Quintela6b6712e2017-03-22 15:18:04 +0100784 if (rs->ram_bulk_stage && start > 0) {
785 next = start + 1;
Juan Quintela56e93d22015-05-07 19:33:31 +0200786 } else {
Juan Quintela6b6712e2017-03-22 15:18:04 +0100787 next = find_next_bit(bitmap, size, start);
Juan Quintela56e93d22015-05-07 19:33:31 +0200788 }
789
Juan Quintela6b6712e2017-03-22 15:18:04 +0100790 return next;
Juan Quintela56e93d22015-05-07 19:33:31 +0200791}
792
Juan Quintela06b10682017-03-21 15:18:05 +0100793static inline bool migration_bitmap_clear_dirty(RAMState *rs,
Juan Quintelaf20e2862017-03-21 16:19:05 +0100794 RAMBlock *rb,
795 unsigned long page)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000796{
797 bool ret;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000798
Juan Quintela6b6712e2017-03-22 15:18:04 +0100799 ret = test_and_clear_bit(page, rb->bmap);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000800
801 if (ret) {
Juan Quintela0d8ec882017-03-13 21:21:41 +0100802 rs->migration_dirty_pages--;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000803 }
804 return ret;
805}
806
Juan Quintela15440dd2017-03-21 09:35:04 +0100807static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb,
808 ram_addr_t start, ram_addr_t length)
Juan Quintela56e93d22015-05-07 19:33:31 +0200809{
Juan Quintela0d8ec882017-03-13 21:21:41 +0100810 rs->migration_dirty_pages +=
Juan Quintela6b6712e2017-03-22 15:18:04 +0100811 cpu_physical_memory_sync_dirty_bitmap(rb, start, length,
Juan Quintela0d8ec882017-03-13 21:21:41 +0100812 &rs->num_dirty_pages_period);
Juan Quintela56e93d22015-05-07 19:33:31 +0200813}
814
Juan Quintela3d0684b2017-03-23 15:06:39 +0100815/**
816 * ram_pagesize_summary: calculate all the pagesizes of a VM
817 *
818 * Returns a summary bitmap of the page sizes of all RAMBlocks
819 *
820 * For VMs with just normal pages this is equivalent to the host page
821 * size. If it's got some huge pages then it's the OR of all the
822 * different page sizes.
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +0000823 */
824uint64_t ram_pagesize_summary(void)
825{
826 RAMBlock *block;
827 uint64_t summary = 0;
828
Peter Xu99e15582017-05-12 12:17:39 +0800829 RAMBLOCK_FOREACH(block) {
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +0000830 summary |= block->page_size;
831 }
832
833 return summary;
834}
835
Juan Quintela8d820d62017-03-13 19:35:50 +0100836static void migration_bitmap_sync(RAMState *rs)
Juan Quintela56e93d22015-05-07 19:33:31 +0200837{
838 RAMBlock *block;
Juan Quintela56e93d22015-05-07 19:33:31 +0200839 int64_t end_time;
Juan Quintelac4bdf0c2017-03-28 14:59:54 +0200840 uint64_t bytes_xfer_now;
Juan Quintela56e93d22015-05-07 19:33:31 +0200841
Juan Quintela93604472017-06-06 19:49:03 +0200842 ram_counters.dirty_sync_count++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200843
Juan Quintelaf664da82017-03-13 19:44:57 +0100844 if (!rs->time_last_bitmap_sync) {
845 rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela56e93d22015-05-07 19:33:31 +0200846 }
847
848 trace_migration_bitmap_sync_start();
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +0200849 memory_global_dirty_log_sync();
Juan Quintela56e93d22015-05-07 19:33:31 +0200850
Juan Quintela108cfae2017-03-13 21:38:09 +0100851 qemu_mutex_lock(&rs->bitmap_mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200852 rcu_read_lock();
Peter Xu99e15582017-05-12 12:17:39 +0800853 RAMBLOCK_FOREACH(block) {
Juan Quintela15440dd2017-03-21 09:35:04 +0100854 migration_bitmap_sync_range(rs, block, 0, block->used_length);
Juan Quintela56e93d22015-05-07 19:33:31 +0200855 }
856 rcu_read_unlock();
Juan Quintela108cfae2017-03-13 21:38:09 +0100857 qemu_mutex_unlock(&rs->bitmap_mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200858
Juan Quintelaa66cd902017-03-28 15:02:43 +0200859 trace_migration_bitmap_sync_end(rs->num_dirty_pages_period);
Chao Fan1ffb5df2017-03-14 09:55:07 +0800860
Juan Quintela56e93d22015-05-07 19:33:31 +0200861 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
862
863 /* more than 1 second = 1000 millisecons */
Juan Quintelaf664da82017-03-13 19:44:57 +0100864 if (end_time > rs->time_last_bitmap_sync + 1000) {
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100865 /* calculate period counters */
Juan Quintela93604472017-06-06 19:49:03 +0200866 ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100867 / (end_time - rs->time_last_bitmap_sync);
Juan Quintela93604472017-06-06 19:49:03 +0200868 bytes_xfer_now = ram_counters.transferred;
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100869
Peter Lieven9ac78b62017-09-26 12:33:16 +0200870 /* During block migration the auto-converge logic incorrectly detects
871 * that ram migration makes no progress. Avoid this by disabling the
872 * throttling logic during the bulk phase of block migration. */
873 if (migrate_auto_converge() && !blk_mig_bulk_active()) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200874 /* The following detection logic can be refined later. For now:
875 Check to see if the dirtied bytes is 50% more than the approx.
876 amount of bytes that just got transferred since the last time we
Jason J. Herne070afca2015-09-08 13:12:35 -0400877 were in this routine. If that happens twice, start or increase
878 throttling */
Jason J. Herne070afca2015-09-08 13:12:35 -0400879
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100880 if ((rs->num_dirty_pages_period * TARGET_PAGE_SIZE >
Juan Quintelaeac74152017-03-28 14:59:01 +0200881 (bytes_xfer_now - rs->bytes_xfer_prev) / 2) &&
Felipe Franciosib4a3c642017-05-24 17:10:03 +0100882 (++rs->dirty_rate_high_cnt >= 2)) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200883 trace_migration_throttle();
Juan Quintela8d820d62017-03-13 19:35:50 +0100884 rs->dirty_rate_high_cnt = 0;
Jason J. Herne070afca2015-09-08 13:12:35 -0400885 mig_throttle_guest_down();
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100886 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200887 }
Jason J. Herne070afca2015-09-08 13:12:35 -0400888
Juan Quintela56e93d22015-05-07 19:33:31 +0200889 if (migrate_use_xbzrle()) {
Juan Quintela23b28c32017-03-13 20:51:34 +0100890 if (rs->iterations_prev != rs->iterations) {
Juan Quintela93604472017-06-06 19:49:03 +0200891 xbzrle_counters.cache_miss_rate =
892 (double)(xbzrle_counters.cache_miss -
Juan Quintelab5833fd2017-03-13 19:49:19 +0100893 rs->xbzrle_cache_miss_prev) /
Juan Quintela23b28c32017-03-13 20:51:34 +0100894 (rs->iterations - rs->iterations_prev);
Juan Quintela56e93d22015-05-07 19:33:31 +0200895 }
Juan Quintela23b28c32017-03-13 20:51:34 +0100896 rs->iterations_prev = rs->iterations;
Juan Quintela93604472017-06-06 19:49:03 +0200897 rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss;
Juan Quintela56e93d22015-05-07 19:33:31 +0200898 }
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100899
900 /* reset period counters */
Juan Quintelaf664da82017-03-13 19:44:57 +0100901 rs->time_last_bitmap_sync = end_time;
Juan Quintelaa66cd902017-03-28 15:02:43 +0200902 rs->num_dirty_pages_period = 0;
Felipe Franciosid2a4d852017-05-24 17:10:02 +0100903 rs->bytes_xfer_prev = bytes_xfer_now;
Juan Quintela56e93d22015-05-07 19:33:31 +0200904 }
Dr. David Alan Gilbert4addcd42015-12-16 11:47:36 +0000905 if (migrate_use_events()) {
Juan Quintela93604472017-06-06 19:49:03 +0200906 qapi_event_send_migration_pass(ram_counters.dirty_sync_count, NULL);
Dr. David Alan Gilbert4addcd42015-12-16 11:47:36 +0000907 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200908}
909
910/**
Juan Quintela3d0684b2017-03-23 15:06:39 +0100911 * save_zero_page: send the zero page to the stream
Juan Quintela56e93d22015-05-07 19:33:31 +0200912 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100913 * Returns the number of pages written.
Juan Quintela56e93d22015-05-07 19:33:31 +0200914 *
Juan Quintelaf7ccd612017-03-13 20:30:21 +0100915 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +0200916 * @block: block that contains the page we want to send
917 * @offset: offset inside the block for the page
918 * @p: pointer to the page
Juan Quintela56e93d22015-05-07 19:33:31 +0200919 */
Juan Quintelace25d332017-03-15 11:00:51 +0100920static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
921 uint8_t *p)
Juan Quintela56e93d22015-05-07 19:33:31 +0200922{
923 int pages = -1;
924
925 if (is_zero_range(p, TARGET_PAGE_SIZE)) {
Juan Quintela93604472017-06-06 19:49:03 +0200926 ram_counters.duplicate++;
927 ram_counters.transferred +=
Juan Quintelabb890ed2017-04-28 09:39:55 +0200928 save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_ZERO);
Juan Quintelace25d332017-03-15 11:00:51 +0100929 qemu_put_byte(rs->f, 0);
Juan Quintela93604472017-06-06 19:49:03 +0200930 ram_counters.transferred += 1;
Juan Quintela56e93d22015-05-07 19:33:31 +0200931 pages = 1;
932 }
933
934 return pages;
935}
936
Juan Quintela57273092017-03-20 22:25:28 +0100937static void ram_release_pages(const char *rbname, uint64_t offset, int pages)
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300938{
Juan Quintela57273092017-03-20 22:25:28 +0100939 if (!migrate_release_ram() || !migration_in_postcopy()) {
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300940 return;
941 }
942
Juan Quintelaaaa20642017-03-21 11:35:24 +0100943 ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS);
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300944}
945
Juan Quintela56e93d22015-05-07 19:33:31 +0200946/**
Juan Quintela3d0684b2017-03-23 15:06:39 +0100947 * ram_save_page: send the given page to the stream
Juan Quintela56e93d22015-05-07 19:33:31 +0200948 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100949 * Returns the number of pages written.
Dr. David Alan Gilbert3fd3c4b2015-12-10 16:31:46 +0000950 * < 0 - error
951 * >=0 - Number of pages written - this might legally be 0
952 * if xbzrle noticed the page was the same.
Juan Quintela56e93d22015-05-07 19:33:31 +0200953 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100954 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +0200955 * @block: block that contains the page we want to send
956 * @offset: offset inside the block for the page
957 * @last_stage: if we are at the completion stage
Juan Quintela56e93d22015-05-07 19:33:31 +0200958 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +0100959static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +0200960{
961 int pages = -1;
962 uint64_t bytes_xmit;
963 ram_addr_t current_addr;
Juan Quintela56e93d22015-05-07 19:33:31 +0200964 uint8_t *p;
965 int ret;
966 bool send_async = true;
zhanghailianga08f6892016-01-15 11:37:44 +0800967 RAMBlock *block = pss->block;
Juan Quintelaa935e302017-03-21 15:36:51 +0100968 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
Juan Quintela56e93d22015-05-07 19:33:31 +0200969
Dr. David Alan Gilbert2f68e392015-08-13 11:51:30 +0100970 p = block->host + offset;
Dr. David Alan Gilbert1db9d8e2017-04-26 19:37:21 +0100971 trace_ram_save_page(block->idstr, (uint64_t)offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +0200972
973 /* In doubt sent page as normal */
974 bytes_xmit = 0;
Juan Quintelace25d332017-03-15 11:00:51 +0100975 ret = ram_control_save_page(rs->f, block->offset,
Juan Quintela56e93d22015-05-07 19:33:31 +0200976 offset, TARGET_PAGE_SIZE, &bytes_xmit);
977 if (bytes_xmit) {
Juan Quintela93604472017-06-06 19:49:03 +0200978 ram_counters.transferred += bytes_xmit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200979 pages = 1;
980 }
981
982 XBZRLE_cache_lock();
983
984 current_addr = block->offset + offset;
985
Juan Quintela56e93d22015-05-07 19:33:31 +0200986 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
987 if (ret != RAM_SAVE_CONTROL_DELAYED) {
988 if (bytes_xmit > 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200989 ram_counters.normal++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200990 } else if (bytes_xmit == 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200991 ram_counters.duplicate++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200992 }
993 }
994 } else {
Juan Quintelace25d332017-03-15 11:00:51 +0100995 pages = save_zero_page(rs, block, offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +0200996 if (pages > 0) {
997 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
998 * page would be stale
999 */
Juan Quintela6f37bb82017-03-13 19:26:29 +01001000 xbzrle_cache_zero_page(rs, current_addr);
Juan Quintelaa935e302017-03-21 15:36:51 +01001001 ram_release_pages(block->idstr, offset, pages);
Juan Quintela6f37bb82017-03-13 19:26:29 +01001002 } else if (!rs->ram_bulk_stage &&
Juan Quintela57273092017-03-20 22:25:28 +01001003 !migration_in_postcopy() && migrate_use_xbzrle()) {
Juan Quintela204b88b2017-03-15 09:16:57 +01001004 pages = save_xbzrle_page(rs, &p, current_addr, block,
Juan Quintela072c2512017-03-14 10:27:31 +01001005 offset, last_stage);
Juan Quintela56e93d22015-05-07 19:33:31 +02001006 if (!last_stage) {
1007 /* Can't send this cached data async, since the cache page
1008 * might get updated before it gets to the wire
1009 */
1010 send_async = false;
1011 }
1012 }
1013 }
1014
1015 /* XBZRLE overflow or normal page */
1016 if (pages == -1) {
Juan Quintela93604472017-06-06 19:49:03 +02001017 ram_counters.transferred +=
1018 save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_PAGE);
Juan Quintela56e93d22015-05-07 19:33:31 +02001019 if (send_async) {
Juan Quintelace25d332017-03-15 11:00:51 +01001020 qemu_put_buffer_async(rs->f, p, TARGET_PAGE_SIZE,
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001021 migrate_release_ram() &
Juan Quintela57273092017-03-20 22:25:28 +01001022 migration_in_postcopy());
Juan Quintela56e93d22015-05-07 19:33:31 +02001023 } else {
Juan Quintelace25d332017-03-15 11:00:51 +01001024 qemu_put_buffer(rs->f, p, TARGET_PAGE_SIZE);
Juan Quintela56e93d22015-05-07 19:33:31 +02001025 }
Juan Quintela93604472017-06-06 19:49:03 +02001026 ram_counters.transferred += TARGET_PAGE_SIZE;
Juan Quintela56e93d22015-05-07 19:33:31 +02001027 pages = 1;
Juan Quintela93604472017-06-06 19:49:03 +02001028 ram_counters.normal++;
Juan Quintela56e93d22015-05-07 19:33:31 +02001029 }
1030
1031 XBZRLE_cache_unlock();
1032
1033 return pages;
1034}
1035
Liang Lia7a9a882016-05-05 15:32:57 +08001036static int do_compress_ram_page(QEMUFile *f, RAMBlock *block,
1037 ram_addr_t offset)
Juan Quintela56e93d22015-05-07 19:33:31 +02001038{
Juan Quintela53518d92017-05-04 11:46:24 +02001039 RAMState *rs = ram_state;
Juan Quintela56e93d22015-05-07 19:33:31 +02001040 int bytes_sent, blen;
Liang Lia7a9a882016-05-05 15:32:57 +08001041 uint8_t *p = block->host + (offset & TARGET_PAGE_MASK);
Juan Quintela56e93d22015-05-07 19:33:31 +02001042
Juan Quintela2bf3aa82017-05-10 13:28:13 +02001043 bytes_sent = save_page_header(rs, f, block, offset |
Juan Quintela56e93d22015-05-07 19:33:31 +02001044 RAM_SAVE_FLAG_COMPRESS_PAGE);
Liang Lia7a9a882016-05-05 15:32:57 +08001045 blen = qemu_put_compression_data(f, p, TARGET_PAGE_SIZE,
Juan Quintela56e93d22015-05-07 19:33:31 +02001046 migrate_compress_level());
Liang Lib3be2892016-05-05 15:32:54 +08001047 if (blen < 0) {
1048 bytes_sent = 0;
1049 qemu_file_set_error(migrate_get_current()->to_dst_file, blen);
1050 error_report("compressed data failed!");
1051 } else {
1052 bytes_sent += blen;
Juan Quintela57273092017-03-20 22:25:28 +01001053 ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1);
Liang Lib3be2892016-05-05 15:32:54 +08001054 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001055
1056 return bytes_sent;
1057}
1058
Juan Quintelace25d332017-03-15 11:00:51 +01001059static void flush_compressed_data(RAMState *rs)
Juan Quintela56e93d22015-05-07 19:33:31 +02001060{
1061 int idx, len, thread_count;
1062
1063 if (!migrate_use_compression()) {
1064 return;
1065 }
1066 thread_count = migrate_compress_threads();
Liang Lia7a9a882016-05-05 15:32:57 +08001067
Liang Li0d9f9a52016-05-05 15:32:59 +08001068 qemu_mutex_lock(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02001069 for (idx = 0; idx < thread_count; idx++) {
Liang Lia7a9a882016-05-05 15:32:57 +08001070 while (!comp_param[idx].done) {
Liang Li0d9f9a52016-05-05 15:32:59 +08001071 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02001072 }
Liang Lia7a9a882016-05-05 15:32:57 +08001073 }
Liang Li0d9f9a52016-05-05 15:32:59 +08001074 qemu_mutex_unlock(&comp_done_lock);
Liang Lia7a9a882016-05-05 15:32:57 +08001075
1076 for (idx = 0; idx < thread_count; idx++) {
1077 qemu_mutex_lock(&comp_param[idx].mutex);
Liang Li90e56fb2016-05-05 15:32:56 +08001078 if (!comp_param[idx].quit) {
Juan Quintelace25d332017-03-15 11:00:51 +01001079 len = qemu_put_qemu_file(rs->f, comp_param[idx].file);
Juan Quintela93604472017-06-06 19:49:03 +02001080 ram_counters.transferred += len;
Juan Quintela56e93d22015-05-07 19:33:31 +02001081 }
Liang Lia7a9a882016-05-05 15:32:57 +08001082 qemu_mutex_unlock(&comp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02001083 }
1084}
1085
1086static inline void set_compress_params(CompressParam *param, RAMBlock *block,
1087 ram_addr_t offset)
1088{
1089 param->block = block;
1090 param->offset = offset;
1091}
1092
Juan Quintelace25d332017-03-15 11:00:51 +01001093static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block,
1094 ram_addr_t offset)
Juan Quintela56e93d22015-05-07 19:33:31 +02001095{
1096 int idx, thread_count, bytes_xmit = -1, pages = -1;
1097
1098 thread_count = migrate_compress_threads();
Liang Li0d9f9a52016-05-05 15:32:59 +08001099 qemu_mutex_lock(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02001100 while (true) {
1101 for (idx = 0; idx < thread_count; idx++) {
1102 if (comp_param[idx].done) {
Liang Lia7a9a882016-05-05 15:32:57 +08001103 comp_param[idx].done = false;
Juan Quintelace25d332017-03-15 11:00:51 +01001104 bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file);
Liang Lia7a9a882016-05-05 15:32:57 +08001105 qemu_mutex_lock(&comp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02001106 set_compress_params(&comp_param[idx], block, offset);
Liang Lia7a9a882016-05-05 15:32:57 +08001107 qemu_cond_signal(&comp_param[idx].cond);
1108 qemu_mutex_unlock(&comp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02001109 pages = 1;
Juan Quintela93604472017-06-06 19:49:03 +02001110 ram_counters.normal++;
1111 ram_counters.transferred += bytes_xmit;
Juan Quintela56e93d22015-05-07 19:33:31 +02001112 break;
1113 }
1114 }
1115 if (pages > 0) {
1116 break;
1117 } else {
Liang Li0d9f9a52016-05-05 15:32:59 +08001118 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02001119 }
1120 }
Liang Li0d9f9a52016-05-05 15:32:59 +08001121 qemu_mutex_unlock(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02001122
1123 return pages;
1124}
1125
1126/**
1127 * ram_save_compressed_page: compress the given page and send it to the stream
1128 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001129 * Returns the number of pages written.
Juan Quintela56e93d22015-05-07 19:33:31 +02001130 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001131 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +02001132 * @block: block that contains the page we want to send
1133 * @offset: offset inside the block for the page
1134 * @last_stage: if we are at the completion stage
Juan Quintela56e93d22015-05-07 19:33:31 +02001135 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001136static int ram_save_compressed_page(RAMState *rs, PageSearchStatus *pss,
1137 bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +02001138{
1139 int pages = -1;
Liang Lifc504382016-05-05 15:32:55 +08001140 uint64_t bytes_xmit = 0;
Juan Quintela56e93d22015-05-07 19:33:31 +02001141 uint8_t *p;
Liang Lifc504382016-05-05 15:32:55 +08001142 int ret, blen;
zhanghailianga08f6892016-01-15 11:37:44 +08001143 RAMBlock *block = pss->block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001144 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
Juan Quintela56e93d22015-05-07 19:33:31 +02001145
Dr. David Alan Gilbert2f68e392015-08-13 11:51:30 +01001146 p = block->host + offset;
Juan Quintela56e93d22015-05-07 19:33:31 +02001147
Juan Quintelace25d332017-03-15 11:00:51 +01001148 ret = ram_control_save_page(rs->f, block->offset,
Juan Quintela56e93d22015-05-07 19:33:31 +02001149 offset, TARGET_PAGE_SIZE, &bytes_xmit);
1150 if (bytes_xmit) {
Juan Quintela93604472017-06-06 19:49:03 +02001151 ram_counters.transferred += bytes_xmit;
Juan Quintela56e93d22015-05-07 19:33:31 +02001152 pages = 1;
1153 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001154 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
1155 if (ret != RAM_SAVE_CONTROL_DELAYED) {
1156 if (bytes_xmit > 0) {
Juan Quintela93604472017-06-06 19:49:03 +02001157 ram_counters.normal++;
Juan Quintela56e93d22015-05-07 19:33:31 +02001158 } else if (bytes_xmit == 0) {
Juan Quintela93604472017-06-06 19:49:03 +02001159 ram_counters.duplicate++;
Juan Quintela56e93d22015-05-07 19:33:31 +02001160 }
1161 }
1162 } else {
1163 /* When starting the process of a new block, the first page of
1164 * the block should be sent out before other pages in the same
1165 * block, and all the pages in last block should have been sent
1166 * out, keeping this order is important, because the 'cont' flag
1167 * is used to avoid resending the block name.
1168 */
Juan Quintela6f37bb82017-03-13 19:26:29 +01001169 if (block != rs->last_sent_block) {
Juan Quintelace25d332017-03-15 11:00:51 +01001170 flush_compressed_data(rs);
1171 pages = save_zero_page(rs, block, offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +02001172 if (pages == -1) {
Liang Lifc504382016-05-05 15:32:55 +08001173 /* Make sure the first page is sent out before other pages */
Juan Quintela2bf3aa82017-05-10 13:28:13 +02001174 bytes_xmit = save_page_header(rs, rs->f, block, offset |
Liang Lifc504382016-05-05 15:32:55 +08001175 RAM_SAVE_FLAG_COMPRESS_PAGE);
Juan Quintelace25d332017-03-15 11:00:51 +01001176 blen = qemu_put_compression_data(rs->f, p, TARGET_PAGE_SIZE,
Liang Lifc504382016-05-05 15:32:55 +08001177 migrate_compress_level());
1178 if (blen > 0) {
Juan Quintela93604472017-06-06 19:49:03 +02001179 ram_counters.transferred += bytes_xmit + blen;
1180 ram_counters.normal++;
Liang Lib3be2892016-05-05 15:32:54 +08001181 pages = 1;
Liang Lifc504382016-05-05 15:32:55 +08001182 } else {
Juan Quintelace25d332017-03-15 11:00:51 +01001183 qemu_file_set_error(rs->f, blen);
Liang Lifc504382016-05-05 15:32:55 +08001184 error_report("compressed data failed!");
Liang Lib3be2892016-05-05 15:32:54 +08001185 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001186 }
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001187 if (pages > 0) {
Juan Quintelaa935e302017-03-21 15:36:51 +01001188 ram_release_pages(block->idstr, offset, pages);
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001189 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001190 } else {
Juan Quintelace25d332017-03-15 11:00:51 +01001191 pages = save_zero_page(rs, block, offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +02001192 if (pages == -1) {
Juan Quintelace25d332017-03-15 11:00:51 +01001193 pages = compress_page_with_multi_thread(rs, block, offset);
Pavel Butsykin53f09a12017-02-03 18:23:20 +03001194 } else {
Juan Quintelaa935e302017-03-21 15:36:51 +01001195 ram_release_pages(block->idstr, offset, pages);
Juan Quintela56e93d22015-05-07 19:33:31 +02001196 }
1197 }
1198 }
1199
1200 return pages;
1201}
1202
Juan Quintela3d0684b2017-03-23 15:06:39 +01001203/**
1204 * find_dirty_block: find the next dirty page and update any state
1205 * associated with the search process.
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001206 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001207 * Returns if a page is found
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001208 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001209 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001210 * @pss: data about the state of the current dirty page scan
1211 * @again: set to false if the search has scanned the whole of RAM
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001212 */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001213static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001214{
Juan Quintelaf20e2862017-03-21 16:19:05 +01001215 pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
Juan Quintela6f37bb82017-03-13 19:26:29 +01001216 if (pss->complete_round && pss->block == rs->last_seen_block &&
Juan Quintelaa935e302017-03-21 15:36:51 +01001217 pss->page >= rs->last_page) {
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001218 /*
1219 * We've been once around the RAM and haven't found anything.
1220 * Give up.
1221 */
1222 *again = false;
1223 return false;
1224 }
Juan Quintelaa935e302017-03-21 15:36:51 +01001225 if ((pss->page << TARGET_PAGE_BITS) >= pss->block->used_length) {
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001226 /* Didn't find anything in this RAM Block */
Juan Quintelaa935e302017-03-21 15:36:51 +01001227 pss->page = 0;
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001228 pss->block = QLIST_NEXT_RCU(pss->block, next);
1229 if (!pss->block) {
1230 /* Hit the end of the list */
1231 pss->block = QLIST_FIRST_RCU(&ram_list.blocks);
1232 /* Flag that we've looped */
1233 pss->complete_round = true;
Juan Quintela6f37bb82017-03-13 19:26:29 +01001234 rs->ram_bulk_stage = false;
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001235 if (migrate_use_xbzrle()) {
1236 /* If xbzrle is on, stop using the data compression at this
1237 * point. In theory, xbzrle can do better than compression.
1238 */
Juan Quintelace25d332017-03-15 11:00:51 +01001239 flush_compressed_data(rs);
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001240 }
1241 }
1242 /* Didn't find anything this time, but try again on the new block */
1243 *again = true;
1244 return false;
1245 } else {
1246 /* Can go around again, but... */
1247 *again = true;
1248 /* We've found something so probably don't need to */
1249 return true;
1250 }
1251}
1252
Juan Quintela3d0684b2017-03-23 15:06:39 +01001253/**
1254 * unqueue_page: gets a page of the queue
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001255 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001256 * Helper for 'get_queued_page' - gets a page off the queue
1257 *
1258 * Returns the block of the page (or NULL if none available)
1259 *
Juan Quintelaec481c62017-03-20 22:12:40 +01001260 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001261 * @offset: used to return the offset within the RAMBlock
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001262 */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001263static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001264{
1265 RAMBlock *block = NULL;
1266
Juan Quintelaec481c62017-03-20 22:12:40 +01001267 qemu_mutex_lock(&rs->src_page_req_mutex);
1268 if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) {
1269 struct RAMSrcPageRequest *entry =
1270 QSIMPLEQ_FIRST(&rs->src_page_requests);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001271 block = entry->rb;
1272 *offset = entry->offset;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001273
1274 if (entry->len > TARGET_PAGE_SIZE) {
1275 entry->len -= TARGET_PAGE_SIZE;
1276 entry->offset += TARGET_PAGE_SIZE;
1277 } else {
1278 memory_region_unref(block->mr);
Juan Quintelaec481c62017-03-20 22:12:40 +01001279 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001280 g_free(entry);
1281 }
1282 }
Juan Quintelaec481c62017-03-20 22:12:40 +01001283 qemu_mutex_unlock(&rs->src_page_req_mutex);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001284
1285 return block;
1286}
1287
Juan Quintela3d0684b2017-03-23 15:06:39 +01001288/**
1289 * get_queued_page: unqueue a page from the postocpy requests
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001290 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001291 * Skips pages that are already sent (!dirty)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001292 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001293 * Returns if a queued page is found
1294 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001295 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001296 * @pss: data about the state of the current dirty page scan
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001297 */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001298static bool get_queued_page(RAMState *rs, PageSearchStatus *pss)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001299{
1300 RAMBlock *block;
1301 ram_addr_t offset;
1302 bool dirty;
1303
1304 do {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001305 block = unqueue_page(rs, &offset);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001306 /*
1307 * We're sending this page, and since it's postcopy nothing else
1308 * will dirty it, and we must make sure it doesn't get sent again
1309 * even if this queue request was received after the background
1310 * search already sent it.
1311 */
1312 if (block) {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001313 unsigned long page;
1314
Juan Quintela6b6712e2017-03-22 15:18:04 +01001315 page = offset >> TARGET_PAGE_BITS;
1316 dirty = test_bit(page, block->bmap);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001317 if (!dirty) {
Juan Quintela06b10682017-03-21 15:18:05 +01001318 trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset,
Juan Quintela6b6712e2017-03-22 15:18:04 +01001319 page, test_bit(page, block->unsentmap));
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001320 } else {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001321 trace_get_queued_page(block->idstr, (uint64_t)offset, page);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001322 }
1323 }
1324
1325 } while (block && !dirty);
1326
1327 if (block) {
1328 /*
1329 * As soon as we start servicing pages out of order, then we have
1330 * to kill the bulk stage, since the bulk stage assumes
1331 * in (migration_bitmap_find_and_reset_dirty) that every page is
1332 * dirty, that's no longer true.
1333 */
Juan Quintela6f37bb82017-03-13 19:26:29 +01001334 rs->ram_bulk_stage = false;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001335
1336 /*
1337 * We want the background search to continue from the queued page
1338 * since the guest is likely to want other pages near to the page
1339 * it just requested.
1340 */
1341 pss->block = block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001342 pss->page = offset >> TARGET_PAGE_BITS;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001343 }
1344
1345 return !!block;
1346}
1347
Juan Quintela56e93d22015-05-07 19:33:31 +02001348/**
Juan Quintela5e58f962017-04-03 22:06:54 +02001349 * migration_page_queue_free: drop any remaining pages in the ram
1350 * request queue
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001351 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001352 * It should be empty at the end anyway, but in error cases there may
1353 * be some left. in case that there is any page left, we drop it.
1354 *
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001355 */
Juan Quintela83c13382017-05-04 11:45:01 +02001356static void migration_page_queue_free(RAMState *rs)
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001357{
Juan Quintelaec481c62017-03-20 22:12:40 +01001358 struct RAMSrcPageRequest *mspr, *next_mspr;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001359 /* This queue generally should be empty - but in the case of a failed
1360 * migration might have some droppings in.
1361 */
1362 rcu_read_lock();
Juan Quintelaec481c62017-03-20 22:12:40 +01001363 QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001364 memory_region_unref(mspr->rb->mr);
Juan Quintelaec481c62017-03-20 22:12:40 +01001365 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001366 g_free(mspr);
1367 }
1368 rcu_read_unlock();
1369}
1370
1371/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001372 * ram_save_queue_pages: queue the page for transmission
1373 *
1374 * A request from postcopy destination for example.
1375 *
1376 * Returns zero on success or negative on error
1377 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001378 * @rbname: Name of the RAMBLock of the request. NULL means the
1379 * same that last one.
1380 * @start: starting address from the start of the RAMBlock
1381 * @len: length (in bytes) to send
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001382 */
Juan Quintela96506892017-03-14 18:41:03 +01001383int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len)
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001384{
1385 RAMBlock *ramblock;
Juan Quintela53518d92017-05-04 11:46:24 +02001386 RAMState *rs = ram_state;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001387
Juan Quintela93604472017-06-06 19:49:03 +02001388 ram_counters.postcopy_requests++;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001389 rcu_read_lock();
1390 if (!rbname) {
1391 /* Reuse last RAMBlock */
Juan Quintela68a098f2017-03-14 13:48:42 +01001392 ramblock = rs->last_req_rb;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001393
1394 if (!ramblock) {
1395 /*
1396 * Shouldn't happen, we can't reuse the last RAMBlock if
1397 * it's the 1st request.
1398 */
1399 error_report("ram_save_queue_pages no previous block");
1400 goto err;
1401 }
1402 } else {
1403 ramblock = qemu_ram_block_by_name(rbname);
1404
1405 if (!ramblock) {
1406 /* We shouldn't be asked for a non-existent RAMBlock */
1407 error_report("ram_save_queue_pages no block '%s'", rbname);
1408 goto err;
1409 }
Juan Quintela68a098f2017-03-14 13:48:42 +01001410 rs->last_req_rb = ramblock;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001411 }
1412 trace_ram_save_queue_pages(ramblock->idstr, start, len);
1413 if (start+len > ramblock->used_length) {
Juan Quintela9458ad62015-11-10 17:42:05 +01001414 error_report("%s request overrun start=" RAM_ADDR_FMT " len="
1415 RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT,
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001416 __func__, start, len, ramblock->used_length);
1417 goto err;
1418 }
1419
Juan Quintelaec481c62017-03-20 22:12:40 +01001420 struct RAMSrcPageRequest *new_entry =
1421 g_malloc0(sizeof(struct RAMSrcPageRequest));
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001422 new_entry->rb = ramblock;
1423 new_entry->offset = start;
1424 new_entry->len = len;
1425
1426 memory_region_ref(ramblock->mr);
Juan Quintelaec481c62017-03-20 22:12:40 +01001427 qemu_mutex_lock(&rs->src_page_req_mutex);
1428 QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req);
1429 qemu_mutex_unlock(&rs->src_page_req_mutex);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001430 rcu_read_unlock();
1431
1432 return 0;
1433
1434err:
1435 rcu_read_unlock();
1436 return -1;
1437}
1438
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001439/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001440 * ram_save_target_page: save one target page
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001441 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001442 * Returns the number of pages written
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001443 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001444 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001445 * @ms: current migration state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001446 * @pss: data about the page we want to send
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001447 * @last_stage: if we are at the completion stage
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001448 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001449static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,
Juan Quintelaf20e2862017-03-21 16:19:05 +01001450 bool last_stage)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001451{
1452 int res = 0;
1453
1454 /* Check the pages is dirty and if it is send it */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001455 if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
Juan Quintela6d358d92017-03-16 21:29:34 +01001456 /*
1457 * If xbzrle is on, stop using the data compression after first
1458 * round of migration even if compression is enabled. In theory,
1459 * xbzrle can do better than compression.
1460 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001461 if (migrate_use_compression() &&
1462 (rs->ram_bulk_stage || !migrate_use_xbzrle())) {
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001463 res = ram_save_compressed_page(rs, pss, last_stage);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001464 } else {
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001465 res = ram_save_page(rs, pss, last_stage);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001466 }
1467
1468 if (res < 0) {
1469 return res;
1470 }
Juan Quintela6b6712e2017-03-22 15:18:04 +01001471 if (pss->block->unsentmap) {
1472 clear_bit(pss->page, pss->block->unsentmap);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001473 }
1474 }
1475
1476 return res;
1477}
1478
1479/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001480 * ram_save_host_page: save a whole host page
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001481 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001482 * Starting at *offset send pages up to the end of the current host
1483 * page. It's valid for the initial offset to point into the middle of
1484 * a host page in which case the remainder of the hostpage is sent.
1485 * Only dirty target pages are sent. Note that the host page size may
1486 * be a huge page for this block.
Dr. David Alan Gilbert1eb3fc02017-05-17 17:58:09 +01001487 * The saving stops at the boundary of the used_length of the block
1488 * if the RAMBlock isn't a multiple of the host page size.
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001489 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001490 * Returns the number of pages written or negative on error
1491 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001492 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001493 * @ms: current migration state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001494 * @pss: data about the page we want to send
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001495 * @last_stage: if we are at the completion stage
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001496 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001497static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
Juan Quintelaf20e2862017-03-21 16:19:05 +01001498 bool last_stage)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001499{
1500 int tmppages, pages = 0;
Juan Quintelaa935e302017-03-21 15:36:51 +01001501 size_t pagesize_bits =
1502 qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
Dr. David Alan Gilbert4c011c32017-02-24 18:28:39 +00001503
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001504 do {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001505 tmppages = ram_save_target_page(rs, pss, last_stage);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001506 if (tmppages < 0) {
1507 return tmppages;
1508 }
1509
1510 pages += tmppages;
Juan Quintelaa935e302017-03-21 15:36:51 +01001511 pss->page++;
Dr. David Alan Gilbert1eb3fc02017-05-17 17:58:09 +01001512 } while ((pss->page & (pagesize_bits - 1)) &&
1513 offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS));
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001514
1515 /* The offset we leave with is the last one we looked at */
Juan Quintelaa935e302017-03-21 15:36:51 +01001516 pss->page--;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001517 return pages;
1518}
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001519
1520/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001521 * ram_find_and_save_block: finds a dirty page and sends it to f
Juan Quintela56e93d22015-05-07 19:33:31 +02001522 *
1523 * Called within an RCU critical section.
1524 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001525 * Returns the number of pages written where zero means no dirty pages
Juan Quintela56e93d22015-05-07 19:33:31 +02001526 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001527 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +02001528 * @last_stage: if we are at the completion stage
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001529 *
1530 * On systems where host-page-size > target-page-size it will send all the
1531 * pages in a host page that are dirty.
Juan Quintela56e93d22015-05-07 19:33:31 +02001532 */
1533
Juan Quintelace25d332017-03-15 11:00:51 +01001534static int ram_find_and_save_block(RAMState *rs, bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +02001535{
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +01001536 PageSearchStatus pss;
Juan Quintela56e93d22015-05-07 19:33:31 +02001537 int pages = 0;
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001538 bool again, found;
Juan Quintela56e93d22015-05-07 19:33:31 +02001539
Ashijeet Acharya0827b9e2017-02-08 19:58:45 +05301540 /* No dirty page as there is zero RAM */
1541 if (!ram_bytes_total()) {
1542 return pages;
1543 }
1544
Juan Quintela6f37bb82017-03-13 19:26:29 +01001545 pss.block = rs->last_seen_block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001546 pss.page = rs->last_page;
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +01001547 pss.complete_round = false;
1548
1549 if (!pss.block) {
1550 pss.block = QLIST_FIRST_RCU(&ram_list.blocks);
1551 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001552
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001553 do {
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001554 again = true;
Juan Quintelaf20e2862017-03-21 16:19:05 +01001555 found = get_queued_page(rs, &pss);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001556
1557 if (!found) {
1558 /* priority queue empty, so just search for something dirty */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001559 found = find_dirty_block(rs, &pss, &again);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001560 }
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001561
1562 if (found) {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001563 pages = ram_save_host_page(rs, &pss, last_stage);
Juan Quintela56e93d22015-05-07 19:33:31 +02001564 }
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001565 } while (!pages && again);
Juan Quintela56e93d22015-05-07 19:33:31 +02001566
Juan Quintela6f37bb82017-03-13 19:26:29 +01001567 rs->last_seen_block = pss.block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001568 rs->last_page = pss.page;
Juan Quintela56e93d22015-05-07 19:33:31 +02001569
1570 return pages;
1571}
1572
1573void acct_update_position(QEMUFile *f, size_t size, bool zero)
1574{
1575 uint64_t pages = size / TARGET_PAGE_SIZE;
Juan Quintelaf7ccd612017-03-13 20:30:21 +01001576
Juan Quintela56e93d22015-05-07 19:33:31 +02001577 if (zero) {
Juan Quintela93604472017-06-06 19:49:03 +02001578 ram_counters.duplicate += pages;
Juan Quintela56e93d22015-05-07 19:33:31 +02001579 } else {
Juan Quintela93604472017-06-06 19:49:03 +02001580 ram_counters.normal += pages;
1581 ram_counters.transferred += size;
Juan Quintela56e93d22015-05-07 19:33:31 +02001582 qemu_update_position(f, size);
1583 }
1584}
1585
Juan Quintela56e93d22015-05-07 19:33:31 +02001586uint64_t ram_bytes_total(void)
1587{
1588 RAMBlock *block;
1589 uint64_t total = 0;
1590
1591 rcu_read_lock();
Peter Xu99e15582017-05-12 12:17:39 +08001592 RAMBLOCK_FOREACH(block) {
Juan Quintela56e93d22015-05-07 19:33:31 +02001593 total += block->used_length;
Peter Xu99e15582017-05-12 12:17:39 +08001594 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001595 rcu_read_unlock();
1596 return total;
1597}
1598
Juan Quintelaf265e0e2017-06-28 11:52:27 +02001599static void xbzrle_load_setup(void)
Juan Quintela56e93d22015-05-07 19:33:31 +02001600{
Juan Quintelaf265e0e2017-06-28 11:52:27 +02001601 XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE);
Juan Quintela56e93d22015-05-07 19:33:31 +02001602}
1603
Juan Quintelaf265e0e2017-06-28 11:52:27 +02001604static void xbzrle_load_cleanup(void)
1605{
1606 g_free(XBZRLE.decoded_buf);
1607 XBZRLE.decoded_buf = NULL;
1608}
1609
Peter Xu7d7c96b2017-10-19 14:31:58 +08001610static void ram_state_cleanup(RAMState **rsp)
1611{
1612 migration_page_queue_free(*rsp);
1613 qemu_mutex_destroy(&(*rsp)->bitmap_mutex);
1614 qemu_mutex_destroy(&(*rsp)->src_page_req_mutex);
1615 g_free(*rsp);
1616 *rsp = NULL;
1617}
1618
Peter Xu84593a02017-10-19 14:31:59 +08001619static void xbzrle_cleanup(void)
1620{
1621 XBZRLE_cache_lock();
1622 if (XBZRLE.cache) {
1623 cache_fini(XBZRLE.cache);
1624 g_free(XBZRLE.encoded_buf);
1625 g_free(XBZRLE.current_buf);
1626 g_free(XBZRLE.zero_target_page);
1627 XBZRLE.cache = NULL;
1628 XBZRLE.encoded_buf = NULL;
1629 XBZRLE.current_buf = NULL;
1630 XBZRLE.zero_target_page = NULL;
1631 }
1632 XBZRLE_cache_unlock();
1633}
1634
Juan Quintelaf265e0e2017-06-28 11:52:27 +02001635static void ram_save_cleanup(void *opaque)
Juan Quintela56e93d22015-05-07 19:33:31 +02001636{
Juan Quintela53518d92017-05-04 11:46:24 +02001637 RAMState **rsp = opaque;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001638 RAMBlock *block;
Juan Quintelaeb859c52017-03-13 21:51:55 +01001639
Li Zhijian2ff64032015-07-02 20:18:05 +08001640 /* caller have hold iothread lock or is in a bh, so there is
1641 * no writing race against this migration_bitmap
1642 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001643 memory_global_dirty_log_stop();
1644
1645 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1646 g_free(block->bmap);
1647 block->bmap = NULL;
1648 g_free(block->unsentmap);
1649 block->unsentmap = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001650 }
1651
Peter Xu84593a02017-10-19 14:31:59 +08001652 xbzrle_cleanup();
Juan Quintelaf0afa332017-06-28 11:52:28 +02001653 compress_threads_save_cleanup();
Peter Xu7d7c96b2017-10-19 14:31:58 +08001654 ram_state_cleanup(rsp);
Juan Quintela56e93d22015-05-07 19:33:31 +02001655}
1656
Juan Quintela6f37bb82017-03-13 19:26:29 +01001657static void ram_state_reset(RAMState *rs)
Juan Quintela56e93d22015-05-07 19:33:31 +02001658{
Juan Quintela6f37bb82017-03-13 19:26:29 +01001659 rs->last_seen_block = NULL;
1660 rs->last_sent_block = NULL;
Juan Quintela269ace22017-03-21 15:23:31 +01001661 rs->last_page = 0;
Juan Quintela6f37bb82017-03-13 19:26:29 +01001662 rs->last_version = ram_list.version;
1663 rs->ram_bulk_stage = true;
Juan Quintela56e93d22015-05-07 19:33:31 +02001664}
1665
1666#define MAX_WAIT 50 /* ms, half buffered_file limit */
1667
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001668/*
1669 * 'expected' is the value you expect the bitmap mostly to be full
1670 * of; it won't bother printing lines that are all this value.
1671 * If 'todump' is null the migration bitmap is dumped.
1672 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001673void ram_debug_dump_bitmap(unsigned long *todump, bool expected,
1674 unsigned long pages)
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001675{
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001676 int64_t cur;
1677 int64_t linelen = 128;
1678 char linebuf[129];
1679
Juan Quintela6b6712e2017-03-22 15:18:04 +01001680 for (cur = 0; cur < pages; cur += linelen) {
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001681 int64_t curb;
1682 bool found = false;
1683 /*
1684 * Last line; catch the case where the line length
1685 * is longer than remaining ram
1686 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001687 if (cur + linelen > pages) {
1688 linelen = pages - cur;
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001689 }
1690 for (curb = 0; curb < linelen; curb++) {
1691 bool thisbit = test_bit(cur + curb, todump);
1692 linebuf[curb] = thisbit ? '1' : '.';
1693 found = found || (thisbit != expected);
1694 }
1695 if (found) {
1696 linebuf[curb] = '\0';
1697 fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf);
1698 }
1699 }
1700}
1701
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001702/* **** functions for postcopy ***** */
1703
Pavel Butsykinced1c612017-02-03 18:23:21 +03001704void ram_postcopy_migrated_memory_release(MigrationState *ms)
1705{
1706 struct RAMBlock *block;
Pavel Butsykinced1c612017-02-03 18:23:21 +03001707
Peter Xu99e15582017-05-12 12:17:39 +08001708 RAMBLOCK_FOREACH(block) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001709 unsigned long *bitmap = block->bmap;
1710 unsigned long range = block->used_length >> TARGET_PAGE_BITS;
1711 unsigned long run_start = find_next_zero_bit(bitmap, range, 0);
Pavel Butsykinced1c612017-02-03 18:23:21 +03001712
1713 while (run_start < range) {
1714 unsigned long run_end = find_next_bit(bitmap, range, run_start + 1);
Juan Quintelaaaa20642017-03-21 11:35:24 +01001715 ram_discard_range(block->idstr, run_start << TARGET_PAGE_BITS,
Pavel Butsykinced1c612017-02-03 18:23:21 +03001716 (run_end - run_start) << TARGET_PAGE_BITS);
1717 run_start = find_next_zero_bit(bitmap, range, run_end + 1);
1718 }
1719 }
1720}
1721
Juan Quintela3d0684b2017-03-23 15:06:39 +01001722/**
1723 * postcopy_send_discard_bm_ram: discard a RAMBlock
1724 *
1725 * Returns zero on success
1726 *
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001727 * Callback from postcopy_each_ram_send_discard for each RAMBlock
1728 * Note: At this point the 'unsentmap' is the processed bitmap combined
1729 * with the dirtymap; so a '1' means it's either dirty or unsent.
Juan Quintela3d0684b2017-03-23 15:06:39 +01001730 *
1731 * @ms: current migration state
1732 * @pds: state for postcopy
1733 * @start: RAMBlock starting page
1734 * @length: RAMBlock size
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001735 */
1736static int postcopy_send_discard_bm_ram(MigrationState *ms,
1737 PostcopyDiscardState *pds,
Juan Quintela6b6712e2017-03-22 15:18:04 +01001738 RAMBlock *block)
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001739{
Juan Quintela6b6712e2017-03-22 15:18:04 +01001740 unsigned long end = block->used_length >> TARGET_PAGE_BITS;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001741 unsigned long current;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001742 unsigned long *unsentmap = block->unsentmap;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001743
Juan Quintela6b6712e2017-03-22 15:18:04 +01001744 for (current = 0; current < end; ) {
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001745 unsigned long one = find_next_bit(unsentmap, end, current);
1746
1747 if (one <= end) {
1748 unsigned long zero = find_next_zero_bit(unsentmap, end, one + 1);
1749 unsigned long discard_length;
1750
1751 if (zero >= end) {
1752 discard_length = end - one;
1753 } else {
1754 discard_length = zero - one;
1755 }
Dr. David Alan Gilbertd688c622016-06-13 12:16:40 +01001756 if (discard_length) {
1757 postcopy_discard_send_range(ms, pds, one, discard_length);
1758 }
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001759 current = one + discard_length;
1760 } else {
1761 current = one;
1762 }
1763 }
1764
1765 return 0;
1766}
1767
Juan Quintela3d0684b2017-03-23 15:06:39 +01001768/**
1769 * postcopy_each_ram_send_discard: discard all RAMBlocks
1770 *
1771 * Returns 0 for success or negative for error
1772 *
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001773 * Utility for the outgoing postcopy code.
1774 * Calls postcopy_send_discard_bm_ram for each RAMBlock
1775 * passing it bitmap indexes and name.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001776 * (qemu_ram_foreach_block ends up passing unscaled lengths
1777 * which would mean postcopy code would have to deal with target page)
Juan Quintela3d0684b2017-03-23 15:06:39 +01001778 *
1779 * @ms: current migration state
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001780 */
1781static int postcopy_each_ram_send_discard(MigrationState *ms)
1782{
1783 struct RAMBlock *block;
1784 int ret;
1785
Peter Xu99e15582017-05-12 12:17:39 +08001786 RAMBLOCK_FOREACH(block) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001787 PostcopyDiscardState *pds =
1788 postcopy_discard_send_init(ms, block->idstr);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001789
1790 /*
1791 * Postcopy sends chunks of bitmap over the wire, but it
1792 * just needs indexes at this point, avoids it having
1793 * target page specific code.
1794 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001795 ret = postcopy_send_discard_bm_ram(ms, pds, block);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001796 postcopy_discard_send_finish(ms, pds);
1797 if (ret) {
1798 return ret;
1799 }
1800 }
1801
1802 return 0;
1803}
1804
Juan Quintela3d0684b2017-03-23 15:06:39 +01001805/**
1806 * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001807 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001808 * Helper for postcopy_chunk_hostpages; it's called twice to
1809 * canonicalize the two bitmaps, that are similar, but one is
1810 * inverted.
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001811 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001812 * Postcopy requires that all target pages in a hostpage are dirty or
1813 * clean, not a mix. This function canonicalizes the bitmaps.
1814 *
1815 * @ms: current migration state
1816 * @unsent_pass: if true we need to canonicalize partially unsent host pages
1817 * otherwise we need to canonicalize partially dirty host pages
1818 * @block: block that contains the page we want to canonicalize
1819 * @pds: state for postcopy
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001820 */
1821static void postcopy_chunk_hostpages_pass(MigrationState *ms, bool unsent_pass,
1822 RAMBlock *block,
1823 PostcopyDiscardState *pds)
1824{
Juan Quintela53518d92017-05-04 11:46:24 +02001825 RAMState *rs = ram_state;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001826 unsigned long *bitmap = block->bmap;
1827 unsigned long *unsentmap = block->unsentmap;
Dr. David Alan Gilbert29c59172017-02-24 18:28:31 +00001828 unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001829 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001830 unsigned long run_start;
1831
Dr. David Alan Gilbert29c59172017-02-24 18:28:31 +00001832 if (block->page_size == TARGET_PAGE_SIZE) {
1833 /* Easy case - TPS==HPS for a non-huge page RAMBlock */
1834 return;
1835 }
1836
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001837 if (unsent_pass) {
1838 /* Find a sent page */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001839 run_start = find_next_zero_bit(unsentmap, pages, 0);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001840 } else {
1841 /* Find a dirty page */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001842 run_start = find_next_bit(bitmap, pages, 0);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001843 }
1844
Juan Quintela6b6712e2017-03-22 15:18:04 +01001845 while (run_start < pages) {
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001846 bool do_fixup = false;
1847 unsigned long fixup_start_addr;
1848 unsigned long host_offset;
1849
1850 /*
1851 * If the start of this run of pages is in the middle of a host
1852 * page, then we need to fixup this host page.
1853 */
1854 host_offset = run_start % host_ratio;
1855 if (host_offset) {
1856 do_fixup = true;
1857 run_start -= host_offset;
1858 fixup_start_addr = run_start;
1859 /* For the next pass */
1860 run_start = run_start + host_ratio;
1861 } else {
1862 /* Find the end of this run */
1863 unsigned long run_end;
1864 if (unsent_pass) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001865 run_end = find_next_bit(unsentmap, pages, run_start + 1);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001866 } else {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001867 run_end = find_next_zero_bit(bitmap, pages, run_start + 1);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001868 }
1869 /*
1870 * If the end isn't at the start of a host page, then the
1871 * run doesn't finish at the end of a host page
1872 * and we need to discard.
1873 */
1874 host_offset = run_end % host_ratio;
1875 if (host_offset) {
1876 do_fixup = true;
1877 fixup_start_addr = run_end - host_offset;
1878 /*
1879 * This host page has gone, the next loop iteration starts
1880 * from after the fixup
1881 */
1882 run_start = fixup_start_addr + host_ratio;
1883 } else {
1884 /*
1885 * No discards on this iteration, next loop starts from
1886 * next sent/dirty page
1887 */
1888 run_start = run_end + 1;
1889 }
1890 }
1891
1892 if (do_fixup) {
1893 unsigned long page;
1894
1895 /* Tell the destination to discard this page */
1896 if (unsent_pass || !test_bit(fixup_start_addr, unsentmap)) {
1897 /* For the unsent_pass we:
1898 * discard partially sent pages
1899 * For the !unsent_pass (dirty) we:
1900 * discard partially dirty pages that were sent
1901 * (any partially sent pages were already discarded
1902 * by the previous unsent_pass)
1903 */
1904 postcopy_discard_send_range(ms, pds, fixup_start_addr,
1905 host_ratio);
1906 }
1907
1908 /* Clean up the bitmap */
1909 for (page = fixup_start_addr;
1910 page < fixup_start_addr + host_ratio; page++) {
1911 /* All pages in this host page are now not sent */
1912 set_bit(page, unsentmap);
1913
1914 /*
1915 * Remark them as dirty, updating the count for any pages
1916 * that weren't previously dirty.
1917 */
Juan Quintela0d8ec882017-03-13 21:21:41 +01001918 rs->migration_dirty_pages += !test_and_set_bit(page, bitmap);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001919 }
1920 }
1921
1922 if (unsent_pass) {
1923 /* Find the next sent page for the next iteration */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001924 run_start = find_next_zero_bit(unsentmap, pages, run_start);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001925 } else {
1926 /* Find the next dirty page for the next iteration */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001927 run_start = find_next_bit(bitmap, pages, run_start);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001928 }
1929 }
1930}
1931
Juan Quintela3d0684b2017-03-23 15:06:39 +01001932/**
1933 * postcopy_chuck_hostpages: discrad any partially sent host page
1934 *
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001935 * Utility for the outgoing postcopy code.
1936 *
1937 * Discard any partially sent host-page size chunks, mark any partially
Dr. David Alan Gilbert29c59172017-02-24 18:28:31 +00001938 * dirty host-page size chunks as all dirty. In this case the host-page
1939 * is the host-page for the particular RAMBlock, i.e. it might be a huge page
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001940 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001941 * Returns zero on success
1942 *
1943 * @ms: current migration state
Juan Quintela6b6712e2017-03-22 15:18:04 +01001944 * @block: block we want to work with
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001945 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001946static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block)
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001947{
Juan Quintela6b6712e2017-03-22 15:18:04 +01001948 PostcopyDiscardState *pds =
1949 postcopy_discard_send_init(ms, block->idstr);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001950
Juan Quintela6b6712e2017-03-22 15:18:04 +01001951 /* First pass: Discard all partially sent host pages */
1952 postcopy_chunk_hostpages_pass(ms, true, block, pds);
1953 /*
1954 * Second pass: Ensure that all partially dirty host pages are made
1955 * fully dirty.
1956 */
1957 postcopy_chunk_hostpages_pass(ms, false, block, pds);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001958
Juan Quintela6b6712e2017-03-22 15:18:04 +01001959 postcopy_discard_send_finish(ms, pds);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001960 return 0;
1961}
1962
Juan Quintela3d0684b2017-03-23 15:06:39 +01001963/**
1964 * ram_postcopy_send_discard_bitmap: transmit the discard bitmap
1965 *
1966 * Returns zero on success
1967 *
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001968 * Transmit the set of pages to be discarded after precopy to the target
1969 * these are pages that:
1970 * a) Have been previously transmitted but are now dirty again
1971 * b) Pages that have never been transmitted, this ensures that
1972 * any pages on the destination that have been mapped by background
1973 * tasks get discarded (transparent huge pages is the specific concern)
1974 * Hopefully this is pretty sparse
Juan Quintela3d0684b2017-03-23 15:06:39 +01001975 *
1976 * @ms: current migration state
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001977 */
1978int ram_postcopy_send_discard_bitmap(MigrationState *ms)
1979{
Juan Quintela53518d92017-05-04 11:46:24 +02001980 RAMState *rs = ram_state;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001981 RAMBlock *block;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001982 int ret;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001983
1984 rcu_read_lock();
1985
1986 /* This should be our last sync, the src is now paused */
Juan Quintelaeb859c52017-03-13 21:51:55 +01001987 migration_bitmap_sync(rs);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001988
Juan Quintela6b6712e2017-03-22 15:18:04 +01001989 /* Easiest way to make sure we don't resume in the middle of a host-page */
1990 rs->last_seen_block = NULL;
1991 rs->last_sent_block = NULL;
1992 rs->last_page = 0;
1993
1994 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1995 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
1996 unsigned long *bitmap = block->bmap;
1997 unsigned long *unsentmap = block->unsentmap;
1998
1999 if (!unsentmap) {
2000 /* We don't have a safe way to resize the sentmap, so
2001 * if the bitmap was resized it will be NULL at this
2002 * point.
2003 */
2004 error_report("migration ram resized during precopy phase");
2005 rcu_read_unlock();
2006 return -EINVAL;
2007 }
2008 /* Deal with TPS != HPS and huge pages */
2009 ret = postcopy_chunk_hostpages(ms, block);
2010 if (ret) {
2011 rcu_read_unlock();
2012 return ret;
2013 }
2014
2015 /*
2016 * Update the unsentmap to be unsentmap = unsentmap | dirty
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002017 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01002018 bitmap_or(unsentmap, unsentmap, bitmap, pages);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002019#ifdef DEBUG_POSTCOPY
Juan Quintela6b6712e2017-03-22 15:18:04 +01002020 ram_debug_dump_bitmap(unsentmap, true, pages);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002021#endif
Juan Quintela6b6712e2017-03-22 15:18:04 +01002022 }
2023 trace_ram_postcopy_send_discard_bitmap();
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002024
2025 ret = postcopy_each_ram_send_discard(ms);
2026 rcu_read_unlock();
2027
2028 return ret;
2029}
2030
Juan Quintela3d0684b2017-03-23 15:06:39 +01002031/**
2032 * ram_discard_range: discard dirtied pages at the beginning of postcopy
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002033 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01002034 * Returns zero on success
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002035 *
Juan Quintela36449152017-03-23 15:11:59 +01002036 * @rbname: name of the RAMBlock of the request. NULL means the
2037 * same that last one.
Juan Quintela3d0684b2017-03-23 15:06:39 +01002038 * @start: RAMBlock starting page
2039 * @length: RAMBlock size
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002040 */
Juan Quintelaaaa20642017-03-21 11:35:24 +01002041int ram_discard_range(const char *rbname, uint64_t start, size_t length)
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002042{
2043 int ret = -1;
2044
Juan Quintela36449152017-03-23 15:11:59 +01002045 trace_ram_discard_range(rbname, start, length);
Dr. David Alan Gilbertd3a50382017-02-24 18:28:32 +00002046
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002047 rcu_read_lock();
Juan Quintela36449152017-03-23 15:11:59 +01002048 RAMBlock *rb = qemu_ram_block_by_name(rbname);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002049
2050 if (!rb) {
Juan Quintela36449152017-03-23 15:11:59 +01002051 error_report("ram_discard_range: Failed to find block '%s'", rbname);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002052 goto err;
2053 }
2054
Alexey Perevalovf9494612017-10-05 14:13:20 +03002055 bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(),
2056 length >> qemu_target_page_bits());
Dr. David Alan Gilbertd3a50382017-02-24 18:28:32 +00002057 ret = ram_block_discard_range(rb, start, length);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00002058
2059err:
2060 rcu_read_unlock();
2061
2062 return ret;
2063}
2064
Peter Xu84593a02017-10-19 14:31:59 +08002065/*
2066 * For every allocation, we will try not to crash the VM if the
2067 * allocation failed.
2068 */
2069static int xbzrle_init(void)
2070{
2071 Error *local_err = NULL;
2072
2073 if (!migrate_use_xbzrle()) {
2074 return 0;
2075 }
2076
2077 XBZRLE_cache_lock();
2078
2079 XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE);
2080 if (!XBZRLE.zero_target_page) {
2081 error_report("%s: Error allocating zero page", __func__);
2082 goto err_out;
2083 }
2084
2085 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(),
2086 TARGET_PAGE_SIZE, &local_err);
2087 if (!XBZRLE.cache) {
2088 error_report_err(local_err);
2089 goto free_zero_page;
2090 }
2091
2092 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
2093 if (!XBZRLE.encoded_buf) {
2094 error_report("%s: Error allocating encoded_buf", __func__);
2095 goto free_cache;
2096 }
2097
2098 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
2099 if (!XBZRLE.current_buf) {
2100 error_report("%s: Error allocating current_buf", __func__);
2101 goto free_encoded_buf;
2102 }
2103
2104 /* We are all good */
2105 XBZRLE_cache_unlock();
2106 return 0;
2107
2108free_encoded_buf:
2109 g_free(XBZRLE.encoded_buf);
2110 XBZRLE.encoded_buf = NULL;
2111free_cache:
2112 cache_fini(XBZRLE.cache);
2113 XBZRLE.cache = NULL;
2114free_zero_page:
2115 g_free(XBZRLE.zero_target_page);
2116 XBZRLE.zero_target_page = NULL;
2117err_out:
2118 XBZRLE_cache_unlock();
2119 return -ENOMEM;
2120}
2121
Juan Quintela53518d92017-05-04 11:46:24 +02002122static int ram_state_init(RAMState **rsp)
Juan Quintela56e93d22015-05-07 19:33:31 +02002123{
Peter Xu7d00ee62017-10-19 14:31:57 +08002124 *rsp = g_try_new0(RAMState, 1);
2125
2126 if (!*rsp) {
2127 error_report("%s: Init ramstate fail", __func__);
2128 return -1;
2129 }
Juan Quintela53518d92017-05-04 11:46:24 +02002130
2131 qemu_mutex_init(&(*rsp)->bitmap_mutex);
2132 qemu_mutex_init(&(*rsp)->src_page_req_mutex);
2133 QSIMPLEQ_INIT(&(*rsp)->src_page_requests);
Juan Quintela56e93d22015-05-07 19:33:31 +02002134
Peter Xu7d00ee62017-10-19 14:31:57 +08002135 /*
2136 * Count the total number of pages used by ram blocks not including any
2137 * gaps due to alignment or unplugs.
2138 */
2139 (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
2140
2141 ram_state_reset(*rsp);
2142
2143 return 0;
2144}
2145
Peter Xud6eff5d2017-10-19 14:32:00 +08002146static void ram_list_init_bitmaps(void)
2147{
2148 RAMBlock *block;
2149 unsigned long pages;
2150
2151 /* Skip setting bitmap if there is no RAM */
2152 if (ram_bytes_total()) {
2153 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
2154 pages = block->max_length >> TARGET_PAGE_BITS;
2155 block->bmap = bitmap_new(pages);
2156 bitmap_set(block->bmap, 0, pages);
2157 if (migrate_postcopy_ram()) {
2158 block->unsentmap = bitmap_new(pages);
2159 bitmap_set(block->unsentmap, 0, pages);
2160 }
2161 }
2162 }
2163}
2164
2165static void ram_init_bitmaps(RAMState *rs)
2166{
2167 /* For memory_global_dirty_log_start below. */
2168 qemu_mutex_lock_iothread();
2169 qemu_mutex_lock_ramlist();
2170 rcu_read_lock();
2171
2172 ram_list_init_bitmaps();
2173 memory_global_dirty_log_start();
2174 migration_bitmap_sync(rs);
2175
2176 rcu_read_unlock();
2177 qemu_mutex_unlock_ramlist();
2178 qemu_mutex_unlock_iothread();
2179}
2180
Peter Xu7d00ee62017-10-19 14:31:57 +08002181static int ram_init_all(RAMState **rsp)
2182{
Peter Xu7d00ee62017-10-19 14:31:57 +08002183 if (ram_state_init(rsp)) {
2184 return -1;
2185 }
2186
Peter Xu84593a02017-10-19 14:31:59 +08002187 if (xbzrle_init()) {
2188 ram_state_cleanup(rsp);
2189 return -1;
Juan Quintela56e93d22015-05-07 19:33:31 +02002190 }
2191
Peter Xud6eff5d2017-10-19 14:32:00 +08002192 ram_init_bitmaps(*rsp);
zhanghailianga91246c2016-10-27 14:42:59 +08002193
2194 return 0;
2195}
2196
Juan Quintela3d0684b2017-03-23 15:06:39 +01002197/*
2198 * Each of ram_save_setup, ram_save_iterate and ram_save_complete has
zhanghailianga91246c2016-10-27 14:42:59 +08002199 * long-running RCU critical section. When rcu-reclaims in the code
2200 * start to become numerous it will be necessary to reduce the
2201 * granularity of these critical sections.
2202 */
2203
Juan Quintela3d0684b2017-03-23 15:06:39 +01002204/**
2205 * ram_save_setup: Setup RAM for migration
2206 *
2207 * Returns zero to indicate success and negative for error
2208 *
2209 * @f: QEMUFile where to send the data
2210 * @opaque: RAMState pointer
2211 */
zhanghailianga91246c2016-10-27 14:42:59 +08002212static int ram_save_setup(QEMUFile *f, void *opaque)
2213{
Juan Quintela53518d92017-05-04 11:46:24 +02002214 RAMState **rsp = opaque;
zhanghailianga91246c2016-10-27 14:42:59 +08002215 RAMBlock *block;
2216
2217 /* migration has already setup the bitmap, reuse it. */
2218 if (!migration_in_colo_state()) {
Peter Xu7d00ee62017-10-19 14:31:57 +08002219 if (ram_init_all(rsp) != 0) {
zhanghailianga91246c2016-10-27 14:42:59 +08002220 return -1;
Juan Quintela53518d92017-05-04 11:46:24 +02002221 }
zhanghailianga91246c2016-10-27 14:42:59 +08002222 }
Juan Quintela53518d92017-05-04 11:46:24 +02002223 (*rsp)->f = f;
zhanghailianga91246c2016-10-27 14:42:59 +08002224
2225 rcu_read_lock();
Juan Quintela56e93d22015-05-07 19:33:31 +02002226
2227 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
2228
Peter Xu99e15582017-05-12 12:17:39 +08002229 RAMBLOCK_FOREACH(block) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002230 qemu_put_byte(f, strlen(block->idstr));
2231 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
2232 qemu_put_be64(f, block->used_length);
Dr. David Alan Gilbertef08fb32017-02-24 18:28:30 +00002233 if (migrate_postcopy_ram() && block->page_size != qemu_host_page_size) {
2234 qemu_put_be64(f, block->page_size);
2235 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002236 }
2237
2238 rcu_read_unlock();
Juan Quintelaf0afa332017-06-28 11:52:28 +02002239 compress_threads_save_setup();
Juan Quintela56e93d22015-05-07 19:33:31 +02002240
2241 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
2242 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
2243
2244 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2245
2246 return 0;
2247}
2248
Juan Quintela3d0684b2017-03-23 15:06:39 +01002249/**
2250 * ram_save_iterate: iterative stage for migration
2251 *
2252 * Returns zero to indicate success and negative for error
2253 *
2254 * @f: QEMUFile where to send the data
2255 * @opaque: RAMState pointer
2256 */
Juan Quintela56e93d22015-05-07 19:33:31 +02002257static int ram_save_iterate(QEMUFile *f, void *opaque)
2258{
Juan Quintela53518d92017-05-04 11:46:24 +02002259 RAMState **temp = opaque;
2260 RAMState *rs = *temp;
Juan Quintela56e93d22015-05-07 19:33:31 +02002261 int ret;
2262 int i;
2263 int64_t t0;
Thomas Huth5c903082016-11-04 14:10:17 +01002264 int done = 0;
Juan Quintela56e93d22015-05-07 19:33:31 +02002265
2266 rcu_read_lock();
Juan Quintela6f37bb82017-03-13 19:26:29 +01002267 if (ram_list.version != rs->last_version) {
2268 ram_state_reset(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02002269 }
2270
2271 /* Read version before ram_list.blocks */
2272 smp_rmb();
2273
2274 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
2275
2276 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
2277 i = 0;
2278 while ((ret = qemu_file_rate_limit(f)) == 0) {
2279 int pages;
2280
Juan Quintelace25d332017-03-15 11:00:51 +01002281 pages = ram_find_and_save_block(rs, false);
Juan Quintela56e93d22015-05-07 19:33:31 +02002282 /* no more pages to sent */
2283 if (pages == 0) {
Thomas Huth5c903082016-11-04 14:10:17 +01002284 done = 1;
Juan Quintela56e93d22015-05-07 19:33:31 +02002285 break;
2286 }
Juan Quintela23b28c32017-03-13 20:51:34 +01002287 rs->iterations++;
Jason J. Herne070afca2015-09-08 13:12:35 -04002288
Juan Quintela56e93d22015-05-07 19:33:31 +02002289 /* we want to check in the 1st loop, just in case it was the 1st time
2290 and we had to sync the dirty bitmap.
2291 qemu_get_clock_ns() is a bit expensive, so we only check each some
2292 iterations
2293 */
2294 if ((i & 63) == 0) {
2295 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
2296 if (t1 > MAX_WAIT) {
Juan Quintela55c44462017-01-23 22:32:05 +01002297 trace_ram_save_iterate_big_wait(t1, i);
Juan Quintela56e93d22015-05-07 19:33:31 +02002298 break;
2299 }
2300 }
2301 i++;
2302 }
Juan Quintelace25d332017-03-15 11:00:51 +01002303 flush_compressed_data(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02002304 rcu_read_unlock();
2305
2306 /*
2307 * Must occur before EOS (or any QEMUFile operation)
2308 * because of RDMA protocol.
2309 */
2310 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
2311
2312 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
Juan Quintela93604472017-06-06 19:49:03 +02002313 ram_counters.transferred += 8;
Juan Quintela56e93d22015-05-07 19:33:31 +02002314
2315 ret = qemu_file_get_error(f);
2316 if (ret < 0) {
2317 return ret;
2318 }
2319
Thomas Huth5c903082016-11-04 14:10:17 +01002320 return done;
Juan Quintela56e93d22015-05-07 19:33:31 +02002321}
2322
Juan Quintela3d0684b2017-03-23 15:06:39 +01002323/**
2324 * ram_save_complete: function called to send the remaining amount of ram
2325 *
2326 * Returns zero to indicate success
2327 *
2328 * Called with iothread lock
2329 *
2330 * @f: QEMUFile where to send the data
2331 * @opaque: RAMState pointer
2332 */
Juan Quintela56e93d22015-05-07 19:33:31 +02002333static int ram_save_complete(QEMUFile *f, void *opaque)
2334{
Juan Quintela53518d92017-05-04 11:46:24 +02002335 RAMState **temp = opaque;
2336 RAMState *rs = *temp;
Juan Quintela6f37bb82017-03-13 19:26:29 +01002337
Juan Quintela56e93d22015-05-07 19:33:31 +02002338 rcu_read_lock();
2339
Juan Quintela57273092017-03-20 22:25:28 +01002340 if (!migration_in_postcopy()) {
Juan Quintela8d820d62017-03-13 19:35:50 +01002341 migration_bitmap_sync(rs);
Dr. David Alan Gilbert663e6c12015-11-05 18:11:13 +00002342 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002343
2344 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
2345
2346 /* try transferring iterative blocks of memory */
2347
2348 /* flush all remaining blocks regardless of rate limiting */
2349 while (true) {
2350 int pages;
2351
Juan Quintelace25d332017-03-15 11:00:51 +01002352 pages = ram_find_and_save_block(rs, !migration_in_colo_state());
Juan Quintela56e93d22015-05-07 19:33:31 +02002353 /* no more blocks to sent */
2354 if (pages == 0) {
2355 break;
2356 }
2357 }
2358
Juan Quintelace25d332017-03-15 11:00:51 +01002359 flush_compressed_data(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02002360 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
Juan Quintela56e93d22015-05-07 19:33:31 +02002361
2362 rcu_read_unlock();
Paolo Bonzinid09a6fd2015-07-09 08:47:58 +02002363
Juan Quintela56e93d22015-05-07 19:33:31 +02002364 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2365
2366 return 0;
2367}
2368
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002369static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
2370 uint64_t *non_postcopiable_pending,
2371 uint64_t *postcopiable_pending)
Juan Quintela56e93d22015-05-07 19:33:31 +02002372{
Juan Quintela53518d92017-05-04 11:46:24 +02002373 RAMState **temp = opaque;
2374 RAMState *rs = *temp;
Juan Quintela56e93d22015-05-07 19:33:31 +02002375 uint64_t remaining_size;
2376
Juan Quintela9edabd42017-03-14 12:02:16 +01002377 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
Juan Quintela56e93d22015-05-07 19:33:31 +02002378
Juan Quintela57273092017-03-20 22:25:28 +01002379 if (!migration_in_postcopy() &&
Dr. David Alan Gilbert663e6c12015-11-05 18:11:13 +00002380 remaining_size < max_size) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002381 qemu_mutex_lock_iothread();
2382 rcu_read_lock();
Juan Quintela8d820d62017-03-13 19:35:50 +01002383 migration_bitmap_sync(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02002384 rcu_read_unlock();
2385 qemu_mutex_unlock_iothread();
Juan Quintela9edabd42017-03-14 12:02:16 +01002386 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
Juan Quintela56e93d22015-05-07 19:33:31 +02002387 }
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002388
Vladimir Sementsov-Ogievskiy86e11672017-07-10 19:30:15 +03002389 if (migrate_postcopy_ram()) {
2390 /* We can do postcopy, and all the data is postcopiable */
2391 *postcopiable_pending += remaining_size;
2392 } else {
2393 *non_postcopiable_pending += remaining_size;
2394 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002395}
2396
2397static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
2398{
2399 unsigned int xh_len;
2400 int xh_flags;
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002401 uint8_t *loaded_data;
Juan Quintela56e93d22015-05-07 19:33:31 +02002402
Juan Quintela56e93d22015-05-07 19:33:31 +02002403 /* extract RLE header */
2404 xh_flags = qemu_get_byte(f);
2405 xh_len = qemu_get_be16(f);
2406
2407 if (xh_flags != ENCODING_FLAG_XBZRLE) {
2408 error_report("Failed to load XBZRLE page - wrong compression!");
2409 return -1;
2410 }
2411
2412 if (xh_len > TARGET_PAGE_SIZE) {
2413 error_report("Failed to load XBZRLE page - len overflow!");
2414 return -1;
2415 }
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002416 loaded_data = XBZRLE.decoded_buf;
Juan Quintela56e93d22015-05-07 19:33:31 +02002417 /* load data and decode */
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002418 /* it can change loaded_data to point to an internal buffer */
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002419 qemu_get_buffer_in_place(f, &loaded_data, xh_len);
Juan Quintela56e93d22015-05-07 19:33:31 +02002420
2421 /* decode RLE */
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002422 if (xbzrle_decode_buffer(loaded_data, xh_len, host,
Juan Quintela56e93d22015-05-07 19:33:31 +02002423 TARGET_PAGE_SIZE) == -1) {
2424 error_report("Failed to load XBZRLE page - decode error!");
2425 return -1;
2426 }
2427
2428 return 0;
2429}
2430
Juan Quintela3d0684b2017-03-23 15:06:39 +01002431/**
2432 * ram_block_from_stream: read a RAMBlock id from the migration stream
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002433 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01002434 * Must be called from within a rcu critical section.
2435 *
2436 * Returns a pointer from within the RCU-protected ram_list.
2437 *
2438 * @f: QEMUFile where to read the data from
2439 * @flags: Page flags (mostly to see if it's a continuation of previous block)
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002440 */
Juan Quintela3d0684b2017-03-23 15:06:39 +01002441static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags)
Juan Quintela56e93d22015-05-07 19:33:31 +02002442{
2443 static RAMBlock *block = NULL;
2444 char id[256];
2445 uint8_t len;
2446
2447 if (flags & RAM_SAVE_FLAG_CONTINUE) {
zhanghailiang4c4bad42016-01-15 11:37:41 +08002448 if (!block) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002449 error_report("Ack, bad migration stream!");
2450 return NULL;
2451 }
zhanghailiang4c4bad42016-01-15 11:37:41 +08002452 return block;
Juan Quintela56e93d22015-05-07 19:33:31 +02002453 }
2454
2455 len = qemu_get_byte(f);
2456 qemu_get_buffer(f, (uint8_t *)id, len);
2457 id[len] = 0;
2458
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002459 block = qemu_ram_block_by_name(id);
zhanghailiang4c4bad42016-01-15 11:37:41 +08002460 if (!block) {
2461 error_report("Can't find block %s", id);
2462 return NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002463 }
2464
zhanghailiang4c4bad42016-01-15 11:37:41 +08002465 return block;
2466}
2467
2468static inline void *host_from_ram_block_offset(RAMBlock *block,
2469 ram_addr_t offset)
2470{
2471 if (!offset_in_ramblock(block, offset)) {
2472 return NULL;
2473 }
2474
2475 return block->host + offset;
Juan Quintela56e93d22015-05-07 19:33:31 +02002476}
2477
Juan Quintela3d0684b2017-03-23 15:06:39 +01002478/**
2479 * ram_handle_compressed: handle the zero page case
2480 *
Juan Quintela56e93d22015-05-07 19:33:31 +02002481 * If a page (or a whole RDMA chunk) has been
2482 * determined to be zero, then zap it.
Juan Quintela3d0684b2017-03-23 15:06:39 +01002483 *
2484 * @host: host address for the zero page
2485 * @ch: what the page is filled from. We only support zero
2486 * @size: size of the zero page
Juan Quintela56e93d22015-05-07 19:33:31 +02002487 */
2488void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
2489{
2490 if (ch != 0 || !is_zero_range(host, size)) {
2491 memset(host, ch, size);
2492 }
2493}
2494
2495static void *do_data_decompress(void *opaque)
2496{
2497 DecompressParam *param = opaque;
2498 unsigned long pagesize;
Liang Li33d151f2016-05-05 15:32:58 +08002499 uint8_t *des;
2500 int len;
Juan Quintela56e93d22015-05-07 19:33:31 +02002501
Liang Li33d151f2016-05-05 15:32:58 +08002502 qemu_mutex_lock(&param->mutex);
Liang Li90e56fb2016-05-05 15:32:56 +08002503 while (!param->quit) {
Liang Li33d151f2016-05-05 15:32:58 +08002504 if (param->des) {
2505 des = param->des;
2506 len = param->len;
2507 param->des = 0;
2508 qemu_mutex_unlock(&param->mutex);
2509
Liang Li73a89122016-05-05 15:32:51 +08002510 pagesize = TARGET_PAGE_SIZE;
2511 /* uncompress() will return failed in some case, especially
2512 * when the page is dirted when doing the compression, it's
2513 * not a problem because the dirty page will be retransferred
2514 * and uncompress() won't break the data in other pages.
2515 */
Liang Li33d151f2016-05-05 15:32:58 +08002516 uncompress((Bytef *)des, &pagesize,
2517 (const Bytef *)param->compbuf, len);
Liang Li73a89122016-05-05 15:32:51 +08002518
Liang Li33d151f2016-05-05 15:32:58 +08002519 qemu_mutex_lock(&decomp_done_lock);
2520 param->done = true;
2521 qemu_cond_signal(&decomp_done_cond);
2522 qemu_mutex_unlock(&decomp_done_lock);
2523
2524 qemu_mutex_lock(&param->mutex);
2525 } else {
2526 qemu_cond_wait(&param->cond, &param->mutex);
2527 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002528 }
Liang Li33d151f2016-05-05 15:32:58 +08002529 qemu_mutex_unlock(&param->mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02002530
2531 return NULL;
2532}
2533
Liang Li5533b2e2016-05-05 15:32:52 +08002534static void wait_for_decompress_done(void)
2535{
2536 int idx, thread_count;
2537
2538 if (!migrate_use_compression()) {
2539 return;
2540 }
2541
2542 thread_count = migrate_decompress_threads();
2543 qemu_mutex_lock(&decomp_done_lock);
2544 for (idx = 0; idx < thread_count; idx++) {
2545 while (!decomp_param[idx].done) {
2546 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
2547 }
2548 }
2549 qemu_mutex_unlock(&decomp_done_lock);
2550}
2551
Juan Quintelaf0afa332017-06-28 11:52:28 +02002552static void compress_threads_load_setup(void)
Juan Quintela56e93d22015-05-07 19:33:31 +02002553{
2554 int i, thread_count;
2555
Juan Quintela3416ab52016-04-20 11:56:01 +02002556 if (!migrate_use_compression()) {
2557 return;
2558 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002559 thread_count = migrate_decompress_threads();
2560 decompress_threads = g_new0(QemuThread, thread_count);
2561 decomp_param = g_new0(DecompressParam, thread_count);
Liang Li73a89122016-05-05 15:32:51 +08002562 qemu_mutex_init(&decomp_done_lock);
2563 qemu_cond_init(&decomp_done_cond);
Juan Quintela56e93d22015-05-07 19:33:31 +02002564 for (i = 0; i < thread_count; i++) {
2565 qemu_mutex_init(&decomp_param[i].mutex);
2566 qemu_cond_init(&decomp_param[i].cond);
2567 decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
Liang Li73a89122016-05-05 15:32:51 +08002568 decomp_param[i].done = true;
Liang Li90e56fb2016-05-05 15:32:56 +08002569 decomp_param[i].quit = false;
Juan Quintela56e93d22015-05-07 19:33:31 +02002570 qemu_thread_create(decompress_threads + i, "decompress",
2571 do_data_decompress, decomp_param + i,
2572 QEMU_THREAD_JOINABLE);
2573 }
2574}
2575
Juan Quintelaf0afa332017-06-28 11:52:28 +02002576static void compress_threads_load_cleanup(void)
Juan Quintela56e93d22015-05-07 19:33:31 +02002577{
2578 int i, thread_count;
2579
Juan Quintela3416ab52016-04-20 11:56:01 +02002580 if (!migrate_use_compression()) {
2581 return;
2582 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002583 thread_count = migrate_decompress_threads();
2584 for (i = 0; i < thread_count; i++) {
2585 qemu_mutex_lock(&decomp_param[i].mutex);
Liang Li90e56fb2016-05-05 15:32:56 +08002586 decomp_param[i].quit = true;
Juan Quintela56e93d22015-05-07 19:33:31 +02002587 qemu_cond_signal(&decomp_param[i].cond);
2588 qemu_mutex_unlock(&decomp_param[i].mutex);
2589 }
2590 for (i = 0; i < thread_count; i++) {
2591 qemu_thread_join(decompress_threads + i);
2592 qemu_mutex_destroy(&decomp_param[i].mutex);
2593 qemu_cond_destroy(&decomp_param[i].cond);
2594 g_free(decomp_param[i].compbuf);
2595 }
2596 g_free(decompress_threads);
2597 g_free(decomp_param);
Juan Quintela56e93d22015-05-07 19:33:31 +02002598 decompress_threads = NULL;
2599 decomp_param = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002600}
2601
Dr. David Alan Gilbertc1bc6622015-12-16 11:47:38 +00002602static void decompress_data_with_multi_threads(QEMUFile *f,
Juan Quintela56e93d22015-05-07 19:33:31 +02002603 void *host, int len)
2604{
2605 int idx, thread_count;
2606
2607 thread_count = migrate_decompress_threads();
Liang Li73a89122016-05-05 15:32:51 +08002608 qemu_mutex_lock(&decomp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02002609 while (true) {
2610 for (idx = 0; idx < thread_count; idx++) {
Liang Li73a89122016-05-05 15:32:51 +08002611 if (decomp_param[idx].done) {
Liang Li33d151f2016-05-05 15:32:58 +08002612 decomp_param[idx].done = false;
2613 qemu_mutex_lock(&decomp_param[idx].mutex);
Dr. David Alan Gilbertc1bc6622015-12-16 11:47:38 +00002614 qemu_get_buffer(f, decomp_param[idx].compbuf, len);
Juan Quintela56e93d22015-05-07 19:33:31 +02002615 decomp_param[idx].des = host;
2616 decomp_param[idx].len = len;
Liang Li33d151f2016-05-05 15:32:58 +08002617 qemu_cond_signal(&decomp_param[idx].cond);
2618 qemu_mutex_unlock(&decomp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02002619 break;
2620 }
2621 }
2622 if (idx < thread_count) {
2623 break;
Liang Li73a89122016-05-05 15:32:51 +08002624 } else {
2625 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02002626 }
2627 }
Liang Li73a89122016-05-05 15:32:51 +08002628 qemu_mutex_unlock(&decomp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02002629}
2630
Juan Quintela3d0684b2017-03-23 15:06:39 +01002631/**
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002632 * ram_load_setup: Setup RAM for migration incoming side
2633 *
2634 * Returns zero to indicate success and negative for error
2635 *
2636 * @f: QEMUFile where to receive the data
2637 * @opaque: RAMState pointer
2638 */
2639static int ram_load_setup(QEMUFile *f, void *opaque)
2640{
2641 xbzrle_load_setup();
Juan Quintelaf0afa332017-06-28 11:52:28 +02002642 compress_threads_load_setup();
Alexey Perevalovf9494612017-10-05 14:13:20 +03002643 ramblock_recv_map_init();
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002644 return 0;
2645}
2646
2647static int ram_load_cleanup(void *opaque)
2648{
Alexey Perevalovf9494612017-10-05 14:13:20 +03002649 RAMBlock *rb;
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002650 xbzrle_load_cleanup();
Juan Quintelaf0afa332017-06-28 11:52:28 +02002651 compress_threads_load_cleanup();
Alexey Perevalovf9494612017-10-05 14:13:20 +03002652
2653 RAMBLOCK_FOREACH(rb) {
2654 g_free(rb->receivedmap);
2655 rb->receivedmap = NULL;
2656 }
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002657 return 0;
2658}
2659
2660/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01002661 * ram_postcopy_incoming_init: allocate postcopy data structures
2662 *
2663 * Returns 0 for success and negative if there was one error
2664 *
2665 * @mis: current migration incoming state
2666 *
2667 * Allocate data structures etc needed by incoming migration with
2668 * postcopy-ram. postcopy-ram's similarly names
2669 * postcopy_ram_incoming_init does the work.
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +00002670 */
2671int ram_postcopy_incoming_init(MigrationIncomingState *mis)
2672{
Juan Quintelab8c48992017-03-21 17:44:30 +01002673 unsigned long ram_pages = last_ram_page();
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +00002674
2675 return postcopy_ram_incoming_init(mis, ram_pages);
2676}
2677
Juan Quintela3d0684b2017-03-23 15:06:39 +01002678/**
2679 * ram_load_postcopy: load a page in postcopy case
2680 *
2681 * Returns 0 for success or -errno in case of error
2682 *
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002683 * Called in postcopy mode by ram_load().
2684 * rcu_read_lock is taken prior to this being called.
Juan Quintela3d0684b2017-03-23 15:06:39 +01002685 *
2686 * @f: QEMUFile where to send the data
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002687 */
2688static int ram_load_postcopy(QEMUFile *f)
2689{
2690 int flags = 0, ret = 0;
2691 bool place_needed = false;
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002692 bool matching_page_sizes = false;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002693 MigrationIncomingState *mis = migration_incoming_get_current();
2694 /* Temporary page that is later 'placed' */
2695 void *postcopy_host_page = postcopy_get_tmp_page(mis);
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002696 void *last_host = NULL;
Dr. David Alan Gilberta3b6ff62015-11-11 14:02:28 +00002697 bool all_zero = false;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002698
2699 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
2700 ram_addr_t addr;
2701 void *host = NULL;
2702 void *page_buffer = NULL;
2703 void *place_source = NULL;
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002704 RAMBlock *block = NULL;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002705 uint8_t ch;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002706
2707 addr = qemu_get_be64(f);
2708 flags = addr & ~TARGET_PAGE_MASK;
2709 addr &= TARGET_PAGE_MASK;
2710
2711 trace_ram_load_postcopy_loop((uint64_t)addr, flags);
2712 place_needed = false;
Juan Quintelabb890ed2017-04-28 09:39:55 +02002713 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE)) {
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002714 block = ram_block_from_stream(f, flags);
zhanghailiang4c4bad42016-01-15 11:37:41 +08002715
2716 host = host_from_ram_block_offset(block, addr);
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002717 if (!host) {
2718 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
2719 ret = -EINVAL;
2720 break;
2721 }
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002722 matching_page_sizes = block->page_size == TARGET_PAGE_SIZE;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002723 /*
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002724 * Postcopy requires that we place whole host pages atomically;
2725 * these may be huge pages for RAMBlocks that are backed by
2726 * hugetlbfs.
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002727 * To make it atomic, the data is read into a temporary page
2728 * that's moved into place later.
2729 * The migration protocol uses, possibly smaller, target-pages
2730 * however the source ensures it always sends all the components
2731 * of a host page in order.
2732 */
2733 page_buffer = postcopy_host_page +
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002734 ((uintptr_t)host & (block->page_size - 1));
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002735 /* If all TP are zero then we can optimise the place */
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002736 if (!((uintptr_t)host & (block->page_size - 1))) {
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002737 all_zero = true;
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002738 } else {
2739 /* not the 1st TP within the HP */
2740 if (host != (last_host + TARGET_PAGE_SIZE)) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +01002741 error_report("Non-sequential target page %p/%p",
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002742 host, last_host);
2743 ret = -EINVAL;
2744 break;
2745 }
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002746 }
2747
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002748
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002749 /*
2750 * If it's the last part of a host page then we place the host
2751 * page
2752 */
2753 place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) &
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002754 (block->page_size - 1)) == 0;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002755 place_source = postcopy_host_page;
2756 }
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002757 last_host = host;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002758
2759 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
Juan Quintelabb890ed2017-04-28 09:39:55 +02002760 case RAM_SAVE_FLAG_ZERO:
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002761 ch = qemu_get_byte(f);
2762 memset(page_buffer, ch, TARGET_PAGE_SIZE);
2763 if (ch) {
2764 all_zero = false;
2765 }
2766 break;
2767
2768 case RAM_SAVE_FLAG_PAGE:
2769 all_zero = false;
2770 if (!place_needed || !matching_page_sizes) {
2771 qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE);
2772 } else {
2773 /* Avoids the qemu_file copy during postcopy, which is
2774 * going to do a copy later; can only do it when we
2775 * do this read in one go (matching page sizes)
2776 */
2777 qemu_get_buffer_in_place(f, (uint8_t **)&place_source,
2778 TARGET_PAGE_SIZE);
2779 }
2780 break;
2781 case RAM_SAVE_FLAG_EOS:
2782 /* normal exit */
2783 break;
2784 default:
2785 error_report("Unknown combination of migration flags: %#x"
2786 " (postcopy mode)", flags);
2787 ret = -EINVAL;
2788 }
2789
2790 if (place_needed) {
2791 /* This gets called at the last target page in the host page */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002792 void *place_dest = host + TARGET_PAGE_SIZE - block->page_size;
2793
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002794 if (all_zero) {
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002795 ret = postcopy_place_page_zero(mis, place_dest,
Alexey Perevalov8be46202017-10-05 14:13:18 +03002796 block);
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002797 } else {
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002798 ret = postcopy_place_page(mis, place_dest,
Alexey Perevalov8be46202017-10-05 14:13:18 +03002799 place_source, block);
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002800 }
2801 }
2802 if (!ret) {
2803 ret = qemu_file_get_error(f);
2804 }
2805 }
2806
2807 return ret;
2808}
2809
Juan Quintela56e93d22015-05-07 19:33:31 +02002810static int ram_load(QEMUFile *f, void *opaque, int version_id)
2811{
Juan Quintelaedc60122016-11-02 12:40:46 +01002812 int flags = 0, ret = 0, invalid_flags = 0;
Juan Quintela56e93d22015-05-07 19:33:31 +02002813 static uint64_t seq_iter;
2814 int len = 0;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002815 /*
2816 * If system is running in postcopy mode, page inserts to host memory must
2817 * be atomic
2818 */
2819 bool postcopy_running = postcopy_state_get() >= POSTCOPY_INCOMING_LISTENING;
Dr. David Alan Gilbertef08fb32017-02-24 18:28:30 +00002820 /* ADVISE is earlier, it shows the source has the postcopy capability on */
2821 bool postcopy_advised = postcopy_state_get() >= POSTCOPY_INCOMING_ADVISE;
Juan Quintela56e93d22015-05-07 19:33:31 +02002822
2823 seq_iter++;
2824
2825 if (version_id != 4) {
2826 ret = -EINVAL;
2827 }
2828
Juan Quintelaedc60122016-11-02 12:40:46 +01002829 if (!migrate_use_compression()) {
2830 invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE;
2831 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002832 /* This RCU critical section can be very long running.
2833 * When RCU reclaims in the code start to become numerous,
2834 * it will be necessary to reduce the granularity of this
2835 * critical section.
2836 */
2837 rcu_read_lock();
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002838
2839 if (postcopy_running) {
2840 ret = ram_load_postcopy(f);
2841 }
2842
2843 while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002844 ram_addr_t addr, total_ram_bytes;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002845 void *host = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002846 uint8_t ch;
2847
2848 addr = qemu_get_be64(f);
2849 flags = addr & ~TARGET_PAGE_MASK;
2850 addr &= TARGET_PAGE_MASK;
2851
Juan Quintelaedc60122016-11-02 12:40:46 +01002852 if (flags & invalid_flags) {
2853 if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) {
2854 error_report("Received an unexpected compressed page");
2855 }
2856
2857 ret = -EINVAL;
2858 break;
2859 }
2860
Juan Quintelabb890ed2017-04-28 09:39:55 +02002861 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE |
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002862 RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) {
zhanghailiang4c4bad42016-01-15 11:37:41 +08002863 RAMBlock *block = ram_block_from_stream(f, flags);
2864
2865 host = host_from_ram_block_offset(block, addr);
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002866 if (!host) {
2867 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
2868 ret = -EINVAL;
2869 break;
2870 }
Alexey Perevalovf9494612017-10-05 14:13:20 +03002871 ramblock_recv_bitmap_set(block, host);
Dr. David Alan Gilbert1db9d8e2017-04-26 19:37:21 +01002872 trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002873 }
2874
Juan Quintela56e93d22015-05-07 19:33:31 +02002875 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
2876 case RAM_SAVE_FLAG_MEM_SIZE:
2877 /* Synchronize RAM block list */
2878 total_ram_bytes = addr;
2879 while (!ret && total_ram_bytes) {
2880 RAMBlock *block;
Juan Quintela56e93d22015-05-07 19:33:31 +02002881 char id[256];
2882 ram_addr_t length;
2883
2884 len = qemu_get_byte(f);
2885 qemu_get_buffer(f, (uint8_t *)id, len);
2886 id[len] = 0;
2887 length = qemu_get_be64(f);
2888
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002889 block = qemu_ram_block_by_name(id);
2890 if (block) {
2891 if (length != block->used_length) {
2892 Error *local_err = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002893
Gongleifa53a0e2016-05-10 10:04:59 +08002894 ret = qemu_ram_resize(block, length,
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002895 &local_err);
2896 if (local_err) {
2897 error_report_err(local_err);
Juan Quintela56e93d22015-05-07 19:33:31 +02002898 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002899 }
Dr. David Alan Gilbertef08fb32017-02-24 18:28:30 +00002900 /* For postcopy we need to check hugepage sizes match */
2901 if (postcopy_advised &&
2902 block->page_size != qemu_host_page_size) {
2903 uint64_t remote_page_size = qemu_get_be64(f);
2904 if (remote_page_size != block->page_size) {
2905 error_report("Mismatched RAM page size %s "
2906 "(local) %zd != %" PRId64,
2907 id, block->page_size,
2908 remote_page_size);
2909 ret = -EINVAL;
2910 }
2911 }
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002912 ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG,
2913 block->idstr);
2914 } else {
Juan Quintela56e93d22015-05-07 19:33:31 +02002915 error_report("Unknown ramblock \"%s\", cannot "
2916 "accept migration", id);
2917 ret = -EINVAL;
2918 }
2919
2920 total_ram_bytes -= length;
2921 }
2922 break;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002923
Juan Quintelabb890ed2017-04-28 09:39:55 +02002924 case RAM_SAVE_FLAG_ZERO:
Juan Quintela56e93d22015-05-07 19:33:31 +02002925 ch = qemu_get_byte(f);
2926 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
2927 break;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002928
Juan Quintela56e93d22015-05-07 19:33:31 +02002929 case RAM_SAVE_FLAG_PAGE:
Juan Quintela56e93d22015-05-07 19:33:31 +02002930 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
2931 break;
Juan Quintela56e93d22015-05-07 19:33:31 +02002932
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002933 case RAM_SAVE_FLAG_COMPRESS_PAGE:
Juan Quintela56e93d22015-05-07 19:33:31 +02002934 len = qemu_get_be32(f);
2935 if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) {
2936 error_report("Invalid compressed data length: %d", len);
2937 ret = -EINVAL;
2938 break;
2939 }
Dr. David Alan Gilbertc1bc6622015-12-16 11:47:38 +00002940 decompress_data_with_multi_threads(f, host, len);
Juan Quintela56e93d22015-05-07 19:33:31 +02002941 break;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002942
Juan Quintela56e93d22015-05-07 19:33:31 +02002943 case RAM_SAVE_FLAG_XBZRLE:
Juan Quintela56e93d22015-05-07 19:33:31 +02002944 if (load_xbzrle(f, addr, host) < 0) {
2945 error_report("Failed to decompress XBZRLE page at "
2946 RAM_ADDR_FMT, addr);
2947 ret = -EINVAL;
2948 break;
2949 }
2950 break;
2951 case RAM_SAVE_FLAG_EOS:
2952 /* normal exit */
2953 break;
2954 default:
2955 if (flags & RAM_SAVE_FLAG_HOOK) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +01002956 ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
Juan Quintela56e93d22015-05-07 19:33:31 +02002957 } else {
2958 error_report("Unknown combination of migration flags: %#x",
2959 flags);
2960 ret = -EINVAL;
2961 }
2962 }
2963 if (!ret) {
2964 ret = qemu_file_get_error(f);
2965 }
2966 }
2967
Liang Li5533b2e2016-05-05 15:32:52 +08002968 wait_for_decompress_done();
Juan Quintela56e93d22015-05-07 19:33:31 +02002969 rcu_read_unlock();
Juan Quintela55c44462017-01-23 22:32:05 +01002970 trace_ram_load_complete(ret, seq_iter);
Juan Quintela56e93d22015-05-07 19:33:31 +02002971 return ret;
2972}
2973
Vladimir Sementsov-Ogievskiyc6467622017-07-10 19:30:14 +03002974static bool ram_has_postcopy(void *opaque)
2975{
2976 return migrate_postcopy_ram();
2977}
2978
Juan Quintela56e93d22015-05-07 19:33:31 +02002979static SaveVMHandlers savevm_ram_handlers = {
Juan Quintela9907e842017-06-28 11:52:24 +02002980 .save_setup = ram_save_setup,
Juan Quintela56e93d22015-05-07 19:33:31 +02002981 .save_live_iterate = ram_save_iterate,
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00002982 .save_live_complete_postcopy = ram_save_complete,
Dr. David Alan Gilberta3e06c32015-11-05 18:10:41 +00002983 .save_live_complete_precopy = ram_save_complete,
Vladimir Sementsov-Ogievskiyc6467622017-07-10 19:30:14 +03002984 .has_postcopy = ram_has_postcopy,
Juan Quintela56e93d22015-05-07 19:33:31 +02002985 .save_live_pending = ram_save_pending,
2986 .load_state = ram_load,
Juan Quintelaf265e0e2017-06-28 11:52:27 +02002987 .save_cleanup = ram_save_cleanup,
2988 .load_setup = ram_load_setup,
2989 .load_cleanup = ram_load_cleanup,
Juan Quintela56e93d22015-05-07 19:33:31 +02002990};
2991
2992void ram_mig_init(void)
2993{
2994 qemu_mutex_init(&XBZRLE.lock);
Juan Quintela6f37bb82017-03-13 19:26:29 +01002995 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, &ram_state);
Juan Quintela56e93d22015-05-07 19:33:31 +02002996}