]>
Commit | Line | Data |
---|---|---|
56e93d26 JQ |
1 | /* |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
76cc7b58 JQ |
5 | * Copyright (c) 2011-2015 Red Hat Inc |
6 | * | |
7 | * Authors: | |
8 | * Juan Quintela <[email protected]> | |
56e93d26 JQ |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
e688df6b | 28 | |
1393a485 | 29 | #include "qemu/osdep.h" |
33c11879 | 30 | #include "cpu.h" |
56e93d26 | 31 | #include <zlib.h> |
f348b6d1 | 32 | #include "qemu/cutils.h" |
56e93d26 JQ |
33 | #include "qemu/bitops.h" |
34 | #include "qemu/bitmap.h" | |
7205c9ec | 35 | #include "qemu/main-loop.h" |
56eb90af | 36 | #include "qemu/pmem.h" |
709e3fe8 | 37 | #include "xbzrle.h" |
7b1e1a22 | 38 | #include "ram.h" |
6666c96a | 39 | #include "migration.h" |
71bb07db | 40 | #include "socket.h" |
f2a8f0a6 | 41 | #include "migration/register.h" |
7b1e1a22 | 42 | #include "migration/misc.h" |
08a0aee1 | 43 | #include "qemu-file.h" |
be07b0ac | 44 | #include "postcopy-ram.h" |
53d37d36 | 45 | #include "page_cache.h" |
56e93d26 | 46 | #include "qemu/error-report.h" |
e688df6b | 47 | #include "qapi/error.h" |
9af23989 | 48 | #include "qapi/qapi-events-migration.h" |
8acabf69 | 49 | #include "qapi/qmp/qerror.h" |
56e93d26 | 50 | #include "trace.h" |
56e93d26 | 51 | #include "exec/ram_addr.h" |
f9494614 | 52 | #include "exec/target_page.h" |
56e93d26 | 53 | #include "qemu/rcu_queue.h" |
a91246c9 | 54 | #include "migration/colo.h" |
53d37d36 | 55 | #include "block.h" |
af8b7d2b JQ |
56 | #include "sysemu/sysemu.h" |
57 | #include "qemu/uuid.h" | |
edd090c7 | 58 | #include "savevm.h" |
b9ee2f7d | 59 | #include "qemu/iov.h" |
56e93d26 | 60 | |
56e93d26 JQ |
61 | /***********************************************************/ |
62 | /* ram save/restore */ | |
63 | ||
bb890ed5 JQ |
64 | /* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it |
65 | * worked for pages that where filled with the same char. We switched | |
66 | * it to only search for the zero value. And to avoid confusion with | |
67 | * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it. | |
68 | */ | |
69 | ||
56e93d26 | 70 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
bb890ed5 | 71 | #define RAM_SAVE_FLAG_ZERO 0x02 |
56e93d26 JQ |
72 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
73 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
74 | #define RAM_SAVE_FLAG_EOS 0x10 | |
75 | #define RAM_SAVE_FLAG_CONTINUE 0x20 | |
76 | #define RAM_SAVE_FLAG_XBZRLE 0x40 | |
77 | /* 0x80 is reserved in migration.h start with 0x100 next */ | |
78 | #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 | |
79 | ||
56e93d26 JQ |
80 | static inline bool is_zero_range(uint8_t *p, uint64_t size) |
81 | { | |
a1febc49 | 82 | return buffer_is_zero(p, size); |
56e93d26 JQ |
83 | } |
84 | ||
9360447d JQ |
85 | XBZRLECacheStats xbzrle_counters; |
86 | ||
56e93d26 JQ |
87 | /* struct contains XBZRLE cache and a static page |
88 | used by the compression */ | |
89 | static struct { | |
90 | /* buffer used for XBZRLE encoding */ | |
91 | uint8_t *encoded_buf; | |
92 | /* buffer for storing page content */ | |
93 | uint8_t *current_buf; | |
94 | /* Cache for XBZRLE, Protected by lock. */ | |
95 | PageCache *cache; | |
96 | QemuMutex lock; | |
c00e0928 JQ |
97 | /* it will store a page full of zeros */ |
98 | uint8_t *zero_target_page; | |
f265e0e4 JQ |
99 | /* buffer used for XBZRLE decoding */ |
100 | uint8_t *decoded_buf; | |
56e93d26 JQ |
101 | } XBZRLE; |
102 | ||
56e93d26 JQ |
103 | static void XBZRLE_cache_lock(void) |
104 | { | |
105 | if (migrate_use_xbzrle()) | |
106 | qemu_mutex_lock(&XBZRLE.lock); | |
107 | } | |
108 | ||
109 | static void XBZRLE_cache_unlock(void) | |
110 | { | |
111 | if (migrate_use_xbzrle()) | |
112 | qemu_mutex_unlock(&XBZRLE.lock); | |
113 | } | |
114 | ||
3d0684b2 JQ |
115 | /** |
116 | * xbzrle_cache_resize: resize the xbzrle cache | |
117 | * | |
118 | * This function is called from qmp_migrate_set_cache_size in main | |
119 | * thread, possibly while a migration is in progress. A running | |
120 | * migration may be using the cache and might finish during this call, | |
121 | * hence changes to the cache are protected by XBZRLE.lock(). | |
122 | * | |
c9dede2d | 123 | * Returns 0 for success or -1 for error |
3d0684b2 JQ |
124 | * |
125 | * @new_size: new cache size | |
8acabf69 | 126 | * @errp: set *errp if the check failed, with reason |
56e93d26 | 127 | */ |
c9dede2d | 128 | int xbzrle_cache_resize(int64_t new_size, Error **errp) |
56e93d26 JQ |
129 | { |
130 | PageCache *new_cache; | |
c9dede2d | 131 | int64_t ret = 0; |
56e93d26 | 132 | |
8acabf69 JQ |
133 | /* Check for truncation */ |
134 | if (new_size != (size_t)new_size) { | |
135 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
136 | "exceeding address space"); | |
137 | return -1; | |
138 | } | |
139 | ||
2a313e5c JQ |
140 | if (new_size == migrate_xbzrle_cache_size()) { |
141 | /* nothing to do */ | |
c9dede2d | 142 | return 0; |
2a313e5c JQ |
143 | } |
144 | ||
56e93d26 JQ |
145 | XBZRLE_cache_lock(); |
146 | ||
147 | if (XBZRLE.cache != NULL) { | |
80f8dfde | 148 | new_cache = cache_init(new_size, TARGET_PAGE_SIZE, errp); |
56e93d26 | 149 | if (!new_cache) { |
56e93d26 JQ |
150 | ret = -1; |
151 | goto out; | |
152 | } | |
153 | ||
154 | cache_fini(XBZRLE.cache); | |
155 | XBZRLE.cache = new_cache; | |
156 | } | |
56e93d26 JQ |
157 | out: |
158 | XBZRLE_cache_unlock(); | |
159 | return ret; | |
160 | } | |
161 | ||
fbd162e6 YK |
162 | static bool ramblock_is_ignored(RAMBlock *block) |
163 | { | |
164 | return !qemu_ram_is_migratable(block) || | |
165 | (migrate_ignore_shared() && qemu_ram_is_shared(block)); | |
166 | } | |
167 | ||
b895de50 | 168 | /* Should be holding either ram_list.mutex, or the RCU lock. */ |
fbd162e6 YK |
169 | #define RAMBLOCK_FOREACH_NOT_IGNORED(block) \ |
170 | INTERNAL_RAMBLOCK_FOREACH(block) \ | |
171 | if (ramblock_is_ignored(block)) {} else | |
172 | ||
b895de50 | 173 | #define RAMBLOCK_FOREACH_MIGRATABLE(block) \ |
343f632c | 174 | INTERNAL_RAMBLOCK_FOREACH(block) \ |
b895de50 CLG |
175 | if (!qemu_ram_is_migratable(block)) {} else |
176 | ||
343f632c DDAG |
177 | #undef RAMBLOCK_FOREACH |
178 | ||
fbd162e6 YK |
179 | int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque) |
180 | { | |
181 | RAMBlock *block; | |
182 | int ret = 0; | |
183 | ||
184 | rcu_read_lock(); | |
185 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
186 | ret = func(block, opaque); | |
187 | if (ret) { | |
188 | break; | |
189 | } | |
190 | } | |
191 | rcu_read_unlock(); | |
192 | return ret; | |
193 | } | |
194 | ||
f9494614 AP |
195 | static void ramblock_recv_map_init(void) |
196 | { | |
197 | RAMBlock *rb; | |
198 | ||
fbd162e6 | 199 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
f9494614 AP |
200 | assert(!rb->receivedmap); |
201 | rb->receivedmap = bitmap_new(rb->max_length >> qemu_target_page_bits()); | |
202 | } | |
203 | } | |
204 | ||
205 | int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_addr) | |
206 | { | |
207 | return test_bit(ramblock_recv_bitmap_offset(host_addr, rb), | |
208 | rb->receivedmap); | |
209 | } | |
210 | ||
1cba9f6e DDAG |
211 | bool ramblock_recv_bitmap_test_byte_offset(RAMBlock *rb, uint64_t byte_offset) |
212 | { | |
213 | return test_bit(byte_offset >> TARGET_PAGE_BITS, rb->receivedmap); | |
214 | } | |
215 | ||
f9494614 AP |
216 | void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr) |
217 | { | |
218 | set_bit_atomic(ramblock_recv_bitmap_offset(host_addr, rb), rb->receivedmap); | |
219 | } | |
220 | ||
221 | void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr, | |
222 | size_t nr) | |
223 | { | |
224 | bitmap_set_atomic(rb->receivedmap, | |
225 | ramblock_recv_bitmap_offset(host_addr, rb), | |
226 | nr); | |
227 | } | |
228 | ||
a335debb PX |
229 | #define RAMBLOCK_RECV_BITMAP_ENDING (0x0123456789abcdefULL) |
230 | ||
231 | /* | |
232 | * Format: bitmap_size (8 bytes) + whole_bitmap (N bytes). | |
233 | * | |
234 | * Returns >0 if success with sent bytes, or <0 if error. | |
235 | */ | |
236 | int64_t ramblock_recv_bitmap_send(QEMUFile *file, | |
237 | const char *block_name) | |
238 | { | |
239 | RAMBlock *block = qemu_ram_block_by_name(block_name); | |
240 | unsigned long *le_bitmap, nbits; | |
241 | uint64_t size; | |
242 | ||
243 | if (!block) { | |
244 | error_report("%s: invalid block name: %s", __func__, block_name); | |
245 | return -1; | |
246 | } | |
247 | ||
248 | nbits = block->used_length >> TARGET_PAGE_BITS; | |
249 | ||
250 | /* | |
251 | * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit | |
252 | * machines we may need 4 more bytes for padding (see below | |
253 | * comment). So extend it a bit before hand. | |
254 | */ | |
255 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); | |
256 | ||
257 | /* | |
258 | * Always use little endian when sending the bitmap. This is | |
259 | * required that when source and destination VMs are not using the | |
260 | * same endianess. (Note: big endian won't work.) | |
261 | */ | |
262 | bitmap_to_le(le_bitmap, block->receivedmap, nbits); | |
263 | ||
264 | /* Size of the bitmap, in bytes */ | |
a725ef9f | 265 | size = DIV_ROUND_UP(nbits, 8); |
a335debb PX |
266 | |
267 | /* | |
268 | * size is always aligned to 8 bytes for 64bit machines, but it | |
269 | * may not be true for 32bit machines. We need this padding to | |
270 | * make sure the migration can survive even between 32bit and | |
271 | * 64bit machines. | |
272 | */ | |
273 | size = ROUND_UP(size, 8); | |
274 | ||
275 | qemu_put_be64(file, size); | |
276 | qemu_put_buffer(file, (const uint8_t *)le_bitmap, size); | |
277 | /* | |
278 | * Mark as an end, in case the middle part is screwed up due to | |
279 | * some "misterious" reason. | |
280 | */ | |
281 | qemu_put_be64(file, RAMBLOCK_RECV_BITMAP_ENDING); | |
282 | qemu_fflush(file); | |
283 | ||
bf269906 | 284 | g_free(le_bitmap); |
a335debb PX |
285 | |
286 | if (qemu_file_get_error(file)) { | |
287 | return qemu_file_get_error(file); | |
288 | } | |
289 | ||
290 | return size + sizeof(size); | |
291 | } | |
292 | ||
ec481c6c JQ |
293 | /* |
294 | * An outstanding page request, on the source, having been received | |
295 | * and queued | |
296 | */ | |
297 | struct RAMSrcPageRequest { | |
298 | RAMBlock *rb; | |
299 | hwaddr offset; | |
300 | hwaddr len; | |
301 | ||
302 | QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req; | |
303 | }; | |
304 | ||
6f37bb8b JQ |
305 | /* State of RAM for migration */ |
306 | struct RAMState { | |
204b88b8 JQ |
307 | /* QEMUFile used for this migration */ |
308 | QEMUFile *f; | |
6f37bb8b JQ |
309 | /* Last block that we have visited searching for dirty pages */ |
310 | RAMBlock *last_seen_block; | |
311 | /* Last block from where we have sent data */ | |
312 | RAMBlock *last_sent_block; | |
269ace29 JQ |
313 | /* Last dirty target page we have sent */ |
314 | ram_addr_t last_page; | |
6f37bb8b JQ |
315 | /* last ram version we have seen */ |
316 | uint32_t last_version; | |
317 | /* We are in the first round */ | |
318 | bool ram_bulk_stage; | |
6eeb63f7 WW |
319 | /* The free page optimization is enabled */ |
320 | bool fpo_enabled; | |
8d820d6f JQ |
321 | /* How many times we have dirty too many pages */ |
322 | int dirty_rate_high_cnt; | |
f664da80 JQ |
323 | /* these variables are used for bitmap sync */ |
324 | /* last time we did a full bitmap_sync */ | |
325 | int64_t time_last_bitmap_sync; | |
eac74159 | 326 | /* bytes transferred at start_time */ |
c4bdf0cf | 327 | uint64_t bytes_xfer_prev; |
a66cd90c | 328 | /* number of dirty pages since start_time */ |
68908ed6 | 329 | uint64_t num_dirty_pages_period; |
b5833fde JQ |
330 | /* xbzrle misses since the beginning of the period */ |
331 | uint64_t xbzrle_cache_miss_prev; | |
76e03000 XG |
332 | |
333 | /* compression statistics since the beginning of the period */ | |
334 | /* amount of count that no free thread to compress data */ | |
335 | uint64_t compress_thread_busy_prev; | |
336 | /* amount bytes after compression */ | |
337 | uint64_t compressed_size_prev; | |
338 | /* amount of compressed pages */ | |
339 | uint64_t compress_pages_prev; | |
340 | ||
be8b02ed XG |
341 | /* total handled target pages at the beginning of period */ |
342 | uint64_t target_page_count_prev; | |
343 | /* total handled target pages since start */ | |
344 | uint64_t target_page_count; | |
9360447d | 345 | /* number of dirty bits in the bitmap */ |
2dfaf12e | 346 | uint64_t migration_dirty_pages; |
386a907b | 347 | /* Protects modification of the bitmap and migration dirty pages */ |
108cfae0 | 348 | QemuMutex bitmap_mutex; |
68a098f3 JQ |
349 | /* The RAMBlock used in the last src_page_requests */ |
350 | RAMBlock *last_req_rb; | |
ec481c6c JQ |
351 | /* Queue of outstanding page requests from the destination */ |
352 | QemuMutex src_page_req_mutex; | |
b58deb34 | 353 | QSIMPLEQ_HEAD(, RAMSrcPageRequest) src_page_requests; |
6f37bb8b JQ |
354 | }; |
355 | typedef struct RAMState RAMState; | |
356 | ||
53518d94 | 357 | static RAMState *ram_state; |
6f37bb8b | 358 | |
bd227060 WW |
359 | static NotifierWithReturnList precopy_notifier_list; |
360 | ||
361 | void precopy_infrastructure_init(void) | |
362 | { | |
363 | notifier_with_return_list_init(&precopy_notifier_list); | |
364 | } | |
365 | ||
366 | void precopy_add_notifier(NotifierWithReturn *n) | |
367 | { | |
368 | notifier_with_return_list_add(&precopy_notifier_list, n); | |
369 | } | |
370 | ||
371 | void precopy_remove_notifier(NotifierWithReturn *n) | |
372 | { | |
373 | notifier_with_return_remove(n); | |
374 | } | |
375 | ||
376 | int precopy_notify(PrecopyNotifyReason reason, Error **errp) | |
377 | { | |
378 | PrecopyNotifyData pnd; | |
379 | pnd.reason = reason; | |
380 | pnd.errp = errp; | |
381 | ||
382 | return notifier_with_return_list_notify(&precopy_notifier_list, &pnd); | |
383 | } | |
384 | ||
6eeb63f7 WW |
385 | void precopy_enable_free_page_optimization(void) |
386 | { | |
387 | if (!ram_state) { | |
388 | return; | |
389 | } | |
390 | ||
391 | ram_state->fpo_enabled = true; | |
392 | } | |
393 | ||
9edabd4d | 394 | uint64_t ram_bytes_remaining(void) |
2f4fde93 | 395 | { |
bae416e5 DDAG |
396 | return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) : |
397 | 0; | |
2f4fde93 JQ |
398 | } |
399 | ||
9360447d | 400 | MigrationStats ram_counters; |
96506894 | 401 | |
b8fb8cb7 DDAG |
402 | /* used by the search for pages to send */ |
403 | struct PageSearchStatus { | |
404 | /* Current block being searched */ | |
405 | RAMBlock *block; | |
a935e30f JQ |
406 | /* Current page to search from */ |
407 | unsigned long page; | |
b8fb8cb7 DDAG |
408 | /* Set once we wrap around */ |
409 | bool complete_round; | |
410 | }; | |
411 | typedef struct PageSearchStatus PageSearchStatus; | |
412 | ||
76e03000 XG |
413 | CompressionStats compression_counters; |
414 | ||
56e93d26 | 415 | struct CompressParam { |
56e93d26 | 416 | bool done; |
90e56fb4 | 417 | bool quit; |
5e5fdcff | 418 | bool zero_page; |
56e93d26 JQ |
419 | QEMUFile *file; |
420 | QemuMutex mutex; | |
421 | QemuCond cond; | |
422 | RAMBlock *block; | |
423 | ram_addr_t offset; | |
34ab9e97 XG |
424 | |
425 | /* internally used fields */ | |
dcaf446e | 426 | z_stream stream; |
34ab9e97 | 427 | uint8_t *originbuf; |
56e93d26 JQ |
428 | }; |
429 | typedef struct CompressParam CompressParam; | |
430 | ||
431 | struct DecompressParam { | |
73a8912b | 432 | bool done; |
90e56fb4 | 433 | bool quit; |
56e93d26 JQ |
434 | QemuMutex mutex; |
435 | QemuCond cond; | |
436 | void *des; | |
d341d9f3 | 437 | uint8_t *compbuf; |
56e93d26 | 438 | int len; |
797ca154 | 439 | z_stream stream; |
56e93d26 JQ |
440 | }; |
441 | typedef struct DecompressParam DecompressParam; | |
442 | ||
443 | static CompressParam *comp_param; | |
444 | static QemuThread *compress_threads; | |
445 | /* comp_done_cond is used to wake up the migration thread when | |
446 | * one of the compression threads has finished the compression. | |
447 | * comp_done_lock is used to co-work with comp_done_cond. | |
448 | */ | |
0d9f9a5c LL |
449 | static QemuMutex comp_done_lock; |
450 | static QemuCond comp_done_cond; | |
56e93d26 JQ |
451 | /* The empty QEMUFileOps will be used by file in CompressParam */ |
452 | static const QEMUFileOps empty_ops = { }; | |
453 | ||
34ab9e97 | 454 | static QEMUFile *decomp_file; |
56e93d26 JQ |
455 | static DecompressParam *decomp_param; |
456 | static QemuThread *decompress_threads; | |
73a8912b LL |
457 | static QemuMutex decomp_done_lock; |
458 | static QemuCond decomp_done_cond; | |
56e93d26 | 459 | |
5e5fdcff | 460 | static bool do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block, |
6ef3771c | 461 | ram_addr_t offset, uint8_t *source_buf); |
56e93d26 JQ |
462 | |
463 | static void *do_data_compress(void *opaque) | |
464 | { | |
465 | CompressParam *param = opaque; | |
a7a9a88f LL |
466 | RAMBlock *block; |
467 | ram_addr_t offset; | |
5e5fdcff | 468 | bool zero_page; |
56e93d26 | 469 | |
a7a9a88f | 470 | qemu_mutex_lock(¶m->mutex); |
90e56fb4 | 471 | while (!param->quit) { |
a7a9a88f LL |
472 | if (param->block) { |
473 | block = param->block; | |
474 | offset = param->offset; | |
475 | param->block = NULL; | |
476 | qemu_mutex_unlock(¶m->mutex); | |
477 | ||
5e5fdcff XG |
478 | zero_page = do_compress_ram_page(param->file, ¶m->stream, |
479 | block, offset, param->originbuf); | |
a7a9a88f | 480 | |
0d9f9a5c | 481 | qemu_mutex_lock(&comp_done_lock); |
a7a9a88f | 482 | param->done = true; |
5e5fdcff | 483 | param->zero_page = zero_page; |
0d9f9a5c LL |
484 | qemu_cond_signal(&comp_done_cond); |
485 | qemu_mutex_unlock(&comp_done_lock); | |
a7a9a88f LL |
486 | |
487 | qemu_mutex_lock(¶m->mutex); | |
488 | } else { | |
56e93d26 JQ |
489 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
490 | } | |
56e93d26 | 491 | } |
a7a9a88f | 492 | qemu_mutex_unlock(¶m->mutex); |
56e93d26 JQ |
493 | |
494 | return NULL; | |
495 | } | |
496 | ||
f0afa331 | 497 | static void compress_threads_save_cleanup(void) |
56e93d26 JQ |
498 | { |
499 | int i, thread_count; | |
500 | ||
05306935 | 501 | if (!migrate_use_compression() || !comp_param) { |
56e93d26 JQ |
502 | return; |
503 | } | |
05306935 | 504 | |
56e93d26 JQ |
505 | thread_count = migrate_compress_threads(); |
506 | for (i = 0; i < thread_count; i++) { | |
dcaf446e XG |
507 | /* |
508 | * we use it as a indicator which shows if the thread is | |
509 | * properly init'd or not | |
510 | */ | |
511 | if (!comp_param[i].file) { | |
512 | break; | |
513 | } | |
05306935 FL |
514 | |
515 | qemu_mutex_lock(&comp_param[i].mutex); | |
516 | comp_param[i].quit = true; | |
517 | qemu_cond_signal(&comp_param[i].cond); | |
518 | qemu_mutex_unlock(&comp_param[i].mutex); | |
519 | ||
56e93d26 | 520 | qemu_thread_join(compress_threads + i); |
56e93d26 JQ |
521 | qemu_mutex_destroy(&comp_param[i].mutex); |
522 | qemu_cond_destroy(&comp_param[i].cond); | |
dcaf446e | 523 | deflateEnd(&comp_param[i].stream); |
34ab9e97 | 524 | g_free(comp_param[i].originbuf); |
dcaf446e XG |
525 | qemu_fclose(comp_param[i].file); |
526 | comp_param[i].file = NULL; | |
56e93d26 | 527 | } |
0d9f9a5c LL |
528 | qemu_mutex_destroy(&comp_done_lock); |
529 | qemu_cond_destroy(&comp_done_cond); | |
56e93d26 JQ |
530 | g_free(compress_threads); |
531 | g_free(comp_param); | |
56e93d26 JQ |
532 | compress_threads = NULL; |
533 | comp_param = NULL; | |
56e93d26 JQ |
534 | } |
535 | ||
dcaf446e | 536 | static int compress_threads_save_setup(void) |
56e93d26 JQ |
537 | { |
538 | int i, thread_count; | |
539 | ||
540 | if (!migrate_use_compression()) { | |
dcaf446e | 541 | return 0; |
56e93d26 | 542 | } |
56e93d26 JQ |
543 | thread_count = migrate_compress_threads(); |
544 | compress_threads = g_new0(QemuThread, thread_count); | |
545 | comp_param = g_new0(CompressParam, thread_count); | |
0d9f9a5c LL |
546 | qemu_cond_init(&comp_done_cond); |
547 | qemu_mutex_init(&comp_done_lock); | |
56e93d26 | 548 | for (i = 0; i < thread_count; i++) { |
34ab9e97 XG |
549 | comp_param[i].originbuf = g_try_malloc(TARGET_PAGE_SIZE); |
550 | if (!comp_param[i].originbuf) { | |
551 | goto exit; | |
552 | } | |
553 | ||
dcaf446e XG |
554 | if (deflateInit(&comp_param[i].stream, |
555 | migrate_compress_level()) != Z_OK) { | |
34ab9e97 | 556 | g_free(comp_param[i].originbuf); |
dcaf446e XG |
557 | goto exit; |
558 | } | |
559 | ||
e110aa91 C |
560 | /* comp_param[i].file is just used as a dummy buffer to save data, |
561 | * set its ops to empty. | |
56e93d26 JQ |
562 | */ |
563 | comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops); | |
564 | comp_param[i].done = true; | |
90e56fb4 | 565 | comp_param[i].quit = false; |
56e93d26 JQ |
566 | qemu_mutex_init(&comp_param[i].mutex); |
567 | qemu_cond_init(&comp_param[i].cond); | |
568 | qemu_thread_create(compress_threads + i, "compress", | |
569 | do_data_compress, comp_param + i, | |
570 | QEMU_THREAD_JOINABLE); | |
571 | } | |
dcaf446e XG |
572 | return 0; |
573 | ||
574 | exit: | |
575 | compress_threads_save_cleanup(); | |
576 | return -1; | |
56e93d26 JQ |
577 | } |
578 | ||
f986c3d2 JQ |
579 | /* Multiple fd's */ |
580 | ||
af8b7d2b JQ |
581 | #define MULTIFD_MAGIC 0x11223344U |
582 | #define MULTIFD_VERSION 1 | |
583 | ||
6df264ac JQ |
584 | #define MULTIFD_FLAG_SYNC (1 << 0) |
585 | ||
efd1a1d6 JQ |
586 | /* This value needs to be a multiple of qemu_target_page_size() */ |
587 | #define MULTIFD_PACKET_SIZE (64 * 1024) | |
588 | ||
af8b7d2b JQ |
589 | typedef struct { |
590 | uint32_t magic; | |
591 | uint32_t version; | |
592 | unsigned char uuid[16]; /* QemuUUID */ | |
593 | uint8_t id; | |
594 | } __attribute__((packed)) MultiFDInit_t; | |
595 | ||
2a26c979 JQ |
596 | typedef struct { |
597 | uint32_t magic; | |
598 | uint32_t version; | |
599 | uint32_t flags; | |
6f862692 JQ |
600 | /* maximum number of allocated pages */ |
601 | uint32_t pages_alloc; | |
602 | uint32_t pages_used; | |
2a34ee59 JQ |
603 | /* size of the next packet that contains pages */ |
604 | uint32_t next_packet_size; | |
2a26c979 JQ |
605 | uint64_t packet_num; |
606 | char ramblock[256]; | |
607 | uint64_t offset[]; | |
608 | } __attribute__((packed)) MultiFDPacket_t; | |
609 | ||
34c55a94 JQ |
610 | typedef struct { |
611 | /* number of used pages */ | |
612 | uint32_t used; | |
613 | /* number of allocated pages */ | |
614 | uint32_t allocated; | |
615 | /* global number of generated multifd packets */ | |
616 | uint64_t packet_num; | |
617 | /* offset of each page */ | |
618 | ram_addr_t *offset; | |
619 | /* pointer to each page */ | |
620 | struct iovec *iov; | |
621 | RAMBlock *block; | |
622 | } MultiFDPages_t; | |
623 | ||
8c4598f2 JQ |
624 | typedef struct { |
625 | /* this fields are not changed once the thread is created */ | |
626 | /* channel number */ | |
f986c3d2 | 627 | uint8_t id; |
8c4598f2 | 628 | /* channel thread name */ |
f986c3d2 | 629 | char *name; |
8c4598f2 | 630 | /* channel thread id */ |
f986c3d2 | 631 | QemuThread thread; |
8c4598f2 | 632 | /* communication channel */ |
60df2d4a | 633 | QIOChannel *c; |
8c4598f2 | 634 | /* sem where to wait for more work */ |
f986c3d2 | 635 | QemuSemaphore sem; |
8c4598f2 | 636 | /* this mutex protects the following parameters */ |
f986c3d2 | 637 | QemuMutex mutex; |
8c4598f2 | 638 | /* is this channel thread running */ |
66770707 | 639 | bool running; |
8c4598f2 | 640 | /* should this thread finish */ |
f986c3d2 | 641 | bool quit; |
0beb5ed3 JQ |
642 | /* thread has work to do */ |
643 | int pending_job; | |
34c55a94 JQ |
644 | /* array of pages to sent */ |
645 | MultiFDPages_t *pages; | |
2a26c979 JQ |
646 | /* packet allocated len */ |
647 | uint32_t packet_len; | |
648 | /* pointer to the packet */ | |
649 | MultiFDPacket_t *packet; | |
650 | /* multifd flags for each packet */ | |
651 | uint32_t flags; | |
2a34ee59 JQ |
652 | /* size of the next packet that contains pages */ |
653 | uint32_t next_packet_size; | |
2a26c979 JQ |
654 | /* global number of generated multifd packets */ |
655 | uint64_t packet_num; | |
408ea6ae JQ |
656 | /* thread local variables */ |
657 | /* packets sent through this channel */ | |
658 | uint64_t num_packets; | |
659 | /* pages sent through this channel */ | |
660 | uint64_t num_pages; | |
6df264ac JQ |
661 | /* syncs main thread and channels */ |
662 | QemuSemaphore sem_sync; | |
8c4598f2 JQ |
663 | } MultiFDSendParams; |
664 | ||
665 | typedef struct { | |
666 | /* this fields are not changed once the thread is created */ | |
667 | /* channel number */ | |
668 | uint8_t id; | |
669 | /* channel thread name */ | |
670 | char *name; | |
671 | /* channel thread id */ | |
672 | QemuThread thread; | |
673 | /* communication channel */ | |
674 | QIOChannel *c; | |
8c4598f2 JQ |
675 | /* this mutex protects the following parameters */ |
676 | QemuMutex mutex; | |
677 | /* is this channel thread running */ | |
678 | bool running; | |
34c55a94 JQ |
679 | /* array of pages to receive */ |
680 | MultiFDPages_t *pages; | |
2a26c979 JQ |
681 | /* packet allocated len */ |
682 | uint32_t packet_len; | |
683 | /* pointer to the packet */ | |
684 | MultiFDPacket_t *packet; | |
685 | /* multifd flags for each packet */ | |
686 | uint32_t flags; | |
687 | /* global number of generated multifd packets */ | |
688 | uint64_t packet_num; | |
408ea6ae | 689 | /* thread local variables */ |
2a34ee59 JQ |
690 | /* size of the next packet that contains pages */ |
691 | uint32_t next_packet_size; | |
408ea6ae JQ |
692 | /* packets sent through this channel */ |
693 | uint64_t num_packets; | |
694 | /* pages sent through this channel */ | |
695 | uint64_t num_pages; | |
6df264ac JQ |
696 | /* syncs main thread and channels */ |
697 | QemuSemaphore sem_sync; | |
8c4598f2 | 698 | } MultiFDRecvParams; |
f986c3d2 | 699 | |
af8b7d2b JQ |
700 | static int multifd_send_initial_packet(MultiFDSendParams *p, Error **errp) |
701 | { | |
702 | MultiFDInit_t msg; | |
703 | int ret; | |
704 | ||
705 | msg.magic = cpu_to_be32(MULTIFD_MAGIC); | |
706 | msg.version = cpu_to_be32(MULTIFD_VERSION); | |
707 | msg.id = p->id; | |
708 | memcpy(msg.uuid, &qemu_uuid.data, sizeof(msg.uuid)); | |
709 | ||
710 | ret = qio_channel_write_all(p->c, (char *)&msg, sizeof(msg), errp); | |
711 | if (ret != 0) { | |
712 | return -1; | |
713 | } | |
714 | return 0; | |
715 | } | |
716 | ||
717 | static int multifd_recv_initial_packet(QIOChannel *c, Error **errp) | |
718 | { | |
719 | MultiFDInit_t msg; | |
720 | int ret; | |
721 | ||
722 | ret = qio_channel_read_all(c, (char *)&msg, sizeof(msg), errp); | |
723 | if (ret != 0) { | |
724 | return -1; | |
725 | } | |
726 | ||
341ba0df PM |
727 | msg.magic = be32_to_cpu(msg.magic); |
728 | msg.version = be32_to_cpu(msg.version); | |
af8b7d2b JQ |
729 | |
730 | if (msg.magic != MULTIFD_MAGIC) { | |
731 | error_setg(errp, "multifd: received packet magic %x " | |
732 | "expected %x", msg.magic, MULTIFD_MAGIC); | |
733 | return -1; | |
734 | } | |
735 | ||
736 | if (msg.version != MULTIFD_VERSION) { | |
737 | error_setg(errp, "multifd: received packet version %d " | |
738 | "expected %d", msg.version, MULTIFD_VERSION); | |
739 | return -1; | |
740 | } | |
741 | ||
742 | if (memcmp(msg.uuid, &qemu_uuid, sizeof(qemu_uuid))) { | |
743 | char *uuid = qemu_uuid_unparse_strdup(&qemu_uuid); | |
744 | char *msg_uuid = qemu_uuid_unparse_strdup((const QemuUUID *)msg.uuid); | |
745 | ||
746 | error_setg(errp, "multifd: received uuid '%s' and expected " | |
747 | "uuid '%s' for channel %hhd", msg_uuid, uuid, msg.id); | |
748 | g_free(uuid); | |
749 | g_free(msg_uuid); | |
750 | return -1; | |
751 | } | |
752 | ||
753 | if (msg.id > migrate_multifd_channels()) { | |
754 | error_setg(errp, "multifd: received channel version %d " | |
755 | "expected %d", msg.version, MULTIFD_VERSION); | |
756 | return -1; | |
757 | } | |
758 | ||
759 | return msg.id; | |
760 | } | |
761 | ||
34c55a94 JQ |
762 | static MultiFDPages_t *multifd_pages_init(size_t size) |
763 | { | |
764 | MultiFDPages_t *pages = g_new0(MultiFDPages_t, 1); | |
765 | ||
766 | pages->allocated = size; | |
767 | pages->iov = g_new0(struct iovec, size); | |
768 | pages->offset = g_new0(ram_addr_t, size); | |
769 | ||
770 | return pages; | |
771 | } | |
772 | ||
773 | static void multifd_pages_clear(MultiFDPages_t *pages) | |
774 | { | |
775 | pages->used = 0; | |
776 | pages->allocated = 0; | |
777 | pages->packet_num = 0; | |
778 | pages->block = NULL; | |
779 | g_free(pages->iov); | |
780 | pages->iov = NULL; | |
781 | g_free(pages->offset); | |
782 | pages->offset = NULL; | |
783 | g_free(pages); | |
784 | } | |
785 | ||
2a26c979 JQ |
786 | static void multifd_send_fill_packet(MultiFDSendParams *p) |
787 | { | |
788 | MultiFDPacket_t *packet = p->packet; | |
efd1a1d6 | 789 | uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size(); |
2a26c979 JQ |
790 | int i; |
791 | ||
792 | packet->magic = cpu_to_be32(MULTIFD_MAGIC); | |
793 | packet->version = cpu_to_be32(MULTIFD_VERSION); | |
794 | packet->flags = cpu_to_be32(p->flags); | |
efd1a1d6 | 795 | packet->pages_alloc = cpu_to_be32(page_count); |
6f862692 | 796 | packet->pages_used = cpu_to_be32(p->pages->used); |
2a34ee59 | 797 | packet->next_packet_size = cpu_to_be32(p->next_packet_size); |
2a26c979 JQ |
798 | packet->packet_num = cpu_to_be64(p->packet_num); |
799 | ||
800 | if (p->pages->block) { | |
801 | strncpy(packet->ramblock, p->pages->block->idstr, 256); | |
802 | } | |
803 | ||
804 | for (i = 0; i < p->pages->used; i++) { | |
805 | packet->offset[i] = cpu_to_be64(p->pages->offset[i]); | |
806 | } | |
807 | } | |
808 | ||
809 | static int multifd_recv_unfill_packet(MultiFDRecvParams *p, Error **errp) | |
810 | { | |
811 | MultiFDPacket_t *packet = p->packet; | |
efd1a1d6 | 812 | uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size(); |
2a26c979 JQ |
813 | RAMBlock *block; |
814 | int i; | |
815 | ||
341ba0df | 816 | packet->magic = be32_to_cpu(packet->magic); |
2a26c979 JQ |
817 | if (packet->magic != MULTIFD_MAGIC) { |
818 | error_setg(errp, "multifd: received packet " | |
819 | "magic %x and expected magic %x", | |
820 | packet->magic, MULTIFD_MAGIC); | |
821 | return -1; | |
822 | } | |
823 | ||
341ba0df | 824 | packet->version = be32_to_cpu(packet->version); |
2a26c979 JQ |
825 | if (packet->version != MULTIFD_VERSION) { |
826 | error_setg(errp, "multifd: received packet " | |
827 | "version %d and expected version %d", | |
828 | packet->version, MULTIFD_VERSION); | |
829 | return -1; | |
830 | } | |
831 | ||
832 | p->flags = be32_to_cpu(packet->flags); | |
833 | ||
6f862692 | 834 | packet->pages_alloc = be32_to_cpu(packet->pages_alloc); |
efd1a1d6 | 835 | if (packet->pages_alloc > page_count) { |
2a26c979 JQ |
836 | error_setg(errp, "multifd: received packet " |
837 | "with size %d and expected maximum size %d", | |
efd1a1d6 | 838 | packet->pages_alloc, page_count) ; |
2a26c979 JQ |
839 | return -1; |
840 | } | |
841 | ||
6f862692 JQ |
842 | p->pages->used = be32_to_cpu(packet->pages_used); |
843 | if (p->pages->used > packet->pages_alloc) { | |
2a26c979 | 844 | error_setg(errp, "multifd: received packet " |
6f862692 JQ |
845 | "with %d pages and expected maximum pages are %d", |
846 | p->pages->used, packet->pages_alloc) ; | |
2a26c979 JQ |
847 | return -1; |
848 | } | |
849 | ||
2a34ee59 | 850 | p->next_packet_size = be32_to_cpu(packet->next_packet_size); |
2a26c979 JQ |
851 | p->packet_num = be64_to_cpu(packet->packet_num); |
852 | ||
853 | if (p->pages->used) { | |
854 | /* make sure that ramblock is 0 terminated */ | |
855 | packet->ramblock[255] = 0; | |
856 | block = qemu_ram_block_by_name(packet->ramblock); | |
857 | if (!block) { | |
858 | error_setg(errp, "multifd: unknown ram block %s", | |
859 | packet->ramblock); | |
860 | return -1; | |
861 | } | |
862 | } | |
863 | ||
864 | for (i = 0; i < p->pages->used; i++) { | |
865 | ram_addr_t offset = be64_to_cpu(packet->offset[i]); | |
866 | ||
867 | if (offset > (block->used_length - TARGET_PAGE_SIZE)) { | |
868 | error_setg(errp, "multifd: offset too long " RAM_ADDR_FMT | |
869 | " (max " RAM_ADDR_FMT ")", | |
870 | offset, block->max_length); | |
871 | return -1; | |
872 | } | |
873 | p->pages->iov[i].iov_base = block->host + offset; | |
874 | p->pages->iov[i].iov_len = TARGET_PAGE_SIZE; | |
875 | } | |
876 | ||
877 | return 0; | |
878 | } | |
879 | ||
f986c3d2 JQ |
880 | struct { |
881 | MultiFDSendParams *params; | |
882 | /* number of created threads */ | |
883 | int count; | |
34c55a94 JQ |
884 | /* array of pages to sent */ |
885 | MultiFDPages_t *pages; | |
6df264ac JQ |
886 | /* syncs main thread and channels */ |
887 | QemuSemaphore sem_sync; | |
888 | /* global number of generated multifd packets */ | |
889 | uint64_t packet_num; | |
b9ee2f7d JQ |
890 | /* send channels ready */ |
891 | QemuSemaphore channels_ready; | |
f986c3d2 JQ |
892 | } *multifd_send_state; |
893 | ||
b9ee2f7d JQ |
894 | /* |
895 | * How we use multifd_send_state->pages and channel->pages? | |
896 | * | |
897 | * We create a pages for each channel, and a main one. Each time that | |
898 | * we need to send a batch of pages we interchange the ones between | |
899 | * multifd_send_state and the channel that is sending it. There are | |
900 | * two reasons for that: | |
901 | * - to not have to do so many mallocs during migration | |
902 | * - to make easier to know what to free at the end of migration | |
903 | * | |
904 | * This way we always know who is the owner of each "pages" struct, | |
905 | * and we don't need any loocking. It belongs to the migration thread | |
906 | * or to the channel thread. Switching is safe because the migration | |
907 | * thread is using the channel mutex when changing it, and the channel | |
908 | * have to had finish with its own, otherwise pending_job can't be | |
909 | * false. | |
910 | */ | |
911 | ||
912 | static void multifd_send_pages(void) | |
913 | { | |
914 | int i; | |
915 | static int next_channel; | |
916 | MultiFDSendParams *p = NULL; /* make happy gcc */ | |
917 | MultiFDPages_t *pages = multifd_send_state->pages; | |
918 | uint64_t transferred; | |
919 | ||
920 | qemu_sem_wait(&multifd_send_state->channels_ready); | |
921 | for (i = next_channel;; i = (i + 1) % migrate_multifd_channels()) { | |
922 | p = &multifd_send_state->params[i]; | |
923 | ||
924 | qemu_mutex_lock(&p->mutex); | |
925 | if (!p->pending_job) { | |
926 | p->pending_job++; | |
927 | next_channel = (i + 1) % migrate_multifd_channels(); | |
928 | break; | |
929 | } | |
930 | qemu_mutex_unlock(&p->mutex); | |
931 | } | |
932 | p->pages->used = 0; | |
933 | ||
934 | p->packet_num = multifd_send_state->packet_num++; | |
935 | p->pages->block = NULL; | |
936 | multifd_send_state->pages = p->pages; | |
937 | p->pages = pages; | |
4fcefd44 | 938 | transferred = ((uint64_t) pages->used) * TARGET_PAGE_SIZE + p->packet_len; |
b9ee2f7d JQ |
939 | ram_counters.multifd_bytes += transferred; |
940 | ram_counters.transferred += transferred;; | |
941 | qemu_mutex_unlock(&p->mutex); | |
942 | qemu_sem_post(&p->sem); | |
943 | } | |
944 | ||
945 | static void multifd_queue_page(RAMBlock *block, ram_addr_t offset) | |
946 | { | |
947 | MultiFDPages_t *pages = multifd_send_state->pages; | |
948 | ||
949 | if (!pages->block) { | |
950 | pages->block = block; | |
951 | } | |
952 | ||
953 | if (pages->block == block) { | |
954 | pages->offset[pages->used] = offset; | |
955 | pages->iov[pages->used].iov_base = block->host + offset; | |
956 | pages->iov[pages->used].iov_len = TARGET_PAGE_SIZE; | |
957 | pages->used++; | |
958 | ||
959 | if (pages->used < pages->allocated) { | |
960 | return; | |
961 | } | |
962 | } | |
963 | ||
964 | multifd_send_pages(); | |
965 | ||
966 | if (pages->block != block) { | |
967 | multifd_queue_page(block, offset); | |
968 | } | |
969 | } | |
970 | ||
66770707 | 971 | static void multifd_send_terminate_threads(Error *err) |
f986c3d2 JQ |
972 | { |
973 | int i; | |
974 | ||
7a169d74 JQ |
975 | if (err) { |
976 | MigrationState *s = migrate_get_current(); | |
977 | migrate_set_error(s, err); | |
978 | if (s->state == MIGRATION_STATUS_SETUP || | |
979 | s->state == MIGRATION_STATUS_PRE_SWITCHOVER || | |
980 | s->state == MIGRATION_STATUS_DEVICE || | |
981 | s->state == MIGRATION_STATUS_ACTIVE) { | |
982 | migrate_set_state(&s->state, s->state, | |
983 | MIGRATION_STATUS_FAILED); | |
984 | } | |
985 | } | |
986 | ||
66770707 | 987 | for (i = 0; i < migrate_multifd_channels(); i++) { |
f986c3d2 JQ |
988 | MultiFDSendParams *p = &multifd_send_state->params[i]; |
989 | ||
990 | qemu_mutex_lock(&p->mutex); | |
991 | p->quit = true; | |
992 | qemu_sem_post(&p->sem); | |
993 | qemu_mutex_unlock(&p->mutex); | |
994 | } | |
995 | } | |
996 | ||
1398b2e3 | 997 | void multifd_save_cleanup(void) |
f986c3d2 JQ |
998 | { |
999 | int i; | |
f986c3d2 JQ |
1000 | |
1001 | if (!migrate_use_multifd()) { | |
1398b2e3 | 1002 | return; |
f986c3d2 | 1003 | } |
66770707 JQ |
1004 | multifd_send_terminate_threads(NULL); |
1005 | for (i = 0; i < migrate_multifd_channels(); i++) { | |
f986c3d2 JQ |
1006 | MultiFDSendParams *p = &multifd_send_state->params[i]; |
1007 | ||
66770707 JQ |
1008 | if (p->running) { |
1009 | qemu_thread_join(&p->thread); | |
1010 | } | |
60df2d4a JQ |
1011 | socket_send_channel_destroy(p->c); |
1012 | p->c = NULL; | |
f986c3d2 JQ |
1013 | qemu_mutex_destroy(&p->mutex); |
1014 | qemu_sem_destroy(&p->sem); | |
6df264ac | 1015 | qemu_sem_destroy(&p->sem_sync); |
f986c3d2 JQ |
1016 | g_free(p->name); |
1017 | p->name = NULL; | |
34c55a94 JQ |
1018 | multifd_pages_clear(p->pages); |
1019 | p->pages = NULL; | |
2a26c979 JQ |
1020 | p->packet_len = 0; |
1021 | g_free(p->packet); | |
1022 | p->packet = NULL; | |
f986c3d2 | 1023 | } |
b9ee2f7d | 1024 | qemu_sem_destroy(&multifd_send_state->channels_ready); |
6df264ac | 1025 | qemu_sem_destroy(&multifd_send_state->sem_sync); |
f986c3d2 JQ |
1026 | g_free(multifd_send_state->params); |
1027 | multifd_send_state->params = NULL; | |
34c55a94 JQ |
1028 | multifd_pages_clear(multifd_send_state->pages); |
1029 | multifd_send_state->pages = NULL; | |
f986c3d2 JQ |
1030 | g_free(multifd_send_state); |
1031 | multifd_send_state = NULL; | |
f986c3d2 JQ |
1032 | } |
1033 | ||
6df264ac JQ |
1034 | static void multifd_send_sync_main(void) |
1035 | { | |
1036 | int i; | |
1037 | ||
1038 | if (!migrate_use_multifd()) { | |
1039 | return; | |
1040 | } | |
b9ee2f7d JQ |
1041 | if (multifd_send_state->pages->used) { |
1042 | multifd_send_pages(); | |
1043 | } | |
6df264ac JQ |
1044 | for (i = 0; i < migrate_multifd_channels(); i++) { |
1045 | MultiFDSendParams *p = &multifd_send_state->params[i]; | |
1046 | ||
1047 | trace_multifd_send_sync_main_signal(p->id); | |
1048 | ||
1049 | qemu_mutex_lock(&p->mutex); | |
b9ee2f7d JQ |
1050 | |
1051 | p->packet_num = multifd_send_state->packet_num++; | |
6df264ac JQ |
1052 | p->flags |= MULTIFD_FLAG_SYNC; |
1053 | p->pending_job++; | |
1054 | qemu_mutex_unlock(&p->mutex); | |
1055 | qemu_sem_post(&p->sem); | |
1056 | } | |
1057 | for (i = 0; i < migrate_multifd_channels(); i++) { | |
1058 | MultiFDSendParams *p = &multifd_send_state->params[i]; | |
1059 | ||
1060 | trace_multifd_send_sync_main_wait(p->id); | |
1061 | qemu_sem_wait(&multifd_send_state->sem_sync); | |
1062 | } | |
1063 | trace_multifd_send_sync_main(multifd_send_state->packet_num); | |
1064 | } | |
1065 | ||
f986c3d2 JQ |
1066 | static void *multifd_send_thread(void *opaque) |
1067 | { | |
1068 | MultiFDSendParams *p = opaque; | |
af8b7d2b | 1069 | Error *local_err = NULL; |
8b2db7f5 | 1070 | int ret; |
af8b7d2b | 1071 | |
408ea6ae | 1072 | trace_multifd_send_thread_start(p->id); |
74637e6f | 1073 | rcu_register_thread(); |
408ea6ae | 1074 | |
af8b7d2b JQ |
1075 | if (multifd_send_initial_packet(p, &local_err) < 0) { |
1076 | goto out; | |
1077 | } | |
408ea6ae JQ |
1078 | /* initial packet */ |
1079 | p->num_packets = 1; | |
f986c3d2 JQ |
1080 | |
1081 | while (true) { | |
d82628e4 | 1082 | qemu_sem_wait(&p->sem); |
f986c3d2 | 1083 | qemu_mutex_lock(&p->mutex); |
0beb5ed3 JQ |
1084 | |
1085 | if (p->pending_job) { | |
1086 | uint32_t used = p->pages->used; | |
1087 | uint64_t packet_num = p->packet_num; | |
1088 | uint32_t flags = p->flags; | |
1089 | ||
2a34ee59 | 1090 | p->next_packet_size = used * qemu_target_page_size(); |
0beb5ed3 JQ |
1091 | multifd_send_fill_packet(p); |
1092 | p->flags = 0; | |
1093 | p->num_packets++; | |
1094 | p->num_pages += used; | |
1095 | p->pages->used = 0; | |
1096 | qemu_mutex_unlock(&p->mutex); | |
1097 | ||
2a34ee59 JQ |
1098 | trace_multifd_send(p->id, packet_num, used, flags, |
1099 | p->next_packet_size); | |
0beb5ed3 | 1100 | |
8b2db7f5 JQ |
1101 | ret = qio_channel_write_all(p->c, (void *)p->packet, |
1102 | p->packet_len, &local_err); | |
1103 | if (ret != 0) { | |
1104 | break; | |
1105 | } | |
1106 | ||
ad24c7cb JQ |
1107 | if (used) { |
1108 | ret = qio_channel_writev_all(p->c, p->pages->iov, | |
1109 | used, &local_err); | |
1110 | if (ret != 0) { | |
1111 | break; | |
1112 | } | |
8b2db7f5 | 1113 | } |
0beb5ed3 JQ |
1114 | |
1115 | qemu_mutex_lock(&p->mutex); | |
1116 | p->pending_job--; | |
1117 | qemu_mutex_unlock(&p->mutex); | |
6df264ac JQ |
1118 | |
1119 | if (flags & MULTIFD_FLAG_SYNC) { | |
1120 | qemu_sem_post(&multifd_send_state->sem_sync); | |
1121 | } | |
b9ee2f7d | 1122 | qemu_sem_post(&multifd_send_state->channels_ready); |
0beb5ed3 | 1123 | } else if (p->quit) { |
f986c3d2 JQ |
1124 | qemu_mutex_unlock(&p->mutex); |
1125 | break; | |
6df264ac JQ |
1126 | } else { |
1127 | qemu_mutex_unlock(&p->mutex); | |
1128 | /* sometimes there are spurious wakeups */ | |
f986c3d2 | 1129 | } |
f986c3d2 JQ |
1130 | } |
1131 | ||
af8b7d2b JQ |
1132 | out: |
1133 | if (local_err) { | |
1134 | multifd_send_terminate_threads(local_err); | |
1135 | } | |
1136 | ||
66770707 JQ |
1137 | qemu_mutex_lock(&p->mutex); |
1138 | p->running = false; | |
1139 | qemu_mutex_unlock(&p->mutex); | |
1140 | ||
74637e6f | 1141 | rcu_unregister_thread(); |
408ea6ae JQ |
1142 | trace_multifd_send_thread_end(p->id, p->num_packets, p->num_pages); |
1143 | ||
f986c3d2 JQ |
1144 | return NULL; |
1145 | } | |
1146 | ||
60df2d4a JQ |
1147 | static void multifd_new_send_channel_async(QIOTask *task, gpointer opaque) |
1148 | { | |
1149 | MultiFDSendParams *p = opaque; | |
1150 | QIOChannel *sioc = QIO_CHANNEL(qio_task_get_source(task)); | |
1151 | Error *local_err = NULL; | |
1152 | ||
1153 | if (qio_task_propagate_error(task, &local_err)) { | |
1398b2e3 FL |
1154 | migrate_set_error(migrate_get_current(), local_err); |
1155 | multifd_save_cleanup(); | |
60df2d4a JQ |
1156 | } else { |
1157 | p->c = QIO_CHANNEL(sioc); | |
1158 | qio_channel_set_delay(p->c, false); | |
1159 | p->running = true; | |
1160 | qemu_thread_create(&p->thread, p->name, multifd_send_thread, p, | |
1161 | QEMU_THREAD_JOINABLE); | |
1162 | ||
1163 | atomic_inc(&multifd_send_state->count); | |
1164 | } | |
1165 | } | |
1166 | ||
f986c3d2 JQ |
1167 | int multifd_save_setup(void) |
1168 | { | |
1169 | int thread_count; | |
efd1a1d6 | 1170 | uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size(); |
f986c3d2 JQ |
1171 | uint8_t i; |
1172 | ||
1173 | if (!migrate_use_multifd()) { | |
1174 | return 0; | |
1175 | } | |
1176 | thread_count = migrate_multifd_channels(); | |
1177 | multifd_send_state = g_malloc0(sizeof(*multifd_send_state)); | |
1178 | multifd_send_state->params = g_new0(MultiFDSendParams, thread_count); | |
66770707 | 1179 | atomic_set(&multifd_send_state->count, 0); |
34c55a94 | 1180 | multifd_send_state->pages = multifd_pages_init(page_count); |
6df264ac | 1181 | qemu_sem_init(&multifd_send_state->sem_sync, 0); |
b9ee2f7d | 1182 | qemu_sem_init(&multifd_send_state->channels_ready, 0); |
34c55a94 | 1183 | |
f986c3d2 JQ |
1184 | for (i = 0; i < thread_count; i++) { |
1185 | MultiFDSendParams *p = &multifd_send_state->params[i]; | |
1186 | ||
1187 | qemu_mutex_init(&p->mutex); | |
1188 | qemu_sem_init(&p->sem, 0); | |
6df264ac | 1189 | qemu_sem_init(&p->sem_sync, 0); |
f986c3d2 | 1190 | p->quit = false; |
0beb5ed3 | 1191 | p->pending_job = 0; |
f986c3d2 | 1192 | p->id = i; |
34c55a94 | 1193 | p->pages = multifd_pages_init(page_count); |
2a26c979 JQ |
1194 | p->packet_len = sizeof(MultiFDPacket_t) |
1195 | + sizeof(ram_addr_t) * page_count; | |
1196 | p->packet = g_malloc0(p->packet_len); | |
f986c3d2 | 1197 | p->name = g_strdup_printf("multifdsend_%d", i); |
60df2d4a | 1198 | socket_send_channel_create(multifd_new_send_channel_async, p); |
f986c3d2 JQ |
1199 | } |
1200 | return 0; | |
1201 | } | |
1202 | ||
f986c3d2 JQ |
1203 | struct { |
1204 | MultiFDRecvParams *params; | |
1205 | /* number of created threads */ | |
1206 | int count; | |
6df264ac JQ |
1207 | /* syncs main thread and channels */ |
1208 | QemuSemaphore sem_sync; | |
1209 | /* global number of generated multifd packets */ | |
1210 | uint64_t packet_num; | |
f986c3d2 JQ |
1211 | } *multifd_recv_state; |
1212 | ||
66770707 | 1213 | static void multifd_recv_terminate_threads(Error *err) |
f986c3d2 JQ |
1214 | { |
1215 | int i; | |
1216 | ||
7a169d74 JQ |
1217 | if (err) { |
1218 | MigrationState *s = migrate_get_current(); | |
1219 | migrate_set_error(s, err); | |
1220 | if (s->state == MIGRATION_STATUS_SETUP || | |
1221 | s->state == MIGRATION_STATUS_ACTIVE) { | |
1222 | migrate_set_state(&s->state, s->state, | |
1223 | MIGRATION_STATUS_FAILED); | |
1224 | } | |
1225 | } | |
1226 | ||
66770707 | 1227 | for (i = 0; i < migrate_multifd_channels(); i++) { |
f986c3d2 JQ |
1228 | MultiFDRecvParams *p = &multifd_recv_state->params[i]; |
1229 | ||
1230 | qemu_mutex_lock(&p->mutex); | |
7a5cc33c JQ |
1231 | /* We could arrive here for two reasons: |
1232 | - normal quit, i.e. everything went fine, just finished | |
1233 | - error quit: We close the channels so the channel threads | |
1234 | finish the qio_channel_read_all_eof() */ | |
1235 | qio_channel_shutdown(p->c, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); | |
f986c3d2 JQ |
1236 | qemu_mutex_unlock(&p->mutex); |
1237 | } | |
1238 | } | |
1239 | ||
1240 | int multifd_load_cleanup(Error **errp) | |
1241 | { | |
1242 | int i; | |
1243 | int ret = 0; | |
1244 | ||
1245 | if (!migrate_use_multifd()) { | |
1246 | return 0; | |
1247 | } | |
66770707 JQ |
1248 | multifd_recv_terminate_threads(NULL); |
1249 | for (i = 0; i < migrate_multifd_channels(); i++) { | |
f986c3d2 JQ |
1250 | MultiFDRecvParams *p = &multifd_recv_state->params[i]; |
1251 | ||
66770707 JQ |
1252 | if (p->running) { |
1253 | qemu_thread_join(&p->thread); | |
1254 | } | |
60df2d4a JQ |
1255 | object_unref(OBJECT(p->c)); |
1256 | p->c = NULL; | |
f986c3d2 | 1257 | qemu_mutex_destroy(&p->mutex); |
6df264ac | 1258 | qemu_sem_destroy(&p->sem_sync); |
f986c3d2 JQ |
1259 | g_free(p->name); |
1260 | p->name = NULL; | |
34c55a94 JQ |
1261 | multifd_pages_clear(p->pages); |
1262 | p->pages = NULL; | |
2a26c979 JQ |
1263 | p->packet_len = 0; |
1264 | g_free(p->packet); | |
1265 | p->packet = NULL; | |
f986c3d2 | 1266 | } |
6df264ac | 1267 | qemu_sem_destroy(&multifd_recv_state->sem_sync); |
f986c3d2 JQ |
1268 | g_free(multifd_recv_state->params); |
1269 | multifd_recv_state->params = NULL; | |
1270 | g_free(multifd_recv_state); | |
1271 | multifd_recv_state = NULL; | |
1272 | ||
1273 | return ret; | |
1274 | } | |
1275 | ||
6df264ac JQ |
1276 | static void multifd_recv_sync_main(void) |
1277 | { | |
1278 | int i; | |
1279 | ||
1280 | if (!migrate_use_multifd()) { | |
1281 | return; | |
1282 | } | |
1283 | for (i = 0; i < migrate_multifd_channels(); i++) { | |
1284 | MultiFDRecvParams *p = &multifd_recv_state->params[i]; | |
1285 | ||
6df264ac JQ |
1286 | trace_multifd_recv_sync_main_wait(p->id); |
1287 | qemu_sem_wait(&multifd_recv_state->sem_sync); | |
1288 | qemu_mutex_lock(&p->mutex); | |
1289 | if (multifd_recv_state->packet_num < p->packet_num) { | |
1290 | multifd_recv_state->packet_num = p->packet_num; | |
1291 | } | |
1292 | qemu_mutex_unlock(&p->mutex); | |
1293 | } | |
1294 | for (i = 0; i < migrate_multifd_channels(); i++) { | |
1295 | MultiFDRecvParams *p = &multifd_recv_state->params[i]; | |
1296 | ||
1297 | trace_multifd_recv_sync_main_signal(p->id); | |
6df264ac JQ |
1298 | qemu_sem_post(&p->sem_sync); |
1299 | } | |
1300 | trace_multifd_recv_sync_main(multifd_recv_state->packet_num); | |
1301 | } | |
1302 | ||
f986c3d2 JQ |
1303 | static void *multifd_recv_thread(void *opaque) |
1304 | { | |
1305 | MultiFDRecvParams *p = opaque; | |
2a26c979 JQ |
1306 | Error *local_err = NULL; |
1307 | int ret; | |
f986c3d2 | 1308 | |
408ea6ae | 1309 | trace_multifd_recv_thread_start(p->id); |
74637e6f | 1310 | rcu_register_thread(); |
408ea6ae | 1311 | |
f986c3d2 | 1312 | while (true) { |
6df264ac JQ |
1313 | uint32_t used; |
1314 | uint32_t flags; | |
0beb5ed3 | 1315 | |
8b2db7f5 JQ |
1316 | ret = qio_channel_read_all_eof(p->c, (void *)p->packet, |
1317 | p->packet_len, &local_err); | |
1318 | if (ret == 0) { /* EOF */ | |
1319 | break; | |
1320 | } | |
1321 | if (ret == -1) { /* Error */ | |
1322 | break; | |
1323 | } | |
2a26c979 | 1324 | |
6df264ac JQ |
1325 | qemu_mutex_lock(&p->mutex); |
1326 | ret = multifd_recv_unfill_packet(p, &local_err); | |
1327 | if (ret) { | |
f986c3d2 JQ |
1328 | qemu_mutex_unlock(&p->mutex); |
1329 | break; | |
1330 | } | |
6df264ac JQ |
1331 | |
1332 | used = p->pages->used; | |
1333 | flags = p->flags; | |
2a34ee59 JQ |
1334 | trace_multifd_recv(p->id, p->packet_num, used, flags, |
1335 | p->next_packet_size); | |
6df264ac JQ |
1336 | p->num_packets++; |
1337 | p->num_pages += used; | |
f986c3d2 | 1338 | qemu_mutex_unlock(&p->mutex); |
6df264ac | 1339 | |
ad24c7cb JQ |
1340 | if (used) { |
1341 | ret = qio_channel_readv_all(p->c, p->pages->iov, | |
1342 | used, &local_err); | |
1343 | if (ret != 0) { | |
1344 | break; | |
1345 | } | |
8b2db7f5 JQ |
1346 | } |
1347 | ||
6df264ac JQ |
1348 | if (flags & MULTIFD_FLAG_SYNC) { |
1349 | qemu_sem_post(&multifd_recv_state->sem_sync); | |
1350 | qemu_sem_wait(&p->sem_sync); | |
1351 | } | |
f986c3d2 JQ |
1352 | } |
1353 | ||
d82628e4 JQ |
1354 | if (local_err) { |
1355 | multifd_recv_terminate_threads(local_err); | |
1356 | } | |
66770707 JQ |
1357 | qemu_mutex_lock(&p->mutex); |
1358 | p->running = false; | |
1359 | qemu_mutex_unlock(&p->mutex); | |
1360 | ||
74637e6f | 1361 | rcu_unregister_thread(); |
408ea6ae JQ |
1362 | trace_multifd_recv_thread_end(p->id, p->num_packets, p->num_pages); |
1363 | ||
f986c3d2 JQ |
1364 | return NULL; |
1365 | } | |
1366 | ||
1367 | int multifd_load_setup(void) | |
1368 | { | |
1369 | int thread_count; | |
efd1a1d6 | 1370 | uint32_t page_count = MULTIFD_PACKET_SIZE / qemu_target_page_size(); |
f986c3d2 JQ |
1371 | uint8_t i; |
1372 | ||
1373 | if (!migrate_use_multifd()) { | |
1374 | return 0; | |
1375 | } | |
1376 | thread_count = migrate_multifd_channels(); | |
1377 | multifd_recv_state = g_malloc0(sizeof(*multifd_recv_state)); | |
1378 | multifd_recv_state->params = g_new0(MultiFDRecvParams, thread_count); | |
66770707 | 1379 | atomic_set(&multifd_recv_state->count, 0); |
6df264ac | 1380 | qemu_sem_init(&multifd_recv_state->sem_sync, 0); |
34c55a94 | 1381 | |
f986c3d2 JQ |
1382 | for (i = 0; i < thread_count; i++) { |
1383 | MultiFDRecvParams *p = &multifd_recv_state->params[i]; | |
1384 | ||
1385 | qemu_mutex_init(&p->mutex); | |
6df264ac | 1386 | qemu_sem_init(&p->sem_sync, 0); |
f986c3d2 | 1387 | p->id = i; |
34c55a94 | 1388 | p->pages = multifd_pages_init(page_count); |
2a26c979 JQ |
1389 | p->packet_len = sizeof(MultiFDPacket_t) |
1390 | + sizeof(ram_addr_t) * page_count; | |
1391 | p->packet = g_malloc0(p->packet_len); | |
f986c3d2 | 1392 | p->name = g_strdup_printf("multifdrecv_%d", i); |
f986c3d2 JQ |
1393 | } |
1394 | return 0; | |
1395 | } | |
1396 | ||
62c1e0ca JQ |
1397 | bool multifd_recv_all_channels_created(void) |
1398 | { | |
1399 | int thread_count = migrate_multifd_channels(); | |
1400 | ||
1401 | if (!migrate_use_multifd()) { | |
1402 | return true; | |
1403 | } | |
1404 | ||
1405 | return thread_count == atomic_read(&multifd_recv_state->count); | |
1406 | } | |
1407 | ||
49ed0d24 FL |
1408 | /* |
1409 | * Try to receive all multifd channels to get ready for the migration. | |
1410 | * - Return true and do not set @errp when correctly receving all channels; | |
1411 | * - Return false and do not set @errp when correctly receiving the current one; | |
1412 | * - Return false and set @errp when failing to receive the current channel. | |
1413 | */ | |
1414 | bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp) | |
71bb07db | 1415 | { |
60df2d4a | 1416 | MultiFDRecvParams *p; |
af8b7d2b JQ |
1417 | Error *local_err = NULL; |
1418 | int id; | |
60df2d4a | 1419 | |
af8b7d2b JQ |
1420 | id = multifd_recv_initial_packet(ioc, &local_err); |
1421 | if (id < 0) { | |
1422 | multifd_recv_terminate_threads(local_err); | |
49ed0d24 FL |
1423 | error_propagate_prepend(errp, local_err, |
1424 | "failed to receive packet" | |
1425 | " via multifd channel %d: ", | |
1426 | atomic_read(&multifd_recv_state->count)); | |
81e62053 | 1427 | return false; |
af8b7d2b JQ |
1428 | } |
1429 | ||
1430 | p = &multifd_recv_state->params[id]; | |
1431 | if (p->c != NULL) { | |
1432 | error_setg(&local_err, "multifd: received id '%d' already setup'", | |
1433 | id); | |
1434 | multifd_recv_terminate_threads(local_err); | |
49ed0d24 | 1435 | error_propagate(errp, local_err); |
81e62053 | 1436 | return false; |
af8b7d2b | 1437 | } |
60df2d4a JQ |
1438 | p->c = ioc; |
1439 | object_ref(OBJECT(ioc)); | |
408ea6ae JQ |
1440 | /* initial packet */ |
1441 | p->num_packets = 1; | |
60df2d4a JQ |
1442 | |
1443 | p->running = true; | |
1444 | qemu_thread_create(&p->thread, p->name, multifd_recv_thread, p, | |
1445 | QEMU_THREAD_JOINABLE); | |
1446 | atomic_inc(&multifd_recv_state->count); | |
49ed0d24 FL |
1447 | return atomic_read(&multifd_recv_state->count) == |
1448 | migrate_multifd_channels(); | |
71bb07db JQ |
1449 | } |
1450 | ||
56e93d26 | 1451 | /** |
3d0684b2 | 1452 | * save_page_header: write page header to wire |
56e93d26 JQ |
1453 | * |
1454 | * If this is the 1st block, it also writes the block identification | |
1455 | * | |
3d0684b2 | 1456 | * Returns the number of bytes written |
56e93d26 JQ |
1457 | * |
1458 | * @f: QEMUFile where to send the data | |
1459 | * @block: block that contains the page we want to send | |
1460 | * @offset: offset inside the block for the page | |
1461 | * in the lower bits, it contains flags | |
1462 | */ | |
2bf3aa85 JQ |
1463 | static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block, |
1464 | ram_addr_t offset) | |
56e93d26 | 1465 | { |
9f5f380b | 1466 | size_t size, len; |
56e93d26 | 1467 | |
24795694 JQ |
1468 | if (block == rs->last_sent_block) { |
1469 | offset |= RAM_SAVE_FLAG_CONTINUE; | |
1470 | } | |
2bf3aa85 | 1471 | qemu_put_be64(f, offset); |
56e93d26 JQ |
1472 | size = 8; |
1473 | ||
1474 | if (!(offset & RAM_SAVE_FLAG_CONTINUE)) { | |
9f5f380b | 1475 | len = strlen(block->idstr); |
2bf3aa85 JQ |
1476 | qemu_put_byte(f, len); |
1477 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); | |
9f5f380b | 1478 | size += 1 + len; |
24795694 | 1479 | rs->last_sent_block = block; |
56e93d26 JQ |
1480 | } |
1481 | return size; | |
1482 | } | |
1483 | ||
3d0684b2 JQ |
1484 | /** |
1485 | * mig_throttle_guest_down: throotle down the guest | |
1486 | * | |
1487 | * Reduce amount of guest cpu execution to hopefully slow down memory | |
1488 | * writes. If guest dirty memory rate is reduced below the rate at | |
1489 | * which we can transfer pages to the destination then we should be | |
1490 | * able to complete migration. Some workloads dirty memory way too | |
1491 | * fast and will not effectively converge, even with auto-converge. | |
070afca2 JH |
1492 | */ |
1493 | static void mig_throttle_guest_down(void) | |
1494 | { | |
1495 | MigrationState *s = migrate_get_current(); | |
2594f56d DB |
1496 | uint64_t pct_initial = s->parameters.cpu_throttle_initial; |
1497 | uint64_t pct_icrement = s->parameters.cpu_throttle_increment; | |
4cbc9c7f | 1498 | int pct_max = s->parameters.max_cpu_throttle; |
070afca2 JH |
1499 | |
1500 | /* We have not started throttling yet. Let's start it. */ | |
1501 | if (!cpu_throttle_active()) { | |
1502 | cpu_throttle_set(pct_initial); | |
1503 | } else { | |
1504 | /* Throttling already on, just increase the rate */ | |
4cbc9c7f LQ |
1505 | cpu_throttle_set(MIN(cpu_throttle_get_percentage() + pct_icrement, |
1506 | pct_max)); | |
070afca2 JH |
1507 | } |
1508 | } | |
1509 | ||
3d0684b2 JQ |
1510 | /** |
1511 | * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache | |
1512 | * | |
6f37bb8b | 1513 | * @rs: current RAM state |
3d0684b2 JQ |
1514 | * @current_addr: address for the zero page |
1515 | * | |
1516 | * Update the xbzrle cache to reflect a page that's been sent as all 0. | |
56e93d26 JQ |
1517 | * The important thing is that a stale (not-yet-0'd) page be replaced |
1518 | * by the new data. | |
1519 | * As a bonus, if the page wasn't in the cache it gets added so that | |
3d0684b2 | 1520 | * when a small write is made into the 0'd page it gets XBZRLE sent. |
56e93d26 | 1521 | */ |
6f37bb8b | 1522 | static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr) |
56e93d26 | 1523 | { |
6f37bb8b | 1524 | if (rs->ram_bulk_stage || !migrate_use_xbzrle()) { |
56e93d26 JQ |
1525 | return; |
1526 | } | |
1527 | ||
1528 | /* We don't care if this fails to allocate a new cache page | |
1529 | * as long as it updated an old one */ | |
c00e0928 | 1530 | cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page, |
9360447d | 1531 | ram_counters.dirty_sync_count); |
56e93d26 JQ |
1532 | } |
1533 | ||
1534 | #define ENCODING_FLAG_XBZRLE 0x1 | |
1535 | ||
1536 | /** | |
1537 | * save_xbzrle_page: compress and send current page | |
1538 | * | |
1539 | * Returns: 1 means that we wrote the page | |
1540 | * 0 means that page is identical to the one already sent | |
1541 | * -1 means that xbzrle would be longer than normal | |
1542 | * | |
5a987738 | 1543 | * @rs: current RAM state |
3d0684b2 JQ |
1544 | * @current_data: pointer to the address of the page contents |
1545 | * @current_addr: addr of the page | |
56e93d26 JQ |
1546 | * @block: block that contains the page we want to send |
1547 | * @offset: offset inside the block for the page | |
1548 | * @last_stage: if we are at the completion stage | |
56e93d26 | 1549 | */ |
204b88b8 | 1550 | static int save_xbzrle_page(RAMState *rs, uint8_t **current_data, |
56e93d26 | 1551 | ram_addr_t current_addr, RAMBlock *block, |
072c2511 | 1552 | ram_addr_t offset, bool last_stage) |
56e93d26 JQ |
1553 | { |
1554 | int encoded_len = 0, bytes_xbzrle; | |
1555 | uint8_t *prev_cached_page; | |
1556 | ||
9360447d JQ |
1557 | if (!cache_is_cached(XBZRLE.cache, current_addr, |
1558 | ram_counters.dirty_sync_count)) { | |
1559 | xbzrle_counters.cache_miss++; | |
56e93d26 JQ |
1560 | if (!last_stage) { |
1561 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, | |
9360447d | 1562 | ram_counters.dirty_sync_count) == -1) { |
56e93d26 JQ |
1563 | return -1; |
1564 | } else { | |
1565 | /* update *current_data when the page has been | |
1566 | inserted into cache */ | |
1567 | *current_data = get_cached_data(XBZRLE.cache, current_addr); | |
1568 | } | |
1569 | } | |
1570 | return -1; | |
1571 | } | |
1572 | ||
1573 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); | |
1574 | ||
1575 | /* save current buffer into memory */ | |
1576 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); | |
1577 | ||
1578 | /* XBZRLE encoding (if there is no overflow) */ | |
1579 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, | |
1580 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, | |
1581 | TARGET_PAGE_SIZE); | |
1582 | if (encoded_len == 0) { | |
55c4446b | 1583 | trace_save_xbzrle_page_skipping(); |
56e93d26 JQ |
1584 | return 0; |
1585 | } else if (encoded_len == -1) { | |
55c4446b | 1586 | trace_save_xbzrle_page_overflow(); |
9360447d | 1587 | xbzrle_counters.overflow++; |
56e93d26 JQ |
1588 | /* update data in the cache */ |
1589 | if (!last_stage) { | |
1590 | memcpy(prev_cached_page, *current_data, TARGET_PAGE_SIZE); | |
1591 | *current_data = prev_cached_page; | |
1592 | } | |
1593 | return -1; | |
1594 | } | |
1595 | ||
1596 | /* we need to update the data in the cache, in order to get the same data */ | |
1597 | if (!last_stage) { | |
1598 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); | |
1599 | } | |
1600 | ||
1601 | /* Send XBZRLE based compressed page */ | |
2bf3aa85 | 1602 | bytes_xbzrle = save_page_header(rs, rs->f, block, |
204b88b8 JQ |
1603 | offset | RAM_SAVE_FLAG_XBZRLE); |
1604 | qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE); | |
1605 | qemu_put_be16(rs->f, encoded_len); | |
1606 | qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len); | |
56e93d26 | 1607 | bytes_xbzrle += encoded_len + 1 + 2; |
9360447d JQ |
1608 | xbzrle_counters.pages++; |
1609 | xbzrle_counters.bytes += bytes_xbzrle; | |
1610 | ram_counters.transferred += bytes_xbzrle; | |
56e93d26 JQ |
1611 | |
1612 | return 1; | |
1613 | } | |
1614 | ||
3d0684b2 JQ |
1615 | /** |
1616 | * migration_bitmap_find_dirty: find the next dirty page from start | |
f3f491fc | 1617 | * |
3d0684b2 JQ |
1618 | * Called with rcu_read_lock() to protect migration_bitmap |
1619 | * | |
1620 | * Returns the byte offset within memory region of the start of a dirty page | |
1621 | * | |
6f37bb8b | 1622 | * @rs: current RAM state |
3d0684b2 | 1623 | * @rb: RAMBlock where to search for dirty pages |
a935e30f | 1624 | * @start: page where we start the search |
f3f491fc | 1625 | */ |
56e93d26 | 1626 | static inline |
a935e30f | 1627 | unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb, |
f20e2865 | 1628 | unsigned long start) |
56e93d26 | 1629 | { |
6b6712ef JQ |
1630 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
1631 | unsigned long *bitmap = rb->bmap; | |
56e93d26 JQ |
1632 | unsigned long next; |
1633 | ||
fbd162e6 | 1634 | if (ramblock_is_ignored(rb)) { |
b895de50 CLG |
1635 | return size; |
1636 | } | |
1637 | ||
6eeb63f7 WW |
1638 | /* |
1639 | * When the free page optimization is enabled, we need to check the bitmap | |
1640 | * to send the non-free pages rather than all the pages in the bulk stage. | |
1641 | */ | |
1642 | if (!rs->fpo_enabled && rs->ram_bulk_stage && start > 0) { | |
6b6712ef | 1643 | next = start + 1; |
56e93d26 | 1644 | } else { |
6b6712ef | 1645 | next = find_next_bit(bitmap, size, start); |
56e93d26 JQ |
1646 | } |
1647 | ||
6b6712ef | 1648 | return next; |
56e93d26 JQ |
1649 | } |
1650 | ||
06b10688 | 1651 | static inline bool migration_bitmap_clear_dirty(RAMState *rs, |
f20e2865 JQ |
1652 | RAMBlock *rb, |
1653 | unsigned long page) | |
a82d593b DDAG |
1654 | { |
1655 | bool ret; | |
a82d593b | 1656 | |
386a907b | 1657 | qemu_mutex_lock(&rs->bitmap_mutex); |
6b6712ef | 1658 | ret = test_and_clear_bit(page, rb->bmap); |
a82d593b DDAG |
1659 | |
1660 | if (ret) { | |
0d8ec885 | 1661 | rs->migration_dirty_pages--; |
a82d593b | 1662 | } |
386a907b WW |
1663 | qemu_mutex_unlock(&rs->bitmap_mutex); |
1664 | ||
a82d593b DDAG |
1665 | return ret; |
1666 | } | |
1667 | ||
15440dd5 JQ |
1668 | static void migration_bitmap_sync_range(RAMState *rs, RAMBlock *rb, |
1669 | ram_addr_t start, ram_addr_t length) | |
56e93d26 | 1670 | { |
0d8ec885 | 1671 | rs->migration_dirty_pages += |
6b6712ef | 1672 | cpu_physical_memory_sync_dirty_bitmap(rb, start, length, |
0d8ec885 | 1673 | &rs->num_dirty_pages_period); |
56e93d26 JQ |
1674 | } |
1675 | ||
3d0684b2 JQ |
1676 | /** |
1677 | * ram_pagesize_summary: calculate all the pagesizes of a VM | |
1678 | * | |
1679 | * Returns a summary bitmap of the page sizes of all RAMBlocks | |
1680 | * | |
1681 | * For VMs with just normal pages this is equivalent to the host page | |
1682 | * size. If it's got some huge pages then it's the OR of all the | |
1683 | * different page sizes. | |
e8ca1db2 DDAG |
1684 | */ |
1685 | uint64_t ram_pagesize_summary(void) | |
1686 | { | |
1687 | RAMBlock *block; | |
1688 | uint64_t summary = 0; | |
1689 | ||
fbd162e6 | 1690 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
e8ca1db2 DDAG |
1691 | summary |= block->page_size; |
1692 | } | |
1693 | ||
1694 | return summary; | |
1695 | } | |
1696 | ||
aecbfe9c XG |
1697 | uint64_t ram_get_total_transferred_pages(void) |
1698 | { | |
1699 | return ram_counters.normal + ram_counters.duplicate + | |
1700 | compression_counters.pages + xbzrle_counters.pages; | |
1701 | } | |
1702 | ||
b734035b XG |
1703 | static void migration_update_rates(RAMState *rs, int64_t end_time) |
1704 | { | |
be8b02ed | 1705 | uint64_t page_count = rs->target_page_count - rs->target_page_count_prev; |
76e03000 | 1706 | double compressed_size; |
b734035b XG |
1707 | |
1708 | /* calculate period counters */ | |
1709 | ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000 | |
1710 | / (end_time - rs->time_last_bitmap_sync); | |
1711 | ||
be8b02ed | 1712 | if (!page_count) { |
b734035b XG |
1713 | return; |
1714 | } | |
1715 | ||
1716 | if (migrate_use_xbzrle()) { | |
1717 | xbzrle_counters.cache_miss_rate = (double)(xbzrle_counters.cache_miss - | |
be8b02ed | 1718 | rs->xbzrle_cache_miss_prev) / page_count; |
b734035b XG |
1719 | rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss; |
1720 | } | |
76e03000 XG |
1721 | |
1722 | if (migrate_use_compression()) { | |
1723 | compression_counters.busy_rate = (double)(compression_counters.busy - | |
1724 | rs->compress_thread_busy_prev) / page_count; | |
1725 | rs->compress_thread_busy_prev = compression_counters.busy; | |
1726 | ||
1727 | compressed_size = compression_counters.compressed_size - | |
1728 | rs->compressed_size_prev; | |
1729 | if (compressed_size) { | |
1730 | double uncompressed_size = (compression_counters.pages - | |
1731 | rs->compress_pages_prev) * TARGET_PAGE_SIZE; | |
1732 | ||
1733 | /* Compression-Ratio = Uncompressed-size / Compressed-size */ | |
1734 | compression_counters.compression_rate = | |
1735 | uncompressed_size / compressed_size; | |
1736 | ||
1737 | rs->compress_pages_prev = compression_counters.pages; | |
1738 | rs->compressed_size_prev = compression_counters.compressed_size; | |
1739 | } | |
1740 | } | |
b734035b XG |
1741 | } |
1742 | ||
8d820d6f | 1743 | static void migration_bitmap_sync(RAMState *rs) |
56e93d26 JQ |
1744 | { |
1745 | RAMBlock *block; | |
56e93d26 | 1746 | int64_t end_time; |
c4bdf0cf | 1747 | uint64_t bytes_xfer_now; |
56e93d26 | 1748 | |
9360447d | 1749 | ram_counters.dirty_sync_count++; |
56e93d26 | 1750 | |
f664da80 JQ |
1751 | if (!rs->time_last_bitmap_sync) { |
1752 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
56e93d26 JQ |
1753 | } |
1754 | ||
1755 | trace_migration_bitmap_sync_start(); | |
9c1f8f44 | 1756 | memory_global_dirty_log_sync(); |
56e93d26 | 1757 | |
108cfae0 | 1758 | qemu_mutex_lock(&rs->bitmap_mutex); |
56e93d26 | 1759 | rcu_read_lock(); |
fbd162e6 | 1760 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
15440dd5 | 1761 | migration_bitmap_sync_range(rs, block, 0, block->used_length); |
56e93d26 | 1762 | } |
650af890 | 1763 | ram_counters.remaining = ram_bytes_remaining(); |
56e93d26 | 1764 | rcu_read_unlock(); |
108cfae0 | 1765 | qemu_mutex_unlock(&rs->bitmap_mutex); |
56e93d26 | 1766 | |
a66cd90c | 1767 | trace_migration_bitmap_sync_end(rs->num_dirty_pages_period); |
1ffb5dfd | 1768 | |
56e93d26 JQ |
1769 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
1770 | ||
1771 | /* more than 1 second = 1000 millisecons */ | |
f664da80 | 1772 | if (end_time > rs->time_last_bitmap_sync + 1000) { |
9360447d | 1773 | bytes_xfer_now = ram_counters.transferred; |
d693c6f1 | 1774 | |
9ac78b61 PL |
1775 | /* During block migration the auto-converge logic incorrectly detects |
1776 | * that ram migration makes no progress. Avoid this by disabling the | |
1777 | * throttling logic during the bulk phase of block migration. */ | |
1778 | if (migrate_auto_converge() && !blk_mig_bulk_active()) { | |
56e93d26 JQ |
1779 | /* The following detection logic can be refined later. For now: |
1780 | Check to see if the dirtied bytes is 50% more than the approx. | |
1781 | amount of bytes that just got transferred since the last time we | |
070afca2 JH |
1782 | were in this routine. If that happens twice, start or increase |
1783 | throttling */ | |
070afca2 | 1784 | |
d693c6f1 | 1785 | if ((rs->num_dirty_pages_period * TARGET_PAGE_SIZE > |
eac74159 | 1786 | (bytes_xfer_now - rs->bytes_xfer_prev) / 2) && |
b4a3c64b | 1787 | (++rs->dirty_rate_high_cnt >= 2)) { |
56e93d26 | 1788 | trace_migration_throttle(); |
8d820d6f | 1789 | rs->dirty_rate_high_cnt = 0; |
070afca2 | 1790 | mig_throttle_guest_down(); |
d693c6f1 | 1791 | } |
56e93d26 | 1792 | } |
070afca2 | 1793 | |
b734035b XG |
1794 | migration_update_rates(rs, end_time); |
1795 | ||
be8b02ed | 1796 | rs->target_page_count_prev = rs->target_page_count; |
d693c6f1 FF |
1797 | |
1798 | /* reset period counters */ | |
f664da80 | 1799 | rs->time_last_bitmap_sync = end_time; |
a66cd90c | 1800 | rs->num_dirty_pages_period = 0; |
d2a4d85a | 1801 | rs->bytes_xfer_prev = bytes_xfer_now; |
56e93d26 | 1802 | } |
4addcd4f | 1803 | if (migrate_use_events()) { |
3ab72385 | 1804 | qapi_event_send_migration_pass(ram_counters.dirty_sync_count); |
4addcd4f | 1805 | } |
56e93d26 JQ |
1806 | } |
1807 | ||
bd227060 WW |
1808 | static void migration_bitmap_sync_precopy(RAMState *rs) |
1809 | { | |
1810 | Error *local_err = NULL; | |
1811 | ||
1812 | /* | |
1813 | * The current notifier usage is just an optimization to migration, so we | |
1814 | * don't stop the normal migration process in the error case. | |
1815 | */ | |
1816 | if (precopy_notify(PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC, &local_err)) { | |
1817 | error_report_err(local_err); | |
1818 | } | |
1819 | ||
1820 | migration_bitmap_sync(rs); | |
1821 | ||
1822 | if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC, &local_err)) { | |
1823 | error_report_err(local_err); | |
1824 | } | |
1825 | } | |
1826 | ||
6c97ec5f XG |
1827 | /** |
1828 | * save_zero_page_to_file: send the zero page to the file | |
1829 | * | |
1830 | * Returns the size of data written to the file, 0 means the page is not | |
1831 | * a zero page | |
1832 | * | |
1833 | * @rs: current RAM state | |
1834 | * @file: the file where the data is saved | |
1835 | * @block: block that contains the page we want to send | |
1836 | * @offset: offset inside the block for the page | |
1837 | */ | |
1838 | static int save_zero_page_to_file(RAMState *rs, QEMUFile *file, | |
1839 | RAMBlock *block, ram_addr_t offset) | |
1840 | { | |
1841 | uint8_t *p = block->host + offset; | |
1842 | int len = 0; | |
1843 | ||
1844 | if (is_zero_range(p, TARGET_PAGE_SIZE)) { | |
1845 | len += save_page_header(rs, file, block, offset | RAM_SAVE_FLAG_ZERO); | |
1846 | qemu_put_byte(file, 0); | |
1847 | len += 1; | |
1848 | } | |
1849 | return len; | |
1850 | } | |
1851 | ||
56e93d26 | 1852 | /** |
3d0684b2 | 1853 | * save_zero_page: send the zero page to the stream |
56e93d26 | 1854 | * |
3d0684b2 | 1855 | * Returns the number of pages written. |
56e93d26 | 1856 | * |
f7ccd61b | 1857 | * @rs: current RAM state |
56e93d26 JQ |
1858 | * @block: block that contains the page we want to send |
1859 | * @offset: offset inside the block for the page | |
56e93d26 | 1860 | */ |
7faccdc3 | 1861 | static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset) |
56e93d26 | 1862 | { |
6c97ec5f | 1863 | int len = save_zero_page_to_file(rs, rs->f, block, offset); |
56e93d26 | 1864 | |
6c97ec5f | 1865 | if (len) { |
9360447d | 1866 | ram_counters.duplicate++; |
6c97ec5f XG |
1867 | ram_counters.transferred += len; |
1868 | return 1; | |
56e93d26 | 1869 | } |
6c97ec5f | 1870 | return -1; |
56e93d26 JQ |
1871 | } |
1872 | ||
5727309d | 1873 | static void ram_release_pages(const char *rbname, uint64_t offset, int pages) |
53f09a10 | 1874 | { |
5727309d | 1875 | if (!migrate_release_ram() || !migration_in_postcopy()) { |
53f09a10 PB |
1876 | return; |
1877 | } | |
1878 | ||
aaa2064c | 1879 | ram_discard_range(rbname, offset, pages << TARGET_PAGE_BITS); |
53f09a10 PB |
1880 | } |
1881 | ||
059ff0fb XG |
1882 | /* |
1883 | * @pages: the number of pages written by the control path, | |
1884 | * < 0 - error | |
1885 | * > 0 - number of pages written | |
1886 | * | |
1887 | * Return true if the pages has been saved, otherwise false is returned. | |
1888 | */ | |
1889 | static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, | |
1890 | int *pages) | |
1891 | { | |
1892 | uint64_t bytes_xmit = 0; | |
1893 | int ret; | |
1894 | ||
1895 | *pages = -1; | |
1896 | ret = ram_control_save_page(rs->f, block->offset, offset, TARGET_PAGE_SIZE, | |
1897 | &bytes_xmit); | |
1898 | if (ret == RAM_SAVE_CONTROL_NOT_SUPP) { | |
1899 | return false; | |
1900 | } | |
1901 | ||
1902 | if (bytes_xmit) { | |
1903 | ram_counters.transferred += bytes_xmit; | |
1904 | *pages = 1; | |
1905 | } | |
1906 | ||
1907 | if (ret == RAM_SAVE_CONTROL_DELAYED) { | |
1908 | return true; | |
1909 | } | |
1910 | ||
1911 | if (bytes_xmit > 0) { | |
1912 | ram_counters.normal++; | |
1913 | } else if (bytes_xmit == 0) { | |
1914 | ram_counters.duplicate++; | |
1915 | } | |
1916 | ||
1917 | return true; | |
1918 | } | |
1919 | ||
65dacaa0 XG |
1920 | /* |
1921 | * directly send the page to the stream | |
1922 | * | |
1923 | * Returns the number of pages written. | |
1924 | * | |
1925 | * @rs: current RAM state | |
1926 | * @block: block that contains the page we want to send | |
1927 | * @offset: offset inside the block for the page | |
1928 | * @buf: the page to be sent | |
1929 | * @async: send to page asyncly | |
1930 | */ | |
1931 | static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, | |
1932 | uint8_t *buf, bool async) | |
1933 | { | |
1934 | ram_counters.transferred += save_page_header(rs, rs->f, block, | |
1935 | offset | RAM_SAVE_FLAG_PAGE); | |
1936 | if (async) { | |
1937 | qemu_put_buffer_async(rs->f, buf, TARGET_PAGE_SIZE, | |
1938 | migrate_release_ram() & | |
1939 | migration_in_postcopy()); | |
1940 | } else { | |
1941 | qemu_put_buffer(rs->f, buf, TARGET_PAGE_SIZE); | |
1942 | } | |
1943 | ram_counters.transferred += TARGET_PAGE_SIZE; | |
1944 | ram_counters.normal++; | |
1945 | return 1; | |
1946 | } | |
1947 | ||
56e93d26 | 1948 | /** |
3d0684b2 | 1949 | * ram_save_page: send the given page to the stream |
56e93d26 | 1950 | * |
3d0684b2 | 1951 | * Returns the number of pages written. |
3fd3c4b3 DDAG |
1952 | * < 0 - error |
1953 | * >=0 - Number of pages written - this might legally be 0 | |
1954 | * if xbzrle noticed the page was the same. | |
56e93d26 | 1955 | * |
6f37bb8b | 1956 | * @rs: current RAM state |
56e93d26 JQ |
1957 | * @block: block that contains the page we want to send |
1958 | * @offset: offset inside the block for the page | |
1959 | * @last_stage: if we are at the completion stage | |
56e93d26 | 1960 | */ |
a0a8aa14 | 1961 | static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage) |
56e93d26 JQ |
1962 | { |
1963 | int pages = -1; | |
56e93d26 | 1964 | uint8_t *p; |
56e93d26 | 1965 | bool send_async = true; |
a08f6890 | 1966 | RAMBlock *block = pss->block; |
a935e30f | 1967 | ram_addr_t offset = pss->page << TARGET_PAGE_BITS; |
059ff0fb | 1968 | ram_addr_t current_addr = block->offset + offset; |
56e93d26 | 1969 | |
2f68e399 | 1970 | p = block->host + offset; |
1db9d8e5 | 1971 | trace_ram_save_page(block->idstr, (uint64_t)offset, p); |
56e93d26 | 1972 | |
56e93d26 | 1973 | XBZRLE_cache_lock(); |
d7400a34 XG |
1974 | if (!rs->ram_bulk_stage && !migration_in_postcopy() && |
1975 | migrate_use_xbzrle()) { | |
059ff0fb XG |
1976 | pages = save_xbzrle_page(rs, &p, current_addr, block, |
1977 | offset, last_stage); | |
1978 | if (!last_stage) { | |
1979 | /* Can't send this cached data async, since the cache page | |
1980 | * might get updated before it gets to the wire | |
56e93d26 | 1981 | */ |
059ff0fb | 1982 | send_async = false; |
56e93d26 JQ |
1983 | } |
1984 | } | |
1985 | ||
1986 | /* XBZRLE overflow or normal page */ | |
1987 | if (pages == -1) { | |
65dacaa0 | 1988 | pages = save_normal_page(rs, block, offset, p, send_async); |
56e93d26 JQ |
1989 | } |
1990 | ||
1991 | XBZRLE_cache_unlock(); | |
1992 | ||
1993 | return pages; | |
1994 | } | |
1995 | ||
b9ee2f7d JQ |
1996 | static int ram_save_multifd_page(RAMState *rs, RAMBlock *block, |
1997 | ram_addr_t offset) | |
1998 | { | |
b9ee2f7d | 1999 | multifd_queue_page(block, offset); |
b9ee2f7d JQ |
2000 | ram_counters.normal++; |
2001 | ||
2002 | return 1; | |
2003 | } | |
2004 | ||
5e5fdcff | 2005 | static bool do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block, |
6ef3771c | 2006 | ram_addr_t offset, uint8_t *source_buf) |
56e93d26 | 2007 | { |
53518d94 | 2008 | RAMState *rs = ram_state; |
a7a9a88f | 2009 | uint8_t *p = block->host + (offset & TARGET_PAGE_MASK); |
5e5fdcff | 2010 | bool zero_page = false; |
6ef3771c | 2011 | int ret; |
56e93d26 | 2012 | |
5e5fdcff XG |
2013 | if (save_zero_page_to_file(rs, f, block, offset)) { |
2014 | zero_page = true; | |
2015 | goto exit; | |
2016 | } | |
2017 | ||
6ef3771c | 2018 | save_page_header(rs, f, block, offset | RAM_SAVE_FLAG_COMPRESS_PAGE); |
34ab9e97 XG |
2019 | |
2020 | /* | |
2021 | * copy it to a internal buffer to avoid it being modified by VM | |
2022 | * so that we can catch up the error during compression and | |
2023 | * decompression | |
2024 | */ | |
2025 | memcpy(source_buf, p, TARGET_PAGE_SIZE); | |
6ef3771c XG |
2026 | ret = qemu_put_compression_data(f, stream, source_buf, TARGET_PAGE_SIZE); |
2027 | if (ret < 0) { | |
2028 | qemu_file_set_error(migrate_get_current()->to_dst_file, ret); | |
b3be2896 | 2029 | error_report("compressed data failed!"); |
5e5fdcff | 2030 | return false; |
b3be2896 | 2031 | } |
56e93d26 | 2032 | |
5e5fdcff | 2033 | exit: |
6ef3771c | 2034 | ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1); |
5e5fdcff XG |
2035 | return zero_page; |
2036 | } | |
2037 | ||
2038 | static void | |
2039 | update_compress_thread_counts(const CompressParam *param, int bytes_xmit) | |
2040 | { | |
76e03000 XG |
2041 | ram_counters.transferred += bytes_xmit; |
2042 | ||
5e5fdcff XG |
2043 | if (param->zero_page) { |
2044 | ram_counters.duplicate++; | |
76e03000 | 2045 | return; |
5e5fdcff | 2046 | } |
76e03000 XG |
2047 | |
2048 | /* 8 means a header with RAM_SAVE_FLAG_CONTINUE. */ | |
2049 | compression_counters.compressed_size += bytes_xmit - 8; | |
2050 | compression_counters.pages++; | |
56e93d26 JQ |
2051 | } |
2052 | ||
32b05495 XG |
2053 | static bool save_page_use_compression(RAMState *rs); |
2054 | ||
ce25d337 | 2055 | static void flush_compressed_data(RAMState *rs) |
56e93d26 JQ |
2056 | { |
2057 | int idx, len, thread_count; | |
2058 | ||
32b05495 | 2059 | if (!save_page_use_compression(rs)) { |
56e93d26 JQ |
2060 | return; |
2061 | } | |
2062 | thread_count = migrate_compress_threads(); | |
a7a9a88f | 2063 | |
0d9f9a5c | 2064 | qemu_mutex_lock(&comp_done_lock); |
56e93d26 | 2065 | for (idx = 0; idx < thread_count; idx++) { |
a7a9a88f | 2066 | while (!comp_param[idx].done) { |
0d9f9a5c | 2067 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
56e93d26 | 2068 | } |
a7a9a88f | 2069 | } |
0d9f9a5c | 2070 | qemu_mutex_unlock(&comp_done_lock); |
a7a9a88f LL |
2071 | |
2072 | for (idx = 0; idx < thread_count; idx++) { | |
2073 | qemu_mutex_lock(&comp_param[idx].mutex); | |
90e56fb4 | 2074 | if (!comp_param[idx].quit) { |
ce25d337 | 2075 | len = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
5e5fdcff XG |
2076 | /* |
2077 | * it's safe to fetch zero_page without holding comp_done_lock | |
2078 | * as there is no further request submitted to the thread, | |
2079 | * i.e, the thread should be waiting for a request at this point. | |
2080 | */ | |
2081 | update_compress_thread_counts(&comp_param[idx], len); | |
56e93d26 | 2082 | } |
a7a9a88f | 2083 | qemu_mutex_unlock(&comp_param[idx].mutex); |
56e93d26 JQ |
2084 | } |
2085 | } | |
2086 | ||
2087 | static inline void set_compress_params(CompressParam *param, RAMBlock *block, | |
2088 | ram_addr_t offset) | |
2089 | { | |
2090 | param->block = block; | |
2091 | param->offset = offset; | |
2092 | } | |
2093 | ||
ce25d337 JQ |
2094 | static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block, |
2095 | ram_addr_t offset) | |
56e93d26 JQ |
2096 | { |
2097 | int idx, thread_count, bytes_xmit = -1, pages = -1; | |
1d58872a | 2098 | bool wait = migrate_compress_wait_thread(); |
56e93d26 JQ |
2099 | |
2100 | thread_count = migrate_compress_threads(); | |
0d9f9a5c | 2101 | qemu_mutex_lock(&comp_done_lock); |
1d58872a XG |
2102 | retry: |
2103 | for (idx = 0; idx < thread_count; idx++) { | |
2104 | if (comp_param[idx].done) { | |
2105 | comp_param[idx].done = false; | |
2106 | bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file); | |
2107 | qemu_mutex_lock(&comp_param[idx].mutex); | |
2108 | set_compress_params(&comp_param[idx], block, offset); | |
2109 | qemu_cond_signal(&comp_param[idx].cond); | |
2110 | qemu_mutex_unlock(&comp_param[idx].mutex); | |
2111 | pages = 1; | |
5e5fdcff | 2112 | update_compress_thread_counts(&comp_param[idx], bytes_xmit); |
56e93d26 | 2113 | break; |
56e93d26 JQ |
2114 | } |
2115 | } | |
1d58872a XG |
2116 | |
2117 | /* | |
2118 | * wait for the free thread if the user specifies 'compress-wait-thread', | |
2119 | * otherwise we will post the page out in the main thread as normal page. | |
2120 | */ | |
2121 | if (pages < 0 && wait) { | |
2122 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); | |
2123 | goto retry; | |
2124 | } | |
0d9f9a5c | 2125 | qemu_mutex_unlock(&comp_done_lock); |
56e93d26 JQ |
2126 | |
2127 | return pages; | |
2128 | } | |
2129 | ||
3d0684b2 JQ |
2130 | /** |
2131 | * find_dirty_block: find the next dirty page and update any state | |
2132 | * associated with the search process. | |
b9e60928 | 2133 | * |
3d0684b2 | 2134 | * Returns if a page is found |
b9e60928 | 2135 | * |
6f37bb8b | 2136 | * @rs: current RAM state |
3d0684b2 JQ |
2137 | * @pss: data about the state of the current dirty page scan |
2138 | * @again: set to false if the search has scanned the whole of RAM | |
b9e60928 | 2139 | */ |
f20e2865 | 2140 | static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again) |
b9e60928 | 2141 | { |
f20e2865 | 2142 | pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page); |
6f37bb8b | 2143 | if (pss->complete_round && pss->block == rs->last_seen_block && |
a935e30f | 2144 | pss->page >= rs->last_page) { |
b9e60928 DDAG |
2145 | /* |
2146 | * We've been once around the RAM and haven't found anything. | |
2147 | * Give up. | |
2148 | */ | |
2149 | *again = false; | |
2150 | return false; | |
2151 | } | |
a935e30f | 2152 | if ((pss->page << TARGET_PAGE_BITS) >= pss->block->used_length) { |
b9e60928 | 2153 | /* Didn't find anything in this RAM Block */ |
a935e30f | 2154 | pss->page = 0; |
b9e60928 DDAG |
2155 | pss->block = QLIST_NEXT_RCU(pss->block, next); |
2156 | if (!pss->block) { | |
48df9d80 XG |
2157 | /* |
2158 | * If memory migration starts over, we will meet a dirtied page | |
2159 | * which may still exists in compression threads's ring, so we | |
2160 | * should flush the compressed data to make sure the new page | |
2161 | * is not overwritten by the old one in the destination. | |
2162 | * | |
2163 | * Also If xbzrle is on, stop using the data compression at this | |
2164 | * point. In theory, xbzrle can do better than compression. | |
2165 | */ | |
2166 | flush_compressed_data(rs); | |
2167 | ||
b9e60928 DDAG |
2168 | /* Hit the end of the list */ |
2169 | pss->block = QLIST_FIRST_RCU(&ram_list.blocks); | |
2170 | /* Flag that we've looped */ | |
2171 | pss->complete_round = true; | |
6f37bb8b | 2172 | rs->ram_bulk_stage = false; |
b9e60928 DDAG |
2173 | } |
2174 | /* Didn't find anything this time, but try again on the new block */ | |
2175 | *again = true; | |
2176 | return false; | |
2177 | } else { | |
2178 | /* Can go around again, but... */ | |
2179 | *again = true; | |
2180 | /* We've found something so probably don't need to */ | |
2181 | return true; | |
2182 | } | |
2183 | } | |
2184 | ||
3d0684b2 JQ |
2185 | /** |
2186 | * unqueue_page: gets a page of the queue | |
2187 | * | |
a82d593b | 2188 | * Helper for 'get_queued_page' - gets a page off the queue |
a82d593b | 2189 | * |
3d0684b2 JQ |
2190 | * Returns the block of the page (or NULL if none available) |
2191 | * | |
ec481c6c | 2192 | * @rs: current RAM state |
3d0684b2 | 2193 | * @offset: used to return the offset within the RAMBlock |
a82d593b | 2194 | */ |
f20e2865 | 2195 | static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset) |
a82d593b DDAG |
2196 | { |
2197 | RAMBlock *block = NULL; | |
2198 | ||
ae526e32 XG |
2199 | if (QSIMPLEQ_EMPTY_ATOMIC(&rs->src_page_requests)) { |
2200 | return NULL; | |
2201 | } | |
2202 | ||
ec481c6c JQ |
2203 | qemu_mutex_lock(&rs->src_page_req_mutex); |
2204 | if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) { | |
2205 | struct RAMSrcPageRequest *entry = | |
2206 | QSIMPLEQ_FIRST(&rs->src_page_requests); | |
a82d593b DDAG |
2207 | block = entry->rb; |
2208 | *offset = entry->offset; | |
a82d593b DDAG |
2209 | |
2210 | if (entry->len > TARGET_PAGE_SIZE) { | |
2211 | entry->len -= TARGET_PAGE_SIZE; | |
2212 | entry->offset += TARGET_PAGE_SIZE; | |
2213 | } else { | |
2214 | memory_region_unref(block->mr); | |
ec481c6c | 2215 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
a82d593b | 2216 | g_free(entry); |
e03a34f8 | 2217 | migration_consume_urgent_request(); |
a82d593b DDAG |
2218 | } |
2219 | } | |
ec481c6c | 2220 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
a82d593b DDAG |
2221 | |
2222 | return block; | |
2223 | } | |
2224 | ||
3d0684b2 JQ |
2225 | /** |
2226 | * get_queued_page: unqueue a page from the postocpy requests | |
2227 | * | |
2228 | * Skips pages that are already sent (!dirty) | |
a82d593b | 2229 | * |
3d0684b2 | 2230 | * Returns if a queued page is found |
a82d593b | 2231 | * |
6f37bb8b | 2232 | * @rs: current RAM state |
3d0684b2 | 2233 | * @pss: data about the state of the current dirty page scan |
a82d593b | 2234 | */ |
f20e2865 | 2235 | static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) |
a82d593b DDAG |
2236 | { |
2237 | RAMBlock *block; | |
2238 | ram_addr_t offset; | |
2239 | bool dirty; | |
2240 | ||
2241 | do { | |
f20e2865 | 2242 | block = unqueue_page(rs, &offset); |
a82d593b DDAG |
2243 | /* |
2244 | * We're sending this page, and since it's postcopy nothing else | |
2245 | * will dirty it, and we must make sure it doesn't get sent again | |
2246 | * even if this queue request was received after the background | |
2247 | * search already sent it. | |
2248 | */ | |
2249 | if (block) { | |
f20e2865 JQ |
2250 | unsigned long page; |
2251 | ||
6b6712ef JQ |
2252 | page = offset >> TARGET_PAGE_BITS; |
2253 | dirty = test_bit(page, block->bmap); | |
a82d593b | 2254 | if (!dirty) { |
06b10688 | 2255 | trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset, |
6b6712ef | 2256 | page, test_bit(page, block->unsentmap)); |
a82d593b | 2257 | } else { |
f20e2865 | 2258 | trace_get_queued_page(block->idstr, (uint64_t)offset, page); |
a82d593b DDAG |
2259 | } |
2260 | } | |
2261 | ||
2262 | } while (block && !dirty); | |
2263 | ||
2264 | if (block) { | |
2265 | /* | |
2266 | * As soon as we start servicing pages out of order, then we have | |
2267 | * to kill the bulk stage, since the bulk stage assumes | |
2268 | * in (migration_bitmap_find_and_reset_dirty) that every page is | |
2269 | * dirty, that's no longer true. | |
2270 | */ | |
6f37bb8b | 2271 | rs->ram_bulk_stage = false; |
a82d593b DDAG |
2272 | |
2273 | /* | |
2274 | * We want the background search to continue from the queued page | |
2275 | * since the guest is likely to want other pages near to the page | |
2276 | * it just requested. | |
2277 | */ | |
2278 | pss->block = block; | |
a935e30f | 2279 | pss->page = offset >> TARGET_PAGE_BITS; |
a82d593b DDAG |
2280 | } |
2281 | ||
2282 | return !!block; | |
2283 | } | |
2284 | ||
6c595cde | 2285 | /** |
5e58f968 JQ |
2286 | * migration_page_queue_free: drop any remaining pages in the ram |
2287 | * request queue | |
6c595cde | 2288 | * |
3d0684b2 JQ |
2289 | * It should be empty at the end anyway, but in error cases there may |
2290 | * be some left. in case that there is any page left, we drop it. | |
2291 | * | |
6c595cde | 2292 | */ |
83c13382 | 2293 | static void migration_page_queue_free(RAMState *rs) |
6c595cde | 2294 | { |
ec481c6c | 2295 | struct RAMSrcPageRequest *mspr, *next_mspr; |
6c595cde DDAG |
2296 | /* This queue generally should be empty - but in the case of a failed |
2297 | * migration might have some droppings in. | |
2298 | */ | |
2299 | rcu_read_lock(); | |
ec481c6c | 2300 | QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) { |
6c595cde | 2301 | memory_region_unref(mspr->rb->mr); |
ec481c6c | 2302 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
6c595cde DDAG |
2303 | g_free(mspr); |
2304 | } | |
2305 | rcu_read_unlock(); | |
2306 | } | |
2307 | ||
2308 | /** | |
3d0684b2 JQ |
2309 | * ram_save_queue_pages: queue the page for transmission |
2310 | * | |
2311 | * A request from postcopy destination for example. | |
2312 | * | |
2313 | * Returns zero on success or negative on error | |
2314 | * | |
3d0684b2 JQ |
2315 | * @rbname: Name of the RAMBLock of the request. NULL means the |
2316 | * same that last one. | |
2317 | * @start: starting address from the start of the RAMBlock | |
2318 | * @len: length (in bytes) to send | |
6c595cde | 2319 | */ |
96506894 | 2320 | int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len) |
6c595cde DDAG |
2321 | { |
2322 | RAMBlock *ramblock; | |
53518d94 | 2323 | RAMState *rs = ram_state; |
6c595cde | 2324 | |
9360447d | 2325 | ram_counters.postcopy_requests++; |
6c595cde DDAG |
2326 | rcu_read_lock(); |
2327 | if (!rbname) { | |
2328 | /* Reuse last RAMBlock */ | |
68a098f3 | 2329 | ramblock = rs->last_req_rb; |
6c595cde DDAG |
2330 | |
2331 | if (!ramblock) { | |
2332 | /* | |
2333 | * Shouldn't happen, we can't reuse the last RAMBlock if | |
2334 | * it's the 1st request. | |
2335 | */ | |
2336 | error_report("ram_save_queue_pages no previous block"); | |
2337 | goto err; | |
2338 | } | |
2339 | } else { | |
2340 | ramblock = qemu_ram_block_by_name(rbname); | |
2341 | ||
2342 | if (!ramblock) { | |
2343 | /* We shouldn't be asked for a non-existent RAMBlock */ | |
2344 | error_report("ram_save_queue_pages no block '%s'", rbname); | |
2345 | goto err; | |
2346 | } | |
68a098f3 | 2347 | rs->last_req_rb = ramblock; |
6c595cde DDAG |
2348 | } |
2349 | trace_ram_save_queue_pages(ramblock->idstr, start, len); | |
2350 | if (start+len > ramblock->used_length) { | |
9458ad6b JQ |
2351 | error_report("%s request overrun start=" RAM_ADDR_FMT " len=" |
2352 | RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT, | |
6c595cde DDAG |
2353 | __func__, start, len, ramblock->used_length); |
2354 | goto err; | |
2355 | } | |
2356 | ||
ec481c6c JQ |
2357 | struct RAMSrcPageRequest *new_entry = |
2358 | g_malloc0(sizeof(struct RAMSrcPageRequest)); | |
6c595cde DDAG |
2359 | new_entry->rb = ramblock; |
2360 | new_entry->offset = start; | |
2361 | new_entry->len = len; | |
2362 | ||
2363 | memory_region_ref(ramblock->mr); | |
ec481c6c JQ |
2364 | qemu_mutex_lock(&rs->src_page_req_mutex); |
2365 | QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req); | |
e03a34f8 | 2366 | migration_make_urgent_request(); |
ec481c6c | 2367 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
6c595cde DDAG |
2368 | rcu_read_unlock(); |
2369 | ||
2370 | return 0; | |
2371 | ||
2372 | err: | |
2373 | rcu_read_unlock(); | |
2374 | return -1; | |
2375 | } | |
2376 | ||
d7400a34 XG |
2377 | static bool save_page_use_compression(RAMState *rs) |
2378 | { | |
2379 | if (!migrate_use_compression()) { | |
2380 | return false; | |
2381 | } | |
2382 | ||
2383 | /* | |
2384 | * If xbzrle is on, stop using the data compression after first | |
2385 | * round of migration even if compression is enabled. In theory, | |
2386 | * xbzrle can do better than compression. | |
2387 | */ | |
2388 | if (rs->ram_bulk_stage || !migrate_use_xbzrle()) { | |
2389 | return true; | |
2390 | } | |
2391 | ||
2392 | return false; | |
2393 | } | |
2394 | ||
5e5fdcff XG |
2395 | /* |
2396 | * try to compress the page before posting it out, return true if the page | |
2397 | * has been properly handled by compression, otherwise needs other | |
2398 | * paths to handle it | |
2399 | */ | |
2400 | static bool save_compress_page(RAMState *rs, RAMBlock *block, ram_addr_t offset) | |
2401 | { | |
2402 | if (!save_page_use_compression(rs)) { | |
2403 | return false; | |
2404 | } | |
2405 | ||
2406 | /* | |
2407 | * When starting the process of a new block, the first page of | |
2408 | * the block should be sent out before other pages in the same | |
2409 | * block, and all the pages in last block should have been sent | |
2410 | * out, keeping this order is important, because the 'cont' flag | |
2411 | * is used to avoid resending the block name. | |
2412 | * | |
2413 | * We post the fist page as normal page as compression will take | |
2414 | * much CPU resource. | |
2415 | */ | |
2416 | if (block != rs->last_sent_block) { | |
2417 | flush_compressed_data(rs); | |
2418 | return false; | |
2419 | } | |
2420 | ||
2421 | if (compress_page_with_multi_thread(rs, block, offset) > 0) { | |
2422 | return true; | |
2423 | } | |
2424 | ||
76e03000 | 2425 | compression_counters.busy++; |
5e5fdcff XG |
2426 | return false; |
2427 | } | |
2428 | ||
a82d593b | 2429 | /** |
3d0684b2 | 2430 | * ram_save_target_page: save one target page |
a82d593b | 2431 | * |
3d0684b2 | 2432 | * Returns the number of pages written |
a82d593b | 2433 | * |
6f37bb8b | 2434 | * @rs: current RAM state |
3d0684b2 | 2435 | * @pss: data about the page we want to send |
a82d593b | 2436 | * @last_stage: if we are at the completion stage |
a82d593b | 2437 | */ |
a0a8aa14 | 2438 | static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss, |
f20e2865 | 2439 | bool last_stage) |
a82d593b | 2440 | { |
a8ec91f9 XG |
2441 | RAMBlock *block = pss->block; |
2442 | ram_addr_t offset = pss->page << TARGET_PAGE_BITS; | |
2443 | int res; | |
2444 | ||
2445 | if (control_save_page(rs, block, offset, &res)) { | |
2446 | return res; | |
2447 | } | |
2448 | ||
5e5fdcff XG |
2449 | if (save_compress_page(rs, block, offset)) { |
2450 | return 1; | |
d7400a34 XG |
2451 | } |
2452 | ||
2453 | res = save_zero_page(rs, block, offset); | |
2454 | if (res > 0) { | |
2455 | /* Must let xbzrle know, otherwise a previous (now 0'd) cached | |
2456 | * page would be stale | |
2457 | */ | |
2458 | if (!save_page_use_compression(rs)) { | |
2459 | XBZRLE_cache_lock(); | |
2460 | xbzrle_cache_zero_page(rs, block->offset + offset); | |
2461 | XBZRLE_cache_unlock(); | |
2462 | } | |
2463 | ram_release_pages(block->idstr, offset, res); | |
2464 | return res; | |
2465 | } | |
2466 | ||
da3f56cb | 2467 | /* |
5e5fdcff XG |
2468 | * do not use multifd for compression as the first page in the new |
2469 | * block should be posted out before sending the compressed page | |
da3f56cb | 2470 | */ |
5e5fdcff | 2471 | if (!save_page_use_compression(rs) && migrate_use_multifd()) { |
b9ee2f7d | 2472 | return ram_save_multifd_page(rs, block, offset); |
a82d593b DDAG |
2473 | } |
2474 | ||
1faa5665 | 2475 | return ram_save_page(rs, pss, last_stage); |
a82d593b DDAG |
2476 | } |
2477 | ||
2478 | /** | |
3d0684b2 | 2479 | * ram_save_host_page: save a whole host page |
a82d593b | 2480 | * |
3d0684b2 JQ |
2481 | * Starting at *offset send pages up to the end of the current host |
2482 | * page. It's valid for the initial offset to point into the middle of | |
2483 | * a host page in which case the remainder of the hostpage is sent. | |
2484 | * Only dirty target pages are sent. Note that the host page size may | |
2485 | * be a huge page for this block. | |
1eb3fc0a DDAG |
2486 | * The saving stops at the boundary of the used_length of the block |
2487 | * if the RAMBlock isn't a multiple of the host page size. | |
a82d593b | 2488 | * |
3d0684b2 JQ |
2489 | * Returns the number of pages written or negative on error |
2490 | * | |
6f37bb8b | 2491 | * @rs: current RAM state |
3d0684b2 | 2492 | * @ms: current migration state |
3d0684b2 | 2493 | * @pss: data about the page we want to send |
a82d593b | 2494 | * @last_stage: if we are at the completion stage |
a82d593b | 2495 | */ |
a0a8aa14 | 2496 | static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss, |
f20e2865 | 2497 | bool last_stage) |
a82d593b DDAG |
2498 | { |
2499 | int tmppages, pages = 0; | |
a935e30f JQ |
2500 | size_t pagesize_bits = |
2501 | qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; | |
4c011c37 | 2502 | |
fbd162e6 | 2503 | if (ramblock_is_ignored(pss->block)) { |
b895de50 CLG |
2504 | error_report("block %s should not be migrated !", pss->block->idstr); |
2505 | return 0; | |
2506 | } | |
2507 | ||
a82d593b | 2508 | do { |
1faa5665 XG |
2509 | /* Check the pages is dirty and if it is send it */ |
2510 | if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) { | |
2511 | pss->page++; | |
2512 | continue; | |
2513 | } | |
2514 | ||
f20e2865 | 2515 | tmppages = ram_save_target_page(rs, pss, last_stage); |
a82d593b DDAG |
2516 | if (tmppages < 0) { |
2517 | return tmppages; | |
2518 | } | |
2519 | ||
2520 | pages += tmppages; | |
1faa5665 XG |
2521 | if (pss->block->unsentmap) { |
2522 | clear_bit(pss->page, pss->block->unsentmap); | |
2523 | } | |
2524 | ||
a935e30f | 2525 | pss->page++; |
1eb3fc0a DDAG |
2526 | } while ((pss->page & (pagesize_bits - 1)) && |
2527 | offset_in_ramblock(pss->block, pss->page << TARGET_PAGE_BITS)); | |
a82d593b DDAG |
2528 | |
2529 | /* The offset we leave with is the last one we looked at */ | |
a935e30f | 2530 | pss->page--; |
a82d593b DDAG |
2531 | return pages; |
2532 | } | |
6c595cde | 2533 | |
56e93d26 | 2534 | /** |
3d0684b2 | 2535 | * ram_find_and_save_block: finds a dirty page and sends it to f |
56e93d26 JQ |
2536 | * |
2537 | * Called within an RCU critical section. | |
2538 | * | |
e8f3735f XG |
2539 | * Returns the number of pages written where zero means no dirty pages, |
2540 | * or negative on error | |
56e93d26 | 2541 | * |
6f37bb8b | 2542 | * @rs: current RAM state |
56e93d26 | 2543 | * @last_stage: if we are at the completion stage |
a82d593b DDAG |
2544 | * |
2545 | * On systems where host-page-size > target-page-size it will send all the | |
2546 | * pages in a host page that are dirty. | |
56e93d26 JQ |
2547 | */ |
2548 | ||
ce25d337 | 2549 | static int ram_find_and_save_block(RAMState *rs, bool last_stage) |
56e93d26 | 2550 | { |
b8fb8cb7 | 2551 | PageSearchStatus pss; |
56e93d26 | 2552 | int pages = 0; |
b9e60928 | 2553 | bool again, found; |
56e93d26 | 2554 | |
0827b9e9 AA |
2555 | /* No dirty page as there is zero RAM */ |
2556 | if (!ram_bytes_total()) { | |
2557 | return pages; | |
2558 | } | |
2559 | ||
6f37bb8b | 2560 | pss.block = rs->last_seen_block; |
a935e30f | 2561 | pss.page = rs->last_page; |
b8fb8cb7 DDAG |
2562 | pss.complete_round = false; |
2563 | ||
2564 | if (!pss.block) { | |
2565 | pss.block = QLIST_FIRST_RCU(&ram_list.blocks); | |
2566 | } | |
56e93d26 | 2567 | |
b9e60928 | 2568 | do { |
a82d593b | 2569 | again = true; |
f20e2865 | 2570 | found = get_queued_page(rs, &pss); |
b9e60928 | 2571 | |
a82d593b DDAG |
2572 | if (!found) { |
2573 | /* priority queue empty, so just search for something dirty */ | |
f20e2865 | 2574 | found = find_dirty_block(rs, &pss, &again); |
a82d593b | 2575 | } |
f3f491fc | 2576 | |
a82d593b | 2577 | if (found) { |
f20e2865 | 2578 | pages = ram_save_host_page(rs, &pss, last_stage); |
56e93d26 | 2579 | } |
b9e60928 | 2580 | } while (!pages && again); |
56e93d26 | 2581 | |
6f37bb8b | 2582 | rs->last_seen_block = pss.block; |
a935e30f | 2583 | rs->last_page = pss.page; |
56e93d26 JQ |
2584 | |
2585 | return pages; | |
2586 | } | |
2587 | ||
2588 | void acct_update_position(QEMUFile *f, size_t size, bool zero) | |
2589 | { | |
2590 | uint64_t pages = size / TARGET_PAGE_SIZE; | |
f7ccd61b | 2591 | |
56e93d26 | 2592 | if (zero) { |
9360447d | 2593 | ram_counters.duplicate += pages; |
56e93d26 | 2594 | } else { |
9360447d JQ |
2595 | ram_counters.normal += pages; |
2596 | ram_counters.transferred += size; | |
56e93d26 JQ |
2597 | qemu_update_position(f, size); |
2598 | } | |
2599 | } | |
2600 | ||
fbd162e6 | 2601 | static uint64_t ram_bytes_total_common(bool count_ignored) |
56e93d26 JQ |
2602 | { |
2603 | RAMBlock *block; | |
2604 | uint64_t total = 0; | |
2605 | ||
2606 | rcu_read_lock(); | |
fbd162e6 YK |
2607 | if (count_ignored) { |
2608 | RAMBLOCK_FOREACH_MIGRATABLE(block) { | |
2609 | total += block->used_length; | |
2610 | } | |
2611 | } else { | |
2612 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
2613 | total += block->used_length; | |
2614 | } | |
99e15582 | 2615 | } |
56e93d26 JQ |
2616 | rcu_read_unlock(); |
2617 | return total; | |
2618 | } | |
2619 | ||
fbd162e6 YK |
2620 | uint64_t ram_bytes_total(void) |
2621 | { | |
2622 | return ram_bytes_total_common(false); | |
2623 | } | |
2624 | ||
f265e0e4 | 2625 | static void xbzrle_load_setup(void) |
56e93d26 | 2626 | { |
f265e0e4 | 2627 | XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE); |
56e93d26 JQ |
2628 | } |
2629 | ||
f265e0e4 JQ |
2630 | static void xbzrle_load_cleanup(void) |
2631 | { | |
2632 | g_free(XBZRLE.decoded_buf); | |
2633 | XBZRLE.decoded_buf = NULL; | |
2634 | } | |
2635 | ||
7d7c96be PX |
2636 | static void ram_state_cleanup(RAMState **rsp) |
2637 | { | |
b9ccaf6d DDAG |
2638 | if (*rsp) { |
2639 | migration_page_queue_free(*rsp); | |
2640 | qemu_mutex_destroy(&(*rsp)->bitmap_mutex); | |
2641 | qemu_mutex_destroy(&(*rsp)->src_page_req_mutex); | |
2642 | g_free(*rsp); | |
2643 | *rsp = NULL; | |
2644 | } | |
7d7c96be PX |
2645 | } |
2646 | ||
84593a08 PX |
2647 | static void xbzrle_cleanup(void) |
2648 | { | |
2649 | XBZRLE_cache_lock(); | |
2650 | if (XBZRLE.cache) { | |
2651 | cache_fini(XBZRLE.cache); | |
2652 | g_free(XBZRLE.encoded_buf); | |
2653 | g_free(XBZRLE.current_buf); | |
2654 | g_free(XBZRLE.zero_target_page); | |
2655 | XBZRLE.cache = NULL; | |
2656 | XBZRLE.encoded_buf = NULL; | |
2657 | XBZRLE.current_buf = NULL; | |
2658 | XBZRLE.zero_target_page = NULL; | |
2659 | } | |
2660 | XBZRLE_cache_unlock(); | |
2661 | } | |
2662 | ||
f265e0e4 | 2663 | static void ram_save_cleanup(void *opaque) |
56e93d26 | 2664 | { |
53518d94 | 2665 | RAMState **rsp = opaque; |
6b6712ef | 2666 | RAMBlock *block; |
eb859c53 | 2667 | |
2ff64038 LZ |
2668 | /* caller have hold iothread lock or is in a bh, so there is |
2669 | * no writing race against this migration_bitmap | |
2670 | */ | |
6b6712ef JQ |
2671 | memory_global_dirty_log_stop(); |
2672 | ||
fbd162e6 | 2673 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
6b6712ef JQ |
2674 | g_free(block->bmap); |
2675 | block->bmap = NULL; | |
2676 | g_free(block->unsentmap); | |
2677 | block->unsentmap = NULL; | |
56e93d26 JQ |
2678 | } |
2679 | ||
84593a08 | 2680 | xbzrle_cleanup(); |
f0afa331 | 2681 | compress_threads_save_cleanup(); |
7d7c96be | 2682 | ram_state_cleanup(rsp); |
56e93d26 JQ |
2683 | } |
2684 | ||
6f37bb8b | 2685 | static void ram_state_reset(RAMState *rs) |
56e93d26 | 2686 | { |
6f37bb8b JQ |
2687 | rs->last_seen_block = NULL; |
2688 | rs->last_sent_block = NULL; | |
269ace29 | 2689 | rs->last_page = 0; |
6f37bb8b JQ |
2690 | rs->last_version = ram_list.version; |
2691 | rs->ram_bulk_stage = true; | |
6eeb63f7 | 2692 | rs->fpo_enabled = false; |
56e93d26 JQ |
2693 | } |
2694 | ||
2695 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ | |
2696 | ||
4f2e4252 DDAG |
2697 | /* |
2698 | * 'expected' is the value you expect the bitmap mostly to be full | |
2699 | * of; it won't bother printing lines that are all this value. | |
2700 | * If 'todump' is null the migration bitmap is dumped. | |
2701 | */ | |
6b6712ef JQ |
2702 | void ram_debug_dump_bitmap(unsigned long *todump, bool expected, |
2703 | unsigned long pages) | |
4f2e4252 | 2704 | { |
4f2e4252 DDAG |
2705 | int64_t cur; |
2706 | int64_t linelen = 128; | |
2707 | char linebuf[129]; | |
2708 | ||
6b6712ef | 2709 | for (cur = 0; cur < pages; cur += linelen) { |
4f2e4252 DDAG |
2710 | int64_t curb; |
2711 | bool found = false; | |
2712 | /* | |
2713 | * Last line; catch the case where the line length | |
2714 | * is longer than remaining ram | |
2715 | */ | |
6b6712ef JQ |
2716 | if (cur + linelen > pages) { |
2717 | linelen = pages - cur; | |
4f2e4252 DDAG |
2718 | } |
2719 | for (curb = 0; curb < linelen; curb++) { | |
2720 | bool thisbit = test_bit(cur + curb, todump); | |
2721 | linebuf[curb] = thisbit ? '1' : '.'; | |
2722 | found = found || (thisbit != expected); | |
2723 | } | |
2724 | if (found) { | |
2725 | linebuf[curb] = '\0'; | |
2726 | fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf); | |
2727 | } | |
2728 | } | |
2729 | } | |
2730 | ||
e0b266f0 DDAG |
2731 | /* **** functions for postcopy ***** */ |
2732 | ||
ced1c616 PB |
2733 | void ram_postcopy_migrated_memory_release(MigrationState *ms) |
2734 | { | |
2735 | struct RAMBlock *block; | |
ced1c616 | 2736 | |
fbd162e6 | 2737 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
6b6712ef JQ |
2738 | unsigned long *bitmap = block->bmap; |
2739 | unsigned long range = block->used_length >> TARGET_PAGE_BITS; | |
2740 | unsigned long run_start = find_next_zero_bit(bitmap, range, 0); | |
ced1c616 PB |
2741 | |
2742 | while (run_start < range) { | |
2743 | unsigned long run_end = find_next_bit(bitmap, range, run_start + 1); | |
aaa2064c | 2744 | ram_discard_range(block->idstr, run_start << TARGET_PAGE_BITS, |
ced1c616 PB |
2745 | (run_end - run_start) << TARGET_PAGE_BITS); |
2746 | run_start = find_next_zero_bit(bitmap, range, run_end + 1); | |
2747 | } | |
2748 | } | |
2749 | } | |
2750 | ||
3d0684b2 JQ |
2751 | /** |
2752 | * postcopy_send_discard_bm_ram: discard a RAMBlock | |
2753 | * | |
2754 | * Returns zero on success | |
2755 | * | |
e0b266f0 DDAG |
2756 | * Callback from postcopy_each_ram_send_discard for each RAMBlock |
2757 | * Note: At this point the 'unsentmap' is the processed bitmap combined | |
2758 | * with the dirtymap; so a '1' means it's either dirty or unsent. | |
3d0684b2 JQ |
2759 | * |
2760 | * @ms: current migration state | |
2761 | * @pds: state for postcopy | |
2762 | * @start: RAMBlock starting page | |
2763 | * @length: RAMBlock size | |
e0b266f0 DDAG |
2764 | */ |
2765 | static int postcopy_send_discard_bm_ram(MigrationState *ms, | |
2766 | PostcopyDiscardState *pds, | |
6b6712ef | 2767 | RAMBlock *block) |
e0b266f0 | 2768 | { |
6b6712ef | 2769 | unsigned long end = block->used_length >> TARGET_PAGE_BITS; |
e0b266f0 | 2770 | unsigned long current; |
6b6712ef | 2771 | unsigned long *unsentmap = block->unsentmap; |
e0b266f0 | 2772 | |
6b6712ef | 2773 | for (current = 0; current < end; ) { |
e0b266f0 DDAG |
2774 | unsigned long one = find_next_bit(unsentmap, end, current); |
2775 | ||
2776 | if (one <= end) { | |
2777 | unsigned long zero = find_next_zero_bit(unsentmap, end, one + 1); | |
2778 | unsigned long discard_length; | |
2779 | ||
2780 | if (zero >= end) { | |
2781 | discard_length = end - one; | |
2782 | } else { | |
2783 | discard_length = zero - one; | |
2784 | } | |
d688c62d DDAG |
2785 | if (discard_length) { |
2786 | postcopy_discard_send_range(ms, pds, one, discard_length); | |
2787 | } | |
e0b266f0 DDAG |
2788 | current = one + discard_length; |
2789 | } else { | |
2790 | current = one; | |
2791 | } | |
2792 | } | |
2793 | ||
2794 | return 0; | |
2795 | } | |
2796 | ||
3d0684b2 JQ |
2797 | /** |
2798 | * postcopy_each_ram_send_discard: discard all RAMBlocks | |
2799 | * | |
2800 | * Returns 0 for success or negative for error | |
2801 | * | |
e0b266f0 DDAG |
2802 | * Utility for the outgoing postcopy code. |
2803 | * Calls postcopy_send_discard_bm_ram for each RAMBlock | |
2804 | * passing it bitmap indexes and name. | |
e0b266f0 DDAG |
2805 | * (qemu_ram_foreach_block ends up passing unscaled lengths |
2806 | * which would mean postcopy code would have to deal with target page) | |
3d0684b2 JQ |
2807 | * |
2808 | * @ms: current migration state | |
e0b266f0 DDAG |
2809 | */ |
2810 | static int postcopy_each_ram_send_discard(MigrationState *ms) | |
2811 | { | |
2812 | struct RAMBlock *block; | |
2813 | int ret; | |
2814 | ||
fbd162e6 | 2815 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
6b6712ef JQ |
2816 | PostcopyDiscardState *pds = |
2817 | postcopy_discard_send_init(ms, block->idstr); | |
e0b266f0 DDAG |
2818 | |
2819 | /* | |
2820 | * Postcopy sends chunks of bitmap over the wire, but it | |
2821 | * just needs indexes at this point, avoids it having | |
2822 | * target page specific code. | |
2823 | */ | |
6b6712ef | 2824 | ret = postcopy_send_discard_bm_ram(ms, pds, block); |
e0b266f0 DDAG |
2825 | postcopy_discard_send_finish(ms, pds); |
2826 | if (ret) { | |
2827 | return ret; | |
2828 | } | |
2829 | } | |
2830 | ||
2831 | return 0; | |
2832 | } | |
2833 | ||
3d0684b2 JQ |
2834 | /** |
2835 | * postcopy_chunk_hostpages_pass: canocalize bitmap in hostpages | |
2836 | * | |
2837 | * Helper for postcopy_chunk_hostpages; it's called twice to | |
2838 | * canonicalize the two bitmaps, that are similar, but one is | |
2839 | * inverted. | |
99e314eb | 2840 | * |
3d0684b2 JQ |
2841 | * Postcopy requires that all target pages in a hostpage are dirty or |
2842 | * clean, not a mix. This function canonicalizes the bitmaps. | |
99e314eb | 2843 | * |
3d0684b2 JQ |
2844 | * @ms: current migration state |
2845 | * @unsent_pass: if true we need to canonicalize partially unsent host pages | |
2846 | * otherwise we need to canonicalize partially dirty host pages | |
2847 | * @block: block that contains the page we want to canonicalize | |
2848 | * @pds: state for postcopy | |
99e314eb DDAG |
2849 | */ |
2850 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, bool unsent_pass, | |
2851 | RAMBlock *block, | |
2852 | PostcopyDiscardState *pds) | |
2853 | { | |
53518d94 | 2854 | RAMState *rs = ram_state; |
6b6712ef JQ |
2855 | unsigned long *bitmap = block->bmap; |
2856 | unsigned long *unsentmap = block->unsentmap; | |
29c59172 | 2857 | unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE; |
6b6712ef | 2858 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
99e314eb DDAG |
2859 | unsigned long run_start; |
2860 | ||
29c59172 DDAG |
2861 | if (block->page_size == TARGET_PAGE_SIZE) { |
2862 | /* Easy case - TPS==HPS for a non-huge page RAMBlock */ | |
2863 | return; | |
2864 | } | |
2865 | ||
99e314eb DDAG |
2866 | if (unsent_pass) { |
2867 | /* Find a sent page */ | |
6b6712ef | 2868 | run_start = find_next_zero_bit(unsentmap, pages, 0); |
99e314eb DDAG |
2869 | } else { |
2870 | /* Find a dirty page */ | |
6b6712ef | 2871 | run_start = find_next_bit(bitmap, pages, 0); |
99e314eb DDAG |
2872 | } |
2873 | ||
6b6712ef | 2874 | while (run_start < pages) { |
99e314eb DDAG |
2875 | bool do_fixup = false; |
2876 | unsigned long fixup_start_addr; | |
2877 | unsigned long host_offset; | |
2878 | ||
2879 | /* | |
2880 | * If the start of this run of pages is in the middle of a host | |
2881 | * page, then we need to fixup this host page. | |
2882 | */ | |
2883 | host_offset = run_start % host_ratio; | |
2884 | if (host_offset) { | |
2885 | do_fixup = true; | |
2886 | run_start -= host_offset; | |
2887 | fixup_start_addr = run_start; | |
2888 | /* For the next pass */ | |
2889 | run_start = run_start + host_ratio; | |
2890 | } else { | |
2891 | /* Find the end of this run */ | |
2892 | unsigned long run_end; | |
2893 | if (unsent_pass) { | |
6b6712ef | 2894 | run_end = find_next_bit(unsentmap, pages, run_start + 1); |
99e314eb | 2895 | } else { |
6b6712ef | 2896 | run_end = find_next_zero_bit(bitmap, pages, run_start + 1); |
99e314eb DDAG |
2897 | } |
2898 | /* | |
2899 | * If the end isn't at the start of a host page, then the | |
2900 | * run doesn't finish at the end of a host page | |
2901 | * and we need to discard. | |
2902 | */ | |
2903 | host_offset = run_end % host_ratio; | |
2904 | if (host_offset) { | |
2905 | do_fixup = true; | |
2906 | fixup_start_addr = run_end - host_offset; | |
2907 | /* | |
2908 | * This host page has gone, the next loop iteration starts | |
2909 | * from after the fixup | |
2910 | */ | |
2911 | run_start = fixup_start_addr + host_ratio; | |
2912 | } else { | |
2913 | /* | |
2914 | * No discards on this iteration, next loop starts from | |
2915 | * next sent/dirty page | |
2916 | */ | |
2917 | run_start = run_end + 1; | |
2918 | } | |
2919 | } | |
2920 | ||
2921 | if (do_fixup) { | |
2922 | unsigned long page; | |
2923 | ||
2924 | /* Tell the destination to discard this page */ | |
2925 | if (unsent_pass || !test_bit(fixup_start_addr, unsentmap)) { | |
2926 | /* For the unsent_pass we: | |
2927 | * discard partially sent pages | |
2928 | * For the !unsent_pass (dirty) we: | |
2929 | * discard partially dirty pages that were sent | |
2930 | * (any partially sent pages were already discarded | |
2931 | * by the previous unsent_pass) | |
2932 | */ | |
2933 | postcopy_discard_send_range(ms, pds, fixup_start_addr, | |
2934 | host_ratio); | |
2935 | } | |
2936 | ||
2937 | /* Clean up the bitmap */ | |
2938 | for (page = fixup_start_addr; | |
2939 | page < fixup_start_addr + host_ratio; page++) { | |
2940 | /* All pages in this host page are now not sent */ | |
2941 | set_bit(page, unsentmap); | |
2942 | ||
2943 | /* | |
2944 | * Remark them as dirty, updating the count for any pages | |
2945 | * that weren't previously dirty. | |
2946 | */ | |
0d8ec885 | 2947 | rs->migration_dirty_pages += !test_and_set_bit(page, bitmap); |
99e314eb DDAG |
2948 | } |
2949 | } | |
2950 | ||
2951 | if (unsent_pass) { | |
2952 | /* Find the next sent page for the next iteration */ | |
6b6712ef | 2953 | run_start = find_next_zero_bit(unsentmap, pages, run_start); |
99e314eb DDAG |
2954 | } else { |
2955 | /* Find the next dirty page for the next iteration */ | |
6b6712ef | 2956 | run_start = find_next_bit(bitmap, pages, run_start); |
99e314eb DDAG |
2957 | } |
2958 | } | |
2959 | } | |
2960 | ||
3d0684b2 JQ |
2961 | /** |
2962 | * postcopy_chuck_hostpages: discrad any partially sent host page | |
2963 | * | |
99e314eb DDAG |
2964 | * Utility for the outgoing postcopy code. |
2965 | * | |
2966 | * Discard any partially sent host-page size chunks, mark any partially | |
29c59172 DDAG |
2967 | * dirty host-page size chunks as all dirty. In this case the host-page |
2968 | * is the host-page for the particular RAMBlock, i.e. it might be a huge page | |
99e314eb | 2969 | * |
3d0684b2 JQ |
2970 | * Returns zero on success |
2971 | * | |
2972 | * @ms: current migration state | |
6b6712ef | 2973 | * @block: block we want to work with |
99e314eb | 2974 | */ |
6b6712ef | 2975 | static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block) |
99e314eb | 2976 | { |
6b6712ef JQ |
2977 | PostcopyDiscardState *pds = |
2978 | postcopy_discard_send_init(ms, block->idstr); | |
99e314eb | 2979 | |
6b6712ef JQ |
2980 | /* First pass: Discard all partially sent host pages */ |
2981 | postcopy_chunk_hostpages_pass(ms, true, block, pds); | |
2982 | /* | |
2983 | * Second pass: Ensure that all partially dirty host pages are made | |
2984 | * fully dirty. | |
2985 | */ | |
2986 | postcopy_chunk_hostpages_pass(ms, false, block, pds); | |
99e314eb | 2987 | |
6b6712ef | 2988 | postcopy_discard_send_finish(ms, pds); |
99e314eb DDAG |
2989 | return 0; |
2990 | } | |
2991 | ||
3d0684b2 JQ |
2992 | /** |
2993 | * ram_postcopy_send_discard_bitmap: transmit the discard bitmap | |
2994 | * | |
2995 | * Returns zero on success | |
2996 | * | |
e0b266f0 DDAG |
2997 | * Transmit the set of pages to be discarded after precopy to the target |
2998 | * these are pages that: | |
2999 | * a) Have been previously transmitted but are now dirty again | |
3000 | * b) Pages that have never been transmitted, this ensures that | |
3001 | * any pages on the destination that have been mapped by background | |
3002 | * tasks get discarded (transparent huge pages is the specific concern) | |
3003 | * Hopefully this is pretty sparse | |
3d0684b2 JQ |
3004 | * |
3005 | * @ms: current migration state | |
e0b266f0 DDAG |
3006 | */ |
3007 | int ram_postcopy_send_discard_bitmap(MigrationState *ms) | |
3008 | { | |
53518d94 | 3009 | RAMState *rs = ram_state; |
6b6712ef | 3010 | RAMBlock *block; |
e0b266f0 | 3011 | int ret; |
e0b266f0 DDAG |
3012 | |
3013 | rcu_read_lock(); | |
3014 | ||
3015 | /* This should be our last sync, the src is now paused */ | |
eb859c53 | 3016 | migration_bitmap_sync(rs); |
e0b266f0 | 3017 | |
6b6712ef JQ |
3018 | /* Easiest way to make sure we don't resume in the middle of a host-page */ |
3019 | rs->last_seen_block = NULL; | |
3020 | rs->last_sent_block = NULL; | |
3021 | rs->last_page = 0; | |
e0b266f0 | 3022 | |
fbd162e6 | 3023 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
6b6712ef JQ |
3024 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
3025 | unsigned long *bitmap = block->bmap; | |
3026 | unsigned long *unsentmap = block->unsentmap; | |
3027 | ||
3028 | if (!unsentmap) { | |
3029 | /* We don't have a safe way to resize the sentmap, so | |
3030 | * if the bitmap was resized it will be NULL at this | |
3031 | * point. | |
3032 | */ | |
3033 | error_report("migration ram resized during precopy phase"); | |
3034 | rcu_read_unlock(); | |
3035 | return -EINVAL; | |
3036 | } | |
3037 | /* Deal with TPS != HPS and huge pages */ | |
3038 | ret = postcopy_chunk_hostpages(ms, block); | |
3039 | if (ret) { | |
3040 | rcu_read_unlock(); | |
3041 | return ret; | |
3042 | } | |
e0b266f0 | 3043 | |
6b6712ef JQ |
3044 | /* |
3045 | * Update the unsentmap to be unsentmap = unsentmap | dirty | |
3046 | */ | |
3047 | bitmap_or(unsentmap, unsentmap, bitmap, pages); | |
e0b266f0 | 3048 | #ifdef DEBUG_POSTCOPY |
6b6712ef | 3049 | ram_debug_dump_bitmap(unsentmap, true, pages); |
e0b266f0 | 3050 | #endif |
6b6712ef JQ |
3051 | } |
3052 | trace_ram_postcopy_send_discard_bitmap(); | |
e0b266f0 DDAG |
3053 | |
3054 | ret = postcopy_each_ram_send_discard(ms); | |
3055 | rcu_read_unlock(); | |
3056 | ||
3057 | return ret; | |
3058 | } | |
3059 | ||
3d0684b2 JQ |
3060 | /** |
3061 | * ram_discard_range: discard dirtied pages at the beginning of postcopy | |
e0b266f0 | 3062 | * |
3d0684b2 | 3063 | * Returns zero on success |
e0b266f0 | 3064 | * |
36449157 JQ |
3065 | * @rbname: name of the RAMBlock of the request. NULL means the |
3066 | * same that last one. | |
3d0684b2 JQ |
3067 | * @start: RAMBlock starting page |
3068 | * @length: RAMBlock size | |
e0b266f0 | 3069 | */ |
aaa2064c | 3070 | int ram_discard_range(const char *rbname, uint64_t start, size_t length) |
e0b266f0 DDAG |
3071 | { |
3072 | int ret = -1; | |
3073 | ||
36449157 | 3074 | trace_ram_discard_range(rbname, start, length); |
d3a5038c | 3075 | |
e0b266f0 | 3076 | rcu_read_lock(); |
36449157 | 3077 | RAMBlock *rb = qemu_ram_block_by_name(rbname); |
e0b266f0 DDAG |
3078 | |
3079 | if (!rb) { | |
36449157 | 3080 | error_report("ram_discard_range: Failed to find block '%s'", rbname); |
e0b266f0 DDAG |
3081 | goto err; |
3082 | } | |
3083 | ||
814bb08f PX |
3084 | /* |
3085 | * On source VM, we don't need to update the received bitmap since | |
3086 | * we don't even have one. | |
3087 | */ | |
3088 | if (rb->receivedmap) { | |
3089 | bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(), | |
3090 | length >> qemu_target_page_bits()); | |
3091 | } | |
3092 | ||
d3a5038c | 3093 | ret = ram_block_discard_range(rb, start, length); |
e0b266f0 DDAG |
3094 | |
3095 | err: | |
3096 | rcu_read_unlock(); | |
3097 | ||
3098 | return ret; | |
3099 | } | |
3100 | ||
84593a08 PX |
3101 | /* |
3102 | * For every allocation, we will try not to crash the VM if the | |
3103 | * allocation failed. | |
3104 | */ | |
3105 | static int xbzrle_init(void) | |
3106 | { | |
3107 | Error *local_err = NULL; | |
3108 | ||
3109 | if (!migrate_use_xbzrle()) { | |
3110 | return 0; | |
3111 | } | |
3112 | ||
3113 | XBZRLE_cache_lock(); | |
3114 | ||
3115 | XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE); | |
3116 | if (!XBZRLE.zero_target_page) { | |
3117 | error_report("%s: Error allocating zero page", __func__); | |
3118 | goto err_out; | |
3119 | } | |
3120 | ||
3121 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(), | |
3122 | TARGET_PAGE_SIZE, &local_err); | |
3123 | if (!XBZRLE.cache) { | |
3124 | error_report_err(local_err); | |
3125 | goto free_zero_page; | |
3126 | } | |
3127 | ||
3128 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); | |
3129 | if (!XBZRLE.encoded_buf) { | |
3130 | error_report("%s: Error allocating encoded_buf", __func__); | |
3131 | goto free_cache; | |
3132 | } | |
3133 | ||
3134 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); | |
3135 | if (!XBZRLE.current_buf) { | |
3136 | error_report("%s: Error allocating current_buf", __func__); | |
3137 | goto free_encoded_buf; | |
3138 | } | |
3139 | ||
3140 | /* We are all good */ | |
3141 | XBZRLE_cache_unlock(); | |
3142 | return 0; | |
3143 | ||
3144 | free_encoded_buf: | |
3145 | g_free(XBZRLE.encoded_buf); | |
3146 | XBZRLE.encoded_buf = NULL; | |
3147 | free_cache: | |
3148 | cache_fini(XBZRLE.cache); | |
3149 | XBZRLE.cache = NULL; | |
3150 | free_zero_page: | |
3151 | g_free(XBZRLE.zero_target_page); | |
3152 | XBZRLE.zero_target_page = NULL; | |
3153 | err_out: | |
3154 | XBZRLE_cache_unlock(); | |
3155 | return -ENOMEM; | |
3156 | } | |
3157 | ||
53518d94 | 3158 | static int ram_state_init(RAMState **rsp) |
56e93d26 | 3159 | { |
7d00ee6a PX |
3160 | *rsp = g_try_new0(RAMState, 1); |
3161 | ||
3162 | if (!*rsp) { | |
3163 | error_report("%s: Init ramstate fail", __func__); | |
3164 | return -1; | |
3165 | } | |
53518d94 JQ |
3166 | |
3167 | qemu_mutex_init(&(*rsp)->bitmap_mutex); | |
3168 | qemu_mutex_init(&(*rsp)->src_page_req_mutex); | |
3169 | QSIMPLEQ_INIT(&(*rsp)->src_page_requests); | |
56e93d26 | 3170 | |
7d00ee6a PX |
3171 | /* |
3172 | * Count the total number of pages used by ram blocks not including any | |
3173 | * gaps due to alignment or unplugs. | |
3174 | */ | |
3175 | (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS; | |
3176 | ||
3177 | ram_state_reset(*rsp); | |
3178 | ||
3179 | return 0; | |
3180 | } | |
3181 | ||
d6eff5d7 | 3182 | static void ram_list_init_bitmaps(void) |
7d00ee6a | 3183 | { |
d6eff5d7 PX |
3184 | RAMBlock *block; |
3185 | unsigned long pages; | |
56e93d26 | 3186 | |
0827b9e9 AA |
3187 | /* Skip setting bitmap if there is no RAM */ |
3188 | if (ram_bytes_total()) { | |
fbd162e6 | 3189 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
d6eff5d7 | 3190 | pages = block->max_length >> TARGET_PAGE_BITS; |
6b6712ef JQ |
3191 | block->bmap = bitmap_new(pages); |
3192 | bitmap_set(block->bmap, 0, pages); | |
3193 | if (migrate_postcopy_ram()) { | |
3194 | block->unsentmap = bitmap_new(pages); | |
3195 | bitmap_set(block->unsentmap, 0, pages); | |
3196 | } | |
0827b9e9 | 3197 | } |
f3f491fc | 3198 | } |
d6eff5d7 PX |
3199 | } |
3200 | ||
3201 | static void ram_init_bitmaps(RAMState *rs) | |
3202 | { | |
3203 | /* For memory_global_dirty_log_start below. */ | |
3204 | qemu_mutex_lock_iothread(); | |
3205 | qemu_mutex_lock_ramlist(); | |
3206 | rcu_read_lock(); | |
f3f491fc | 3207 | |
d6eff5d7 | 3208 | ram_list_init_bitmaps(); |
56e93d26 | 3209 | memory_global_dirty_log_start(); |
bd227060 | 3210 | migration_bitmap_sync_precopy(rs); |
d6eff5d7 PX |
3211 | |
3212 | rcu_read_unlock(); | |
56e93d26 | 3213 | qemu_mutex_unlock_ramlist(); |
49877834 | 3214 | qemu_mutex_unlock_iothread(); |
d6eff5d7 PX |
3215 | } |
3216 | ||
3217 | static int ram_init_all(RAMState **rsp) | |
3218 | { | |
3219 | if (ram_state_init(rsp)) { | |
3220 | return -1; | |
3221 | } | |
3222 | ||
3223 | if (xbzrle_init()) { | |
3224 | ram_state_cleanup(rsp); | |
3225 | return -1; | |
3226 | } | |
3227 | ||
3228 | ram_init_bitmaps(*rsp); | |
a91246c9 HZ |
3229 | |
3230 | return 0; | |
3231 | } | |
3232 | ||
08614f34 PX |
3233 | static void ram_state_resume_prepare(RAMState *rs, QEMUFile *out) |
3234 | { | |
3235 | RAMBlock *block; | |
3236 | uint64_t pages = 0; | |
3237 | ||
3238 | /* | |
3239 | * Postcopy is not using xbzrle/compression, so no need for that. | |
3240 | * Also, since source are already halted, we don't need to care | |
3241 | * about dirty page logging as well. | |
3242 | */ | |
3243 | ||
fbd162e6 | 3244 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
08614f34 PX |
3245 | pages += bitmap_count_one(block->bmap, |
3246 | block->used_length >> TARGET_PAGE_BITS); | |
3247 | } | |
3248 | ||
3249 | /* This may not be aligned with current bitmaps. Recalculate. */ | |
3250 | rs->migration_dirty_pages = pages; | |
3251 | ||
3252 | rs->last_seen_block = NULL; | |
3253 | rs->last_sent_block = NULL; | |
3254 | rs->last_page = 0; | |
3255 | rs->last_version = ram_list.version; | |
3256 | /* | |
3257 | * Disable the bulk stage, otherwise we'll resend the whole RAM no | |
3258 | * matter what we have sent. | |
3259 | */ | |
3260 | rs->ram_bulk_stage = false; | |
3261 | ||
3262 | /* Update RAMState cache of output QEMUFile */ | |
3263 | rs->f = out; | |
3264 | ||
3265 | trace_ram_state_resume_prepare(pages); | |
3266 | } | |
3267 | ||
6bcb05fc WW |
3268 | /* |
3269 | * This function clears bits of the free pages reported by the caller from the | |
3270 | * migration dirty bitmap. @addr is the host address corresponding to the | |
3271 | * start of the continuous guest free pages, and @len is the total bytes of | |
3272 | * those pages. | |
3273 | */ | |
3274 | void qemu_guest_free_page_hint(void *addr, size_t len) | |
3275 | { | |
3276 | RAMBlock *block; | |
3277 | ram_addr_t offset; | |
3278 | size_t used_len, start, npages; | |
3279 | MigrationState *s = migrate_get_current(); | |
3280 | ||
3281 | /* This function is currently expected to be used during live migration */ | |
3282 | if (!migration_is_setup_or_active(s->state)) { | |
3283 | return; | |
3284 | } | |
3285 | ||
3286 | for (; len > 0; len -= used_len, addr += used_len) { | |
3287 | block = qemu_ram_block_from_host(addr, false, &offset); | |
3288 | if (unlikely(!block || offset >= block->used_length)) { | |
3289 | /* | |
3290 | * The implementation might not support RAMBlock resize during | |
3291 | * live migration, but it could happen in theory with future | |
3292 | * updates. So we add a check here to capture that case. | |
3293 | */ | |
3294 | error_report_once("%s unexpected error", __func__); | |
3295 | return; | |
3296 | } | |
3297 | ||
3298 | if (len <= block->used_length - offset) { | |
3299 | used_len = len; | |
3300 | } else { | |
3301 | used_len = block->used_length - offset; | |
3302 | } | |
3303 | ||
3304 | start = offset >> TARGET_PAGE_BITS; | |
3305 | npages = used_len >> TARGET_PAGE_BITS; | |
3306 | ||
3307 | qemu_mutex_lock(&ram_state->bitmap_mutex); | |
3308 | ram_state->migration_dirty_pages -= | |
3309 | bitmap_count_one_with_offset(block->bmap, start, npages); | |
3310 | bitmap_clear(block->bmap, start, npages); | |
3311 | qemu_mutex_unlock(&ram_state->bitmap_mutex); | |
3312 | } | |
3313 | } | |
3314 | ||
3d0684b2 JQ |
3315 | /* |
3316 | * Each of ram_save_setup, ram_save_iterate and ram_save_complete has | |
a91246c9 HZ |
3317 | * long-running RCU critical section. When rcu-reclaims in the code |
3318 | * start to become numerous it will be necessary to reduce the | |
3319 | * granularity of these critical sections. | |
3320 | */ | |
3321 | ||
3d0684b2 JQ |
3322 | /** |
3323 | * ram_save_setup: Setup RAM for migration | |
3324 | * | |
3325 | * Returns zero to indicate success and negative for error | |
3326 | * | |
3327 | * @f: QEMUFile where to send the data | |
3328 | * @opaque: RAMState pointer | |
3329 | */ | |
a91246c9 HZ |
3330 | static int ram_save_setup(QEMUFile *f, void *opaque) |
3331 | { | |
53518d94 | 3332 | RAMState **rsp = opaque; |
a91246c9 HZ |
3333 | RAMBlock *block; |
3334 | ||
dcaf446e XG |
3335 | if (compress_threads_save_setup()) { |
3336 | return -1; | |
3337 | } | |
3338 | ||
a91246c9 HZ |
3339 | /* migration has already setup the bitmap, reuse it. */ |
3340 | if (!migration_in_colo_state()) { | |
7d00ee6a | 3341 | if (ram_init_all(rsp) != 0) { |
dcaf446e | 3342 | compress_threads_save_cleanup(); |
a91246c9 | 3343 | return -1; |
53518d94 | 3344 | } |
a91246c9 | 3345 | } |
53518d94 | 3346 | (*rsp)->f = f; |
a91246c9 HZ |
3347 | |
3348 | rcu_read_lock(); | |
56e93d26 | 3349 | |
fbd162e6 | 3350 | qemu_put_be64(f, ram_bytes_total_common(true) | RAM_SAVE_FLAG_MEM_SIZE); |
56e93d26 | 3351 | |
b895de50 | 3352 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
56e93d26 JQ |
3353 | qemu_put_byte(f, strlen(block->idstr)); |
3354 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); | |
3355 | qemu_put_be64(f, block->used_length); | |
ef08fb38 DDAG |
3356 | if (migrate_postcopy_ram() && block->page_size != qemu_host_page_size) { |
3357 | qemu_put_be64(f, block->page_size); | |
3358 | } | |
fbd162e6 YK |
3359 | if (migrate_ignore_shared()) { |
3360 | qemu_put_be64(f, block->mr->addr); | |
3361 | qemu_put_byte(f, ramblock_is_ignored(block) ? 1 : 0); | |
3362 | } | |
56e93d26 JQ |
3363 | } |
3364 | ||
3365 | rcu_read_unlock(); | |
3366 | ||
3367 | ram_control_before_iterate(f, RAM_CONTROL_SETUP); | |
3368 | ram_control_after_iterate(f, RAM_CONTROL_SETUP); | |
3369 | ||
6df264ac | 3370 | multifd_send_sync_main(); |
56e93d26 | 3371 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
35374cbd | 3372 | qemu_fflush(f); |
56e93d26 JQ |
3373 | |
3374 | return 0; | |
3375 | } | |
3376 | ||
3d0684b2 JQ |
3377 | /** |
3378 | * ram_save_iterate: iterative stage for migration | |
3379 | * | |
3380 | * Returns zero to indicate success and negative for error | |
3381 | * | |
3382 | * @f: QEMUFile where to send the data | |
3383 | * @opaque: RAMState pointer | |
3384 | */ | |
56e93d26 JQ |
3385 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
3386 | { | |
53518d94 JQ |
3387 | RAMState **temp = opaque; |
3388 | RAMState *rs = *temp; | |
56e93d26 JQ |
3389 | int ret; |
3390 | int i; | |
3391 | int64_t t0; | |
5c90308f | 3392 | int done = 0; |
56e93d26 | 3393 | |
b2557345 PL |
3394 | if (blk_mig_bulk_active()) { |
3395 | /* Avoid transferring ram during bulk phase of block migration as | |
3396 | * the bulk phase will usually take a long time and transferring | |
3397 | * ram updates during that time is pointless. */ | |
3398 | goto out; | |
3399 | } | |
3400 | ||
56e93d26 | 3401 | rcu_read_lock(); |
6f37bb8b JQ |
3402 | if (ram_list.version != rs->last_version) { |
3403 | ram_state_reset(rs); | |
56e93d26 JQ |
3404 | } |
3405 | ||
3406 | /* Read version before ram_list.blocks */ | |
3407 | smp_rmb(); | |
3408 | ||
3409 | ram_control_before_iterate(f, RAM_CONTROL_ROUND); | |
3410 | ||
3411 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); | |
3412 | i = 0; | |
e03a34f8 DDAG |
3413 | while ((ret = qemu_file_rate_limit(f)) == 0 || |
3414 | !QSIMPLEQ_EMPTY(&rs->src_page_requests)) { | |
56e93d26 JQ |
3415 | int pages; |
3416 | ||
e03a34f8 DDAG |
3417 | if (qemu_file_get_error(f)) { |
3418 | break; | |
3419 | } | |
3420 | ||
ce25d337 | 3421 | pages = ram_find_and_save_block(rs, false); |
56e93d26 JQ |
3422 | /* no more pages to sent */ |
3423 | if (pages == 0) { | |
5c90308f | 3424 | done = 1; |
56e93d26 JQ |
3425 | break; |
3426 | } | |
e8f3735f XG |
3427 | |
3428 | if (pages < 0) { | |
3429 | qemu_file_set_error(f, pages); | |
3430 | break; | |
3431 | } | |
3432 | ||
be8b02ed | 3433 | rs->target_page_count += pages; |
070afca2 | 3434 | |
56e93d26 JQ |
3435 | /* we want to check in the 1st loop, just in case it was the 1st time |
3436 | and we had to sync the dirty bitmap. | |
3437 | qemu_get_clock_ns() is a bit expensive, so we only check each some | |
3438 | iterations | |
3439 | */ | |
3440 | if ((i & 63) == 0) { | |
3441 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / 1000000; | |
3442 | if (t1 > MAX_WAIT) { | |
55c4446b | 3443 | trace_ram_save_iterate_big_wait(t1, i); |
56e93d26 JQ |
3444 | break; |
3445 | } | |
3446 | } | |
3447 | i++; | |
3448 | } | |
56e93d26 JQ |
3449 | rcu_read_unlock(); |
3450 | ||
3451 | /* | |
3452 | * Must occur before EOS (or any QEMUFile operation) | |
3453 | * because of RDMA protocol. | |
3454 | */ | |
3455 | ram_control_after_iterate(f, RAM_CONTROL_ROUND); | |
3456 | ||
6df264ac | 3457 | multifd_send_sync_main(); |
b2557345 | 3458 | out: |
56e93d26 | 3459 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
35374cbd | 3460 | qemu_fflush(f); |
9360447d | 3461 | ram_counters.transferred += 8; |
56e93d26 JQ |
3462 | |
3463 | ret = qemu_file_get_error(f); | |
3464 | if (ret < 0) { | |
3465 | return ret; | |
3466 | } | |
3467 | ||
5c90308f | 3468 | return done; |
56e93d26 JQ |
3469 | } |
3470 | ||
3d0684b2 JQ |
3471 | /** |
3472 | * ram_save_complete: function called to send the remaining amount of ram | |
3473 | * | |
e8f3735f | 3474 | * Returns zero to indicate success or negative on error |
3d0684b2 JQ |
3475 | * |
3476 | * Called with iothread lock | |
3477 | * | |
3478 | * @f: QEMUFile where to send the data | |
3479 | * @opaque: RAMState pointer | |
3480 | */ | |
56e93d26 JQ |
3481 | static int ram_save_complete(QEMUFile *f, void *opaque) |
3482 | { | |
53518d94 JQ |
3483 | RAMState **temp = opaque; |
3484 | RAMState *rs = *temp; | |
e8f3735f | 3485 | int ret = 0; |
6f37bb8b | 3486 | |
56e93d26 JQ |
3487 | rcu_read_lock(); |
3488 | ||
5727309d | 3489 | if (!migration_in_postcopy()) { |
bd227060 | 3490 | migration_bitmap_sync_precopy(rs); |
663e6c1d | 3491 | } |
56e93d26 JQ |
3492 | |
3493 | ram_control_before_iterate(f, RAM_CONTROL_FINISH); | |
3494 | ||
3495 | /* try transferring iterative blocks of memory */ | |
3496 | ||
3497 | /* flush all remaining blocks regardless of rate limiting */ | |
3498 | while (true) { | |
3499 | int pages; | |
3500 | ||
ce25d337 | 3501 | pages = ram_find_and_save_block(rs, !migration_in_colo_state()); |
56e93d26 JQ |
3502 | /* no more blocks to sent */ |
3503 | if (pages == 0) { | |
3504 | break; | |
3505 | } | |
e8f3735f XG |
3506 | if (pages < 0) { |
3507 | ret = pages; | |
3508 | break; | |
3509 | } | |
56e93d26 JQ |
3510 | } |
3511 | ||
ce25d337 | 3512 | flush_compressed_data(rs); |
56e93d26 | 3513 | ram_control_after_iterate(f, RAM_CONTROL_FINISH); |
56e93d26 JQ |
3514 | |
3515 | rcu_read_unlock(); | |
d09a6fde | 3516 | |
6df264ac | 3517 | multifd_send_sync_main(); |
56e93d26 | 3518 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
35374cbd | 3519 | qemu_fflush(f); |
56e93d26 | 3520 | |
e8f3735f | 3521 | return ret; |
56e93d26 JQ |
3522 | } |
3523 | ||
c31b098f | 3524 | static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, |
47995026 VSO |
3525 | uint64_t *res_precopy_only, |
3526 | uint64_t *res_compatible, | |
3527 | uint64_t *res_postcopy_only) | |
56e93d26 | 3528 | { |
53518d94 JQ |
3529 | RAMState **temp = opaque; |
3530 | RAMState *rs = *temp; | |
56e93d26 JQ |
3531 | uint64_t remaining_size; |
3532 | ||
9edabd4d | 3533 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
56e93d26 | 3534 | |
5727309d | 3535 | if (!migration_in_postcopy() && |
663e6c1d | 3536 | remaining_size < max_size) { |
56e93d26 JQ |
3537 | qemu_mutex_lock_iothread(); |
3538 | rcu_read_lock(); | |
bd227060 | 3539 | migration_bitmap_sync_precopy(rs); |
56e93d26 JQ |
3540 | rcu_read_unlock(); |
3541 | qemu_mutex_unlock_iothread(); | |
9edabd4d | 3542 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
56e93d26 | 3543 | } |
c31b098f | 3544 | |
86e1167e VSO |
3545 | if (migrate_postcopy_ram()) { |
3546 | /* We can do postcopy, and all the data is postcopiable */ | |
47995026 | 3547 | *res_compatible += remaining_size; |
86e1167e | 3548 | } else { |
47995026 | 3549 | *res_precopy_only += remaining_size; |
86e1167e | 3550 | } |
56e93d26 JQ |
3551 | } |
3552 | ||
3553 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) | |
3554 | { | |
3555 | unsigned int xh_len; | |
3556 | int xh_flags; | |
063e760a | 3557 | uint8_t *loaded_data; |
56e93d26 | 3558 | |
56e93d26 JQ |
3559 | /* extract RLE header */ |
3560 | xh_flags = qemu_get_byte(f); | |
3561 | xh_len = qemu_get_be16(f); | |
3562 | ||
3563 | if (xh_flags != ENCODING_FLAG_XBZRLE) { | |
3564 | error_report("Failed to load XBZRLE page - wrong compression!"); | |
3565 | return -1; | |
3566 | } | |
3567 | ||
3568 | if (xh_len > TARGET_PAGE_SIZE) { | |
3569 | error_report("Failed to load XBZRLE page - len overflow!"); | |
3570 | return -1; | |
3571 | } | |
f265e0e4 | 3572 | loaded_data = XBZRLE.decoded_buf; |
56e93d26 | 3573 | /* load data and decode */ |
f265e0e4 | 3574 | /* it can change loaded_data to point to an internal buffer */ |
063e760a | 3575 | qemu_get_buffer_in_place(f, &loaded_data, xh_len); |
56e93d26 JQ |
3576 | |
3577 | /* decode RLE */ | |
063e760a | 3578 | if (xbzrle_decode_buffer(loaded_data, xh_len, host, |
56e93d26 JQ |
3579 | TARGET_PAGE_SIZE) == -1) { |
3580 | error_report("Failed to load XBZRLE page - decode error!"); | |
3581 | return -1; | |
3582 | } | |
3583 | ||
3584 | return 0; | |
3585 | } | |
3586 | ||
3d0684b2 JQ |
3587 | /** |
3588 | * ram_block_from_stream: read a RAMBlock id from the migration stream | |
3589 | * | |
3590 | * Must be called from within a rcu critical section. | |
3591 | * | |
56e93d26 | 3592 | * Returns a pointer from within the RCU-protected ram_list. |
a7180877 | 3593 | * |
3d0684b2 JQ |
3594 | * @f: QEMUFile where to read the data from |
3595 | * @flags: Page flags (mostly to see if it's a continuation of previous block) | |
a7180877 | 3596 | */ |
3d0684b2 | 3597 | static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags) |
56e93d26 JQ |
3598 | { |
3599 | static RAMBlock *block = NULL; | |
3600 | char id[256]; | |
3601 | uint8_t len; | |
3602 | ||
3603 | if (flags & RAM_SAVE_FLAG_CONTINUE) { | |
4c4bad48 | 3604 | if (!block) { |
56e93d26 JQ |
3605 | error_report("Ack, bad migration stream!"); |
3606 | return NULL; | |
3607 | } | |
4c4bad48 | 3608 | return block; |
56e93d26 JQ |
3609 | } |
3610 | ||
3611 | len = qemu_get_byte(f); | |
3612 | qemu_get_buffer(f, (uint8_t *)id, len); | |
3613 | id[len] = 0; | |
3614 | ||
e3dd7493 | 3615 | block = qemu_ram_block_by_name(id); |
4c4bad48 HZ |
3616 | if (!block) { |
3617 | error_report("Can't find block %s", id); | |
3618 | return NULL; | |
56e93d26 JQ |
3619 | } |
3620 | ||
fbd162e6 | 3621 | if (ramblock_is_ignored(block)) { |
b895de50 CLG |
3622 | error_report("block %s should not be migrated !", id); |
3623 | return NULL; | |
3624 | } | |
3625 | ||
4c4bad48 HZ |
3626 | return block; |
3627 | } | |
3628 | ||
3629 | static inline void *host_from_ram_block_offset(RAMBlock *block, | |
3630 | ram_addr_t offset) | |
3631 | { | |
3632 | if (!offset_in_ramblock(block, offset)) { | |
3633 | return NULL; | |
3634 | } | |
3635 | ||
3636 | return block->host + offset; | |
56e93d26 JQ |
3637 | } |
3638 | ||
13af18f2 ZC |
3639 | static inline void *colo_cache_from_block_offset(RAMBlock *block, |
3640 | ram_addr_t offset) | |
3641 | { | |
3642 | if (!offset_in_ramblock(block, offset)) { | |
3643 | return NULL; | |
3644 | } | |
3645 | if (!block->colo_cache) { | |
3646 | error_report("%s: colo_cache is NULL in block :%s", | |
3647 | __func__, block->idstr); | |
3648 | return NULL; | |
3649 | } | |
7d9acafa ZC |
3650 | |
3651 | /* | |
3652 | * During colo checkpoint, we need bitmap of these migrated pages. | |
3653 | * It help us to decide which pages in ram cache should be flushed | |
3654 | * into VM's RAM later. | |
3655 | */ | |
3656 | if (!test_and_set_bit(offset >> TARGET_PAGE_BITS, block->bmap)) { | |
3657 | ram_state->migration_dirty_pages++; | |
3658 | } | |
13af18f2 ZC |
3659 | return block->colo_cache + offset; |
3660 | } | |
3661 | ||
3d0684b2 JQ |
3662 | /** |
3663 | * ram_handle_compressed: handle the zero page case | |
3664 | * | |
56e93d26 JQ |
3665 | * If a page (or a whole RDMA chunk) has been |
3666 | * determined to be zero, then zap it. | |
3d0684b2 JQ |
3667 | * |
3668 | * @host: host address for the zero page | |
3669 | * @ch: what the page is filled from. We only support zero | |
3670 | * @size: size of the zero page | |
56e93d26 JQ |
3671 | */ |
3672 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) | |
3673 | { | |
3674 | if (ch != 0 || !is_zero_range(host, size)) { | |
3675 | memset(host, ch, size); | |
3676 | } | |
3677 | } | |
3678 | ||
797ca154 XG |
3679 | /* return the size after decompression, or negative value on error */ |
3680 | static int | |
3681 | qemu_uncompress_data(z_stream *stream, uint8_t *dest, size_t dest_len, | |
3682 | const uint8_t *source, size_t source_len) | |
3683 | { | |
3684 | int err; | |
3685 | ||
3686 | err = inflateReset(stream); | |
3687 | if (err != Z_OK) { | |
3688 | return -1; | |
3689 | } | |
3690 | ||
3691 | stream->avail_in = source_len; | |
3692 | stream->next_in = (uint8_t *)source; | |
3693 | stream->avail_out = dest_len; | |
3694 | stream->next_out = dest; | |
3695 | ||
3696 | err = inflate(stream, Z_NO_FLUSH); | |
3697 | if (err != Z_STREAM_END) { | |
3698 | return -1; | |
3699 | } | |
3700 | ||
3701 | return stream->total_out; | |
3702 | } | |
3703 | ||
56e93d26 JQ |
3704 | static void *do_data_decompress(void *opaque) |
3705 | { | |
3706 | DecompressParam *param = opaque; | |
3707 | unsigned long pagesize; | |
33d151f4 | 3708 | uint8_t *des; |
34ab9e97 | 3709 | int len, ret; |
56e93d26 | 3710 | |
33d151f4 | 3711 | qemu_mutex_lock(¶m->mutex); |
90e56fb4 | 3712 | while (!param->quit) { |
33d151f4 LL |
3713 | if (param->des) { |
3714 | des = param->des; | |
3715 | len = param->len; | |
3716 | param->des = 0; | |
3717 | qemu_mutex_unlock(¶m->mutex); | |
3718 | ||
56e93d26 | 3719 | pagesize = TARGET_PAGE_SIZE; |
34ab9e97 XG |
3720 | |
3721 | ret = qemu_uncompress_data(¶m->stream, des, pagesize, | |
3722 | param->compbuf, len); | |
f548222c | 3723 | if (ret < 0 && migrate_get_current()->decompress_error_check) { |
34ab9e97 XG |
3724 | error_report("decompress data failed"); |
3725 | qemu_file_set_error(decomp_file, ret); | |
3726 | } | |
73a8912b | 3727 | |
33d151f4 LL |
3728 | qemu_mutex_lock(&decomp_done_lock); |
3729 | param->done = true; | |
3730 | qemu_cond_signal(&decomp_done_cond); | |
3731 | qemu_mutex_unlock(&decomp_done_lock); | |
3732 | ||
3733 | qemu_mutex_lock(¶m->mutex); | |
3734 | } else { | |
3735 | qemu_cond_wait(¶m->cond, ¶m->mutex); | |
3736 | } | |
56e93d26 | 3737 | } |
33d151f4 | 3738 | qemu_mutex_unlock(¶m->mutex); |
56e93d26 JQ |
3739 | |
3740 | return NULL; | |
3741 | } | |
3742 | ||
34ab9e97 | 3743 | static int wait_for_decompress_done(void) |
5533b2e9 LL |
3744 | { |
3745 | int idx, thread_count; | |
3746 | ||
3747 | if (!migrate_use_compression()) { | |
34ab9e97 | 3748 | return 0; |
5533b2e9 LL |
3749 | } |
3750 | ||
3751 | thread_count = migrate_decompress_threads(); | |
3752 | qemu_mutex_lock(&decomp_done_lock); | |
3753 | for (idx = 0; idx < thread_count; idx++) { | |
3754 | while (!decomp_param[idx].done) { | |
3755 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); | |
3756 | } | |
3757 | } | |
3758 | qemu_mutex_unlock(&decomp_done_lock); | |
34ab9e97 | 3759 | return qemu_file_get_error(decomp_file); |
5533b2e9 LL |
3760 | } |
3761 | ||
f0afa331 | 3762 | static void compress_threads_load_cleanup(void) |
56e93d26 JQ |
3763 | { |
3764 | int i, thread_count; | |
3765 | ||
3416ab5b JQ |
3766 | if (!migrate_use_compression()) { |
3767 | return; | |
3768 | } | |
56e93d26 JQ |
3769 | thread_count = migrate_decompress_threads(); |
3770 | for (i = 0; i < thread_count; i++) { | |
797ca154 XG |
3771 | /* |
3772 | * we use it as a indicator which shows if the thread is | |
3773 | * properly init'd or not | |
3774 | */ | |
3775 | if (!decomp_param[i].compbuf) { | |
3776 | break; | |
3777 | } | |
3778 | ||
56e93d26 | 3779 | qemu_mutex_lock(&decomp_param[i].mutex); |
90e56fb4 | 3780 | decomp_param[i].quit = true; |
56e93d26 JQ |
3781 | qemu_cond_signal(&decomp_param[i].cond); |
3782 | qemu_mutex_unlock(&decomp_param[i].mutex); | |
3783 | } | |
3784 | for (i = 0; i < thread_count; i++) { | |
797ca154 XG |
3785 | if (!decomp_param[i].compbuf) { |
3786 | break; | |
3787 | } | |
3788 | ||
56e93d26 JQ |
3789 | qemu_thread_join(decompress_threads + i); |
3790 | qemu_mutex_destroy(&decomp_param[i].mutex); | |
3791 | qemu_cond_destroy(&decomp_param[i].cond); | |
797ca154 | 3792 | inflateEnd(&decomp_param[i].stream); |
56e93d26 | 3793 | g_free(decomp_param[i].compbuf); |
797ca154 | 3794 | decomp_param[i].compbuf = NULL; |
56e93d26 JQ |
3795 | } |
3796 | g_free(decompress_threads); | |
3797 | g_free(decomp_param); | |
56e93d26 JQ |
3798 | decompress_threads = NULL; |
3799 | decomp_param = NULL; | |
34ab9e97 | 3800 | decomp_file = NULL; |
56e93d26 JQ |
3801 | } |
3802 | ||
34ab9e97 | 3803 | static int compress_threads_load_setup(QEMUFile *f) |
797ca154 XG |
3804 | { |
3805 | int i, thread_count; | |
3806 | ||
3807 | if (!migrate_use_compression()) { | |
3808 | return 0; | |
3809 | } | |
3810 | ||
3811 | thread_count = migrate_decompress_threads(); | |
3812 | decompress_threads = g_new0(QemuThread, thread_count); | |
3813 | decomp_param = g_new0(DecompressParam, thread_count); | |
3814 | qemu_mutex_init(&decomp_done_lock); | |
3815 | qemu_cond_init(&decomp_done_cond); | |
34ab9e97 | 3816 | decomp_file = f; |
797ca154 XG |
3817 | for (i = 0; i < thread_count; i++) { |
3818 | if (inflateInit(&decomp_param[i].stream) != Z_OK) { | |
3819 | goto exit; | |
3820 | } | |
3821 | ||
3822 | decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE)); | |
3823 | qemu_mutex_init(&decomp_param[i].mutex); | |
3824 | qemu_cond_init(&decomp_param[i].cond); | |
3825 | decomp_param[i].done = true; | |
3826 | decomp_param[i].quit = false; | |
3827 | qemu_thread_create(decompress_threads + i, "decompress", | |
3828 | do_data_decompress, decomp_param + i, | |
3829 | QEMU_THREAD_JOINABLE); | |
3830 | } | |
3831 | return 0; | |
3832 | exit: | |
3833 | compress_threads_load_cleanup(); | |
3834 | return -1; | |
3835 | } | |
3836 | ||
c1bc6626 | 3837 | static void decompress_data_with_multi_threads(QEMUFile *f, |
56e93d26 JQ |
3838 | void *host, int len) |
3839 | { | |
3840 | int idx, thread_count; | |
3841 | ||
3842 | thread_count = migrate_decompress_threads(); | |
73a8912b | 3843 | qemu_mutex_lock(&decomp_done_lock); |
56e93d26 JQ |
3844 | while (true) { |
3845 | for (idx = 0; idx < thread_count; idx++) { | |
73a8912b | 3846 | if (decomp_param[idx].done) { |
33d151f4 LL |
3847 | decomp_param[idx].done = false; |
3848 | qemu_mutex_lock(&decomp_param[idx].mutex); | |
c1bc6626 | 3849 | qemu_get_buffer(f, decomp_param[idx].compbuf, len); |
56e93d26 JQ |
3850 | decomp_param[idx].des = host; |
3851 | decomp_param[idx].len = len; | |
33d151f4 LL |
3852 | qemu_cond_signal(&decomp_param[idx].cond); |
3853 | qemu_mutex_unlock(&decomp_param[idx].mutex); | |
56e93d26 JQ |
3854 | break; |
3855 | } | |
3856 | } | |
3857 | if (idx < thread_count) { | |
3858 | break; | |
73a8912b LL |
3859 | } else { |
3860 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); | |
56e93d26 JQ |
3861 | } |
3862 | } | |
73a8912b | 3863 | qemu_mutex_unlock(&decomp_done_lock); |
56e93d26 JQ |
3864 | } |
3865 | ||
13af18f2 ZC |
3866 | /* |
3867 | * colo cache: this is for secondary VM, we cache the whole | |
3868 | * memory of the secondary VM, it is need to hold the global lock | |
3869 | * to call this helper. | |
3870 | */ | |
3871 | int colo_init_ram_cache(void) | |
3872 | { | |
3873 | RAMBlock *block; | |
3874 | ||
3875 | rcu_read_lock(); | |
fbd162e6 | 3876 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
13af18f2 ZC |
3877 | block->colo_cache = qemu_anon_ram_alloc(block->used_length, |
3878 | NULL, | |
3879 | false); | |
3880 | if (!block->colo_cache) { | |
3881 | error_report("%s: Can't alloc memory for COLO cache of block %s," | |
3882 | "size 0x" RAM_ADDR_FMT, __func__, block->idstr, | |
3883 | block->used_length); | |
3884 | goto out_locked; | |
3885 | } | |
3886 | memcpy(block->colo_cache, block->host, block->used_length); | |
3887 | } | |
3888 | rcu_read_unlock(); | |
7d9acafa ZC |
3889 | /* |
3890 | * Record the dirty pages that sent by PVM, we use this dirty bitmap together | |
3891 | * with to decide which page in cache should be flushed into SVM's RAM. Here | |
3892 | * we use the same name 'ram_bitmap' as for migration. | |
3893 | */ | |
3894 | if (ram_bytes_total()) { | |
3895 | RAMBlock *block; | |
3896 | ||
fbd162e6 | 3897 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
7d9acafa ZC |
3898 | unsigned long pages = block->max_length >> TARGET_PAGE_BITS; |
3899 | ||
3900 | block->bmap = bitmap_new(pages); | |
3901 | bitmap_set(block->bmap, 0, pages); | |
3902 | } | |
3903 | } | |
3904 | ram_state = g_new0(RAMState, 1); | |
3905 | ram_state->migration_dirty_pages = 0; | |
d1955d22 | 3906 | memory_global_dirty_log_start(); |
7d9acafa | 3907 | |
13af18f2 ZC |
3908 | return 0; |
3909 | ||
3910 | out_locked: | |
7d9acafa | 3911 | |
fbd162e6 | 3912 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
13af18f2 ZC |
3913 | if (block->colo_cache) { |
3914 | qemu_anon_ram_free(block->colo_cache, block->used_length); | |
3915 | block->colo_cache = NULL; | |
3916 | } | |
3917 | } | |
3918 | ||
3919 | rcu_read_unlock(); | |
3920 | return -errno; | |
3921 | } | |
3922 | ||
3923 | /* It is need to hold the global lock to call this helper */ | |
3924 | void colo_release_ram_cache(void) | |
3925 | { | |
3926 | RAMBlock *block; | |
3927 | ||
d1955d22 | 3928 | memory_global_dirty_log_stop(); |
fbd162e6 | 3929 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
7d9acafa ZC |
3930 | g_free(block->bmap); |
3931 | block->bmap = NULL; | |
3932 | } | |
3933 | ||
13af18f2 | 3934 | rcu_read_lock(); |
7d9acafa | 3935 | |
fbd162e6 | 3936 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
13af18f2 ZC |
3937 | if (block->colo_cache) { |
3938 | qemu_anon_ram_free(block->colo_cache, block->used_length); | |
3939 | block->colo_cache = NULL; | |
3940 | } | |
3941 | } | |
7d9acafa | 3942 | |
13af18f2 | 3943 | rcu_read_unlock(); |
7d9acafa ZC |
3944 | g_free(ram_state); |
3945 | ram_state = NULL; | |
13af18f2 ZC |
3946 | } |
3947 | ||
f265e0e4 JQ |
3948 | /** |
3949 | * ram_load_setup: Setup RAM for migration incoming side | |
3950 | * | |
3951 | * Returns zero to indicate success and negative for error | |
3952 | * | |
3953 | * @f: QEMUFile where to receive the data | |
3954 | * @opaque: RAMState pointer | |
3955 | */ | |
3956 | static int ram_load_setup(QEMUFile *f, void *opaque) | |
3957 | { | |
34ab9e97 | 3958 | if (compress_threads_load_setup(f)) { |
797ca154 XG |
3959 | return -1; |
3960 | } | |
3961 | ||
f265e0e4 | 3962 | xbzrle_load_setup(); |
f9494614 | 3963 | ramblock_recv_map_init(); |
13af18f2 | 3964 | |
f265e0e4 JQ |
3965 | return 0; |
3966 | } | |
3967 | ||
3968 | static int ram_load_cleanup(void *opaque) | |
3969 | { | |
f9494614 | 3970 | RAMBlock *rb; |
56eb90af | 3971 | |
fbd162e6 | 3972 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
56eb90af JH |
3973 | if (ramblock_is_pmem(rb)) { |
3974 | pmem_persist(rb->host, rb->used_length); | |
3975 | } | |
3976 | } | |
3977 | ||
f265e0e4 | 3978 | xbzrle_load_cleanup(); |
f0afa331 | 3979 | compress_threads_load_cleanup(); |
f9494614 | 3980 | |
fbd162e6 | 3981 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
f9494614 AP |
3982 | g_free(rb->receivedmap); |
3983 | rb->receivedmap = NULL; | |
3984 | } | |
13af18f2 | 3985 | |
f265e0e4 JQ |
3986 | return 0; |
3987 | } | |
3988 | ||
3d0684b2 JQ |
3989 | /** |
3990 | * ram_postcopy_incoming_init: allocate postcopy data structures | |
3991 | * | |
3992 | * Returns 0 for success and negative if there was one error | |
3993 | * | |
3994 | * @mis: current migration incoming state | |
3995 | * | |
3996 | * Allocate data structures etc needed by incoming migration with | |
3997 | * postcopy-ram. postcopy-ram's similarly names | |
3998 | * postcopy_ram_incoming_init does the work. | |
1caddf8a DDAG |
3999 | */ |
4000 | int ram_postcopy_incoming_init(MigrationIncomingState *mis) | |
4001 | { | |
c136180c | 4002 | return postcopy_ram_incoming_init(mis); |
1caddf8a DDAG |
4003 | } |
4004 | ||
3d0684b2 JQ |
4005 | /** |
4006 | * ram_load_postcopy: load a page in postcopy case | |
4007 | * | |
4008 | * Returns 0 for success or -errno in case of error | |
4009 | * | |
a7180877 DDAG |
4010 | * Called in postcopy mode by ram_load(). |
4011 | * rcu_read_lock is taken prior to this being called. | |
3d0684b2 JQ |
4012 | * |
4013 | * @f: QEMUFile where to send the data | |
a7180877 DDAG |
4014 | */ |
4015 | static int ram_load_postcopy(QEMUFile *f) | |
4016 | { | |
4017 | int flags = 0, ret = 0; | |
4018 | bool place_needed = false; | |
1aa83678 | 4019 | bool matches_target_page_size = false; |
a7180877 DDAG |
4020 | MigrationIncomingState *mis = migration_incoming_get_current(); |
4021 | /* Temporary page that is later 'placed' */ | |
4022 | void *postcopy_host_page = postcopy_get_tmp_page(mis); | |
c53b7ddc | 4023 | void *last_host = NULL; |
a3b6ff6d | 4024 | bool all_zero = false; |
a7180877 DDAG |
4025 | |
4026 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { | |
4027 | ram_addr_t addr; | |
4028 | void *host = NULL; | |
4029 | void *page_buffer = NULL; | |
4030 | void *place_source = NULL; | |
df9ff5e1 | 4031 | RAMBlock *block = NULL; |
a7180877 | 4032 | uint8_t ch; |
a7180877 DDAG |
4033 | |
4034 | addr = qemu_get_be64(f); | |
7a9ddfbf PX |
4035 | |
4036 | /* | |
4037 | * If qemu file error, we should stop here, and then "addr" | |
4038 | * may be invalid | |
4039 | */ | |
4040 | ret = qemu_file_get_error(f); | |
4041 | if (ret) { | |
4042 | break; | |
4043 | } | |
4044 | ||
a7180877 DDAG |
4045 | flags = addr & ~TARGET_PAGE_MASK; |
4046 | addr &= TARGET_PAGE_MASK; | |
4047 | ||
4048 | trace_ram_load_postcopy_loop((uint64_t)addr, flags); | |
4049 | place_needed = false; | |
bb890ed5 | 4050 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE)) { |
df9ff5e1 | 4051 | block = ram_block_from_stream(f, flags); |
4c4bad48 HZ |
4052 | |
4053 | host = host_from_ram_block_offset(block, addr); | |
a7180877 DDAG |
4054 | if (!host) { |
4055 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); | |
4056 | ret = -EINVAL; | |
4057 | break; | |
4058 | } | |
1aa83678 | 4059 | matches_target_page_size = block->page_size == TARGET_PAGE_SIZE; |
a7180877 | 4060 | /* |
28abd200 DDAG |
4061 | * Postcopy requires that we place whole host pages atomically; |
4062 | * these may be huge pages for RAMBlocks that are backed by | |
4063 | * hugetlbfs. | |
a7180877 DDAG |
4064 | * To make it atomic, the data is read into a temporary page |
4065 | * that's moved into place later. | |
4066 | * The migration protocol uses, possibly smaller, target-pages | |
4067 | * however the source ensures it always sends all the components | |
4068 | * of a host page in order. | |
4069 | */ | |
4070 | page_buffer = postcopy_host_page + | |
28abd200 | 4071 | ((uintptr_t)host & (block->page_size - 1)); |
a7180877 | 4072 | /* If all TP are zero then we can optimise the place */ |
28abd200 | 4073 | if (!((uintptr_t)host & (block->page_size - 1))) { |
a7180877 | 4074 | all_zero = true; |
c53b7ddc DDAG |
4075 | } else { |
4076 | /* not the 1st TP within the HP */ | |
4077 | if (host != (last_host + TARGET_PAGE_SIZE)) { | |
9af9e0fe | 4078 | error_report("Non-sequential target page %p/%p", |
c53b7ddc DDAG |
4079 | host, last_host); |
4080 | ret = -EINVAL; | |
4081 | break; | |
4082 | } | |
a7180877 DDAG |
4083 | } |
4084 | ||
c53b7ddc | 4085 | |
a7180877 DDAG |
4086 | /* |
4087 | * If it's the last part of a host page then we place the host | |
4088 | * page | |
4089 | */ | |
4090 | place_needed = (((uintptr_t)host + TARGET_PAGE_SIZE) & | |
28abd200 | 4091 | (block->page_size - 1)) == 0; |
a7180877 DDAG |
4092 | place_source = postcopy_host_page; |
4093 | } | |
c53b7ddc | 4094 | last_host = host; |
a7180877 DDAG |
4095 | |
4096 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { | |
bb890ed5 | 4097 | case RAM_SAVE_FLAG_ZERO: |
a7180877 DDAG |
4098 | ch = qemu_get_byte(f); |
4099 | memset(page_buffer, ch, TARGET_PAGE_SIZE); | |
4100 | if (ch) { | |
4101 | all_zero = false; | |
4102 | } | |
4103 | break; | |
4104 | ||
4105 | case RAM_SAVE_FLAG_PAGE: | |
4106 | all_zero = false; | |
1aa83678 PX |
4107 | if (!matches_target_page_size) { |
4108 | /* For huge pages, we always use temporary buffer */ | |
a7180877 DDAG |
4109 | qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); |
4110 | } else { | |
1aa83678 PX |
4111 | /* |
4112 | * For small pages that matches target page size, we | |
4113 | * avoid the qemu_file copy. Instead we directly use | |
4114 | * the buffer of QEMUFile to place the page. Note: we | |
4115 | * cannot do any QEMUFile operation before using that | |
4116 | * buffer to make sure the buffer is valid when | |
4117 | * placing the page. | |
a7180877 DDAG |
4118 | */ |
4119 | qemu_get_buffer_in_place(f, (uint8_t **)&place_source, | |
4120 | TARGET_PAGE_SIZE); | |
4121 | } | |
4122 | break; | |
4123 | case RAM_SAVE_FLAG_EOS: | |
4124 | /* normal exit */ | |
6df264ac | 4125 | multifd_recv_sync_main(); |
a7180877 DDAG |
4126 | break; |
4127 | default: | |
4128 | error_report("Unknown combination of migration flags: %#x" | |
4129 | " (postcopy mode)", flags); | |
4130 | ret = -EINVAL; | |
7a9ddfbf PX |
4131 | break; |
4132 | } | |
4133 | ||
4134 | /* Detect for any possible file errors */ | |
4135 | if (!ret && qemu_file_get_error(f)) { | |
4136 | ret = qemu_file_get_error(f); | |
a7180877 DDAG |
4137 | } |
4138 | ||
7a9ddfbf | 4139 | if (!ret && place_needed) { |
a7180877 | 4140 | /* This gets called at the last target page in the host page */ |
df9ff5e1 DDAG |
4141 | void *place_dest = host + TARGET_PAGE_SIZE - block->page_size; |
4142 | ||
a7180877 | 4143 | if (all_zero) { |
df9ff5e1 | 4144 | ret = postcopy_place_page_zero(mis, place_dest, |
8be4620b | 4145 | block); |
a7180877 | 4146 | } else { |
df9ff5e1 | 4147 | ret = postcopy_place_page(mis, place_dest, |
8be4620b | 4148 | place_source, block); |
a7180877 DDAG |
4149 | } |
4150 | } | |
a7180877 DDAG |
4151 | } |
4152 | ||
4153 | return ret; | |
4154 | } | |
4155 | ||
acab30b8 DHB |
4156 | static bool postcopy_is_advised(void) |
4157 | { | |
4158 | PostcopyState ps = postcopy_state_get(); | |
4159 | return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END; | |
4160 | } | |
4161 | ||
4162 | static bool postcopy_is_running(void) | |
4163 | { | |
4164 | PostcopyState ps = postcopy_state_get(); | |
4165 | return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END; | |
4166 | } | |
4167 | ||
e6f4aa18 ZC |
4168 | /* |
4169 | * Flush content of RAM cache into SVM's memory. | |
4170 | * Only flush the pages that be dirtied by PVM or SVM or both. | |
4171 | */ | |
4172 | static void colo_flush_ram_cache(void) | |
4173 | { | |
4174 | RAMBlock *block = NULL; | |
4175 | void *dst_host; | |
4176 | void *src_host; | |
4177 | unsigned long offset = 0; | |
4178 | ||
d1955d22 HZ |
4179 | memory_global_dirty_log_sync(); |
4180 | rcu_read_lock(); | |
fbd162e6 | 4181 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
d1955d22 HZ |
4182 | migration_bitmap_sync_range(ram_state, block, 0, block->used_length); |
4183 | } | |
4184 | rcu_read_unlock(); | |
4185 | ||
e6f4aa18 ZC |
4186 | trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages); |
4187 | rcu_read_lock(); | |
4188 | block = QLIST_FIRST_RCU(&ram_list.blocks); | |
4189 | ||
4190 | while (block) { | |
4191 | offset = migration_bitmap_find_dirty(ram_state, block, offset); | |
4192 | ||
4193 | if (offset << TARGET_PAGE_BITS >= block->used_length) { | |
4194 | offset = 0; | |
4195 | block = QLIST_NEXT_RCU(block, next); | |
4196 | } else { | |
4197 | migration_bitmap_clear_dirty(ram_state, block, offset); | |
4198 | dst_host = block->host + (offset << TARGET_PAGE_BITS); | |
4199 | src_host = block->colo_cache + (offset << TARGET_PAGE_BITS); | |
4200 | memcpy(dst_host, src_host, TARGET_PAGE_SIZE); | |
4201 | } | |
4202 | } | |
4203 | ||
4204 | rcu_read_unlock(); | |
4205 | trace_colo_flush_ram_cache_end(); | |
4206 | } | |
4207 | ||
56e93d26 JQ |
4208 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
4209 | { | |
edc60127 | 4210 | int flags = 0, ret = 0, invalid_flags = 0; |
56e93d26 JQ |
4211 | static uint64_t seq_iter; |
4212 | int len = 0; | |
a7180877 DDAG |
4213 | /* |
4214 | * If system is running in postcopy mode, page inserts to host memory must | |
4215 | * be atomic | |
4216 | */ | |
acab30b8 | 4217 | bool postcopy_running = postcopy_is_running(); |
ef08fb38 | 4218 | /* ADVISE is earlier, it shows the source has the postcopy capability on */ |
acab30b8 | 4219 | bool postcopy_advised = postcopy_is_advised(); |
56e93d26 JQ |
4220 | |
4221 | seq_iter++; | |
4222 | ||
4223 | if (version_id != 4) { | |
4224 | ret = -EINVAL; | |
4225 | } | |
4226 | ||
edc60127 JQ |
4227 | if (!migrate_use_compression()) { |
4228 | invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE; | |
4229 | } | |
56e93d26 JQ |
4230 | /* This RCU critical section can be very long running. |
4231 | * When RCU reclaims in the code start to become numerous, | |
4232 | * it will be necessary to reduce the granularity of this | |
4233 | * critical section. | |
4234 | */ | |
4235 | rcu_read_lock(); | |
a7180877 DDAG |
4236 | |
4237 | if (postcopy_running) { | |
4238 | ret = ram_load_postcopy(f); | |
4239 | } | |
4240 | ||
4241 | while (!postcopy_running && !ret && !(flags & RAM_SAVE_FLAG_EOS)) { | |
56e93d26 | 4242 | ram_addr_t addr, total_ram_bytes; |
a776aa15 | 4243 | void *host = NULL; |
56e93d26 JQ |
4244 | uint8_t ch; |
4245 | ||
4246 | addr = qemu_get_be64(f); | |
4247 | flags = addr & ~TARGET_PAGE_MASK; | |
4248 | addr &= TARGET_PAGE_MASK; | |
4249 | ||
edc60127 JQ |
4250 | if (flags & invalid_flags) { |
4251 | if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) { | |
4252 | error_report("Received an unexpected compressed page"); | |
4253 | } | |
4254 | ||
4255 | ret = -EINVAL; | |
4256 | break; | |
4257 | } | |
4258 | ||
bb890ed5 | 4259 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
a776aa15 | 4260 | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { |
4c4bad48 HZ |
4261 | RAMBlock *block = ram_block_from_stream(f, flags); |
4262 | ||
13af18f2 ZC |
4263 | /* |
4264 | * After going into COLO, we should load the Page into colo_cache. | |
4265 | */ | |
4266 | if (migration_incoming_in_colo_state()) { | |
4267 | host = colo_cache_from_block_offset(block, addr); | |
4268 | } else { | |
4269 | host = host_from_ram_block_offset(block, addr); | |
4270 | } | |
a776aa15 DDAG |
4271 | if (!host) { |
4272 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); | |
4273 | ret = -EINVAL; | |
4274 | break; | |
4275 | } | |
13af18f2 ZC |
4276 | |
4277 | if (!migration_incoming_in_colo_state()) { | |
4278 | ramblock_recv_bitmap_set(block, host); | |
4279 | } | |
4280 | ||
1db9d8e5 | 4281 | trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host); |
a776aa15 DDAG |
4282 | } |
4283 | ||
56e93d26 JQ |
4284 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
4285 | case RAM_SAVE_FLAG_MEM_SIZE: | |
4286 | /* Synchronize RAM block list */ | |
4287 | total_ram_bytes = addr; | |
4288 | while (!ret && total_ram_bytes) { | |
4289 | RAMBlock *block; | |
56e93d26 JQ |
4290 | char id[256]; |
4291 | ram_addr_t length; | |
4292 | ||
4293 | len = qemu_get_byte(f); | |
4294 | qemu_get_buffer(f, (uint8_t *)id, len); | |
4295 | id[len] = 0; | |
4296 | length = qemu_get_be64(f); | |
4297 | ||
e3dd7493 | 4298 | block = qemu_ram_block_by_name(id); |
b895de50 CLG |
4299 | if (block && !qemu_ram_is_migratable(block)) { |
4300 | error_report("block %s should not be migrated !", id); | |
4301 | ret = -EINVAL; | |
4302 | } else if (block) { | |
e3dd7493 DDAG |
4303 | if (length != block->used_length) { |
4304 | Error *local_err = NULL; | |
56e93d26 | 4305 | |
fa53a0e5 | 4306 | ret = qemu_ram_resize(block, length, |
e3dd7493 DDAG |
4307 | &local_err); |
4308 | if (local_err) { | |
4309 | error_report_err(local_err); | |
56e93d26 | 4310 | } |
56e93d26 | 4311 | } |
ef08fb38 DDAG |
4312 | /* For postcopy we need to check hugepage sizes match */ |
4313 | if (postcopy_advised && | |
4314 | block->page_size != qemu_host_page_size) { | |
4315 | uint64_t remote_page_size = qemu_get_be64(f); | |
4316 | if (remote_page_size != block->page_size) { | |
4317 | error_report("Mismatched RAM page size %s " | |
4318 | "(local) %zd != %" PRId64, | |
4319 | id, block->page_size, | |
4320 | remote_page_size); | |
4321 | ret = -EINVAL; | |
4322 | } | |
4323 | } | |
fbd162e6 YK |
4324 | if (migrate_ignore_shared()) { |
4325 | hwaddr addr = qemu_get_be64(f); | |
4326 | bool ignored = qemu_get_byte(f); | |
4327 | if (ignored != ramblock_is_ignored(block)) { | |
4328 | error_report("RAM block %s should %s be migrated", | |
4329 | id, ignored ? "" : "not"); | |
4330 | ret = -EINVAL; | |
4331 | } | |
4332 | if (ramblock_is_ignored(block) && | |
4333 | block->mr->addr != addr) { | |
4334 | error_report("Mismatched GPAs for block %s " | |
4335 | "%" PRId64 "!= %" PRId64, | |
4336 | id, (uint64_t)addr, | |
4337 | (uint64_t)block->mr->addr); | |
4338 | ret = -EINVAL; | |
4339 | } | |
4340 | } | |
e3dd7493 DDAG |
4341 | ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG, |
4342 | block->idstr); | |
4343 | } else { | |
56e93d26 JQ |
4344 | error_report("Unknown ramblock \"%s\", cannot " |
4345 | "accept migration", id); | |
4346 | ret = -EINVAL; | |
4347 | } | |
4348 | ||
4349 | total_ram_bytes -= length; | |
4350 | } | |
4351 | break; | |
a776aa15 | 4352 | |
bb890ed5 | 4353 | case RAM_SAVE_FLAG_ZERO: |
56e93d26 JQ |
4354 | ch = qemu_get_byte(f); |
4355 | ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); | |
4356 | break; | |
a776aa15 | 4357 | |
56e93d26 | 4358 | case RAM_SAVE_FLAG_PAGE: |
56e93d26 JQ |
4359 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
4360 | break; | |
56e93d26 | 4361 | |
a776aa15 | 4362 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
56e93d26 JQ |
4363 | len = qemu_get_be32(f); |
4364 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { | |
4365 | error_report("Invalid compressed data length: %d", len); | |
4366 | ret = -EINVAL; | |
4367 | break; | |
4368 | } | |
c1bc6626 | 4369 | decompress_data_with_multi_threads(f, host, len); |
56e93d26 | 4370 | break; |
a776aa15 | 4371 | |
56e93d26 | 4372 | case RAM_SAVE_FLAG_XBZRLE: |
56e93d26 JQ |
4373 | if (load_xbzrle(f, addr, host) < 0) { |
4374 | error_report("Failed to decompress XBZRLE page at " | |
4375 | RAM_ADDR_FMT, addr); | |
4376 | ret = -EINVAL; | |
4377 | break; | |
4378 | } | |
4379 | break; | |
4380 | case RAM_SAVE_FLAG_EOS: | |
4381 | /* normal exit */ | |
6df264ac | 4382 | multifd_recv_sync_main(); |
56e93d26 JQ |
4383 | break; |
4384 | default: | |
4385 | if (flags & RAM_SAVE_FLAG_HOOK) { | |
632e3a5c | 4386 | ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL); |
56e93d26 JQ |
4387 | } else { |
4388 | error_report("Unknown combination of migration flags: %#x", | |
4389 | flags); | |
4390 | ret = -EINVAL; | |
4391 | } | |
4392 | } | |
4393 | if (!ret) { | |
4394 | ret = qemu_file_get_error(f); | |
4395 | } | |
4396 | } | |
4397 | ||
34ab9e97 | 4398 | ret |= wait_for_decompress_done(); |
56e93d26 | 4399 | rcu_read_unlock(); |
55c4446b | 4400 | trace_ram_load_complete(ret, seq_iter); |
e6f4aa18 ZC |
4401 | |
4402 | if (!ret && migration_incoming_in_colo_state()) { | |
4403 | colo_flush_ram_cache(); | |
4404 | } | |
56e93d26 JQ |
4405 | return ret; |
4406 | } | |
4407 | ||
c6467627 VSO |
4408 | static bool ram_has_postcopy(void *opaque) |
4409 | { | |
469dd51b | 4410 | RAMBlock *rb; |
fbd162e6 | 4411 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
469dd51b JH |
4412 | if (ramblock_is_pmem(rb)) { |
4413 | info_report("Block: %s, host: %p is a nvdimm memory, postcopy" | |
4414 | "is not supported now!", rb->idstr, rb->host); | |
4415 | return false; | |
4416 | } | |
4417 | } | |
4418 | ||
c6467627 VSO |
4419 | return migrate_postcopy_ram(); |
4420 | } | |
4421 | ||
edd090c7 PX |
4422 | /* Sync all the dirty bitmap with destination VM. */ |
4423 | static int ram_dirty_bitmap_sync_all(MigrationState *s, RAMState *rs) | |
4424 | { | |
4425 | RAMBlock *block; | |
4426 | QEMUFile *file = s->to_dst_file; | |
4427 | int ramblock_count = 0; | |
4428 | ||
4429 | trace_ram_dirty_bitmap_sync_start(); | |
4430 | ||
fbd162e6 | 4431 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
edd090c7 PX |
4432 | qemu_savevm_send_recv_bitmap(file, block->idstr); |
4433 | trace_ram_dirty_bitmap_request(block->idstr); | |
4434 | ramblock_count++; | |
4435 | } | |
4436 | ||
4437 | trace_ram_dirty_bitmap_sync_wait(); | |
4438 | ||
4439 | /* Wait until all the ramblocks' dirty bitmap synced */ | |
4440 | while (ramblock_count--) { | |
4441 | qemu_sem_wait(&s->rp_state.rp_sem); | |
4442 | } | |
4443 | ||
4444 | trace_ram_dirty_bitmap_sync_complete(); | |
4445 | ||
4446 | return 0; | |
4447 | } | |
4448 | ||
4449 | static void ram_dirty_bitmap_reload_notify(MigrationState *s) | |
4450 | { | |
4451 | qemu_sem_post(&s->rp_state.rp_sem); | |
4452 | } | |
4453 | ||
a335debb PX |
4454 | /* |
4455 | * Read the received bitmap, revert it as the initial dirty bitmap. | |
4456 | * This is only used when the postcopy migration is paused but wants | |
4457 | * to resume from a middle point. | |
4458 | */ | |
4459 | int ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *block) | |
4460 | { | |
4461 | int ret = -EINVAL; | |
4462 | QEMUFile *file = s->rp_state.from_dst_file; | |
4463 | unsigned long *le_bitmap, nbits = block->used_length >> TARGET_PAGE_BITS; | |
a725ef9f | 4464 | uint64_t local_size = DIV_ROUND_UP(nbits, 8); |
a335debb PX |
4465 | uint64_t size, end_mark; |
4466 | ||
4467 | trace_ram_dirty_bitmap_reload_begin(block->idstr); | |
4468 | ||
4469 | if (s->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
4470 | error_report("%s: incorrect state %s", __func__, | |
4471 | MigrationStatus_str(s->state)); | |
4472 | return -EINVAL; | |
4473 | } | |
4474 | ||
4475 | /* | |
4476 | * Note: see comments in ramblock_recv_bitmap_send() on why we | |
4477 | * need the endianess convertion, and the paddings. | |
4478 | */ | |
4479 | local_size = ROUND_UP(local_size, 8); | |
4480 | ||
4481 | /* Add paddings */ | |
4482 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); | |
4483 | ||
4484 | size = qemu_get_be64(file); | |
4485 | ||
4486 | /* The size of the bitmap should match with our ramblock */ | |
4487 | if (size != local_size) { | |
4488 | error_report("%s: ramblock '%s' bitmap size mismatch " | |
4489 | "(0x%"PRIx64" != 0x%"PRIx64")", __func__, | |
4490 | block->idstr, size, local_size); | |
4491 | ret = -EINVAL; | |
4492 | goto out; | |
4493 | } | |
4494 | ||
4495 | size = qemu_get_buffer(file, (uint8_t *)le_bitmap, local_size); | |
4496 | end_mark = qemu_get_be64(file); | |
4497 | ||
4498 | ret = qemu_file_get_error(file); | |
4499 | if (ret || size != local_size) { | |
4500 | error_report("%s: read bitmap failed for ramblock '%s': %d" | |
4501 | " (size 0x%"PRIx64", got: 0x%"PRIx64")", | |
4502 | __func__, block->idstr, ret, local_size, size); | |
4503 | ret = -EIO; | |
4504 | goto out; | |
4505 | } | |
4506 | ||
4507 | if (end_mark != RAMBLOCK_RECV_BITMAP_ENDING) { | |
4508 | error_report("%s: ramblock '%s' end mark incorrect: 0x%"PRIu64, | |
4509 | __func__, block->idstr, end_mark); | |
4510 | ret = -EINVAL; | |
4511 | goto out; | |
4512 | } | |
4513 | ||
4514 | /* | |
4515 | * Endianess convertion. We are during postcopy (though paused). | |
4516 | * The dirty bitmap won't change. We can directly modify it. | |
4517 | */ | |
4518 | bitmap_from_le(block->bmap, le_bitmap, nbits); | |
4519 | ||
4520 | /* | |
4521 | * What we received is "received bitmap". Revert it as the initial | |
4522 | * dirty bitmap for this ramblock. | |
4523 | */ | |
4524 | bitmap_complement(block->bmap, block->bmap, nbits); | |
4525 | ||
4526 | trace_ram_dirty_bitmap_reload_complete(block->idstr); | |
4527 | ||
edd090c7 PX |
4528 | /* |
4529 | * We succeeded to sync bitmap for current ramblock. If this is | |
4530 | * the last one to sync, we need to notify the main send thread. | |
4531 | */ | |
4532 | ram_dirty_bitmap_reload_notify(s); | |
4533 | ||
a335debb PX |
4534 | ret = 0; |
4535 | out: | |
bf269906 | 4536 | g_free(le_bitmap); |
a335debb PX |
4537 | return ret; |
4538 | } | |
4539 | ||
edd090c7 PX |
4540 | static int ram_resume_prepare(MigrationState *s, void *opaque) |
4541 | { | |
4542 | RAMState *rs = *(RAMState **)opaque; | |
08614f34 | 4543 | int ret; |
edd090c7 | 4544 | |
08614f34 PX |
4545 | ret = ram_dirty_bitmap_sync_all(s, rs); |
4546 | if (ret) { | |
4547 | return ret; | |
4548 | } | |
4549 | ||
4550 | ram_state_resume_prepare(rs, s->to_dst_file); | |
4551 | ||
4552 | return 0; | |
edd090c7 PX |
4553 | } |
4554 | ||
56e93d26 | 4555 | static SaveVMHandlers savevm_ram_handlers = { |
9907e842 | 4556 | .save_setup = ram_save_setup, |
56e93d26 | 4557 | .save_live_iterate = ram_save_iterate, |
763c906b | 4558 | .save_live_complete_postcopy = ram_save_complete, |
a3e06c3d | 4559 | .save_live_complete_precopy = ram_save_complete, |
c6467627 | 4560 | .has_postcopy = ram_has_postcopy, |
56e93d26 JQ |
4561 | .save_live_pending = ram_save_pending, |
4562 | .load_state = ram_load, | |
f265e0e4 JQ |
4563 | .save_cleanup = ram_save_cleanup, |
4564 | .load_setup = ram_load_setup, | |
4565 | .load_cleanup = ram_load_cleanup, | |
edd090c7 | 4566 | .resume_prepare = ram_resume_prepare, |
56e93d26 JQ |
4567 | }; |
4568 | ||
4569 | void ram_mig_init(void) | |
4570 | { | |
4571 | qemu_mutex_init(&XBZRLE.lock); | |
6f37bb8b | 4572 | register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, &ram_state); |
56e93d26 | 4573 | } |