blob: 480248a00f07f8b5c909c02a84075790af0f0a53 [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 Quintela56e93d22015-05-07 19:33:31 +020045#include "trace.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020046#include "exec/ram_addr.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020047#include "qemu/rcu_queue.h"
zhanghailianga91246c2016-10-27 14:42:59 +080048#include "migration/colo.h"
Juan Quintela56e93d22015-05-07 19:33:31 +020049
Juan Quintela56e93d22015-05-07 19:33:31 +020050/***********************************************************/
51/* ram save/restore */
52
Juan Quintelabb890ed2017-04-28 09:39:55 +020053/* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it
54 * worked for pages that where filled with the same char. We switched
55 * it to only search for the zero value. And to avoid confusion with
56 * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it.
57 */
58
Juan Quintela56e93d22015-05-07 19:33:31 +020059#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
Juan Quintelabb890ed2017-04-28 09:39:55 +020060#define RAM_SAVE_FLAG_ZERO 0x02
Juan Quintela56e93d22015-05-07 19:33:31 +020061#define RAM_SAVE_FLAG_MEM_SIZE 0x04
62#define RAM_SAVE_FLAG_PAGE 0x08
63#define RAM_SAVE_FLAG_EOS 0x10
64#define RAM_SAVE_FLAG_CONTINUE 0x20
65#define RAM_SAVE_FLAG_XBZRLE 0x40
66/* 0x80 is reserved in migration.h start with 0x100 next */
67#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
68
Juan Quintela56e93d22015-05-07 19:33:31 +020069static inline bool is_zero_range(uint8_t *p, uint64_t size)
70{
Richard Hendersona1febc42016-08-29 11:46:14 -070071 return buffer_is_zero(p, size);
Juan Quintela56e93d22015-05-07 19:33:31 +020072}
73
Juan Quintela93604472017-06-06 19:49:03 +020074XBZRLECacheStats xbzrle_counters;
75
Juan Quintela56e93d22015-05-07 19:33:31 +020076/* struct contains XBZRLE cache and a static page
77 used by the compression */
78static struct {
79 /* buffer used for XBZRLE encoding */
80 uint8_t *encoded_buf;
81 /* buffer for storing page content */
82 uint8_t *current_buf;
83 /* Cache for XBZRLE, Protected by lock. */
84 PageCache *cache;
85 QemuMutex lock;
Juan Quintelac00e0922017-05-09 16:22:01 +020086 /* it will store a page full of zeros */
87 uint8_t *zero_target_page;
Juan Quintela56e93d22015-05-07 19:33:31 +020088} XBZRLE;
89
90/* buffer used for XBZRLE decoding */
91static uint8_t *xbzrle_decoded_buf;
92
93static void XBZRLE_cache_lock(void)
94{
95 if (migrate_use_xbzrle())
96 qemu_mutex_lock(&XBZRLE.lock);
97}
98
99static void XBZRLE_cache_unlock(void)
100{
101 if (migrate_use_xbzrle())
102 qemu_mutex_unlock(&XBZRLE.lock);
103}
104
Juan Quintela3d0684b2017-03-23 15:06:39 +0100105/**
106 * xbzrle_cache_resize: resize the xbzrle cache
107 *
108 * This function is called from qmp_migrate_set_cache_size in main
109 * thread, possibly while a migration is in progress. A running
110 * migration may be using the cache and might finish during this call,
111 * hence changes to the cache are protected by XBZRLE.lock().
112 *
113 * Returns the new_size or negative in case of error.
114 *
115 * @new_size: new cache size
Juan Quintela56e93d22015-05-07 19:33:31 +0200116 */
117int64_t xbzrle_cache_resize(int64_t new_size)
118{
119 PageCache *new_cache;
120 int64_t ret;
121
122 if (new_size < TARGET_PAGE_SIZE) {
123 return -1;
124 }
125
126 XBZRLE_cache_lock();
127
128 if (XBZRLE.cache != NULL) {
129 if (pow2floor(new_size) == migrate_xbzrle_cache_size()) {
130 goto out_new_size;
131 }
132 new_cache = cache_init(new_size / TARGET_PAGE_SIZE,
133 TARGET_PAGE_SIZE);
134 if (!new_cache) {
135 error_report("Error creating cache");
136 ret = -1;
137 goto out;
138 }
139
140 cache_fini(XBZRLE.cache);
141 XBZRLE.cache = new_cache;
142 }
143
144out_new_size:
145 ret = pow2floor(new_size);
146out:
147 XBZRLE_cache_unlock();
148 return ret;
149}
150
Juan Quintelaec481c62017-03-20 22:12:40 +0100151/*
152 * An outstanding page request, on the source, having been received
153 * and queued
154 */
155struct RAMSrcPageRequest {
156 RAMBlock *rb;
157 hwaddr offset;
158 hwaddr len;
159
160 QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req;
161};
162
Juan Quintela6f37bb82017-03-13 19:26:29 +0100163/* State of RAM for migration */
164struct RAMState {
Juan Quintela204b88b2017-03-15 09:16:57 +0100165 /* QEMUFile used for this migration */
166 QEMUFile *f;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100167 /* Last block that we have visited searching for dirty pages */
168 RAMBlock *last_seen_block;
169 /* Last block from where we have sent data */
170 RAMBlock *last_sent_block;
Juan Quintela269ace22017-03-21 15:23:31 +0100171 /* Last dirty target page we have sent */
172 ram_addr_t last_page;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100173 /* last ram version we have seen */
174 uint32_t last_version;
175 /* We are in the first round */
176 bool ram_bulk_stage;
Juan Quintela8d820d62017-03-13 19:35:50 +0100177 /* How many times we have dirty too many pages */
178 int dirty_rate_high_cnt;
Juan Quintelaf664da82017-03-13 19:44:57 +0100179 /* these variables are used for bitmap sync */
180 /* last time we did a full bitmap_sync */
181 int64_t time_last_bitmap_sync;
Juan Quintelaeac74152017-03-28 14:59:01 +0200182 /* bytes transferred at start_time */
Juan Quintelac4bdf0c2017-03-28 14:59:54 +0200183 uint64_t bytes_xfer_prev;
Juan Quintelaa66cd902017-03-28 15:02:43 +0200184 /* number of dirty pages since start_time */
Juan Quintela68908ed2017-03-28 15:05:53 +0200185 uint64_t num_dirty_pages_period;
Juan Quintelab5833fd2017-03-13 19:49:19 +0100186 /* xbzrle misses since the beginning of the period */
187 uint64_t xbzrle_cache_miss_prev;
Juan Quintela36040d92017-03-13 19:51:13 +0100188 /* number of iterations at the beginning of period */
189 uint64_t iterations_prev;
Juan Quintela23b28c32017-03-13 20:51:34 +0100190 /* Iterations since start */
191 uint64_t iterations;
Juan Quintela108cfae2017-03-13 21:38:09 +0100192 /* protects modification of the bitmap */
Juan Quintela93604472017-06-06 19:49:03 +0200193 uint64_t migration_dirty_pages;
194 /* number of dirty bits in the bitmap */
Juan Quintela108cfae2017-03-13 21:38:09 +0100195 QemuMutex bitmap_mutex;
Juan Quintela68a098f2017-03-14 13:48:42 +0100196 /* The RAMBlock used in the last src_page_requests */
197 RAMBlock *last_req_rb;
Juan Quintelaec481c62017-03-20 22:12:40 +0100198 /* Queue of outstanding page requests from the destination */
199 QemuMutex src_page_req_mutex;
200 QSIMPLEQ_HEAD(src_page_requests, RAMSrcPageRequest) src_page_requests;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100201};
202typedef struct RAMState RAMState;
203
Juan Quintela53518d92017-05-04 11:46:24 +0200204static RAMState *ram_state;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100205
Juan Quintela9edabd42017-03-14 12:02:16 +0100206uint64_t ram_bytes_remaining(void)
207{
Juan Quintela53518d92017-05-04 11:46:24 +0200208 return ram_state->migration_dirty_pages * TARGET_PAGE_SIZE;
Juan Quintela9edabd42017-03-14 12:02:16 +0100209}
210
Juan Quintela93604472017-06-06 19:49:03 +0200211MigrationStats ram_counters;
Juan Quintela96506892017-03-14 18:41:03 +0100212
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +0100213/* used by the search for pages to send */
214struct PageSearchStatus {
215 /* Current block being searched */
216 RAMBlock *block;
Juan Quintelaa935e302017-03-21 15:36:51 +0100217 /* Current page to search from */
218 unsigned long page;
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +0100219 /* Set once we wrap around */
220 bool complete_round;
221};
222typedef struct PageSearchStatus PageSearchStatus;
223
Juan Quintela56e93d22015-05-07 19:33:31 +0200224struct CompressParam {
Juan Quintela56e93d22015-05-07 19:33:31 +0200225 bool done;
Liang Li90e56fb2016-05-05 15:32:56 +0800226 bool quit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200227 QEMUFile *file;
228 QemuMutex mutex;
229 QemuCond cond;
230 RAMBlock *block;
231 ram_addr_t offset;
232};
233typedef struct CompressParam CompressParam;
234
235struct DecompressParam {
Liang Li73a89122016-05-05 15:32:51 +0800236 bool done;
Liang Li90e56fb2016-05-05 15:32:56 +0800237 bool quit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200238 QemuMutex mutex;
239 QemuCond cond;
240 void *des;
Peter Maydelld341d9f2016-01-22 15:09:21 +0000241 uint8_t *compbuf;
Juan Quintela56e93d22015-05-07 19:33:31 +0200242 int len;
243};
244typedef struct DecompressParam DecompressParam;
245
246static CompressParam *comp_param;
247static QemuThread *compress_threads;
248/* comp_done_cond is used to wake up the migration thread when
249 * one of the compression threads has finished the compression.
250 * comp_done_lock is used to co-work with comp_done_cond.
251 */
Liang Li0d9f9a52016-05-05 15:32:59 +0800252static QemuMutex comp_done_lock;
253static QemuCond comp_done_cond;
Juan Quintela56e93d22015-05-07 19:33:31 +0200254/* The empty QEMUFileOps will be used by file in CompressParam */
255static const QEMUFileOps empty_ops = { };
256
Juan Quintela56e93d22015-05-07 19:33:31 +0200257static DecompressParam *decomp_param;
258static QemuThread *decompress_threads;
Liang Li73a89122016-05-05 15:32:51 +0800259static QemuMutex decomp_done_lock;
260static QemuCond decomp_done_cond;
Juan Quintela56e93d22015-05-07 19:33:31 +0200261
Liang Lia7a9a882016-05-05 15:32:57 +0800262static int do_compress_ram_page(QEMUFile *f, RAMBlock *block,
263 ram_addr_t offset);
Juan Quintela56e93d22015-05-07 19:33:31 +0200264
265static void *do_data_compress(void *opaque)
266{
267 CompressParam *param = opaque;
Liang Lia7a9a882016-05-05 15:32:57 +0800268 RAMBlock *block;
269 ram_addr_t offset;
Juan Quintela56e93d22015-05-07 19:33:31 +0200270
Liang Lia7a9a882016-05-05 15:32:57 +0800271 qemu_mutex_lock(&param->mutex);
Liang Li90e56fb2016-05-05 15:32:56 +0800272 while (!param->quit) {
Liang Lia7a9a882016-05-05 15:32:57 +0800273 if (param->block) {
274 block = param->block;
275 offset = param->offset;
276 param->block = NULL;
277 qemu_mutex_unlock(&param->mutex);
278
279 do_compress_ram_page(param->file, block, offset);
280
Liang Li0d9f9a52016-05-05 15:32:59 +0800281 qemu_mutex_lock(&comp_done_lock);
Liang Lia7a9a882016-05-05 15:32:57 +0800282 param->done = true;
Liang Li0d9f9a52016-05-05 15:32:59 +0800283 qemu_cond_signal(&comp_done_cond);
284 qemu_mutex_unlock(&comp_done_lock);
Liang Lia7a9a882016-05-05 15:32:57 +0800285
286 qemu_mutex_lock(&param->mutex);
287 } else {
Juan Quintela56e93d22015-05-07 19:33:31 +0200288 qemu_cond_wait(&param->cond, &param->mutex);
289 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200290 }
Liang Lia7a9a882016-05-05 15:32:57 +0800291 qemu_mutex_unlock(&param->mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200292
293 return NULL;
294}
295
296static inline void terminate_compression_threads(void)
297{
298 int idx, thread_count;
299
300 thread_count = migrate_compress_threads();
Juan Quintela3d0684b2017-03-23 15:06:39 +0100301
Juan Quintela56e93d22015-05-07 19:33:31 +0200302 for (idx = 0; idx < thread_count; idx++) {
303 qemu_mutex_lock(&comp_param[idx].mutex);
Liang Li90e56fb2016-05-05 15:32:56 +0800304 comp_param[idx].quit = true;
Juan Quintela56e93d22015-05-07 19:33:31 +0200305 qemu_cond_signal(&comp_param[idx].cond);
306 qemu_mutex_unlock(&comp_param[idx].mutex);
307 }
308}
309
310void migrate_compress_threads_join(void)
311{
312 int i, thread_count;
313
314 if (!migrate_use_compression()) {
315 return;
316 }
317 terminate_compression_threads();
318 thread_count = migrate_compress_threads();
319 for (i = 0; i < thread_count; i++) {
320 qemu_thread_join(compress_threads + i);
321 qemu_fclose(comp_param[i].file);
322 qemu_mutex_destroy(&comp_param[i].mutex);
323 qemu_cond_destroy(&comp_param[i].cond);
324 }
Liang Li0d9f9a52016-05-05 15:32:59 +0800325 qemu_mutex_destroy(&comp_done_lock);
326 qemu_cond_destroy(&comp_done_cond);
Juan Quintela56e93d22015-05-07 19:33:31 +0200327 g_free(compress_threads);
328 g_free(comp_param);
Juan Quintela56e93d22015-05-07 19:33:31 +0200329 compress_threads = NULL;
330 comp_param = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +0200331}
332
333void migrate_compress_threads_create(void)
334{
335 int i, thread_count;
336
337 if (!migrate_use_compression()) {
338 return;
339 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200340 thread_count = migrate_compress_threads();
341 compress_threads = g_new0(QemuThread, thread_count);
342 comp_param = g_new0(CompressParam, thread_count);
Liang Li0d9f9a52016-05-05 15:32:59 +0800343 qemu_cond_init(&comp_done_cond);
344 qemu_mutex_init(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200345 for (i = 0; i < thread_count; i++) {
Cao jine110aa92016-07-29 15:10:31 +0800346 /* comp_param[i].file is just used as a dummy buffer to save data,
347 * set its ops to empty.
Juan Quintela56e93d22015-05-07 19:33:31 +0200348 */
349 comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops);
350 comp_param[i].done = true;
Liang Li90e56fb2016-05-05 15:32:56 +0800351 comp_param[i].quit = false;
Juan Quintela56e93d22015-05-07 19:33:31 +0200352 qemu_mutex_init(&comp_param[i].mutex);
353 qemu_cond_init(&comp_param[i].cond);
354 qemu_thread_create(compress_threads + i, "compress",
355 do_data_compress, comp_param + i,
356 QEMU_THREAD_JOINABLE);
357 }
358}
359
360/**
Juan Quintela3d0684b2017-03-23 15:06:39 +0100361 * save_page_header: write page header to wire
Juan Quintela56e93d22015-05-07 19:33:31 +0200362 *
363 * If this is the 1st block, it also writes the block identification
364 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100365 * Returns the number of bytes written
Juan Quintela56e93d22015-05-07 19:33:31 +0200366 *
367 * @f: QEMUFile where to send the data
368 * @block: block that contains the page we want to send
369 * @offset: offset inside the block for the page
370 * in the lower bits, it contains flags
371 */
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200372static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block,
373 ram_addr_t offset)
Juan Quintela56e93d22015-05-07 19:33:31 +0200374{
Liang Li9f5f3802015-07-13 17:34:10 +0800375 size_t size, len;
Juan Quintela56e93d22015-05-07 19:33:31 +0200376
Juan Quintela24795692017-03-21 11:45:01 +0100377 if (block == rs->last_sent_block) {
378 offset |= RAM_SAVE_FLAG_CONTINUE;
379 }
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200380 qemu_put_be64(f, offset);
Juan Quintela56e93d22015-05-07 19:33:31 +0200381 size = 8;
382
383 if (!(offset & RAM_SAVE_FLAG_CONTINUE)) {
Liang Li9f5f3802015-07-13 17:34:10 +0800384 len = strlen(block->idstr);
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200385 qemu_put_byte(f, len);
386 qemu_put_buffer(f, (uint8_t *)block->idstr, len);
Liang Li9f5f3802015-07-13 17:34:10 +0800387 size += 1 + len;
Juan Quintela24795692017-03-21 11:45:01 +0100388 rs->last_sent_block = block;
Juan Quintela56e93d22015-05-07 19:33:31 +0200389 }
390 return size;
391}
392
Juan Quintela3d0684b2017-03-23 15:06:39 +0100393/**
394 * mig_throttle_guest_down: throotle down the guest
395 *
396 * Reduce amount of guest cpu execution to hopefully slow down memory
397 * writes. If guest dirty memory rate is reduced below the rate at
398 * which we can transfer pages to the destination then we should be
399 * able to complete migration. Some workloads dirty memory way too
400 * fast and will not effectively converge, even with auto-converge.
Jason J. Herne070afca2015-09-08 13:12:35 -0400401 */
402static void mig_throttle_guest_down(void)
403{
404 MigrationState *s = migrate_get_current();
Daniel P. Berrange2594f562016-04-27 11:05:14 +0100405 uint64_t pct_initial = s->parameters.cpu_throttle_initial;
406 uint64_t pct_icrement = s->parameters.cpu_throttle_increment;
Jason J. Herne070afca2015-09-08 13:12:35 -0400407
408 /* We have not started throttling yet. Let's start it. */
409 if (!cpu_throttle_active()) {
410 cpu_throttle_set(pct_initial);
411 } else {
412 /* Throttling already on, just increase the rate */
413 cpu_throttle_set(cpu_throttle_get_percentage() + pct_icrement);
414 }
415}
416
Juan Quintela3d0684b2017-03-23 15:06:39 +0100417/**
418 * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache
419 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100420 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100421 * @current_addr: address for the zero page
422 *
423 * Update the xbzrle cache to reflect a page that's been sent as all 0.
Juan Quintela56e93d22015-05-07 19:33:31 +0200424 * The important thing is that a stale (not-yet-0'd) page be replaced
425 * by the new data.
426 * As a bonus, if the page wasn't in the cache it gets added so that
Juan Quintela3d0684b2017-03-23 15:06:39 +0100427 * when a small write is made into the 0'd page it gets XBZRLE sent.
Juan Quintela56e93d22015-05-07 19:33:31 +0200428 */
Juan Quintela6f37bb82017-03-13 19:26:29 +0100429static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr)
Juan Quintela56e93d22015-05-07 19:33:31 +0200430{
Juan Quintela6f37bb82017-03-13 19:26:29 +0100431 if (rs->ram_bulk_stage || !migrate_use_xbzrle()) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200432 return;
433 }
434
435 /* We don't care if this fails to allocate a new cache page
436 * as long as it updated an old one */
Juan Quintelac00e0922017-05-09 16:22:01 +0200437 cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page,
Juan Quintela93604472017-06-06 19:49:03 +0200438 ram_counters.dirty_sync_count);
Juan Quintela56e93d22015-05-07 19:33:31 +0200439}
440
441#define ENCODING_FLAG_XBZRLE 0x1
442
443/**
444 * save_xbzrle_page: compress and send current page
445 *
446 * Returns: 1 means that we wrote the page
447 * 0 means that page is identical to the one already sent
448 * -1 means that xbzrle would be longer than normal
449 *
Juan Quintela5a987732017-03-13 19:39:02 +0100450 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100451 * @current_data: pointer to the address of the page contents
452 * @current_addr: addr of the page
Juan Quintela56e93d22015-05-07 19:33:31 +0200453 * @block: block that contains the page we want to send
454 * @offset: offset inside the block for the page
455 * @last_stage: if we are at the completion stage
Juan Quintela56e93d22015-05-07 19:33:31 +0200456 */
Juan Quintela204b88b2017-03-15 09:16:57 +0100457static int save_xbzrle_page(RAMState *rs, uint8_t **current_data,
Juan Quintela56e93d22015-05-07 19:33:31 +0200458 ram_addr_t current_addr, RAMBlock *block,
Juan Quintela072c2512017-03-14 10:27:31 +0100459 ram_addr_t offset, bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +0200460{
461 int encoded_len = 0, bytes_xbzrle;
462 uint8_t *prev_cached_page;
463
Juan Quintela93604472017-06-06 19:49:03 +0200464 if (!cache_is_cached(XBZRLE.cache, current_addr,
465 ram_counters.dirty_sync_count)) {
466 xbzrle_counters.cache_miss++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200467 if (!last_stage) {
468 if (cache_insert(XBZRLE.cache, current_addr, *current_data,
Juan Quintela93604472017-06-06 19:49:03 +0200469 ram_counters.dirty_sync_count) == -1) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200470 return -1;
471 } else {
472 /* update *current_data when the page has been
473 inserted into cache */
474 *current_data = get_cached_data(XBZRLE.cache, current_addr);
475 }
476 }
477 return -1;
478 }
479
480 prev_cached_page = get_cached_data(XBZRLE.cache, current_addr);
481
482 /* save current buffer into memory */
483 memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE);
484
485 /* XBZRLE encoding (if there is no overflow) */
486 encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf,
487 TARGET_PAGE_SIZE, XBZRLE.encoded_buf,
488 TARGET_PAGE_SIZE);
489 if (encoded_len == 0) {
Juan Quintela55c44462017-01-23 22:32:05 +0100490 trace_save_xbzrle_page_skipping();
Juan Quintela56e93d22015-05-07 19:33:31 +0200491 return 0;
492 } else if (encoded_len == -1) {
Juan Quintela55c44462017-01-23 22:32:05 +0100493 trace_save_xbzrle_page_overflow();
Juan Quintela93604472017-06-06 19:49:03 +0200494 xbzrle_counters.overflow++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200495 /* update data in the cache */
496 if (!last_stage) {
497 memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE);
498 *current_data = prev_cached_page;
499 }
500 return -1;
501 }
502
503 /* we need to update the data in the cache, in order to get the same data */
504 if (!last_stage) {
505 memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE);
506 }
507
508 /* Send XBZRLE based compressed page */
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200509 bytes_xbzrle = save_page_header(rs, rs->f, block,
Juan Quintela204b88b2017-03-15 09:16:57 +0100510 offset | RAM_SAVE_FLAG_XBZRLE);
511 qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE);
512 qemu_put_be16(rs->f, encoded_len);
513 qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len);
Juan Quintela56e93d22015-05-07 19:33:31 +0200514 bytes_xbzrle += encoded_len + 1 + 2;
Juan Quintela93604472017-06-06 19:49:03 +0200515 xbzrle_counters.pages++;
516 xbzrle_counters.bytes += bytes_xbzrle;
517 ram_counters.transferred += bytes_xbzrle;
Juan Quintela56e93d22015-05-07 19:33:31 +0200518
519 return 1;
520}
521
Juan Quintela3d0684b2017-03-23 15:06:39 +0100522/**
523 * migration_bitmap_find_dirty: find the next dirty page from start
Dr. David Alan Gilbertf3f491f2015-11-05 18:11:01 +0000524 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100525 * Called with rcu_read_lock() to protect migration_bitmap
526 *
527 * Returns the byte offset within memory region of the start of a dirty page
528 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100529 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100530 * @rb: RAMBlock where to search for dirty pages
Juan Quintelaa935e302017-03-21 15:36:51 +0100531 * @start: page where we start the search
Dr. David Alan Gilbertf3f491f2015-11-05 18:11:01 +0000532 */
Juan Quintela56e93d22015-05-07 19:33:31 +0200533static inline
Juan Quintelaa935e302017-03-21 15:36:51 +0100534unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb,
Juan Quintelaf20e2862017-03-21 16:19:05 +0100535 unsigned long start)
Juan Quintela56e93d22015-05-07 19:33:31 +0200536{
Juan Quintela6b6712e2017-03-22 15:18:04 +0100537 unsigned long size = rb->used_length >> TARGET_PAGE_BITS;
538 unsigned long *bitmap = rb->bmap;
Juan Quintela56e93d22015-05-07 19:33:31 +0200539 unsigned long next;
540
Juan Quintela6b6712e2017-03-22 15:18:04 +0100541 if (rs->ram_bulk_stage && start > 0) {
542 next = start + 1;
Juan Quintela56e93d22015-05-07 19:33:31 +0200543 } else {
Juan Quintela6b6712e2017-03-22 15:18:04 +0100544 next = find_next_bit(bitmap, size, start);
Juan Quintela56e93d22015-05-07 19:33:31 +0200545 }
546
Juan Quintela6b6712e2017-03-22 15:18:04 +0100547 return next;
Juan Quintela56e93d22015-05-07 19:33:31 +0200548}
549
Juan Quintela06b10682017-03-21 15:18:05 +0100550static inline bool migration_bitmap_clear_dirty(RAMState *rs,
Juan Quintelaf20e2862017-03-21 16:19:05 +0100551 RAMBlock *rb,
552 unsigned long page)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000553{
554 bool ret;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000555
Juan Quintela6b6712e2017-03-22 15:18:04 +0100556 ret = test_and_clear_bit(page, rb->bmap);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000557
558 if (ret) {
Juan Quintela0d8ec882017-03-13 21:21:41 +0100559 rs->migration_dirty_pages--;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +0000560 }
561 return ret;
562}
563
Juan Quintela15440dd2017-03-21 09:35:04 +0100564static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb,
565 ram_addr_t start, ram_addr_t length)
Juan Quintela56e93d22015-05-07 19:33:31 +0200566{
Juan Quintela0d8ec882017-03-13 21:21:41 +0100567 rs->migration_dirty_pages +=
Juan Quintela6b6712e2017-03-22 15:18:04 +0100568 cpu_physical_memory_sync_dirty_bitmap(rb, start, length,
Juan Quintela0d8ec882017-03-13 21:21:41 +0100569 &rs->num_dirty_pages_period);
Juan Quintela56e93d22015-05-07 19:33:31 +0200570}
571
Juan Quintela3d0684b2017-03-23 15:06:39 +0100572/**
573 * ram_pagesize_summary: calculate all the pagesizes of a VM
574 *
575 * Returns a summary bitmap of the page sizes of all RAMBlocks
576 *
577 * For VMs with just normal pages this is equivalent to the host page
578 * size. If it's got some huge pages then it's the OR of all the
579 * different page sizes.
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +0000580 */
581uint64_t ram_pagesize_summary(void)
582{
583 RAMBlock *block;
584 uint64_t summary = 0;
585
Peter Xu99e15582017-05-12 12:17:39 +0800586 RAMBLOCK_FOREACH(block) {
Dr. David Alan Gilberte8ca1db2017-02-24 18:28:29 +0000587 summary |= block->page_size;
588 }
589
590 return summary;
591}
592
Juan Quintela8d820d62017-03-13 19:35:50 +0100593static void migration_bitmap_sync(RAMState *rs)
Juan Quintela56e93d22015-05-07 19:33:31 +0200594{
595 RAMBlock *block;
Juan Quintela56e93d22015-05-07 19:33:31 +0200596 int64_t end_time;
Juan Quintelac4bdf0c2017-03-28 14:59:54 +0200597 uint64_t bytes_xfer_now;
Juan Quintela56e93d22015-05-07 19:33:31 +0200598
Juan Quintela93604472017-06-06 19:49:03 +0200599 ram_counters.dirty_sync_count++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200600
Juan Quintelaf664da82017-03-13 19:44:57 +0100601 if (!rs->time_last_bitmap_sync) {
602 rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
Juan Quintela56e93d22015-05-07 19:33:31 +0200603 }
604
605 trace_migration_bitmap_sync_start();
Paolo Bonzini9c1f8f42016-09-22 16:08:31 +0200606 memory_global_dirty_log_sync();
Juan Quintela56e93d22015-05-07 19:33:31 +0200607
Juan Quintela108cfae2017-03-13 21:38:09 +0100608 qemu_mutex_lock(&rs->bitmap_mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200609 rcu_read_lock();
Peter Xu99e15582017-05-12 12:17:39 +0800610 RAMBLOCK_FOREACH(block) {
Juan Quintela15440dd2017-03-21 09:35:04 +0100611 migration_bitmap_sync_range(rs, block, 0, block->used_length);
Juan Quintela56e93d22015-05-07 19:33:31 +0200612 }
613 rcu_read_unlock();
Juan Quintela108cfae2017-03-13 21:38:09 +0100614 qemu_mutex_unlock(&rs->bitmap_mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200615
Juan Quintelaa66cd902017-03-28 15:02:43 +0200616 trace_migration_bitmap_sync_end(rs->num_dirty_pages_period);
Chao Fan1ffb5df2017-03-14 09:55:07 +0800617
Juan Quintela56e93d22015-05-07 19:33:31 +0200618 end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
619
620 /* more than 1 second = 1000 millisecons */
Juan Quintelaf664da82017-03-13 19:44:57 +0100621 if (end_time > rs->time_last_bitmap_sync + 1000) {
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100622 /* calculate period counters */
Juan Quintela93604472017-06-06 19:49:03 +0200623 ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100624 / (end_time - rs->time_last_bitmap_sync);
Juan Quintela93604472017-06-06 19:49:03 +0200625 bytes_xfer_now = ram_counters.transferred;
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100626
Juan Quintela56e93d22015-05-07 19:33:31 +0200627 if (migrate_auto_converge()) {
628 /* The following detection logic can be refined later. For now:
629 Check to see if the dirtied bytes is 50% more than the approx.
630 amount of bytes that just got transferred since the last time we
Jason J. Herne070afca2015-09-08 13:12:35 -0400631 were in this routine. If that happens twice, start or increase
632 throttling */
Jason J. Herne070afca2015-09-08 13:12:35 -0400633
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100634 if ((rs->num_dirty_pages_period * TARGET_PAGE_SIZE >
Juan Quintelaeac74152017-03-28 14:59:01 +0200635 (bytes_xfer_now - rs->bytes_xfer_prev) / 2) &&
Felipe Franciosib4a3c642017-05-24 17:10:03 +0100636 (++rs->dirty_rate_high_cnt >= 2)) {
Juan Quintela56e93d22015-05-07 19:33:31 +0200637 trace_migration_throttle();
Juan Quintela8d820d62017-03-13 19:35:50 +0100638 rs->dirty_rate_high_cnt = 0;
Jason J. Herne070afca2015-09-08 13:12:35 -0400639 mig_throttle_guest_down();
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100640 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200641 }
Jason J. Herne070afca2015-09-08 13:12:35 -0400642
Juan Quintela56e93d22015-05-07 19:33:31 +0200643 if (migrate_use_xbzrle()) {
Juan Quintela23b28c32017-03-13 20:51:34 +0100644 if (rs->iterations_prev != rs->iterations) {
Juan Quintela93604472017-06-06 19:49:03 +0200645 xbzrle_counters.cache_miss_rate =
646 (double)(xbzrle_counters.cache_miss -
Juan Quintelab5833fd2017-03-13 19:49:19 +0100647 rs->xbzrle_cache_miss_prev) /
Juan Quintela23b28c32017-03-13 20:51:34 +0100648 (rs->iterations - rs->iterations_prev);
Juan Quintela56e93d22015-05-07 19:33:31 +0200649 }
Juan Quintela23b28c32017-03-13 20:51:34 +0100650 rs->iterations_prev = rs->iterations;
Juan Quintela93604472017-06-06 19:49:03 +0200651 rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss;
Juan Quintela56e93d22015-05-07 19:33:31 +0200652 }
Felipe Franciosid693c6f2017-05-24 17:10:01 +0100653
654 /* reset period counters */
Juan Quintelaf664da82017-03-13 19:44:57 +0100655 rs->time_last_bitmap_sync = end_time;
Juan Quintelaa66cd902017-03-28 15:02:43 +0200656 rs->num_dirty_pages_period = 0;
Felipe Franciosid2a4d852017-05-24 17:10:02 +0100657 rs->bytes_xfer_prev = bytes_xfer_now;
Juan Quintela56e93d22015-05-07 19:33:31 +0200658 }
Dr. David Alan Gilbert4addcd42015-12-16 11:47:36 +0000659 if (migrate_use_events()) {
Juan Quintela93604472017-06-06 19:49:03 +0200660 qapi_event_send_migration_pass(ram_counters.dirty_sync_count, NULL);
Dr. David Alan Gilbert4addcd42015-12-16 11:47:36 +0000661 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200662}
663
664/**
Juan Quintela3d0684b2017-03-23 15:06:39 +0100665 * save_zero_page: send the zero page to the stream
Juan Quintela56e93d22015-05-07 19:33:31 +0200666 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100667 * Returns the number of pages written.
Juan Quintela56e93d22015-05-07 19:33:31 +0200668 *
Juan Quintelaf7ccd612017-03-13 20:30:21 +0100669 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +0200670 * @block: block that contains the page we want to send
671 * @offset: offset inside the block for the page
672 * @p: pointer to the page
Juan Quintela56e93d22015-05-07 19:33:31 +0200673 */
Juan Quintelace25d332017-03-15 11:00:51 +0100674static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
675 uint8_t *p)
Juan Quintela56e93d22015-05-07 19:33:31 +0200676{
677 int pages = -1;
678
679 if (is_zero_range(p, TARGET_PAGE_SIZE)) {
Juan Quintela93604472017-06-06 19:49:03 +0200680 ram_counters.duplicate++;
681 ram_counters.transferred +=
Juan Quintelabb890ed2017-04-28 09:39:55 +0200682 save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_ZERO);
Juan Quintelace25d332017-03-15 11:00:51 +0100683 qemu_put_byte(rs->f, 0);
Juan Quintela93604472017-06-06 19:49:03 +0200684 ram_counters.transferred += 1;
Juan Quintela56e93d22015-05-07 19:33:31 +0200685 pages = 1;
686 }
687
688 return pages;
689}
690
Juan Quintela57273092017-03-20 22:25:28 +0100691static void ram_release_pages(const char *rbname, uint64_t offset, int pages)
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300692{
Juan Quintela57273092017-03-20 22:25:28 +0100693 if (!migrate_release_ram() || !migration_in_postcopy()) {
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300694 return;
695 }
696
Juan Quintelaaaa20642017-03-21 11:35:24 +0100697 ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS);
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300698}
699
Juan Quintela56e93d22015-05-07 19:33:31 +0200700/**
Juan Quintela3d0684b2017-03-23 15:06:39 +0100701 * ram_save_page: send the given page to the stream
Juan Quintela56e93d22015-05-07 19:33:31 +0200702 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100703 * Returns the number of pages written.
Dr. David Alan Gilbert3fd3c4b2015-12-10 16:31:46 +0000704 * < 0 - error
705 * >=0 - Number of pages written - this might legally be 0
706 * if xbzrle noticed the page was the same.
Juan Quintela56e93d22015-05-07 19:33:31 +0200707 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100708 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +0200709 * @block: block that contains the page we want to send
710 * @offset: offset inside the block for the page
711 * @last_stage: if we are at the completion stage
Juan Quintela56e93d22015-05-07 19:33:31 +0200712 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +0100713static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +0200714{
715 int pages = -1;
716 uint64_t bytes_xmit;
717 ram_addr_t current_addr;
Juan Quintela56e93d22015-05-07 19:33:31 +0200718 uint8_t *p;
719 int ret;
720 bool send_async = true;
zhanghailianga08f6892016-01-15 11:37:44 +0800721 RAMBlock *block = pss->block;
Juan Quintelaa935e302017-03-21 15:36:51 +0100722 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
Juan Quintela56e93d22015-05-07 19:33:31 +0200723
Dr. David Alan Gilbert2f68e392015-08-13 11:51:30 +0100724 p = block->host + offset;
Dr. David Alan Gilbert1db9d8e2017-04-26 19:37:21 +0100725 trace_ram_save_page(block->idstr, (uint64_t)offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +0200726
727 /* In doubt sent page as normal */
728 bytes_xmit = 0;
Juan Quintelace25d332017-03-15 11:00:51 +0100729 ret = ram_control_save_page(rs->f, block->offset,
Juan Quintela56e93d22015-05-07 19:33:31 +0200730 offset, TARGET_PAGE_SIZE, &bytes_xmit);
731 if (bytes_xmit) {
Juan Quintela93604472017-06-06 19:49:03 +0200732 ram_counters.transferred += bytes_xmit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200733 pages = 1;
734 }
735
736 XBZRLE_cache_lock();
737
738 current_addr = block->offset + offset;
739
Juan Quintela56e93d22015-05-07 19:33:31 +0200740 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
741 if (ret != RAM_SAVE_CONTROL_DELAYED) {
742 if (bytes_xmit > 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200743 ram_counters.normal++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200744 } else if (bytes_xmit == 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200745 ram_counters.duplicate++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200746 }
747 }
748 } else {
Juan Quintelace25d332017-03-15 11:00:51 +0100749 pages = save_zero_page(rs, block, offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +0200750 if (pages > 0) {
751 /* Must let xbzrle know, otherwise a previous (now 0'd) cached
752 * page would be stale
753 */
Juan Quintela6f37bb82017-03-13 19:26:29 +0100754 xbzrle_cache_zero_page(rs, current_addr);
Juan Quintelaa935e302017-03-21 15:36:51 +0100755 ram_release_pages(block->idstr, offset, pages);
Juan Quintela6f37bb82017-03-13 19:26:29 +0100756 } else if (!rs->ram_bulk_stage &&
Juan Quintela57273092017-03-20 22:25:28 +0100757 !migration_in_postcopy() && migrate_use_xbzrle()) {
Juan Quintela204b88b2017-03-15 09:16:57 +0100758 pages = save_xbzrle_page(rs, &p, current_addr, block,
Juan Quintela072c2512017-03-14 10:27:31 +0100759 offset, last_stage);
Juan Quintela56e93d22015-05-07 19:33:31 +0200760 if (!last_stage) {
761 /* Can't send this cached data async, since the cache page
762 * might get updated before it gets to the wire
763 */
764 send_async = false;
765 }
766 }
767 }
768
769 /* XBZRLE overflow or normal page */
770 if (pages == -1) {
Juan Quintela93604472017-06-06 19:49:03 +0200771 ram_counters.transferred +=
772 save_page_header(rs, rs->f, block, offset | RAM_SAVE_FLAG_PAGE);
Juan Quintela56e93d22015-05-07 19:33:31 +0200773 if (send_async) {
Juan Quintelace25d332017-03-15 11:00:51 +0100774 qemu_put_buffer_async(rs->f, p, TARGET_PAGE_SIZE,
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300775 migrate_release_ram() &
Juan Quintela57273092017-03-20 22:25:28 +0100776 migration_in_postcopy());
Juan Quintela56e93d22015-05-07 19:33:31 +0200777 } else {
Juan Quintelace25d332017-03-15 11:00:51 +0100778 qemu_put_buffer(rs->f, p, TARGET_PAGE_SIZE);
Juan Quintela56e93d22015-05-07 19:33:31 +0200779 }
Juan Quintela93604472017-06-06 19:49:03 +0200780 ram_counters.transferred += TARGET_PAGE_SIZE;
Juan Quintela56e93d22015-05-07 19:33:31 +0200781 pages = 1;
Juan Quintela93604472017-06-06 19:49:03 +0200782 ram_counters.normal++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200783 }
784
785 XBZRLE_cache_unlock();
786
787 return pages;
788}
789
Liang Lia7a9a882016-05-05 15:32:57 +0800790static int do_compress_ram_page(QEMUFile *f, RAMBlock *block,
791 ram_addr_t offset)
Juan Quintela56e93d22015-05-07 19:33:31 +0200792{
Juan Quintela53518d92017-05-04 11:46:24 +0200793 RAMState *rs = ram_state;
Juan Quintela56e93d22015-05-07 19:33:31 +0200794 int bytes_sent, blen;
Liang Lia7a9a882016-05-05 15:32:57 +0800795 uint8_t *p = block->host + (offset & TARGET_PAGE_MASK);
Juan Quintela56e93d22015-05-07 19:33:31 +0200796
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200797 bytes_sent = save_page_header(rs, f, block, offset |
Juan Quintela56e93d22015-05-07 19:33:31 +0200798 RAM_SAVE_FLAG_COMPRESS_PAGE);
Liang Lia7a9a882016-05-05 15:32:57 +0800799 blen = qemu_put_compression_data(f, p, TARGET_PAGE_SIZE,
Juan Quintela56e93d22015-05-07 19:33:31 +0200800 migrate_compress_level());
Liang Lib3be2892016-05-05 15:32:54 +0800801 if (blen < 0) {
802 bytes_sent = 0;
803 qemu_file_set_error(migrate_get_current()->to_dst_file, blen);
804 error_report("compressed data failed!");
805 } else {
806 bytes_sent += blen;
Juan Quintela57273092017-03-20 22:25:28 +0100807 ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1);
Liang Lib3be2892016-05-05 15:32:54 +0800808 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200809
810 return bytes_sent;
811}
812
Juan Quintelace25d332017-03-15 11:00:51 +0100813static void flush_compressed_data(RAMState *rs)
Juan Quintela56e93d22015-05-07 19:33:31 +0200814{
815 int idx, len, thread_count;
816
817 if (!migrate_use_compression()) {
818 return;
819 }
820 thread_count = migrate_compress_threads();
Liang Lia7a9a882016-05-05 15:32:57 +0800821
Liang Li0d9f9a52016-05-05 15:32:59 +0800822 qemu_mutex_lock(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200823 for (idx = 0; idx < thread_count; idx++) {
Liang Lia7a9a882016-05-05 15:32:57 +0800824 while (!comp_param[idx].done) {
Liang Li0d9f9a52016-05-05 15:32:59 +0800825 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200826 }
Liang Lia7a9a882016-05-05 15:32:57 +0800827 }
Liang Li0d9f9a52016-05-05 15:32:59 +0800828 qemu_mutex_unlock(&comp_done_lock);
Liang Lia7a9a882016-05-05 15:32:57 +0800829
830 for (idx = 0; idx < thread_count; idx++) {
831 qemu_mutex_lock(&comp_param[idx].mutex);
Liang Li90e56fb2016-05-05 15:32:56 +0800832 if (!comp_param[idx].quit) {
Juan Quintelace25d332017-03-15 11:00:51 +0100833 len = qemu_put_qemu_file(rs->f, comp_param[idx].file);
Juan Quintela93604472017-06-06 19:49:03 +0200834 ram_counters.transferred += len;
Juan Quintela56e93d22015-05-07 19:33:31 +0200835 }
Liang Lia7a9a882016-05-05 15:32:57 +0800836 qemu_mutex_unlock(&comp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200837 }
838}
839
840static inline void set_compress_params(CompressParam *param, RAMBlock *block,
841 ram_addr_t offset)
842{
843 param->block = block;
844 param->offset = offset;
845}
846
Juan Quintelace25d332017-03-15 11:00:51 +0100847static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block,
848 ram_addr_t offset)
Juan Quintela56e93d22015-05-07 19:33:31 +0200849{
850 int idx, thread_count, bytes_xmit = -1, pages = -1;
851
852 thread_count = migrate_compress_threads();
Liang Li0d9f9a52016-05-05 15:32:59 +0800853 qemu_mutex_lock(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200854 while (true) {
855 for (idx = 0; idx < thread_count; idx++) {
856 if (comp_param[idx].done) {
Liang Lia7a9a882016-05-05 15:32:57 +0800857 comp_param[idx].done = false;
Juan Quintelace25d332017-03-15 11:00:51 +0100858 bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file);
Liang Lia7a9a882016-05-05 15:32:57 +0800859 qemu_mutex_lock(&comp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200860 set_compress_params(&comp_param[idx], block, offset);
Liang Lia7a9a882016-05-05 15:32:57 +0800861 qemu_cond_signal(&comp_param[idx].cond);
862 qemu_mutex_unlock(&comp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +0200863 pages = 1;
Juan Quintela93604472017-06-06 19:49:03 +0200864 ram_counters.normal++;
865 ram_counters.transferred += bytes_xmit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200866 break;
867 }
868 }
869 if (pages > 0) {
870 break;
871 } else {
Liang Li0d9f9a52016-05-05 15:32:59 +0800872 qemu_cond_wait(&comp_done_cond, &comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200873 }
874 }
Liang Li0d9f9a52016-05-05 15:32:59 +0800875 qemu_mutex_unlock(&comp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +0200876
877 return pages;
878}
879
880/**
881 * ram_save_compressed_page: compress the given page and send it to the stream
882 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100883 * Returns the number of pages written.
Juan Quintela56e93d22015-05-07 19:33:31 +0200884 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100885 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +0200886 * @block: block that contains the page we want to send
887 * @offset: offset inside the block for the page
888 * @last_stage: if we are at the completion stage
Juan Quintela56e93d22015-05-07 19:33:31 +0200889 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +0100890static int ram_save_compressed_page(RAMState *rs, PageSearchStatus *pss,
891 bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +0200892{
893 int pages = -1;
Liang Lifc504382016-05-05 15:32:55 +0800894 uint64_t bytes_xmit = 0;
Juan Quintela56e93d22015-05-07 19:33:31 +0200895 uint8_t *p;
Liang Lifc504382016-05-05 15:32:55 +0800896 int ret, blen;
zhanghailianga08f6892016-01-15 11:37:44 +0800897 RAMBlock *block = pss->block;
Juan Quintelaa935e302017-03-21 15:36:51 +0100898 ram_addr_t offset = pss->page << TARGET_PAGE_BITS;
Juan Quintela56e93d22015-05-07 19:33:31 +0200899
Dr. David Alan Gilbert2f68e392015-08-13 11:51:30 +0100900 p = block->host + offset;
Juan Quintela56e93d22015-05-07 19:33:31 +0200901
Juan Quintelace25d332017-03-15 11:00:51 +0100902 ret = ram_control_save_page(rs->f, block->offset,
Juan Quintela56e93d22015-05-07 19:33:31 +0200903 offset, TARGET_PAGE_SIZE, &bytes_xmit);
904 if (bytes_xmit) {
Juan Quintela93604472017-06-06 19:49:03 +0200905 ram_counters.transferred += bytes_xmit;
Juan Quintela56e93d22015-05-07 19:33:31 +0200906 pages = 1;
907 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200908 if (ret != RAM_SAVE_CONTROL_NOT_SUPP) {
909 if (ret != RAM_SAVE_CONTROL_DELAYED) {
910 if (bytes_xmit > 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200911 ram_counters.normal++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200912 } else if (bytes_xmit == 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200913 ram_counters.duplicate++;
Juan Quintela56e93d22015-05-07 19:33:31 +0200914 }
915 }
916 } else {
917 /* When starting the process of a new block, the first page of
918 * the block should be sent out before other pages in the same
919 * block, and all the pages in last block should have been sent
920 * out, keeping this order is important, because the 'cont' flag
921 * is used to avoid resending the block name.
922 */
Juan Quintela6f37bb82017-03-13 19:26:29 +0100923 if (block != rs->last_sent_block) {
Juan Quintelace25d332017-03-15 11:00:51 +0100924 flush_compressed_data(rs);
925 pages = save_zero_page(rs, block, offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +0200926 if (pages == -1) {
Liang Lifc504382016-05-05 15:32:55 +0800927 /* Make sure the first page is sent out before other pages */
Juan Quintela2bf3aa82017-05-10 13:28:13 +0200928 bytes_xmit = save_page_header(rs, rs->f, block, offset |
Liang Lifc504382016-05-05 15:32:55 +0800929 RAM_SAVE_FLAG_COMPRESS_PAGE);
Juan Quintelace25d332017-03-15 11:00:51 +0100930 blen = qemu_put_compression_data(rs->f, p, TARGET_PAGE_SIZE,
Liang Lifc504382016-05-05 15:32:55 +0800931 migrate_compress_level());
932 if (blen > 0) {
Juan Quintela93604472017-06-06 19:49:03 +0200933 ram_counters.transferred += bytes_xmit + blen;
934 ram_counters.normal++;
Liang Lib3be2892016-05-05 15:32:54 +0800935 pages = 1;
Liang Lifc504382016-05-05 15:32:55 +0800936 } else {
Juan Quintelace25d332017-03-15 11:00:51 +0100937 qemu_file_set_error(rs->f, blen);
Liang Lifc504382016-05-05 15:32:55 +0800938 error_report("compressed data failed!");
Liang Lib3be2892016-05-05 15:32:54 +0800939 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200940 }
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300941 if (pages > 0) {
Juan Quintelaa935e302017-03-21 15:36:51 +0100942 ram_release_pages(block->idstr, offset, pages);
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300943 }
Juan Quintela56e93d22015-05-07 19:33:31 +0200944 } else {
Juan Quintelace25d332017-03-15 11:00:51 +0100945 pages = save_zero_page(rs, block, offset, p);
Juan Quintela56e93d22015-05-07 19:33:31 +0200946 if (pages == -1) {
Juan Quintelace25d332017-03-15 11:00:51 +0100947 pages = compress_page_with_multi_thread(rs, block, offset);
Pavel Butsykin53f09a12017-02-03 18:23:20 +0300948 } else {
Juan Quintelaa935e302017-03-21 15:36:51 +0100949 ram_release_pages(block->idstr, offset, pages);
Juan Quintela56e93d22015-05-07 19:33:31 +0200950 }
951 }
952 }
953
954 return pages;
955}
956
Juan Quintela3d0684b2017-03-23 15:06:39 +0100957/**
958 * find_dirty_block: find the next dirty page and update any state
959 * associated with the search process.
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100960 *
Juan Quintela3d0684b2017-03-23 15:06:39 +0100961 * Returns if a page is found
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100962 *
Juan Quintela6f37bb82017-03-13 19:26:29 +0100963 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +0100964 * @pss: data about the state of the current dirty page scan
965 * @again: set to false if the search has scanned the whole of RAM
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100966 */
Juan Quintelaf20e2862017-03-21 16:19:05 +0100967static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again)
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100968{
Juan Quintelaf20e2862017-03-21 16:19:05 +0100969 pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page);
Juan Quintela6f37bb82017-03-13 19:26:29 +0100970 if (pss->complete_round && pss->block == rs->last_seen_block &&
Juan Quintelaa935e302017-03-21 15:36:51 +0100971 pss->page >= rs->last_page) {
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100972 /*
973 * We've been once around the RAM and haven't found anything.
974 * Give up.
975 */
976 *again = false;
977 return false;
978 }
Juan Quintelaa935e302017-03-21 15:36:51 +0100979 if ((pss->page << TARGET_PAGE_BITS) >= pss->block->used_length) {
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100980 /* Didn't find anything in this RAM Block */
Juan Quintelaa935e302017-03-21 15:36:51 +0100981 pss->page = 0;
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100982 pss->block = QLIST_NEXT_RCU(pss->block, next);
983 if (!pss->block) {
984 /* Hit the end of the list */
985 pss->block = QLIST_FIRST_RCU(&ram_list.blocks);
986 /* Flag that we've looped */
987 pss->complete_round = true;
Juan Quintela6f37bb82017-03-13 19:26:29 +0100988 rs->ram_bulk_stage = false;
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100989 if (migrate_use_xbzrle()) {
990 /* If xbzrle is on, stop using the data compression at this
991 * point. In theory, xbzrle can do better than compression.
992 */
Juan Quintelace25d332017-03-15 11:00:51 +0100993 flush_compressed_data(rs);
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +0100994 }
995 }
996 /* Didn't find anything this time, but try again on the new block */
997 *again = true;
998 return false;
999 } else {
1000 /* Can go around again, but... */
1001 *again = true;
1002 /* We've found something so probably don't need to */
1003 return true;
1004 }
1005}
1006
Juan Quintela3d0684b2017-03-23 15:06:39 +01001007/**
1008 * unqueue_page: gets a page of the queue
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001009 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001010 * Helper for 'get_queued_page' - gets a page off the queue
1011 *
1012 * Returns the block of the page (or NULL if none available)
1013 *
Juan Quintelaec481c62017-03-20 22:12:40 +01001014 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001015 * @offset: used to return the offset within the RAMBlock
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001016 */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001017static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001018{
1019 RAMBlock *block = NULL;
1020
Juan Quintelaec481c62017-03-20 22:12:40 +01001021 qemu_mutex_lock(&rs->src_page_req_mutex);
1022 if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) {
1023 struct RAMSrcPageRequest *entry =
1024 QSIMPLEQ_FIRST(&rs->src_page_requests);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001025 block = entry->rb;
1026 *offset = entry->offset;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001027
1028 if (entry->len > TARGET_PAGE_SIZE) {
1029 entry->len -= TARGET_PAGE_SIZE;
1030 entry->offset += TARGET_PAGE_SIZE;
1031 } else {
1032 memory_region_unref(block->mr);
Juan Quintelaec481c62017-03-20 22:12:40 +01001033 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001034 g_free(entry);
1035 }
1036 }
Juan Quintelaec481c62017-03-20 22:12:40 +01001037 qemu_mutex_unlock(&rs->src_page_req_mutex);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001038
1039 return block;
1040}
1041
Juan Quintela3d0684b2017-03-23 15:06:39 +01001042/**
1043 * get_queued_page: unqueue a page from the postocpy requests
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001044 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001045 * Skips pages that are already sent (!dirty)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001046 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001047 * Returns if a queued page is found
1048 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001049 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001050 * @pss: data about the state of the current dirty page scan
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001051 */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001052static bool get_queued_page(RAMState *rs, PageSearchStatus *pss)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001053{
1054 RAMBlock *block;
1055 ram_addr_t offset;
1056 bool dirty;
1057
1058 do {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001059 block = unqueue_page(rs, &offset);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001060 /*
1061 * We're sending this page, and since it's postcopy nothing else
1062 * will dirty it, and we must make sure it doesn't get sent again
1063 * even if this queue request was received after the background
1064 * search already sent it.
1065 */
1066 if (block) {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001067 unsigned long page;
1068
Juan Quintela6b6712e2017-03-22 15:18:04 +01001069 page = offset >> TARGET_PAGE_BITS;
1070 dirty = test_bit(page, block->bmap);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001071 if (!dirty) {
Juan Quintela06b10682017-03-21 15:18:05 +01001072 trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset,
Juan Quintela6b6712e2017-03-22 15:18:04 +01001073 page, test_bit(page, block->unsentmap));
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001074 } else {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001075 trace_get_queued_page(block->idstr, (uint64_t)offset, page);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001076 }
1077 }
1078
1079 } while (block && !dirty);
1080
1081 if (block) {
1082 /*
1083 * As soon as we start servicing pages out of order, then we have
1084 * to kill the bulk stage, since the bulk stage assumes
1085 * in (migration_bitmap_find_and_reset_dirty) that every page is
1086 * dirty, that's no longer true.
1087 */
Juan Quintela6f37bb82017-03-13 19:26:29 +01001088 rs->ram_bulk_stage = false;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001089
1090 /*
1091 * We want the background search to continue from the queued page
1092 * since the guest is likely to want other pages near to the page
1093 * it just requested.
1094 */
1095 pss->block = block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001096 pss->page = offset >> TARGET_PAGE_BITS;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001097 }
1098
1099 return !!block;
1100}
1101
Juan Quintela56e93d22015-05-07 19:33:31 +02001102/**
Juan Quintela5e58f962017-04-03 22:06:54 +02001103 * migration_page_queue_free: drop any remaining pages in the ram
1104 * request queue
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001105 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001106 * It should be empty at the end anyway, but in error cases there may
1107 * be some left. in case that there is any page left, we drop it.
1108 *
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001109 */
Juan Quintela83c13382017-05-04 11:45:01 +02001110static void migration_page_queue_free(RAMState *rs)
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001111{
Juan Quintelaec481c62017-03-20 22:12:40 +01001112 struct RAMSrcPageRequest *mspr, *next_mspr;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001113 /* This queue generally should be empty - but in the case of a failed
1114 * migration might have some droppings in.
1115 */
1116 rcu_read_lock();
Juan Quintelaec481c62017-03-20 22:12:40 +01001117 QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) {
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001118 memory_region_unref(mspr->rb->mr);
Juan Quintelaec481c62017-03-20 22:12:40 +01001119 QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001120 g_free(mspr);
1121 }
1122 rcu_read_unlock();
1123}
1124
1125/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001126 * ram_save_queue_pages: queue the page for transmission
1127 *
1128 * A request from postcopy destination for example.
1129 *
1130 * Returns zero on success or negative on error
1131 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001132 * @rbname: Name of the RAMBLock of the request. NULL means the
1133 * same that last one.
1134 * @start: starting address from the start of the RAMBlock
1135 * @len: length (in bytes) to send
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001136 */
Juan Quintela96506892017-03-14 18:41:03 +01001137int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len)
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001138{
1139 RAMBlock *ramblock;
Juan Quintela53518d92017-05-04 11:46:24 +02001140 RAMState *rs = ram_state;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001141
Juan Quintela93604472017-06-06 19:49:03 +02001142 ram_counters.postcopy_requests++;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001143 rcu_read_lock();
1144 if (!rbname) {
1145 /* Reuse last RAMBlock */
Juan Quintela68a098f2017-03-14 13:48:42 +01001146 ramblock = rs->last_req_rb;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001147
1148 if (!ramblock) {
1149 /*
1150 * Shouldn't happen, we can't reuse the last RAMBlock if
1151 * it's the 1st request.
1152 */
1153 error_report("ram_save_queue_pages no previous block");
1154 goto err;
1155 }
1156 } else {
1157 ramblock = qemu_ram_block_by_name(rbname);
1158
1159 if (!ramblock) {
1160 /* We shouldn't be asked for a non-existent RAMBlock */
1161 error_report("ram_save_queue_pages no block '%s'", rbname);
1162 goto err;
1163 }
Juan Quintela68a098f2017-03-14 13:48:42 +01001164 rs->last_req_rb = ramblock;
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001165 }
1166 trace_ram_save_queue_pages(ramblock->idstr, start, len);
1167 if (start+len > ramblock->used_length) {
Juan Quintela9458ad62015-11-10 17:42:05 +01001168 error_report("%s request overrun start=" RAM_ADDR_FMT " len="
1169 RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT,
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001170 __func__, start, len, ramblock->used_length);
1171 goto err;
1172 }
1173
Juan Quintelaec481c62017-03-20 22:12:40 +01001174 struct RAMSrcPageRequest *new_entry =
1175 g_malloc0(sizeof(struct RAMSrcPageRequest));
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001176 new_entry->rb = ramblock;
1177 new_entry->offset = start;
1178 new_entry->len = len;
1179
1180 memory_region_ref(ramblock->mr);
Juan Quintelaec481c62017-03-20 22:12:40 +01001181 qemu_mutex_lock(&rs->src_page_req_mutex);
1182 QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req);
1183 qemu_mutex_unlock(&rs->src_page_req_mutex);
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001184 rcu_read_unlock();
1185
1186 return 0;
1187
1188err:
1189 rcu_read_unlock();
1190 return -1;
1191}
1192
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001193/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001194 * ram_save_target_page: save one target page
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001195 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001196 * Returns the number of pages written
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001197 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001198 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001199 * @ms: current migration state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001200 * @pss: data about the page we want to send
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001201 * @last_stage: if we are at the completion stage
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001202 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001203static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss,
Juan Quintelaf20e2862017-03-21 16:19:05 +01001204 bool last_stage)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001205{
1206 int res = 0;
1207
1208 /* Check the pages is dirty and if it is send it */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001209 if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) {
Juan Quintela6d358d92017-03-16 21:29:34 +01001210 /*
1211 * If xbzrle is on, stop using the data compression after first
1212 * round of migration even if compression is enabled. In theory,
1213 * xbzrle can do better than compression.
1214 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001215 if (migrate_use_compression() &&
1216 (rs->ram_bulk_stage || !migrate_use_xbzrle())) {
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001217 res = ram_save_compressed_page(rs, pss, last_stage);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001218 } else {
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001219 res = ram_save_page(rs, pss, last_stage);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001220 }
1221
1222 if (res < 0) {
1223 return res;
1224 }
Juan Quintela6b6712e2017-03-22 15:18:04 +01001225 if (pss->block->unsentmap) {
1226 clear_bit(pss->page, pss->block->unsentmap);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001227 }
1228 }
1229
1230 return res;
1231}
1232
1233/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001234 * ram_save_host_page: save a whole host page
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001235 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001236 * Starting at *offset send pages up to the end of the current host
1237 * page. It's valid for the initial offset to point into the middle of
1238 * a host page in which case the remainder of the hostpage is sent.
1239 * Only dirty target pages are sent. Note that the host page size may
1240 * be a huge page for this block.
Dr. David Alan Gilbert1eb3fc02017-05-17 17:58:09 +01001241 * The saving stops at the boundary of the used_length of the block
1242 * if the RAMBlock isn't a multiple of the host page size.
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001243 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001244 * Returns the number of pages written or negative on error
1245 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001246 * @rs: current RAM state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001247 * @ms: current migration state
Juan Quintela3d0684b2017-03-23 15:06:39 +01001248 * @pss: data about the page we want to send
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001249 * @last_stage: if we are at the completion stage
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001250 */
Juan Quintelaa0a8aa12017-03-20 22:29:07 +01001251static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss,
Juan Quintelaf20e2862017-03-21 16:19:05 +01001252 bool last_stage)
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001253{
1254 int tmppages, pages = 0;
Juan Quintelaa935e302017-03-21 15:36:51 +01001255 size_t pagesize_bits =
1256 qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS;
Dr. David Alan Gilbert4c011c32017-02-24 18:28:39 +00001257
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001258 do {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001259 tmppages = ram_save_target_page(rs, pss, last_stage);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001260 if (tmppages < 0) {
1261 return tmppages;
1262 }
1263
1264 pages += tmppages;
Juan Quintelaa935e302017-03-21 15:36:51 +01001265 pss->page++;
Dr. David Alan Gilbert1eb3fc02017-05-17 17:58:09 +01001266 } while ((pss->page & (pagesize_bits - 1)) &&
1267 offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS));
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001268
1269 /* The offset we leave with is the last one we looked at */
Juan Quintelaa935e302017-03-21 15:36:51 +01001270 pss->page--;
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001271 return pages;
1272}
Dr. David Alan Gilbert6c595cd2015-11-05 18:11:08 +00001273
1274/**
Juan Quintela3d0684b2017-03-23 15:06:39 +01001275 * ram_find_and_save_block: finds a dirty page and sends it to f
Juan Quintela56e93d22015-05-07 19:33:31 +02001276 *
1277 * Called within an RCU critical section.
1278 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001279 * Returns the number of pages written where zero means no dirty pages
Juan Quintela56e93d22015-05-07 19:33:31 +02001280 *
Juan Quintela6f37bb82017-03-13 19:26:29 +01001281 * @rs: current RAM state
Juan Quintela56e93d22015-05-07 19:33:31 +02001282 * @last_stage: if we are at the completion stage
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001283 *
1284 * On systems where host-page-size > target-page-size it will send all the
1285 * pages in a host page that are dirty.
Juan Quintela56e93d22015-05-07 19:33:31 +02001286 */
1287
Juan Quintelace25d332017-03-15 11:00:51 +01001288static int ram_find_and_save_block(RAMState *rs, bool last_stage)
Juan Quintela56e93d22015-05-07 19:33:31 +02001289{
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +01001290 PageSearchStatus pss;
Juan Quintela56e93d22015-05-07 19:33:31 +02001291 int pages = 0;
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001292 bool again, found;
Juan Quintela56e93d22015-05-07 19:33:31 +02001293
Ashijeet Acharya0827b9e2017-02-08 19:58:45 +05301294 /* No dirty page as there is zero RAM */
1295 if (!ram_bytes_total()) {
1296 return pages;
1297 }
1298
Juan Quintela6f37bb82017-03-13 19:26:29 +01001299 pss.block = rs->last_seen_block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001300 pss.page = rs->last_page;
Dr. David Alan Gilbertb8fb8cb2015-09-23 15:27:10 +01001301 pss.complete_round = false;
1302
1303 if (!pss.block) {
1304 pss.block = QLIST_FIRST_RCU(&ram_list.blocks);
1305 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001306
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001307 do {
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001308 again = true;
Juan Quintelaf20e2862017-03-21 16:19:05 +01001309 found = get_queued_page(rs, &pss);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001310
1311 if (!found) {
1312 /* priority queue empty, so just search for something dirty */
Juan Quintelaf20e2862017-03-21 16:19:05 +01001313 found = find_dirty_block(rs, &pss, &again);
Dr. David Alan Gilberta82d5932015-11-05 18:11:09 +00001314 }
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001315
1316 if (found) {
Juan Quintelaf20e2862017-03-21 16:19:05 +01001317 pages = ram_save_host_page(rs, &pss, last_stage);
Juan Quintela56e93d22015-05-07 19:33:31 +02001318 }
Dr. David Alan Gilbertb9e60922015-09-23 15:27:11 +01001319 } while (!pages && again);
Juan Quintela56e93d22015-05-07 19:33:31 +02001320
Juan Quintela6f37bb82017-03-13 19:26:29 +01001321 rs->last_seen_block = pss.block;
Juan Quintelaa935e302017-03-21 15:36:51 +01001322 rs->last_page = pss.page;
Juan Quintela56e93d22015-05-07 19:33:31 +02001323
1324 return pages;
1325}
1326
1327void acct_update_position(QEMUFile *f, size_t size, bool zero)
1328{
1329 uint64_t pages = size / TARGET_PAGE_SIZE;
Juan Quintelaf7ccd612017-03-13 20:30:21 +01001330
Juan Quintela56e93d22015-05-07 19:33:31 +02001331 if (zero) {
Juan Quintela93604472017-06-06 19:49:03 +02001332 ram_counters.duplicate += pages;
Juan Quintela56e93d22015-05-07 19:33:31 +02001333 } else {
Juan Quintela93604472017-06-06 19:49:03 +02001334 ram_counters.normal += pages;
1335 ram_counters.transferred += size;
Juan Quintela56e93d22015-05-07 19:33:31 +02001336 qemu_update_position(f, size);
1337 }
1338}
1339
Juan Quintela56e93d22015-05-07 19:33:31 +02001340uint64_t ram_bytes_total(void)
1341{
1342 RAMBlock *block;
1343 uint64_t total = 0;
1344
1345 rcu_read_lock();
Peter Xu99e15582017-05-12 12:17:39 +08001346 RAMBLOCK_FOREACH(block) {
Juan Quintela56e93d22015-05-07 19:33:31 +02001347 total += block->used_length;
Peter Xu99e15582017-05-12 12:17:39 +08001348 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001349 rcu_read_unlock();
1350 return total;
1351}
1352
1353void free_xbzrle_decoded_buf(void)
1354{
1355 g_free(xbzrle_decoded_buf);
1356 xbzrle_decoded_buf = NULL;
1357}
1358
Liang Li6ad2a212015-11-02 15:37:03 +08001359static void ram_migration_cleanup(void *opaque)
Juan Quintela56e93d22015-05-07 19:33:31 +02001360{
Juan Quintela53518d92017-05-04 11:46:24 +02001361 RAMState **rsp = opaque;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001362 RAMBlock *block;
Juan Quintelaeb859c52017-03-13 21:51:55 +01001363
Li Zhijian2ff64032015-07-02 20:18:05 +08001364 /* caller have hold iothread lock or is in a bh, so there is
1365 * no writing race against this migration_bitmap
1366 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001367 memory_global_dirty_log_stop();
1368
1369 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1370 g_free(block->bmap);
1371 block->bmap = NULL;
1372 g_free(block->unsentmap);
1373 block->unsentmap = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001374 }
1375
1376 XBZRLE_cache_lock();
1377 if (XBZRLE.cache) {
1378 cache_fini(XBZRLE.cache);
1379 g_free(XBZRLE.encoded_buf);
1380 g_free(XBZRLE.current_buf);
Juan Quintelac00e0922017-05-09 16:22:01 +02001381 g_free(XBZRLE.zero_target_page);
Juan Quintela56e93d22015-05-07 19:33:31 +02001382 XBZRLE.cache = NULL;
1383 XBZRLE.encoded_buf = NULL;
1384 XBZRLE.current_buf = NULL;
Juan Quintelac00e0922017-05-09 16:22:01 +02001385 XBZRLE.zero_target_page = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001386 }
1387 XBZRLE_cache_unlock();
Juan Quintela53518d92017-05-04 11:46:24 +02001388 migration_page_queue_free(*rsp);
1389 g_free(*rsp);
1390 *rsp = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001391}
1392
Juan Quintela6f37bb82017-03-13 19:26:29 +01001393static void ram_state_reset(RAMState *rs)
Juan Quintela56e93d22015-05-07 19:33:31 +02001394{
Juan Quintela6f37bb82017-03-13 19:26:29 +01001395 rs->last_seen_block = NULL;
1396 rs->last_sent_block = NULL;
Juan Quintela269ace22017-03-21 15:23:31 +01001397 rs->last_page = 0;
Juan Quintela6f37bb82017-03-13 19:26:29 +01001398 rs->last_version = ram_list.version;
1399 rs->ram_bulk_stage = true;
Juan Quintela56e93d22015-05-07 19:33:31 +02001400}
1401
1402#define MAX_WAIT 50 /* ms, half buffered_file limit */
1403
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001404/*
1405 * 'expected' is the value you expect the bitmap mostly to be full
1406 * of; it won't bother printing lines that are all this value.
1407 * If 'todump' is null the migration bitmap is dumped.
1408 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001409void ram_debug_dump_bitmap(unsigned long *todump, bool expected,
1410 unsigned long pages)
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001411{
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001412 int64_t cur;
1413 int64_t linelen = 128;
1414 char linebuf[129];
1415
Juan Quintela6b6712e2017-03-22 15:18:04 +01001416 for (cur = 0; cur < pages; cur += linelen) {
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001417 int64_t curb;
1418 bool found = false;
1419 /*
1420 * Last line; catch the case where the line length
1421 * is longer than remaining ram
1422 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001423 if (cur + linelen > pages) {
1424 linelen = pages - cur;
Dr. David Alan Gilbert4f2e4252015-11-05 18:10:38 +00001425 }
1426 for (curb = 0; curb < linelen; curb++) {
1427 bool thisbit = test_bit(cur + curb, todump);
1428 linebuf[curb] = thisbit ? '1' : '.';
1429 found = found || (thisbit != expected);
1430 }
1431 if (found) {
1432 linebuf[curb] = '\0';
1433 fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf);
1434 }
1435 }
1436}
1437
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001438/* **** functions for postcopy ***** */
1439
Pavel Butsykinced1c612017-02-03 18:23:21 +03001440void ram_postcopy_migrated_memory_release(MigrationState *ms)
1441{
1442 struct RAMBlock *block;
Pavel Butsykinced1c612017-02-03 18:23:21 +03001443
Peter Xu99e15582017-05-12 12:17:39 +08001444 RAMBLOCK_FOREACH(block) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001445 unsigned long *bitmap = block->bmap;
1446 unsigned long range = block->used_length >> TARGET_PAGE_BITS;
1447 unsigned long run_start = find_next_zero_bit(bitmap, range, 0);
Pavel Butsykinced1c612017-02-03 18:23:21 +03001448
1449 while (run_start < range) {
1450 unsigned long run_end = find_next_bit(bitmap, range, run_start + 1);
Juan Quintelaaaa20642017-03-21 11:35:24 +01001451 ram_discard_range(block->idstr, run_start << TARGET_PAGE_BITS,
Pavel Butsykinced1c612017-02-03 18:23:21 +03001452 (run_end - run_start) << TARGET_PAGE_BITS);
1453 run_start = find_next_zero_bit(bitmap, range, run_end + 1);
1454 }
1455 }
1456}
1457
Juan Quintela3d0684b2017-03-23 15:06:39 +01001458/**
1459 * postcopy_send_discard_bm_ram: discard a RAMBlock
1460 *
1461 * Returns zero on success
1462 *
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001463 * Callback from postcopy_each_ram_send_discard for each RAMBlock
1464 * Note: At this point the 'unsentmap' is the processed bitmap combined
1465 * with the dirtymap; so a '1' means it's either dirty or unsent.
Juan Quintela3d0684b2017-03-23 15:06:39 +01001466 *
1467 * @ms: current migration state
1468 * @pds: state for postcopy
1469 * @start: RAMBlock starting page
1470 * @length: RAMBlock size
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001471 */
1472static int postcopy_send_discard_bm_ram(MigrationState *ms,
1473 PostcopyDiscardState *pds,
Juan Quintela6b6712e2017-03-22 15:18:04 +01001474 RAMBlock *block)
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001475{
Juan Quintela6b6712e2017-03-22 15:18:04 +01001476 unsigned long end = block->used_length >> TARGET_PAGE_BITS;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001477 unsigned long current;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001478 unsigned long *unsentmap = block->unsentmap;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001479
Juan Quintela6b6712e2017-03-22 15:18:04 +01001480 for (current = 0; current < end; ) {
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001481 unsigned long one = find_next_bit(unsentmap, end, current);
1482
1483 if (one <= end) {
1484 unsigned long zero = find_next_zero_bit(unsentmap, end, one + 1);
1485 unsigned long discard_length;
1486
1487 if (zero >= end) {
1488 discard_length = end - one;
1489 } else {
1490 discard_length = zero - one;
1491 }
Dr. David Alan Gilbertd688c622016-06-13 12:16:40 +01001492 if (discard_length) {
1493 postcopy_discard_send_range(ms, pds, one, discard_length);
1494 }
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001495 current = one + discard_length;
1496 } else {
1497 current = one;
1498 }
1499 }
1500
1501 return 0;
1502}
1503
Juan Quintela3d0684b2017-03-23 15:06:39 +01001504/**
1505 * postcopy_each_ram_send_discard: discard all RAMBlocks
1506 *
1507 * Returns 0 for success or negative for error
1508 *
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001509 * Utility for the outgoing postcopy code.
1510 * Calls postcopy_send_discard_bm_ram for each RAMBlock
1511 * passing it bitmap indexes and name.
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001512 * (qemu_ram_foreach_block ends up passing unscaled lengths
1513 * which would mean postcopy code would have to deal with target page)
Juan Quintela3d0684b2017-03-23 15:06:39 +01001514 *
1515 * @ms: current migration state
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001516 */
1517static int postcopy_each_ram_send_discard(MigrationState *ms)
1518{
1519 struct RAMBlock *block;
1520 int ret;
1521
Peter Xu99e15582017-05-12 12:17:39 +08001522 RAMBLOCK_FOREACH(block) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001523 PostcopyDiscardState *pds =
1524 postcopy_discard_send_init(ms, block->idstr);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001525
1526 /*
1527 * Postcopy sends chunks of bitmap over the wire, but it
1528 * just needs indexes at this point, avoids it having
1529 * target page specific code.
1530 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001531 ret = postcopy_send_discard_bm_ram(ms, pds, block);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001532 postcopy_discard_send_finish(ms, pds);
1533 if (ret) {
1534 return ret;
1535 }
1536 }
1537
1538 return 0;
1539}
1540
Juan Quintela3d0684b2017-03-23 15:06:39 +01001541/**
1542 * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001543 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001544 * Helper for postcopy_chunk_hostpages; it's called twice to
1545 * canonicalize the two bitmaps, that are similar, but one is
1546 * inverted.
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001547 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001548 * Postcopy requires that all target pages in a hostpage are dirty or
1549 * clean, not a mix. This function canonicalizes the bitmaps.
1550 *
1551 * @ms: current migration state
1552 * @unsent_pass: if true we need to canonicalize partially unsent host pages
1553 * otherwise we need to canonicalize partially dirty host pages
1554 * @block: block that contains the page we want to canonicalize
1555 * @pds: state for postcopy
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001556 */
1557static void postcopy_chunk_hostpages_pass(MigrationState *ms, bool unsent_pass,
1558 RAMBlock *block,
1559 PostcopyDiscardState *pds)
1560{
Juan Quintela53518d92017-05-04 11:46:24 +02001561 RAMState *rs = ram_state;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001562 unsigned long *bitmap = block->bmap;
1563 unsigned long *unsentmap = block->unsentmap;
Dr. David Alan Gilbert29c59172017-02-24 18:28:31 +00001564 unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001565 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001566 unsigned long run_start;
1567
Dr. David Alan Gilbert29c59172017-02-24 18:28:31 +00001568 if (block->page_size == TARGET_PAGE_SIZE) {
1569 /* Easy case - TPS==HPS for a non-huge page RAMBlock */
1570 return;
1571 }
1572
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001573 if (unsent_pass) {
1574 /* Find a sent page */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001575 run_start = find_next_zero_bit(unsentmap, pages, 0);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001576 } else {
1577 /* Find a dirty page */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001578 run_start = find_next_bit(bitmap, pages, 0);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001579 }
1580
Juan Quintela6b6712e2017-03-22 15:18:04 +01001581 while (run_start < pages) {
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001582 bool do_fixup = false;
1583 unsigned long fixup_start_addr;
1584 unsigned long host_offset;
1585
1586 /*
1587 * If the start of this run of pages is in the middle of a host
1588 * page, then we need to fixup this host page.
1589 */
1590 host_offset = run_start % host_ratio;
1591 if (host_offset) {
1592 do_fixup = true;
1593 run_start -= host_offset;
1594 fixup_start_addr = run_start;
1595 /* For the next pass */
1596 run_start = run_start + host_ratio;
1597 } else {
1598 /* Find the end of this run */
1599 unsigned long run_end;
1600 if (unsent_pass) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001601 run_end = find_next_bit(unsentmap, pages, run_start + 1);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001602 } else {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001603 run_end = find_next_zero_bit(bitmap, pages, run_start + 1);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001604 }
1605 /*
1606 * If the end isn't at the start of a host page, then the
1607 * run doesn't finish at the end of a host page
1608 * and we need to discard.
1609 */
1610 host_offset = run_end % host_ratio;
1611 if (host_offset) {
1612 do_fixup = true;
1613 fixup_start_addr = run_end - host_offset;
1614 /*
1615 * This host page has gone, the next loop iteration starts
1616 * from after the fixup
1617 */
1618 run_start = fixup_start_addr + host_ratio;
1619 } else {
1620 /*
1621 * No discards on this iteration, next loop starts from
1622 * next sent/dirty page
1623 */
1624 run_start = run_end + 1;
1625 }
1626 }
1627
1628 if (do_fixup) {
1629 unsigned long page;
1630
1631 /* Tell the destination to discard this page */
1632 if (unsent_pass || !test_bit(fixup_start_addr, unsentmap)) {
1633 /* For the unsent_pass we:
1634 * discard partially sent pages
1635 * For the !unsent_pass (dirty) we:
1636 * discard partially dirty pages that were sent
1637 * (any partially sent pages were already discarded
1638 * by the previous unsent_pass)
1639 */
1640 postcopy_discard_send_range(ms, pds, fixup_start_addr,
1641 host_ratio);
1642 }
1643
1644 /* Clean up the bitmap */
1645 for (page = fixup_start_addr;
1646 page < fixup_start_addr + host_ratio; page++) {
1647 /* All pages in this host page are now not sent */
1648 set_bit(page, unsentmap);
1649
1650 /*
1651 * Remark them as dirty, updating the count for any pages
1652 * that weren't previously dirty.
1653 */
Juan Quintela0d8ec882017-03-13 21:21:41 +01001654 rs->migration_dirty_pages += !test_and_set_bit(page, bitmap);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001655 }
1656 }
1657
1658 if (unsent_pass) {
1659 /* Find the next sent page for the next iteration */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001660 run_start = find_next_zero_bit(unsentmap, pages, run_start);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001661 } else {
1662 /* Find the next dirty page for the next iteration */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001663 run_start = find_next_bit(bitmap, pages, run_start);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001664 }
1665 }
1666}
1667
Juan Quintela3d0684b2017-03-23 15:06:39 +01001668/**
1669 * postcopy_chuck_hostpages: discrad any partially sent host page
1670 *
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001671 * Utility for the outgoing postcopy code.
1672 *
1673 * Discard any partially sent host-page size chunks, mark any partially
Dr. David Alan Gilbert29c59172017-02-24 18:28:31 +00001674 * dirty host-page size chunks as all dirty. In this case the host-page
1675 * 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 +00001676 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001677 * Returns zero on success
1678 *
1679 * @ms: current migration state
Juan Quintela6b6712e2017-03-22 15:18:04 +01001680 * @block: block we want to work with
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001681 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001682static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block)
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001683{
Juan Quintela6b6712e2017-03-22 15:18:04 +01001684 PostcopyDiscardState *pds =
1685 postcopy_discard_send_init(ms, block->idstr);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001686
Juan Quintela6b6712e2017-03-22 15:18:04 +01001687 /* First pass: Discard all partially sent host pages */
1688 postcopy_chunk_hostpages_pass(ms, true, block, pds);
1689 /*
1690 * Second pass: Ensure that all partially dirty host pages are made
1691 * fully dirty.
1692 */
1693 postcopy_chunk_hostpages_pass(ms, false, block, pds);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001694
Juan Quintela6b6712e2017-03-22 15:18:04 +01001695 postcopy_discard_send_finish(ms, pds);
Dr. David Alan Gilbert99e314e2015-11-05 18:11:15 +00001696 return 0;
1697}
1698
Juan Quintela3d0684b2017-03-23 15:06:39 +01001699/**
1700 * ram_postcopy_send_discard_bitmap: transmit the discard bitmap
1701 *
1702 * Returns zero on success
1703 *
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001704 * Transmit the set of pages to be discarded after precopy to the target
1705 * these are pages that:
1706 * a) Have been previously transmitted but are now dirty again
1707 * b) Pages that have never been transmitted, this ensures that
1708 * any pages on the destination that have been mapped by background
1709 * tasks get discarded (transparent huge pages is the specific concern)
1710 * Hopefully this is pretty sparse
Juan Quintela3d0684b2017-03-23 15:06:39 +01001711 *
1712 * @ms: current migration state
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001713 */
1714int ram_postcopy_send_discard_bitmap(MigrationState *ms)
1715{
Juan Quintela53518d92017-05-04 11:46:24 +02001716 RAMState *rs = ram_state;
Juan Quintela6b6712e2017-03-22 15:18:04 +01001717 RAMBlock *block;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001718 int ret;
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001719
1720 rcu_read_lock();
1721
1722 /* This should be our last sync, the src is now paused */
Juan Quintelaeb859c52017-03-13 21:51:55 +01001723 migration_bitmap_sync(rs);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001724
Juan Quintela6b6712e2017-03-22 15:18:04 +01001725 /* Easiest way to make sure we don't resume in the middle of a host-page */
1726 rs->last_seen_block = NULL;
1727 rs->last_sent_block = NULL;
1728 rs->last_page = 0;
1729
1730 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1731 unsigned long pages = block->used_length >> TARGET_PAGE_BITS;
1732 unsigned long *bitmap = block->bmap;
1733 unsigned long *unsentmap = block->unsentmap;
1734
1735 if (!unsentmap) {
1736 /* We don't have a safe way to resize the sentmap, so
1737 * if the bitmap was resized it will be NULL at this
1738 * point.
1739 */
1740 error_report("migration ram resized during precopy phase");
1741 rcu_read_unlock();
1742 return -EINVAL;
1743 }
1744 /* Deal with TPS != HPS and huge pages */
1745 ret = postcopy_chunk_hostpages(ms, block);
1746 if (ret) {
1747 rcu_read_unlock();
1748 return ret;
1749 }
1750
1751 /*
1752 * Update the unsentmap to be unsentmap = unsentmap | dirty
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001753 */
Juan Quintela6b6712e2017-03-22 15:18:04 +01001754 bitmap_or(unsentmap, unsentmap, bitmap, pages);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001755#ifdef DEBUG_POSTCOPY
Juan Quintela6b6712e2017-03-22 15:18:04 +01001756 ram_debug_dump_bitmap(unsentmap, true, pages);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001757#endif
Juan Quintela6b6712e2017-03-22 15:18:04 +01001758 }
1759 trace_ram_postcopy_send_discard_bitmap();
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001760
1761 ret = postcopy_each_ram_send_discard(ms);
1762 rcu_read_unlock();
1763
1764 return ret;
1765}
1766
Juan Quintela3d0684b2017-03-23 15:06:39 +01001767/**
1768 * ram_discard_range: discard dirtied pages at the beginning of postcopy
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001769 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01001770 * Returns zero on success
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001771 *
Juan Quintela36449152017-03-23 15:11:59 +01001772 * @rbname: name of the RAMBlock of the request. NULL means the
1773 * same that last one.
Juan Quintela3d0684b2017-03-23 15:06:39 +01001774 * @start: RAMBlock starting page
1775 * @length: RAMBlock size
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001776 */
Juan Quintelaaaa20642017-03-21 11:35:24 +01001777int ram_discard_range(const char *rbname, uint64_t start, size_t length)
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001778{
1779 int ret = -1;
1780
Juan Quintela36449152017-03-23 15:11:59 +01001781 trace_ram_discard_range(rbname, start, length);
Dr. David Alan Gilbertd3a50382017-02-24 18:28:32 +00001782
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001783 rcu_read_lock();
Juan Quintela36449152017-03-23 15:11:59 +01001784 RAMBlock *rb = qemu_ram_block_by_name(rbname);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001785
1786 if (!rb) {
Juan Quintela36449152017-03-23 15:11:59 +01001787 error_report("ram_discard_range: Failed to find block '%s'", rbname);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001788 goto err;
1789 }
1790
Dr. David Alan Gilbertd3a50382017-02-24 18:28:32 +00001791 ret = ram_block_discard_range(rb, start, length);
Dr. David Alan Gilberte0b266f2015-11-05 18:11:02 +00001792
1793err:
1794 rcu_read_unlock();
1795
1796 return ret;
1797}
1798
Juan Quintela53518d92017-05-04 11:46:24 +02001799static int ram_state_init(RAMState **rsp)
Juan Quintela56e93d22015-05-07 19:33:31 +02001800{
Juan Quintela53518d92017-05-04 11:46:24 +02001801 *rsp = g_new0(RAMState, 1);
1802
1803 qemu_mutex_init(&(*rsp)->bitmap_mutex);
1804 qemu_mutex_init(&(*rsp)->src_page_req_mutex);
1805 QSIMPLEQ_INIT(&(*rsp)->src_page_requests);
Juan Quintela56e93d22015-05-07 19:33:31 +02001806
1807 if (migrate_use_xbzrle()) {
1808 XBZRLE_cache_lock();
Juan Quintelac00e0922017-05-09 16:22:01 +02001809 XBZRLE.zero_target_page = g_malloc0(TARGET_PAGE_SIZE);
Juan Quintela56e93d22015-05-07 19:33:31 +02001810 XBZRLE.cache = cache_init(migrate_xbzrle_cache_size() /
1811 TARGET_PAGE_SIZE,
1812 TARGET_PAGE_SIZE);
1813 if (!XBZRLE.cache) {
1814 XBZRLE_cache_unlock();
1815 error_report("Error creating cache");
Juan Quintela53518d92017-05-04 11:46:24 +02001816 g_free(*rsp);
1817 *rsp = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001818 return -1;
1819 }
1820 XBZRLE_cache_unlock();
1821
1822 /* We prefer not to abort if there is no memory */
1823 XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE);
1824 if (!XBZRLE.encoded_buf) {
1825 error_report("Error allocating encoded_buf");
Juan Quintela53518d92017-05-04 11:46:24 +02001826 g_free(*rsp);
1827 *rsp = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001828 return -1;
1829 }
1830
1831 XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE);
1832 if (!XBZRLE.current_buf) {
1833 error_report("Error allocating current_buf");
1834 g_free(XBZRLE.encoded_buf);
1835 XBZRLE.encoded_buf = NULL;
Juan Quintela53518d92017-05-04 11:46:24 +02001836 g_free(*rsp);
1837 *rsp = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02001838 return -1;
1839 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001840 }
1841
Paolo Bonzini49877832016-02-15 19:57:57 +01001842 /* For memory_global_dirty_log_start below. */
1843 qemu_mutex_lock_iothread();
1844
Juan Quintela56e93d22015-05-07 19:33:31 +02001845 qemu_mutex_lock_ramlist();
1846 rcu_read_lock();
Juan Quintela53518d92017-05-04 11:46:24 +02001847 ram_state_reset(*rsp);
Juan Quintela56e93d22015-05-07 19:33:31 +02001848
Ashijeet Acharya0827b9e2017-02-08 19:58:45 +05301849 /* Skip setting bitmap if there is no RAM */
1850 if (ram_bytes_total()) {
Juan Quintela6b6712e2017-03-22 15:18:04 +01001851 RAMBlock *block;
Juan Quintela56e93d22015-05-07 19:33:31 +02001852
Juan Quintela6b6712e2017-03-22 15:18:04 +01001853 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1854 unsigned long pages = block->max_length >> TARGET_PAGE_BITS;
1855
1856 block->bmap = bitmap_new(pages);
1857 bitmap_set(block->bmap, 0, pages);
1858 if (migrate_postcopy_ram()) {
1859 block->unsentmap = bitmap_new(pages);
1860 bitmap_set(block->unsentmap, 0, pages);
1861 }
Ashijeet Acharya0827b9e2017-02-08 19:58:45 +05301862 }
Dr. David Alan Gilbertf3f491f2015-11-05 18:11:01 +00001863 }
1864
Juan Quintela56e93d22015-05-07 19:33:31 +02001865 /*
1866 * Count the total number of pages used by ram blocks not including any
1867 * gaps due to alignment or unplugs.
1868 */
Juan Quintela53518d92017-05-04 11:46:24 +02001869 (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS;
Juan Quintela56e93d22015-05-07 19:33:31 +02001870
1871 memory_global_dirty_log_start();
Juan Quintela53518d92017-05-04 11:46:24 +02001872 migration_bitmap_sync(*rsp);
Juan Quintela56e93d22015-05-07 19:33:31 +02001873 qemu_mutex_unlock_ramlist();
Paolo Bonzini49877832016-02-15 19:57:57 +01001874 qemu_mutex_unlock_iothread();
zhanghailianga91246c2016-10-27 14:42:59 +08001875 rcu_read_unlock();
1876
1877 return 0;
1878}
1879
Juan Quintela3d0684b2017-03-23 15:06:39 +01001880/*
1881 * Each of ram_save_setup, ram_save_iterate and ram_save_complete has
zhanghailianga91246c2016-10-27 14:42:59 +08001882 * long-running RCU critical section. When rcu-reclaims in the code
1883 * start to become numerous it will be necessary to reduce the
1884 * granularity of these critical sections.
1885 */
1886
Juan Quintela3d0684b2017-03-23 15:06:39 +01001887/**
1888 * ram_save_setup: Setup RAM for migration
1889 *
1890 * Returns zero to indicate success and negative for error
1891 *
1892 * @f: QEMUFile where to send the data
1893 * @opaque: RAMState pointer
1894 */
zhanghailianga91246c2016-10-27 14:42:59 +08001895static int ram_save_setup(QEMUFile *f, void *opaque)
1896{
Juan Quintela53518d92017-05-04 11:46:24 +02001897 RAMState **rsp = opaque;
zhanghailianga91246c2016-10-27 14:42:59 +08001898 RAMBlock *block;
1899
1900 /* migration has already setup the bitmap, reuse it. */
1901 if (!migration_in_colo_state()) {
Juan Quintela53518d92017-05-04 11:46:24 +02001902 if (ram_state_init(rsp) != 0) {
zhanghailianga91246c2016-10-27 14:42:59 +08001903 return -1;
Juan Quintela53518d92017-05-04 11:46:24 +02001904 }
zhanghailianga91246c2016-10-27 14:42:59 +08001905 }
Juan Quintela53518d92017-05-04 11:46:24 +02001906 (*rsp)->f = f;
zhanghailianga91246c2016-10-27 14:42:59 +08001907
1908 rcu_read_lock();
Juan Quintela56e93d22015-05-07 19:33:31 +02001909
1910 qemu_put_be64(f, ram_bytes_total() | RAM_SAVE_FLAG_MEM_SIZE);
1911
Peter Xu99e15582017-05-12 12:17:39 +08001912 RAMBLOCK_FOREACH(block) {
Juan Quintela56e93d22015-05-07 19:33:31 +02001913 qemu_put_byte(f, strlen(block->idstr));
1914 qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr));
1915 qemu_put_be64(f, block->used_length);
Dr. David Alan Gilbertef08fb32017-02-24 18:28:30 +00001916 if (migrate_postcopy_ram() && block->page_size != qemu_host_page_size) {
1917 qemu_put_be64(f, block->page_size);
1918 }
Juan Quintela56e93d22015-05-07 19:33:31 +02001919 }
1920
1921 rcu_read_unlock();
1922
1923 ram_control_before_iterate(f, RAM_CONTROL_SETUP);
1924 ram_control_after_iterate(f, RAM_CONTROL_SETUP);
1925
1926 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
1927
1928 return 0;
1929}
1930
Juan Quintela3d0684b2017-03-23 15:06:39 +01001931/**
1932 * ram_save_iterate: iterative stage for migration
1933 *
1934 * Returns zero to indicate success and negative for error
1935 *
1936 * @f: QEMUFile where to send the data
1937 * @opaque: RAMState pointer
1938 */
Juan Quintela56e93d22015-05-07 19:33:31 +02001939static int ram_save_iterate(QEMUFile *f, void *opaque)
1940{
Juan Quintela53518d92017-05-04 11:46:24 +02001941 RAMState **temp = opaque;
1942 RAMState *rs = *temp;
Juan Quintela56e93d22015-05-07 19:33:31 +02001943 int ret;
1944 int i;
1945 int64_t t0;
Thomas Huth5c903082016-11-04 14:10:17 +01001946 int done = 0;
Juan Quintela56e93d22015-05-07 19:33:31 +02001947
1948 rcu_read_lock();
Juan Quintela6f37bb82017-03-13 19:26:29 +01001949 if (ram_list.version != rs->last_version) {
1950 ram_state_reset(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02001951 }
1952
1953 /* Read version before ram_list.blocks */
1954 smp_rmb();
1955
1956 ram_control_before_iterate(f, RAM_CONTROL_ROUND);
1957
1958 t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
1959 i = 0;
1960 while ((ret = qemu_file_rate_limit(f)) == 0) {
1961 int pages;
1962
Juan Quintelace25d332017-03-15 11:00:51 +01001963 pages = ram_find_and_save_block(rs, false);
Juan Quintela56e93d22015-05-07 19:33:31 +02001964 /* no more pages to sent */
1965 if (pages == 0) {
Thomas Huth5c903082016-11-04 14:10:17 +01001966 done = 1;
Juan Quintela56e93d22015-05-07 19:33:31 +02001967 break;
1968 }
Juan Quintela23b28c32017-03-13 20:51:34 +01001969 rs->iterations++;
Jason J. Herne070afca2015-09-08 13:12:35 -04001970
Juan Quintela56e93d22015-05-07 19:33:31 +02001971 /* we want to check in the 1st loop, just in case it was the 1st time
1972 and we had to sync the dirty bitmap.
1973 qemu_get_clock_ns() is a bit expensive, so we only check each some
1974 iterations
1975 */
1976 if ((i & 63) == 0) {
1977 uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000;
1978 if (t1 > MAX_WAIT) {
Juan Quintela55c44462017-01-23 22:32:05 +01001979 trace_ram_save_iterate_big_wait(t1, i);
Juan Quintela56e93d22015-05-07 19:33:31 +02001980 break;
1981 }
1982 }
1983 i++;
1984 }
Juan Quintelace25d332017-03-15 11:00:51 +01001985 flush_compressed_data(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02001986 rcu_read_unlock();
1987
1988 /*
1989 * Must occur before EOS (or any QEMUFile operation)
1990 * because of RDMA protocol.
1991 */
1992 ram_control_after_iterate(f, RAM_CONTROL_ROUND);
1993
1994 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
Juan Quintela93604472017-06-06 19:49:03 +02001995 ram_counters.transferred += 8;
Juan Quintela56e93d22015-05-07 19:33:31 +02001996
1997 ret = qemu_file_get_error(f);
1998 if (ret < 0) {
1999 return ret;
2000 }
2001
Thomas Huth5c903082016-11-04 14:10:17 +01002002 return done;
Juan Quintela56e93d22015-05-07 19:33:31 +02002003}
2004
Juan Quintela3d0684b2017-03-23 15:06:39 +01002005/**
2006 * ram_save_complete: function called to send the remaining amount of ram
2007 *
2008 * Returns zero to indicate success
2009 *
2010 * Called with iothread lock
2011 *
2012 * @f: QEMUFile where to send the data
2013 * @opaque: RAMState pointer
2014 */
Juan Quintela56e93d22015-05-07 19:33:31 +02002015static int ram_save_complete(QEMUFile *f, void *opaque)
2016{
Juan Quintela53518d92017-05-04 11:46:24 +02002017 RAMState **temp = opaque;
2018 RAMState *rs = *temp;
Juan Quintela6f37bb82017-03-13 19:26:29 +01002019
Juan Quintela56e93d22015-05-07 19:33:31 +02002020 rcu_read_lock();
2021
Juan Quintela57273092017-03-20 22:25:28 +01002022 if (!migration_in_postcopy()) {
Juan Quintela8d820d62017-03-13 19:35:50 +01002023 migration_bitmap_sync(rs);
Dr. David Alan Gilbert663e6c12015-11-05 18:11:13 +00002024 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002025
2026 ram_control_before_iterate(f, RAM_CONTROL_FINISH);
2027
2028 /* try transferring iterative blocks of memory */
2029
2030 /* flush all remaining blocks regardless of rate limiting */
2031 while (true) {
2032 int pages;
2033
Juan Quintelace25d332017-03-15 11:00:51 +01002034 pages = ram_find_and_save_block(rs, !migration_in_colo_state());
Juan Quintela56e93d22015-05-07 19:33:31 +02002035 /* no more blocks to sent */
2036 if (pages == 0) {
2037 break;
2038 }
2039 }
2040
Juan Quintelace25d332017-03-15 11:00:51 +01002041 flush_compressed_data(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02002042 ram_control_after_iterate(f, RAM_CONTROL_FINISH);
Juan Quintela56e93d22015-05-07 19:33:31 +02002043
2044 rcu_read_unlock();
Paolo Bonzinid09a6fd2015-07-09 08:47:58 +02002045
Juan Quintela56e93d22015-05-07 19:33:31 +02002046 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2047
2048 return 0;
2049}
2050
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002051static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size,
2052 uint64_t *non_postcopiable_pending,
2053 uint64_t *postcopiable_pending)
Juan Quintela56e93d22015-05-07 19:33:31 +02002054{
Juan Quintela53518d92017-05-04 11:46:24 +02002055 RAMState **temp = opaque;
2056 RAMState *rs = *temp;
Juan Quintela56e93d22015-05-07 19:33:31 +02002057 uint64_t remaining_size;
2058
Juan Quintela9edabd42017-03-14 12:02:16 +01002059 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
Juan Quintela56e93d22015-05-07 19:33:31 +02002060
Juan Quintela57273092017-03-20 22:25:28 +01002061 if (!migration_in_postcopy() &&
Dr. David Alan Gilbert663e6c12015-11-05 18:11:13 +00002062 remaining_size < max_size) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002063 qemu_mutex_lock_iothread();
2064 rcu_read_lock();
Juan Quintela8d820d62017-03-13 19:35:50 +01002065 migration_bitmap_sync(rs);
Juan Quintela56e93d22015-05-07 19:33:31 +02002066 rcu_read_unlock();
2067 qemu_mutex_unlock_iothread();
Juan Quintela9edabd42017-03-14 12:02:16 +01002068 remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE;
Juan Quintela56e93d22015-05-07 19:33:31 +02002069 }
Dr. David Alan Gilbertc31b0982015-11-05 18:10:54 +00002070
2071 /* We can do postcopy, and all the data is postcopiable */
2072 *postcopiable_pending += remaining_size;
Juan Quintela56e93d22015-05-07 19:33:31 +02002073}
2074
2075static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host)
2076{
2077 unsigned int xh_len;
2078 int xh_flags;
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002079 uint8_t *loaded_data;
Juan Quintela56e93d22015-05-07 19:33:31 +02002080
2081 if (!xbzrle_decoded_buf) {
2082 xbzrle_decoded_buf = g_malloc(TARGET_PAGE_SIZE);
2083 }
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002084 loaded_data = xbzrle_decoded_buf;
Juan Quintela56e93d22015-05-07 19:33:31 +02002085
2086 /* extract RLE header */
2087 xh_flags = qemu_get_byte(f);
2088 xh_len = qemu_get_be16(f);
2089
2090 if (xh_flags != ENCODING_FLAG_XBZRLE) {
2091 error_report("Failed to load XBZRLE page - wrong compression!");
2092 return -1;
2093 }
2094
2095 if (xh_len > TARGET_PAGE_SIZE) {
2096 error_report("Failed to load XBZRLE page - len overflow!");
2097 return -1;
2098 }
2099 /* load data and decode */
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002100 qemu_get_buffer_in_place(f, &loaded_data, xh_len);
Juan Quintela56e93d22015-05-07 19:33:31 +02002101
2102 /* decode RLE */
Dr. David Alan Gilbert063e7602015-12-16 11:47:37 +00002103 if (xbzrle_decode_buffer(loaded_data, xh_len, host,
Juan Quintela56e93d22015-05-07 19:33:31 +02002104 TARGET_PAGE_SIZE) == -1) {
2105 error_report("Failed to load XBZRLE page - decode error!");
2106 return -1;
2107 }
2108
2109 return 0;
2110}
2111
Juan Quintela3d0684b2017-03-23 15:06:39 +01002112/**
2113 * ram_block_from_stream: read a RAMBlock id from the migration stream
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002114 *
Juan Quintela3d0684b2017-03-23 15:06:39 +01002115 * Must be called from within a rcu critical section.
2116 *
2117 * Returns a pointer from within the RCU-protected ram_list.
2118 *
2119 * @f: QEMUFile where to read the data from
2120 * @flags: Page flags (mostly to see if it's a continuation of previous block)
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002121 */
Juan Quintela3d0684b2017-03-23 15:06:39 +01002122static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags)
Juan Quintela56e93d22015-05-07 19:33:31 +02002123{
2124 static RAMBlock *block = NULL;
2125 char id[256];
2126 uint8_t len;
2127
2128 if (flags & RAM_SAVE_FLAG_CONTINUE) {
zhanghailiang4c4bad42016-01-15 11:37:41 +08002129 if (!block) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002130 error_report("Ack, bad migration stream!");
2131 return NULL;
2132 }
zhanghailiang4c4bad42016-01-15 11:37:41 +08002133 return block;
Juan Quintela56e93d22015-05-07 19:33:31 +02002134 }
2135
2136 len = qemu_get_byte(f);
2137 qemu_get_buffer(f, (uint8_t *)id, len);
2138 id[len] = 0;
2139
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002140 block = qemu_ram_block_by_name(id);
zhanghailiang4c4bad42016-01-15 11:37:41 +08002141 if (!block) {
2142 error_report("Can't find block %s", id);
2143 return NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002144 }
2145
zhanghailiang4c4bad42016-01-15 11:37:41 +08002146 return block;
2147}
2148
2149static inline void *host_from_ram_block_offset(RAMBlock *block,
2150 ram_addr_t offset)
2151{
2152 if (!offset_in_ramblock(block, offset)) {
2153 return NULL;
2154 }
2155
2156 return block->host + offset;
Juan Quintela56e93d22015-05-07 19:33:31 +02002157}
2158
Juan Quintela3d0684b2017-03-23 15:06:39 +01002159/**
2160 * ram_handle_compressed: handle the zero page case
2161 *
Juan Quintela56e93d22015-05-07 19:33:31 +02002162 * If a page (or a whole RDMA chunk) has been
2163 * determined to be zero, then zap it.
Juan Quintela3d0684b2017-03-23 15:06:39 +01002164 *
2165 * @host: host address for the zero page
2166 * @ch: what the page is filled from. We only support zero
2167 * @size: size of the zero page
Juan Quintela56e93d22015-05-07 19:33:31 +02002168 */
2169void ram_handle_compressed(void *host, uint8_t ch, uint64_t size)
2170{
2171 if (ch != 0 || !is_zero_range(host, size)) {
2172 memset(host, ch, size);
2173 }
2174}
2175
2176static void *do_data_decompress(void *opaque)
2177{
2178 DecompressParam *param = opaque;
2179 unsigned long pagesize;
Liang Li33d151f2016-05-05 15:32:58 +08002180 uint8_t *des;
2181 int len;
Juan Quintela56e93d22015-05-07 19:33:31 +02002182
Liang Li33d151f2016-05-05 15:32:58 +08002183 qemu_mutex_lock(&param->mutex);
Liang Li90e56fb2016-05-05 15:32:56 +08002184 while (!param->quit) {
Liang Li33d151f2016-05-05 15:32:58 +08002185 if (param->des) {
2186 des = param->des;
2187 len = param->len;
2188 param->des = 0;
2189 qemu_mutex_unlock(&param->mutex);
2190
Liang Li73a89122016-05-05 15:32:51 +08002191 pagesize = TARGET_PAGE_SIZE;
2192 /* uncompress() will return failed in some case, especially
2193 * when the page is dirted when doing the compression, it's
2194 * not a problem because the dirty page will be retransferred
2195 * and uncompress() won't break the data in other pages.
2196 */
Liang Li33d151f2016-05-05 15:32:58 +08002197 uncompress((Bytef *)des, &pagesize,
2198 (const Bytef *)param->compbuf, len);
Liang Li73a89122016-05-05 15:32:51 +08002199
Liang Li33d151f2016-05-05 15:32:58 +08002200 qemu_mutex_lock(&decomp_done_lock);
2201 param->done = true;
2202 qemu_cond_signal(&decomp_done_cond);
2203 qemu_mutex_unlock(&decomp_done_lock);
2204
2205 qemu_mutex_lock(&param->mutex);
2206 } else {
2207 qemu_cond_wait(&param->cond, &param->mutex);
2208 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002209 }
Liang Li33d151f2016-05-05 15:32:58 +08002210 qemu_mutex_unlock(&param->mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02002211
2212 return NULL;
2213}
2214
Liang Li5533b2e2016-05-05 15:32:52 +08002215static void wait_for_decompress_done(void)
2216{
2217 int idx, thread_count;
2218
2219 if (!migrate_use_compression()) {
2220 return;
2221 }
2222
2223 thread_count = migrate_decompress_threads();
2224 qemu_mutex_lock(&decomp_done_lock);
2225 for (idx = 0; idx < thread_count; idx++) {
2226 while (!decomp_param[idx].done) {
2227 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
2228 }
2229 }
2230 qemu_mutex_unlock(&decomp_done_lock);
2231}
2232
Juan Quintela56e93d22015-05-07 19:33:31 +02002233void migrate_decompress_threads_create(void)
2234{
2235 int i, thread_count;
2236
Juan Quintela3416ab52016-04-20 11:56:01 +02002237 if (!migrate_use_compression()) {
2238 return;
2239 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002240 thread_count = migrate_decompress_threads();
2241 decompress_threads = g_new0(QemuThread, thread_count);
2242 decomp_param = g_new0(DecompressParam, thread_count);
Liang Li73a89122016-05-05 15:32:51 +08002243 qemu_mutex_init(&decomp_done_lock);
2244 qemu_cond_init(&decomp_done_cond);
Juan Quintela56e93d22015-05-07 19:33:31 +02002245 for (i = 0; i < thread_count; i++) {
2246 qemu_mutex_init(&decomp_param[i].mutex);
2247 qemu_cond_init(&decomp_param[i].cond);
2248 decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE));
Liang Li73a89122016-05-05 15:32:51 +08002249 decomp_param[i].done = true;
Liang Li90e56fb2016-05-05 15:32:56 +08002250 decomp_param[i].quit = false;
Juan Quintela56e93d22015-05-07 19:33:31 +02002251 qemu_thread_create(decompress_threads + i, "decompress",
2252 do_data_decompress, decomp_param + i,
2253 QEMU_THREAD_JOINABLE);
2254 }
2255}
2256
2257void migrate_decompress_threads_join(void)
2258{
2259 int i, thread_count;
2260
Juan Quintela3416ab52016-04-20 11:56:01 +02002261 if (!migrate_use_compression()) {
2262 return;
2263 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002264 thread_count = migrate_decompress_threads();
2265 for (i = 0; i < thread_count; i++) {
2266 qemu_mutex_lock(&decomp_param[i].mutex);
Liang Li90e56fb2016-05-05 15:32:56 +08002267 decomp_param[i].quit = true;
Juan Quintela56e93d22015-05-07 19:33:31 +02002268 qemu_cond_signal(&decomp_param[i].cond);
2269 qemu_mutex_unlock(&decomp_param[i].mutex);
2270 }
2271 for (i = 0; i < thread_count; i++) {
2272 qemu_thread_join(decompress_threads + i);
2273 qemu_mutex_destroy(&decomp_param[i].mutex);
2274 qemu_cond_destroy(&decomp_param[i].cond);
2275 g_free(decomp_param[i].compbuf);
2276 }
2277 g_free(decompress_threads);
2278 g_free(decomp_param);
Juan Quintela56e93d22015-05-07 19:33:31 +02002279 decompress_threads = NULL;
2280 decomp_param = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002281}
2282
Dr. David Alan Gilbertc1bc6622015-12-16 11:47:38 +00002283static void decompress_data_with_multi_threads(QEMUFile *f,
Juan Quintela56e93d22015-05-07 19:33:31 +02002284 void *host, int len)
2285{
2286 int idx, thread_count;
2287
2288 thread_count = migrate_decompress_threads();
Liang Li73a89122016-05-05 15:32:51 +08002289 qemu_mutex_lock(&decomp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02002290 while (true) {
2291 for (idx = 0; idx < thread_count; idx++) {
Liang Li73a89122016-05-05 15:32:51 +08002292 if (decomp_param[idx].done) {
Liang Li33d151f2016-05-05 15:32:58 +08002293 decomp_param[idx].done = false;
2294 qemu_mutex_lock(&decomp_param[idx].mutex);
Dr. David Alan Gilbertc1bc6622015-12-16 11:47:38 +00002295 qemu_get_buffer(f, decomp_param[idx].compbuf, len);
Juan Quintela56e93d22015-05-07 19:33:31 +02002296 decomp_param[idx].des = host;
2297 decomp_param[idx].len = len;
Liang Li33d151f2016-05-05 15:32:58 +08002298 qemu_cond_signal(&decomp_param[idx].cond);
2299 qemu_mutex_unlock(&decomp_param[idx].mutex);
Juan Quintela56e93d22015-05-07 19:33:31 +02002300 break;
2301 }
2302 }
2303 if (idx < thread_count) {
2304 break;
Liang Li73a89122016-05-05 15:32:51 +08002305 } else {
2306 qemu_cond_wait(&decomp_done_cond, &decomp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02002307 }
2308 }
Liang Li73a89122016-05-05 15:32:51 +08002309 qemu_mutex_unlock(&decomp_done_lock);
Juan Quintela56e93d22015-05-07 19:33:31 +02002310}
2311
Juan Quintela3d0684b2017-03-23 15:06:39 +01002312/**
2313 * ram_postcopy_incoming_init: allocate postcopy data structures
2314 *
2315 * Returns 0 for success and negative if there was one error
2316 *
2317 * @mis: current migration incoming state
2318 *
2319 * Allocate data structures etc needed by incoming migration with
2320 * postcopy-ram. postcopy-ram's similarly names
2321 * postcopy_ram_incoming_init does the work.
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +00002322 */
2323int ram_postcopy_incoming_init(MigrationIncomingState *mis)
2324{
Juan Quintelab8c48992017-03-21 17:44:30 +01002325 unsigned long ram_pages = last_ram_page();
Dr. David Alan Gilbert1caddf82015-11-05 18:11:03 +00002326
2327 return postcopy_ram_incoming_init(mis, ram_pages);
2328}
2329
Juan Quintela3d0684b2017-03-23 15:06:39 +01002330/**
2331 * ram_load_postcopy: load a page in postcopy case
2332 *
2333 * Returns 0 for success or -errno in case of error
2334 *
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002335 * Called in postcopy mode by ram_load().
2336 * rcu_read_lock is taken prior to this being called.
Juan Quintela3d0684b2017-03-23 15:06:39 +01002337 *
2338 * @f: QEMUFile where to send the data
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002339 */
2340static int ram_load_postcopy(QEMUFile *f)
2341{
2342 int flags = 0, ret = 0;
2343 bool place_needed = false;
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002344 bool matching_page_sizes = false;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002345 MigrationIncomingState *mis = migration_incoming_get_current();
2346 /* Temporary page that is later 'placed' */
2347 void *postcopy_host_page = postcopy_get_tmp_page(mis);
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002348 void *last_host = NULL;
Dr. David Alan Gilberta3b6ff62015-11-11 14:02:28 +00002349 bool all_zero = false;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002350
2351 while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) {
2352 ram_addr_t addr;
2353 void *host = NULL;
2354 void *page_buffer = NULL;
2355 void *place_source = NULL;
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002356 RAMBlock *block = NULL;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002357 uint8_t ch;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002358
2359 addr = qemu_get_be64(f);
2360 flags = addr & ~TARGET_PAGE_MASK;
2361 addr &= TARGET_PAGE_MASK;
2362
2363 trace_ram_load_postcopy_loop((uint64_t)addr, flags);
2364 place_needed = false;
Juan Quintelabb890ed2017-04-28 09:39:55 +02002365 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE)) {
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002366 block = ram_block_from_stream(f, flags);
zhanghailiang4c4bad42016-01-15 11:37:41 +08002367
2368 host = host_from_ram_block_offset(block, addr);
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002369 if (!host) {
2370 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
2371 ret = -EINVAL;
2372 break;
2373 }
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002374 matching_page_sizes = block->page_size == TARGET_PAGE_SIZE;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002375 /*
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002376 * Postcopy requires that we place whole host pages atomically;
2377 * these may be huge pages for RAMBlocks that are backed by
2378 * hugetlbfs.
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002379 * To make it atomic, the data is read into a temporary page
2380 * that's moved into place later.
2381 * The migration protocol uses, possibly smaller, target-pages
2382 * however the source ensures it always sends all the components
2383 * of a host page in order.
2384 */
2385 page_buffer = postcopy_host_page +
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002386 ((uintptr_t)host & (block->page_size - 1));
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002387 /* If all TP are zero then we can optimise the place */
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002388 if (!((uintptr_t)host & (block->page_size - 1))) {
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002389 all_zero = true;
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002390 } else {
2391 /* not the 1st TP within the HP */
2392 if (host != (last_host + TARGET_PAGE_SIZE)) {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +01002393 error_report("Non-sequential target page %p/%p",
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002394 host, last_host);
2395 ret = -EINVAL;
2396 break;
2397 }
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002398 }
2399
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002400
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002401 /*
2402 * If it's the last part of a host page then we place the host
2403 * page
2404 */
2405 place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) &
Dr. David Alan Gilbert28abd202017-02-24 18:28:37 +00002406 (block->page_size - 1)) == 0;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002407 place_source = postcopy_host_page;
2408 }
Dr. David Alan Gilbertc53b7dd2015-11-05 18:11:12 +00002409 last_host = host;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002410
2411 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
Juan Quintelabb890ed2017-04-28 09:39:55 +02002412 case RAM_SAVE_FLAG_ZERO:
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002413 ch = qemu_get_byte(f);
2414 memset(page_buffer, ch, TARGET_PAGE_SIZE);
2415 if (ch) {
2416 all_zero = false;
2417 }
2418 break;
2419
2420 case RAM_SAVE_FLAG_PAGE:
2421 all_zero = false;
2422 if (!place_needed || !matching_page_sizes) {
2423 qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE);
2424 } else {
2425 /* Avoids the qemu_file copy during postcopy, which is
2426 * going to do a copy later; can only do it when we
2427 * do this read in one go (matching page sizes)
2428 */
2429 qemu_get_buffer_in_place(f, (uint8_t **)&place_source,
2430 TARGET_PAGE_SIZE);
2431 }
2432 break;
2433 case RAM_SAVE_FLAG_EOS:
2434 /* normal exit */
2435 break;
2436 default:
2437 error_report("Unknown combination of migration flags: %#x"
2438 " (postcopy mode)", flags);
2439 ret = -EINVAL;
2440 }
2441
2442 if (place_needed) {
2443 /* This gets called at the last target page in the host page */
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002444 void *place_dest = host + TARGET_PAGE_SIZE - block->page_size;
2445
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002446 if (all_zero) {
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002447 ret = postcopy_place_page_zero(mis, place_dest,
2448 block->page_size);
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002449 } else {
Dr. David Alan Gilbertdf9ff5e2017-02-24 18:28:35 +00002450 ret = postcopy_place_page(mis, place_dest,
2451 place_source, block->page_size);
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002452 }
2453 }
2454 if (!ret) {
2455 ret = qemu_file_get_error(f);
2456 }
2457 }
2458
2459 return ret;
2460}
2461
Juan Quintela56e93d22015-05-07 19:33:31 +02002462static int ram_load(QEMUFile *f, void *opaque, int version_id)
2463{
Juan Quintelaedc60122016-11-02 12:40:46 +01002464 int flags = 0, ret = 0, invalid_flags = 0;
Juan Quintela56e93d22015-05-07 19:33:31 +02002465 static uint64_t seq_iter;
2466 int len = 0;
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002467 /*
2468 * If system is running in postcopy mode, page inserts to host memory must
2469 * be atomic
2470 */
2471 bool postcopy_running = postcopy_state_get() >= POSTCOPY_INCOMING_LISTENING;
Dr. David Alan Gilbertef08fb32017-02-24 18:28:30 +00002472 /* ADVISE is earlier, it shows the source has the postcopy capability on */
2473 bool postcopy_advised = postcopy_state_get() >= POSTCOPY_INCOMING_ADVISE;
Juan Quintela56e93d22015-05-07 19:33:31 +02002474
2475 seq_iter++;
2476
2477 if (version_id != 4) {
2478 ret = -EINVAL;
2479 }
2480
Juan Quintelaedc60122016-11-02 12:40:46 +01002481 if (!migrate_use_compression()) {
2482 invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE;
2483 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002484 /* This RCU critical section can be very long running.
2485 * When RCU reclaims in the code start to become numerous,
2486 * it will be necessary to reduce the granularity of this
2487 * critical section.
2488 */
2489 rcu_read_lock();
Dr. David Alan Gilberta7180872015-11-05 18:11:11 +00002490
2491 if (postcopy_running) {
2492 ret = ram_load_postcopy(f);
2493 }
2494
2495 while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) {
Juan Quintela56e93d22015-05-07 19:33:31 +02002496 ram_addr_t addr, total_ram_bytes;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002497 void *host = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002498 uint8_t ch;
2499
2500 addr = qemu_get_be64(f);
2501 flags = addr & ~TARGET_PAGE_MASK;
2502 addr &= TARGET_PAGE_MASK;
2503
Juan Quintelaedc60122016-11-02 12:40:46 +01002504 if (flags & invalid_flags) {
2505 if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) {
2506 error_report("Received an unexpected compressed page");
2507 }
2508
2509 ret = -EINVAL;
2510 break;
2511 }
2512
Juan Quintelabb890ed2017-04-28 09:39:55 +02002513 if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE |
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002514 RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) {
zhanghailiang4c4bad42016-01-15 11:37:41 +08002515 RAMBlock *block = ram_block_from_stream(f, flags);
2516
2517 host = host_from_ram_block_offset(block, addr);
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002518 if (!host) {
2519 error_report("Illegal RAM offset " RAM_ADDR_FMT, addr);
2520 ret = -EINVAL;
2521 break;
2522 }
Dr. David Alan Gilbert1db9d8e2017-04-26 19:37:21 +01002523 trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002524 }
2525
Juan Quintela56e93d22015-05-07 19:33:31 +02002526 switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
2527 case RAM_SAVE_FLAG_MEM_SIZE:
2528 /* Synchronize RAM block list */
2529 total_ram_bytes = addr;
2530 while (!ret && total_ram_bytes) {
2531 RAMBlock *block;
Juan Quintela56e93d22015-05-07 19:33:31 +02002532 char id[256];
2533 ram_addr_t length;
2534
2535 len = qemu_get_byte(f);
2536 qemu_get_buffer(f, (uint8_t *)id, len);
2537 id[len] = 0;
2538 length = qemu_get_be64(f);
2539
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002540 block = qemu_ram_block_by_name(id);
2541 if (block) {
2542 if (length != block->used_length) {
2543 Error *local_err = NULL;
Juan Quintela56e93d22015-05-07 19:33:31 +02002544
Gongleifa53a0e2016-05-10 10:04:59 +08002545 ret = qemu_ram_resize(block, length,
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002546 &local_err);
2547 if (local_err) {
2548 error_report_err(local_err);
Juan Quintela56e93d22015-05-07 19:33:31 +02002549 }
Juan Quintela56e93d22015-05-07 19:33:31 +02002550 }
Dr. David Alan Gilbertef08fb32017-02-24 18:28:30 +00002551 /* For postcopy we need to check hugepage sizes match */
2552 if (postcopy_advised &&
2553 block->page_size != qemu_host_page_size) {
2554 uint64_t remote_page_size = qemu_get_be64(f);
2555 if (remote_page_size != block->page_size) {
2556 error_report("Mismatched RAM page size %s "
2557 "(local) %zd != %" PRId64,
2558 id, block->page_size,
2559 remote_page_size);
2560 ret = -EINVAL;
2561 }
2562 }
Dr. David Alan Gilberte3dd7492015-11-05 18:10:33 +00002563 ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG,
2564 block->idstr);
2565 } else {
Juan Quintela56e93d22015-05-07 19:33:31 +02002566 error_report("Unknown ramblock \"%s\", cannot "
2567 "accept migration", id);
2568 ret = -EINVAL;
2569 }
2570
2571 total_ram_bytes -= length;
2572 }
2573 break;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002574
Juan Quintelabb890ed2017-04-28 09:39:55 +02002575 case RAM_SAVE_FLAG_ZERO:
Juan Quintela56e93d22015-05-07 19:33:31 +02002576 ch = qemu_get_byte(f);
2577 ram_handle_compressed(host, ch, TARGET_PAGE_SIZE);
2578 break;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002579
Juan Quintela56e93d22015-05-07 19:33:31 +02002580 case RAM_SAVE_FLAG_PAGE:
Juan Quintela56e93d22015-05-07 19:33:31 +02002581 qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
2582 break;
Juan Quintela56e93d22015-05-07 19:33:31 +02002583
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002584 case RAM_SAVE_FLAG_COMPRESS_PAGE:
Juan Quintela56e93d22015-05-07 19:33:31 +02002585 len = qemu_get_be32(f);
2586 if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) {
2587 error_report("Invalid compressed data length: %d", len);
2588 ret = -EINVAL;
2589 break;
2590 }
Dr. David Alan Gilbertc1bc6622015-12-16 11:47:38 +00002591 decompress_data_with_multi_threads(f, host, len);
Juan Quintela56e93d22015-05-07 19:33:31 +02002592 break;
Dr. David Alan Gilberta776aa12015-11-05 18:10:39 +00002593
Juan Quintela56e93d22015-05-07 19:33:31 +02002594 case RAM_SAVE_FLAG_XBZRLE:
Juan Quintela56e93d22015-05-07 19:33:31 +02002595 if (load_xbzrle(f, addr, host) < 0) {
2596 error_report("Failed to decompress XBZRLE page at "
2597 RAM_ADDR_FMT, addr);
2598 ret = -EINVAL;
2599 break;
2600 }
2601 break;
2602 case RAM_SAVE_FLAG_EOS:
2603 /* normal exit */
2604 break;
2605 default:
2606 if (flags & RAM_SAVE_FLAG_HOOK) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +01002607 ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
Juan Quintela56e93d22015-05-07 19:33:31 +02002608 } else {
2609 error_report("Unknown combination of migration flags: %#x",
2610 flags);
2611 ret = -EINVAL;
2612 }
2613 }
2614 if (!ret) {
2615 ret = qemu_file_get_error(f);
2616 }
2617 }
2618
Liang Li5533b2e2016-05-05 15:32:52 +08002619 wait_for_decompress_done();
Juan Quintela56e93d22015-05-07 19:33:31 +02002620 rcu_read_unlock();
Juan Quintela55c44462017-01-23 22:32:05 +01002621 trace_ram_load_complete(ret, seq_iter);
Juan Quintela56e93d22015-05-07 19:33:31 +02002622 return ret;
2623}
2624
2625static SaveVMHandlers savevm_ram_handlers = {
Juan Quintela9907e842017-06-28 11:52:24 +02002626 .save_setup = ram_save_setup,
Juan Quintela56e93d22015-05-07 19:33:31 +02002627 .save_live_iterate = ram_save_iterate,
Dr. David Alan Gilbert763c9062015-11-05 18:11:00 +00002628 .save_live_complete_postcopy = ram_save_complete,
Dr. David Alan Gilberta3e06c32015-11-05 18:10:41 +00002629 .save_live_complete_precopy = ram_save_complete,
Juan Quintela56e93d22015-05-07 19:33:31 +02002630 .save_live_pending = ram_save_pending,
2631 .load_state = ram_load,
Liang Li6ad2a212015-11-02 15:37:03 +08002632 .cleanup = ram_migration_cleanup,
Juan Quintela56e93d22015-05-07 19:33:31 +02002633};
2634
2635void ram_mig_init(void)
2636{
2637 qemu_mutex_init(&XBZRLE.lock);
Juan Quintela6f37bb82017-03-13 19:26:29 +01002638 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, &ram_state);
Juan Quintela56e93d22015-05-07 19:33:31 +02002639}